2 * NTDLL directory functions
4 * Copyright 1993 Erik Bos
5 * Copyright 2003 Eric Pouech
6 * Copyright 1996, 2004 Alexandre Julliard
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "wine/port.h"
26 #include <sys/types.h>
38 #ifdef HAVE_SYS_IOCTL_H
39 #include <sys/ioctl.h>
41 #ifdef HAVE_LINUX_IOCTL_H
42 #include <linux/ioctl.h>
44 #ifdef HAVE_SYS_PARAM_H
45 #include <sys/param.h>
47 #ifdef HAVE_SYS_MOUNT_H
48 #include <sys/mount.h>
55 #define NONAMELESSUNION
56 #define NONAMELESSSTRUCT
63 #include "ntdll_misc.h"
64 #include "wine/unicode.h"
65 #include "wine/server.h"
66 #include "wine/library.h"
67 #include "wine/debug.h"
69 WINE_DEFAULT_DEBUG_CHANNEL(file
);
71 /* Define the VFAT ioctl to get both short and long file names */
72 /* FIXME: is it possible to get this to work on other systems? */
74 /* We want the real kernel dirent structure, not the libc one */
79 unsigned short d_reclen
;
83 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, KERNEL_DIRENT [2] )
86 # define O_DIRECTORY 0200000 /* must be directory */
89 /* Using the same seekdir value across multiple directories is not portable, */
90 /* but it works on Linux, and it's a major performance gain so we want to use */
92 /* FIXME: do some sort of runtime check instead */
96 #undef VFAT_IOCTL_READDIR_BOTH /* just in case... */
100 #define IS_OPTION_TRUE(ch) ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
101 #define IS_SEPARATOR(ch) ((ch) == '\\' || (ch) == '/')
103 #define INVALID_NT_CHARS '*','?','<','>','|','"'
104 #define INVALID_DOS_CHARS INVALID_NT_CHARS,'+','=',',',';','[',']',' ','\345'
106 #define MAX_DIR_ENTRY_LEN 255 /* max length of a directory entry in chars */
108 static int show_dir_symlinks
= -1;
109 static int show_dot_files
;
111 /* at some point we may want to allow Winelib apps to set this */
112 static const int is_case_sensitive
= FALSE
;
114 static CRITICAL_SECTION dir_section
;
115 static CRITICAL_SECTION_DEBUG critsect_debug
=
118 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
119 0, 0, { 0, (DWORD
)(__FILE__
": dir_section") }
121 static CRITICAL_SECTION dir_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
124 /***********************************************************************
127 * Wrapper for supporting seekdir across multiple directory objects.
129 static inline void seekdir_wrapper( DIR *dir
, off_t pos
)
134 while (pos
-- > 0) if (!readdir( dir
)) break;
138 /***********************************************************************
141 * Wrapper for supporting telldir across multiple directory objects.
143 static inline off_t
telldir_wrapper( DIR *dir
, off_t pos
, int count
)
146 return telldir( dir
);
153 /***********************************************************************
154 * get_default_com_device
156 * Return the default device to use for serial ports.
158 static char *get_default_com_device( int num
)
162 if (!num
|| num
> 9) return ret
;
164 ret
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/ttyS0") );
167 strcpy( ret
, "/dev/ttyS0" );
168 ret
[strlen(ret
) - 1] = '0' + num
- 1;
171 FIXME( "no known default for device com%d\n", num
);
177 /***********************************************************************
178 * get_default_lpt_device
180 * Return the default device to use for parallel ports.
182 static char *get_default_lpt_device( int num
)
186 if (!num
|| num
> 9) return ret
;
188 ret
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/lp0") );
191 strcpy( ret
, "/dev/lp0" );
192 ret
[strlen(ret
) - 1] = '0' + num
- 1;
195 FIXME( "no known default for device lpt%d\n", num
);
201 /***********************************************************************
202 * parse_mount_entries
204 * Parse mount entries looking for a given device. Helper for get_default_drive_device.
207 static char *parse_mount_entries( FILE *f
, dev_t dev
, ino_t ino
)
209 struct mntent
*entry
;
213 while ((entry
= getmntent( f
)))
215 if (stat( entry
->mnt_dir
, &st
) == -1) continue;
216 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
217 if (!strcmp( entry
->mnt_type
, "supermount" ))
219 if ((device
= strstr( entry
->mnt_opts
, "dev=" )))
221 char *p
= strchr( device
+ 4, ',' );
227 return entry
->mnt_fsname
;
233 /***********************************************************************
234 * get_default_drive_device
236 * Return the default device to use for a given drive mount point.
238 static char *get_default_drive_device( const char *root
)
248 /* try to open it first to force it to get mounted */
249 if ((fd
= open( root
, O_RDONLY
| O_DIRECTORY
)) != -1)
251 res
= fstat( fd
, &st
);
254 /* now try normal stat just in case */
255 if (res
== -1) res
= stat( root
, &st
);
256 if (res
== -1) return NULL
;
258 RtlEnterCriticalSection( &dir_section
);
260 if ((f
= fopen( "/etc/mtab", "r" )))
262 device
= parse_mount_entries( f
, st
.st_dev
, st
.st_ino
);
265 /* look through fstab too in case it's not mounted (for instance if it's an audio CD) */
266 if (!device
&& (f
= fopen( "/etc/fstab", "r" )))
268 device
= parse_mount_entries( f
, st
.st_dev
, st
.st_ino
);
273 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(device
) + 1 );
274 if (ret
) strcpy( ret
, device
);
276 RtlLeaveCriticalSection( &dir_section
);
277 #elif defined(__APPLE__)
278 struct statfs
*mntStat
;
284 static const char path_bsd_device
[] = "/dev/disk";
287 res
= stat( root
, &st
);
288 if (res
== -1) return NULL
;
293 RtlEnterCriticalSection( &dir_section
);
295 mntSize
= getmntinfo(&mntStat
, MNT_NOWAIT
);
297 for (i
= 0; i
< mntSize
&& !ret
; i
++)
299 if (stat(mntStat
[i
].f_mntonname
, &st
) == -1) continue;
300 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
302 /* FIXME add support for mounted network drive */
303 if ( strncmp(mntStat
[i
].f_mntfromname
, path_bsd_device
, strlen(path_bsd_device
)) == 0)
305 /* set return value to the corresponding raw BSD node */
306 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(mntStat
[i
].f_mntfromname
) + 2 /* 2 : r and \0 */ );
309 strcpy(ret
, "/dev/r");
310 strcat(ret
, mntStat
[i
].f_mntfromname
+sizeof("/dev/")-1);
314 RtlLeaveCriticalSection( &dir_section
);
317 if (!warned
++) FIXME( "auto detection of DOS devices not supported on this platform\n" );
323 /***********************************************************************
326 * Initialize the show_dir_symlinks and show_dot_files options.
328 static void init_options(void)
330 static const WCHAR WineW
[] = {'M','a','c','h','i','n','e','\\',
331 'S','o','f','t','w','a','r','e','\\',
332 'W','i','n','e','\\','W','i','n','e','\\',
333 'C','o','n','f','i','g','\\','W','i','n','e',0};
334 static const WCHAR ShowDotFilesW
[] = {'S','h','o','w','D','o','t','F','i','l','e','s',0};
335 static const WCHAR ShowDirSymlinksW
[] = {'S','h','o','w','D','i','r','S','y','m','l','i','n','k','s',0};
339 OBJECT_ATTRIBUTES attr
;
340 UNICODE_STRING nameW
;
342 show_dot_files
= show_dir_symlinks
= 0;
344 attr
.Length
= sizeof(attr
);
345 attr
.RootDirectory
= 0;
346 attr
.ObjectName
= &nameW
;
348 attr
.SecurityDescriptor
= NULL
;
349 attr
.SecurityQualityOfService
= NULL
;
350 RtlInitUnicodeString( &nameW
, WineW
);
352 if (!NtOpenKey( &hkey
, KEY_ALL_ACCESS
, &attr
))
354 RtlInitUnicodeString( &nameW
, ShowDotFilesW
);
355 if (!NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
, tmp
, sizeof(tmp
), &dummy
))
357 WCHAR
*str
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)tmp
)->Data
;
358 show_dot_files
= IS_OPTION_TRUE( str
[0] );
360 RtlInitUnicodeString( &nameW
, ShowDirSymlinksW
);
361 if (!NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
, tmp
, sizeof(tmp
), &dummy
))
363 WCHAR
*str
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)tmp
)->Data
;
364 show_dir_symlinks
= IS_OPTION_TRUE( str
[0] );
371 /***********************************************************************
374 * Check if the specified file should be hidden based on its name and the show dot files option.
376 BOOL
DIR_is_hidden_file( const UNICODE_STRING
*name
)
380 if (show_dir_symlinks
== -1) init_options();
381 if (show_dot_files
) return FALSE
;
383 end
= p
= name
->Buffer
+ name
->Length
/sizeof(WCHAR
);
384 while (p
> name
->Buffer
&& IS_SEPARATOR(p
[-1])) p
--;
385 while (p
> name
->Buffer
&& !IS_SEPARATOR(p
[-1])) p
--;
386 if (p
== end
|| *p
!= '.') return FALSE
;
387 /* make sure it isn't '.' or '..' */
388 if (p
+ 1 == end
) return FALSE
;
389 if (p
[1] == '.' && p
+ 2 == end
) return FALSE
;
394 /***********************************************************************
395 * hash_short_file_name
397 * Transform a Unix file name into a hashed DOS name. If the name is a valid
398 * DOS name, it is converted to upper-case; otherwise it is replaced by a
399 * hashed version that fits in 8.3 format.
400 * 'buffer' must be at least 12 characters long.
401 * Returns length of short name in bytes; short name is NOT null-terminated.
403 static ULONG
hash_short_file_name( const UNICODE_STRING
*name
, LPWSTR buffer
)
405 static const WCHAR invalid_chars
[] = { INVALID_DOS_CHARS
,'~','.',0 };
406 static const char hash_chars
[32] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345";
408 LPCWSTR p
, ext
, end
= name
->Buffer
+ name
->Length
/ sizeof(WCHAR
);
413 /* Compute the hash code of the file name */
414 /* If you know something about hash functions, feel free to */
415 /* insert a better algorithm here... */
416 if (!is_case_sensitive
)
418 for (p
= name
->Buffer
, hash
= 0xbeef; p
< end
- 1; p
++)
419 hash
= (hash
<<3) ^ (hash
>>5) ^ tolowerW(*p
) ^ (tolowerW(p
[1]) << 8);
420 hash
= (hash
<<3) ^ (hash
>>5) ^ tolowerW(*p
); /* Last character */
424 for (p
= name
->Buffer
, hash
= 0xbeef; p
< end
- 1; p
++)
425 hash
= (hash
<< 3) ^ (hash
>> 5) ^ *p
^ (p
[1] << 8);
426 hash
= (hash
<< 3) ^ (hash
>> 5) ^ *p
; /* Last character */
429 /* Find last dot for start of the extension */
430 for (p
= name
->Buffer
+ 1, ext
= NULL
; p
< end
- 1; p
++) if (*p
== '.') ext
= p
;
432 /* Copy first 4 chars, replacing invalid chars with '_' */
433 for (i
= 4, p
= name
->Buffer
, dst
= buffer
; i
> 0; i
--, p
++)
435 if (p
== end
|| p
== ext
) break;
436 *dst
++ = strchrW( invalid_chars
, *p
) ? '_' : toupperW(*p
);
438 /* Pad to 5 chars with '~' */
439 while (i
-- >= 0) *dst
++ = '~';
441 /* Insert hash code converted to 3 ASCII chars */
442 *dst
++ = hash_chars
[(hash
>> 10) & 0x1f];
443 *dst
++ = hash_chars
[(hash
>> 5) & 0x1f];
444 *dst
++ = hash_chars
[hash
& 0x1f];
446 /* Copy the first 3 chars of the extension (if any) */
450 for (i
= 3, ext
++; (i
> 0) && ext
< end
; i
--, ext
++)
451 *dst
++ = strchrW( invalid_chars
, *ext
) ? '_' : toupperW(*ext
);
457 /***********************************************************************
460 * Check a long file name against a mask.
462 * Tests (done in W95 DOS shell - case insensitive):
463 * *.txt test1.test.txt *
465 * *.t??????.t* test1.ta.tornado.txt *
466 * *tornado* test1.ta.tornado.txt *
467 * t*t test1.ta.tornado.txt *
469 * ?est??? test1.txt -
470 * *test1.txt* test1.txt *
471 * h?l?o*t.dat hellothisisatest.dat *
473 static BOOLEAN
match_filename( const UNICODE_STRING
*name_str
, const UNICODE_STRING
*mask_str
)
476 const WCHAR
*name
= name_str
->Buffer
;
477 const WCHAR
*mask
= mask_str
->Buffer
;
478 const WCHAR
*name_end
= name
+ name_str
->Length
/ sizeof(WCHAR
);
479 const WCHAR
*mask_end
= mask
+ mask_str
->Length
/ sizeof(WCHAR
);
480 const WCHAR
*lastjoker
= NULL
;
481 const WCHAR
*next_to_retry
= NULL
;
483 TRACE("(%s, %s)\n", debugstr_us(name_str
), debugstr_us(mask_str
));
485 while (name
< name_end
&& mask
< mask_end
)
491 while (mask
< mask_end
&& *mask
== '*') mask
++; /* Skip consecutive '*' */
492 if (mask
== mask_end
) return TRUE
; /* end of mask is all '*', so match */
495 /* skip to the next match after the joker(s) */
496 if (is_case_sensitive
)
497 while (name
< name_end
&& (*name
!= *mask
)) name
++;
499 while (name
< name_end
&& (toupperW(*name
) != toupperW(*mask
))) name
++;
500 next_to_retry
= name
;
507 if (is_case_sensitive
) mismatch
= (*mask
!= *name
);
508 else mismatch
= (toupperW(*mask
) != toupperW(*name
));
514 if (mask
== mask_end
)
516 if (name
== name_end
) return TRUE
;
517 if (lastjoker
) mask
= lastjoker
;
520 else /* mismatch ! */
522 if (lastjoker
) /* we had an '*', so we can try unlimitedly */
526 /* this scan sequence was a mismatch, so restart
527 * 1 char after the first char we checked last time */
529 name
= next_to_retry
;
531 else return FALSE
; /* bad luck */
536 while (mask
< mask_end
&& ((*mask
== '.') || (*mask
== '*')))
537 mask
++; /* Ignore trailing '.' or '*' in mask */
538 return (name
== name_end
&& mask
== mask_end
);
542 /***********************************************************************
545 * helper for NtQueryDirectoryFile
547 static FILE_BOTH_DIR_INFORMATION
*append_entry( void *info_ptr
, ULONG
*pos
, ULONG max_length
,
548 const char *long_name
, const char *short_name
,
549 const UNICODE_STRING
*mask
)
551 FILE_BOTH_DIR_INFORMATION
*info
;
552 int i
, long_len
, short_len
, total_len
;
554 WCHAR long_nameW
[MAX_DIR_ENTRY_LEN
];
555 WCHAR short_nameW
[12];
558 long_len
= ntdll_umbstowcs( 0, long_name
, strlen(long_name
), long_nameW
, MAX_DIR_ENTRY_LEN
);
559 if (long_len
== -1) return NULL
;
561 str
.Buffer
= long_nameW
;
562 str
.Length
= long_len
* sizeof(WCHAR
);
563 str
.MaximumLength
= sizeof(long_nameW
);
567 short_len
= ntdll_umbstowcs( 0, short_name
, strlen(short_name
),
568 short_nameW
, sizeof(short_nameW
) / sizeof(WCHAR
) );
569 if (short_len
== -1) short_len
= sizeof(short_nameW
) / sizeof(WCHAR
);
571 else /* generate a short name if necessary */
576 if (!RtlIsNameLegalDOS8Dot3( &str
, NULL
, &spaces
) || spaces
)
577 short_len
= hash_short_file_name( &str
, short_nameW
);
580 TRACE( "long %s short %s mask %s\n",
581 debugstr_us(&str
), debugstr_wn(short_nameW
, short_len
), debugstr_us(mask
) );
583 if (mask
&& !match_filename( &str
, mask
))
585 if (!short_len
) return NULL
; /* no short name to match */
586 str
.Buffer
= short_nameW
;
587 str
.Length
= short_len
* sizeof(WCHAR
);
588 str
.MaximumLength
= sizeof(short_nameW
);
589 if (!match_filename( &str
, mask
)) return NULL
;
592 total_len
= (sizeof(*info
) - sizeof(info
->FileName
) + long_len
*sizeof(WCHAR
) + 3) & ~3;
593 info
= (FILE_BOTH_DIR_INFORMATION
*)((char *)info_ptr
+ *pos
);
595 if (*pos
+ total_len
> max_length
) total_len
= max_length
- *pos
;
597 if (lstat( long_name
, &st
) == -1) return NULL
;
598 if (S_ISLNK( st
.st_mode
))
600 if (stat( long_name
, &st
) == -1) return NULL
;
601 if (S_ISDIR( st
.st_mode
) && !show_dir_symlinks
) return NULL
;
604 info
->NextEntryOffset
= total_len
;
605 info
->FileIndex
= 0; /* NTFS always has 0 here, so let's not bother with it */
607 RtlSecondsSince1970ToTime( st
.st_mtime
, &info
->CreationTime
);
608 RtlSecondsSince1970ToTime( st
.st_mtime
, &info
->LastWriteTime
);
609 RtlSecondsSince1970ToTime( st
.st_atime
, &info
->LastAccessTime
);
610 RtlSecondsSince1970ToTime( st
.st_ctime
, &info
->ChangeTime
);
612 if (S_ISDIR(st
.st_mode
))
614 info
->EndOfFile
.QuadPart
= info
->AllocationSize
.QuadPart
= 0;
615 info
->FileAttributes
= FILE_ATTRIBUTE_DIRECTORY
;
619 info
->EndOfFile
.QuadPart
= st
.st_size
;
620 info
->AllocationSize
.QuadPart
= (ULONGLONG
)st
.st_blocks
* 512;
621 info
->FileAttributes
= FILE_ATTRIBUTE_ARCHIVE
;
624 if (!(st
.st_mode
& S_IWUSR
))
625 info
->FileAttributes
|= FILE_ATTRIBUTE_READONLY
;
627 if (!show_dot_files
&& long_name
[0] == '.' && long_name
[1] && (long_name
[1] != '.' || long_name
[2]))
628 info
->FileAttributes
|= FILE_ATTRIBUTE_HIDDEN
;
630 info
->EaSize
= 0; /* FIXME */
631 info
->ShortNameLength
= short_len
* sizeof(WCHAR
);
632 for (i
= 0; i
< short_len
; i
++) info
->ShortName
[i
] = toupperW(short_nameW
[i
]);
633 info
->FileNameLength
= long_len
* sizeof(WCHAR
);
634 memcpy( info
->FileName
, long_nameW
,
635 min( info
->FileNameLength
, total_len
-sizeof(*info
)+sizeof(info
->FileName
) ));
642 /******************************************************************************
643 * NtQueryDirectoryFile [NTDLL.@]
644 * ZwQueryDirectoryFile [NTDLL.@]
646 NTSTATUS WINAPI
NtQueryDirectoryFile( HANDLE handle
, HANDLE event
,
647 PIO_APC_ROUTINE apc_routine
, PVOID apc_context
,
649 PVOID buffer
, ULONG length
,
650 FILE_INFORMATION_CLASS info_class
,
651 BOOLEAN single_entry
,
652 PUNICODE_STRING mask
,
653 BOOLEAN restart_scan
)
656 FILE_BOTH_DIR_INFORMATION
*info
, *last_info
= NULL
;
657 static const unsigned int max_dir_info_size
= sizeof(*info
) + (MAX_DIR_ENTRY_LEN
-1) * sizeof(WCHAR
);
659 TRACE("(%p %p %p %p %p %p 0x%08lx 0x%08x 0x%08x %s 0x%08x\n",
660 handle
, event
, apc_routine
, apc_context
, io
, buffer
,
661 length
, info_class
, single_entry
, debugstr_us(mask
),
664 if (length
< sizeof(*info
)) return STATUS_INFO_LENGTH_MISMATCH
;
666 if (event
|| apc_routine
)
668 FIXME( "Unsupported yet option\n" );
669 return io
->u
.Status
= STATUS_NOT_IMPLEMENTED
;
671 if (info_class
!= FileBothDirectoryInformation
)
673 FIXME( "Unsupported file info class %d\n", info_class
);
674 return io
->u
.Status
= STATUS_NOT_IMPLEMENTED
;
677 if ((io
->u
.Status
= wine_server_handle_to_fd( handle
, GENERIC_READ
, &fd
, NULL
)) != STATUS_SUCCESS
)
682 RtlEnterCriticalSection( &dir_section
);
684 if (show_dir_symlinks
== -1) init_options();
686 if ((cwd
= open(".", O_RDONLY
)) != -1 && fchdir( fd
) != -1)
690 #ifdef VFAT_IOCTL_READDIR_BOTH
693 io
->u
.Status
= STATUS_SUCCESS
;
695 /* Check if the VFAT ioctl is supported on this directory */
697 if (restart_scan
) lseek( fd
, 0, SEEK_SET
);
698 else old_pos
= lseek( fd
, 0, SEEK_CUR
);
700 if (ioctl( fd
, VFAT_IOCTL_READDIR_BOTH
, (long)de
) != -1)
702 if (length
< max_dir_info_size
) /* we may have to return a partial entry here */
706 if (!de
[0].d_reclen
) break;
708 info
= append_entry( buffer
, &io
->Information
, length
,
709 de
[1].d_name
, de
[0].d_name
, mask
);
711 info
= append_entry( buffer
, &io
->Information
, length
,
712 de
[0].d_name
, NULL
, mask
);
716 if ((char *)info
->FileName
+ info
->FileNameLength
> (char *)buffer
+ length
)
718 io
->u
.Status
= STATUS_BUFFER_OVERFLOW
;
719 lseek( fd
, old_pos
, SEEK_SET
); /* restore pos to previous entry */
723 old_pos
= lseek( fd
, 0, SEEK_CUR
);
724 if (ioctl( fd
, VFAT_IOCTL_READDIR_BOTH
, (long)de
) == -1) break;
727 else /* we'll only return full entries, no need to worry about overflow */
731 if (!de
[0].d_reclen
) break;
733 info
= append_entry( buffer
, &io
->Information
, length
,
734 de
[1].d_name
, de
[0].d_name
, mask
);
736 info
= append_entry( buffer
, &io
->Information
, length
,
737 de
[0].d_name
, NULL
, mask
);
741 if (single_entry
) break;
742 /* check if we still have enough space for the largest possible entry */
743 if (io
->Information
+ max_dir_info_size
> length
) break;
745 if (ioctl( fd
, VFAT_IOCTL_READDIR_BOTH
, (long)de
) == -1) break;
749 else if (errno
!= ENOENT
)
750 #endif /* VFAT_IOCTL_READDIR_BOTH */
755 if (!(dir
= opendir( "." )))
757 io
->u
.Status
= FILE_GetNtStatus();
762 old_pos
= lseek( fd
, 0, SEEK_CUR
);
763 seekdir_wrapper( dir
, old_pos
);
765 io
->u
.Status
= STATUS_SUCCESS
;
767 if (length
< max_dir_info_size
) /* we may have to return a partial entry here */
769 while ((de
= readdir( dir
)))
771 info
= append_entry( buffer
, &io
->Information
, length
,
772 de
->d_name
, NULL
, mask
);
776 if ((char *)info
->FileName
+ info
->FileNameLength
> (char *)buffer
+ length
)
777 io
->u
.Status
= STATUS_BUFFER_OVERFLOW
;
779 old_pos
= telldir_wrapper( dir
, old_pos
, 1 );
782 old_pos
= telldir_wrapper( dir
, old_pos
, 1 );
785 else /* we'll only return full entries, no need to worry about overflow */
788 while ((de
= readdir( dir
)))
791 info
= append_entry( buffer
, &io
->Information
, length
,
792 de
->d_name
, NULL
, mask
);
796 if (single_entry
) break;
797 /* check if we still have enough space for the largest possible entry */
798 if (io
->Information
+ max_dir_info_size
> length
) break;
801 old_pos
= telldir_wrapper( dir
, old_pos
, count
);
803 lseek( fd
, old_pos
, SEEK_SET
); /* store dir offset as filepos for fd */
807 if (last_info
) last_info
->NextEntryOffset
= 0;
808 else io
->u
.Status
= restart_scan
? STATUS_NO_SUCH_FILE
: STATUS_NO_MORE_FILES
;
811 if (fchdir( cwd
) == -1) chdir( "/" );
813 else io
->u
.Status
= FILE_GetNtStatus();
815 RtlLeaveCriticalSection( &dir_section
);
817 wine_server_release_fd( handle
, fd
);
818 if (cwd
!= -1) close( cwd
);
819 TRACE( "=> %lx (%ld)\n", io
->u
.Status
, io
->Information
);
824 /***********************************************************************
827 * Find a file in a directory the hard way, by doing a case-insensitive search.
828 * The file found is appended to unix_name at pos.
829 * There must be at least MAX_DIR_ENTRY_LEN+2 chars available at pos.
831 static NTSTATUS
find_file_in_dir( char *unix_name
, int pos
, const WCHAR
*name
, int length
,
834 WCHAR buffer
[MAX_DIR_ENTRY_LEN
];
840 int ret
, used_default
, is_name_8_dot_3
;
842 /* try a shortcut for this directory */
844 unix_name
[pos
++] = '/';
845 ret
= ntdll_wcstoumbs( 0, name
, length
, unix_name
+ pos
, MAX_DIR_ENTRY_LEN
,
846 NULL
, &used_default
);
847 /* if we used the default char, the Unix name won't round trip properly back to Unicode */
848 /* so it cannot match the file we are looking for */
849 if (ret
>= 0 && !used_default
)
851 unix_name
[pos
+ ret
] = 0;
852 if (!stat( unix_name
, &st
)) return STATUS_SUCCESS
;
854 if (check_case
) goto not_found
; /* we want an exact match */
856 if (pos
> 1) unix_name
[pos
- 1] = 0;
857 else unix_name
[1] = 0; /* keep the initial slash */
859 /* check if it fits in 8.3 so that we don't look for short names if we won't need them */
861 str
.Buffer
= (WCHAR
*)name
;
862 str
.Length
= length
* sizeof(WCHAR
);
863 str
.MaximumLength
= str
.Length
;
864 is_name_8_dot_3
= RtlIsNameLegalDOS8Dot3( &str
, NULL
, &spaces
) && !spaces
;
866 /* now look for it through the directory */
868 #ifdef VFAT_IOCTL_READDIR_BOTH
871 int fd
= open( unix_name
, O_RDONLY
| O_DIRECTORY
);
876 if (ioctl( fd
, VFAT_IOCTL_READDIR_BOTH
, (long)de
) != -1)
878 unix_name
[pos
- 1] = '/';
881 if (!de
[0].d_reclen
) break;
885 ret
= ntdll_umbstowcs( 0, de
[1].d_name
, strlen(de
[1].d_name
),
886 buffer
, MAX_DIR_ENTRY_LEN
);
887 if (ret
== length
&& !memicmpW( buffer
, name
, length
))
889 strcpy( unix_name
+ pos
, de
[1].d_name
);
891 return STATUS_SUCCESS
;
894 ret
= ntdll_umbstowcs( 0, de
[0].d_name
, strlen(de
[0].d_name
),
895 buffer
, MAX_DIR_ENTRY_LEN
);
896 if (ret
== length
&& !memicmpW( buffer
, name
, length
))
898 strcpy( unix_name
+ pos
,
899 de
[1].d_name
[0] ? de
[1].d_name
: de
[0].d_name
);
901 return STATUS_SUCCESS
;
903 if (ioctl( fd
, VFAT_IOCTL_READDIR_BOTH
, (long)de
) == -1)
912 /* fall through to normal handling */
914 #endif /* VFAT_IOCTL_READDIR_BOTH */
916 if (!(dir
= opendir( unix_name
)))
918 if (errno
== ENOENT
) return STATUS_OBJECT_PATH_NOT_FOUND
;
919 else return FILE_GetNtStatus();
921 unix_name
[pos
- 1] = '/';
923 str
.MaximumLength
= sizeof(buffer
);
924 while ((de
= readdir( dir
)))
926 ret
= ntdll_umbstowcs( 0, de
->d_name
, strlen(de
->d_name
), buffer
, MAX_DIR_ENTRY_LEN
);
927 if (ret
== length
&& !memicmpW( buffer
, name
, length
))
929 strcpy( unix_name
+ pos
, de
->d_name
);
931 return STATUS_SUCCESS
;
934 if (!is_name_8_dot_3
) continue;
936 str
.Length
= ret
* sizeof(WCHAR
);
937 if (!RtlIsNameLegalDOS8Dot3( &str
, NULL
, &spaces
) || spaces
)
939 WCHAR short_nameW
[12];
940 ret
= hash_short_file_name( &str
, short_nameW
);
941 if (ret
== length
&& !memicmpW( short_nameW
, name
, length
))
943 strcpy( unix_name
+ pos
, de
->d_name
);
945 return STATUS_SUCCESS
;
950 goto not_found
; /* avoid warning */
953 unix_name
[pos
- 1] = 0;
954 return STATUS_OBJECT_PATH_NOT_FOUND
;
958 /******************************************************************************
961 * Get the Unix path of a DOS device.
963 static NTSTATUS
get_dos_device( const WCHAR
*name
, UINT name_len
, ANSI_STRING
*unix_name_ret
)
965 const char *config_dir
= wine_get_config_dir();
967 char *unix_name
, *new_name
, *dev
;
971 /* make sure the device name is ASCII */
972 for (i
= 0; i
< name_len
; i
++)
973 if (name
[i
] <= 32 || name
[i
] >= 127) return STATUS_OBJECT_NAME_NOT_FOUND
;
975 unix_len
= strlen(config_dir
) + sizeof("/dosdevices/") + name_len
+ 1;
977 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, unix_len
)))
978 return STATUS_NO_MEMORY
;
980 strcpy( unix_name
, config_dir
);
981 strcat( unix_name
, "/dosdevices/" );
982 dev
= unix_name
+ strlen(unix_name
);
984 for (i
= 0; i
< name_len
; i
++) dev
[i
] = (char)tolowerW(name
[i
]);
987 /* special case for drive devices */
988 if (name_len
== 2 && dev
[1] == ':')
996 if (!stat( unix_name
, &st
))
998 TRACE( "%s -> %s\n", debugstr_wn(name
,name_len
), debugstr_a(unix_name
) );
999 unix_name_ret
->Buffer
= unix_name
;
1000 unix_name_ret
->Length
= strlen(unix_name
);
1001 unix_name_ret
->MaximumLength
= unix_len
;
1002 return STATUS_SUCCESS
;
1006 /* now try some defaults for it */
1007 if (!strcmp( dev
, "aux" ))
1009 strcpy( dev
, "com1" );
1012 if (!strcmp( dev
, "prn" ))
1014 strcpy( dev
, "lpt1" );
1017 if (!strcmp( dev
, "nul" ))
1019 strcpy( unix_name
, "/dev/null" );
1020 dev
= NULL
; /* last try */
1025 if (dev
[1] == ':' && dev
[2] == ':') /* drive device */
1027 dev
[2] = 0; /* remove last ':' to get the drive mount point symlink */
1028 new_name
= get_default_drive_device( unix_name
);
1030 else if (!strncmp( dev
, "com", 3 )) new_name
= get_default_com_device( dev
[3] - '0' );
1031 else if (!strncmp( dev
, "lpt", 3 )) new_name
= get_default_lpt_device( dev
[3] - '0' );
1033 if (!new_name
) break;
1035 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
1036 unix_name
= new_name
;
1037 unix_len
= strlen(unix_name
) + 1;
1038 dev
= NULL
; /* last try */
1040 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
1041 return STATUS_OBJECT_NAME_NOT_FOUND
;
1045 /* return the length of the DOS namespace prefix if any */
1046 static inline int get_dos_prefix_len( const UNICODE_STRING
*name
)
1048 static const WCHAR nt_prefixW
[] = {'\\','?','?','\\'};
1049 static const WCHAR dosdev_prefixW
[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\'};
1051 if (name
->Length
> sizeof(nt_prefixW
) &&
1052 !memcmp( name
->Buffer
, nt_prefixW
, sizeof(nt_prefixW
) ))
1053 return sizeof(nt_prefixW
) / sizeof(WCHAR
);
1055 if (name
->Length
> sizeof(dosdev_prefixW
) &&
1056 !memicmpW( name
->Buffer
, dosdev_prefixW
, sizeof(dosdev_prefixW
)/sizeof(WCHAR
) ))
1057 return sizeof(dosdev_prefixW
) / sizeof(WCHAR
);
1063 /******************************************************************************
1064 * wine_nt_to_unix_file_name (NTDLL.@) Not a Windows API
1066 * Convert a file name from NT namespace to Unix namespace.
1068 * If disposition is not FILE_OPEN or FILE_OVERWRITTE, the last path
1069 * element doesn't have to exist; in that case STATUS_NO_SUCH_FILE is
1070 * returned, but the unix name is still filled in properly.
1072 NTSTATUS
wine_nt_to_unix_file_name( const UNICODE_STRING
*nameW
, ANSI_STRING
*unix_name_ret
,
1073 UINT disposition
, BOOLEAN check_case
)
1075 static const WCHAR uncW
[] = {'U','N','C','\\'};
1076 static const WCHAR invalid_charsW
[] = { INVALID_NT_CHARS
, 0 };
1078 NTSTATUS status
= STATUS_SUCCESS
;
1079 const char *config_dir
= wine_get_config_dir();
1080 const WCHAR
*name
, *p
;
1083 int pos
, ret
, name_len
, unix_len
, used_default
;
1085 name
= nameW
->Buffer
;
1086 name_len
= nameW
->Length
/ sizeof(WCHAR
);
1088 if (!name_len
|| !IS_SEPARATOR(name
[0])) return STATUS_OBJECT_PATH_SYNTAX_BAD
;
1090 if ((pos
= get_dos_prefix_len( nameW
)))
1092 BOOLEAN is_unc
= FALSE
;
1097 /* check for UNC prefix */
1098 if (name_len
> 4 && !memicmpW( name
, uncW
, 4 ))
1106 /* check for a drive letter with path */
1107 if (name_len
< 3 || !isalphaW(name
[0]) || name
[1] != ':' || !IS_SEPARATOR(name
[2]))
1109 /* not a drive with path, try other DOS devices */
1110 return get_dos_device( name
, name_len
, unix_name_ret
);
1112 name
+= 2; /* skip drive letter */
1116 /* check for invalid characters */
1117 for (p
= name
; p
< name
+ name_len
; p
++)
1118 if (*p
< 32 || strchrW( invalid_charsW
, *p
)) return STATUS_OBJECT_NAME_INVALID
;
1120 unix_len
= ntdll_wcstoumbs( 0, name
, name_len
, NULL
, 0, NULL
, NULL
);
1121 unix_len
+= MAX_DIR_ENTRY_LEN
+ 3;
1122 unix_len
+= strlen(config_dir
) + sizeof("/dosdevices/") + 3;
1123 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, unix_len
)))
1124 return STATUS_NO_MEMORY
;
1125 strcpy( unix_name
, config_dir
);
1126 strcat( unix_name
, "/dosdevices/" );
1127 pos
= strlen(unix_name
);
1130 strcpy( unix_name
+ pos
, "unc" );
1135 unix_name
[pos
++] = tolowerW( name
[-2] );
1136 unix_name
[pos
++] = ':';
1140 else /* no DOS prefix, assume NT native name, map directly to Unix */
1142 if (!name_len
|| !IS_SEPARATOR(name
[0])) return STATUS_OBJECT_NAME_INVALID
;
1143 unix_len
= ntdll_wcstoumbs( 0, name
, name_len
, NULL
, 0, NULL
, NULL
);
1144 unix_len
+= MAX_DIR_ENTRY_LEN
+ 3;
1145 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, unix_len
)))
1146 return STATUS_NO_MEMORY
;
1150 /* try a shortcut first */
1152 ret
= ntdll_wcstoumbs( 0, name
, name_len
, unix_name
+ pos
, unix_len
- pos
- 1,
1153 NULL
, &used_default
);
1155 while (name_len
&& IS_SEPARATOR(*name
))
1161 if (ret
> 0 && !used_default
) /* if we used the default char the name didn't convert properly */
1164 unix_name
[pos
+ ret
] = 0;
1165 for (p
= unix_name
+ pos
; *p
; p
++) if (*p
== '\\') *p
= '/';
1166 if (!stat( unix_name
, &st
))
1168 /* creation fails with STATUS_ACCESS_DENIED for the root of the drive */
1169 if (disposition
== FILE_CREATE
)
1170 return name_len
? STATUS_OBJECT_NAME_COLLISION
: STATUS_ACCESS_DENIED
;
1175 if (!name_len
) /* empty name -> drive root doesn't exist */
1177 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
1178 return STATUS_OBJECT_PATH_NOT_FOUND
;
1180 if (check_case
&& (disposition
== FILE_OPEN
|| disposition
== FILE_OVERWRITE
))
1182 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
1183 return STATUS_OBJECT_NAME_NOT_FOUND
;
1186 /* now do it component by component */
1190 const WCHAR
*end
, *next
;
1193 while (end
< name
+ name_len
&& !IS_SEPARATOR(*end
)) end
++;
1195 while (next
< name
+ name_len
&& IS_SEPARATOR(*next
)) next
++;
1196 name_len
-= next
- name
;
1198 /* grow the buffer if needed */
1200 if (unix_len
- pos
< MAX_DIR_ENTRY_LEN
+ 2)
1203 unix_len
+= 2 * MAX_DIR_ENTRY_LEN
;
1204 if (!(new_name
= RtlReAllocateHeap( GetProcessHeap(), 0, unix_name
, unix_len
)))
1206 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
1207 return STATUS_NO_MEMORY
;
1209 unix_name
= new_name
;
1212 status
= find_file_in_dir( unix_name
, pos
, name
, end
- name
, check_case
);
1214 /* if this is the last element, not finding it is not necessarily fatal */
1217 if (status
== STATUS_OBJECT_PATH_NOT_FOUND
)
1219 status
= STATUS_OBJECT_NAME_NOT_FOUND
;
1220 if (disposition
!= FILE_OPEN
&& disposition
!= FILE_OVERWRITE
)
1222 ret
= ntdll_wcstoumbs( 0, name
, end
- name
, unix_name
+ pos
+ 1,
1223 MAX_DIR_ENTRY_LEN
, NULL
, &used_default
);
1224 if (ret
> 0 && !used_default
)
1226 unix_name
[pos
] = '/';
1227 unix_name
[pos
+ 1 + ret
] = 0;
1228 status
= STATUS_NO_SUCH_FILE
;
1233 else if (status
== STATUS_SUCCESS
&& disposition
== FILE_CREATE
)
1235 status
= STATUS_OBJECT_NAME_COLLISION
;
1239 if (status
!= STATUS_SUCCESS
)
1241 /* couldn't find it at all, fail */
1242 WARN( "%s not found in %s\n", debugstr_w(name
), unix_name
);
1243 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
1247 pos
+= strlen( unix_name
+ pos
);
1251 WARN( "%s -> %s required a case-insensitive search\n",
1252 debugstr_us(nameW
), debugstr_a(unix_name
) );
1255 TRACE( "%s -> %s\n", debugstr_us(nameW
), debugstr_a(unix_name
) );
1256 unix_name_ret
->Buffer
= unix_name
;
1257 unix_name_ret
->Length
= strlen(unix_name
);
1258 unix_name_ret
->MaximumLength
= unix_len
;
1263 /******************************************************************
1264 * RtlDoesFileExists_U (NTDLL.@)
1266 BOOLEAN WINAPI
RtlDoesFileExists_U(LPCWSTR file_name
)
1268 UNICODE_STRING nt_name
;
1269 ANSI_STRING unix_name
;
1272 if (!RtlDosPathNameToNtPathName_U( file_name
, &nt_name
, NULL
, NULL
)) return FALSE
;
1273 ret
= (wine_nt_to_unix_file_name( &nt_name
, &unix_name
, FILE_OPEN
, FALSE
) == STATUS_SUCCESS
);
1274 if (ret
) RtlFreeAnsiString( &unix_name
);
1275 RtlFreeUnicodeString( &nt_name
);