Implemented async comm buffers (but probably not bugfree), and along
[wine.git] / files / drive.c
blob78806f47e16f7e6c4240d34bbdf478217258bd2c
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 "winbase.h"
36 #include "wine/winbase16.h" /* for GetCurrentTask */
37 #include "wine/winestring.h" /* for lstrcpyAtoW */
38 #include "winerror.h"
39 #include "drive.h"
40 #include "file.h"
41 #include "heap.h"
42 #include "msdos.h"
43 #include "options.h"
44 #include "task.h"
45 #include "debug.h"
47 typedef struct
49 char *root; /* root dir in Unix format without trailing / */
50 char *dos_cwd; /* cwd in DOS format without leading or trailing \ */
51 char *unix_cwd; /* cwd in Unix format without leading or trailing / */
52 char *device; /* raw device path */
53 char label[12]; /* drive label */
54 DWORD serial; /* drive serial number */
55 DRIVETYPE type; /* drive type */
56 UINT flags; /* drive flags */
57 dev_t dev; /* unix device number */
58 ino_t ino; /* unix inode number */
59 } DOSDRIVE;
62 static const char * const DRIVE_Types[] =
64 "floppy", /* TYPE_FLOPPY */
65 "hd", /* TYPE_HD */
66 "cdrom", /* TYPE_CDROM */
67 "network" /* TYPE_NETWORK */
71 /* Known filesystem types */
73 typedef struct
75 const char *name;
76 UINT flags;
77 } FS_DESCR;
79 static const FS_DESCR DRIVE_Filesystems[] =
81 { "unix", DRIVE_CASE_SENSITIVE | DRIVE_CASE_PRESERVING },
82 { "msdos", DRIVE_SHORT_NAMES },
83 { "dos", DRIVE_SHORT_NAMES },
84 { "fat", DRIVE_SHORT_NAMES },
85 { "vfat", DRIVE_CASE_PRESERVING },
86 { "win95", DRIVE_CASE_PRESERVING },
87 { NULL, 0 }
91 static DOSDRIVE DOSDrives[MAX_DOS_DRIVES];
92 static int DRIVE_CurDrive = -1;
94 static HTASK16 DRIVE_LastTask = 0;
97 /***********************************************************************
98 * DRIVE_GetDriveType
100 static DRIVETYPE DRIVE_GetDriveType( const char *name )
102 char buffer[20];
103 int i;
105 PROFILE_GetWineIniString( name, "Type", "hd", buffer, sizeof(buffer) );
106 for (i = 0; i < sizeof(DRIVE_Types)/sizeof(DRIVE_Types[0]); i++)
108 if (!strcasecmp( buffer, DRIVE_Types[i] )) return (DRIVETYPE)i;
110 MSG("%s: unknown type '%s', defaulting to 'hd'.\n", name, buffer );
111 return TYPE_HD;
115 /***********************************************************************
116 * DRIVE_GetFSFlags
118 static UINT DRIVE_GetFSFlags( const char *name, const char *value )
120 const FS_DESCR *descr;
122 for (descr = DRIVE_Filesystems; descr->name; descr++)
123 if (!strcasecmp( value, descr->name )) return descr->flags;
124 MSG("%s: unknown filesystem type '%s', defaulting to 'win95'.\n",
125 name, value );
126 return DRIVE_CASE_PRESERVING;
130 /***********************************************************************
131 * DRIVE_Init
133 int DRIVE_Init(void)
135 int i, len, count = 0;
136 char name[] = "Drive A";
137 char path[MAX_PATHNAME_LEN];
138 char buffer[80];
139 struct stat drive_stat_buffer;
140 char *p;
141 DOSDRIVE *drive;
143 for (i = 0, drive = DOSDrives; i < MAX_DOS_DRIVES; i++, name[6]++, drive++)
145 PROFILE_GetWineIniString( name, "Path", "", path, sizeof(path)-1 );
146 if (path[0])
148 p = path + strlen(path) - 1;
149 while ((p > path) && ((*p == '/') || (*p == '\\'))) *p-- = '\0';
150 if (!path[0]) strcpy( path, "/" );
152 if (stat( path, &drive_stat_buffer ))
154 MSG("Could not stat %s, ignoring drive %c:\n", path, 'A' + i );
155 continue;
157 if (!S_ISDIR(drive_stat_buffer.st_mode))
159 MSG("%s is not a directory, ignoring drive %c:\n",
160 path, 'A' + i );
161 continue;
164 drive->root = HEAP_strdupA( SystemHeap, 0, path );
165 drive->dos_cwd = HEAP_strdupA( SystemHeap, 0, "" );
166 drive->unix_cwd = HEAP_strdupA( SystemHeap, 0, "" );
167 drive->type = DRIVE_GetDriveType( name );
168 drive->device = NULL;
169 drive->flags = 0;
170 drive->dev = drive_stat_buffer.st_dev;
171 drive->ino = drive_stat_buffer.st_ino;
173 /* Get the drive label */
174 PROFILE_GetWineIniString( name, "Label", name, drive->label, 12 );
175 if ((len = strlen(drive->label)) < 11)
177 /* Pad label with spaces */
178 memset( drive->label + len, ' ', 11 - len );
179 drive->label[12] = '\0';
182 /* Get the serial number */
183 PROFILE_GetWineIniString( name, "Serial", "12345678",
184 buffer, sizeof(buffer) );
185 drive->serial = strtoul( buffer, NULL, 16 );
187 /* Get the filesystem type */
188 PROFILE_GetWineIniString( name, "Filesystem", "win95",
189 buffer, sizeof(buffer) );
190 drive->flags = DRIVE_GetFSFlags( name, buffer );
192 /* Get the device */
193 PROFILE_GetWineIniString( name, "Device", "",
194 buffer, sizeof(buffer) );
195 if (buffer[0])
196 drive->device = HEAP_strdupA( SystemHeap, 0, buffer );
198 /* Make the first hard disk the current drive */
199 if ((DRIVE_CurDrive == -1) && (drive->type == TYPE_HD))
200 DRIVE_CurDrive = i;
202 count++;
203 TRACE(dosfs, "%s: path=%s type=%s label='%s' serial=%08lx flags=%08x dev=%x ino=%x\n",
204 name, path, DRIVE_Types[drive->type],
205 drive->label, drive->serial, drive->flags,
206 (int)drive->dev, (int)drive->ino );
208 else WARN(dosfs, "%s: not defined\n", name );
211 if (!count)
213 MSG("Warning: no valid DOS drive found, check your configuration file.\n" );
214 /* Create a C drive pointing to Unix root dir */
215 DOSDrives[2].root = HEAP_strdupA( SystemHeap, 0, "/" );
216 DOSDrives[2].dos_cwd = HEAP_strdupA( SystemHeap, 0, "" );
217 DOSDrives[2].unix_cwd = HEAP_strdupA( SystemHeap, 0, "" );
218 strcpy( DOSDrives[2].label, "Drive C " );
219 DOSDrives[2].serial = 0x12345678;
220 DOSDrives[2].type = TYPE_HD;
221 DOSDrives[2].flags = 0;
222 DRIVE_CurDrive = 2;
225 /* Make sure the current drive is valid */
226 if (DRIVE_CurDrive == -1)
228 for (i = 0, drive = DOSDrives; i < MAX_DOS_DRIVES; i++, drive++)
230 if (drive->root && !(drive->flags & DRIVE_DISABLED))
232 DRIVE_CurDrive = i;
233 break;
238 return 1;
242 /***********************************************************************
243 * DRIVE_IsValid
245 int DRIVE_IsValid( int drive )
247 if ((drive < 0) || (drive >= MAX_DOS_DRIVES)) return 0;
248 return (DOSDrives[drive].root &&
249 !(DOSDrives[drive].flags & DRIVE_DISABLED));
253 /***********************************************************************
254 * DRIVE_GetCurrentDrive
256 int DRIVE_GetCurrentDrive(void)
258 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
259 if (pTask && (pTask->curdrive & 0x80)) return pTask->curdrive & ~0x80;
260 return DRIVE_CurDrive;
264 /***********************************************************************
265 * DRIVE_SetCurrentDrive
267 int DRIVE_SetCurrentDrive( int drive )
269 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
270 if (!DRIVE_IsValid( drive ))
272 SetLastError( ERROR_INVALID_DRIVE );
273 return 0;
275 TRACE(dosfs, "%c:\n", 'A' + drive );
276 DRIVE_CurDrive = drive;
277 if (pTask) pTask->curdrive = drive | 0x80;
278 return 1;
282 /***********************************************************************
283 * DRIVE_FindDriveRoot
285 * Find a drive for which the root matches the begginning of the given path.
286 * This can be used to translate a Unix path into a drive + DOS path.
287 * Return value is the drive, or -1 on error. On success, path is modified
288 * to point to the beginning of the DOS path.
290 int DRIVE_FindDriveRoot( const char **path )
292 /* idea: check at all '/' positions.
293 * If the device and inode of that path is identical with the
294 * device and inode of the current drive then we found a solution.
295 * If there is another drive pointing to a deeper position in
296 * the file tree, we want to find that one, not the earlier solution.
298 int drive, rootdrive = -1;
299 char buffer[MAX_PATHNAME_LEN];
300 char *next = buffer;
301 const char *p = *path;
302 struct stat st;
304 strcpy( buffer, "/" );
305 for (;;)
307 if (stat( buffer, &st ) || !S_ISDIR( st.st_mode )) break;
309 /* Find the drive */
311 for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
313 if (!DOSDrives[drive].root ||
314 (DOSDrives[drive].flags & DRIVE_DISABLED)) continue;
316 if ((DOSDrives[drive].dev == st.st_dev) &&
317 (DOSDrives[drive].ino == st.st_ino))
319 rootdrive = drive;
320 *path = p;
324 /* Get the next path component */
326 *next++ = '/';
327 while ((*p == '/') || (*p == '\\')) p++;
328 if (!*p) break;
329 while (!IS_END_OF_NAME(*p)) *next++ = *p++;
330 *next = 0;
332 *next = 0;
334 if (rootdrive != -1)
335 TRACE(dosfs, "%s -> drive %c:, root='%s', name='%s'\n",
336 buffer, 'A' + rootdrive,
337 DOSDrives[rootdrive].root, *path );
338 return rootdrive;
342 /***********************************************************************
343 * DRIVE_GetRoot
345 const char * DRIVE_GetRoot( int drive )
347 if (!DRIVE_IsValid( drive )) return NULL;
348 return DOSDrives[drive].root;
352 /***********************************************************************
353 * DRIVE_GetDosCwd
355 const char * DRIVE_GetDosCwd( int drive )
357 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
358 if (!DRIVE_IsValid( drive )) return NULL;
360 /* Check if we need to change the directory to the new task. */
361 if (pTask && (pTask->curdrive & 0x80) && /* The task drive is valid */
362 ((pTask->curdrive & ~0x80) == drive) && /* and it's the one we want */
363 (DRIVE_LastTask != GetCurrentTask())) /* and the task changed */
365 /* Perform the task-switch */
366 if (!DRIVE_Chdir( drive, pTask->curdir )) DRIVE_Chdir( drive, "\\" );
367 DRIVE_LastTask = GetCurrentTask();
369 return DOSDrives[drive].dos_cwd;
373 /***********************************************************************
374 * DRIVE_GetUnixCwd
376 const char * DRIVE_GetUnixCwd( int drive )
378 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
379 if (!DRIVE_IsValid( drive )) return NULL;
381 /* Check if we need to change the directory to the new task. */
382 if (pTask && (pTask->curdrive & 0x80) && /* The task drive is valid */
383 ((pTask->curdrive & ~0x80) == drive) && /* and it's the one we want */
384 (DRIVE_LastTask != GetCurrentTask())) /* and the task changed */
386 /* Perform the task-switch */
387 if (!DRIVE_Chdir( drive, pTask->curdir )) DRIVE_Chdir( drive, "\\" );
388 DRIVE_LastTask = GetCurrentTask();
390 return DOSDrives[drive].unix_cwd;
394 /***********************************************************************
395 * DRIVE_GetLabel
397 const char * DRIVE_GetLabel( int drive )
399 if (!DRIVE_IsValid( drive )) return NULL;
400 return DOSDrives[drive].label;
404 /***********************************************************************
405 * DRIVE_GetSerialNumber
407 DWORD DRIVE_GetSerialNumber( int drive )
409 if (!DRIVE_IsValid( drive )) return 0;
410 return DOSDrives[drive].serial;
414 /***********************************************************************
415 * DRIVE_SetSerialNumber
417 int DRIVE_SetSerialNumber( int drive, DWORD serial )
419 if (!DRIVE_IsValid( drive )) return 0;
420 DOSDrives[drive].serial = serial;
421 return 1;
425 /***********************************************************************
426 * DRIVE_GetType
428 DRIVETYPE DRIVE_GetType( int drive )
430 if (!DRIVE_IsValid( drive )) return TYPE_INVALID;
431 return DOSDrives[drive].type;
435 /***********************************************************************
436 * DRIVE_GetFlags
438 UINT DRIVE_GetFlags( int drive )
440 if ((drive < 0) || (drive >= MAX_DOS_DRIVES)) return 0;
441 return DOSDrives[drive].flags;
445 /***********************************************************************
446 * DRIVE_Chdir
448 int DRIVE_Chdir( int drive, const char *path )
450 DOS_FULL_NAME full_name;
451 char buffer[MAX_PATHNAME_LEN];
452 LPSTR unix_cwd;
453 BY_HANDLE_FILE_INFORMATION info;
454 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
456 strcpy( buffer, "A:" );
457 buffer[0] += drive;
458 TRACE(dosfs, "(%c:,%s)\n", buffer[0], path );
459 lstrcpynA( buffer + 2, path, sizeof(buffer) - 2 );
461 if (!DOSFS_GetFullName( buffer, TRUE, &full_name )) return 0;
462 if (!FILE_Stat( full_name.long_name, &info )) return 0;
463 if (!(info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
465 SetLastError( ERROR_FILE_NOT_FOUND );
466 return 0;
468 unix_cwd = full_name.long_name + strlen( DOSDrives[drive].root );
469 while (*unix_cwd == '/') unix_cwd++;
471 TRACE(dosfs, "(%c:): unix_cwd=%s dos_cwd=%s\n",
472 'A' + drive, unix_cwd, full_name.short_name + 3 );
474 HeapFree( SystemHeap, 0, DOSDrives[drive].dos_cwd );
475 HeapFree( SystemHeap, 0, DOSDrives[drive].unix_cwd );
476 DOSDrives[drive].dos_cwd = HEAP_strdupA( SystemHeap, 0,
477 full_name.short_name + 3 );
478 DOSDrives[drive].unix_cwd = HEAP_strdupA( SystemHeap, 0, unix_cwd );
480 if (pTask && (pTask->curdrive & 0x80) &&
481 ((pTask->curdrive & ~0x80) == drive))
483 lstrcpynA( pTask->curdir, full_name.short_name + 2,
484 sizeof(pTask->curdir) );
485 DRIVE_LastTask = GetCurrentTask();
487 return 1;
491 /***********************************************************************
492 * DRIVE_Disable
494 int DRIVE_Disable( int drive )
496 if ((drive < 0) || (drive >= MAX_DOS_DRIVES) || !DOSDrives[drive].root)
498 SetLastError( ERROR_INVALID_DRIVE );
499 return 0;
501 DOSDrives[drive].flags |= DRIVE_DISABLED;
502 return 1;
506 /***********************************************************************
507 * DRIVE_Enable
509 int DRIVE_Enable( int drive )
511 if ((drive < 0) || (drive >= MAX_DOS_DRIVES) || !DOSDrives[drive].root)
513 SetLastError( ERROR_INVALID_DRIVE );
514 return 0;
516 DOSDrives[drive].flags &= ~DRIVE_DISABLED;
517 return 1;
521 /***********************************************************************
522 * DRIVE_SetLogicalMapping
524 int DRIVE_SetLogicalMapping ( int existing_drive, int new_drive )
526 /* If new_drive is already valid, do nothing and return 0
527 otherwise, copy DOSDrives[existing_drive] to DOSDrives[new_drive] */
529 DOSDRIVE *old, *new;
531 old = DOSDrives + existing_drive;
532 new = DOSDrives + new_drive;
534 if ((existing_drive < 0) || (existing_drive >= MAX_DOS_DRIVES) ||
535 !old->root ||
536 (new_drive < 0) || (new_drive >= MAX_DOS_DRIVES))
538 SetLastError( ERROR_INVALID_DRIVE );
539 return 0;
542 if ( new->root )
544 TRACE(dosfs, "Can\'t map drive %c to drive %c - "
545 "drive %c already exists\n",
546 'A' + existing_drive, 'A' + new_drive,
547 'A' + new_drive );
548 /* it is already mapped there, so return success */
549 if (!strcmp(old->root,new->root))
550 return 1;
551 return 0;
554 new->root = HEAP_strdupA( SystemHeap, 0, old->root );
555 new->dos_cwd = HEAP_strdupA( SystemHeap, 0, old->dos_cwd );
556 new->unix_cwd = HEAP_strdupA( SystemHeap, 0, old->unix_cwd );
557 memcpy ( new->label, old->label, 12 );
558 new->serial = old->serial;
559 new->type = old->type;
560 new->flags = old->flags;
561 new->dev = old->dev;
562 new->ino = old->ino;
564 TRACE(dosfs, "Drive %c is now equal to drive %c\n",
565 'A' + new_drive, 'A' + existing_drive );
567 return 1;
571 /***********************************************************************
572 * DRIVE_OpenDevice
574 * Open the drive raw device and return a Unix fd (or -1 on error).
576 int DRIVE_OpenDevice( int drive, int flags )
578 if (!DRIVE_IsValid( drive )) return -1;
579 return open( DOSDrives[drive].device, flags );
583 /***********************************************************************
584 * DRIVE_RawRead
586 * Read raw sectors from a device
588 int DRIVE_RawRead(BYTE drive, DWORD begin, DWORD nr_sect, BYTE *dataptr, BOOL fake_success)
590 int fd;
592 if ((fd = DRIVE_OpenDevice( drive, O_RDONLY )) != -1)
594 lseek( fd, begin * 512, SEEK_SET );
595 /* FIXME: check errors */
596 read( fd, dataptr, nr_sect * 512 );
597 close( fd );
599 else
601 memset(dataptr, 0, nr_sect * 512);
602 if (fake_success)
604 if (begin == 0 && nr_sect > 1) *(dataptr + 512) = 0xf8;
605 if (begin == 1) *dataptr = 0xf8;
607 else
608 return 0;
610 return 1;
614 /***********************************************************************
615 * DRIVE_RawWrite
617 * Write raw sectors to a device
619 int DRIVE_RawWrite(BYTE drive, DWORD begin, DWORD nr_sect, BYTE *dataptr, BOOL fake_success)
621 int fd;
623 if ((fd = DRIVE_OpenDevice( drive, O_RDONLY )) != -1)
625 lseek( fd, begin * 512, SEEK_SET );
626 /* FIXME: check errors */
627 write( fd, dataptr, nr_sect * 512 );
628 close( fd );
630 else
631 if (!(fake_success))
632 return 0;
634 return 1;
638 /***********************************************************************
639 * DRIVE_GetFreeSpace
641 static int DRIVE_GetFreeSpace( int drive, PULARGE_INTEGER size,
642 PULARGE_INTEGER available )
644 struct statfs info;
645 unsigned long long bigsize,bigavail=0;
647 if (!DRIVE_IsValid(drive))
649 SetLastError( ERROR_INVALID_DRIVE );
650 return 0;
653 /* FIXME: add autoconf check for this */
654 #if defined(__svr4__) || defined(_SCO_DS)
655 if (statfs( DOSDrives[drive].root, &info, 0, 0) < 0)
656 #else
657 if (statfs( DOSDrives[drive].root, &info) < 0)
658 #endif
660 FILE_SetDosError();
661 WARN(dosfs, "cannot do statfs(%s)\n", DOSDrives[drive].root);
662 return 0;
665 bigsize = (unsigned long long)info.f_bsize
666 * (unsigned long long)info.f_blocks;
667 #ifdef STATFS_HAS_BAVAIL
668 bigavail = (unsigned long long)info.f_bavail
669 * (unsigned long long)info.f_bsize;
670 #else
671 # ifdef STATFS_HAS_BFREE
672 bigavail = (unsigned long long)info.f_bfree
673 * (unsigned long long)info.f_bsize;
674 # else
675 # error "statfs has no bfree/bavail member!"
676 # endif
677 #endif
678 size->LowPart = (DWORD)bigsize;
679 size->HighPart = (DWORD)(bigsize>>32);
680 available->LowPart = (DWORD)bigavail;
681 available->HighPart = (DWORD)(bigavail>>32);
682 return 1;
686 /***********************************************************************
687 * GetDiskFreeSpace16 (KERNEL.422)
689 BOOL16 WINAPI GetDiskFreeSpace16( LPCSTR root, LPDWORD cluster_sectors,
690 LPDWORD sector_bytes, LPDWORD free_clusters,
691 LPDWORD total_clusters )
693 return GetDiskFreeSpaceA( root, cluster_sectors, sector_bytes,
694 free_clusters, total_clusters );
698 /***********************************************************************
699 * GetDiskFreeSpace32A (KERNEL32.206)
701 * Fails if expression resulting from current drive's dir and "root"
702 * is not a root dir of the target drive.
704 * UNDOC: setting some LPDWORDs to NULL is perfectly possible
705 * if the corresponding info is unneeded.
707 * FIXME: needs to support UNC names from Win95 OSR2 on.
709 * Behaviour under Win95a:
710 * CurrDir root result
711 * "E:\\TEST" "E:" FALSE
712 * "E:\\" "E:" TRUE
713 * "E:\\" "E" FALSE
714 * "E:\\" "\\" TRUE
715 * "E:\\TEST" "\\" TRUE
716 * "E:\\TEST" ":\\" FALSE
717 * "E:\\TEST" "E:\\" TRUE
718 * "E:\\TEST" "" FALSE
719 * "E:\\" "" FALSE (!)
720 * "E:\\" 0x0 TRUE
721 * "E:\\TEST" 0x0 TRUE (!)
722 * "E:\\TEST" "C:" TRUE (when CurrDir of "C:" set to "\\")
723 * "E:\\TEST" "C:" FALSE (when CurrDir of "C:" set to "\\TEST")
725 BOOL WINAPI GetDiskFreeSpaceA( LPCSTR root, LPDWORD cluster_sectors,
726 LPDWORD sector_bytes, LPDWORD free_clusters,
727 LPDWORD total_clusters )
729 int drive;
730 ULARGE_INTEGER size,available;
731 LPCSTR path;
732 DWORD cluster_sec;
734 if ((!root) || (root == "\\"))
735 drive = DRIVE_GetCurrentDrive();
736 else
737 if ( (strlen(root) >= 2) && (root[1] == ':')) /* root contains drive tag */
739 drive = toupper(root[0]) - 'A';
740 path = &root[2];
741 if (path[0] == '\0')
742 path = DRIVE_GetDosCwd(drive);
743 else
744 if (path[0] == '\\')
745 path++;
746 if (strlen(path)) /* oops, we are in a subdir */
747 return FALSE;
749 else
750 return FALSE;
752 if (!DRIVE_GetFreeSpace(drive, &size, &available)) return FALSE;
754 /* Cap the size and available at 2GB as per specs. */
755 if ((size.HighPart) ||(size.LowPart > 0x7fffffff))
757 size.HighPart = 0;
758 size.LowPart = 0x7fffffff;
760 if ((available.HighPart) ||(available.LowPart > 0x7fffffff))
762 available.HighPart =0;
763 available.LowPart = 0x7fffffff;
765 if (DRIVE_GetType(drive)==TYPE_CDROM) {
766 if (sector_bytes)
767 *sector_bytes = 2048;
768 size.LowPart /= 2048;
769 available.LowPart /= 2048;
770 } else {
771 if (sector_bytes)
772 *sector_bytes = 512;
773 size.LowPart /= 512;
774 available.LowPart /= 512;
776 /* fixme: probably have to adjust those variables too for CDFS */
777 cluster_sec = 1;
778 while (cluster_sec * 65536 < size.LowPart) cluster_sec *= 2;
780 if (cluster_sectors)
781 *cluster_sectors = cluster_sec;
782 if (free_clusters)
783 *free_clusters = available.LowPart / cluster_sec;
784 if (total_clusters)
785 *total_clusters = size.LowPart / cluster_sec;
786 return TRUE;
790 /***********************************************************************
791 * GetDiskFreeSpace32W (KERNEL32.207)
793 BOOL WINAPI GetDiskFreeSpaceW( LPCWSTR root, LPDWORD cluster_sectors,
794 LPDWORD sector_bytes, LPDWORD free_clusters,
795 LPDWORD total_clusters )
797 LPSTR xroot;
798 BOOL ret;
800 xroot = HEAP_strdupWtoA( GetProcessHeap(), 0, root);
801 ret = GetDiskFreeSpaceA( xroot,cluster_sectors, sector_bytes,
802 free_clusters, total_clusters );
803 HeapFree( GetProcessHeap(), 0, xroot );
804 return ret;
808 /***********************************************************************
809 * GetDiskFreeSpaceEx32A (KERNEL32.871)
811 BOOL WINAPI GetDiskFreeSpaceExA( LPCSTR root,
812 PULARGE_INTEGER avail,
813 PULARGE_INTEGER total,
814 PULARGE_INTEGER totalfree)
816 int drive;
817 ULARGE_INTEGER size,available;
819 if (!root) drive = DRIVE_GetCurrentDrive();
820 else
822 if ((root[1]) && ((root[1] != ':') || (root[2] != '\\')))
824 WARN(dosfs, "invalid root '%s'\n", root );
825 return FALSE;
827 drive = toupper(root[0]) - 'A';
829 if (!DRIVE_GetFreeSpace(drive, &size, &available)) return FALSE;
830 /*FIXME: Do we have the number of bytes available to the user? */
831 if (totalfree) {
832 totalfree->HighPart = size.HighPart;
833 totalfree->LowPart = size.LowPart ;
835 if (avail) {
836 avail->HighPart = available.HighPart;
837 avail->LowPart = available.LowPart ;
839 return TRUE;
842 /***********************************************************************
843 * GetDiskFreeSpaceEx32W (KERNEL32.873)
845 BOOL WINAPI GetDiskFreeSpaceExW( LPCWSTR root, PULARGE_INTEGER avail,
846 PULARGE_INTEGER total,
847 PULARGE_INTEGER totalfree)
849 LPSTR xroot;
850 BOOL ret;
852 xroot = HEAP_strdupWtoA( GetProcessHeap(), 0, root);
853 ret = GetDiskFreeSpaceExA( xroot, avail, total, totalfree);
854 HeapFree( GetProcessHeap(), 0, xroot );
855 return ret;
858 /***********************************************************************
859 * GetDriveType16 (KERNEL.136)
860 * This functions returns the drivetype of a drive in Win16.
861 * Note that it returns DRIVE_REMOTE for CD-ROMs, since MSCDEX uses the
862 * remote drive API. The returnvalue DRIVE_REMOTE for CD-ROMs has been
863 * verified on Win3.11 and Windows 95. Some programs rely on it, so don't
864 * do any pseudo-clever changes.
866 * RETURNS
867 * drivetype DRIVE_xxx
869 UINT16 WINAPI GetDriveType16(
870 UINT16 drive /* [in] number (NOT letter) of drive */
872 TRACE(dosfs, "(%c:)\n", 'A' + drive );
873 switch(DRIVE_GetType(drive))
875 case TYPE_FLOPPY: return DRIVE_REMOVABLE;
876 case TYPE_HD: return DRIVE_FIXED;
877 case TYPE_CDROM: return DRIVE_REMOTE;
878 case TYPE_NETWORK: return DRIVE_REMOTE;
879 case TYPE_INVALID:
880 default: return DRIVE_CANNOTDETERMINE;
885 /***********************************************************************
886 * GetDriveType32A (KERNEL32.208)
888 * Returns the type of the disk drive specified. If root is NULL the
889 * root of the current directory is used.
891 * RETURNS
893 * Type of drive (from Win32 SDK):
895 * DRIVE_UNKNOWN unable to find out anything about the drive
896 * DRIVE_NO_ROOT_DIR nonexistand root dir
897 * DRIVE_REMOVABLE the disk can be removed from the machine
898 * DRIVE_FIXED the disk can not be removed from the machine
899 * DRIVE_REMOTE network disk
900 * DRIVE_CDROM CDROM drive
901 * DRIVE_RAMDISK virtual disk in ram
903 * DRIVE_DOESNOTEXIST XXX Not valid return value
904 * DRIVE_CANNOTDETERMINE XXX Not valid return value
906 * BUGS
908 * Currently returns DRIVE_DOESNOTEXIST and DRIVE_CANNOTDETERMINE
909 * when it really should return DRIVE_NO_ROOT_DIR and DRIVE_UNKNOWN.
910 * Why where the former defines used?
912 * DRIVE_RAMDISK is unsupported.
914 UINT WINAPI GetDriveTypeA(LPCSTR root /* String describing drive */)
916 int drive;
917 TRACE(dosfs, "(%s)\n", debugstr_a(root));
919 if (NULL == root) drive = DRIVE_GetCurrentDrive();
920 else
922 if ((root[1]) && (root[1] != ':'))
924 WARN(dosfs, "invalid root '%s'\n", debugstr_a(root));
925 return DRIVE_DOESNOTEXIST;
927 drive = toupper(root[0]) - 'A';
929 switch(DRIVE_GetType(drive))
931 case TYPE_FLOPPY: return DRIVE_REMOVABLE;
932 case TYPE_HD: return DRIVE_FIXED;
933 case TYPE_CDROM: return DRIVE_CDROM;
934 case TYPE_NETWORK: return DRIVE_REMOTE;
935 case TYPE_INVALID: return DRIVE_DOESNOTEXIST;
936 default: return DRIVE_CANNOTDETERMINE;
941 /***********************************************************************
942 * GetDriveType32W (KERNEL32.209)
944 UINT WINAPI GetDriveTypeW( LPCWSTR root )
946 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, root );
947 UINT ret = GetDriveTypeA( xpath );
948 HeapFree( GetProcessHeap(), 0, xpath );
949 return ret;
953 /***********************************************************************
954 * GetCurrentDirectory16 (KERNEL.411)
956 UINT16 WINAPI GetCurrentDirectory16( UINT16 buflen, LPSTR buf )
958 return (UINT16)GetCurrentDirectoryA( buflen, buf );
962 /***********************************************************************
963 * GetCurrentDirectory32A (KERNEL32.196)
965 * Returns "X:\\path\\etc\\".
967 * Despite the API description, return required length including the
968 * terminating null when buffer too small. This is the real behaviour.
970 UINT WINAPI GetCurrentDirectoryA( UINT buflen, LPSTR buf )
972 UINT ret;
973 const char *s = DRIVE_GetDosCwd( DRIVE_GetCurrentDrive() );
975 assert(s);
976 ret = strlen(s) + 3; /* length of WHOLE current directory */
977 if (ret >= buflen) return ret + 1;
978 lstrcpynA( buf, "A:\\", MIN( 4, buflen ) );
979 if (buflen) buf[0] += DRIVE_GetCurrentDrive();
980 if (buflen > 3) lstrcpynA( buf + 3, s, buflen - 3 );
981 return ret;
985 /***********************************************************************
986 * GetCurrentDirectory32W (KERNEL32.197)
988 UINT WINAPI GetCurrentDirectoryW( UINT buflen, LPWSTR buf )
990 LPSTR xpath = HeapAlloc( GetProcessHeap(), 0, buflen+1 );
991 UINT ret = GetCurrentDirectoryA( buflen, xpath );
992 lstrcpyAtoW( buf, xpath );
993 HeapFree( GetProcessHeap(), 0, xpath );
994 return ret;
998 /***********************************************************************
999 * SetCurrentDirectory (KERNEL.412)
1001 BOOL16 WINAPI SetCurrentDirectory16( LPCSTR dir )
1003 return SetCurrentDirectoryA( dir );
1007 /***********************************************************************
1008 * SetCurrentDirectory32A (KERNEL32.479)
1010 BOOL WINAPI SetCurrentDirectoryA( LPCSTR dir )
1012 int olddrive, drive = DRIVE_GetCurrentDrive();
1014 if (!dir) {
1015 ERR(file,"(NULL)!\n");
1016 return FALSE;
1018 if (dir[0] && (dir[1]==':'))
1020 drive = tolower( *dir ) - 'a';
1021 dir += 2;
1024 /* WARNING: we need to set the drive before the dir, as DRIVE_Chdir
1025 sets pTask->curdir only if pTask->curdrive is drive */
1026 olddrive = drive; /* in case DRIVE_Chdir fails */
1027 if (!(DRIVE_SetCurrentDrive( drive )))
1028 return FALSE;
1029 /* FIXME: what about empty strings? Add a \\ ? */
1030 if (!DRIVE_Chdir( drive, dir )) {
1031 DRIVE_SetCurrentDrive(olddrive);
1032 return FALSE;
1034 return TRUE;
1038 /***********************************************************************
1039 * SetCurrentDirectory32W (KERNEL32.480)
1041 BOOL WINAPI SetCurrentDirectoryW( LPCWSTR dirW )
1043 LPSTR dir = HEAP_strdupWtoA( GetProcessHeap(), 0, dirW );
1044 BOOL res = SetCurrentDirectoryA( dir );
1045 HeapFree( GetProcessHeap(), 0, dir );
1046 return res;
1050 /***********************************************************************
1051 * GetLogicalDriveStrings32A (KERNEL32.231)
1053 UINT WINAPI GetLogicalDriveStringsA( UINT len, LPSTR buffer )
1055 int drive, count;
1057 for (drive = count = 0; drive < MAX_DOS_DRIVES; drive++)
1058 if (DRIVE_IsValid(drive)) count++;
1059 if (count * 4 * sizeof(char) <= len)
1061 LPSTR p = buffer;
1062 for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
1063 if (DRIVE_IsValid(drive))
1065 *p++ = 'a' + drive;
1066 *p++ = ':';
1067 *p++ = '\\';
1068 *p++ = '\0';
1070 *p = '\0';
1072 return count * 4 * sizeof(char);
1076 /***********************************************************************
1077 * GetLogicalDriveStrings32W (KERNEL32.232)
1079 UINT WINAPI GetLogicalDriveStringsW( UINT len, LPWSTR buffer )
1081 int drive, count;
1083 for (drive = count = 0; drive < MAX_DOS_DRIVES; drive++)
1084 if (DRIVE_IsValid(drive)) count++;
1085 if (count * 4 * sizeof(WCHAR) <= len)
1087 LPWSTR p = buffer;
1088 for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
1089 if (DRIVE_IsValid(drive))
1091 *p++ = (WCHAR)('a' + drive);
1092 *p++ = (WCHAR)':';
1093 *p++ = (WCHAR)'\\';
1094 *p++ = (WCHAR)'\0';
1096 *p = (WCHAR)'\0';
1098 return count * 4 * sizeof(WCHAR);
1102 /***********************************************************************
1103 * GetLogicalDrives (KERNEL32.233)
1105 DWORD WINAPI GetLogicalDrives(void)
1107 DWORD ret = 0;
1108 int drive;
1110 for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
1111 if (DRIVE_IsValid(drive)) ret |= (1 << drive);
1112 return ret;
1116 /***********************************************************************
1117 * GetVolumeInformation32A (KERNEL32.309)
1119 BOOL WINAPI GetVolumeInformationA( LPCSTR root, LPSTR label,
1120 DWORD label_len, DWORD *serial,
1121 DWORD *filename_len, DWORD *flags,
1122 LPSTR fsname, DWORD fsname_len )
1124 int drive;
1125 char *cp;
1127 /* FIXME, SetLastErrors missing */
1129 if (!root) drive = DRIVE_GetCurrentDrive();
1130 else
1132 if ((root[1]) && (root[1] != ':'))
1134 WARN(dosfs, "invalid root '%s'\n",root);
1135 return FALSE;
1137 drive = toupper(root[0]) - 'A';
1139 if (!DRIVE_IsValid( drive )) return FALSE;
1140 if (label)
1142 lstrcpynA( label, DRIVE_GetLabel(drive), label_len );
1143 for (cp = label; *cp; cp++);
1144 while (cp != label && *(cp-1) == ' ') cp--;
1145 *cp = '\0';
1147 if (serial) *serial = DRIVE_GetSerialNumber(drive);
1149 /* Set the filesystem information */
1150 /* Note: we only emulate a FAT fs at the present */
1152 if (filename_len) {
1153 if (DOSDrives[drive].flags & DRIVE_SHORT_NAMES)
1154 *filename_len = 12;
1155 else
1156 *filename_len = 255;
1158 if (flags)
1160 *flags=0;
1161 if (DOSDrives[drive].flags & DRIVE_CASE_SENSITIVE)
1162 *flags|=FS_CASE_SENSITIVE;
1163 if (DOSDrives[drive].flags & DRIVE_CASE_PRESERVING)
1164 *flags|=FS_CASE_IS_PRESERVED ;
1166 if (fsname) {
1167 /* Diablo checks that return code ... */
1168 if (DRIVE_GetType(drive)==TYPE_CDROM)
1169 lstrcpynA( fsname, "CDFS", fsname_len );
1170 else
1171 lstrcpynA( fsname, "FAT", fsname_len );
1173 return TRUE;
1177 /***********************************************************************
1178 * GetVolumeInformation32W (KERNEL32.310)
1180 BOOL WINAPI GetVolumeInformationW( LPCWSTR root, LPWSTR label,
1181 DWORD label_len, DWORD *serial,
1182 DWORD *filename_len, DWORD *flags,
1183 LPWSTR fsname, DWORD fsname_len )
1185 LPSTR xroot = HEAP_strdupWtoA( GetProcessHeap(), 0, root );
1186 LPSTR xvolname = label ? HeapAlloc(GetProcessHeap(),0,label_len) : NULL;
1187 LPSTR xfsname = fsname ? HeapAlloc(GetProcessHeap(),0,fsname_len) : NULL;
1188 BOOL ret = GetVolumeInformationA( xroot, xvolname, label_len, serial,
1189 filename_len, flags, xfsname,
1190 fsname_len );
1191 if (ret)
1193 if (label) lstrcpyAtoW( label, xvolname );
1194 if (fsname) lstrcpyAtoW( fsname, xfsname );
1196 HeapFree( GetProcessHeap(), 0, xroot );
1197 HeapFree( GetProcessHeap(), 0, xvolname );
1198 HeapFree( GetProcessHeap(), 0, xfsname );
1199 return ret;
1202 BOOL WINAPI SetVolumeLabelA(LPCSTR rootpath,LPCSTR volname) {
1203 FIXME(dosfs,"(%s,%s),stub!\n",rootpath,volname);
1204 return TRUE;