Fixed automatic drive detection for the case that the root directory
[wine/wine64.git] / programs / winecfg / drivedetect.c
blob4583e2f73c3d14d9b8b6e86a42c377e4173864ee
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 <wine/debug.h>
23 #include <wine/library.h>
25 #include "winecfg.h"
27 #include <stdio.h>
28 #ifdef HAVE_MNTENT_H
29 #include <mntent.h>
30 #endif
31 #include <stdlib.h>
32 #include <errno.h>
34 #include <sys/stat.h>
36 #include <winbase.h>
38 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
40 BOOL gui_mode = TRUE;
41 static long working_mask = 0;
43 #ifdef HAVE_MNTENT_H
44 static char *ignored_fstypes[] = {
45 "devpts",
46 "tmpfs",
47 "proc",
48 "sysfs",
49 "swap",
50 "usbdevfs",
51 "rpc_pipefs",
52 NULL
55 static BOOL should_ignore_fstype(char *type)
57 char **s;
59 for (s = ignored_fstypes; *s; s++)
60 if (!strcmp(*s, type)) return TRUE;
62 return FALSE;
65 static BOOL is_drive_defined(char *path)
67 int i;
69 for (i = 0; i < 26; i++)
70 if (drives[i].in_use && !strcmp(drives[i].unixpath, path)) return TRUE;
72 return FALSE;
75 /* returns Z + 1 if there are no more available letters */
76 static char allocate_letter()
78 char letter;
80 for (letter = 'C'; letter <= 'Z'; letter++)
81 if ((DRIVE_MASK_BIT(letter) & working_mask) != 0) break;
83 return letter;
85 #endif
87 #define FSTAB_OPEN 1
88 #define NO_MORE_LETTERS 2
89 #define NO_ROOT 3
90 #define NO_DRIVE_C 4
92 static void report_error(int code)
94 char *buffer;
95 int len;
97 switch (code)
99 case FSTAB_OPEN:
100 if (gui_mode)
102 static const char *s = "Could not open your mountpoint description table.\n\nOpening of /etc/fstab failed: %s";
103 len = snprintf(NULL, 0, s, strerror(errno));
104 buffer = HeapAlloc(GetProcessHeap(), 0, len + 1);
105 snprintf(buffer, len, s, strerror(errno));
106 MessageBox(NULL, s, "", MB_OK | MB_ICONEXCLAMATION);
107 HeapFree(GetProcessHeap(), 0, buffer);
109 else
111 fprintf(stderr, "winecfg: could not open fstab: %s", strerror(errno));
113 break;
115 case NO_MORE_LETTERS:
116 if (gui_mode) MessageBox(NULL, "No more letters are available to auto-detect available drives with.", "", MB_OK | MB_ICONEXCLAMATION);
117 fprintf(stderr, "winecfg: no more available letters while scanning /etc/fstab");
118 break;
120 case NO_ROOT:
121 if (gui_mode) MessageBox(NULL, "Could not ensure that the root directory was mapped.\n\n"
122 "This can happen if you run out of drive letters. "
123 "It's important to have the root directory mapped, otherwise Wine"
124 "will not be able to always find the programs you want to run. "
125 "Try unmapping a drive letter then trying again.", "",
126 MB_OK | MB_ICONEXCLAMATION);
127 else fprintf(stderr, "winecfg: unable to map root drive\n");
128 break;
130 case NO_DRIVE_C:
131 if (gui_mode)
132 MessageBox(NULL, "No virtual drive C mapped\n\nTry running wineprefixcreate", "", MB_OK | MB_ICONEXCLAMATION);
133 else
134 fprintf(stderr, "winecfg: no drive_c directory\n");
139 static void ensure_root_is_mapped()
141 int i;
142 BOOL mapped = FALSE;
144 for (i = 0; i < 26; i++)
145 if (drives[i].in_use && !strcmp(drives[i].unixpath, "/")) mapped = TRUE;
147 if (!mapped)
149 /* work backwards from Z, trying to map it */
150 char letter;
152 for (letter = 'Z'; letter >= 'A'; letter--)
154 if (!drives[letter - 'A'].in_use)
156 add_drive(letter, "/", "System", "0", DRIVE_FIXED);
157 WINE_TRACE("allocated drive %c as the root drive\n", letter);
158 break;
162 if (letter == ('A' - 1)) report_error(NO_ROOT);
166 static void ensure_drive_c_is_mapped()
168 struct stat buf;
169 const char *configdir = wine_get_config_dir();
170 int len;
171 char *drive_c_dir;
173 if (drives[2].in_use) return;
175 len = snprintf(NULL, 0, "%s/../drive_c", configdir);
176 drive_c_dir = HeapAlloc(GetProcessHeap(), 0, len);
177 snprintf(drive_c_dir, len, "%s/../drive_c", configdir);
178 HeapFree(GetProcessHeap(), 0, drive_c_dir);
180 if (stat(drive_c_dir, &buf) == 0)
182 add_drive('C', "../drive_c", "Virtual Windows Drive", "0", DRIVE_FIXED);
184 else
186 report_error(NO_DRIVE_C);
190 int autodetect_drives()
192 #ifdef HAVE_MNTENT_H
193 struct mntent *ent;
194 FILE *fstab;
195 #endif
197 /* we want to build a list of autodetected drives, then ensure each entry
198 exists in the users setup. so, we superimpose the autodetected drives
199 onto whatever is pre-existing.
201 for now let's just rummage around inside the fstab.
204 load_drives();
206 working_mask = drive_available_mask('\0');
208 #ifdef HAVE_MNTENT_H
209 fstab = fopen("/etc/fstab", "r");
210 if (!fstab)
212 report_error(FSTAB_OPEN);
213 return FALSE;
216 while ((ent = getmntent(fstab)))
218 char letter;
219 char label[256];
220 int type;
222 WINE_TRACE("ent->mnt_dir=%s\n", ent->mnt_dir);
224 if (should_ignore_fstype(ent->mnt_type)) continue;
225 if (is_drive_defined(ent->mnt_dir)) continue;
227 /* allocate a drive for it */
228 letter = allocate_letter();
229 if (letter == ']')
231 report_error(NO_MORE_LETTERS);
232 fclose(fstab);
233 return FALSE;
236 WINE_TRACE("adding drive %c for %s, type %s\n", letter, ent->mnt_dir, ent->mnt_type);
238 strncpy(label, "Drive X", 8);
239 label[6] = letter;
241 if (!strcmp(ent->mnt_type, "nfs")) type = DRIVE_REMOTE;
242 else if (!strcmp(ent->mnt_type, "nfs4")) type = DRIVE_REMOTE;
243 else if (!strcmp(ent->mnt_type, "smbfs")) type = DRIVE_REMOTE;
244 else if (!strcmp(ent->mnt_type, "cifs")) type = DRIVE_REMOTE;
245 else if (!strcmp(ent->mnt_type, "coda")) type = DRIVE_REMOTE;
246 else if (!strcmp(ent->mnt_type, "iso9660")) type = DRIVE_CDROM;
247 else if (!strcmp(ent->mnt_type, "ramfs")) type = DRIVE_RAMDISK;
248 else type = DRIVE_FIXED;
250 add_drive(letter, ent->mnt_dir, label, "0", type);
252 working_mask |= DRIVE_MASK_BIT(letter);
255 fclose(fstab);
256 #endif
258 ensure_root_is_mapped();
260 ensure_drive_c_is_mapped();
262 return TRUE;