Changed DOS extended error handling to be based on SetLastError;
[wine/testsucceed.git] / files / drive.c
blob0c63a9c5bc216176482bf9048ad6104b4d0b838d
1 /*
2 * DOS drives handling functions
4 * Copyright 1993 Erik Bos
5 * Copyright 1996 Alexandre Julliard
6 */
8 #include "config.h"
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>
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
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"
46 typedef struct
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;
61 static const char * const DRIVE_Types[] =
63 "floppy", /* TYPE_FLOPPY */
64 "hd", /* TYPE_HD */
65 "cdrom", /* TYPE_CDROM */
66 "network" /* TYPE_NETWORK */
70 /* Known filesystem types */
72 typedef struct
74 const char *name;
75 UINT32 flags;
76 } FS_DESCR;
78 static const FS_DESCR DRIVE_Filesystems[] =
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 }
90 static DOSDRIVE DOSDrives[MAX_DOS_DRIVES];
91 static int DRIVE_CurDrive = -1;
93 static HTASK16 DRIVE_LastTask = 0;
96 /***********************************************************************
97 * DRIVE_GetDriveType
99 static DRIVETYPE DRIVE_GetDriveType( const char *name )
101 char buffer[20];
102 int i;
104 PROFILE_GetWineIniString( name, "Type", "hd", buffer, sizeof(buffer) );
105 for (i = 0; i < sizeof(DRIVE_Types)/sizeof(DRIVE_Types[0]); i++)
107 if (!strcasecmp( buffer, DRIVE_Types[i] )) return (DRIVETYPE)i;
109 MSG("%s: unknown type '%s', defaulting to 'hd'.\n", name, buffer );
110 return TYPE_HD;
114 /***********************************************************************
115 * DRIVE_GetFSFlags
117 static UINT32 DRIVE_GetFSFlags( const char *name, const char *value )
119 const FS_DESCR *descr;
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 'unix'.\n",
124 name, value );
125 return DRIVE_CASE_SENSITIVE | DRIVE_CASE_PRESERVING;
129 /***********************************************************************
130 * DRIVE_Init
132 int DRIVE_Init(void)
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;
142 for (i = 0, drive = DOSDrives; i < MAX_DOS_DRIVES; i++, name[6]++, drive++)
144 PROFILE_GetWineIniString( name, "Path", "", path, sizeof(path)-1 );
145 if (path[0])
147 p = path + strlen(path) - 1;
148 while ((p > path) && ((*p == '/') || (*p == '\\'))) *p-- = '\0';
149 if (!path[0]) strcpy( path, "/" );
151 if (stat( path, &drive_stat_buffer ))
153 MSG("Could not stat %s, ignoring drive %c:\n", path, 'A' + i );
154 continue;
156 if (!S_ISDIR(drive_stat_buffer.st_mode))
158 MSG("%s is not a directory, ignoring drive %c:\n",
159 path, 'A' + i );
160 continue;
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;
172 /* Get the drive label */
173 PROFILE_GetWineIniString( name, "Label", name, drive->label, 12 );
174 if ((len = strlen(drive->label)) < 11)
176 /* Pad label with spaces */
177 memset( drive->label + len, ' ', 11 - len );
178 drive->label[12] = '\0';
181 /* Get the serial number */
182 PROFILE_GetWineIniString( name, "Serial", "12345678",
183 buffer, sizeof(buffer) );
184 drive->serial = strtoul( buffer, NULL, 16 );
186 /* Get the filesystem type */
187 PROFILE_GetWineIniString( name, "Filesystem", "unix",
188 buffer, sizeof(buffer) );
189 drive->flags = DRIVE_GetFSFlags( name, buffer );
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 );
197 /* Make the first hard disk the current drive */
198 if ((DRIVE_CurDrive == -1) && (drive->type == TYPE_HD))
199 DRIVE_CurDrive = i;
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 );
207 else WARN(dosfs, "%s: not defined\n", name );
210 if (!count)
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;
224 /* Make sure the current drive is valid */
225 if (DRIVE_CurDrive == -1)
227 for (i = 0, drive = DOSDrives; i < MAX_DOS_DRIVES; i++, drive++)
229 if (drive->root && !(drive->flags & DRIVE_DISABLED))
231 DRIVE_CurDrive = i;
232 break;
237 return 1;
241 /***********************************************************************
242 * DRIVE_IsValid
244 int DRIVE_IsValid( int drive )
246 if ((drive < 0) || (drive >= MAX_DOS_DRIVES)) return 0;
247 return (DOSDrives[drive].root &&
248 !(DOSDrives[drive].flags & DRIVE_DISABLED));
252 /***********************************************************************
253 * DRIVE_GetCurrentDrive
255 int DRIVE_GetCurrentDrive(void)
257 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
258 if (pTask && (pTask->curdrive & 0x80)) return pTask->curdrive & ~0x80;
259 return DRIVE_CurDrive;
263 /***********************************************************************
264 * DRIVE_SetCurrentDrive
266 int DRIVE_SetCurrentDrive( int drive )
268 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
269 if (!DRIVE_IsValid( drive ))
271 SetLastError( ERROR_INVALID_DRIVE );
272 return 0;
274 TRACE(dosfs, "%c:\n", 'A' + drive );
275 DRIVE_CurDrive = drive;
276 if (pTask) pTask->curdrive = drive | 0x80;
277 return 1;
281 /***********************************************************************
282 * DRIVE_FindDriveRoot
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.
289 int DRIVE_FindDriveRoot( const char **path )
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.
297 int drive, rootdrive = -1;
298 char buffer[MAX_PATHNAME_LEN];
299 char *next = buffer;
300 const char *p = *path;
301 struct stat st;
303 strcpy( buffer, "/" );
304 for (;;)
306 if (stat( buffer, &st ) || !S_ISDIR( st.st_mode )) break;
308 /* Find the drive */
310 for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
312 if (!DOSDrives[drive].root ||
313 (DOSDrives[drive].flags & DRIVE_DISABLED)) continue;
315 if ((DOSDrives[drive].dev == st.st_dev) &&
316 (DOSDrives[drive].ino == st.st_ino))
318 rootdrive = drive;
319 *path = p;
323 /* Get the next path component */
325 *next++ = '/';
326 while ((*p == '/') || (*p == '\\')) p++;
327 if (!*p) break;
328 while (!IS_END_OF_NAME(*p)) *next++ = *p++;
329 *next = 0;
331 *next = 0;
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;
341 /***********************************************************************
342 * DRIVE_GetRoot
344 const char * DRIVE_GetRoot( int drive )
346 if (!DRIVE_IsValid( drive )) return NULL;
347 return DOSDrives[drive].root;
351 /***********************************************************************
352 * DRIVE_GetDosCwd
354 const char * DRIVE_GetDosCwd( int drive )
356 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
357 if (!DRIVE_IsValid( drive )) return NULL;
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 */
364 /* Perform the task-switch */
365 if (!DRIVE_Chdir( drive, pTask->curdir )) DRIVE_Chdir( drive, "\\" );
366 DRIVE_LastTask = GetCurrentTask();
368 return DOSDrives[drive].dos_cwd;
372 /***********************************************************************
373 * DRIVE_GetUnixCwd
375 const char * DRIVE_GetUnixCwd( int drive )
377 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
378 if (!DRIVE_IsValid( drive )) return NULL;
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 */
385 /* Perform the task-switch */
386 if (!DRIVE_Chdir( drive, pTask->curdir )) DRIVE_Chdir( drive, "\\" );
387 DRIVE_LastTask = GetCurrentTask();
389 return DOSDrives[drive].unix_cwd;
393 /***********************************************************************
394 * DRIVE_GetLabel
396 const char * DRIVE_GetLabel( int drive )
398 if (!DRIVE_IsValid( drive )) return NULL;
399 return DOSDrives[drive].label;
403 /***********************************************************************
404 * DRIVE_GetSerialNumber
406 DWORD DRIVE_GetSerialNumber( int drive )
408 if (!DRIVE_IsValid( drive )) return 0;
409 return DOSDrives[drive].serial;
413 /***********************************************************************
414 * DRIVE_SetSerialNumber
416 int DRIVE_SetSerialNumber( int drive, DWORD serial )
418 if (!DRIVE_IsValid( drive )) return 0;
419 DOSDrives[drive].serial = serial;
420 return 1;
424 /***********************************************************************
425 * DRIVE_GetType
427 DRIVETYPE DRIVE_GetType( int drive )
429 if (!DRIVE_IsValid( drive )) return TYPE_INVALID;
430 return DOSDrives[drive].type;
434 /***********************************************************************
435 * DRIVE_GetFlags
437 UINT32 DRIVE_GetFlags( int drive )
439 if ((drive < 0) || (drive >= MAX_DOS_DRIVES)) return 0;
440 return DOSDrives[drive].flags;
444 /***********************************************************************
445 * DRIVE_Chdir
447 int DRIVE_Chdir( int drive, const char *path )
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() );
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 );
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))
464 SetLastError( ERROR_FILE_NOT_FOUND );
465 return 0;
467 unix_cwd = full_name.long_name + strlen( DOSDrives[drive].root );
468 while (*unix_cwd == '/') unix_cwd++;
470 TRACE(dosfs, "(%c:): unix_cwd=%s dos_cwd=%s\n",
471 'A' + drive, unix_cwd, full_name.short_name + 3 );
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 );
479 if (pTask && (pTask->curdrive & 0x80) &&
480 ((pTask->curdrive & ~0x80) == drive))
482 lstrcpyn32A( pTask->curdir, full_name.short_name + 2,
483 sizeof(pTask->curdir) );
484 DRIVE_LastTask = GetCurrentTask();
486 return 1;
490 /***********************************************************************
491 * DRIVE_Disable
493 int DRIVE_Disable( int drive )
495 if ((drive < 0) || (drive >= MAX_DOS_DRIVES) || !DOSDrives[drive].root)
497 SetLastError( ERROR_INVALID_DRIVE );
498 return 0;
500 DOSDrives[drive].flags |= DRIVE_DISABLED;
501 return 1;
505 /***********************************************************************
506 * DRIVE_Enable
508 int DRIVE_Enable( int drive )
510 if ((drive < 0) || (drive >= MAX_DOS_DRIVES) || !DOSDrives[drive].root)
512 SetLastError( ERROR_INVALID_DRIVE );
513 return 0;
515 DOSDrives[drive].flags &= ~DRIVE_DISABLED;
516 return 1;
520 /***********************************************************************
521 * DRIVE_SetLogicalMapping
523 int DRIVE_SetLogicalMapping ( int existing_drive, int new_drive )
525 /* If new_drive is already valid, do nothing and return 0
526 otherwise, copy DOSDrives[existing_drive] to DOSDrives[new_drive] */
528 DOSDRIVE *old, *new;
530 old = DOSDrives + existing_drive;
531 new = DOSDrives + new_drive;
533 if ((existing_drive < 0) || (existing_drive >= MAX_DOS_DRIVES) ||
534 !old->root ||
535 (new_drive < 0) || (new_drive >= MAX_DOS_DRIVES))
537 SetLastError( ERROR_INVALID_DRIVE );
538 return 0;
541 if ( new->root )
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;
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;
563 TRACE(dosfs, "Drive %c is now equal to drive %c\n",
564 'A' + new_drive, 'A' + existing_drive );
566 return 1;
570 /***********************************************************************
571 * DRIVE_OpenDevice
573 * Open the drive raw device and return a Unix fd (or -1 on error).
575 int DRIVE_OpenDevice( int drive, int flags )
577 if (!DRIVE_IsValid( drive )) return -1;
578 return open( DOSDrives[drive].device, flags );
582 /***********************************************************************
583 * DRIVE_RawRead
585 * Read raw sectors from a device
587 int DRIVE_RawRead(BYTE drive, DWORD begin, DWORD nr_sect, BYTE *dataptr, BOOL32 fake_success)
589 int fd;
591 if ((fd = DRIVE_OpenDevice( drive, O_RDONLY )) != -1)
593 lseek( fd, begin * 512, SEEK_SET );
594 /* FIXME: check errors */
595 read( fd, dataptr, nr_sect * 512 );
596 close( fd );
598 else
600 memset(dataptr, 0, nr_sect * 512);
601 if (fake_success)
603 if (begin == 0 && nr_sect > 1) *(dataptr + 512) = 0xf8;
604 if (begin == 1) *dataptr = 0xf8;
606 else
607 return 0;
609 return 1;
613 /***********************************************************************
614 * DRIVE_RawWrite
616 * Write raw sectors to a device
618 int DRIVE_RawWrite(BYTE drive, DWORD begin, DWORD nr_sect, BYTE *dataptr, BOOL32 fake_success)
620 int fd;
622 if ((fd = DRIVE_OpenDevice( drive, O_RDONLY )) != -1)
624 lseek( fd, begin * 512, SEEK_SET );
625 /* FIXME: check errors */
626 write( fd, dataptr, nr_sect * 512 );
627 close( fd );
629 else
630 if (!(fake_success))
631 return 0;
633 return 1;
637 /***********************************************************************
638 * DRIVE_GetFreeSpace
640 static int DRIVE_GetFreeSpace( int drive, LPULARGE_INTEGER size,
641 LPULARGE_INTEGER available )
643 struct statfs info;
644 unsigned long long bigsize,bigavail=0;
646 if (!DRIVE_IsValid(drive))
648 SetLastError( ERROR_INVALID_DRIVE );
649 return 0;
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
659 FILE_SetDosError();
660 WARN(dosfs, "cannot do statfs(%s)\n", DOSDrives[drive].root);
661 return 0;
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;
685 /***********************************************************************
686 * GetDiskFreeSpace16 (KERNEL.422)
688 BOOL16 WINAPI GetDiskFreeSpace16( LPCSTR root, LPDWORD cluster_sectors,
689 LPDWORD sector_bytes, LPDWORD free_clusters,
690 LPDWORD total_clusters )
692 return GetDiskFreeSpace32A( root, cluster_sectors, sector_bytes,
693 free_clusters, total_clusters );
697 /***********************************************************************
698 * GetDiskFreeSpace32A (KERNEL32.206)
700 * Fails if expression resulting from current drive's dir and "root"
701 * is not a root dir of the target drive.
703 * UNDOC: setting some LPDWORDs to NULL is perfectly possible
704 * if the corresponding info is unneeded.
706 * FIXME: needs to support UNC names from Win95 OSR2 on.
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")
724 BOOL32 WINAPI GetDiskFreeSpace32A( LPCSTR root, LPDWORD cluster_sectors,
725 LPDWORD sector_bytes, LPDWORD free_clusters,
726 LPDWORD total_clusters )
728 int drive;
729 ULARGE_INTEGER size,available;
730 LPCSTR path;
731 DWORD cluster_sec;
733 if ((!root) || (root == "\\"))
734 drive = DRIVE_GetCurrentDrive();
735 else
736 if ( (strlen(root) >= 2) && (root[1] == ':')) /* root contains drive tag */
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;
748 else
749 return FALSE;
751 if (!DRIVE_GetFreeSpace(drive, &size, &available)) return FALSE;
753 /* Cap the size and available at 2GB as per specs. */
754 if ((size.HighPart) ||(size.LowPart > 0x7fffffff))
756 size.HighPart = 0;
757 size.LowPart = 0x7fffffff;
759 if ((available.HighPart) ||(available.LowPart > 0x7fffffff))
761 available.HighPart =0;
762 available.LowPart = 0x7fffffff;
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;
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;
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;
789 /***********************************************************************
790 * GetDiskFreeSpace32W (KERNEL32.207)
792 BOOL32 WINAPI GetDiskFreeSpace32W( LPCWSTR root, LPDWORD cluster_sectors,
793 LPDWORD sector_bytes, LPDWORD free_clusters,
794 LPDWORD total_clusters )
796 LPSTR xroot;
797 BOOL32 ret;
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;
807 /***********************************************************************
808 * GetDiskFreeSpaceEx32A (KERNEL32.871)
810 BOOL32 WINAPI GetDiskFreeSpaceEx32A( LPCSTR root,
811 LPULARGE_INTEGER avail,
812 LPULARGE_INTEGER total,
813 LPULARGE_INTEGER totalfree)
815 int drive;
816 ULARGE_INTEGER size,available;
818 if (!root) drive = DRIVE_GetCurrentDrive();
819 else
821 if ((root[1]) && ((root[1] != ':') || (root[2] != '\\')))
823 WARN(dosfs, "invalid root '%s'\n", root );
824 return FALSE;
826 drive = toupper(root[0]) - 'A';
828 if (!DRIVE_GetFreeSpace(drive, &size, &available)) return FALSE;
829 /*FIXME: Do we have the number of bytes available to the user? */
830 avail->HighPart = available.HighPart;
831 totalfree->HighPart = size.HighPart;
832 avail->LowPart = available.LowPart ;
833 totalfree->LowPart = size.LowPart ;
834 return TRUE;
837 /***********************************************************************
838 * GetDiskFreeSpaceEx32W (KERNEL32.873)
840 BOOL32 WINAPI GetDiskFreeSpaceEx32W( LPCWSTR root, LPULARGE_INTEGER avail,
841 LPULARGE_INTEGER total,
842 LPULARGE_INTEGER totalfree)
844 LPSTR xroot;
845 BOOL32 ret;
847 xroot = HEAP_strdupWtoA( GetProcessHeap(), 0, root);
848 ret = GetDiskFreeSpaceEx32A( xroot, avail, total, totalfree);
849 HeapFree( GetProcessHeap(), 0, xroot );
850 return ret;
853 /***********************************************************************
854 * GetDriveType16 (KERNEL.136)
855 * This functions returns the drivetype of a drive in Win16.
856 * Note that it returns DRIVE_REMOTE for CD-ROMs, since MSCDEX uses the
857 * remote drive API. The returnvalue DRIVE_REMOTE for CD-ROMs has been
858 * verified on Win3.11 and Windows 95. Some programs rely on it, so don't
859 * do any pseudo-clever changes.
861 * RETURNS
862 * drivetype DRIVE_xxx
864 UINT16 WINAPI GetDriveType16(
865 UINT16 drive /* [in] number (NOT letter) of drive */
867 TRACE(dosfs, "(%c:)\n", 'A' + drive );
868 switch(DRIVE_GetType(drive))
870 case TYPE_FLOPPY: return DRIVE_REMOVABLE;
871 case TYPE_HD: return DRIVE_FIXED;
872 case TYPE_CDROM: return DRIVE_REMOTE;
873 case TYPE_NETWORK: return DRIVE_REMOTE;
874 case TYPE_INVALID:
875 default: return DRIVE_CANNOTDETERMINE;
880 /***********************************************************************
881 * GetDriveType32A (KERNEL32.208)
883 * Returns the type of the disk drive specified. If root is NULL the
884 * root of the current directory is used.
886 * RETURNS
888 * Type of drive (from Win32 SDK):
890 * DRIVE_UNKNOWN unable to find out anything about the drive
891 * DRIVE_NO_ROOT_DIR nonexistand root dir
892 * DRIVE_REMOVABLE the disk can be removed from the machine
893 * DRIVE_FIXED the disk can not be removed from the machine
894 * DRIVE_REMOTE network disk
895 * DRIVE_CDROM CDROM drive
896 * DRIVE_RAMDISK virtual disk in ram
898 * DRIVE_DOESNOTEXIST XXX Not valid return value
899 * DRIVE_CANNOTDETERMINE XXX Not valid return value
901 * BUGS
903 * Currently returns DRIVE_DOESNOTEXIST and DRIVE_CANNOTDETERMINE
904 * when it really should return DRIVE_NO_ROOT_DIR and DRIVE_UNKNOWN.
905 * Why where the former defines used?
907 * DRIVE_RAMDISK is unsupported.
909 UINT32 WINAPI GetDriveType32A(LPCSTR root /* String describing drive */)
911 int drive;
912 TRACE(dosfs, "(%s)\n", debugstr_a(root));
914 if (NULL == root) drive = DRIVE_GetCurrentDrive();
915 else
917 if ((root[1]) && (root[1] != ':'))
919 WARN(dosfs, "invalid root '%s'\n", debugstr_a(root));
920 return DRIVE_DOESNOTEXIST;
922 drive = toupper(root[0]) - 'A';
924 switch(DRIVE_GetType(drive))
926 case TYPE_FLOPPY: return DRIVE_REMOVABLE;
927 case TYPE_HD: return DRIVE_FIXED;
928 case TYPE_CDROM: return DRIVE_CDROM;
929 case TYPE_NETWORK: return DRIVE_REMOTE;
930 case TYPE_INVALID: return DRIVE_DOESNOTEXIST;
931 default: return DRIVE_CANNOTDETERMINE;
936 /***********************************************************************
937 * GetDriveType32W (KERNEL32.209)
939 UINT32 WINAPI GetDriveType32W( LPCWSTR root )
941 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, root );
942 UINT32 ret = GetDriveType32A( xpath );
943 HeapFree( GetProcessHeap(), 0, xpath );
944 return ret;
948 /***********************************************************************
949 * GetCurrentDirectory16 (KERNEL.411)
951 UINT16 WINAPI GetCurrentDirectory16( UINT16 buflen, LPSTR buf )
953 return (UINT16)GetCurrentDirectory32A( buflen, buf );
957 /***********************************************************************
958 * GetCurrentDirectory32A (KERNEL32.196)
960 * Returns "X:\\path\\etc\\".
962 * Despite the API description, return required length including the
963 * terminating null when buffer too small. This is the real behaviour.
965 UINT32 WINAPI GetCurrentDirectory32A( UINT32 buflen, LPSTR buf )
967 UINT32 ret;
968 const char *s = DRIVE_GetDosCwd( DRIVE_GetCurrentDrive() );
970 assert(s);
971 ret = strlen(s) + 3; /* length of WHOLE current directory */
972 if (ret >= buflen) return ret + 1;
973 lstrcpyn32A( buf, "A:\\", MIN( 4, buflen ) );
974 if (buflen) buf[0] += DRIVE_GetCurrentDrive();
975 if (buflen > 3) lstrcpyn32A( buf + 3, s, buflen - 3 );
976 return ret;
980 /***********************************************************************
981 * GetCurrentDirectory32W (KERNEL32.197)
983 UINT32 WINAPI GetCurrentDirectory32W( UINT32 buflen, LPWSTR buf )
985 LPSTR xpath = HeapAlloc( GetProcessHeap(), 0, buflen+1 );
986 UINT32 ret = GetCurrentDirectory32A( buflen, xpath );
987 lstrcpyAtoW( buf, xpath );
988 HeapFree( GetProcessHeap(), 0, xpath );
989 return ret;
993 /***********************************************************************
994 * SetCurrentDirectory (KERNEL.412)
996 BOOL16 WINAPI SetCurrentDirectory16( LPCSTR dir )
998 return SetCurrentDirectory32A( dir );
1002 /***********************************************************************
1003 * SetCurrentDirectory32A (KERNEL32.479)
1005 BOOL32 WINAPI SetCurrentDirectory32A( LPCSTR dir )
1007 int olddrive, drive = DRIVE_GetCurrentDrive();
1009 if (!dir) {
1010 ERR(file,"(NULL)!\n");
1011 return FALSE;
1013 if (dir[0] && (dir[1]==':'))
1015 drive = tolower( *dir ) - 'a';
1016 dir += 2;
1019 /* WARNING: we need to set the drive before the dir, as DRIVE_Chdir
1020 sets pTask->curdir only if pTask->curdrive is drive */
1021 olddrive = drive; /* in case DRIVE_Chdir fails */
1022 if (!(DRIVE_SetCurrentDrive( drive )))
1023 return FALSE;
1024 /* FIXME: what about empty strings? Add a \\ ? */
1025 if (!DRIVE_Chdir( drive, dir )) {
1026 DRIVE_SetCurrentDrive(olddrive);
1027 return FALSE;
1029 return TRUE;
1033 /***********************************************************************
1034 * SetCurrentDirectory32W (KERNEL32.480)
1036 BOOL32 WINAPI SetCurrentDirectory32W( LPCWSTR dirW )
1038 LPSTR dir = HEAP_strdupWtoA( GetProcessHeap(), 0, dirW );
1039 BOOL32 res = SetCurrentDirectory32A( dir );
1040 HeapFree( GetProcessHeap(), 0, dir );
1041 return res;
1045 /***********************************************************************
1046 * GetLogicalDriveStrings32A (KERNEL32.231)
1048 UINT32 WINAPI GetLogicalDriveStrings32A( UINT32 len, LPSTR buffer )
1050 int drive, count;
1052 for (drive = count = 0; drive < MAX_DOS_DRIVES; drive++)
1053 if (DRIVE_IsValid(drive)) count++;
1054 if (count * 4 * sizeof(char) <= len)
1056 LPSTR p = buffer;
1057 for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
1058 if (DRIVE_IsValid(drive))
1060 *p++ = 'a' + drive;
1061 *p++ = ':';
1062 *p++ = '\\';
1063 *p++ = '\0';
1065 *p = '\0';
1067 return count * 4 * sizeof(char);
1071 /***********************************************************************
1072 * GetLogicalDriveStrings32W (KERNEL32.232)
1074 UINT32 WINAPI GetLogicalDriveStrings32W( UINT32 len, LPWSTR buffer )
1076 int drive, count;
1078 for (drive = count = 0; drive < MAX_DOS_DRIVES; drive++)
1079 if (DRIVE_IsValid(drive)) count++;
1080 if (count * 4 * sizeof(WCHAR) <= len)
1082 LPWSTR p = buffer;
1083 for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
1084 if (DRIVE_IsValid(drive))
1086 *p++ = (WCHAR)('a' + drive);
1087 *p++ = (WCHAR)':';
1088 *p++ = (WCHAR)'\\';
1089 *p++ = (WCHAR)'\0';
1091 *p = (WCHAR)'\0';
1093 return count * 4 * sizeof(WCHAR);
1097 /***********************************************************************
1098 * GetLogicalDrives (KERNEL32.233)
1100 DWORD WINAPI GetLogicalDrives(void)
1102 DWORD ret = 0;
1103 int drive;
1105 for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
1106 if (DRIVE_IsValid(drive)) ret |= (1 << drive);
1107 return ret;
1111 /***********************************************************************
1112 * GetVolumeInformation32A (KERNEL32.309)
1114 BOOL32 WINAPI GetVolumeInformation32A( LPCSTR root, LPSTR label,
1115 DWORD label_len, DWORD *serial,
1116 DWORD *filename_len, DWORD *flags,
1117 LPSTR fsname, DWORD fsname_len )
1119 int drive;
1120 char *cp;
1122 /* FIXME, SetLastErrors missing */
1124 if (!root) drive = DRIVE_GetCurrentDrive();
1125 else
1127 if ((root[1]) && (root[1] != ':'))
1129 WARN(dosfs, "invalid root '%s'\n",root);
1130 return FALSE;
1132 drive = toupper(root[0]) - 'A';
1134 if (!DRIVE_IsValid( drive )) return FALSE;
1135 if (label)
1137 lstrcpyn32A( label, DRIVE_GetLabel(drive), label_len );
1138 for (cp = label; *cp; cp++);
1139 while (cp != label && *(cp-1) == ' ') cp--;
1140 *cp = '\0';
1142 if (serial) *serial = DRIVE_GetSerialNumber(drive);
1144 /* Set the filesystem information */
1145 /* Note: we only emulate a FAT fs at the present */
1147 if (filename_len) {
1148 if (DOSDrives[drive].flags & DRIVE_SHORT_NAMES)
1149 *filename_len = 12;
1150 else
1151 *filename_len = 255;
1153 if (flags) *flags = 0;
1154 if (fsname) {
1155 /* Diablo checks that return code ... */
1156 if (DRIVE_GetType(drive)==TYPE_CDROM)
1157 lstrcpyn32A( fsname, "CDFS", fsname_len );
1158 else
1159 lstrcpyn32A( fsname, "FAT", fsname_len );
1161 return TRUE;
1165 /***********************************************************************
1166 * GetVolumeInformation32W (KERNEL32.310)
1168 BOOL32 WINAPI GetVolumeInformation32W( LPCWSTR root, LPWSTR label,
1169 DWORD label_len, DWORD *serial,
1170 DWORD *filename_len, DWORD *flags,
1171 LPWSTR fsname, DWORD fsname_len )
1173 LPSTR xroot = HEAP_strdupWtoA( GetProcessHeap(), 0, root );
1174 LPSTR xvolname = label ? HeapAlloc(GetProcessHeap(),0,label_len) : NULL;
1175 LPSTR xfsname = fsname ? HeapAlloc(GetProcessHeap(),0,fsname_len) : NULL;
1176 BOOL32 ret = GetVolumeInformation32A( xroot, xvolname, label_len, serial,
1177 filename_len, flags, xfsname,
1178 fsname_len );
1179 if (ret)
1181 if (label) lstrcpyAtoW( label, xvolname );
1182 if (fsname) lstrcpyAtoW( fsname, xfsname );
1184 HeapFree( GetProcessHeap(), 0, xroot );
1185 HeapFree( GetProcessHeap(), 0, xvolname );
1186 HeapFree( GetProcessHeap(), 0, xfsname );
1187 return ret;
1190 BOOL32 WINAPI SetVolumeLabel32A(LPCSTR rootpath,LPCSTR volname) {
1191 FIXME(dosfs,"(%s,%s),stub!\n",rootpath,volname);
1192 return TRUE;