Reverse the order for deleting the items in resetcontent to correctly
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "config.h"
23
24 #include <wine/debug.h>
25 #include <wine/library.h>
26
27 #include "winecfg.h"
28
29 #include <stdio.h>
30 #ifdef HAVE_MNTENT_H
31 #include <mntent.h>
32 #endif
33 #include <stdlib.h>
34 #include <errno.h>
35
36 #include <sys/stat.h>
37
38 #include <winbase.h>
39
40 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
41
42 BOOL gui_mode = TRUE;
43 static long working_mask = 0;
44
45 #ifdef HAVE_MNTENT_H
46 static char *ignored_fstypes[] = {
47     "devpts",
48     "tmpfs",
49     "proc",
50     "sysfs",
51     "swap",
52     "usbdevfs",
53     "rpc_pipefs",
54     NULL
55 };
56
57 static char *ignored_mnt_dirs[] = {
58     "/boot",
59     NULL
60 };
61
62 static BOOL should_ignore_fstype(char *type)
63 {
64     char **s;
65     
66     for (s = ignored_fstypes; *s; s++)
67         if (!strcmp(*s, type)) return TRUE;
68
69     return FALSE;
70 }
71
72 static BOOL should_ignore_mnt_dir(char *dir)
73 {
74     char **s;
75
76     for (s = ignored_mnt_dirs; *s; s++)
77         if (!strcmp(*s, dir)) return TRUE;
78
79     return FALSE;
80 }
81
82 static BOOL is_drive_defined(char *path)
83 {
84     int i;
85     
86     for (i = 0; i < 26; i++)
87         if (drives[i].in_use && !strcmp(drives[i].unixpath, path)) return TRUE;
88
89     return FALSE;
90 }
91
92 /* returns Z + 1 if there are no more available letters */
93 static char allocate_letter()
94 {
95     char letter;
96
97     for (letter = 'C'; letter <= 'Z'; letter++)
98         if ((DRIVE_MASK_BIT(letter) & working_mask) != 0) break;
99
100     return letter;
101 }
102 #endif
103
104 #define FSTAB_OPEN 1
105 #define NO_MORE_LETTERS 2
106 #define NO_ROOT 3
107 #define NO_DRIVE_C 4
108 #define NO_HOME 5
109
110 static void report_error(int code)
111 {
112     char *buffer;
113     int len;
114     
115     switch (code)
116     {
117         case FSTAB_OPEN:
118             if (gui_mode)
119             {
120                 static const char *s = "Could not open your mountpoint description table.\n\nOpening of /etc/fstab failed: %s";
121                 len = snprintf(NULL, 0, s, strerror(errno));
122                 buffer = HeapAlloc(GetProcessHeap(), 0, len + 1);
123                 snprintf(buffer, len, s, strerror(errno));
124                 MessageBox(NULL, s, "", MB_OK | MB_ICONEXCLAMATION);
125                 HeapFree(GetProcessHeap(), 0, buffer);
126             }
127             else
128             {
129                 fprintf(stderr, "winecfg: could not open fstab: %s\n", strerror(errno));
130             }
131             break;
132
133         case NO_MORE_LETTERS:
134             if (gui_mode) MessageBox(NULL, "No more letters are available to auto-detect available drives with.", "", MB_OK | MB_ICONEXCLAMATION);
135             fprintf(stderr, "winecfg: no more available letters while scanning /etc/fstab\n");
136             break;
137
138         case NO_ROOT:
139             if (gui_mode) MessageBox(NULL, "Could not ensure that the root directory was mapped.\n\n"
140                                      "This can happen if you run out of drive letters. "
141                                      "It's important to have the root directory mapped, otherwise Wine"
142                                      "will not be able to always find the programs you want to run. "
143                                      "Try unmapping a drive letter then trying again.", "",
144                                      MB_OK | MB_ICONEXCLAMATION);
145             else fprintf(stderr, "winecfg: unable to map root drive\n");
146             break;
147
148         case NO_DRIVE_C:
149             if (gui_mode)
150                 MessageBox(NULL, "No virtual drive C mapped\n\nTry running wineprefixcreate", "", MB_OK | MB_ICONEXCLAMATION);
151             else
152                 fprintf(stderr, "winecfg: no drive_c directory\n");
153
154         case NO_HOME:
155             if (gui_mode)
156                 MessageBox(NULL, "Could not ensure that your home directory was mapped.\n\n"
157                                  "This can happen if you run out of drive letters. "
158                                  "Try unmapping a drive letter then try again.", "",
159                                  MB_OK | MB_ICONEXCLAMATION);
160             else 
161                 fprintf(stderr, "winecfg: unable to map home drive\n");
162             break;
163     }
164 }
165
166 static void ensure_root_is_mapped()
167 {
168     int i;
169     BOOL mapped = FALSE;
170
171     for (i = 0; i < 26; i++)
172         if (drives[i].in_use && !strcmp(drives[i].unixpath, "/")) mapped = TRUE;
173
174     if (!mapped)
175     {
176         /* work backwards from Z, trying to map it */
177         char letter;
178
179         for (letter = 'Z'; letter >= 'A'; letter--)
180         {
181             if (!drives[letter - 'A'].in_use) 
182             {
183                 add_drive(letter, "/", "System", "0", DRIVE_FIXED);
184                 WINE_TRACE("allocated drive %c as the root drive\n", letter);
185                 break;
186             }
187         }
188
189         if (letter == ('A' - 1)) report_error(NO_ROOT);
190     }
191 }
192
193 static void ensure_home_is_mapped()
194 {
195     int i;
196     BOOL mapped = FALSE;
197     char *home = getenv("HOME");
198
199     if (!home) return;
200
201     for (i = 0; i < 26; i++)
202         if (drives[i].in_use && !strcmp(drives[i].unixpath, home)) mapped = TRUE;
203
204     if (!mapped)
205     {
206         char letter;
207
208         for (letter = 'H'; letter <= 'Z'; letter++)
209         {
210             if (!drives[letter - 'A'].in_use)
211             {
212                 add_drive(letter, home, "Home", "0", DRIVE_FIXED);
213                 WINE_TRACE("allocated drive %c as the user's home directory\n", letter);
214                 break;
215             }
216         }
217         if (letter == ('Z' + 1)) report_error(NO_HOME);
218     }        
219 }
220
221 static void ensure_drive_c_is_mapped()
222 {
223     struct stat buf;
224     const char *configdir = wine_get_config_dir();
225     int len;
226     char *drive_c_dir;
227     
228     if (drives[2].in_use) return;
229
230     len = snprintf(NULL, 0, "%s/../drive_c", configdir);
231     drive_c_dir = HeapAlloc(GetProcessHeap(), 0, len);
232     snprintf(drive_c_dir, len, "%s/../drive_c", configdir);
233     HeapFree(GetProcessHeap(), 0, drive_c_dir);
234
235     if (stat(drive_c_dir, &buf) == 0)
236     {
237         add_drive('C', "../drive_c", "Virtual Windows Drive", "0", DRIVE_FIXED);
238     }
239     else
240     {
241         report_error(NO_DRIVE_C);
242     }
243 }
244
245 int autodetect_drives()
246 {
247 #ifdef HAVE_MNTENT_H
248     struct mntent *ent;
249     FILE *fstab;
250 #endif
251
252     /* we want to build a list of autodetected drives, then ensure each entry
253        exists in the users setup. so, we superimpose the autodetected drives
254        onto whatever is pre-existing.
255
256        for now let's just rummage around inside the fstab.
257      */
258
259     load_drives();
260
261     working_mask = drive_available_mask('\0');
262
263 #ifdef HAVE_MNTENT_H
264     fstab = fopen("/etc/fstab", "r");
265     if (!fstab)
266     {
267         report_error(FSTAB_OPEN);
268         return FALSE;
269     }
270
271     while ((ent = getmntent(fstab)))
272     {
273         char letter;
274         char label[256];
275         int type;
276         
277         WINE_TRACE("ent->mnt_dir=%s\n", ent->mnt_dir);
278
279         if (should_ignore_fstype(ent->mnt_type)) continue;
280         if (should_ignore_mnt_dir(ent->mnt_dir)) continue;
281         if (is_drive_defined(ent->mnt_dir)) continue;
282
283         /* allocate a drive for it */
284         letter = allocate_letter();
285         if (letter == ']')
286         {
287             report_error(NO_MORE_LETTERS);
288             fclose(fstab);
289             return FALSE;
290         }
291             
292         WINE_TRACE("adding drive %c for %s, type %s\n", letter, ent->mnt_dir, ent->mnt_type);
293         
294         strncpy(label, "Drive X", 8);
295         label[6] = letter;
296
297         if (!strcmp(ent->mnt_type, "nfs")) type = DRIVE_REMOTE;
298         else if (!strcmp(ent->mnt_type, "nfs4")) type = DRIVE_REMOTE;
299         else if (!strcmp(ent->mnt_type, "smbfs")) type = DRIVE_REMOTE;
300         else if (!strcmp(ent->mnt_type, "cifs")) type = DRIVE_REMOTE;
301         else if (!strcmp(ent->mnt_type, "coda")) type = DRIVE_REMOTE;
302         else if (!strcmp(ent->mnt_type, "iso9660")) type = DRIVE_CDROM;
303         else if (!strcmp(ent->mnt_type, "ramfs")) type = DRIVE_RAMDISK;
304         else type = DRIVE_FIXED;
305         
306         add_drive(letter, ent->mnt_dir, label, "0", type);
307         
308         /* working_mask is a map of the drive letters still available. */
309         working_mask &= ~DRIVE_MASK_BIT(letter);
310     }
311
312     fclose(fstab);
313 #endif
314
315     ensure_root_is_mapped();
316
317     ensure_drive_c_is_mapped();
318
319     ensure_home_is_mapped();
320
321     return TRUE;
322 }