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