mlang: Implement DllCanUnloadNow.
[wine/gsoc_dplay.git] / programs / winecfg / drivedetect.c
blob7c3cf3d3f811b75a6cc8bb1926dd81516d4d5e54
1 /*
2 * Drive autodetection code
4 * Copyright 2004 Mike Hearn
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.
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.
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
22 #include "config.h"
23 #include "wine/port.h"
25 #include <wine/debug.h>
26 #include <wine/library.h>
28 #include "winecfg.h"
30 #include <stdio.h>
31 #ifdef HAVE_MNTENT_H
32 #include <mntent.h>
33 #endif
34 #include <stdlib.h>
35 #include <errno.h>
37 #include <sys/stat.h>
39 #include <winbase.h>
41 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
43 BOOL gui_mode = TRUE;
44 static long working_mask = 0;
46 #ifdef HAVE_MNTENT_H
48 static const DEV_NODES sDeviceNodes[] = {
49 {"/dev/fd", DRIVE_REMOVABLE},
50 {"/dev/cdrom", DRIVE_CDROM},
51 {"",0}
54 static const char * const ignored_fstypes[] = {
55 "devpts",
56 "tmpfs",
57 "proc",
58 "sysfs",
59 "swap",
60 "usbdevfs",
61 "rpc_pipefs",
62 "binfmt_misc",
63 NULL
66 static const char * const ignored_mnt_dirs[] = {
67 "/boot",
68 NULL
71 static int try_dev_node(char *dev)
73 const DEV_NODES *pDevNodes = sDeviceNodes;
75 while(pDevNodes->szNode[0])
77 if(!strncmp(dev,pDevNodes->szNode,strlen(pDevNodes->szNode)))
78 return pDevNodes->nType;
79 ++pDevNodes;
82 return DRIVE_FIXED;
85 static BOOL should_ignore_fstype(char *type)
87 const char * const *s;
89 for (s = ignored_fstypes; *s; s++)
90 if (!strcmp(*s, type)) return TRUE;
92 return FALSE;
95 static BOOL should_ignore_mnt_dir(char *dir)
97 const char * const *s;
99 for (s = ignored_mnt_dirs; *s; s++)
100 if (!strcmp(*s, dir)) return TRUE;
102 return FALSE;
105 static BOOL is_drive_defined(char *path)
107 int i;
109 for (i = 0; i < 26; i++)
110 if (drives[i].in_use && !strcmp(drives[i].unixpath, path)) return TRUE;
112 return FALSE;
115 /* returns Z + 1 if there are no more available letters */
116 static char allocate_letter(void)
118 char letter;
120 for (letter = 'C'; letter <= 'Z'; letter++)
121 if ((DRIVE_MASK_BIT(letter) & working_mask) != 0) break;
123 return letter;
125 #endif
127 #define FSTAB_OPEN 1
128 #define NO_MORE_LETTERS 2
129 #define NO_ROOT 3
130 #define NO_DRIVE_C 4
131 #define NO_HOME 5
133 static void report_error(int code)
135 char *buffer;
136 int len;
138 switch (code)
140 case FSTAB_OPEN:
141 if (gui_mode)
143 static const char s[] = "Could not open your mountpoint description table.\n\nOpening of /etc/fstab failed: %s";
144 len = snprintf(NULL, 0, s, strerror(errno));
145 buffer = HeapAlloc(GetProcessHeap(), 0, len + 1);
146 snprintf(buffer, len, s, strerror(errno));
147 MessageBox(NULL, s, "", MB_OK | MB_ICONEXCLAMATION);
148 HeapFree(GetProcessHeap(), 0, buffer);
150 else
152 fprintf(stderr, "winecfg: could not open fstab: %s\n", strerror(errno));
154 break;
156 case NO_MORE_LETTERS:
157 if (gui_mode) MessageBox(NULL, "No more letters are available to auto-detect available drives with.", "", MB_OK | MB_ICONEXCLAMATION);
158 fprintf(stderr, "winecfg: no more available letters while scanning /etc/fstab\n");
159 break;
161 case NO_ROOT:
162 if (gui_mode) MessageBox(NULL, "Could not ensure that the root directory was mapped.\n\n"
163 "This can happen if you run out of drive letters. "
164 "It's important to have the root directory mapped, otherwise Wine"
165 "will not be able to always find the programs you want to run. "
166 "Try unmapping a drive letter then trying again.", "",
167 MB_OK | MB_ICONEXCLAMATION);
168 else fprintf(stderr, "winecfg: unable to map root drive\n");
169 break;
171 case NO_DRIVE_C:
172 if (gui_mode)
173 MessageBox(NULL, "No virtual drive C mapped\n\nTry running wineprefixcreate", "", MB_OK | MB_ICONEXCLAMATION);
174 else
175 fprintf(stderr, "winecfg: no drive_c directory\n");
177 case NO_HOME:
178 if (gui_mode)
179 MessageBox(NULL, "Could not ensure that your home directory was mapped.\n\n"
180 "This can happen if you run out of drive letters. "
181 "Try unmapping a drive letter then try again.", "",
182 MB_OK | MB_ICONEXCLAMATION);
183 else
184 fprintf(stderr, "winecfg: unable to map home drive\n");
185 break;
189 static void ensure_root_is_mapped(void)
191 int i;
192 BOOL mapped = FALSE;
194 for (i = 0; i < 26; i++)
195 if (drives[i].in_use && !strcmp(drives[i].unixpath, "/")) mapped = TRUE;
197 if (!mapped)
199 /* work backwards from Z, trying to map it */
200 char letter;
202 for (letter = 'Z'; letter >= 'A'; letter--)
204 if (!drives[letter - 'A'].in_use)
206 add_drive(letter, "/", "System", "0", DRIVE_FIXED);
207 WINE_TRACE("allocated drive %c as the root drive\n", letter);
208 break;
212 if (letter == ('A' - 1)) report_error(NO_ROOT);
216 static void ensure_home_is_mapped(void)
218 int i;
219 BOOL mapped = FALSE;
220 char *home = getenv("HOME");
222 if (!home) return;
224 for (i = 0; i < 26; i++)
225 if (drives[i].in_use && !strcmp(drives[i].unixpath, home)) mapped = TRUE;
227 if (!mapped)
229 char letter;
231 for (letter = 'H'; letter <= 'Z'; letter++)
233 if (!drives[letter - 'A'].in_use)
235 add_drive(letter, home, "Home", "0", DRIVE_FIXED);
236 WINE_TRACE("allocated drive %c as the user's home directory\n", letter);
237 break;
240 if (letter == ('Z' + 1)) report_error(NO_HOME);
244 static void ensure_drive_c_is_mapped(void)
246 struct stat buf;
247 const char *configdir = wine_get_config_dir();
248 int len;
249 char *drive_c_dir;
251 if (drives[2].in_use) return;
253 len = snprintf(NULL, 0, "%s/../drive_c", configdir);
254 drive_c_dir = HeapAlloc(GetProcessHeap(), 0, len);
255 snprintf(drive_c_dir, len, "%s/../drive_c", configdir);
256 HeapFree(GetProcessHeap(), 0, drive_c_dir);
258 if (stat(drive_c_dir, &buf) == 0)
260 add_drive('C', "../drive_c", "Virtual Windows Drive", "0", DRIVE_FIXED);
262 else
264 report_error(NO_DRIVE_C);
268 int autodetect_drives()
270 #ifdef HAVE_MNTENT_H
271 struct mntent *ent;
272 FILE *fstab;
273 #endif
275 /* we want to build a list of autodetected drives, then ensure each entry
276 exists in the users setup. so, we superimpose the autodetected drives
277 onto whatever is pre-existing.
279 for now let's just rummage around inside the fstab.
282 load_drives();
284 working_mask = drive_available_mask('\0');
286 #ifdef HAVE_MNTENT_H
287 fstab = fopen("/etc/fstab", "r");
288 if (!fstab)
290 report_error(FSTAB_OPEN);
291 return FALSE;
294 while ((ent = getmntent(fstab)))
296 char letter;
297 char label[256];
298 int type;
300 WINE_TRACE("ent->mnt_dir=%s\n", ent->mnt_dir);
302 if (should_ignore_fstype(ent->mnt_type)) continue;
303 if (should_ignore_mnt_dir(ent->mnt_dir)) continue;
304 if (is_drive_defined(ent->mnt_dir)) continue;
306 /* allocate a drive for it */
307 letter = allocate_letter();
308 if (letter == ']')
310 report_error(NO_MORE_LETTERS);
311 fclose(fstab);
312 return FALSE;
315 strcpy(label, "Drive X");
316 label[6] = letter;
318 WINE_TRACE("adding drive %c for %s, type %s with label %s\n", letter, ent->mnt_dir, ent->mnt_type,label);
320 if (!strcmp(ent->mnt_type, "nfs")) type = DRIVE_REMOTE;
321 else if (!strcmp(ent->mnt_type, "nfs4")) type = DRIVE_REMOTE;
322 else if (!strcmp(ent->mnt_type, "smbfs")) type = DRIVE_REMOTE;
323 else if (!strcmp(ent->mnt_type, "cifs")) type = DRIVE_REMOTE;
324 else if (!strcmp(ent->mnt_type, "coda")) type = DRIVE_REMOTE;
325 else if (!strcmp(ent->mnt_type, "iso9660")) type = DRIVE_CDROM;
326 else if (!strcmp(ent->mnt_type, "ramfs")) type = DRIVE_RAMDISK;
327 else type = try_dev_node(ent->mnt_fsname);
329 add_drive(letter, ent->mnt_dir, label, "0", type);
331 /* working_mask is a map of the drive letters still available. */
332 working_mask &= ~DRIVE_MASK_BIT(letter);
335 fclose(fstab);
336 #endif
338 ensure_root_is_mapped();
340 ensure_drive_c_is_mapped();
342 ensure_home_is_mapped();
344 return TRUE;