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