2 * DOS drives handling functions
4 * Copyright 1993 Erik Bos
5 * Copyright 1996 Alexandre Julliard
11 #include <sys/types.h>
14 #if defined(__linux__) || defined(sun)
17 #if defined(__NetBSD__) || defined(__FreeBSD__)
18 #include <sys/param.h>
19 #include <sys/mount.h>
20 #include <sys/errno.h>
22 #if defined(__svr4__) || defined(_SCO_DS)
23 #include <sys/statfs.h>
41 char *root
; /* root dir in Unix format without trailing / */
42 char *dos_cwd
; /* cwd in DOS format without leading or trailing \ */
43 char *unix_cwd
; /* cwd in Unix format without leading or trailing / */
44 char label
[12]; /* drive label */
45 DWORD serial
; /* drive serial number */
46 DRIVETYPE type
; /* drive type */
47 BYTE disabled
; /* disabled flag */
51 static const char *DRIVE_Types
[] =
53 "floppy", /* TYPE_FLOPPY */
55 "cdrom", /* TYPE_CDROM */
56 "network" /* TYPE_NETWORK */
60 static DOSDRIVE DOSDrives
[MAX_DOS_DRIVES
];
61 static int DRIVE_CurDrive
= -1;
63 static HTASK DRIVE_LastTask
= 0;
66 /***********************************************************************
69 static DRIVETYPE
DRIVE_GetDriveType( const char *name
)
74 PROFILE_GetWineIniString( name
, "Type", "hd", buffer
, sizeof(buffer
) );
75 for (i
= 0; i
< sizeof(DRIVE_Types
)/sizeof(DRIVE_Types
[0]); i
++)
77 if (!lstrcmpi32A( buffer
, DRIVE_Types
[i
] )) return (DRIVETYPE
)i
;
79 fprintf( stderr
, "%s: unknown type '%s', defaulting to 'hd'.\n",
85 /***********************************************************************
90 int i
, len
, count
= 0;
91 char name
[] = "Drive A";
92 char path
[MAX_PATHNAME_LEN
];
97 for (i
= 0, drive
= DOSDrives
; i
< MAX_DOS_DRIVES
; i
++, name
[6]++, drive
++)
99 PROFILE_GetWineIniString( name
, "Path", "", path
, sizeof(path
)-1 );
102 p
= path
+ strlen(path
) - 1;
103 while ((p
> path
) && ((*p
== '/') || (*p
== '\\'))) *p
-- = '\0';
104 drive
->root
= xstrdup( path
);
105 drive
->dos_cwd
= xstrdup( "" );
106 drive
->unix_cwd
= xstrdup( "" );
107 drive
->type
= DRIVE_GetDriveType( name
);
110 /* Get the drive label */
111 PROFILE_GetWineIniString( name
, "Label", name
, drive
->label
, 12 );
112 if ((len
= strlen(drive
->label
)) < 11)
114 /* Pad label with spaces */
115 memset( drive
->label
+ len
, ' ', 11 - len
);
116 drive
->label
[12] = '\0';
119 /* Get the serial number */
120 PROFILE_GetWineIniString( name
, "Serial", "12345678",
121 buffer
, sizeof(buffer
) );
122 drive
->serial
= strtoul( buffer
, NULL
, 16 );
124 /* Make the first hard disk the current drive */
125 if ((DRIVE_CurDrive
== -1) && (drive
->type
== TYPE_HD
))
129 dprintf_dosfs( stddeb
, "%s: path=%s type=%s label='%s' serial=%08lx\n",
130 name
, path
, DRIVE_Types
[drive
->type
],
131 drive
->label
, drive
->serial
);
133 else dprintf_dosfs( stddeb
, "%s: not defined\n", name
);
138 fprintf( stderr
, "Warning: no valid DOS drive found, check your configuration file.\n" );
139 /* Create a C drive pointing to Unix root dir */
140 DOSDrives
[2].root
= xstrdup( "/" );
141 DOSDrives
[2].dos_cwd
= xstrdup( "" );
142 DOSDrives
[2].unix_cwd
= xstrdup( "" );
143 strcpy( DOSDrives
[2].label
, "Drive C " );
144 DOSDrives
[2].serial
= 0x12345678;
145 DOSDrives
[2].type
= TYPE_HD
;
146 DOSDrives
[2].disabled
= 0;
150 /* Make sure the current drive is valid */
151 if (DRIVE_CurDrive
== -1)
153 for (i
= 0, drive
= DOSDrives
; i
< MAX_DOS_DRIVES
; i
++, drive
++)
155 if (drive
->root
&& !drive
->disabled
)
167 /***********************************************************************
170 int DRIVE_IsValid( int drive
)
172 if ((drive
< 0) || (drive
>= MAX_DOS_DRIVES
)) return 0;
173 return (DOSDrives
[drive
].root
&& !DOSDrives
[drive
].disabled
);
177 /***********************************************************************
178 * DRIVE_GetCurrentDrive
180 int DRIVE_GetCurrentDrive(void)
182 TDB
*pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
183 if (pTask
&& (pTask
->curdrive
& 0x80)) return pTask
->curdrive
& ~0x80;
184 return DRIVE_CurDrive
;
188 /***********************************************************************
189 * DRIVE_SetCurrentDrive
191 int DRIVE_SetCurrentDrive( int drive
)
193 TDB
*pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
194 if (!DRIVE_IsValid( drive
))
196 DOS_ERROR( ER_InvalidDrive
, EC_MediaError
, SA_Abort
, EL_Disk
);
199 dprintf_dosfs( stddeb
, "DRIVE_SetCurrentDrive: %c:\n", 'A' + drive
);
200 DRIVE_CurDrive
= drive
;
201 if (pTask
) pTask
->curdrive
= drive
| 0x80;
206 /***********************************************************************
207 * DRIVE_FindDriveRoot
209 * Find a drive for which the root matches the begginning of the given path.
210 * This can be used to translate a Unix path into a drive + DOS path.
211 * Return value is the drive, or -1 on error. On success, path is modified
212 * to point to the beginning of the DOS path.
213 * FIXME: this only does a textual comparison of the path names, and won't
214 * work well in the presence of symbolic links.
216 int DRIVE_FindDriveRoot( const char **path
)
218 int drive
, rootdrive
= -1;
221 dprintf_dosfs( stddeb
, "DRIVE_FindDriveRoot: searching '%s'\n", *path
);
222 for (drive
= 0; drive
< MAX_DOS_DRIVES
; drive
++)
224 if (!DOSDrives
[drive
].root
|| DOSDrives
[drive
].disabled
) continue;
226 p2
= DOSDrives
[drive
].root
;
227 dprintf_dosfs( stddeb
, "DRIVE_FindDriveRoot: checking %c: '%s'\n",
230 while (*p2
== '/') p2
++;
234 continue; /* Look if there's a better match */
238 while ((*p1
== '\\') || (*p1
== '/')) p1
++;
239 while (*p2
== '/') p2
++;
240 while ((*p1
== *p2
) && (*p2
) && (*p2
!= '/')) p1
++, p2
++;
243 if (IS_END_OF_NAME(*p1
)) /* OK, found it */
251 if (IS_END_OF_NAME(*p1
))
252 continue; /* Go to next path element */
254 break; /* No match, go to next drive */
261 /***********************************************************************
264 const char * DRIVE_GetRoot( int drive
)
266 if (!DRIVE_IsValid( drive
)) return NULL
;
267 return DOSDrives
[drive
].root
;
271 /***********************************************************************
274 const char * DRIVE_GetDosCwd( int drive
)
276 TDB
*pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
277 if (!DRIVE_IsValid( drive
)) return NULL
;
279 /* Check if we need to change the directory to the new task. */
280 if (pTask
&& (pTask
->curdrive
& 0x80) && /* The task drive is valid */
281 ((pTask
->curdrive
& ~0x80) == drive
) && /* and it's the one we want */
282 (DRIVE_LastTask
!= GetCurrentTask())) /* and the task changed */
284 /* Perform the task-switch */
285 if (!DRIVE_Chdir( drive
, pTask
->curdir
)) DRIVE_Chdir( drive
, "\\" );
286 DRIVE_LastTask
= GetCurrentTask();
288 return DOSDrives
[drive
].dos_cwd
;
292 /***********************************************************************
295 const char * DRIVE_GetUnixCwd( int drive
)
297 TDB
*pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
298 if (!DRIVE_IsValid( drive
)) return NULL
;
300 /* Check if we need to change the directory to the new task. */
301 if (pTask
&& (pTask
->curdrive
& 0x80) && /* The task drive is valid */
302 ((pTask
->curdrive
& ~0x80) == drive
) && /* and it's the one we want */
303 (DRIVE_LastTask
!= GetCurrentTask())) /* and the task changed */
305 /* Perform the task-switch */
306 if (!DRIVE_Chdir( drive
, pTask
->curdir
)) DRIVE_Chdir( drive
, "\\" );
307 DRIVE_LastTask
= GetCurrentTask();
309 return DOSDrives
[drive
].unix_cwd
;
313 /***********************************************************************
316 const char * DRIVE_GetLabel( int drive
)
318 if (!DRIVE_IsValid( drive
)) return NULL
;
319 return DOSDrives
[drive
].label
;
323 /***********************************************************************
324 * DRIVE_GetSerialNumber
326 DWORD
DRIVE_GetSerialNumber( int drive
)
328 if (!DRIVE_IsValid( drive
)) return 0;
329 return DOSDrives
[drive
].serial
;
333 /***********************************************************************
334 * DRIVE_SetSerialNumber
336 int DRIVE_SetSerialNumber( int drive
, DWORD serial
)
338 if (!DRIVE_IsValid( drive
)) return 0;
339 DOSDrives
[drive
].serial
= serial
;
344 /***********************************************************************
347 DRIVETYPE
DRIVE_GetType( int drive
)
349 if (!DRIVE_IsValid( drive
)) return TYPE_INVALID
;
350 return DOSDrives
[drive
].type
;
354 /***********************************************************************
357 int DRIVE_Chdir( int drive
, const char *path
)
359 char buffer
[MAX_PATHNAME_LEN
];
360 const char *unix_cwd
, *dos_cwd
;
362 TDB
*pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
364 dprintf_dosfs( stddeb
, "DRIVE_Chdir(%c:,%s)\n", 'A' + drive
, path
);
365 strcpy( buffer
, "A:" );
367 lstrcpyn32A( buffer
+ 2, path
, sizeof(buffer
) - 2 );
369 if (!(unix_cwd
= DOSFS_GetUnixFileName( buffer
, TRUE
))) return 0;
370 if (!FILE_Stat( unix_cwd
, &attr
, NULL
, NULL
, NULL
)) return 0;
371 if (!(attr
& FA_DIRECTORY
))
373 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
376 unix_cwd
+= strlen( DOSDrives
[drive
].root
);
377 while (*unix_cwd
== '/') unix_cwd
++;
379 lstrcpyn32A( buffer
+ 3, unix_cwd
, sizeof(buffer
) - 3 );
380 if (!(dos_cwd
= DOSFS_GetDosTrueName( buffer
, TRUE
))) return 0;
382 dprintf_dosfs( stddeb
, "DRIVE_Chdir(%c:): unix_cwd=%s dos_cwd=%s\n",
383 'A' + drive
, unix_cwd
, dos_cwd
+ 3 );
385 free( DOSDrives
[drive
].dos_cwd
);
386 free( DOSDrives
[drive
].unix_cwd
);
387 DOSDrives
[drive
].dos_cwd
= xstrdup( dos_cwd
+ 3 );
388 DOSDrives
[drive
].unix_cwd
= xstrdup( unix_cwd
);
390 if (pTask
&& (pTask
->curdrive
& 0x80) &&
391 ((pTask
->curdrive
& ~0x80) == drive
))
393 lstrcpyn32A( pTask
->curdir
, dos_cwd
+ 2, sizeof(pTask
->curdir
) );
394 DRIVE_LastTask
= GetCurrentTask();
400 /***********************************************************************
403 int DRIVE_Disable( int drive
)
405 if ((drive
< 0) || (drive
>= MAX_DOS_DRIVES
) || !DOSDrives
[drive
].root
)
407 DOS_ERROR( ER_InvalidDrive
, EC_MediaError
, SA_Abort
, EL_Disk
);
410 DOSDrives
[drive
].disabled
= 1;
415 /***********************************************************************
418 int DRIVE_Enable( int drive
)
420 if ((drive
< 0) || (drive
>= MAX_DOS_DRIVES
) || !DOSDrives
[drive
].root
)
422 DOS_ERROR( ER_InvalidDrive
, EC_MediaError
, SA_Abort
, EL_Disk
);
425 DOSDrives
[drive
].disabled
= 0;
430 /***********************************************************************
433 static int DRIVE_GetFreeSpace( int drive
, DWORD
*size
, DWORD
*available
)
437 if (!DRIVE_IsValid(drive
))
439 DOS_ERROR( ER_InvalidDrive
, EC_MediaError
, SA_Abort
, EL_Disk
);
443 #if defined(__svr4__) || defined(_SCO_DS)
444 if (statfs( DOSDrives
[drive
].root
, &info
, 0, 0) < 0)
446 if (statfs( DOSDrives
[drive
].root
, &info
) < 0)
450 fprintf(stderr
,"dosfs: cannot do statfs(%s)\n", DOSDrives
[drive
].root
);
454 *size
= info
.f_bsize
* info
.f_blocks
;
455 #if defined(__svr4__) || defined(_SCO_DS)
456 *available
= info
.f_bfree
* info
.f_bsize
;
458 *available
= info
.f_bavail
* info
.f_bsize
;
464 /***********************************************************************
465 * GetDiskFreeSpace16 (KERNEL.422)
467 BOOL16
GetDiskFreeSpace16( LPCSTR root
, LPDWORD cluster_sectors
,
468 LPDWORD sector_bytes
, LPDWORD free_clusters
,
469 LPDWORD total_clusters
)
471 return GetDiskFreeSpace32A( root
, cluster_sectors
, sector_bytes
,
472 free_clusters
, total_clusters
);
476 /***********************************************************************
477 * GetDiskFreeSpaceA (KERNEL32.206)
479 BOOL32
GetDiskFreeSpace32A( LPCSTR root
, LPDWORD cluster_sectors
,
480 LPDWORD sector_bytes
, LPDWORD free_clusters
,
481 LPDWORD total_clusters
)
484 DWORD size
,available
;
486 if (!root
) drive
= DRIVE_GetCurrentDrive();
489 if ((root
[1] != ':') || (root
[2] != '\\'))
491 fprintf( stderr
, "GetDiskFreeSpaceA: invalid root '%s'\n", root
);
494 drive
= toupper(root
[0]) - 'A';
496 if (!DRIVE_GetFreeSpace(drive
, &size
, &available
)) return FALSE
;
501 *cluster_sectors
= 1;
502 while (*cluster_sectors
* 65530 < size
) *cluster_sectors
*= 2;
503 *free_clusters
= available
/ *cluster_sectors
;
504 *total_clusters
= size
/ *cluster_sectors
;
509 /***********************************************************************
510 * GetDiskFreeSpaceW (KERNEL32.207)
512 BOOL32
GetDiskFreeSpace32W( LPCWSTR root
, LPDWORD cluster_sectors
,
513 LPDWORD sector_bytes
, LPDWORD free_clusters
,
514 LPDWORD total_clusters
)
519 xroot
= STRING32_DupUniToAnsi(root
);
520 ret
= GetDiskFreeSpace32A( xroot
,cluster_sectors
, sector_bytes
,
521 free_clusters
, total_clusters
);
527 /***********************************************************************
528 * GetDriveType (KERNEL.136)
530 WORD
GetDriveType( INT drive
)
532 dprintf_dosfs( stddeb
, "GetDriveType(%c:)\n", 'A' + drive
);
533 switch(DRIVE_GetType(drive
))
535 case TYPE_FLOPPY
: return DRIVE_REMOVABLE
;
536 case TYPE_HD
: return DRIVE_FIXED
;
537 case TYPE_CDROM
: return DRIVE_REMOVABLE
;
538 case TYPE_NETWORK
: return DRIVE_REMOTE
;
540 default: return DRIVE_CANNOTDETERMINE
;
545 /***********************************************************************
546 * GetDriveType32A (KERNEL32.)
548 WORD
GetDriveType32A( LPCSTR root
)
550 dprintf_dosfs( stddeb
, "GetDriveType32A(%s)\n", root
);
551 if ((root
[1] != ':') || (root
[2] != '\\'))
553 fprintf( stderr
, "GetDriveType32A: invalid root '%s'\n", root
);
554 return DRIVE_DOESNOTEXIST
;
556 switch(DRIVE_GetType(toupper(root
[0]) - 'A'))
558 case TYPE_FLOPPY
: return DRIVE_REMOVABLE
;
559 case TYPE_HD
: return DRIVE_FIXED
;
560 case TYPE_CDROM
: return DRIVE_REMOVABLE
;
561 case TYPE_NETWORK
: return DRIVE_REMOTE
;
563 default: return DRIVE_CANNOTDETERMINE
;
568 /***********************************************************************
569 * GetCurrentDirectory (KERNEL.411) (KERNEL32.196)
571 UINT32
GetCurrentDirectory( UINT32 buflen
, LPSTR buf
)
573 const char *s
= DRIVE_GetDosCwd( DRIVE_GetCurrentDrive() );
579 lstrcpyn32A( buf
, s
, buflen
);
580 return strlen(s
); /* yes */
584 /***********************************************************************
585 * SetCurrentDirectory (KERNEL.412)
587 BOOL32
SetCurrentDirectory( LPCSTR dir
)
589 return DRIVE_Chdir( DRIVE_GetCurrentDrive(), dir
);
593 /***********************************************************************
594 * GetLogicalDriveStrings32A (KERNEL32.231)
596 UINT32
GetLogicalDriveStrings32A( UINT32 len
, LPSTR buffer
)
600 for (drive
= count
= 0; drive
< MAX_DOS_DRIVES
; drive
++)
601 if (DRIVE_IsValid(drive
)) count
++;
602 if (count
* 4 * sizeof(char) <= len
)
605 for (drive
= 0; drive
< MAX_DOS_DRIVES
; drive
++)
606 if (DRIVE_IsValid(drive
))
615 return count
* 4 * sizeof(char);
619 /***********************************************************************
620 * GetLogicalDriveStrings32W (KERNEL32.232)
622 UINT32
GetLogicalDriveStrings32W( UINT32 len
, LPWSTR buffer
)
626 for (drive
= count
= 0; drive
< MAX_DOS_DRIVES
; drive
++)
627 if (DRIVE_IsValid(drive
)) count
++;
628 if (count
* 4 * sizeof(WCHAR
) <= len
)
631 for (drive
= 0; drive
< MAX_DOS_DRIVES
; drive
++)
632 if (DRIVE_IsValid(drive
))
634 *p
++ = (WCHAR
)('a' + drive
);
641 return count
* 4 * sizeof(WCHAR
);
645 /***********************************************************************
646 * GetLogicalDrives (KERNEL32.233)
648 DWORD
GetLogicalDrives(void)
653 for (drive
= 0; drive
< MAX_DOS_DRIVES
; drive
++)
654 if (DRIVE_IsValid(drive
)) ret
|= (1 << drive
);
659 /***********************************************************************
660 * GetVolumeInformation32A (KERNEL32.309)
662 BOOL32
GetVolumeInformation32A( LPCSTR root
, LPSTR label
, DWORD label_len
,
663 DWORD
*serial
, DWORD
*filename_len
,
664 DWORD
*flags
, LPSTR fsname
, DWORD fsname_len
)
668 /* FIXME, SetLastErrors missing */
670 if (!root
) drive
= DRIVE_GetCurrentDrive();
673 if ((root
[1] != ':') || (root
[2] != '\\'))
675 fprintf( stderr
, "GetVolumeInformation: invalid root '%s'\n",root
);
678 drive
= toupper(root
[0]) - 'A';
680 if (!DRIVE_IsValid( drive
)) return FALSE
;
681 if (label
) lstrcpyn32A( label
, DOSDrives
[drive
].label
, label_len
);
682 if (serial
) *serial
= DOSDrives
[drive
].serial
;
684 /* Set the filesystem information */
685 /* Note: we only emulate a FAT fs at the present */
687 if (filename_len
) *filename_len
= 12;
688 if (flags
) *flags
= 0;
689 if (fsname
) lstrcpyn32A( fsname
, "FAT", fsname_len
);
694 /***********************************************************************
695 * GetVolumeInformation32W (KERNEL32.310)
697 BOOL32
GetVolumeInformation32W( LPCWSTR root
, LPWSTR label
, DWORD label_len
,
698 DWORD
*serial
, DWORD
*filename_len
,
699 DWORD
*flags
, LPWSTR fsname
, DWORD fsname_len
)
701 LPSTR xroot
= STRING32_DupUniToAnsi(root
);
702 LPSTR xvolname
= (char*)xmalloc( label_len
);
703 LPSTR xfsname
= (char*)xmalloc( fsname_len
);
704 BOOL32 ret
= GetVolumeInformation32A( xroot
, xvolname
, label_len
, serial
,
705 filename_len
, flags
, xfsname
,
709 STRING32_AnsiToUni( label
, xvolname
);
710 STRING32_AnsiToUni( fsname
, xfsname
);