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