2 * DOS drives handling functions
4 * Copyright 1993 Erik Bos
5 * Copyright 1996 Alexandre Julliard
7 * Label & serial number read support.
8 * (c) 1999 Petr Tomasek <tomasek@etf.cuni.cz>
9 * (c) 2000 Andreas Mohr (changes)
20 #include <sys/types.h>
26 #ifdef HAVE_SYS_PARAM_H
27 # include <sys/param.h>
29 #ifdef STATFS_DEFINED_BY_SYS_VFS
32 # ifdef STATFS_DEFINED_BY_SYS_MOUNT
33 # include <sys/mount.h>
35 # ifdef STATFS_DEFINED_BY_SYS_STATFS
36 # include <sys/statfs.h>
43 #include "wine/winbase16.h" /* for GetCurrentTask */
51 #include "wine/port.h"
53 #include "debugtools.h"
55 DEFAULT_DEBUG_CHANNEL(dosfs
);
56 DECLARE_DEBUG_CHANNEL(file
);
60 char *root
; /* root dir in Unix format without trailing / */
61 char *dos_cwd
; /* cwd in DOS format without leading or trailing \ */
62 char *unix_cwd
; /* cwd in Unix format without leading or trailing / */
63 char *device
; /* raw device path */
64 char label_conf
[12]; /* drive label as cfg'd in wine config */
65 char label_read
[12]; /* drive label as read from device */
66 DWORD serial_conf
; /* drive serial number as cfg'd in wine config */
67 UINT type
; /* drive type */
68 UINT flags
; /* drive flags */
69 dev_t dev
; /* unix device number */
70 ino_t ino
; /* unix inode number */
74 static const char * const DRIVE_Types
[] =
76 "", /* DRIVE_UNKNOWN */
77 "", /* DRIVE_NO_ROOT_DIR */
78 "floppy", /* DRIVE_REMOVABLE */
79 "hd", /* DRIVE_FIXED */
80 "network", /* DRIVE_REMOTE */
81 "cdrom", /* DRIVE_CDROM */
82 "ramdisk" /* DRIVE_RAMDISK */
86 /* Known filesystem types */
94 static const FS_DESCR DRIVE_Filesystems
[] =
96 { "unix", DRIVE_CASE_SENSITIVE
| DRIVE_CASE_PRESERVING
},
97 { "msdos", DRIVE_SHORT_NAMES
},
98 { "dos", DRIVE_SHORT_NAMES
},
99 { "fat", DRIVE_SHORT_NAMES
},
100 { "vfat", DRIVE_CASE_PRESERVING
},
101 { "win95", DRIVE_CASE_PRESERVING
},
106 static DOSDRIVE DOSDrives
[MAX_DOS_DRIVES
];
107 static int DRIVE_CurDrive
= -1;
109 static HTASK16 DRIVE_LastTask
= 0;
111 /* strdup on the process heap */
112 inline static char *heap_strdup( const char *str
)
114 INT len
= strlen(str
) + 1;
115 LPSTR p
= HeapAlloc( GetProcessHeap(), 0, len
);
116 if (p
) memcpy( p
, str
, len
);
120 /***********************************************************************
123 static UINT
DRIVE_GetDriveType( const char *name
)
128 PROFILE_GetWineIniString( name
, "Type", "hd", buffer
, sizeof(buffer
) );
129 for (i
= 0; i
< sizeof(DRIVE_Types
)/sizeof(DRIVE_Types
[0]); i
++)
131 if (!strcasecmp( buffer
, DRIVE_Types
[i
] )) return i
;
133 MESSAGE("%s: unknown drive type '%s', defaulting to 'hd'.\n",
139 /***********************************************************************
142 static UINT
DRIVE_GetFSFlags( const char *name
, const char *value
)
144 const FS_DESCR
*descr
;
146 for (descr
= DRIVE_Filesystems
; descr
->name
; descr
++)
147 if (!strcasecmp( value
, descr
->name
)) return descr
->flags
;
148 MESSAGE("%s: unknown filesystem type '%s', defaulting to 'win95'.\n",
150 return DRIVE_CASE_PRESERVING
;
154 /***********************************************************************
159 int i
, len
, count
= 0;
160 char name
[] = "Drive A";
161 char drive_env
[] = "=A:";
162 char path
[MAX_PATHNAME_LEN
];
164 struct stat drive_stat_buffer
;
168 for (i
= 0, drive
= DOSDrives
; i
< MAX_DOS_DRIVES
; i
++, name
[6]++, drive
++)
170 PROFILE_GetWineIniString( name
, "Path", "", path
, sizeof(path
)-1 );
173 p
= path
+ strlen(path
) - 1;
174 while ((p
> path
) && ((*p
== '/') || (*p
== '\\'))) *p
-- = '\0';
175 if (!path
[0]) strcpy( path
, "/" );
177 if (stat( path
, &drive_stat_buffer
))
179 MESSAGE("Could not stat %s (%s), ignoring drive %c:\n",
180 path
, strerror(errno
), 'A' + i
);
183 if (!S_ISDIR(drive_stat_buffer
.st_mode
))
185 MESSAGE("%s is not a directory, ignoring drive %c:\n",
190 drive
->root
= heap_strdup( path
);
191 drive
->dos_cwd
= heap_strdup( "" );
192 drive
->unix_cwd
= heap_strdup( "" );
193 drive
->type
= DRIVE_GetDriveType( name
);
194 drive
->device
= NULL
;
196 drive
->dev
= drive_stat_buffer
.st_dev
;
197 drive
->ino
= drive_stat_buffer
.st_ino
;
199 /* Get the drive label */
200 PROFILE_GetWineIniString( name
, "Label", "", drive
->label_conf
, 12 );
201 if ((len
= strlen(drive
->label_conf
)) < 11)
203 /* Pad label with spaces */
204 memset( drive
->label_conf
+ len
, ' ', 11 - len
);
205 drive
->label_conf
[11] = '\0';
208 /* Get the serial number */
209 PROFILE_GetWineIniString( name
, "Serial", "12345678",
210 buffer
, sizeof(buffer
) );
211 drive
->serial_conf
= strtoul( buffer
, NULL
, 16 );
213 /* Get the filesystem type */
214 PROFILE_GetWineIniString( name
, "Filesystem", "win95",
215 buffer
, sizeof(buffer
) );
216 drive
->flags
= DRIVE_GetFSFlags( name
, buffer
);
219 PROFILE_GetWineIniString( name
, "Device", "",
220 buffer
, sizeof(buffer
) );
223 drive
->device
= heap_strdup( buffer
);
224 if (PROFILE_GetWineIniBool( name
, "ReadVolInfo", 1))
225 drive
->flags
|= DRIVE_READ_VOL_INFO
;
228 /* Get the FailReadOnly flag */
229 if (PROFILE_GetWineIniBool( name
, "FailReadOnly", 0 ))
230 drive
->flags
|= DRIVE_FAIL_READ_ONLY
;
232 /* Make the first hard disk the current drive */
233 if ((DRIVE_CurDrive
== -1) && (drive
->type
== DRIVE_FIXED
))
237 TRACE("%s: path=%s type=%s label='%s' serial=%08lx "
238 "flags=%08x dev=%x ino=%x\n",
239 name
, path
, DRIVE_Types
[drive
->type
],
240 drive
->label_conf
, drive
->serial_conf
, drive
->flags
,
241 (int)drive
->dev
, (int)drive
->ino
);
243 else WARN("%s: not defined\n", name
);
248 MESSAGE("Warning: no valid DOS drive found, check your configuration file.\n" );
249 /* Create a C drive pointing to Unix root dir */
250 DOSDrives
[2].root
= heap_strdup( "/" );
251 DOSDrives
[2].dos_cwd
= heap_strdup( "" );
252 DOSDrives
[2].unix_cwd
= heap_strdup( "" );
253 strcpy( DOSDrives
[2].label_conf
, "Drive C " );
254 DOSDrives
[2].serial_conf
= 12345678;
255 DOSDrives
[2].type
= DRIVE_FIXED
;
256 DOSDrives
[2].device
= NULL
;
257 DOSDrives
[2].flags
= 0;
261 /* Make sure the current drive is valid */
262 if (DRIVE_CurDrive
== -1)
264 for (i
= 0, drive
= DOSDrives
; i
< MAX_DOS_DRIVES
; i
++, drive
++)
266 if (drive
->root
&& !(drive
->flags
& DRIVE_DISABLED
))
274 /* get current working directory info for all drives */
275 for (i
= 0; i
< MAX_DOS_DRIVES
; i
++, drive_env
[1]++)
277 if (!GetEnvironmentVariableA(drive_env
, path
, sizeof(path
))) continue;
279 if (toupper(path
[0]) != drive_env
[1] || path
[1] != ':') continue;
280 DRIVE_Chdir( i
, path
+ 2 );
286 /***********************************************************************
289 int DRIVE_IsValid( int drive
)
291 if ((drive
< 0) || (drive
>= MAX_DOS_DRIVES
)) return 0;
292 return (DOSDrives
[drive
].root
&&
293 !(DOSDrives
[drive
].flags
& DRIVE_DISABLED
));
297 /***********************************************************************
298 * DRIVE_GetCurrentDrive
300 int DRIVE_GetCurrentDrive(void)
302 TDB
*pTask
= TASK_GetCurrent();
303 if (pTask
&& (pTask
->curdrive
& 0x80)) return pTask
->curdrive
& ~0x80;
304 return DRIVE_CurDrive
;
308 /***********************************************************************
309 * DRIVE_SetCurrentDrive
311 int DRIVE_SetCurrentDrive( int drive
)
313 TDB
*pTask
= TASK_GetCurrent();
314 if (!DRIVE_IsValid( drive
))
316 SetLastError( ERROR_INVALID_DRIVE
);
319 TRACE("%c:\n", 'A' + drive
);
320 DRIVE_CurDrive
= drive
;
321 if (pTask
) pTask
->curdrive
= drive
| 0x80;
322 chdir(DRIVE_GetUnixCwd(drive
));
327 /***********************************************************************
328 * DRIVE_FindDriveRoot
330 * Find a drive for which the root matches the beginning of the given path.
331 * This can be used to translate a Unix path into a drive + DOS path.
332 * Return value is the drive, or -1 on error. On success, path is modified
333 * to point to the beginning of the DOS path.
335 int DRIVE_FindDriveRoot( const char **path
)
337 /* idea: check at all '/' positions.
338 * If the device and inode of that path is identical with the
339 * device and inode of the current drive then we found a solution.
340 * If there is another drive pointing to a deeper position in
341 * the file tree, we want to find that one, not the earlier solution.
343 int drive
, rootdrive
= -1;
344 char buffer
[MAX_PATHNAME_LEN
];
346 const char *p
= *path
;
349 strcpy( buffer
, "/" );
352 if (stat( buffer
, &st
) || !S_ISDIR( st
.st_mode
)) break;
356 for (drive
= 0; drive
< MAX_DOS_DRIVES
; drive
++)
358 if (!DOSDrives
[drive
].root
||
359 (DOSDrives
[drive
].flags
& DRIVE_DISABLED
)) continue;
361 if ((DOSDrives
[drive
].dev
== st
.st_dev
) &&
362 (DOSDrives
[drive
].ino
== st
.st_ino
))
370 /* Get the next path component */
373 while ((*p
== '/') || (*p
== '\\')) p
++;
375 while (!IS_END_OF_NAME(*p
)) *next
++ = *p
++;
381 TRACE("%s -> drive %c:, root='%s', name='%s'\n",
382 buffer
, 'A' + rootdrive
, DOSDrives
[rootdrive
].root
, *path
);
387 /***********************************************************************
390 const char * DRIVE_GetRoot( int drive
)
392 if (!DRIVE_IsValid( drive
)) return NULL
;
393 return DOSDrives
[drive
].root
;
397 /***********************************************************************
400 const char * DRIVE_GetDosCwd( int drive
)
402 TDB
*pTask
= TASK_GetCurrent();
403 if (!DRIVE_IsValid( drive
)) return NULL
;
405 /* Check if we need to change the directory to the new task. */
406 if (pTask
&& (pTask
->curdrive
& 0x80) && /* The task drive is valid */
407 ((pTask
->curdrive
& ~0x80) == drive
) && /* and it's the one we want */
408 (DRIVE_LastTask
!= GetCurrentTask())) /* and the task changed */
410 /* Perform the task-switch */
411 if (!DRIVE_Chdir( drive
, pTask
->curdir
)) DRIVE_Chdir( drive
, "\\" );
412 DRIVE_LastTask
= GetCurrentTask();
414 return DOSDrives
[drive
].dos_cwd
;
418 /***********************************************************************
421 const char * DRIVE_GetUnixCwd( int drive
)
423 TDB
*pTask
= TASK_GetCurrent();
424 if (!DRIVE_IsValid( drive
)) return NULL
;
426 /* Check if we need to change the directory to the new task. */
427 if (pTask
&& (pTask
->curdrive
& 0x80) && /* The task drive is valid */
428 ((pTask
->curdrive
& ~0x80) == drive
) && /* and it's the one we want */
429 (DRIVE_LastTask
!= GetCurrentTask())) /* and the task changed */
431 /* Perform the task-switch */
432 if (!DRIVE_Chdir( drive
, pTask
->curdir
)) DRIVE_Chdir( drive
, "\\" );
433 DRIVE_LastTask
= GetCurrentTask();
435 return DOSDrives
[drive
].unix_cwd
;
439 /***********************************************************************
442 const char * DRIVE_GetDevice( int drive
)
444 return (DRIVE_IsValid( drive
)) ? DOSDrives
[drive
].device
: NULL
;
448 /***********************************************************************
449 * DRIVE_ReadSuperblock
452 * DRIVE_SetLabel and DRIVE_SetSerialNumber use this in order
453 * to check, that they are writing on a FAT filesystem !
455 int DRIVE_ReadSuperblock (int drive
, char * buff
)
457 #define DRIVE_SUPER 96
461 if (memset(buff
,0,DRIVE_SUPER
)!=buff
) return -1;
462 if ((fd
=open(DOSDrives
[drive
].device
,O_RDONLY
)) == -1)
465 if (!DOSDrives
[drive
].device
)
466 ERR("No device configured for drive %c: !\n", 'A'+drive
);
468 ERR("Couldn't open device '%s' for drive %c: ! (%s)\n", DOSDrives
[drive
].device
, 'A'+drive
,
469 (stat(DOSDrives
[drive
].device
, &st
)) ?
470 "not available or symlink not valid ?" : "no permission");
471 ERR("Can't read drive volume info ! Either pre-set it or make sure the device to read it from is accessible !\n");
472 PROFILE_UsageWineIni();
476 switch(DOSDrives
[drive
].type
)
478 case DRIVE_REMOVABLE
:
483 offs
= CDROM_Data_FindBestVoldesc(fd
);
490 if ((offs
) && (lseek(fd
,offs
,SEEK_SET
)!=offs
)) return -4;
491 if (read(fd
,buff
,DRIVE_SUPER
)!=DRIVE_SUPER
) return -2;
493 switch(DOSDrives
[drive
].type
)
495 case DRIVE_REMOVABLE
:
497 if ((buff
[0x26]!=0x29) || /* Check for FAT present */
498 /* FIXME: do really all FAT have their name beginning with
499 "FAT" ? (At least FAT12, FAT16 and FAT32 have :) */
500 memcmp( buff
+0x36,"FAT",3))
502 ERR("The filesystem is not FAT !! (device=%s)\n",
503 DOSDrives
[drive
].device
);
508 if (strncmp(&buff
[1],"CD001",5)) /* Check for iso9660 present */
510 /* FIXME: do we need to check for "CDROM", too ? (high sierra) */
521 /***********************************************************************
522 * DRIVE_WriteSuperblockEntry
525 * We are writing as little as possible (ie. not the whole SuperBlock)
526 * not to interfere with kernel. The drive can be mounted !
528 int DRIVE_WriteSuperblockEntry (int drive
, off_t ofs
, size_t len
, char * buff
)
532 if ((fd
=open(DOSDrives
[drive
].device
,O_WRONLY
))==-1)
534 ERR("Cannot open the device %s (for writing)\n",
535 DOSDrives
[drive
].device
);
538 if (lseek(fd
,ofs
,SEEK_SET
)!=ofs
)
540 ERR("lseek failed on device %s !\n",
541 DOSDrives
[drive
].device
);
545 if (write(fd
,buff
,len
)!=len
)
548 ERR("Cannot write on %s !\n",
549 DOSDrives
[drive
].device
);
557 /***********************************************************************
560 const char * DRIVE_GetLabel( int drive
)
563 char buff
[DRIVE_SUPER
];
566 if (!DRIVE_IsValid( drive
)) return NULL
;
567 if (DOSDrives
[drive
].type
== DRIVE_CDROM
)
569 read
= CDROM_GetLabel(drive
, DOSDrives
[drive
].label_read
);
572 if (DOSDrives
[drive
].flags
& DRIVE_READ_VOL_INFO
)
574 if (DRIVE_ReadSuperblock(drive
,(char *) buff
))
575 ERR("Invalid or unreadable superblock on %s (%c:).\n",
576 DOSDrives
[drive
].device
, (char)(drive
+'A'));
578 if (DOSDrives
[drive
].type
== DRIVE_REMOVABLE
||
579 DOSDrives
[drive
].type
== DRIVE_FIXED
)
582 /* FIXME: ISO9660 uses a 32 bytes long label. Should we do also? */
583 if (offs
!= -1) memcpy(DOSDrives
[drive
].label_read
,buff
+offs
,11);
584 DOSDrives
[drive
].label_read
[11]='\0';
590 DOSDrives
[drive
].label_read
: DOSDrives
[drive
].label_conf
;
594 /***********************************************************************
595 * DRIVE_GetSerialNumber
597 DWORD
DRIVE_GetSerialNumber( int drive
)
600 char buff
[DRIVE_SUPER
];
602 if (!DRIVE_IsValid( drive
)) return 0;
604 if (DOSDrives
[drive
].flags
& DRIVE_READ_VOL_INFO
)
606 switch(DOSDrives
[drive
].type
)
608 case DRIVE_REMOVABLE
:
610 if (DRIVE_ReadSuperblock(drive
,(char *) buff
))
611 MESSAGE("Invalid or unreadable superblock on %s (%c:)."
612 " Maybe not FAT?\n" ,
613 DOSDrives
[drive
].device
, 'A'+drive
);
615 serial
= *((DWORD
*)(buff
+0x27));
618 serial
= CDROM_GetSerial(drive
);
621 FIXME("Serial number reading from file system on drive %c: not supported yet.\n", drive
+'A');
625 return (serial
) ? serial
: DOSDrives
[drive
].serial_conf
;
629 /***********************************************************************
630 * DRIVE_SetSerialNumber
632 int DRIVE_SetSerialNumber( int drive
, DWORD serial
)
634 char buff
[DRIVE_SUPER
];
636 if (!DRIVE_IsValid( drive
)) return 0;
638 if (DOSDrives
[drive
].flags
& DRIVE_READ_VOL_INFO
)
640 if ((DOSDrives
[drive
].type
!= DRIVE_REMOVABLE
) &&
641 (DOSDrives
[drive
].type
!= DRIVE_FIXED
)) return 0;
642 /* check, if the drive has a FAT filesystem */
643 if (DRIVE_ReadSuperblock(drive
, buff
)) return 0;
644 if (DRIVE_WriteSuperblockEntry(drive
, 0x27, 4, (char *) &serial
)) return 0;
648 if (DOSDrives
[drive
].type
== DRIVE_CDROM
) return 0;
649 DOSDrives
[drive
].serial_conf
= serial
;
654 /***********************************************************************
657 static UINT
DRIVE_GetType( int drive
)
659 if (!DRIVE_IsValid( drive
)) return DRIVE_UNKNOWN
;
660 return DOSDrives
[drive
].type
;
664 /***********************************************************************
667 UINT
DRIVE_GetFlags( int drive
)
669 if ((drive
< 0) || (drive
>= MAX_DOS_DRIVES
)) return 0;
670 return DOSDrives
[drive
].flags
;
674 /***********************************************************************
677 int DRIVE_Chdir( int drive
, const char *path
)
679 DOS_FULL_NAME full_name
;
680 char buffer
[MAX_PATHNAME_LEN
];
682 BY_HANDLE_FILE_INFORMATION info
;
683 TDB
*pTask
= TASK_GetCurrent();
685 strcpy( buffer
, "A:" );
687 TRACE("(%s,%s)\n", buffer
, path
);
688 lstrcpynA( buffer
+ 2, path
, sizeof(buffer
) - 2 );
690 if (!DOSFS_GetFullName( buffer
, TRUE
, &full_name
)) return 0;
691 if (!FILE_Stat( full_name
.long_name
, &info
)) return 0;
692 if (!(info
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
))
694 SetLastError( ERROR_FILE_NOT_FOUND
);
697 unix_cwd
= full_name
.long_name
+ strlen( DOSDrives
[drive
].root
);
698 while (*unix_cwd
== '/') unix_cwd
++;
700 TRACE("(%c:): unix_cwd=%s dos_cwd=%s\n",
701 'A' + drive
, unix_cwd
, full_name
.short_name
+ 3 );
703 HeapFree( GetProcessHeap(), 0, DOSDrives
[drive
].dos_cwd
);
704 HeapFree( GetProcessHeap(), 0, DOSDrives
[drive
].unix_cwd
);
705 DOSDrives
[drive
].dos_cwd
= heap_strdup( full_name
.short_name
+ 3 );
706 DOSDrives
[drive
].unix_cwd
= heap_strdup( unix_cwd
);
708 if (pTask
&& (pTask
->curdrive
& 0x80) &&
709 ((pTask
->curdrive
& ~0x80) == drive
))
711 lstrcpynA( pTask
->curdir
, full_name
.short_name
+ 2,
712 sizeof(pTask
->curdir
) );
713 DRIVE_LastTask
= GetCurrentTask();
714 chdir(unix_cwd
); /* Only change if on current drive */
720 /***********************************************************************
723 int DRIVE_Disable( int drive
)
725 if ((drive
< 0) || (drive
>= MAX_DOS_DRIVES
) || !DOSDrives
[drive
].root
)
727 SetLastError( ERROR_INVALID_DRIVE
);
730 DOSDrives
[drive
].flags
|= DRIVE_DISABLED
;
735 /***********************************************************************
738 int DRIVE_Enable( int drive
)
740 if ((drive
< 0) || (drive
>= MAX_DOS_DRIVES
) || !DOSDrives
[drive
].root
)
742 SetLastError( ERROR_INVALID_DRIVE
);
745 DOSDrives
[drive
].flags
&= ~DRIVE_DISABLED
;
750 /***********************************************************************
751 * DRIVE_SetLogicalMapping
753 int DRIVE_SetLogicalMapping ( int existing_drive
, int new_drive
)
755 /* If new_drive is already valid, do nothing and return 0
756 otherwise, copy DOSDrives[existing_drive] to DOSDrives[new_drive] */
760 old
= DOSDrives
+ existing_drive
;
761 new = DOSDrives
+ new_drive
;
763 if ((existing_drive
< 0) || (existing_drive
>= MAX_DOS_DRIVES
) ||
765 (new_drive
< 0) || (new_drive
>= MAX_DOS_DRIVES
))
767 SetLastError( ERROR_INVALID_DRIVE
);
773 TRACE("Can't map drive %c: to already existing drive %c:\n",
774 'A' + existing_drive
, 'A' + new_drive
);
775 /* it is already mapped there, so return success */
776 if (!strcmp(old
->root
,new->root
))
781 new->root
= heap_strdup( old
->root
);
782 new->dos_cwd
= heap_strdup( old
->dos_cwd
);
783 new->unix_cwd
= heap_strdup( old
->unix_cwd
);
784 new->device
= heap_strdup( old
->device
);
785 memcpy ( new->label_conf
, old
->label_conf
, 12 );
786 memcpy ( new->label_read
, old
->label_read
, 12 );
787 new->serial_conf
= old
->serial_conf
;
788 new->type
= old
->type
;
789 new->flags
= old
->flags
;
793 TRACE("Drive %c: is now equal to drive %c:\n",
794 'A' + new_drive
, 'A' + existing_drive
);
800 /***********************************************************************
803 * Open the drive raw device and return a Unix fd (or -1 on error).
805 int DRIVE_OpenDevice( int drive
, int flags
)
807 if (!DRIVE_IsValid( drive
)) return -1;
808 return open( DOSDrives
[drive
].device
, flags
);
812 /***********************************************************************
815 * Read raw sectors from a device
817 int DRIVE_RawRead(BYTE drive
, DWORD begin
, DWORD nr_sect
, BYTE
*dataptr
, BOOL fake_success
)
821 if ((fd
= DRIVE_OpenDevice( drive
, O_RDONLY
)) != -1)
823 lseek( fd
, begin
* 512, SEEK_SET
);
824 /* FIXME: check errors */
825 read( fd
, dataptr
, nr_sect
* 512 );
830 memset(dataptr
, 0, nr_sect
* 512);
833 if (begin
== 0 && nr_sect
> 1) *(dataptr
+ 512) = 0xf8;
834 if (begin
== 1) *dataptr
= 0xf8;
843 /***********************************************************************
846 * Write raw sectors to a device
848 int DRIVE_RawWrite(BYTE drive
, DWORD begin
, DWORD nr_sect
, BYTE
*dataptr
, BOOL fake_success
)
852 if ((fd
= DRIVE_OpenDevice( drive
, O_RDONLY
)) != -1)
854 lseek( fd
, begin
* 512, SEEK_SET
);
855 /* FIXME: check errors */
856 write( fd
, dataptr
, nr_sect
* 512 );
867 /***********************************************************************
870 static int DRIVE_GetFreeSpace( int drive
, PULARGE_INTEGER size
,
871 PULARGE_INTEGER available
)
875 if (!DRIVE_IsValid(drive
))
877 SetLastError( ERROR_INVALID_DRIVE
);
881 /* FIXME: add autoconf check for this */
882 #if defined(__svr4__) || defined(_SCO_DS) || defined(__sun)
883 if (statfs( DOSDrives
[drive
].root
, &info
, 0, 0) < 0)
885 if (statfs( DOSDrives
[drive
].root
, &info
) < 0)
889 WARN("cannot do statfs(%s)\n", DOSDrives
[drive
].root
);
893 size
->QuadPart
= RtlEnlargedUnsignedMultiply( info
.f_bsize
, info
.f_blocks
);
894 #ifdef STATFS_HAS_BAVAIL
895 available
->QuadPart
= RtlEnlargedUnsignedMultiply( info
.f_bavail
, info
.f_bsize
);
897 # ifdef STATFS_HAS_BFREE
898 available
->QuadPart
= RtlEnlargedUnsignedMultiply( info
.f_bfree
, info
.f_bsize
);
900 # error "statfs has no bfree/bavail member!"
903 if (DOSDrives
[drive
].type
== DRIVE_CDROM
)
904 { /* ALWAYS 0, even if no real CD-ROM mounted there !! */
905 available
->QuadPart
= 0;
910 /***********************************************************************
911 * DRIVE_GetCurrentDirectory
912 * Returns "X:\\path\\etc\\".
914 * Despite the API description, return required length including the
915 * terminating null when buffer too small. This is the real behaviour.
918 static UINT
DRIVE_GetCurrentDirectory( UINT buflen
, LPSTR buf
)
921 const char *s
= DRIVE_GetDosCwd( DRIVE_GetCurrentDrive() );
924 ret
= strlen(s
) + 3; /* length of WHOLE current directory */
925 if (ret
>= buflen
) return ret
+ 1;
926 lstrcpynA( buf
, "A:\\", min( 4, buflen
) );
927 if (buflen
) buf
[0] += DRIVE_GetCurrentDrive();
928 if (buflen
> 3) lstrcpynA( buf
+ 3, s
, buflen
- 3 );
933 /***********************************************************************
936 * Build the environment array containing the drives current directories.
937 * Resulting pointer must be freed with HeapFree.
939 char *DRIVE_BuildEnv(void)
942 const char *cwd
[MAX_DOS_DRIVES
];
945 for (i
= 0; i
< MAX_DOS_DRIVES
; i
++)
947 if ((cwd
[i
] = DRIVE_GetDosCwd(i
)) && cwd
[i
][0]) length
+= strlen(cwd
[i
]) + 8;
949 if (!(env
= HeapAlloc( GetProcessHeap(), 0, length
+1 ))) return NULL
;
950 for (i
= 0, p
= env
; i
< MAX_DOS_DRIVES
; i
++)
952 if (cwd
[i
] && cwd
[i
][0])
953 p
+= sprintf( p
, "=%c:=%c:\\%s", 'A'+i
, 'A'+i
, cwd
[i
] ) + 1;
960 /***********************************************************************
961 * GetDiskFreeSpace (KERNEL.422)
963 BOOL16 WINAPI
GetDiskFreeSpace16( LPCSTR root
, LPDWORD cluster_sectors
,
964 LPDWORD sector_bytes
, LPDWORD free_clusters
,
965 LPDWORD total_clusters
)
967 return GetDiskFreeSpaceA( root
, cluster_sectors
, sector_bytes
,
968 free_clusters
, total_clusters
);
972 /***********************************************************************
973 * GetDiskFreeSpaceA (KERNEL32.@)
975 * Fails if expression resulting from current drive's dir and "root"
976 * is not a root dir of the target drive.
978 * UNDOC: setting some LPDWORDs to NULL is perfectly possible
979 * if the corresponding info is unneeded.
981 * FIXME: needs to support UNC names from Win95 OSR2 on.
983 * Behaviour under Win95a:
984 * CurrDir root result
985 * "E:\\TEST" "E:" FALSE
989 * "E:\\TEST" "\\" TRUE
990 * "E:\\TEST" ":\\" FALSE
991 * "E:\\TEST" "E:\\" TRUE
992 * "E:\\TEST" "" FALSE
993 * "E:\\" "" FALSE (!)
995 * "E:\\TEST" 0x0 TRUE (!)
996 * "E:\\TEST" "C:" TRUE (when CurrDir of "C:" set to "\\")
997 * "E:\\TEST" "C:" FALSE (when CurrDir of "C:" set to "\\TEST")
999 BOOL WINAPI
GetDiskFreeSpaceA( LPCSTR root
, LPDWORD cluster_sectors
,
1000 LPDWORD sector_bytes
, LPDWORD free_clusters
,
1001 LPDWORD total_clusters
)
1003 int drive
, sec_size
;
1004 ULARGE_INTEGER size
,available
;
1008 if ((!root
) || (strcmp(root
,"\\") == 0))
1009 drive
= DRIVE_GetCurrentDrive();
1011 if ( (strlen(root
) >= 2) && (root
[1] == ':')) /* root contains drive tag */
1013 drive
= toupper(root
[0]) - 'A';
1015 if (path
[0] == '\0')
1016 path
= DRIVE_GetDosCwd(drive
);
1018 if (path
[0] == '\\')
1020 if (strlen(path
)) /* oops, we are in a subdir */
1026 if (!DRIVE_GetFreeSpace(drive
, &size
, &available
)) return FALSE
;
1028 /* Cap the size and available at 2GB as per specs. */
1029 if ((size
.s
.HighPart
) ||(size
.s
.LowPart
> 0x7fffffff))
1031 size
.s
.HighPart
= 0;
1032 size
.s
.LowPart
= 0x7fffffff;
1034 if ((available
.s
.HighPart
) ||(available
.s
.LowPart
> 0x7fffffff))
1036 available
.s
.HighPart
=0;
1037 available
.s
.LowPart
= 0x7fffffff;
1039 sec_size
= (DRIVE_GetType(drive
)==DRIVE_CDROM
) ? 2048 : 512;
1040 size
.s
.LowPart
/= sec_size
;
1041 available
.s
.LowPart
/= sec_size
;
1042 /* fixme: probably have to adjust those variables too for CDFS */
1044 while (cluster_sec
* 65536 < size
.s
.LowPart
) cluster_sec
*= 2;
1046 if (cluster_sectors
)
1047 *cluster_sectors
= cluster_sec
;
1049 *sector_bytes
= sec_size
;
1051 *free_clusters
= available
.s
.LowPart
/ cluster_sec
;
1053 *total_clusters
= size
.s
.LowPart
/ cluster_sec
;
1058 /***********************************************************************
1059 * GetDiskFreeSpaceW (KERNEL32.@)
1061 BOOL WINAPI
GetDiskFreeSpaceW( LPCWSTR root
, LPDWORD cluster_sectors
,
1062 LPDWORD sector_bytes
, LPDWORD free_clusters
,
1063 LPDWORD total_clusters
)
1068 xroot
= HEAP_strdupWtoA( GetProcessHeap(), 0, root
);
1069 ret
= GetDiskFreeSpaceA( xroot
,cluster_sectors
, sector_bytes
,
1070 free_clusters
, total_clusters
);
1071 HeapFree( GetProcessHeap(), 0, xroot
);
1076 /***********************************************************************
1077 * GetDiskFreeSpaceExA (KERNEL32.@)
1079 * This function is used to acquire the size of the available and
1080 * total space on a logical volume.
1084 * Zero on failure, nonzero upon success. Use GetLastError to obtain
1085 * detailed error information.
1088 BOOL WINAPI
GetDiskFreeSpaceExA( LPCSTR root
,
1089 PULARGE_INTEGER avail
,
1090 PULARGE_INTEGER total
,
1091 PULARGE_INTEGER totalfree
)
1094 ULARGE_INTEGER size
,available
;
1096 if (!root
) drive
= DRIVE_GetCurrentDrive();
1099 if ((root
[1]) && ((root
[1] != ':') || (root
[2] != '\\')))
1101 FIXME("there are valid root names which are not supported yet\n");
1102 /* ..like UNC names, for instance. */
1104 WARN("invalid root '%s'\n", root
);
1107 drive
= toupper(root
[0]) - 'A';
1110 if (!DRIVE_GetFreeSpace(drive
, &size
, &available
)) return FALSE
;
1114 total
->s
.HighPart
= size
.s
.HighPart
;
1115 total
->s
.LowPart
= size
.s
.LowPart
;
1120 totalfree
->s
.HighPart
= available
.s
.HighPart
;
1121 totalfree
->s
.LowPart
= available
.s
.LowPart
;
1126 if (FIXME_ON(dosfs
))
1128 /* On Windows2000, we need to check the disk quota
1129 allocated for the user owning the calling process. We
1130 don't want to be more obtrusive than necessary with the
1131 FIXME messages, so don't print the FIXME unless Wine is
1132 actually masquerading as Windows2000. */
1135 ovi
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFOA
);
1136 if (GetVersionExA(&ovi
))
1138 if (ovi
.dwPlatformId
== VER_PLATFORM_WIN32_NT
&& ovi
.dwMajorVersion
> 4)
1139 FIXME("no per-user quota support yet\n");
1143 /* Quick hack, should eventually be fixed to work 100% with
1144 Windows2000 (see comment above). */
1145 avail
->s
.HighPart
= available
.s
.HighPart
;
1146 avail
->s
.LowPart
= available
.s
.LowPart
;
1152 /***********************************************************************
1153 * GetDiskFreeSpaceExW (KERNEL32.@)
1155 BOOL WINAPI
GetDiskFreeSpaceExW( LPCWSTR root
, PULARGE_INTEGER avail
,
1156 PULARGE_INTEGER total
,
1157 PULARGE_INTEGER totalfree
)
1162 xroot
= HEAP_strdupWtoA( GetProcessHeap(), 0, root
);
1163 ret
= GetDiskFreeSpaceExA( xroot
, avail
, total
, totalfree
);
1164 HeapFree( GetProcessHeap(), 0, xroot
);
1168 /***********************************************************************
1169 * GetDriveType (KERNEL.136)
1170 * This function returns the type of a drive in Win16.
1171 * Note that it returns DRIVE_REMOTE for CD-ROMs, since MSCDEX uses the
1172 * remote drive API. The return value DRIVE_REMOTE for CD-ROMs has been
1173 * verified on Win 3.11 and Windows 95. Some programs rely on it, so don't
1174 * do any pseudo-clever changes.
1177 * drivetype DRIVE_xxx
1179 UINT16 WINAPI
GetDriveType16( UINT16 drive
) /* [in] number (NOT letter) of drive */
1181 UINT type
= DRIVE_GetType(drive
);
1182 TRACE("(%c:)\n", 'A' + drive
);
1183 if (type
== DRIVE_CDROM
) type
= DRIVE_REMOTE
;
1188 /***********************************************************************
1189 * GetDriveTypeA (KERNEL32.@)
1191 * Returns the type of the disk drive specified. If root is NULL the
1192 * root of the current directory is used.
1196 * Type of drive (from Win32 SDK):
1198 * DRIVE_UNKNOWN unable to find out anything about the drive
1199 * DRIVE_NO_ROOT_DIR nonexistent root dir
1200 * DRIVE_REMOVABLE the disk can be removed from the machine
1201 * DRIVE_FIXED the disk can not be removed from the machine
1202 * DRIVE_REMOTE network disk
1203 * DRIVE_CDROM CDROM drive
1204 * DRIVE_RAMDISK virtual disk in RAM
1206 UINT WINAPI
GetDriveTypeA(LPCSTR root
) /* [in] String describing drive */
1209 TRACE("(%s)\n", debugstr_a(root
));
1211 if (NULL
== root
) drive
= DRIVE_GetCurrentDrive();
1214 if ((root
[1]) && (root
[1] != ':'))
1216 WARN("invalid root %s\n", debugstr_a(root
));
1217 return DRIVE_NO_ROOT_DIR
;
1219 drive
= toupper(root
[0]) - 'A';
1221 return DRIVE_GetType(drive
);
1225 /***********************************************************************
1226 * GetDriveTypeW (KERNEL32.@)
1228 UINT WINAPI
GetDriveTypeW( LPCWSTR root
)
1230 LPSTR xpath
= HEAP_strdupWtoA( GetProcessHeap(), 0, root
);
1231 UINT ret
= GetDriveTypeA( xpath
);
1232 HeapFree( GetProcessHeap(), 0, xpath
);
1237 /***********************************************************************
1238 * GetCurrentDirectory (KERNEL.411)
1240 UINT16 WINAPI
GetCurrentDirectory16( UINT16 buflen
, LPSTR buf
)
1242 return (UINT16
)DRIVE_GetCurrentDirectory(buflen
, buf
);
1246 /***********************************************************************
1247 * GetCurrentDirectoryA (KERNEL32.@)
1249 UINT WINAPI
GetCurrentDirectoryA( UINT buflen
, LPSTR buf
)
1252 char longname
[MAX_PATHNAME_LEN
];
1253 char shortname
[MAX_PATHNAME_LEN
];
1254 ret
= DRIVE_GetCurrentDirectory(MAX_PATHNAME_LEN
, shortname
);
1255 if ( ret
> MAX_PATHNAME_LEN
) {
1256 ERR_(file
)("pathnamelength (%d) > MAX_PATHNAME_LEN!\n", ret
);
1259 GetLongPathNameA(shortname
, longname
, MAX_PATHNAME_LEN
);
1260 ret
= strlen( longname
) + 1;
1261 if (ret
> buflen
) return ret
;
1262 strcpy(buf
, longname
);
1266 /***********************************************************************
1267 * GetCurrentDirectoryW (KERNEL32.@)
1269 UINT WINAPI
GetCurrentDirectoryW( UINT buflen
, LPWSTR buf
)
1271 LPSTR xpath
= HeapAlloc( GetProcessHeap(), 0, buflen
+1 );
1272 UINT ret
= GetCurrentDirectoryA( buflen
, xpath
);
1273 if (ret
< buflen
) ret
= MultiByteToWideChar( CP_ACP
, 0, xpath
, -1, buf
, buflen
) - 1;
1274 HeapFree( GetProcessHeap(), 0, xpath
);
1279 /***********************************************************************
1280 * SetCurrentDirectory (KERNEL.412)
1282 BOOL16 WINAPI
SetCurrentDirectory16( LPCSTR dir
)
1284 return SetCurrentDirectoryA( dir
);
1288 /***********************************************************************
1289 * SetCurrentDirectoryA (KERNEL32.@)
1291 BOOL WINAPI
SetCurrentDirectoryA( LPCSTR dir
)
1293 int drive
, olddrive
= DRIVE_GetCurrentDrive();
1296 ERR_(file
)("(NULL)!\n");
1299 if (dir
[0] && (dir
[1]==':'))
1301 drive
= toupper( *dir
) - 'A';
1307 /* WARNING: we need to set the drive before the dir, as DRIVE_Chdir
1308 sets pTask->curdir only if pTask->curdrive is drive */
1309 if (!(DRIVE_SetCurrentDrive( drive
)))
1311 /* FIXME: what about empty strings? Add a \\ ? */
1312 if (!DRIVE_Chdir( drive
, dir
)) {
1313 DRIVE_SetCurrentDrive(olddrive
);
1320 /***********************************************************************
1321 * SetCurrentDirectoryW (KERNEL32.@)
1323 BOOL WINAPI
SetCurrentDirectoryW( LPCWSTR dirW
)
1325 LPSTR dir
= HEAP_strdupWtoA( GetProcessHeap(), 0, dirW
);
1326 BOOL res
= SetCurrentDirectoryA( dir
);
1327 HeapFree( GetProcessHeap(), 0, dir
);
1332 /***********************************************************************
1333 * GetLogicalDriveStringsA (KERNEL32.@)
1335 UINT WINAPI
GetLogicalDriveStringsA( UINT len
, LPSTR buffer
)
1339 for (drive
= count
= 0; drive
< MAX_DOS_DRIVES
; drive
++)
1340 if (DRIVE_IsValid(drive
)) count
++;
1341 if ((count
* 4) + 1 <= len
)
1344 for (drive
= 0; drive
< MAX_DOS_DRIVES
; drive
++)
1345 if (DRIVE_IsValid(drive
))
1356 return (count
* 4) + 1; /* account for terminating null */
1357 /* The API tells about these different return values */
1361 /***********************************************************************
1362 * GetLogicalDriveStringsW (KERNEL32.@)
1364 UINT WINAPI
GetLogicalDriveStringsW( UINT len
, LPWSTR buffer
)
1368 for (drive
= count
= 0; drive
< MAX_DOS_DRIVES
; drive
++)
1369 if (DRIVE_IsValid(drive
)) count
++;
1370 if (count
* 4 * sizeof(WCHAR
) <= len
)
1373 for (drive
= 0; drive
< MAX_DOS_DRIVES
; drive
++)
1374 if (DRIVE_IsValid(drive
))
1376 *p
++ = (WCHAR
)('a' + drive
);
1383 return count
* 4 * sizeof(WCHAR
);
1387 /***********************************************************************
1388 * GetLogicalDrives (KERNEL32.@)
1390 DWORD WINAPI
GetLogicalDrives(void)
1395 for (drive
= 0; drive
< MAX_DOS_DRIVES
; drive
++)
1397 if ( (DRIVE_IsValid(drive
)) ||
1398 (DOSDrives
[drive
].type
== DRIVE_CDROM
)) /* audio CD is also valid */
1399 ret
|= (1 << drive
);
1405 /***********************************************************************
1406 * GetVolumeInformationA (KERNEL32.@)
1408 BOOL WINAPI
GetVolumeInformationA( LPCSTR root
, LPSTR label
,
1409 DWORD label_len
, DWORD
*serial
,
1410 DWORD
*filename_len
, DWORD
*flags
,
1411 LPSTR fsname
, DWORD fsname_len
)
1416 /* FIXME, SetLastError()s missing */
1418 if (!root
) drive
= DRIVE_GetCurrentDrive();
1421 if ((root
[1]) && (root
[1] != ':'))
1423 WARN("invalid root '%s'\n",root
);
1426 drive
= toupper(root
[0]) - 'A';
1428 if (!DRIVE_IsValid( drive
)) return FALSE
;
1431 lstrcpynA( label
, DRIVE_GetLabel(drive
), label_len
);
1432 cp
= label
+ strlen(label
);
1433 while (cp
!= label
&& *(cp
-1) == ' ') cp
--;
1436 if (serial
) *serial
= DRIVE_GetSerialNumber(drive
);
1438 /* Set the filesystem information */
1439 /* Note: we only emulate a FAT fs at present */
1442 if (DOSDrives
[drive
].flags
& DRIVE_SHORT_NAMES
)
1445 *filename_len
= 255;
1450 if (DOSDrives
[drive
].flags
& DRIVE_CASE_SENSITIVE
)
1451 *flags
|=FS_CASE_SENSITIVE
;
1452 if (DOSDrives
[drive
].flags
& DRIVE_CASE_PRESERVING
)
1453 *flags
|=FS_CASE_IS_PRESERVED
;
1456 /* Diablo checks that return code ... */
1457 if (DOSDrives
[drive
].type
== DRIVE_CDROM
)
1458 lstrcpynA( fsname
, "CDFS", fsname_len
);
1460 lstrcpynA( fsname
, "FAT", fsname_len
);
1466 /***********************************************************************
1467 * GetVolumeInformationW (KERNEL32.@)
1469 BOOL WINAPI
GetVolumeInformationW( LPCWSTR root
, LPWSTR label
,
1470 DWORD label_len
, DWORD
*serial
,
1471 DWORD
*filename_len
, DWORD
*flags
,
1472 LPWSTR fsname
, DWORD fsname_len
)
1474 LPSTR xroot
= HEAP_strdupWtoA( GetProcessHeap(), 0, root
);
1475 LPSTR xvolname
= label
? HeapAlloc(GetProcessHeap(),0,label_len
) : NULL
;
1476 LPSTR xfsname
= fsname
? HeapAlloc(GetProcessHeap(),0,fsname_len
) : NULL
;
1477 BOOL ret
= GetVolumeInformationA( xroot
, xvolname
, label_len
, serial
,
1478 filename_len
, flags
, xfsname
,
1482 if (label
) MultiByteToWideChar( CP_ACP
, 0, xvolname
, -1, label
, label_len
);
1483 if (fsname
) MultiByteToWideChar( CP_ACP
, 0, xfsname
, -1, fsname
, fsname_len
);
1485 HeapFree( GetProcessHeap(), 0, xroot
);
1486 HeapFree( GetProcessHeap(), 0, xvolname
);
1487 HeapFree( GetProcessHeap(), 0, xfsname
);
1491 /***********************************************************************
1492 * SetVolumeLabelA (KERNEL32.@)
1494 BOOL WINAPI
SetVolumeLabelA( LPCSTR root
, LPCSTR volname
)
1498 /* FIXME, SetLastErrors missing */
1500 if (!root
) drive
= DRIVE_GetCurrentDrive();
1503 if ((root
[1]) && (root
[1] != ':'))
1505 WARN("invalid root '%s'\n",root
);
1508 drive
= toupper(root
[0]) - 'A';
1510 if (!DRIVE_IsValid( drive
)) return FALSE
;
1512 /* some copy protection stuff check this */
1513 if (DOSDrives
[drive
].type
== DRIVE_CDROM
) return FALSE
;
1515 FIXME("(%s,%s),stub!\n", root
, volname
);
1519 /***********************************************************************
1520 * SetVolumeLabelW (KERNEL32.@)
1522 BOOL WINAPI
SetVolumeLabelW(LPCWSTR rootpath
,LPCWSTR volname
)
1527 xroot
= HEAP_strdupWtoA( GetProcessHeap(), 0, rootpath
);
1528 xvol
= HEAP_strdupWtoA( GetProcessHeap(), 0, volname
);
1529 ret
= SetVolumeLabelA( xroot
, xvol
);
1530 HeapFree( GetProcessHeap(), 0, xroot
);
1531 HeapFree( GetProcessHeap(), 0, xvol
);