winecfg: Update Norwegian resource.
[wine] / programs / winecfg / drivedetect.c
1 /*
2  * Drive autodetection code
3  *
4  * Copyright 2004 Mike Hearn
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <wine/debug.h>
26 #include <wine/library.h>
27
28 #include "winecfg.h"
29
30 #include <stdio.h>
31 #ifdef HAVE_MNTENT_H
32 #include <mntent.h>
33 #endif
34 #include <stdlib.h>
35 #include <errno.h>
36
37 #include <sys/stat.h>
38
39 #include <winbase.h>
40
41 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
42
43 BOOL gui_mode = TRUE;
44 static long working_mask = 0;
45
46 #ifdef HAVE_MNTENT_H
47
48 static const DEV_NODES sDeviceNodes[] = {
49   {"/dev/fd", DRIVE_REMOVABLE},
50   {"/dev/pf", DRIVE_REMOVABLE},
51   {"/dev/aztcd", DRIVE_CDROM},
52   {"/dev/bpcd", DRIVE_CDROM},
53   {"/dev/cdrom", DRIVE_CDROM},
54   {"/dev/cdu535", DRIVE_CDROM},
55   {"/dev/cdwriter", DRIVE_CDROM},
56   {"/dev/cm205cd", DRIVE_CDROM},
57   {"/dev/cm206cd", DRIVE_CDROM},
58   {"/dev/gscd", DRIVE_CDROM},
59   {"/dev/hitcd", DRIVE_CDROM},
60   {"/dev/iseries/vcd", DRIVE_CDROM},
61   {"/dev/lmscd", DRIVE_CDROM},
62   {"/dev/mcd", DRIVE_CDROM},
63   {"/dev/optcd", DRIVE_CDROM},
64   {"/dev/pcd", DRIVE_CDROM},
65   {"/dev/sbpcd", DRIVE_CDROM},
66   {"/dev/scd", DRIVE_CDROM},
67   {"/dev/sjcd", DRIVE_CDROM},
68   {"/dev/sonycd", DRIVE_CDROM},
69   {"/dev/sr", DRIVE_CDROM},
70   {"",0}
71 };
72
73 static const char * const ignored_fstypes[] = {
74     "devpts",
75     "tmpfs",
76     "proc",
77     "sysfs",
78     "swap",
79     "usbdevfs",
80     "rpc_pipefs",
81     "binfmt_misc",
82     NULL
83 };
84
85 static const char * const ignored_mnt_dirs[] = {
86     "/boot",
87     NULL
88 };
89
90 static int try_dev_node(char *dev)
91 {
92     const DEV_NODES *pDevNodes = sDeviceNodes;
93     
94     while(pDevNodes->szNode[0])
95     {
96         if(!strncmp(dev,pDevNodes->szNode,strlen(pDevNodes->szNode)))
97             return pDevNodes->nType;
98         ++pDevNodes;
99     }
100     
101     return DRIVE_FIXED;
102 }
103
104 static BOOL should_ignore_fstype(char *type)
105 {
106     const char * const *s;
107     
108     for (s = ignored_fstypes; *s; s++)
109         if (!strcmp(*s, type)) return TRUE;
110
111     return FALSE;
112 }
113
114 static BOOL should_ignore_mnt_dir(char *dir)
115 {
116     const char * const *s;
117
118     for (s = ignored_mnt_dirs; *s; s++)
119         if (!strcmp(*s, dir)) return TRUE;
120
121     return FALSE;
122 }
123
124 static BOOL is_drive_defined(char *path)
125 {
126     int i;
127     
128     for (i = 0; i < 26; i++)
129         if (drives[i].in_use && !strcmp(drives[i].unixpath, path)) return TRUE;
130
131     return FALSE;
132 }
133
134 /* returns Z + 1 if there are no more available letters */
135 static char allocate_letter(int type)
136 {
137     char letter, start;
138
139     if (type == DRIVE_REMOVABLE)
140         start = 'A';
141     else
142         start = 'C';
143
144     for (letter = start; letter <= 'Z'; letter++)
145         if ((DRIVE_MASK_BIT(letter) & working_mask) != 0) break;
146
147     return letter;
148 }
149 #endif
150
151 #define FSTAB_OPEN 1
152 #define NO_MORE_LETTERS 2
153 #define NO_ROOT 3
154 #define NO_DRIVE_C 4
155 #define NO_HOME 5
156
157 static void report_error(int code)
158 {
159     char *buffer;
160     int len;
161     
162     switch (code)
163     {
164         case FSTAB_OPEN:
165             if (gui_mode)
166             {
167                 static const char s[] = "Could not open your mountpoint description table.\n\nOpening of /etc/fstab failed: %s";
168                 len = snprintf(NULL, 0, s, strerror(errno));
169                 buffer = HeapAlloc(GetProcessHeap(), 0, len + 1);
170                 snprintf(buffer, len, s, strerror(errno));
171                 MessageBox(NULL, s, "", MB_OK | MB_ICONEXCLAMATION);
172                 HeapFree(GetProcessHeap(), 0, buffer);
173             }
174             else
175             {
176                 fprintf(stderr, "winecfg: could not open fstab: %s\n", strerror(errno));
177             }
178             break;
179
180         case NO_MORE_LETTERS:
181             if (gui_mode) MessageBox(NULL, "No more letters are available to auto-detect available drives with.", "", MB_OK | MB_ICONEXCLAMATION);
182             fprintf(stderr, "winecfg: no more available letters while scanning /etc/fstab\n");
183             break;
184
185         case NO_ROOT:
186             if (gui_mode) MessageBox(NULL, "Could not ensure that the root directory was mapped.\n\n"
187                                      "This can happen if you run out of drive letters. "
188                                      "It's important to have the root directory mapped, otherwise Wine"
189                                      "will not be able to always find the programs you want to run. "
190                                      "Try unmapping a drive letter then trying again.", "",
191                                      MB_OK | MB_ICONEXCLAMATION);
192             else fprintf(stderr, "winecfg: unable to map root drive\n");
193             break;
194
195         case NO_DRIVE_C:
196             if (gui_mode)
197                 MessageBox(NULL, "No virtual drive C mapped\n\nTry running wineprefixcreate", "", MB_OK | MB_ICONEXCLAMATION);
198             else
199                 fprintf(stderr, "winecfg: no drive_c directory\n");
200
201         case NO_HOME:
202             if (gui_mode)
203                 MessageBox(NULL, "Could not ensure that your home directory was mapped.\n\n"
204                                  "This can happen if you run out of drive letters. "
205                                  "Try unmapping a drive letter then try again.", "",
206                                  MB_OK | MB_ICONEXCLAMATION);
207             else 
208                 fprintf(stderr, "winecfg: unable to map home drive\n");
209             break;
210     }
211 }
212
213 static void ensure_root_is_mapped(void)
214 {
215     int i;
216     BOOL mapped = FALSE;
217
218     for (i = 0; i < 26; i++)
219         if (drives[i].in_use && !strcmp(drives[i].unixpath, "/")) mapped = TRUE;
220
221     if (!mapped)
222     {
223         /* work backwards from Z, trying to map it */
224         char letter;
225
226         for (letter = 'Z'; letter >= 'A'; letter--)
227         {
228             if (!drives[letter - 'A'].in_use) 
229             {
230                 add_drive(letter, "/", "System", "0", DRIVE_FIXED);
231                 WINE_TRACE("allocated drive %c as the root drive\n", letter);
232                 break;
233             }
234         }
235
236         if (letter == ('A' - 1)) report_error(NO_ROOT);
237     }
238 }
239
240 static void ensure_home_is_mapped(void)
241 {
242     int i;
243     BOOL mapped = FALSE;
244     char *home = getenv("HOME");
245
246     if (!home) return;
247
248     for (i = 0; i < 26; i++)
249         if (drives[i].in_use && !strcmp(drives[i].unixpath, home)) mapped = TRUE;
250
251     if (!mapped)
252     {
253         char letter;
254
255         for (letter = 'H'; letter <= 'Z'; letter++)
256         {
257             if (!drives[letter - 'A'].in_use)
258             {
259                 add_drive(letter, home, "Home", "0", DRIVE_FIXED);
260                 WINE_TRACE("allocated drive %c as the user's home directory\n", letter);
261                 break;
262             }
263         }
264         if (letter == ('Z' + 1)) report_error(NO_HOME);
265     }        
266 }
267
268 static void ensure_drive_c_is_mapped(void)
269 {
270     struct stat buf;
271     const char *configdir = wine_get_config_dir();
272     int len;
273     char *drive_c_dir;
274     
275     if (drives[2].in_use) return;
276
277     len = snprintf(NULL, 0, "%s/../drive_c", configdir);
278     drive_c_dir = HeapAlloc(GetProcessHeap(), 0, len);
279     snprintf(drive_c_dir, len, "%s/../drive_c", configdir);
280     HeapFree(GetProcessHeap(), 0, drive_c_dir);
281
282     if (stat(drive_c_dir, &buf) == 0)
283     {
284         add_drive('C', "../drive_c", "Virtual Windows Drive", "0", DRIVE_FIXED);
285     }
286     else
287     {
288         report_error(NO_DRIVE_C);
289     }
290 }
291
292 int autodetect_drives(void)
293 {
294 #ifdef HAVE_MNTENT_H
295     struct mntent *ent;
296     FILE *fstab;
297 #endif
298
299     /* we want to build a list of autodetected drives, then ensure each entry
300        exists in the users setup. so, we superimpose the autodetected drives
301        onto whatever is pre-existing.
302
303        for now let's just rummage around inside the fstab.
304      */
305
306     load_drives();
307
308     working_mask = drive_available_mask('\0');
309
310 #ifdef HAVE_MNTENT_H
311     fstab = fopen("/etc/fstab", "r");
312     if (!fstab)
313     {
314         report_error(FSTAB_OPEN);
315         return FALSE;
316     }
317
318     while ((ent = getmntent(fstab)))
319     {
320         char letter;
321         char label[256];
322         int type;
323         
324         WINE_TRACE("ent->mnt_dir=%s\n", ent->mnt_dir);
325
326         if (should_ignore_fstype(ent->mnt_type)) continue;
327         if (should_ignore_mnt_dir(ent->mnt_dir)) continue;
328         if (is_drive_defined(ent->mnt_dir)) continue;
329
330         if (!strcmp(ent->mnt_type, "nfs")) type = DRIVE_REMOTE;
331         else if (!strcmp(ent->mnt_type, "nfs4")) type = DRIVE_REMOTE;
332         else if (!strcmp(ent->mnt_type, "smbfs")) type = DRIVE_REMOTE;
333         else if (!strcmp(ent->mnt_type, "cifs")) type = DRIVE_REMOTE;
334         else if (!strcmp(ent->mnt_type, "coda")) type = DRIVE_REMOTE;
335         else if (!strcmp(ent->mnt_type, "iso9660")) type = DRIVE_CDROM;
336         else if (!strcmp(ent->mnt_type, "ramfs")) type = DRIVE_RAMDISK;
337         else type = try_dev_node(ent->mnt_fsname);
338         
339         /* allocate a drive for it */
340         letter = allocate_letter(type);
341         if (letter == ']')
342         {
343             report_error(NO_MORE_LETTERS);
344             fclose(fstab);
345             return FALSE;
346         }
347         
348         strcpy(label, "Drive X");
349         label[6] = letter;
350         
351         WINE_TRACE("adding drive %c for %s, type %s with label %s\n", letter, ent->mnt_dir, ent->mnt_type,label);
352
353         add_drive(letter, ent->mnt_dir, label, "0", type);
354         
355         /* working_mask is a map of the drive letters still available. */
356         working_mask &= ~DRIVE_MASK_BIT(letter);
357     }
358
359     fclose(fstab);
360 #endif
361
362     ensure_root_is_mapped();
363
364     ensure_drive_c_is_mapped();
365
366     ensure_home_is_mapped();
367
368     return TRUE;
369 }