Set the flags for GetVolumeInformation32A.
[wine] / files / drive.c
1 /*
2  * DOS drives handling functions
3  *
4  * Copyright 1993 Erik Bos
5  * Copyright 1996 Alexandre Julliard
6  */
7
8 #include "config.h"
9
10 #include <assert.h>
11 #include <ctype.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <fcntl.h>
17 #include <errno.h>
18 #include <unistd.h>
19
20 #ifdef HAVE_SYS_PARAM_H
21 # include <sys/param.h>
22 #endif
23 #ifdef STATFS_DEFINED_BY_SYS_VFS
24 # include <sys/vfs.h>
25 #else
26 # ifdef STATFS_DEFINED_BY_SYS_MOUNT
27 #  include <sys/mount.h>
28 # else
29 #  ifdef STATFS_DEFINED_BY_SYS_STATFS
30 #   include <sys/statfs.h>
31 #  endif
32 # endif
33 #endif
34
35 #include "windows.h"
36 #include "winbase.h"
37 #include "winerror.h"
38 #include "drive.h"
39 #include "file.h"
40 #include "heap.h"
41 #include "msdos.h"
42 #include "options.h"
43 #include "task.h"
44 #include "debug.h"
45
46 typedef struct
47 {
48     char     *root;      /* root dir in Unix format without trailing / */
49     char     *dos_cwd;   /* cwd in DOS format without leading or trailing \ */
50     char     *unix_cwd;  /* cwd in Unix format without leading or trailing / */
51     char     *device;    /* raw device path */
52     char      label[12]; /* drive label */
53     DWORD     serial;    /* drive serial number */
54     DRIVETYPE type;      /* drive type */
55     UINT32    flags;     /* drive flags */
56     dev_t     dev;       /* unix device number */
57     ino_t     ino;       /* unix inode number */
58 } DOSDRIVE;
59
60
61 static const char * const DRIVE_Types[] =
62 {
63     "floppy",   /* TYPE_FLOPPY */
64     "hd",       /* TYPE_HD */
65     "cdrom",    /* TYPE_CDROM */
66     "network"   /* TYPE_NETWORK */
67 };
68
69
70 /* Known filesystem types */
71
72 typedef struct
73 {
74     const char *name;
75     UINT32      flags;
76 } FS_DESCR;
77
78 static const FS_DESCR DRIVE_Filesystems[] =
79 {
80     { "unix",   DRIVE_CASE_SENSITIVE | DRIVE_CASE_PRESERVING },
81     { "msdos",  DRIVE_SHORT_NAMES },
82     { "dos",    DRIVE_SHORT_NAMES },
83     { "fat",    DRIVE_SHORT_NAMES },
84     { "vfat",   DRIVE_CASE_PRESERVING },
85     { "win95",  DRIVE_CASE_PRESERVING },
86     { NULL, 0 }
87 };
88
89
90 static DOSDRIVE DOSDrives[MAX_DOS_DRIVES];
91 static int DRIVE_CurDrive = -1;
92
93 static HTASK16 DRIVE_LastTask = 0;
94
95
96 /***********************************************************************
97  *           DRIVE_GetDriveType
98  */
99 static DRIVETYPE DRIVE_GetDriveType( const char *name )
100 {
101     char buffer[20];
102     int i;
103
104     PROFILE_GetWineIniString( name, "Type", "hd", buffer, sizeof(buffer) );
105     for (i = 0; i < sizeof(DRIVE_Types)/sizeof(DRIVE_Types[0]); i++)
106     {
107         if (!strcasecmp( buffer, DRIVE_Types[i] )) return (DRIVETYPE)i;
108     }
109     MSG("%s: unknown type '%s', defaulting to 'hd'.\n", name, buffer );
110     return TYPE_HD;
111 }
112
113
114 /***********************************************************************
115  *           DRIVE_GetFSFlags
116  */
117 static UINT32 DRIVE_GetFSFlags( const char *name, const char *value )
118 {
119     const FS_DESCR *descr;
120
121     for (descr = DRIVE_Filesystems; descr->name; descr++)
122         if (!strcasecmp( value, descr->name )) return descr->flags;
123     MSG("%s: unknown filesystem type '%s', defaulting to 'win95'.\n",
124         name, value );
125     return DRIVE_CASE_PRESERVING;
126 }
127
128
129 /***********************************************************************
130  *           DRIVE_Init
131  */
132 int DRIVE_Init(void)
133 {
134     int i, len, count = 0;
135     char name[] = "Drive A";
136     char path[MAX_PATHNAME_LEN];
137     char buffer[80];
138     struct stat drive_stat_buffer;
139     char *p;
140     DOSDRIVE *drive;
141
142     for (i = 0, drive = DOSDrives; i < MAX_DOS_DRIVES; i++, name[6]++, drive++)
143     {
144         PROFILE_GetWineIniString( name, "Path", "", path, sizeof(path)-1 );
145         if (path[0])
146         {
147             p = path + strlen(path) - 1;
148             while ((p > path) && ((*p == '/') || (*p == '\\'))) *p-- = '\0';
149             if (!path[0]) strcpy( path, "/" );
150
151             if (stat( path, &drive_stat_buffer ))
152             {
153                 MSG("Could not stat %s, ignoring drive %c:\n", path, 'A' + i );
154                 continue;
155             }
156             if (!S_ISDIR(drive_stat_buffer.st_mode))
157             {
158                 MSG("%s is not a directory, ignoring drive %c:\n",
159                     path, 'A' + i );
160                 continue;
161             }
162
163             drive->root = HEAP_strdupA( SystemHeap, 0, path );
164             drive->dos_cwd  = HEAP_strdupA( SystemHeap, 0, "" );
165             drive->unix_cwd = HEAP_strdupA( SystemHeap, 0, "" );
166             drive->type     = DRIVE_GetDriveType( name );
167             drive->device   = NULL;
168             drive->flags    = 0;
169             drive->dev      = drive_stat_buffer.st_dev;
170             drive->ino      = drive_stat_buffer.st_ino;
171
172             /* Get the drive label */
173             PROFILE_GetWineIniString( name, "Label", name, drive->label, 12 );
174             if ((len = strlen(drive->label)) < 11)
175             {
176                 /* Pad label with spaces */
177                 memset( drive->label + len, ' ', 11 - len );
178                 drive->label[12] = '\0';
179             }
180
181             /* Get the serial number */
182             PROFILE_GetWineIniString( name, "Serial", "12345678",
183                                       buffer, sizeof(buffer) );
184             drive->serial = strtoul( buffer, NULL, 16 );
185
186             /* Get the filesystem type */
187             PROFILE_GetWineIniString( name, "Filesystem", "win95",
188                                       buffer, sizeof(buffer) );
189             drive->flags = DRIVE_GetFSFlags( name, buffer );
190
191             /* Get the device */
192             PROFILE_GetWineIniString( name, "Device", "",
193                                       buffer, sizeof(buffer) );
194             if (buffer[0])
195                 drive->device = HEAP_strdupA( SystemHeap, 0, buffer );
196
197             /* Make the first hard disk the current drive */
198             if ((DRIVE_CurDrive == -1) && (drive->type == TYPE_HD))
199                 DRIVE_CurDrive = i;
200
201             count++;
202             TRACE(dosfs, "%s: path=%s type=%s label='%s' serial=%08lx flags=%08x dev=%x ino=%x\n",
203                            name, path, DRIVE_Types[drive->type],
204                            drive->label, drive->serial, drive->flags,
205                            (int)drive->dev, (int)drive->ino );
206         }
207         else WARN(dosfs, "%s: not defined\n", name );
208     }
209
210     if (!count) 
211     {
212         MSG("Warning: no valid DOS drive found, check your configuration file.\n" );
213         /* Create a C drive pointing to Unix root dir */
214         DOSDrives[2].root     = HEAP_strdupA( SystemHeap, 0, "/" );
215         DOSDrives[2].dos_cwd  = HEAP_strdupA( SystemHeap, 0, "" );
216         DOSDrives[2].unix_cwd = HEAP_strdupA( SystemHeap, 0, "" );
217         strcpy( DOSDrives[2].label, "Drive C    " );
218         DOSDrives[2].serial   = 0x12345678;
219         DOSDrives[2].type     = TYPE_HD;
220         DOSDrives[2].flags    = 0;
221         DRIVE_CurDrive = 2;
222     }
223
224     /* Make sure the current drive is valid */
225     if (DRIVE_CurDrive == -1)
226     {
227         for (i = 0, drive = DOSDrives; i < MAX_DOS_DRIVES; i++, drive++)
228         {
229             if (drive->root && !(drive->flags & DRIVE_DISABLED))
230             {
231                 DRIVE_CurDrive = i;
232                 break;
233             }
234         }
235     }
236
237     return 1;
238 }
239
240
241 /***********************************************************************
242  *           DRIVE_IsValid
243  */
244 int DRIVE_IsValid( int drive )
245 {
246     if ((drive < 0) || (drive >= MAX_DOS_DRIVES)) return 0;
247     return (DOSDrives[drive].root &&
248             !(DOSDrives[drive].flags & DRIVE_DISABLED));
249 }
250
251
252 /***********************************************************************
253  *           DRIVE_GetCurrentDrive
254  */
255 int DRIVE_GetCurrentDrive(void)
256 {
257     TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
258     if (pTask && (pTask->curdrive & 0x80)) return pTask->curdrive & ~0x80;
259     return DRIVE_CurDrive;
260 }
261
262
263 /***********************************************************************
264  *           DRIVE_SetCurrentDrive
265  */
266 int DRIVE_SetCurrentDrive( int drive )
267 {
268     TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
269     if (!DRIVE_IsValid( drive ))
270     {
271         SetLastError( ERROR_INVALID_DRIVE );
272         return 0;
273     }
274     TRACE(dosfs, "%c:\n", 'A' + drive );
275     DRIVE_CurDrive = drive;
276     if (pTask) pTask->curdrive = drive | 0x80;
277     return 1;
278 }
279
280
281 /***********************************************************************
282  *           DRIVE_FindDriveRoot
283  *
284  * Find a drive for which the root matches the begginning of the given path.
285  * This can be used to translate a Unix path into a drive + DOS path.
286  * Return value is the drive, or -1 on error. On success, path is modified
287  * to point to the beginning of the DOS path.
288  */
289 int DRIVE_FindDriveRoot( const char **path )
290 {
291     /* idea: check at all '/' positions.
292      * If the device and inode of that path is identical with the
293      * device and inode of the current drive then we found a solution.
294      * If there is another drive pointing to a deeper position in
295      * the file tree, we want to find that one, not the earlier solution.
296      */
297     int drive, rootdrive = -1;
298     char buffer[MAX_PATHNAME_LEN];
299     char *next = buffer;
300     const char *p = *path;
301     struct stat st;
302
303     strcpy( buffer, "/" );
304     for (;;)
305     {
306         if (stat( buffer, &st ) || !S_ISDIR( st.st_mode )) break;
307
308         /* Find the drive */
309
310         for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
311         {
312            if (!DOSDrives[drive].root ||
313                (DOSDrives[drive].flags & DRIVE_DISABLED)) continue;
314
315            if ((DOSDrives[drive].dev == st.st_dev) &&
316                (DOSDrives[drive].ino == st.st_ino))
317            {
318                rootdrive = drive;
319                *path = p;
320            }
321         }
322
323         /* Get the next path component */
324
325         *next++ = '/';
326         while ((*p == '/') || (*p == '\\')) p++;
327         if (!*p) break;
328         while (!IS_END_OF_NAME(*p)) *next++ = *p++;
329         *next = 0;
330     }
331     *next = 0;
332
333     if (rootdrive != -1)
334         TRACE(dosfs, "%s -> drive %c:, root='%s', name='%s'\n",
335                        buffer, 'A' + rootdrive,
336                        DOSDrives[rootdrive].root, *path );
337     return rootdrive;
338 }
339
340
341 /***********************************************************************
342  *           DRIVE_GetRoot
343  */
344 const char * DRIVE_GetRoot( int drive )
345 {
346     if (!DRIVE_IsValid( drive )) return NULL;
347     return DOSDrives[drive].root;
348 }
349
350
351 /***********************************************************************
352  *           DRIVE_GetDosCwd
353  */
354 const char * DRIVE_GetDosCwd( int drive )
355 {
356     TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
357     if (!DRIVE_IsValid( drive )) return NULL;
358
359     /* Check if we need to change the directory to the new task. */
360     if (pTask && (pTask->curdrive & 0x80) &&    /* The task drive is valid */
361         ((pTask->curdrive & ~0x80) == drive) && /* and it's the one we want */
362         (DRIVE_LastTask != GetCurrentTask()))   /* and the task changed */
363     {
364         /* Perform the task-switch */
365         if (!DRIVE_Chdir( drive, pTask->curdir )) DRIVE_Chdir( drive, "\\" );
366         DRIVE_LastTask = GetCurrentTask();
367     }
368     return DOSDrives[drive].dos_cwd;
369 }
370
371
372 /***********************************************************************
373  *           DRIVE_GetUnixCwd
374  */
375 const char * DRIVE_GetUnixCwd( int drive )
376 {
377     TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
378     if (!DRIVE_IsValid( drive )) return NULL;
379
380     /* Check if we need to change the directory to the new task. */
381     if (pTask && (pTask->curdrive & 0x80) &&    /* The task drive is valid */
382         ((pTask->curdrive & ~0x80) == drive) && /* and it's the one we want */
383         (DRIVE_LastTask != GetCurrentTask()))   /* and the task changed */
384     {
385         /* Perform the task-switch */
386         if (!DRIVE_Chdir( drive, pTask->curdir )) DRIVE_Chdir( drive, "\\" );
387         DRIVE_LastTask = GetCurrentTask();
388     }
389     return DOSDrives[drive].unix_cwd;
390 }
391
392
393 /***********************************************************************
394  *           DRIVE_GetLabel
395  */
396 const char * DRIVE_GetLabel( int drive )
397 {
398     if (!DRIVE_IsValid( drive )) return NULL;
399     return DOSDrives[drive].label;
400 }
401
402
403 /***********************************************************************
404  *           DRIVE_GetSerialNumber
405  */
406 DWORD DRIVE_GetSerialNumber( int drive )
407 {
408     if (!DRIVE_IsValid( drive )) return 0;
409     return DOSDrives[drive].serial;
410 }
411
412
413 /***********************************************************************
414  *           DRIVE_SetSerialNumber
415  */
416 int DRIVE_SetSerialNumber( int drive, DWORD serial )
417 {
418     if (!DRIVE_IsValid( drive )) return 0;
419     DOSDrives[drive].serial = serial;
420     return 1;
421 }
422
423
424 /***********************************************************************
425  *           DRIVE_GetType
426  */
427 DRIVETYPE DRIVE_GetType( int drive )
428 {
429     if (!DRIVE_IsValid( drive )) return TYPE_INVALID;
430     return DOSDrives[drive].type;
431 }
432
433
434 /***********************************************************************
435  *           DRIVE_GetFlags
436  */
437 UINT32 DRIVE_GetFlags( int drive )
438 {
439     if ((drive < 0) || (drive >= MAX_DOS_DRIVES)) return 0;
440     return DOSDrives[drive].flags;
441 }
442
443
444 /***********************************************************************
445  *           DRIVE_Chdir
446  */
447 int DRIVE_Chdir( int drive, const char *path )
448 {
449     DOS_FULL_NAME full_name;
450     char buffer[MAX_PATHNAME_LEN];
451     LPSTR unix_cwd;
452     BY_HANDLE_FILE_INFORMATION info;
453     TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
454
455     strcpy( buffer, "A:" );
456     buffer[0] += drive;
457     TRACE(dosfs, "(%c:,%s)\n", buffer[0], path );
458     lstrcpyn32A( buffer + 2, path, sizeof(buffer) - 2 );
459
460     if (!DOSFS_GetFullName( buffer, TRUE, &full_name )) return 0;
461     if (!FILE_Stat( full_name.long_name, &info )) return 0;
462     if (!(info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
463     {
464         SetLastError( ERROR_FILE_NOT_FOUND );
465         return 0;
466     }
467     unix_cwd = full_name.long_name + strlen( DOSDrives[drive].root );
468     while (*unix_cwd == '/') unix_cwd++;
469
470     TRACE(dosfs, "(%c:): unix_cwd=%s dos_cwd=%s\n",
471                    'A' + drive, unix_cwd, full_name.short_name + 3 );
472
473     HeapFree( SystemHeap, 0, DOSDrives[drive].dos_cwd );
474     HeapFree( SystemHeap, 0, DOSDrives[drive].unix_cwd );
475     DOSDrives[drive].dos_cwd  = HEAP_strdupA( SystemHeap, 0,
476                                               full_name.short_name + 3 );
477     DOSDrives[drive].unix_cwd = HEAP_strdupA( SystemHeap, 0, unix_cwd );
478
479     if (pTask && (pTask->curdrive & 0x80) && 
480         ((pTask->curdrive & ~0x80) == drive))
481     {
482         lstrcpyn32A( pTask->curdir, full_name.short_name + 2,
483                      sizeof(pTask->curdir) );
484         DRIVE_LastTask = GetCurrentTask();
485     }
486     return 1;
487 }
488
489
490 /***********************************************************************
491  *           DRIVE_Disable
492  */
493 int DRIVE_Disable( int drive  )
494 {
495     if ((drive < 0) || (drive >= MAX_DOS_DRIVES) || !DOSDrives[drive].root)
496     {
497         SetLastError( ERROR_INVALID_DRIVE );
498         return 0;
499     }
500     DOSDrives[drive].flags |= DRIVE_DISABLED;
501     return 1;
502 }
503
504
505 /***********************************************************************
506  *           DRIVE_Enable
507  */
508 int DRIVE_Enable( int drive  )
509 {
510     if ((drive < 0) || (drive >= MAX_DOS_DRIVES) || !DOSDrives[drive].root)
511     {
512         SetLastError( ERROR_INVALID_DRIVE );
513         return 0;
514     }
515     DOSDrives[drive].flags &= ~DRIVE_DISABLED;
516     return 1;
517 }
518
519
520 /***********************************************************************
521  *           DRIVE_SetLogicalMapping
522  */
523 int DRIVE_SetLogicalMapping ( int existing_drive, int new_drive )
524 {
525  /* If new_drive is already valid, do nothing and return 0
526     otherwise, copy DOSDrives[existing_drive] to DOSDrives[new_drive] */
527   
528     DOSDRIVE *old, *new;
529     
530     old = DOSDrives + existing_drive;
531     new = DOSDrives + new_drive;
532
533     if ((existing_drive < 0) || (existing_drive >= MAX_DOS_DRIVES) ||
534         !old->root ||
535         (new_drive < 0) || (new_drive >= MAX_DOS_DRIVES))
536     {
537         SetLastError( ERROR_INVALID_DRIVE );
538         return 0;
539     }
540
541     if ( new->root )
542     {
543         TRACE(dosfs, "Can\'t map drive %c to drive %c - "
544                                 "drive %c already exists\n",
545                         'A' + existing_drive, 'A' + new_drive,
546                         'A' + new_drive );
547         /* it is already mapped there, so return success */
548         if (!strcmp(old->root,new->root))
549             return 1;
550         return 0;
551     }
552
553     new->root = HEAP_strdupA( SystemHeap, 0, old->root );
554     new->dos_cwd = HEAP_strdupA( SystemHeap, 0, old->dos_cwd );
555     new->unix_cwd = HEAP_strdupA( SystemHeap, 0, old->unix_cwd );
556     memcpy ( new->label, old->label, 12 );
557     new->serial = old->serial;
558     new->type = old->type;
559     new->flags = old->flags;
560     new->dev = old->dev;
561     new->ino = old->ino;
562
563     TRACE(dosfs, "Drive %c is now equal to drive %c\n",
564                     'A' + new_drive, 'A' + existing_drive );
565
566     return 1;
567 }
568
569
570 /***********************************************************************
571  *           DRIVE_OpenDevice
572  *
573  * Open the drive raw device and return a Unix fd (or -1 on error).
574  */
575 int DRIVE_OpenDevice( int drive, int flags )
576 {
577     if (!DRIVE_IsValid( drive )) return -1;
578     return open( DOSDrives[drive].device, flags );
579 }
580
581
582 /***********************************************************************
583  *           DRIVE_RawRead
584  *
585  * Read raw sectors from a device
586  */
587 int DRIVE_RawRead(BYTE drive, DWORD begin, DWORD nr_sect, BYTE *dataptr, BOOL32 fake_success)
588 {
589     int fd;
590
591     if ((fd = DRIVE_OpenDevice( drive, O_RDONLY )) != -1)
592     {
593         lseek( fd, begin * 512, SEEK_SET );
594         /* FIXME: check errors */
595         read( fd, dataptr, nr_sect * 512 );
596         close( fd );
597     }
598     else
599     {
600         memset(dataptr, 0, nr_sect * 512);
601                 if (fake_success)
602         {
603                         if (begin == 0 && nr_sect > 1) *(dataptr + 512) = 0xf8;
604                         if (begin == 1) *dataptr = 0xf8;
605                 }
606                 else
607                 return 0;
608     }
609         return 1;
610 }
611
612
613 /***********************************************************************
614  *           DRIVE_RawWrite
615  *
616  * Write raw sectors to a device
617  */
618 int DRIVE_RawWrite(BYTE drive, DWORD begin, DWORD nr_sect, BYTE *dataptr, BOOL32 fake_success)
619 {
620         int fd;
621
622     if ((fd = DRIVE_OpenDevice( drive, O_RDONLY )) != -1)
623     {
624         lseek( fd, begin * 512, SEEK_SET );
625         /* FIXME: check errors */
626         write( fd, dataptr, nr_sect * 512 );
627         close( fd );
628     }
629     else
630         if (!(fake_success))
631                 return 0;
632
633         return 1;
634 }
635
636
637 /***********************************************************************
638  *           DRIVE_GetFreeSpace
639  */
640 static int DRIVE_GetFreeSpace( int drive, LPULARGE_INTEGER size, 
641                                LPULARGE_INTEGER available )
642 {
643     struct statfs info;
644     unsigned long long  bigsize,bigavail=0;
645
646     if (!DRIVE_IsValid(drive))
647     {
648         SetLastError( ERROR_INVALID_DRIVE );
649         return 0;
650     }
651
652 /* FIXME: add autoconf check for this */
653 #if defined(__svr4__) || defined(_SCO_DS)
654     if (statfs( DOSDrives[drive].root, &info, 0, 0) < 0)
655 #else
656     if (statfs( DOSDrives[drive].root, &info) < 0)
657 #endif
658     {
659         FILE_SetDosError();
660         WARN(dosfs, "cannot do statfs(%s)\n", DOSDrives[drive].root);
661         return 0;
662     }
663
664     bigsize = (unsigned long long)info.f_bsize 
665       * (unsigned long long)info.f_blocks;
666 #ifdef STATFS_HAS_BAVAIL
667     bigavail = (unsigned long long)info.f_bavail 
668       * (unsigned long long)info.f_bsize;
669 #else
670 # ifdef STATFS_HAS_BFREE
671     bigavail = (unsigned long long)info.f_bfree 
672       * (unsigned long long)info.f_bsize;
673 # else
674 #  error "statfs has no bfree/bavail member!"
675 # endif
676 #endif
677     size->LowPart = (DWORD)bigsize;
678     size->HighPart = (DWORD)(bigsize>>32);
679     available->LowPart = (DWORD)bigavail;
680     available->HighPart = (DWORD)(bigavail>>32);
681     return 1;
682 }
683
684
685 /***********************************************************************
686  *           GetDiskFreeSpace16   (KERNEL.422)
687  */
688 BOOL16 WINAPI GetDiskFreeSpace16( LPCSTR root, LPDWORD cluster_sectors,
689                                   LPDWORD sector_bytes, LPDWORD free_clusters,
690                                   LPDWORD total_clusters )
691 {
692     return GetDiskFreeSpace32A( root, cluster_sectors, sector_bytes,
693                                 free_clusters, total_clusters );
694 }
695
696
697 /***********************************************************************
698  *           GetDiskFreeSpace32A   (KERNEL32.206)
699  *
700  * Fails if expression resulting from current drive's dir and "root"
701  * is not a root dir of the target drive.
702  *
703  * UNDOC: setting some LPDWORDs to NULL is perfectly possible 
704  * if the corresponding info is unneeded.
705  *
706  * FIXME: needs to support UNC names from Win95 OSR2 on.
707  *
708  * Behaviour under Win95a:
709  * CurrDir     root   result
710  * "E:\\TEST"  "E:"   FALSE
711  * "E:\\"      "E:"   TRUE
712  * "E:\\"      "E"    FALSE
713  * "E:\\"      "\\"   TRUE
714  * "E:\\TEST"  "\\"   TRUE
715  * "E:\\TEST"  ":\\"  FALSE
716  * "E:\\TEST"  "E:\\" TRUE
717  * "E:\\TEST"  ""     FALSE
718  * "E:\\"      ""     FALSE (!)
719  * "E:\\"      0x0    TRUE
720  * "E:\\TEST"  0x0    TRUE  (!)
721  * "E:\\TEST"  "C:"   TRUE  (when CurrDir of "C:" set to "\\")
722  * "E:\\TEST"  "C:"   FALSE (when CurrDir of "C:" set to "\\TEST")
723  */
724 BOOL32 WINAPI GetDiskFreeSpace32A( LPCSTR root, LPDWORD cluster_sectors,
725                                    LPDWORD sector_bytes, LPDWORD free_clusters,
726                                    LPDWORD total_clusters )
727 {
728     int drive;
729     ULARGE_INTEGER size,available;
730     LPCSTR path;
731     DWORD cluster_sec;
732
733     if ((!root) || (root == "\\"))
734         drive = DRIVE_GetCurrentDrive();
735     else
736     if ( (strlen(root) >= 2) && (root[1] == ':')) /* root contains drive tag */
737     {
738         drive = toupper(root[0]) - 'A';
739         path = &root[2];
740         if (path[0] == '\0')
741             path = DRIVE_GetDosCwd(drive);
742         else
743         if (path[0] == '\\')
744             path++;
745         if (strlen(path)) /* oops, we are in a subdir */
746             return FALSE;
747     }
748     else
749         return FALSE;
750
751     if (!DRIVE_GetFreeSpace(drive, &size, &available)) return FALSE;
752
753     /* Cap the size and available at 2GB as per specs.  */
754     if ((size.HighPart) ||(size.LowPart > 0x7fffffff))
755         {
756           size.HighPart = 0;
757           size.LowPart = 0x7fffffff;
758         }
759     if ((available.HighPart) ||(available.LowPart > 0x7fffffff))
760       {
761         available.HighPart =0;
762         available.LowPart = 0x7fffffff;
763       }
764     if (DRIVE_GetType(drive)==TYPE_CDROM) {
765         if (sector_bytes)
766         *sector_bytes    = 2048;
767         size.LowPart            /= 2048;
768         available.LowPart       /= 2048;
769     } else {
770         if (sector_bytes)
771         *sector_bytes    = 512;
772         size.LowPart            /= 512;
773         available.LowPart       /= 512;
774     }
775     /* fixme: probably have to adjust those variables too for CDFS */
776     cluster_sec = 1;
777     while (cluster_sec * 65536 < size.LowPart) cluster_sec *= 2;
778
779     if (cluster_sectors)
780         *cluster_sectors = cluster_sec;
781     if (free_clusters)
782         *free_clusters   = available.LowPart / cluster_sec;
783     if (total_clusters)
784         *total_clusters  = size.LowPart / cluster_sec;
785     return TRUE;
786 }
787
788
789 /***********************************************************************
790  *           GetDiskFreeSpace32W   (KERNEL32.207)
791  */
792 BOOL32 WINAPI GetDiskFreeSpace32W( LPCWSTR root, LPDWORD cluster_sectors,
793                                    LPDWORD sector_bytes, LPDWORD free_clusters,
794                                    LPDWORD total_clusters )
795 {
796     LPSTR xroot;
797     BOOL32 ret;
798
799     xroot = HEAP_strdupWtoA( GetProcessHeap(), 0, root);
800     ret = GetDiskFreeSpace32A( xroot,cluster_sectors, sector_bytes,
801                                free_clusters, total_clusters );
802     HeapFree( GetProcessHeap(), 0, xroot );
803     return ret;
804 }
805
806
807 /***********************************************************************
808  *           GetDiskFreeSpaceEx32A   (KERNEL32.871)
809  */
810 BOOL32 WINAPI GetDiskFreeSpaceEx32A( LPCSTR root,
811                                      LPULARGE_INTEGER avail,
812                                      LPULARGE_INTEGER total,
813                                      LPULARGE_INTEGER totalfree)
814 {
815     int drive;
816     ULARGE_INTEGER size,available;
817
818     if (!root) drive = DRIVE_GetCurrentDrive();
819     else
820     {
821         if ((root[1]) && ((root[1] != ':') || (root[2] != '\\')))
822         {
823             WARN(dosfs, "invalid root '%s'\n", root );
824             return FALSE;
825         }
826         drive = toupper(root[0]) - 'A';
827     }
828     if (!DRIVE_GetFreeSpace(drive, &size, &available)) return FALSE;
829     /*FIXME: Do we have the number of bytes available to the user? */
830     if (totalfree) {
831         totalfree->HighPart = size.HighPart;
832         totalfree->LowPart = size.LowPart ;
833     }
834     if (avail) {
835         avail->HighPart = available.HighPart;
836         avail->LowPart = available.LowPart ;
837     }
838     return TRUE;
839 }
840
841 /***********************************************************************
842  *           GetDiskFreeSpaceEx32W   (KERNEL32.873)
843  */
844 BOOL32 WINAPI GetDiskFreeSpaceEx32W( LPCWSTR root, LPULARGE_INTEGER avail,
845                                      LPULARGE_INTEGER total,
846                                      LPULARGE_INTEGER  totalfree)
847 {
848     LPSTR xroot;
849     BOOL32 ret;
850
851     xroot = HEAP_strdupWtoA( GetProcessHeap(), 0, root);
852     ret = GetDiskFreeSpaceEx32A( xroot, avail, total, totalfree);
853     HeapFree( GetProcessHeap(), 0, xroot );
854     return ret;
855 }
856
857 /***********************************************************************
858  *           GetDriveType16   (KERNEL.136)
859  * This functions returns the drivetype of a drive in Win16. 
860  * Note that it returns DRIVE_REMOTE for CD-ROMs, since MSCDEX uses the
861  * remote drive API. The returnvalue DRIVE_REMOTE for CD-ROMs has been
862  * verified on Win3.11 and Windows 95. Some programs rely on it, so don't
863  * do any pseudo-clever changes.
864  *
865  * RETURNS
866  *      drivetype DRIVE_xxx
867  */
868 UINT16 WINAPI GetDriveType16(
869         UINT16 drive    /* [in] number (NOT letter) of drive */
870 ) {
871     TRACE(dosfs, "(%c:)\n", 'A' + drive );
872     switch(DRIVE_GetType(drive))
873     {
874     case TYPE_FLOPPY:  return DRIVE_REMOVABLE;
875     case TYPE_HD:      return DRIVE_FIXED;
876     case TYPE_CDROM:   return DRIVE_REMOTE;
877     case TYPE_NETWORK: return DRIVE_REMOTE;
878     case TYPE_INVALID:
879     default:           return DRIVE_CANNOTDETERMINE;
880     }
881 }
882
883
884 /***********************************************************************
885  *           GetDriveType32A   (KERNEL32.208)
886  *
887  * Returns the type of the disk drive specified.  If root is NULL the
888  * root of the current directory is used.
889  *
890  * RETURNS
891  *
892  *  Type of drive (from Win32 SDK):
893  *
894  *   DRIVE_UNKNOWN     unable to find out anything about the drive
895  *   DRIVE_NO_ROOT_DIR nonexistand root dir
896  *   DRIVE_REMOVABLE   the disk can be removed from the machine
897  *   DRIVE_FIXED       the disk can not be removed from the machine
898  *   DRIVE_REMOTE      network disk
899  *   DRIVE_CDROM       CDROM drive
900  *   DRIVE_RAMDISK     virtual disk in ram
901  *
902  *   DRIVE_DOESNOTEXIST    XXX Not valid return value
903  *   DRIVE_CANNOTDETERMINE XXX Not valid return value
904  *   
905  * BUGS
906  *
907  *  Currently returns DRIVE_DOESNOTEXIST and DRIVE_CANNOTDETERMINE
908  *  when it really should return DRIVE_NO_ROOT_DIR and DRIVE_UNKNOWN.
909  *  Why where the former defines used?
910  *
911  *  DRIVE_RAMDISK is unsupported.
912  */
913 UINT32 WINAPI GetDriveType32A(LPCSTR root /* String describing drive */)
914 {
915     int drive;
916     TRACE(dosfs, "(%s)\n", debugstr_a(root));
917
918     if (NULL == root) drive = DRIVE_GetCurrentDrive();
919     else
920     {
921         if ((root[1]) && (root[1] != ':'))
922         {
923             WARN(dosfs, "invalid root '%s'\n", debugstr_a(root));
924             return DRIVE_DOESNOTEXIST;
925         }
926         drive = toupper(root[0]) - 'A';
927     }
928     switch(DRIVE_GetType(drive))
929     {
930     case TYPE_FLOPPY:  return DRIVE_REMOVABLE;
931     case TYPE_HD:      return DRIVE_FIXED;
932     case TYPE_CDROM:   return DRIVE_CDROM;
933     case TYPE_NETWORK: return DRIVE_REMOTE;
934     case TYPE_INVALID: return DRIVE_DOESNOTEXIST;
935     default:           return DRIVE_CANNOTDETERMINE;
936     }
937 }
938
939
940 /***********************************************************************
941  *           GetDriveType32W   (KERNEL32.209)
942  */
943 UINT32 WINAPI GetDriveType32W( LPCWSTR root )
944 {
945     LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, root );
946     UINT32 ret = GetDriveType32A( xpath );
947     HeapFree( GetProcessHeap(), 0, xpath );
948     return ret;
949 }
950
951
952 /***********************************************************************
953  *           GetCurrentDirectory16   (KERNEL.411)
954  */
955 UINT16 WINAPI GetCurrentDirectory16( UINT16 buflen, LPSTR buf )
956 {
957     return (UINT16)GetCurrentDirectory32A( buflen, buf );
958 }
959
960
961 /***********************************************************************
962  *           GetCurrentDirectory32A   (KERNEL32.196)
963  *
964  * Returns "X:\\path\\etc\\".
965  *
966  * Despite the API description, return required length including the 
967  * terminating null when buffer too small. This is the real behaviour.
968  */
969 UINT32 WINAPI GetCurrentDirectory32A( UINT32 buflen, LPSTR buf )
970 {
971     UINT32 ret;
972     const char *s = DRIVE_GetDosCwd( DRIVE_GetCurrentDrive() );
973
974     assert(s);
975     ret = strlen(s) + 3; /* length of WHOLE current directory */
976     if (ret >= buflen) return ret + 1;
977     lstrcpyn32A( buf, "A:\\", MIN( 4, buflen ) );
978     if (buflen) buf[0] += DRIVE_GetCurrentDrive();
979     if (buflen > 3) lstrcpyn32A( buf + 3, s, buflen - 3 );
980     return ret;
981 }
982
983
984 /***********************************************************************
985  *           GetCurrentDirectory32W   (KERNEL32.197)
986  */
987 UINT32 WINAPI GetCurrentDirectory32W( UINT32 buflen, LPWSTR buf )
988 {
989     LPSTR xpath = HeapAlloc( GetProcessHeap(), 0, buflen+1 );
990     UINT32 ret = GetCurrentDirectory32A( buflen, xpath );
991     lstrcpyAtoW( buf, xpath );
992     HeapFree( GetProcessHeap(), 0, xpath );
993     return ret;
994 }
995
996
997 /***********************************************************************
998  *           SetCurrentDirectory   (KERNEL.412)
999  */
1000 BOOL16 WINAPI SetCurrentDirectory16( LPCSTR dir )
1001 {
1002     return SetCurrentDirectory32A( dir );
1003 }
1004
1005
1006 /***********************************************************************
1007  *           SetCurrentDirectory32A   (KERNEL32.479)
1008  */
1009 BOOL32 WINAPI SetCurrentDirectory32A( LPCSTR dir )
1010 {
1011     int olddrive, drive = DRIVE_GetCurrentDrive();
1012
1013     if (!dir) {
1014         ERR(file,"(NULL)!\n");
1015         return FALSE;
1016     }
1017     if (dir[0] && (dir[1]==':'))
1018     {
1019         drive = tolower( *dir ) - 'a';
1020         dir += 2;
1021     }
1022
1023     /* WARNING: we need to set the drive before the dir, as DRIVE_Chdir
1024        sets pTask->curdir only if pTask->curdrive is drive */
1025     olddrive = drive; /* in case DRIVE_Chdir fails */
1026     if (!(DRIVE_SetCurrentDrive( drive )))
1027         return FALSE;
1028     /* FIXME: what about empty strings? Add a \\ ? */
1029     if (!DRIVE_Chdir( drive, dir )) {
1030         DRIVE_SetCurrentDrive(olddrive);
1031         return FALSE;
1032     }
1033     return TRUE;
1034 }
1035
1036
1037 /***********************************************************************
1038  *           SetCurrentDirectory32W   (KERNEL32.480)
1039  */
1040 BOOL32 WINAPI SetCurrentDirectory32W( LPCWSTR dirW )
1041 {
1042     LPSTR dir = HEAP_strdupWtoA( GetProcessHeap(), 0, dirW );
1043     BOOL32 res = SetCurrentDirectory32A( dir );
1044     HeapFree( GetProcessHeap(), 0, dir );
1045     return res;
1046 }
1047
1048
1049 /***********************************************************************
1050  *           GetLogicalDriveStrings32A   (KERNEL32.231)
1051  */
1052 UINT32 WINAPI GetLogicalDriveStrings32A( UINT32 len, LPSTR buffer )
1053 {
1054     int drive, count;
1055
1056     for (drive = count = 0; drive < MAX_DOS_DRIVES; drive++)
1057         if (DRIVE_IsValid(drive)) count++;
1058     if (count * 4 * sizeof(char) <= len)
1059     {
1060         LPSTR p = buffer;
1061         for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
1062             if (DRIVE_IsValid(drive))
1063             {
1064                 *p++ = 'a' + drive;
1065                 *p++ = ':';
1066                 *p++ = '\\';
1067                 *p++ = '\0';
1068             }
1069         *p = '\0';
1070     }
1071     return count * 4 * sizeof(char);
1072 }
1073
1074
1075 /***********************************************************************
1076  *           GetLogicalDriveStrings32W   (KERNEL32.232)
1077  */
1078 UINT32 WINAPI GetLogicalDriveStrings32W( UINT32 len, LPWSTR buffer )
1079 {
1080     int drive, count;
1081
1082     for (drive = count = 0; drive < MAX_DOS_DRIVES; drive++)
1083         if (DRIVE_IsValid(drive)) count++;
1084     if (count * 4 * sizeof(WCHAR) <= len)
1085     {
1086         LPWSTR p = buffer;
1087         for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
1088             if (DRIVE_IsValid(drive))
1089             {
1090                 *p++ = (WCHAR)('a' + drive);
1091                 *p++ = (WCHAR)':';
1092                 *p++ = (WCHAR)'\\';
1093                 *p++ = (WCHAR)'\0';
1094             }
1095         *p = (WCHAR)'\0';
1096     }
1097     return count * 4 * sizeof(WCHAR);
1098 }
1099
1100
1101 /***********************************************************************
1102  *           GetLogicalDrives   (KERNEL32.233)
1103  */
1104 DWORD WINAPI GetLogicalDrives(void)
1105 {
1106     DWORD ret = 0;
1107     int drive;
1108
1109     for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
1110         if (DRIVE_IsValid(drive)) ret |= (1 << drive);
1111     return ret;
1112 }
1113
1114
1115 /***********************************************************************
1116  *           GetVolumeInformation32A   (KERNEL32.309)
1117  */
1118 BOOL32 WINAPI GetVolumeInformation32A( LPCSTR root, LPSTR label,
1119                                        DWORD label_len, DWORD *serial,
1120                                        DWORD *filename_len, DWORD *flags,
1121                                        LPSTR fsname, DWORD fsname_len )
1122 {
1123     int drive;
1124     char *cp;
1125
1126     /* FIXME, SetLastErrors missing */
1127
1128     if (!root) drive = DRIVE_GetCurrentDrive();
1129     else
1130     {
1131         if ((root[1]) && (root[1] != ':'))
1132         {
1133             WARN(dosfs, "invalid root '%s'\n",root);
1134             return FALSE;
1135         }
1136         drive = toupper(root[0]) - 'A';
1137     }
1138     if (!DRIVE_IsValid( drive )) return FALSE;
1139     if (label)
1140     {
1141        lstrcpyn32A( label, DRIVE_GetLabel(drive), label_len );
1142        for (cp = label; *cp; cp++);
1143        while (cp != label && *(cp-1) == ' ') cp--;
1144        *cp = '\0';
1145     }
1146     if (serial) *serial = DRIVE_GetSerialNumber(drive);
1147
1148     /* Set the filesystem information */
1149     /* Note: we only emulate a FAT fs at the present */
1150
1151     if (filename_len) {
1152         if (DOSDrives[drive].flags & DRIVE_SHORT_NAMES)
1153             *filename_len = 12;
1154         else
1155             *filename_len = 255;
1156     }
1157     if (flags)
1158       {
1159        *flags=0;
1160        if (DOSDrives[drive].flags & DRIVE_CASE_SENSITIVE)
1161          *flags|=FS_CASE_SENSITIVE;
1162        if (DOSDrives[drive].flags & DRIVE_CASE_PRESERVING)
1163          *flags|=FS_CASE_IS_PRESERVED ;
1164       }
1165     if (fsname) {
1166         /* Diablo checks that return code ... */
1167         if (DRIVE_GetType(drive)==TYPE_CDROM)
1168             lstrcpyn32A( fsname, "CDFS", fsname_len );
1169         else
1170             lstrcpyn32A( fsname, "FAT", fsname_len );
1171     }
1172     return TRUE;
1173 }
1174
1175
1176 /***********************************************************************
1177  *           GetVolumeInformation32W   (KERNEL32.310)
1178  */
1179 BOOL32 WINAPI GetVolumeInformation32W( LPCWSTR root, LPWSTR label,
1180                                        DWORD label_len, DWORD *serial,
1181                                        DWORD *filename_len, DWORD *flags,
1182                                        LPWSTR fsname, DWORD fsname_len )
1183 {
1184     LPSTR xroot    = HEAP_strdupWtoA( GetProcessHeap(), 0, root );
1185     LPSTR xvolname = label ? HeapAlloc(GetProcessHeap(),0,label_len) : NULL;
1186     LPSTR xfsname  = fsname ? HeapAlloc(GetProcessHeap(),0,fsname_len) : NULL;
1187     BOOL32 ret = GetVolumeInformation32A( xroot, xvolname, label_len, serial,
1188                                           filename_len, flags, xfsname,
1189                                           fsname_len );
1190     if (ret)
1191     {
1192         if (label) lstrcpyAtoW( label, xvolname );
1193         if (fsname) lstrcpyAtoW( fsname, xfsname );
1194     }
1195     HeapFree( GetProcessHeap(), 0, xroot );
1196     HeapFree( GetProcessHeap(), 0, xvolname );
1197     HeapFree( GetProcessHeap(), 0, xfsname );
1198     return ret;
1199 }
1200
1201 BOOL32 WINAPI SetVolumeLabel32A(LPCSTR rootpath,LPCSTR volname) {
1202         FIXME(dosfs,"(%s,%s),stub!\n",rootpath,volname);
1203         return TRUE;
1204 }