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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/port.h"
27 #include <sys/types.h>
41 #ifdef HAVE_SYS_STAT_H
42 # include <sys/stat.h>
44 #ifdef HAVE_SYS_SYSCALL_H
45 # include <sys/syscall.h>
47 #ifdef HAVE_SYS_ATTR_H
51 # include <sys/mkdev.h>
52 #elif defined(MAJOR_IN_SYSMACROS)
53 # include <sys/sysmacros.h>
55 #ifdef HAVE_SYS_VNODE_H
56 /* Work around a conflict with Solaris' system list defined in sys/list.h. */
58 #define list_next SYSLIST_NEXT
59 #define list_prev SYSLIST_PREV
60 #define list_head SYSLIST_HEAD
61 #define list_tail SYSLIST_TAIL
62 #define list_move_tail SYSLIST_MOVE_TAIL
63 #define list_remove SYSLIST_REMOVE
64 #include <sys/vnode.h>
73 #ifdef HAVE_SYS_IOCTL_H
74 #include <sys/ioctl.h>
76 #ifdef HAVE_LINUX_IOCTL_H
77 #include <linux/ioctl.h>
79 #ifdef HAVE_LINUX_MAJOR_H
80 # include <linux/major.h>
82 #ifdef HAVE_SYS_PARAM_H
83 #include <sys/param.h>
85 #ifdef HAVE_SYS_MOUNT_H
86 #include <sys/mount.h>
88 #ifdef HAVE_SYS_STATFS_H
89 #include <sys/statfs.h>
97 #define WIN32_NO_STATUS
98 #define NONAMELESSUNION
101 #include "winternl.h"
103 #include "ntdll_misc.h"
104 #include "wine/unicode.h"
105 #include "wine/server.h"
106 #include "wine/list.h"
107 #include "wine/library.h"
108 #include "wine/debug.h"
110 WINE_DEFAULT_DEBUG_CHANNEL(file
);
112 /* just in case... */
113 #undef VFAT_IOCTL_READDIR_BOTH
118 /* We want the real kernel dirent structure, not the libc one */
123 unsigned short d_reclen
;
127 /* Define the VFAT ioctl to get both short and long file names */
128 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, KERNEL_DIRENT [2] )
131 # define O_DIRECTORY 0200000 /* must be directory */
134 #ifdef __NR_getdents64
139 unsigned short d_reclen
;
140 unsigned char d_type
;
145 static inline int getdents64( int fd
, char *de
, unsigned int size
)
147 return syscall( __NR_getdents64
, fd
, de
, size
);
154 #define IS_OPTION_TRUE(ch) ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
155 #define IS_SEPARATOR(ch) ((ch) == '\\' || (ch) == '/')
157 #define INVALID_NT_CHARS '*','?','<','>','|','"'
158 #define INVALID_DOS_CHARS INVALID_NT_CHARS,'+','=',',',';','[',']',' ','\345'
160 #define MAX_DIR_ENTRY_LEN 255 /* max length of a directory entry in chars */
162 #define MAX_IGNORED_FILES 4
170 static struct file_identity ignored_files
[MAX_IGNORED_FILES
];
171 static unsigned int ignored_files_count
;
173 union file_directory_info
176 FILE_DIRECTORY_INFORMATION dir
;
177 FILE_BOTH_DIRECTORY_INFORMATION both
;
178 FILE_FULL_DIRECTORY_INFORMATION full
;
179 FILE_ID_BOTH_DIRECTORY_INFORMATION id_both
;
180 FILE_ID_FULL_DIRECTORY_INFORMATION id_full
;
183 static BOOL show_dot_files
;
184 static RTL_RUN_ONCE init_once
= RTL_RUN_ONCE_INIT
;
186 /* at some point we may want to allow Winelib apps to set this */
187 static const BOOL is_case_sensitive
= FALSE
;
189 UNICODE_STRING system_dir
= { 0, 0, NULL
}; /* system directory */
191 static struct file_identity curdir
;
192 static struct file_identity windir
;
194 static RTL_CRITICAL_SECTION dir_section
;
195 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
198 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
199 0, 0, { (DWORD_PTR
)(__FILE__
": dir_section") }
201 static RTL_CRITICAL_SECTION dir_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
204 /* check if a given Unicode char is OK in a DOS short name */
205 static inline BOOL
is_invalid_dos_char( WCHAR ch
)
207 static const WCHAR invalid_chars
[] = { INVALID_DOS_CHARS
,'~','.',0 };
208 if (ch
> 0x7f) return TRUE
;
209 return strchrW( invalid_chars
, ch
) != NULL
;
212 /* check if the device can be a mounted volume */
213 static inline BOOL
is_valid_mounted_device( const struct stat
*st
)
215 #if defined(linux) || defined(__sun__)
216 return S_ISBLK( st
->st_mode
);
218 /* disks are char devices on *BSD */
219 return S_ISCHR( st
->st_mode
);
223 static inline void ignore_file( const char *name
)
226 assert( ignored_files_count
< MAX_IGNORED_FILES
);
227 if (!stat( name
, &st
))
229 ignored_files
[ignored_files_count
].dev
= st
.st_dev
;
230 ignored_files
[ignored_files_count
].ino
= st
.st_ino
;
231 ignored_files_count
++;
235 static inline BOOL
is_same_file( const struct file_identity
*file
, const struct stat
*st
)
237 return st
->st_dev
== file
->dev
&& st
->st_ino
== file
->ino
;
240 static inline BOOL
is_ignored_file( const struct stat
*st
)
244 for (i
= 0; i
< ignored_files_count
; i
++)
245 if (is_same_file( &ignored_files
[i
], st
)) return TRUE
;
249 static inline unsigned int dir_info_size( FILE_INFORMATION_CLASS
class, unsigned int len
)
253 case FileDirectoryInformation
:
254 return (FIELD_OFFSET( FILE_DIRECTORY_INFORMATION
, FileName
[len
] ) + 7) & ~7;
255 case FileBothDirectoryInformation
:
256 return (FIELD_OFFSET( FILE_BOTH_DIRECTORY_INFORMATION
, FileName
[len
] ) + 7) & ~7;
257 case FileFullDirectoryInformation
:
258 return (FIELD_OFFSET( FILE_FULL_DIRECTORY_INFORMATION
, FileName
[len
] ) + 7) & ~7;
259 case FileIdBothDirectoryInformation
:
260 return (FIELD_OFFSET( FILE_ID_BOTH_DIRECTORY_INFORMATION
, FileName
[len
] ) + 7) & ~7;
261 case FileIdFullDirectoryInformation
:
262 return (FIELD_OFFSET( FILE_ID_FULL_DIRECTORY_INFORMATION
, FileName
[len
] ) + 7) & ~7;
269 static inline unsigned int max_dir_info_size( FILE_INFORMATION_CLASS
class )
271 return dir_info_size( class, MAX_DIR_ENTRY_LEN
);
274 static inline BOOL
has_wildcard( const UNICODE_STRING
*mask
)
277 memchrW( mask
->Buffer
, '*', mask
->Length
/ sizeof(WCHAR
) ) ||
278 memchrW( mask
->Buffer
, '?', mask
->Length
/ sizeof(WCHAR
) ));
282 /* support for a directory queue for filesystem searches */
290 static struct list dir_queue
= LIST_INIT( dir_queue
);
292 static NTSTATUS
add_dir_to_queue( const char *name
)
294 int len
= strlen( name
) + 1;
295 struct dir_name
*dir
= RtlAllocateHeap( GetProcessHeap(), 0,
296 FIELD_OFFSET( struct dir_name
, name
[len
] ));
297 if (!dir
) return STATUS_NO_MEMORY
;
298 strcpy( dir
->name
, name
);
299 list_add_tail( &dir_queue
, &dir
->entry
);
300 return STATUS_SUCCESS
;
303 static NTSTATUS
next_dir_in_queue( char *name
)
305 struct list
*head
= list_head( &dir_queue
);
308 struct dir_name
*dir
= LIST_ENTRY( head
, struct dir_name
, entry
);
309 strcpy( name
, dir
->name
);
310 list_remove( &dir
->entry
);
311 RtlFreeHeap( GetProcessHeap(), 0, dir
);
312 return STATUS_SUCCESS
;
314 return STATUS_OBJECT_NAME_NOT_FOUND
;
317 static void flush_dir_queue(void)
321 while ((head
= list_head( &dir_queue
)))
323 struct dir_name
*dir
= LIST_ENTRY( head
, struct dir_name
, entry
);
324 list_remove( &dir
->entry
);
325 RtlFreeHeap( GetProcessHeap(), 0, dir
);
330 /***********************************************************************
331 * get_default_com_device
333 * Return the default device to use for serial ports.
335 static char *get_default_com_device( int num
)
339 if (num
< 1 || num
> 256) return NULL
;
341 ret
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/ttyS256") );
342 if (!ret
) return NULL
;
343 sprintf( ret
, "/dev/ttyS%d", num
- 1 );
344 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
345 ret
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/cuau256") );
346 if (!ret
) return NULL
;
347 sprintf( ret
, "/dev/cuau%d", num
- 1 );
348 #elif defined(__DragonFly__)
349 ret
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/cuaa256") );
350 if (!ret
) return NULL
;
351 sprintf( ret
, "/dev/cuaa%d", num
- 1 );
353 FIXME( "no known default for device com%d\n", num
);
359 /***********************************************************************
360 * get_default_lpt_device
362 * Return the default device to use for parallel ports.
364 static char *get_default_lpt_device( int num
)
368 if (num
< 1 || num
> 256) return NULL
;
370 ret
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/lp256") );
371 if (!ret
) return NULL
;
372 sprintf( ret
, "/dev/lp%d", num
- 1 );
374 FIXME( "no known default for device lpt%d\n", num
);
381 static char *unescape_field( char *str
)
385 for (in
= out
= str
; *in
; in
++, out
++)
395 else if (in
[1] == '0' && in
[2] == '4' && in
[3] == '0')
400 else if (in
[1] == '0' && in
[2] == '1' && in
[3] == '1')
405 else if (in
[1] == '0' && in
[2] == '1' && in
[3] == '2')
410 else if (in
[1] == '1' && in
[2] == '3' && in
[3] == '4')
422 static inline char *get_field( char **str
)
426 ret
= strsep( str
, " \t" );
427 if (*str
) *str
+= strspn( *str
, " \t" );
431 /************************************************************************
432 * getmntent_replacement
434 * getmntent replacement for Android.
436 * NB returned static buffer is not thread safe; protect with dir_section.
438 static struct mntent
*getmntent_replacement( FILE *f
)
440 static struct mntent entry
;
441 static char buf
[4096];
446 if (!fgets( buf
, sizeof(buf
), f
)) return NULL
;
447 p
= strchr( buf
, '\n' );
449 else /* Partially unread line, move file ptr to end */
452 while (fgets( tmp
, sizeof(tmp
), f
))
453 if (strchr( tmp
, '\n' )) break;
455 start
= buf
+ strspn( buf
, " \t" );
456 } while (start
[0] == '\0' || start
[0] == '#');
458 p
= get_field( &start
);
459 entry
.mnt_fsname
= p
? unescape_field( p
) : (char *)"";
461 p
= get_field( &start
);
462 entry
.mnt_dir
= p
? unescape_field( p
) : (char *)"";
464 p
= get_field( &start
);
465 entry
.mnt_type
= p
? unescape_field( p
) : (char *)"";
467 p
= get_field( &start
);
468 entry
.mnt_opts
= p
? unescape_field( p
) : (char *)"";
470 p
= get_field( &start
);
471 entry
.mnt_freq
= p
? atoi(p
) : 0;
473 p
= get_field( &start
);
474 entry
.mnt_passno
= p
? atoi(p
) : 0;
478 #define getmntent getmntent_replacement
481 /***********************************************************************
482 * DIR_get_drives_info
484 * Retrieve device/inode number for all the drives. Helper for find_drive_root.
486 unsigned int DIR_get_drives_info( struct drive_info info
[MAX_DOS_DRIVES
] )
488 static struct drive_info cache
[MAX_DOS_DRIVES
];
489 static time_t last_update
;
490 static unsigned int nb_drives
;
492 time_t now
= time(NULL
);
494 RtlEnterCriticalSection( &dir_section
);
495 if (now
!= last_update
)
497 const char *config_dir
= wine_get_config_dir();
502 if ((buffer
= RtlAllocateHeap( GetProcessHeap(), 0,
503 strlen(config_dir
) + sizeof("/dosdevices/a:") )))
505 strcpy( buffer
, config_dir
);
506 strcat( buffer
, "/dosdevices/a:" );
507 p
= buffer
+ strlen(buffer
) - 2;
509 for (i
= nb_drives
= 0; i
< MAX_DOS_DRIVES
; i
++)
512 if (!stat( buffer
, &st
))
514 cache
[i
].dev
= st
.st_dev
;
515 cache
[i
].ino
= st
.st_ino
;
524 RtlFreeHeap( GetProcessHeap(), 0, buffer
);
528 memcpy( info
, cache
, sizeof(cache
) );
530 RtlLeaveCriticalSection( &dir_section
);
535 /***********************************************************************
536 * parse_mount_entries
538 * Parse mount entries looking for a given device. Helper for get_default_drive_device.
542 #include <sys/vfstab.h>
543 static char *parse_vfstab_entries( FILE *f
, dev_t dev
, ino_t ino
)
549 while (! getvfsent( f
, &entry
))
551 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
552 if (!strcmp( entry
.vfs_fstype
, "nfs" ) ||
553 !strcmp( entry
.vfs_fstype
, "smbfs" ) ||
554 !strcmp( entry
.vfs_fstype
, "ncpfs" )) continue;
556 if (stat( entry
.vfs_mountp
, &st
) == -1) continue;
557 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
558 if (!strcmp( entry
.vfs_fstype
, "fd" ))
560 if ((device
= strstr( entry
.vfs_mntopts
, "dev=" )))
562 char *p
= strchr( device
+ 4, ',' );
568 return entry
.vfs_special
;
575 static char *parse_mount_entries( FILE *f
, dev_t dev
, ino_t ino
)
577 struct mntent
*entry
;
581 while ((entry
= getmntent( f
)))
583 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
584 if (!strcmp( entry
->mnt_type
, "nfs" ) ||
585 !strcmp( entry
->mnt_type
, "smbfs" ) ||
586 !strcmp( entry
->mnt_type
, "ncpfs" )) continue;
588 if (stat( entry
->mnt_dir
, &st
) == -1) continue;
589 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
590 if (!strcmp( entry
->mnt_type
, "supermount" ))
592 if ((device
= strstr( entry
->mnt_opts
, "dev=" )))
594 char *p
= strchr( device
+ 4, ',' );
599 else if (!stat( entry
->mnt_fsname
, &st
) && S_ISREG(st
.st_mode
))
601 /* if device is a regular file check for a loop mount */
602 if ((device
= strstr( entry
->mnt_opts
, "loop=" )))
604 char *p
= strchr( device
+ 5, ',' );
610 return entry
->mnt_fsname
;
616 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
618 static char *parse_mount_entries( FILE *f
, dev_t dev
, ino_t ino
)
623 while ((entry
= getfsent()))
625 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
626 if (!strcmp( entry
->fs_vfstype
, "nfs" ) ||
627 !strcmp( entry
->fs_vfstype
, "smbfs" ) ||
628 !strcmp( entry
->fs_vfstype
, "ncpfs" )) continue;
630 if (stat( entry
->fs_file
, &st
) == -1) continue;
631 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
632 return entry
->fs_spec
;
639 #include <sys/mnttab.h>
640 static char *parse_mount_entries( FILE *f
, dev_t dev
, ino_t ino
)
647 while (( ! getmntent( f
, &entry
) ))
649 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
650 if (!strcmp( entry
.mnt_fstype
, "nfs" ) ||
651 !strcmp( entry
.mnt_fstype
, "smbfs" ) ||
652 !strcmp( entry
.mnt_fstype
, "ncpfs" )) continue;
654 if (stat( entry
.mnt_mountp
, &st
) == -1) continue;
655 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
656 if (!strcmp( entry
.mnt_fstype
, "fd" ))
658 if ((device
= strstr( entry
.mnt_mntopts
, "dev=" )))
660 char *p
= strchr( device
+ 4, ',' );
666 return entry
.mnt_special
;
672 /***********************************************************************
673 * get_default_drive_device
675 * Return the default device to use for a given drive mount point.
677 static char *get_default_drive_device( const char *root
)
687 /* try to open it first to force it to get mounted */
688 if ((fd
= open( root
, O_RDONLY
| O_DIRECTORY
)) != -1)
690 res
= fstat( fd
, &st
);
693 /* now try normal stat just in case */
694 if (res
== -1) res
= stat( root
, &st
);
695 if (res
== -1) return NULL
;
697 RtlEnterCriticalSection( &dir_section
);
700 if ((f
= fopen( "/proc/mounts", "r" )))
702 device
= parse_mount_entries( f
, st
.st_dev
, st
.st_ino
);
706 if ((f
= fopen( "/etc/mtab", "r" )))
708 device
= parse_mount_entries( f
, st
.st_dev
, st
.st_ino
);
711 /* look through fstab too in case it's not mounted (for instance if it's an audio CD) */
712 if (!device
&& (f
= fopen( "/etc/fstab", "r" )))
714 device
= parse_mount_entries( f
, st
.st_dev
, st
.st_ino
);
720 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(device
) + 1 );
721 if (ret
) strcpy( ret
, device
);
723 RtlLeaveCriticalSection( &dir_section
);
725 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__ ) || defined(__DragonFly__)
730 /* try to open it first to force it to get mounted */
731 if ((fd
= open( root
, O_RDONLY
)) != -1)
733 res
= fstat( fd
, &st
);
736 /* now try normal stat just in case */
737 if (res
== -1) res
= stat( root
, &st
);
738 if (res
== -1) return NULL
;
740 RtlEnterCriticalSection( &dir_section
);
742 /* The FreeBSD parse_mount_entries doesn't require a file argument, so just
743 * pass NULL. Leave the argument in for symmetry.
745 device
= parse_mount_entries( NULL
, st
.st_dev
, st
.st_ino
);
748 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(device
) + 1 );
749 if (ret
) strcpy( ret
, device
);
751 RtlLeaveCriticalSection( &dir_section
);
759 /* try to open it first to force it to get mounted */
760 if ((fd
= open( root
, O_RDONLY
)) != -1)
762 res
= fstat( fd
, &st
);
765 /* now try normal stat just in case */
766 if (res
== -1) res
= stat( root
, &st
);
767 if (res
== -1) return NULL
;
769 RtlEnterCriticalSection( &dir_section
);
771 if ((f
= fopen( "/etc/mnttab", "r" )))
773 device
= parse_mount_entries( f
, st
.st_dev
, st
.st_ino
);
776 /* look through fstab too in case it's not mounted (for instance if it's an audio CD) */
777 if (!device
&& (f
= fopen( "/etc/vfstab", "r" )))
779 device
= parse_vfstab_entries( f
, st
.st_dev
, st
.st_ino
);
784 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(device
) + 1 );
785 if (ret
) strcpy( ret
, device
);
787 RtlLeaveCriticalSection( &dir_section
);
789 #elif defined(__APPLE__)
790 struct statfs
*mntStat
;
796 static const char path_bsd_device
[] = "/dev/disk";
799 res
= stat( root
, &st
);
800 if (res
== -1) return NULL
;
805 RtlEnterCriticalSection( &dir_section
);
807 mntSize
= getmntinfo(&mntStat
, MNT_NOWAIT
);
809 for (i
= 0; i
< mntSize
&& !ret
; i
++)
811 if (stat(mntStat
[i
].f_mntonname
, &st
) == -1) continue;
812 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
814 /* FIXME add support for mounted network drive */
815 if ( strncmp(mntStat
[i
].f_mntfromname
, path_bsd_device
, strlen(path_bsd_device
)) == 0)
817 /* set return value to the corresponding raw BSD node */
818 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(mntStat
[i
].f_mntfromname
) + 2 /* 2 : r and \0 */ );
821 strcpy(ret
, "/dev/r");
822 strcat(ret
, mntStat
[i
].f_mntfromname
+sizeof("/dev/")-1);
826 RtlLeaveCriticalSection( &dir_section
);
829 if (!warned
++) FIXME( "auto detection of DOS devices not supported on this platform\n" );
835 /***********************************************************************
836 * get_device_mount_point
838 * Return the current mount point for a device.
840 static char *get_device_mount_point( dev_t dev
)
847 RtlEnterCriticalSection( &dir_section
);
850 if ((f
= fopen( "/proc/mounts", "r" )))
852 if ((f
= fopen( "/etc/mtab", "r" )))
855 struct mntent
*entry
;
859 while ((entry
= getmntent( f
)))
861 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
862 if (!strcmp( entry
->mnt_type
, "nfs" ) ||
863 !strcmp( entry
->mnt_type
, "smbfs" ) ||
864 !strcmp( entry
->mnt_type
, "ncpfs" )) continue;
866 if (!strcmp( entry
->mnt_type
, "supermount" ))
868 if ((device
= strstr( entry
->mnt_opts
, "dev=" )))
871 if ((p
= strchr( device
, ',' ))) *p
= 0;
874 else if (!stat( entry
->mnt_fsname
, &st
) && S_ISREG(st
.st_mode
))
876 /* if device is a regular file check for a loop mount */
877 if ((device
= strstr( entry
->mnt_opts
, "loop=" )))
880 if ((p
= strchr( device
, ',' ))) *p
= 0;
883 else device
= entry
->mnt_fsname
;
885 if (device
&& !stat( device
, &st
) && S_ISBLK(st
.st_mode
) && st
.st_rdev
== dev
)
887 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(entry
->mnt_dir
) + 1 );
888 if (ret
) strcpy( ret
, entry
->mnt_dir
);
894 RtlLeaveCriticalSection( &dir_section
);
895 #elif defined(__APPLE__)
896 struct statfs
*entry
;
900 RtlEnterCriticalSection( &dir_section
);
902 size
= getmntinfo( &entry
, MNT_NOWAIT
);
903 for (i
= 0; i
< size
; i
++)
905 if (stat( entry
[i
].f_mntfromname
, &st
) == -1) continue;
906 if (S_ISBLK(st
.st_mode
) && st
.st_rdev
== dev
)
908 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(entry
[i
].f_mntonname
) + 1 );
909 if (ret
) strcpy( ret
, entry
[i
].f_mntonname
);
913 RtlLeaveCriticalSection( &dir_section
);
916 if (!warned
++) FIXME( "unmounting devices not supported on this platform\n" );
922 #if defined(HAVE_GETATTRLIST) && defined(ATTR_VOL_CAPABILITIES) && \
923 defined(VOL_CAPABILITIES_FORMAT) && defined(VOL_CAP_FMT_CASE_SENSITIVE)
936 BOOLEAN case_sensitive
;
942 vol_capabilities_attr_t caps
;
945 /***********************************************************************
948 * Checks if the specified file system is in the cache.
950 static struct fs_cache
*look_up_fs_cache( dev_t dev
)
953 for (i
= 0; i
< sizeof(fs_cache
)/sizeof(fs_cache
[0]); i
++)
954 if (fs_cache
[i
].dev
== dev
)
959 /***********************************************************************
962 * Adds the specified file system to the cache.
964 static void add_fs_cache( dev_t dev
, fsid_t fsid
, BOOLEAN case_sensitive
)
967 struct fs_cache
*entry
= look_up_fs_cache( dev
);
971 /* Update the cache */
973 entry
->case_sensitive
= case_sensitive
;
977 /* Add a new entry */
978 for (i
= 0; i
< sizeof(fs_cache
)/sizeof(fs_cache
[0]); i
++)
979 if (fs_cache
[i
].dev
== 0)
981 /* This entry is empty, use it */
982 fs_cache
[i
].dev
= dev
;
983 fs_cache
[i
].fsid
= fsid
;
984 fs_cache
[i
].case_sensitive
= case_sensitive
;
988 /* Cache is out of space, warn */
990 WARN( "FS cache is out of space, expect performance problems\n" );
993 /***********************************************************************
994 * get_dir_case_sensitivity_attr
996 * Checks if the volume containing the specified directory is case
997 * sensitive or not. Uses getattrlist(2).
999 static int get_dir_case_sensitivity_attr( const char *dir
)
1002 struct attrlist attr
;
1003 struct vol_caps caps
;
1004 struct get_fsid get_fsid
;
1005 struct fs_cache
*entry
;
1007 /* First get the FS ID of the volume */
1008 attr
.bitmapcount
= ATTR_BIT_MAP_COUNT
;
1010 attr
.commonattr
= ATTR_CMN_DEVID
|ATTR_CMN_FSID
;
1011 attr
.volattr
= attr
.dirattr
= attr
.fileattr
= attr
.forkattr
= 0;
1013 if (getattrlist( dir
, &attr
, &get_fsid
, sizeof(get_fsid
), 0 ) != 0 ||
1014 get_fsid
.size
!= sizeof(get_fsid
))
1016 /* Try to look it up in the cache */
1017 entry
= look_up_fs_cache( get_fsid
.dev
);
1018 if (entry
&& !memcmp( &entry
->fsid
, &get_fsid
.fsid
, sizeof(fsid_t
) ))
1019 /* Cache lookup succeeded */
1020 return entry
->case_sensitive
;
1021 /* Cache is stale at this point, we have to update it */
1023 mntpoint
= get_device_mount_point( get_fsid
.dev
);
1024 /* Now look up the case-sensitivity */
1025 attr
.commonattr
= 0;
1026 attr
.volattr
= ATTR_VOL_INFO
|ATTR_VOL_CAPABILITIES
;
1027 if (getattrlist( mntpoint
, &attr
, &caps
, sizeof(caps
), 0 ) < 0)
1029 RtlFreeHeap( GetProcessHeap(), 0, mntpoint
);
1030 add_fs_cache( get_fsid
.dev
, get_fsid
.fsid
, TRUE
);
1033 RtlFreeHeap( GetProcessHeap(), 0, mntpoint
);
1034 if (caps
.size
== sizeof(caps
) &&
1035 (caps
.caps
.valid
[VOL_CAPABILITIES_FORMAT
] &
1036 (VOL_CAP_FMT_CASE_SENSITIVE
| VOL_CAP_FMT_CASE_PRESERVING
)) ==
1037 (VOL_CAP_FMT_CASE_SENSITIVE
| VOL_CAP_FMT_CASE_PRESERVING
))
1041 if ((caps
.caps
.capabilities
[VOL_CAPABILITIES_FORMAT
] &
1042 VOL_CAP_FMT_CASE_SENSITIVE
) != VOL_CAP_FMT_CASE_SENSITIVE
)
1046 /* Update the cache */
1047 add_fs_cache( get_fsid
.dev
, get_fsid
.fsid
, ret
);
1054 /***********************************************************************
1055 * get_dir_case_sensitivity_stat
1057 * Checks if the volume containing the specified directory is case
1058 * sensitive or not. Uses statfs(2) or statvfs(2).
1060 static BOOLEAN
get_dir_case_sensitivity_stat( const char *dir
)
1062 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1065 if (statfs( dir
, &stfs
) == -1) return FALSE
;
1066 /* Assume these file systems are always case insensitive on Mac OS.
1067 * For FreeBSD, only assume CIOPFS is case insensitive (AFAIK, Mac OS
1068 * is the only UNIX that supports case-insensitive lookup).
1070 if (!strcmp( stfs
.f_fstypename
, "fusefs" ) &&
1071 !strncmp( stfs
.f_mntfromname
, "ciopfs", 5 ))
1074 if (!strcmp( stfs
.f_fstypename
, "msdos" ) ||
1075 !strcmp( stfs
.f_fstypename
, "cd9660" ) ||
1076 !strcmp( stfs
.f_fstypename
, "udf" ) ||
1077 !strcmp( stfs
.f_fstypename
, "ntfs" ) ||
1078 !strcmp( stfs
.f_fstypename
, "smbfs" ))
1080 #ifdef _DARWIN_FEATURE_64_BIT_INODE
1081 if (!strcmp( stfs
.f_fstypename
, "hfs" ) && (stfs
.f_fssubtype
== 0 ||
1082 stfs
.f_fssubtype
== 1 ||
1083 stfs
.f_fssubtype
== 128))
1086 /* The field says "reserved", but a quick look at the kernel source
1087 * tells us that this "reserved" field is really the same as the
1088 * "fssubtype" field from the inode64 structure (see munge_statfs()
1089 * in <xnu-source>/bsd/vfs/vfs_syscalls.c).
1091 if (!strcmp( stfs
.f_fstypename
, "hfs" ) && (stfs
.f_reserved1
== 0 ||
1092 stfs
.f_reserved1
== 1 ||
1093 stfs
.f_reserved1
== 128))
1099 #elif defined(__NetBSD__)
1100 struct statvfs stfs
;
1102 if (statvfs( dir
, &stfs
) == -1) return FALSE
;
1103 /* Only assume CIOPFS is case insensitive. */
1104 if (strcmp( stfs
.f_fstypename
, "fusefs" ) ||
1105 strncmp( stfs
.f_mntfromname
, "ciopfs", 5 ))
1109 #elif defined(__linux__)
1114 /* Only assume CIOPFS is case insensitive. */
1115 if (statfs( dir
, &stfs
) == -1) return FALSE
;
1116 if (stfs
.f_type
!= 0x65735546 /* FUSE_SUPER_MAGIC */)
1118 /* Normally, we'd have to parse the mtab to find out exactly what
1119 * kind of FUSE FS this is. But, someone on wine-devel suggested
1120 * a shortcut. We'll stat a special file in the directory. If it's
1121 * there, we'll assume it's a CIOPFS, else not.
1122 * This will break if somebody puts a file named ".ciopfs" in a non-
1125 cifile
= RtlAllocateHeap( GetProcessHeap(), 0, strlen( dir
)+sizeof("/.ciopfs") );
1126 if (!cifile
) return TRUE
;
1127 strcpy( cifile
, dir
);
1128 strcat( cifile
, "/.ciopfs" );
1129 if (stat( cifile
, &st
) == 0)
1131 RtlFreeHeap( GetProcessHeap(), 0, cifile
);
1134 RtlFreeHeap( GetProcessHeap(), 0, cifile
);
1142 /***********************************************************************
1143 * get_dir_case_sensitivity
1145 * Checks if the volume containing the specified directory is case
1146 * sensitive or not. Uses statfs(2) or statvfs(2).
1148 static BOOLEAN
get_dir_case_sensitivity( const char *dir
)
1150 #if defined(HAVE_GETATTRLIST) && defined(ATTR_VOL_CAPABILITIES) && \
1151 defined(VOL_CAPABILITIES_FORMAT) && defined(VOL_CAP_FMT_CASE_SENSITIVE)
1152 int case_sensitive
= get_dir_case_sensitivity_attr( dir
);
1153 if (case_sensitive
!= -1) return case_sensitive
;
1155 return get_dir_case_sensitivity_stat( dir
);
1159 /***********************************************************************
1162 * Initialize the show_dot_files options.
1164 static DWORD WINAPI
init_options( RTL_RUN_ONCE
*once
, void *param
, void **context
)
1166 static const WCHAR WineW
[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e',0};
1167 static const WCHAR ShowDotFilesW
[] = {'S','h','o','w','D','o','t','F','i','l','e','s',0};
1171 OBJECT_ATTRIBUTES attr
;
1172 UNICODE_STRING nameW
;
1174 RtlOpenCurrentUser( KEY_ALL_ACCESS
, &root
);
1175 attr
.Length
= sizeof(attr
);
1176 attr
.RootDirectory
= root
;
1177 attr
.ObjectName
= &nameW
;
1178 attr
.Attributes
= 0;
1179 attr
.SecurityDescriptor
= NULL
;
1180 attr
.SecurityQualityOfService
= NULL
;
1181 RtlInitUnicodeString( &nameW
, WineW
);
1183 /* @@ Wine registry key: HKCU\Software\Wine */
1184 if (!NtOpenKey( &hkey
, KEY_ALL_ACCESS
, &attr
))
1186 RtlInitUnicodeString( &nameW
, ShowDotFilesW
);
1187 if (!NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
, tmp
, sizeof(tmp
), &dummy
))
1189 WCHAR
*str
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)tmp
)->Data
;
1190 show_dot_files
= IS_OPTION_TRUE( str
[0] );
1196 /* a couple of directories that we don't want to return in directory searches */
1197 ignore_file( wine_get_config_dir() );
1198 ignore_file( "/dev" );
1199 ignore_file( "/proc" );
1201 ignore_file( "/sys" );
1207 /***********************************************************************
1208 * DIR_is_hidden_file
1210 * Check if the specified file should be hidden based on its name and the show dot files option.
1212 BOOL
DIR_is_hidden_file( const UNICODE_STRING
*name
)
1216 RtlRunOnceExecuteOnce( &init_once
, init_options
, NULL
, NULL
);
1218 if (show_dot_files
) return FALSE
;
1220 end
= p
= name
->Buffer
+ name
->Length
/sizeof(WCHAR
);
1221 while (p
> name
->Buffer
&& IS_SEPARATOR(p
[-1])) p
--;
1222 while (p
> name
->Buffer
&& !IS_SEPARATOR(p
[-1])) p
--;
1223 if (p
== end
|| *p
!= '.') return FALSE
;
1224 /* make sure it isn't '.' or '..' */
1225 if (p
+ 1 == end
) return FALSE
;
1226 if (p
[1] == '.' && p
+ 2 == end
) return FALSE
;
1231 /***********************************************************************
1232 * hash_short_file_name
1234 * Transform a Unix file name into a hashed DOS name. If the name is a valid
1235 * DOS name, it is converted to upper-case; otherwise it is replaced by a
1236 * hashed version that fits in 8.3 format.
1237 * 'buffer' must be at least 12 characters long.
1238 * Returns length of short name in bytes; short name is NOT null-terminated.
1240 static ULONG
hash_short_file_name( const UNICODE_STRING
*name
, LPWSTR buffer
)
1242 static const char hash_chars
[32] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345";
1244 LPCWSTR p
, ext
, end
= name
->Buffer
+ name
->Length
/ sizeof(WCHAR
);
1246 unsigned short hash
;
1249 /* Compute the hash code of the file name */
1250 /* If you know something about hash functions, feel free to */
1251 /* insert a better algorithm here... */
1252 if (!is_case_sensitive
)
1254 for (p
= name
->Buffer
, hash
= 0xbeef; p
< end
- 1; p
++)
1255 hash
= (hash
<<3) ^ (hash
>>5) ^ tolowerW(*p
) ^ (tolowerW(p
[1]) << 8);
1256 hash
= (hash
<<3) ^ (hash
>>5) ^ tolowerW(*p
); /* Last character */
1260 for (p
= name
->Buffer
, hash
= 0xbeef; p
< end
- 1; p
++)
1261 hash
= (hash
<< 3) ^ (hash
>> 5) ^ *p
^ (p
[1] << 8);
1262 hash
= (hash
<< 3) ^ (hash
>> 5) ^ *p
; /* Last character */
1265 /* Find last dot for start of the extension */
1266 for (p
= name
->Buffer
+ 1, ext
= NULL
; p
< end
- 1; p
++) if (*p
== '.') ext
= p
;
1268 /* Copy first 4 chars, replacing invalid chars with '_' */
1269 for (i
= 4, p
= name
->Buffer
, dst
= buffer
; i
> 0; i
--, p
++)
1271 if (p
== end
|| p
== ext
) break;
1272 *dst
++ = is_invalid_dos_char(*p
) ? '_' : toupperW(*p
);
1274 /* Pad to 5 chars with '~' */
1275 while (i
-- >= 0) *dst
++ = '~';
1277 /* Insert hash code converted to 3 ASCII chars */
1278 *dst
++ = hash_chars
[(hash
>> 10) & 0x1f];
1279 *dst
++ = hash_chars
[(hash
>> 5) & 0x1f];
1280 *dst
++ = hash_chars
[hash
& 0x1f];
1282 /* Copy the first 3 chars of the extension (if any) */
1286 for (i
= 3, ext
++; (i
> 0) && ext
< end
; i
--, ext
++)
1287 *dst
++ = is_invalid_dos_char(*ext
) ? '_' : toupperW(*ext
);
1289 return dst
- buffer
;
1293 /***********************************************************************
1296 * Check a long file name against a mask.
1298 * Tests (done in W95 DOS shell - case insensitive):
1299 * *.txt test1.test.txt *
1301 * *.t??????.t* test1.ta.tornado.txt *
1302 * *tornado* test1.ta.tornado.txt *
1303 * t*t test1.ta.tornado.txt *
1305 * ?est??? test1.txt -
1306 * *test1.txt* test1.txt *
1307 * h?l?o*t.dat hellothisisatest.dat *
1309 static BOOLEAN
match_filename( const UNICODE_STRING
*name_str
, const UNICODE_STRING
*mask_str
)
1312 const WCHAR
*name
= name_str
->Buffer
;
1313 const WCHAR
*mask
= mask_str
->Buffer
;
1314 const WCHAR
*name_end
= name
+ name_str
->Length
/ sizeof(WCHAR
);
1315 const WCHAR
*mask_end
= mask
+ mask_str
->Length
/ sizeof(WCHAR
);
1316 const WCHAR
*lastjoker
= NULL
;
1317 const WCHAR
*next_to_retry
= NULL
;
1319 TRACE("(%s, %s)\n", debugstr_us(name_str
), debugstr_us(mask_str
));
1321 while (name
< name_end
&& mask
< mask_end
)
1327 while (mask
< mask_end
&& *mask
== '*') mask
++; /* Skip consecutive '*' */
1328 if (mask
== mask_end
) return TRUE
; /* end of mask is all '*', so match */
1331 /* skip to the next match after the joker(s) */
1332 if (is_case_sensitive
)
1333 while (name
< name_end
&& (*name
!= *mask
)) name
++;
1335 while (name
< name_end
&& (toupperW(*name
) != toupperW(*mask
))) name
++;
1336 next_to_retry
= name
;
1343 if (is_case_sensitive
) mismatch
= (*mask
!= *name
);
1344 else mismatch
= (toupperW(*mask
) != toupperW(*name
));
1350 if (mask
== mask_end
)
1352 if (name
== name_end
) return TRUE
;
1353 if (lastjoker
) mask
= lastjoker
;
1356 else /* mismatch ! */
1358 if (lastjoker
) /* we had an '*', so we can try unlimitedly */
1362 /* this scan sequence was a mismatch, so restart
1363 * 1 char after the first char we checked last time */
1365 name
= next_to_retry
;
1367 else return FALSE
; /* bad luck */
1372 while (mask
< mask_end
&& ((*mask
== '.') || (*mask
== '*')))
1373 mask
++; /* Ignore trailing '.' or '*' in mask */
1374 return (name
== name_end
&& mask
== mask_end
);
1378 /***********************************************************************
1381 * helper for NtQueryDirectoryFile
1383 static union file_directory_info
*append_entry( void *info_ptr
, IO_STATUS_BLOCK
*io
, ULONG max_length
,
1384 const char *long_name
, const char *short_name
,
1385 const UNICODE_STRING
*mask
, FILE_INFORMATION_CLASS
class )
1387 union file_directory_info
*info
;
1388 int i
, long_len
, short_len
, total_len
;
1390 WCHAR long_nameW
[MAX_DIR_ENTRY_LEN
];
1391 WCHAR short_nameW
[12];
1396 io
->u
.Status
= STATUS_SUCCESS
;
1397 long_len
= ntdll_umbstowcs( 0, long_name
, strlen(long_name
), long_nameW
, MAX_DIR_ENTRY_LEN
);
1398 if (long_len
== -1) return NULL
;
1400 str
.Buffer
= long_nameW
;
1401 str
.Length
= long_len
* sizeof(WCHAR
);
1402 str
.MaximumLength
= sizeof(long_nameW
);
1406 short_len
= ntdll_umbstowcs( 0, short_name
, strlen(short_name
),
1407 short_nameW
, sizeof(short_nameW
) / sizeof(WCHAR
) );
1408 if (short_len
== -1) short_len
= sizeof(short_nameW
) / sizeof(WCHAR
);
1410 else /* generate a short name if necessary */
1415 if (!RtlIsNameLegalDOS8Dot3( &str
, NULL
, &spaces
) || spaces
)
1416 short_len
= hash_short_file_name( &str
, short_nameW
);
1419 TRACE( "long %s short %s mask %s\n",
1420 debugstr_us(&str
), debugstr_wn(short_nameW
, short_len
), debugstr_us(mask
) );
1422 if (mask
&& !match_filename( &str
, mask
))
1424 if (!short_len
) return NULL
; /* no short name to match */
1425 str
.Buffer
= short_nameW
;
1426 str
.Length
= short_len
* sizeof(WCHAR
);
1427 str
.MaximumLength
= sizeof(short_nameW
);
1428 if (!match_filename( &str
, mask
)) return NULL
;
1431 if (get_file_info( long_name
, &st
, &attributes
) == -1) return NULL
;
1432 if (is_ignored_file( &st
))
1434 TRACE( "ignoring file %s\n", long_name
);
1437 if (!show_dot_files
&& long_name
[0] == '.' && long_name
[1] && (long_name
[1] != '.' || long_name
[2]))
1438 attributes
|= FILE_ATTRIBUTE_HIDDEN
;
1440 total_len
= dir_info_size( class, long_len
);
1441 if (io
->Information
+ total_len
> max_length
)
1443 total_len
= max_length
- io
->Information
;
1444 io
->u
.Status
= STATUS_BUFFER_OVERFLOW
;
1446 info
= (union file_directory_info
*)((char *)info_ptr
+ io
->Information
);
1447 if (st
.st_dev
!= curdir
.dev
) st
.st_ino
= 0; /* ignore inode if on a different device */
1448 /* all the structures start with a FileDirectoryInformation layout */
1449 fill_file_info( &st
, attributes
, info
, class );
1450 info
->dir
.NextEntryOffset
= total_len
;
1451 info
->dir
.FileIndex
= 0; /* NTFS always has 0 here, so let's not bother with it */
1455 case FileDirectoryInformation
:
1456 info
->dir
.FileNameLength
= long_len
* sizeof(WCHAR
);
1457 filename
= info
->dir
.FileName
;
1460 case FileFullDirectoryInformation
:
1461 info
->full
.EaSize
= 0; /* FIXME */
1462 info
->full
.FileNameLength
= long_len
* sizeof(WCHAR
);
1463 filename
= info
->full
.FileName
;
1466 case FileIdFullDirectoryInformation
:
1467 info
->id_full
.EaSize
= 0; /* FIXME */
1468 info
->id_full
.FileNameLength
= long_len
* sizeof(WCHAR
);
1469 filename
= info
->id_full
.FileName
;
1472 case FileBothDirectoryInformation
:
1473 info
->both
.EaSize
= 0; /* FIXME */
1474 info
->both
.ShortNameLength
= short_len
* sizeof(WCHAR
);
1475 for (i
= 0; i
< short_len
; i
++) info
->both
.ShortName
[i
] = toupperW(short_nameW
[i
]);
1476 info
->both
.FileNameLength
= long_len
* sizeof(WCHAR
);
1477 filename
= info
->both
.FileName
;
1480 case FileIdBothDirectoryInformation
:
1481 info
->id_both
.EaSize
= 0; /* FIXME */
1482 info
->id_both
.ShortNameLength
= short_len
* sizeof(WCHAR
);
1483 for (i
= 0; i
< short_len
; i
++) info
->id_both
.ShortName
[i
] = toupperW(short_nameW
[i
]);
1484 info
->id_both
.FileNameLength
= long_len
* sizeof(WCHAR
);
1485 filename
= info
->id_both
.FileName
;
1492 memcpy( filename
, long_nameW
, long_len
* sizeof(WCHAR
) );
1493 io
->Information
+= total_len
;
1498 #ifdef VFAT_IOCTL_READDIR_BOTH
1500 /***********************************************************************
1503 * Wrapper for the VFAT ioctl to work around various kernel bugs.
1504 * dir_section must be held by caller.
1506 static KERNEL_DIRENT
*start_vfat_ioctl( int fd
)
1508 static KERNEL_DIRENT
*de
;
1513 SIZE_T size
= 2 * sizeof(*de
) + page_size
;
1516 if (NtAllocateVirtualMemory( GetCurrentProcess(), &addr
, 1, &size
, MEM_RESERVE
, PAGE_READWRITE
))
1518 /* commit only the size needed for the dir entries */
1519 /* this leaves an extra unaccessible page, which should make the kernel */
1520 /* fail with -EFAULT before it stomps all over our memory */
1522 size
= 2 * sizeof(*de
);
1523 NtAllocateVirtualMemory( GetCurrentProcess(), &addr
, 1, &size
, MEM_COMMIT
, PAGE_READWRITE
);
1526 /* set d_reclen to 65535 to work around an AFS kernel bug */
1527 de
[0].d_reclen
= 65535;
1528 res
= ioctl( fd
, VFAT_IOCTL_READDIR_BOTH
, (long)de
);
1531 if (errno
!= ENOENT
) return NULL
; /* VFAT ioctl probably not supported */
1532 de
[0].d_reclen
= 0; /* eof */
1534 else if (!res
&& de
[0].d_reclen
== 65535) return NULL
; /* AFS bug */
1540 /***********************************************************************
1541 * read_directory_vfat
1543 * Read a directory using the VFAT ioctl; helper for NtQueryDirectoryFile.
1545 static int read_directory_vfat( int fd
, IO_STATUS_BLOCK
*io
, void *buffer
, ULONG length
,
1546 BOOLEAN single_entry
, const UNICODE_STRING
*mask
,
1547 BOOLEAN restart_scan
, FILE_INFORMATION_CLASS
class )
1552 union file_directory_info
*info
, *last_info
= NULL
;
1554 io
->u
.Status
= STATUS_SUCCESS
;
1556 if (restart_scan
) lseek( fd
, 0, SEEK_SET
);
1558 if (length
< max_dir_info_size(class)) /* we may have to return a partial entry here */
1560 off_t old_pos
= lseek( fd
, 0, SEEK_CUR
);
1562 if (!(de
= start_vfat_ioctl( fd
))) return -1; /* not supported */
1564 while (de
[0].d_reclen
)
1566 /* make sure names are null-terminated to work around an x86-64 kernel bug */
1567 len
= min(de
[0].d_reclen
, sizeof(de
[0].d_name
) - 1 );
1568 de
[0].d_name
[len
] = 0;
1569 len
= min(de
[1].d_reclen
, sizeof(de
[1].d_name
) - 1 );
1570 de
[1].d_name
[len
] = 0;
1572 if (de
[1].d_name
[0])
1573 info
= append_entry( buffer
, io
, length
, de
[1].d_name
, de
[0].d_name
, mask
, class );
1575 info
= append_entry( buffer
, io
, length
, de
[0].d_name
, NULL
, mask
, class );
1579 if (io
->u
.Status
== STATUS_BUFFER_OVERFLOW
)
1580 lseek( fd
, old_pos
, SEEK_SET
); /* restore pos to previous entry */
1583 old_pos
= lseek( fd
, 0, SEEK_CUR
);
1584 if (ioctl( fd
, VFAT_IOCTL_READDIR_BOTH
, (long)de
) == -1) break;
1587 else /* we'll only return full entries, no need to worry about overflow */
1589 if (!(de
= start_vfat_ioctl( fd
))) return -1; /* not supported */
1591 while (de
[0].d_reclen
)
1593 /* make sure names are null-terminated to work around an x86-64 kernel bug */
1594 len
= min(de
[0].d_reclen
, sizeof(de
[0].d_name
) - 1 );
1595 de
[0].d_name
[len
] = 0;
1596 len
= min(de
[1].d_reclen
, sizeof(de
[1].d_name
) - 1 );
1597 de
[1].d_name
[len
] = 0;
1599 if (de
[1].d_name
[0])
1600 info
= append_entry( buffer
, io
, length
, de
[1].d_name
, de
[0].d_name
, mask
, class );
1602 info
= append_entry( buffer
, io
, length
, de
[0].d_name
, NULL
, mask
, class );
1606 if (single_entry
) break;
1607 /* check if we still have enough space for the largest possible entry */
1608 if (io
->Information
+ max_dir_info_size(class) > length
) break;
1610 if (ioctl( fd
, VFAT_IOCTL_READDIR_BOTH
, (long)de
) == -1) break;
1614 if (last_info
) last_info
->next
= 0;
1615 else io
->u
.Status
= restart_scan
? STATUS_NO_SUCH_FILE
: STATUS_NO_MORE_FILES
;
1618 #endif /* VFAT_IOCTL_READDIR_BOTH */
1622 /***********************************************************************
1623 * read_first_dent_name
1625 * reads name of first or second dentry (if they have inodes).
1627 static char *read_first_dent_name( int which
, int fd
, off_t second_offs
, KERNEL_DIRENT64
*de_first_two
,
1628 char *buffer
, size_t size
, BOOL
*buffer_changed
)
1630 KERNEL_DIRENT64
*de
;
1637 de
= (KERNEL_DIRENT64
*)((char *)de
+ de
->d_reclen
);
1639 return de
->d_ino
? de
->d_name
: NULL
;
1642 *buffer_changed
= TRUE
;
1643 lseek( fd
, which
== 1 ? second_offs
: 0, SEEK_SET
);
1644 res
= getdents64( fd
, buffer
, size
);
1648 de
= (KERNEL_DIRENT64
*)buffer
;
1649 return de
->d_ino
? de
->d_name
: NULL
;
1652 /***********************************************************************
1653 * read_directory_getdents
1655 * Read a directory using the Linux getdents64 system call; helper for NtQueryDirectoryFile.
1657 static int read_directory_getdents( int fd
, IO_STATUS_BLOCK
*io
, void *buffer
, ULONG length
,
1658 BOOLEAN single_entry
, const UNICODE_STRING
*mask
,
1659 BOOLEAN restart_scan
, FILE_INFORMATION_CLASS
class )
1661 static off_t second_entry_pos
;
1662 static struct file_identity last_dir_id
;
1663 off_t old_pos
= 0, next_pos
;
1664 size_t size
= length
;
1665 char *data
, local_buffer
[8192];
1666 KERNEL_DIRENT64
*de
, *de_first_two
= NULL
;
1667 union file_directory_info
*info
, *last_info
= NULL
;
1668 const char *filename
;
1669 BOOL data_buffer_changed
;
1672 if (size
<= sizeof(local_buffer
) || !(data
= RtlAllocateHeap( GetProcessHeap(), 0, size
)))
1674 size
= sizeof(local_buffer
);
1675 data
= local_buffer
;
1678 if (restart_scan
) lseek( fd
, 0, SEEK_SET
);
1681 old_pos
= lseek( fd
, 0, SEEK_CUR
);
1684 io
->u
.Status
= (errno
== ENOENT
) ? STATUS_NO_MORE_FILES
: FILE_GetNtStatus();
1690 io
->u
.Status
= STATUS_SUCCESS
;
1691 de
= (KERNEL_DIRENT64
*)data
;
1693 /* if old_pos is not 0 we don't know how many entries have been returned already,
1694 * so maintain second_entry_pos to know when to return '..' */
1695 if (old_pos
!= 0 && (last_dir_id
.dev
!= curdir
.dev
|| last_dir_id
.ino
!= curdir
.ino
))
1697 lseek( fd
, 0, SEEK_SET
);
1698 res
= getdents64( fd
, data
, size
);
1701 second_entry_pos
= de
->d_off
;
1702 last_dir_id
= curdir
;
1704 lseek( fd
, old_pos
, SEEK_SET
);
1707 res
= getdents64( fd
, data
, size
);
1710 if (errno
!= ENOSYS
)
1712 io
->u
.Status
= FILE_GetNtStatus();
1718 if (old_pos
== 0 && res
> 0)
1720 second_entry_pos
= de
->d_off
;
1721 last_dir_id
= curdir
;
1722 if (res
> de
->d_reclen
)
1728 res
-= de
->d_reclen
;
1729 next_pos
= de
->d_off
;
1732 /* we must return first 2 entries as "." and "..", but getdents64()
1733 * can return them anywhere, so swap first entries with "." and ".." */
1736 else if (old_pos
== second_entry_pos
)
1738 else if (!strcmp( de
->d_name
, "." ) || !strcmp( de
->d_name
, ".." ))
1740 swap_to
= !strcmp( de
->d_name
, "." ) ? 0 : 1;
1741 data_buffer_changed
= FALSE
;
1743 filename
= read_first_dent_name( swap_to
, fd
, second_entry_pos
, de_first_two
,
1744 data
, size
, &data_buffer_changed
);
1745 if (filename
!= NULL
&& (!strcmp( filename
, "." ) || !strcmp( filename
, ".." )))
1746 filename
= read_first_dent_name( swap_to
^ 1, fd
, second_entry_pos
, NULL
,
1747 data
, size
, &data_buffer_changed
);
1748 if (data_buffer_changed
)
1750 lseek( fd
, next_pos
, SEEK_SET
);
1755 filename
= de
->d_name
;
1757 if (filename
&& (info
= append_entry( buffer
, io
, length
, filename
, NULL
, mask
, class )))
1760 if (io
->u
.Status
== STATUS_BUFFER_OVERFLOW
)
1762 lseek( fd
, old_pos
, SEEK_SET
); /* restore pos to previous entry */
1765 /* check if we still have enough space for the largest possible entry */
1766 if (single_entry
|| io
->Information
+ max_dir_info_size(class) > length
)
1768 if (res
> 0) lseek( fd
, next_pos
, SEEK_SET
); /* set pos to next entry */
1773 /* move on to the next entry */
1774 if (res
> 0) de
= (KERNEL_DIRENT64
*)((char *)de
+ de
->d_reclen
);
1777 res
= getdents64( fd
, data
, size
);
1778 de
= (KERNEL_DIRENT64
*)data
;
1779 de_first_two
= NULL
;
1783 if (last_info
) last_info
->next
= 0;
1784 else io
->u
.Status
= restart_scan
? STATUS_NO_SUCH_FILE
: STATUS_NO_MORE_FILES
;
1787 if (data
!= local_buffer
) RtlFreeHeap( GetProcessHeap(), 0, data
);
1791 #elif defined HAVE_GETDIRENTRIES
1793 #ifdef _DARWIN_FEATURE_64_BIT_INODE
1795 /* Darwin doesn't provide a version of getdirentries with support for 64-bit
1796 * inodes. When 64-bit inodes are enabled, the getdirentries symbol is mapped
1797 * to _getdirentries_is_not_available_when_64_bit_inodes_are_in_effect so that
1798 * we get link errors if we try to use it. We still need getdirentries, but we
1799 * don't need it to support 64-bit inodes. So, we use the legacy getdirentries
1800 * with 32-bit inodes. We have to be careful to use a corresponding dirent
1803 int darwin_legacy_getdirentries(int, char *, int, long *) __asm("_getdirentries");
1804 #define getdirentries darwin_legacy_getdirentries
1806 struct darwin_legacy_dirent
{
1808 __uint16_t d_reclen
;
1811 char d_name
[__DARWIN_MAXNAMLEN
+ 1];
1813 #define dirent darwin_legacy_dirent
1817 /***********************************************************************
1818 * wine_getdirentries
1820 * Wrapper for the BSD getdirentries system call to fix a bug in the
1821 * Mac OS X version. For some file systems (at least Apple Filing
1822 * Protocol a.k.a. AFP), getdirentries resets the file position to 0
1823 * when it's about to return 0 (no more entries). So, a subsequent
1824 * getdirentries call starts over at the beginning again, causing an
1827 static inline int wine_getdirentries(int fd
, char *buf
, int nbytes
, long *basep
)
1829 int res
= getdirentries(fd
, buf
, nbytes
, basep
);
1832 lseek(fd
, *basep
, SEEK_SET
);
1837 static inline int dir_reclen(struct dirent
*de
)
1839 #ifdef HAVE_STRUCT_DIRENT_D_RECLEN
1840 return de
->d_reclen
;
1842 return _DIRENT_RECLEN(de
->d_namlen
);
1846 /***********************************************************************
1847 * read_directory_getdirentries
1849 * Read a directory using the BSD getdirentries system call; helper for NtQueryDirectoryFile.
1851 static int read_directory_getdirentries( int fd
, IO_STATUS_BLOCK
*io
, void *buffer
, ULONG length
,
1852 BOOLEAN single_entry
, const UNICODE_STRING
*mask
,
1853 BOOLEAN restart_scan
, FILE_INFORMATION_CLASS
class )
1856 ULONG_PTR restart_info_pos
= 0;
1857 size_t size
, initial_size
= length
;
1858 int res
, fake_dot_dot
= 1;
1859 char *data
, local_buffer
[8192];
1861 union file_directory_info
*info
, *last_info
= NULL
, *restart_last_info
= NULL
;
1863 size
= initial_size
;
1864 data
= local_buffer
;
1865 if (size
> sizeof(local_buffer
) && !(data
= RtlAllocateHeap( GetProcessHeap(), 0, size
)))
1867 io
->u
.Status
= STATUS_NO_MEMORY
;
1868 return io
->u
.Status
;
1871 if (restart_scan
) lseek( fd
, 0, SEEK_SET
);
1873 io
->u
.Status
= STATUS_SUCCESS
;
1875 /* FIXME: should make sure size is larger than filesystem block size */
1876 res
= wine_getdirentries( fd
, data
, size
, &restart_pos
);
1879 io
->u
.Status
= FILE_GetNtStatus();
1884 de
= (struct dirent
*)data
;
1888 /* check if we got . and .. from getdirentries */
1891 if (!strcmp( de
->d_name
, "." ) && res
> dir_reclen(de
))
1893 struct dirent
*next_de
= (struct dirent
*)(data
+ dir_reclen(de
));
1894 if (!strcmp( next_de
->d_name
, ".." )) fake_dot_dot
= 0;
1897 /* make sure we have enough room for both entries */
1900 const ULONG min_info_size
= dir_info_size( class, 1 ) + dir_info_size( class, 2 );
1901 if (length
< min_info_size
|| single_entry
)
1903 FIXME( "not enough room %u/%u for fake . and .. entries\n", length
, single_entry
);
1910 if ((info
= append_entry( buffer
, io
, length
, ".", NULL
, mask
, class )))
1912 if ((info
= append_entry( buffer
, io
, length
, "..", NULL
, mask
, class )))
1915 restart_last_info
= last_info
;
1916 restart_info_pos
= io
->Information
;
1918 /* check if we still have enough space for the largest possible entry */
1919 if (last_info
&& io
->Information
+ max_dir_info_size(class) > length
)
1921 lseek( fd
, 0, SEEK_SET
); /* reset pos to first entry */
1929 res
-= dir_reclen(de
);
1931 !(fake_dot_dot
&& (!strcmp( de
->d_name
, "." ) || !strcmp( de
->d_name
, ".." ))) &&
1932 ((info
= append_entry( buffer
, io
, length
, de
->d_name
, NULL
, mask
, class ))))
1935 if (io
->u
.Status
== STATUS_BUFFER_OVERFLOW
)
1937 lseek( fd
, (unsigned long)restart_pos
, SEEK_SET
);
1938 if (restart_info_pos
) /* if we have a complete read already, return it */
1940 io
->u
.Status
= STATUS_SUCCESS
;
1941 io
->Information
= restart_info_pos
;
1942 last_info
= restart_last_info
;
1945 /* otherwise restart from the start with a smaller size */
1946 size
= (char *)de
- data
;
1948 io
->Information
= 0;
1952 if (!has_wildcard( mask
)) break;
1953 /* if we have to return but the buffer contains more data, restart with a smaller size */
1954 if (res
> 0 && (single_entry
|| io
->Information
+ max_dir_info_size(class) > length
))
1956 lseek( fd
, (unsigned long)restart_pos
, SEEK_SET
);
1957 size
= (char *)de
+ dir_reclen(de
) - data
;
1958 io
->Information
= restart_info_pos
;
1959 last_info
= restart_last_info
;
1963 /* move on to the next entry */
1966 de
= (struct dirent
*)((char *)de
+ dir_reclen(de
));
1969 if (size
< initial_size
) break; /* already restarted once, give up now */
1970 restart_last_info
= last_info
;
1971 restart_info_pos
= io
->Information
;
1973 res
= wine_getdirentries( fd
, data
, size
, &restart_pos
);
1974 de
= (struct dirent
*)data
;
1977 if (last_info
) last_info
->next
= 0;
1978 else io
->u
.Status
= restart_scan
? STATUS_NO_SUCH_FILE
: STATUS_NO_MORE_FILES
;
1981 if (data
!= local_buffer
) RtlFreeHeap( GetProcessHeap(), 0, data
);
1985 #ifdef _DARWIN_FEATURE_64_BIT_INODE
1986 #undef getdirentries
1990 #endif /* HAVE_GETDIRENTRIES */
1993 /***********************************************************************
1994 * read_directory_readdir
1996 * Read a directory using the POSIX readdir interface; helper for NtQueryDirectoryFile.
1998 static void read_directory_readdir( int fd
, IO_STATUS_BLOCK
*io
, void *buffer
, ULONG length
,
1999 BOOLEAN single_entry
, const UNICODE_STRING
*mask
,
2000 BOOLEAN restart_scan
, FILE_INFORMATION_CLASS
class )
2003 off_t i
, old_pos
= 0;
2005 union file_directory_info
*info
, *last_info
= NULL
;
2007 if (!(dir
= opendir( "." )))
2009 io
->u
.Status
= FILE_GetNtStatus();
2015 old_pos
= lseek( fd
, 0, SEEK_CUR
);
2016 /* skip the right number of entries */
2017 for (i
= 0; i
< old_pos
- 2; i
++)
2019 if (!readdir( dir
))
2022 io
->u
.Status
= STATUS_NO_MORE_FILES
;
2027 io
->u
.Status
= STATUS_SUCCESS
;
2032 info
= append_entry( buffer
, io
, length
, ".", NULL
, mask
, class );
2033 else if (old_pos
== 1)
2034 info
= append_entry( buffer
, io
, length
, "..", NULL
, mask
, class );
2035 else if ((de
= readdir( dir
)))
2037 if (strcmp( de
->d_name
, "." ) && strcmp( de
->d_name
, ".." ))
2038 info
= append_entry( buffer
, io
, length
, de
->d_name
, NULL
, mask
, class );
2048 if (io
->u
.Status
== STATUS_BUFFER_OVERFLOW
)
2050 old_pos
--; /* restore pos to previous entry */
2053 if (single_entry
) break;
2054 /* check if we still have enough space for the largest possible entry */
2055 if (io
->Information
+ max_dir_info_size(class) > length
) break;
2059 lseek( fd
, old_pos
, SEEK_SET
); /* store dir offset as filepos for fd */
2062 if (last_info
) last_info
->next
= 0;
2063 else io
->u
.Status
= restart_scan
? STATUS_NO_SUCH_FILE
: STATUS_NO_MORE_FILES
;
2066 /***********************************************************************
2067 * read_directory_stat
2069 * Read a single file from a directory by determining whether the file
2070 * identified by mask exists using stat.
2072 static int read_directory_stat( int fd
, IO_STATUS_BLOCK
*io
, void *buffer
, ULONG length
,
2073 BOOLEAN single_entry
, const UNICODE_STRING
*mask
,
2074 BOOLEAN restart_scan
, FILE_INFORMATION_CLASS
class )
2076 int unix_len
, ret
, used_default
;
2079 BOOL case_sensitive
= get_dir_case_sensitivity(".");
2081 TRACE("looking up file %s\n", debugstr_us( mask
));
2083 unix_len
= ntdll_wcstoumbs( 0, mask
->Buffer
, mask
->Length
/ sizeof(WCHAR
), NULL
, 0, NULL
, NULL
);
2084 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, unix_len
+ 1)))
2086 io
->u
.Status
= STATUS_NO_MEMORY
;
2089 ret
= ntdll_wcstoumbs( 0, mask
->Buffer
, mask
->Length
/ sizeof(WCHAR
), unix_name
, unix_len
,
2090 NULL
, &used_default
);
2091 if (ret
> 0 && !used_default
)
2096 lseek( fd
, 0, SEEK_SET
);
2098 else if (lseek( fd
, 0, SEEK_CUR
) != 0)
2100 io
->u
.Status
= STATUS_NO_MORE_FILES
;
2105 ret
= stat( unix_name
, &st
);
2106 if (case_sensitive
&& !ret
)
2108 union file_directory_info
*info
= append_entry( buffer
, io
, length
, unix_name
, NULL
, NULL
, class );
2112 if (io
->u
.Status
!= STATUS_BUFFER_OVERFLOW
) lseek( fd
, 1, SEEK_CUR
);
2114 else io
->u
.Status
= restart_scan
? STATUS_NO_SUCH_FILE
: STATUS_NO_MORE_FILES
;
2124 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2126 TRACE("returning %d\n", ret
);
2131 #ifdef HAVE_GETATTRLIST
2132 /***********************************************************************
2133 * read_directory_getattrlist
2135 * Read a single file from a directory by determining whether the file
2136 * identified by mask exists using getattrlist.
2138 static int read_directory_getattrlist( int fd
, IO_STATUS_BLOCK
*io
, void *buffer
, ULONG length
,
2139 BOOLEAN single_entry
, const UNICODE_STRING
*mask
,
2140 BOOLEAN restart_scan
, FILE_INFORMATION_CLASS
class )
2142 int unix_len
, ret
, used_default
;
2144 struct attrlist attrlist
;
2145 #include "pshpack4.h"
2149 struct attrreference name_reference
;
2151 char name
[NAME_MAX
* 3 + 1];
2153 #include "poppack.h"
2155 TRACE("looking up file %s\n", debugstr_us( mask
));
2157 unix_len
= ntdll_wcstoumbs( 0, mask
->Buffer
, mask
->Length
/ sizeof(WCHAR
), NULL
, 0, NULL
, NULL
);
2158 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, unix_len
+ 1)))
2160 io
->u
.Status
= STATUS_NO_MEMORY
;
2163 ret
= ntdll_wcstoumbs( 0, mask
->Buffer
, mask
->Length
/ sizeof(WCHAR
), unix_name
, unix_len
,
2164 NULL
, &used_default
);
2165 if (ret
> 0 && !used_default
)
2170 lseek( fd
, 0, SEEK_SET
);
2172 else if (lseek( fd
, 0, SEEK_CUR
) != 0)
2174 io
->u
.Status
= STATUS_NO_MORE_FILES
;
2179 memset( &attrlist
, 0, sizeof(attrlist
) );
2180 attrlist
.bitmapcount
= ATTR_BIT_MAP_COUNT
;
2181 attrlist
.commonattr
= ATTR_CMN_NAME
| ATTR_CMN_OBJTYPE
;
2182 ret
= getattrlist( unix_name
, &attrlist
, &attrlist_buffer
, sizeof(attrlist_buffer
), FSOPT_NOFOLLOW
);
2183 /* If unix_name named a symlink, the above may have succeeded even if the symlink is broken.
2184 Check that with another call without FSOPT_NOFOLLOW. We don't ask for any attributes. */
2185 if (!ret
&& attrlist_buffer
.type
== VLNK
)
2188 attrlist
.commonattr
= 0;
2189 ret
= getattrlist( unix_name
, &attrlist
, &dummy
, sizeof(dummy
), 0 );
2193 union file_directory_info
*info
= append_entry( buffer
, io
, length
, attrlist_buffer
.name
, NULL
, NULL
, class );
2197 if (io
->u
.Status
!= STATUS_BUFFER_OVERFLOW
) lseek( fd
, 1, SEEK_CUR
);
2199 else io
->u
.Status
= restart_scan
? STATUS_NO_SUCH_FILE
: STATUS_NO_MORE_FILES
;
2205 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2207 TRACE("returning %d\n", ret
);
2214 /******************************************************************************
2215 * NtQueryDirectoryFile [NTDLL.@]
2216 * ZwQueryDirectoryFile [NTDLL.@]
2218 NTSTATUS WINAPI
NtQueryDirectoryFile( HANDLE handle
, HANDLE event
,
2219 PIO_APC_ROUTINE apc_routine
, PVOID apc_context
,
2220 PIO_STATUS_BLOCK io
,
2221 PVOID buffer
, ULONG length
,
2222 FILE_INFORMATION_CLASS info_class
,
2223 BOOLEAN single_entry
,
2224 PUNICODE_STRING mask
,
2225 BOOLEAN restart_scan
)
2227 int cwd
, fd
, needs_close
;
2229 TRACE("(%p %p %p %p %p %p 0x%08x 0x%08x 0x%08x %s 0x%08x\n",
2230 handle
, event
, apc_routine
, apc_context
, io
, buffer
,
2231 length
, info_class
, single_entry
, debugstr_us(mask
),
2234 if (event
|| apc_routine
)
2236 FIXME( "Unsupported yet option\n" );
2237 return io
->u
.Status
= STATUS_NOT_IMPLEMENTED
;
2241 case FileDirectoryInformation
:
2242 case FileBothDirectoryInformation
:
2243 case FileFullDirectoryInformation
:
2244 case FileIdBothDirectoryInformation
:
2245 case FileIdFullDirectoryInformation
:
2246 if (length
< dir_info_size( info_class
, 1 )) return io
->u
.Status
= STATUS_INFO_LENGTH_MISMATCH
;
2247 if (!buffer
) return io
->u
.Status
= STATUS_ACCESS_VIOLATION
;
2250 FIXME( "Unsupported file info class %d\n", info_class
);
2251 return io
->u
.Status
= STATUS_NOT_IMPLEMENTED
;
2254 if ((io
->u
.Status
= server_get_unix_fd( handle
, FILE_LIST_DIRECTORY
, &fd
, &needs_close
, NULL
, NULL
)) != STATUS_SUCCESS
)
2255 return io
->u
.Status
;
2257 io
->Information
= 0;
2259 RtlRunOnceExecuteOnce( &init_once
, init_options
, NULL
, NULL
);
2261 RtlEnterCriticalSection( &dir_section
);
2263 cwd
= open( ".", O_RDONLY
);
2264 if (fchdir( fd
) != -1)
2268 curdir
.dev
= st
.st_dev
;
2269 curdir
.ino
= st
.st_ino
;
2270 #ifdef VFAT_IOCTL_READDIR_BOTH
2271 if ((read_directory_vfat( fd
, io
, buffer
, length
, single_entry
,
2272 mask
, restart_scan
, info_class
)) != -1) goto done
;
2274 if (!has_wildcard( mask
))
2276 #ifdef HAVE_GETATTRLIST
2277 if (read_directory_getattrlist( fd
, io
, buffer
, length
, single_entry
,
2278 mask
, restart_scan
, info_class
) != -1) goto done
;
2280 if (read_directory_stat( fd
, io
, buffer
, length
, single_entry
,
2281 mask
, restart_scan
, info_class
) != -1) goto done
;
2284 if ((read_directory_getdents( fd
, io
, buffer
, length
, single_entry
,
2285 mask
, restart_scan
, info_class
)) != -1) goto done
;
2286 #elif defined HAVE_GETDIRENTRIES
2287 if ((read_directory_getdirentries( fd
, io
, buffer
, length
, single_entry
,
2288 mask
, restart_scan
, info_class
)) != -1) goto done
;
2290 read_directory_readdir( fd
, io
, buffer
, length
, single_entry
, mask
, restart_scan
, info_class
);
2293 if (cwd
== -1 || fchdir( cwd
) == -1) chdir( "/" );
2295 else io
->u
.Status
= FILE_GetNtStatus();
2297 RtlLeaveCriticalSection( &dir_section
);
2299 if (needs_close
) close( fd
);
2300 if (cwd
!= -1) close( cwd
);
2301 TRACE( "=> %x (%ld)\n", io
->u
.Status
, io
->Information
);
2302 return io
->u
.Status
;
2306 /***********************************************************************
2309 * Find a file in a directory the hard way, by doing a case-insensitive search.
2310 * The file found is appended to unix_name at pos.
2311 * There must be at least MAX_DIR_ENTRY_LEN+2 chars available at pos.
2313 static NTSTATUS
find_file_in_dir( char *unix_name
, int pos
, const WCHAR
*name
, int length
,
2314 BOOLEAN check_case
, BOOLEAN
*is_win_dir
)
2316 WCHAR buffer
[MAX_DIR_ENTRY_LEN
];
2318 BOOLEAN spaces
, is_name_8_dot_3
;
2322 int ret
, used_default
;
2324 /* try a shortcut for this directory */
2326 unix_name
[pos
++] = '/';
2327 ret
= ntdll_wcstoumbs( 0, name
, length
, unix_name
+ pos
, MAX_DIR_ENTRY_LEN
,
2328 NULL
, &used_default
);
2329 /* if we used the default char, the Unix name won't round trip properly back to Unicode */
2330 /* so it cannot match the file we are looking for */
2331 if (ret
>= 0 && !used_default
)
2333 unix_name
[pos
+ ret
] = 0;
2334 if (!stat( unix_name
, &st
))
2336 if (is_win_dir
) *is_win_dir
= is_same_file( &windir
, &st
);
2337 return STATUS_SUCCESS
;
2340 if (check_case
) goto not_found
; /* we want an exact match */
2342 if (pos
> 1) unix_name
[pos
- 1] = 0;
2343 else unix_name
[1] = 0; /* keep the initial slash */
2345 /* check if it fits in 8.3 so that we don't look for short names if we won't need them */
2347 str
.Buffer
= (WCHAR
*)name
;
2348 str
.Length
= length
* sizeof(WCHAR
);
2349 str
.MaximumLength
= str
.Length
;
2350 is_name_8_dot_3
= RtlIsNameLegalDOS8Dot3( &str
, NULL
, &spaces
) && !spaces
;
2351 #ifndef VFAT_IOCTL_READDIR_BOTH
2352 is_name_8_dot_3
= is_name_8_dot_3
&& length
>= 8 && name
[4] == '~';
2355 if (!is_name_8_dot_3
&& !get_dir_case_sensitivity( unix_name
)) goto not_found
;
2357 /* now look for it through the directory */
2359 #ifdef VFAT_IOCTL_READDIR_BOTH
2360 if (is_name_8_dot_3
)
2362 int fd
= open( unix_name
, O_RDONLY
| O_DIRECTORY
);
2367 RtlEnterCriticalSection( &dir_section
);
2368 if ((kde
= start_vfat_ioctl( fd
)))
2370 unix_name
[pos
- 1] = '/';
2371 while (kde
[0].d_reclen
)
2373 /* make sure names are null-terminated to work around an x86-64 kernel bug */
2374 size_t len
= min(kde
[0].d_reclen
, sizeof(kde
[0].d_name
) - 1 );
2375 kde
[0].d_name
[len
] = 0;
2376 len
= min(kde
[1].d_reclen
, sizeof(kde
[1].d_name
) - 1 );
2377 kde
[1].d_name
[len
] = 0;
2379 if (kde
[1].d_name
[0])
2381 ret
= ntdll_umbstowcs( 0, kde
[1].d_name
, strlen(kde
[1].d_name
),
2382 buffer
, MAX_DIR_ENTRY_LEN
);
2383 if (ret
== length
&& !memicmpW( buffer
, name
, length
))
2385 strcpy( unix_name
+ pos
, kde
[1].d_name
);
2386 RtlLeaveCriticalSection( &dir_section
);
2391 ret
= ntdll_umbstowcs( 0, kde
[0].d_name
, strlen(kde
[0].d_name
),
2392 buffer
, MAX_DIR_ENTRY_LEN
);
2393 if (ret
== length
&& !memicmpW( buffer
, name
, length
))
2395 strcpy( unix_name
+ pos
,
2396 kde
[1].d_name
[0] ? kde
[1].d_name
: kde
[0].d_name
);
2397 RtlLeaveCriticalSection( &dir_section
);
2401 if (ioctl( fd
, VFAT_IOCTL_READDIR_BOTH
, (long)kde
) == -1)
2403 RtlLeaveCriticalSection( &dir_section
);
2409 RtlLeaveCriticalSection( &dir_section
);
2412 /* fall through to normal handling */
2414 #endif /* VFAT_IOCTL_READDIR_BOTH */
2416 if (!(dir
= opendir( unix_name
)))
2418 if (errno
== ENOENT
) return STATUS_OBJECT_PATH_NOT_FOUND
;
2419 else return FILE_GetNtStatus();
2421 unix_name
[pos
- 1] = '/';
2422 str
.Buffer
= buffer
;
2423 str
.MaximumLength
= sizeof(buffer
);
2424 while ((de
= readdir( dir
)))
2426 ret
= ntdll_umbstowcs( 0, de
->d_name
, strlen(de
->d_name
), buffer
, MAX_DIR_ENTRY_LEN
);
2427 if (ret
== length
&& !memicmpW( buffer
, name
, length
))
2429 strcpy( unix_name
+ pos
, de
->d_name
);
2434 if (!is_name_8_dot_3
) continue;
2436 str
.Length
= ret
* sizeof(WCHAR
);
2437 if (!RtlIsNameLegalDOS8Dot3( &str
, NULL
, &spaces
) || spaces
)
2439 WCHAR short_nameW
[12];
2440 ret
= hash_short_file_name( &str
, short_nameW
);
2441 if (ret
== length
&& !memicmpW( short_nameW
, name
, length
))
2443 strcpy( unix_name
+ pos
, de
->d_name
);
2452 unix_name
[pos
- 1] = 0;
2453 return STATUS_OBJECT_PATH_NOT_FOUND
;
2456 if (is_win_dir
&& !stat( unix_name
, &st
)) *is_win_dir
= is_same_file( &windir
, &st
);
2457 return STATUS_SUCCESS
;
2463 static const WCHAR catrootW
[] = {'s','y','s','t','e','m','3','2','\\','c','a','t','r','o','o','t',0};
2464 static const WCHAR catroot2W
[] = {'s','y','s','t','e','m','3','2','\\','c','a','t','r','o','o','t','2',0};
2465 static const WCHAR driversstoreW
[] = {'s','y','s','t','e','m','3','2','\\','d','r','i','v','e','r','s','s','t','o','r','e',0};
2466 static const WCHAR driversetcW
[] = {'s','y','s','t','e','m','3','2','\\','d','r','i','v','e','r','s','\\','e','t','c',0};
2467 static const WCHAR logfilesW
[] = {'s','y','s','t','e','m','3','2','\\','l','o','g','f','i','l','e','s',0};
2468 static const WCHAR spoolW
[] = {'s','y','s','t','e','m','3','2','\\','s','p','o','o','l',0};
2469 static const WCHAR system32W
[] = {'s','y','s','t','e','m','3','2',0};
2470 static const WCHAR syswow64W
[] = {'s','y','s','w','o','w','6','4',0};
2471 static const WCHAR sysnativeW
[] = {'s','y','s','n','a','t','i','v','e',0};
2472 static const WCHAR regeditW
[] = {'r','e','g','e','d','i','t','.','e','x','e',0};
2473 static const WCHAR wow_regeditW
[] = {'s','y','s','w','o','w','6','4','\\','r','e','g','e','d','i','t','.','e','x','e',0};
2477 const WCHAR
*source
;
2478 const WCHAR
*dos_target
;
2479 const char *unix_target
;
2482 { catrootW
, NULL
, NULL
},
2483 { catroot2W
, NULL
, NULL
},
2484 { driversstoreW
, NULL
, NULL
},
2485 { driversetcW
, NULL
, NULL
},
2486 { logfilesW
, NULL
, NULL
},
2487 { spoolW
, NULL
, NULL
},
2488 { system32W
, syswow64W
, NULL
},
2489 { sysnativeW
, system32W
, NULL
},
2490 { regeditW
, wow_regeditW
, NULL
}
2493 static unsigned int nb_redirects
;
2496 /***********************************************************************
2497 * get_redirect_target
2499 * Find the target unix name for a redirected dir.
2501 static const char *get_redirect_target( const char *windows_dir
, const WCHAR
*name
)
2503 int used_default
, len
, pos
, win_len
= strlen( windows_dir
);
2504 char *unix_name
, *unix_target
= NULL
;
2507 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, win_len
+ MAX_DIR_ENTRY_LEN
+ 2 )))
2509 memcpy( unix_name
, windows_dir
, win_len
);
2514 const WCHAR
*end
, *next
;
2516 for (end
= name
; *end
; end
++) if (IS_SEPARATOR(*end
)) break;
2517 for (next
= end
; *next
; next
++) if (!IS_SEPARATOR(*next
)) break;
2519 status
= find_file_in_dir( unix_name
, pos
, name
, end
- name
, FALSE
, NULL
);
2520 if (status
== STATUS_OBJECT_PATH_NOT_FOUND
&& !*next
) /* not finding last element is ok */
2522 len
= ntdll_wcstoumbs( 0, name
, end
- name
, unix_name
+ pos
+ 1,
2523 MAX_DIR_ENTRY_LEN
- (pos
- win_len
), NULL
, &used_default
);
2524 if (len
> 0 && !used_default
)
2526 unix_name
[pos
] = '/';
2532 if (status
) goto done
;
2533 pos
+= strlen( unix_name
+ pos
);
2537 if ((unix_target
= RtlAllocateHeap( GetProcessHeap(), 0, pos
- win_len
)))
2538 memcpy( unix_target
, unix_name
+ win_len
+ 1, pos
- win_len
);
2541 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2546 /***********************************************************************
2549 static void init_redirects(void)
2551 UNICODE_STRING nt_name
;
2552 ANSI_STRING unix_name
;
2557 if (!RtlDosPathNameToNtPathName_U( user_shared_data
->NtSystemRoot
, &nt_name
, NULL
, NULL
))
2559 ERR( "can't convert %s\n", debugstr_w(user_shared_data
->NtSystemRoot
) );
2562 status
= wine_nt_to_unix_file_name( &nt_name
, &unix_name
, FILE_OPEN_IF
, FALSE
);
2563 RtlFreeUnicodeString( &nt_name
);
2566 ERR( "cannot open %s (%x)\n", debugstr_w(user_shared_data
->NtSystemRoot
), status
);
2569 if (!stat( unix_name
.Buffer
, &st
))
2571 windir
.dev
= st
.st_dev
;
2572 windir
.ino
= st
.st_ino
;
2573 nb_redirects
= sizeof(redirects
) / sizeof(redirects
[0]);
2574 for (i
= 0; i
< nb_redirects
; i
++)
2576 if (!redirects
[i
].dos_target
) continue;
2577 redirects
[i
].unix_target
= get_redirect_target( unix_name
.Buffer
, redirects
[i
].dos_target
);
2578 TRACE( "%s -> %s\n", debugstr_w(redirects
[i
].source
), redirects
[i
].unix_target
);
2581 RtlFreeAnsiString( &unix_name
);
2586 /***********************************************************************
2589 * Check if path matches a redirect name. If yes, return matched length.
2591 static int match_redirect( const WCHAR
*path
, int len
, const WCHAR
*redir
, BOOLEAN check_case
)
2595 while (i
< len
&& *redir
)
2597 if (IS_SEPARATOR(path
[i
]))
2599 if (*redir
++ != '\\') return 0;
2600 while (i
< len
&& IS_SEPARATOR(path
[i
])) i
++;
2601 continue; /* move on to next path component */
2603 else if (check_case
)
2605 if (path
[i
] != *redir
) return 0;
2609 if (tolowerW(path
[i
]) != tolowerW(*redir
)) return 0;
2614 if (*redir
) return 0;
2615 if (i
< len
&& !IS_SEPARATOR(path
[i
])) return 0;
2616 while (i
< len
&& IS_SEPARATOR(path
[i
])) i
++;
2621 /***********************************************************************
2624 * Retrieve the Unix path corresponding to a redirected path if any.
2626 static int get_redirect_path( char *unix_name
, int pos
, const WCHAR
*name
, int length
, BOOLEAN check_case
)
2631 for (i
= 0; i
< nb_redirects
; i
++)
2633 if ((len
= match_redirect( name
, length
, redirects
[i
].source
, check_case
)))
2635 if (!redirects
[i
].unix_target
) break;
2636 unix_name
[pos
++] = '/';
2637 strcpy( unix_name
+ pos
, redirects
[i
].unix_target
);
2646 /* there are no redirects on 64-bit */
2648 static const unsigned int nb_redirects
= 0;
2650 static int get_redirect_path( char *unix_name
, int pos
, const WCHAR
*name
, int length
, BOOLEAN check_case
)
2657 /***********************************************************************
2658 * DIR_init_windows_dir
2660 void DIR_init_windows_dir( const WCHAR
*win
, const WCHAR
*sys
)
2662 /* FIXME: should probably store paths as NT file names */
2664 RtlCreateUnicodeString( &system_dir
, sys
);
2667 if (is_wow64
) init_redirects();
2672 /******************************************************************************
2675 * Get the Unix path of a DOS device.
2677 static NTSTATUS
get_dos_device( const WCHAR
*name
, UINT name_len
, ANSI_STRING
*unix_name_ret
)
2679 const char *config_dir
= wine_get_config_dir();
2681 char *unix_name
, *new_name
, *dev
;
2685 /* make sure the device name is ASCII */
2686 for (i
= 0; i
< name_len
; i
++)
2687 if (name
[i
] <= 32 || name
[i
] >= 127) return STATUS_BAD_DEVICE_TYPE
;
2689 unix_len
= strlen(config_dir
) + sizeof("/dosdevices/") + name_len
+ 1;
2691 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, unix_len
)))
2692 return STATUS_NO_MEMORY
;
2694 strcpy( unix_name
, config_dir
);
2695 strcat( unix_name
, "/dosdevices/" );
2696 dev
= unix_name
+ strlen(unix_name
);
2698 for (i
= 0; i
< name_len
; i
++) dev
[i
] = (char)tolowerW(name
[i
]);
2701 /* special case for drive devices */
2702 if (name_len
== 2 && dev
[1] == ':')
2710 if (!stat( unix_name
, &st
))
2712 TRACE( "%s -> %s\n", debugstr_wn(name
,name_len
), debugstr_a(unix_name
) );
2713 unix_name_ret
->Buffer
= unix_name
;
2714 unix_name_ret
->Length
= strlen(unix_name
);
2715 unix_name_ret
->MaximumLength
= unix_len
;
2716 return STATUS_SUCCESS
;
2720 /* now try some defaults for it */
2721 if (!strcmp( dev
, "aux" ))
2723 strcpy( dev
, "com1" );
2726 if (!strcmp( dev
, "prn" ))
2728 strcpy( dev
, "lpt1" );
2733 if (dev
[1] == ':' && dev
[2] == ':') /* drive device */
2735 dev
[2] = 0; /* remove last ':' to get the drive mount point symlink */
2736 new_name
= get_default_drive_device( unix_name
);
2738 else if (!strncmp( dev
, "com", 3 )) new_name
= get_default_com_device( atoi(dev
+ 3 ));
2739 else if (!strncmp( dev
, "lpt", 3 )) new_name
= get_default_lpt_device( atoi(dev
+ 3 ));
2741 if (!new_name
) break;
2743 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2744 unix_name
= new_name
;
2745 unix_len
= strlen(unix_name
) + 1;
2746 dev
= NULL
; /* last try */
2748 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2749 return STATUS_BAD_DEVICE_TYPE
;
2753 /* return the length of the DOS namespace prefix if any */
2754 static inline int get_dos_prefix_len( const UNICODE_STRING
*name
)
2756 static const WCHAR nt_prefixW
[] = {'\\','?','?','\\'};
2757 static const WCHAR dosdev_prefixW
[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\'};
2759 if (name
->Length
>= sizeof(nt_prefixW
) &&
2760 !memcmp( name
->Buffer
, nt_prefixW
, sizeof(nt_prefixW
) ))
2761 return sizeof(nt_prefixW
) / sizeof(WCHAR
);
2763 if (name
->Length
>= sizeof(dosdev_prefixW
) &&
2764 !memicmpW( name
->Buffer
, dosdev_prefixW
, sizeof(dosdev_prefixW
)/sizeof(WCHAR
) ))
2765 return sizeof(dosdev_prefixW
) / sizeof(WCHAR
);
2771 /******************************************************************************
2774 * Recursively search directories from the dir queue for a given inode.
2776 static NTSTATUS
find_file_id( ANSI_STRING
*unix_name
, ULONGLONG file_id
, dev_t dev
)
2784 while (!(status
= next_dir_in_queue( unix_name
->Buffer
)))
2786 if (!(dir
= opendir( unix_name
->Buffer
))) continue;
2787 TRACE( "searching %s for %s\n", unix_name
->Buffer
, wine_dbgstr_longlong(file_id
) );
2788 pos
= strlen( unix_name
->Buffer
);
2789 if (pos
+ MAX_DIR_ENTRY_LEN
>= unix_name
->MaximumLength
/sizeof(WCHAR
))
2791 char *new = RtlReAllocateHeap( GetProcessHeap(), 0, unix_name
->Buffer
,
2792 unix_name
->MaximumLength
* 2 );
2796 return STATUS_NO_MEMORY
;
2798 unix_name
->MaximumLength
*= 2;
2799 unix_name
->Buffer
= new;
2801 unix_name
->Buffer
[pos
++] = '/';
2802 while ((de
= readdir( dir
)))
2804 if (!strcmp( de
->d_name
, "." ) || !strcmp( de
->d_name
, ".." )) continue;
2805 strcpy( unix_name
->Buffer
+ pos
, de
->d_name
);
2806 if (lstat( unix_name
->Buffer
, &st
) == -1) continue;
2807 if (st
.st_dev
!= dev
) continue;
2808 if (st
.st_ino
== file_id
)
2811 return STATUS_SUCCESS
;
2813 if (!S_ISDIR( st
.st_mode
)) continue;
2814 if ((status
= add_dir_to_queue( unix_name
->Buffer
)) != STATUS_SUCCESS
)
2826 /******************************************************************************
2827 * file_id_to_unix_file_name
2829 * Lookup a file from its file id instead of its name.
2831 NTSTATUS
file_id_to_unix_file_name( const OBJECT_ATTRIBUTES
*attr
, ANSI_STRING
*unix_name
)
2833 enum server_fd_type type
;
2834 int old_cwd
, root_fd
, needs_close
;
2837 struct stat st
, root_st
;
2839 if (attr
->ObjectName
->Length
!= sizeof(ULONGLONG
)) return STATUS_OBJECT_PATH_SYNTAX_BAD
;
2840 if (!attr
->RootDirectory
) return STATUS_INVALID_PARAMETER
;
2841 memcpy( &file_id
, attr
->ObjectName
->Buffer
, sizeof(file_id
) );
2843 unix_name
->MaximumLength
= 2 * MAX_DIR_ENTRY_LEN
+ 4;
2844 if (!(unix_name
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, unix_name
->MaximumLength
)))
2845 return STATUS_NO_MEMORY
;
2846 strcpy( unix_name
->Buffer
, "." );
2848 if ((status
= server_get_unix_fd( attr
->RootDirectory
, 0, &root_fd
, &needs_close
, &type
, NULL
)))
2851 if (type
!= FD_TYPE_DIR
)
2853 status
= STATUS_OBJECT_TYPE_MISMATCH
;
2857 fstat( root_fd
, &root_st
);
2858 if (root_st
.st_ino
== file_id
) /* shortcut for "." */
2860 status
= STATUS_SUCCESS
;
2864 RtlEnterCriticalSection( &dir_section
);
2865 if ((old_cwd
= open( ".", O_RDONLY
)) != -1 && fchdir( root_fd
) != -1)
2867 /* shortcut for ".." */
2868 if (!stat( "..", &st
) && st
.st_dev
== root_st
.st_dev
&& st
.st_ino
== file_id
)
2870 strcpy( unix_name
->Buffer
, ".." );
2871 status
= STATUS_SUCCESS
;
2875 status
= add_dir_to_queue( "." );
2877 status
= find_file_id( unix_name
, file_id
, root_st
.st_dev
);
2878 if (!status
) /* get rid of "./" prefix */
2879 memmove( unix_name
->Buffer
, unix_name
->Buffer
+ 2, strlen(unix_name
->Buffer
) - 1 );
2882 if (fchdir( old_cwd
) == -1) chdir( "/" );
2884 else status
= FILE_GetNtStatus();
2885 RtlLeaveCriticalSection( &dir_section
);
2886 if (old_cwd
!= -1) close( old_cwd
);
2889 if (status
== STATUS_SUCCESS
)
2891 TRACE( "%s -> %s\n", wine_dbgstr_longlong(file_id
), debugstr_a(unix_name
->Buffer
) );
2892 unix_name
->Length
= strlen( unix_name
->Buffer
);
2896 TRACE( "%s not found in dir %p\n", wine_dbgstr_longlong(file_id
), attr
->RootDirectory
);
2897 RtlFreeHeap( GetProcessHeap(), 0, unix_name
->Buffer
);
2899 if (needs_close
) close( root_fd
);
2904 /******************************************************************************
2907 * Helper for nt_to_unix_file_name
2909 static NTSTATUS
lookup_unix_name( const WCHAR
*name
, int name_len
, char **buffer
, int unix_len
, int pos
,
2910 UINT disposition
, BOOLEAN check_case
)
2913 int ret
, used_default
, len
;
2915 char *unix_name
= *buffer
;
2916 const BOOL redirect
= nb_redirects
&& ntdll_get_thread_data()->wow64_redir
;
2918 /* try a shortcut first */
2920 ret
= ntdll_wcstoumbs( 0, name
, name_len
, unix_name
+ pos
, unix_len
- pos
- 1,
2921 NULL
, &used_default
);
2923 while (name_len
&& IS_SEPARATOR(*name
))
2929 if (ret
>= 0 && !used_default
) /* if we used the default char the name didn't convert properly */
2932 unix_name
[pos
+ ret
] = 0;
2933 for (p
= unix_name
+ pos
; *p
; p
++) if (*p
== '\\') *p
= '/';
2934 if (!redirect
|| (!strstr( unix_name
, "/windows/") && strncmp( unix_name
, "windows/", 8 )))
2936 if (!stat( unix_name
, &st
))
2938 /* creation fails with STATUS_ACCESS_DENIED for the root of the drive */
2939 if (disposition
== FILE_CREATE
)
2940 return name_len
? STATUS_OBJECT_NAME_COLLISION
: STATUS_ACCESS_DENIED
;
2941 return STATUS_SUCCESS
;
2946 if (!name_len
) /* empty name -> drive root doesn't exist */
2947 return STATUS_OBJECT_PATH_NOT_FOUND
;
2948 if (check_case
&& !redirect
&& (disposition
== FILE_OPEN
|| disposition
== FILE_OVERWRITE
))
2949 return STATUS_OBJECT_NAME_NOT_FOUND
;
2951 /* now do it component by component */
2955 const WCHAR
*end
, *next
;
2956 BOOLEAN is_win_dir
= FALSE
;
2959 while (end
< name
+ name_len
&& !IS_SEPARATOR(*end
)) end
++;
2961 while (next
< name
+ name_len
&& IS_SEPARATOR(*next
)) next
++;
2962 name_len
-= next
- name
;
2964 /* grow the buffer if needed */
2966 if (unix_len
- pos
< MAX_DIR_ENTRY_LEN
+ 2)
2969 unix_len
+= 2 * MAX_DIR_ENTRY_LEN
;
2970 if (!(new_name
= RtlReAllocateHeap( GetProcessHeap(), 0, unix_name
, unix_len
)))
2971 return STATUS_NO_MEMORY
;
2972 unix_name
= *buffer
= new_name
;
2975 status
= find_file_in_dir( unix_name
, pos
, name
, end
- name
,
2976 check_case
, redirect
? &is_win_dir
: NULL
);
2978 /* if this is the last element, not finding it is not necessarily fatal */
2981 if (status
== STATUS_OBJECT_PATH_NOT_FOUND
)
2983 status
= STATUS_OBJECT_NAME_NOT_FOUND
;
2984 if (disposition
!= FILE_OPEN
&& disposition
!= FILE_OVERWRITE
)
2986 ret
= ntdll_wcstoumbs( 0, name
, end
- name
, unix_name
+ pos
+ 1,
2987 MAX_DIR_ENTRY_LEN
, NULL
, &used_default
);
2988 if (ret
> 0 && !used_default
)
2990 unix_name
[pos
] = '/';
2991 unix_name
[pos
+ 1 + ret
] = 0;
2992 status
= STATUS_NO_SUCH_FILE
;
2997 else if (status
== STATUS_SUCCESS
&& disposition
== FILE_CREATE
)
2999 status
= STATUS_OBJECT_NAME_COLLISION
;
3003 if (status
!= STATUS_SUCCESS
) break;
3005 pos
+= strlen( unix_name
+ pos
);
3008 if (is_win_dir
&& (len
= get_redirect_path( unix_name
, pos
, name
, name_len
, check_case
)))
3012 pos
+= strlen( unix_name
+ pos
);
3013 TRACE( "redirecting -> %s + %s\n", debugstr_a(unix_name
), debugstr_w(name
) );
3021 /******************************************************************************
3022 * nt_to_unix_file_name_attr
3024 NTSTATUS
nt_to_unix_file_name_attr( const OBJECT_ATTRIBUTES
*attr
, ANSI_STRING
*unix_name_ret
,
3027 static const WCHAR invalid_charsW
[] = { INVALID_NT_CHARS
, 0 };
3028 enum server_fd_type type
;
3029 int old_cwd
, root_fd
, needs_close
;
3030 const WCHAR
*name
, *p
;
3032 int name_len
, unix_len
;
3034 BOOLEAN check_case
= !(attr
->Attributes
& OBJ_CASE_INSENSITIVE
);
3036 if (!attr
->RootDirectory
) /* without root dir fall back to normal lookup */
3037 return wine_nt_to_unix_file_name( attr
->ObjectName
, unix_name_ret
, disposition
, check_case
);
3039 name
= attr
->ObjectName
->Buffer
;
3040 name_len
= attr
->ObjectName
->Length
/ sizeof(WCHAR
);
3042 if (name_len
&& IS_SEPARATOR(name
[0])) return STATUS_INVALID_PARAMETER
;
3044 /* check for invalid characters */
3045 for (p
= name
; p
< name
+ name_len
; p
++)
3046 if (*p
< 32 || strchrW( invalid_charsW
, *p
)) return STATUS_OBJECT_NAME_INVALID
;
3048 unix_len
= ntdll_wcstoumbs( 0, name
, name_len
, NULL
, 0, NULL
, NULL
);
3049 unix_len
+= MAX_DIR_ENTRY_LEN
+ 3;
3050 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, unix_len
)))
3051 return STATUS_NO_MEMORY
;
3054 if (!(status
= server_get_unix_fd( attr
->RootDirectory
, 0, &root_fd
, &needs_close
, &type
, NULL
)))
3056 if (type
!= FD_TYPE_DIR
)
3058 if (needs_close
) close( root_fd
);
3059 status
= STATUS_BAD_DEVICE_TYPE
;
3063 RtlEnterCriticalSection( &dir_section
);
3064 if ((old_cwd
= open( ".", O_RDONLY
)) != -1 && fchdir( root_fd
) != -1)
3066 status
= lookup_unix_name( name
, name_len
, &unix_name
, unix_len
, 1,
3067 disposition
, check_case
);
3068 if (fchdir( old_cwd
) == -1) chdir( "/" );
3070 else status
= FILE_GetNtStatus();
3071 RtlLeaveCriticalSection( &dir_section
);
3072 if (old_cwd
!= -1) close( old_cwd
);
3073 if (needs_close
) close( root_fd
);
3076 else if (status
== STATUS_OBJECT_TYPE_MISMATCH
) status
= STATUS_BAD_DEVICE_TYPE
;
3078 if (status
== STATUS_SUCCESS
|| status
== STATUS_NO_SUCH_FILE
)
3080 TRACE( "%s -> %s\n", debugstr_us(attr
->ObjectName
), debugstr_a(unix_name
) );
3081 unix_name_ret
->Buffer
= unix_name
;
3082 unix_name_ret
->Length
= strlen(unix_name
);
3083 unix_name_ret
->MaximumLength
= unix_len
;
3087 TRACE( "%s not found in %s\n", debugstr_w(name
), unix_name
);
3088 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
3094 /******************************************************************************
3095 * wine_nt_to_unix_file_name (NTDLL.@) Not a Windows API
3097 * Convert a file name from NT namespace to Unix namespace.
3099 * If disposition is not FILE_OPEN or FILE_OVERWRITE, the last path
3100 * element doesn't have to exist; in that case STATUS_NO_SUCH_FILE is
3101 * returned, but the unix name is still filled in properly.
3103 NTSTATUS CDECL
wine_nt_to_unix_file_name( const UNICODE_STRING
*nameW
, ANSI_STRING
*unix_name_ret
,
3104 UINT disposition
, BOOLEAN check_case
)
3106 static const WCHAR unixW
[] = {'u','n','i','x'};
3107 static const WCHAR invalid_charsW
[] = { INVALID_NT_CHARS
, 0 };
3109 NTSTATUS status
= STATUS_SUCCESS
;
3110 const char *config_dir
= wine_get_config_dir();
3111 const WCHAR
*name
, *p
;
3114 int pos
, ret
, name_len
, unix_len
, prefix_len
, used_default
;
3115 WCHAR prefix
[MAX_DIR_ENTRY_LEN
];
3116 BOOLEAN is_unix
= FALSE
;
3118 name
= nameW
->Buffer
;
3119 name_len
= nameW
->Length
/ sizeof(WCHAR
);
3121 if (!name_len
|| !IS_SEPARATOR(name
[0])) return STATUS_OBJECT_PATH_SYNTAX_BAD
;
3123 if (!(pos
= get_dos_prefix_len( nameW
)))
3124 return STATUS_BAD_DEVICE_TYPE
; /* no DOS prefix, assume NT native name */
3129 if (!name_len
) return STATUS_OBJECT_NAME_INVALID
;
3131 /* check for sub-directory */
3132 for (pos
= 0; pos
< name_len
; pos
++)
3134 if (IS_SEPARATOR(name
[pos
])) break;
3135 if (name
[pos
] < 32 || strchrW( invalid_charsW
, name
[pos
] ))
3136 return STATUS_OBJECT_NAME_INVALID
;
3138 if (pos
> MAX_DIR_ENTRY_LEN
)
3139 return STATUS_OBJECT_NAME_INVALID
;
3141 if (pos
== name_len
) /* no subdir, plain DOS device */
3142 return get_dos_device( name
, name_len
, unix_name_ret
);
3144 for (prefix_len
= 0; prefix_len
< pos
; prefix_len
++)
3145 prefix
[prefix_len
] = tolowerW(name
[prefix_len
]);
3148 name_len
-= prefix_len
;
3150 /* check for invalid characters (all chars except 0 are valid for unix) */
3151 is_unix
= (prefix_len
== 4 && !memcmp( prefix
, unixW
, sizeof(unixW
) ));
3154 for (p
= name
; p
< name
+ name_len
; p
++)
3155 if (!*p
) return STATUS_OBJECT_NAME_INVALID
;
3160 for (p
= name
; p
< name
+ name_len
; p
++)
3161 if (*p
< 32 || strchrW( invalid_charsW
, *p
)) return STATUS_OBJECT_NAME_INVALID
;
3164 unix_len
= ntdll_wcstoumbs( 0, prefix
, prefix_len
, NULL
, 0, NULL
, NULL
);
3165 unix_len
+= ntdll_wcstoumbs( 0, name
, name_len
, NULL
, 0, NULL
, NULL
);
3166 unix_len
+= MAX_DIR_ENTRY_LEN
+ 3;
3167 unix_len
+= strlen(config_dir
) + sizeof("/dosdevices/");
3168 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, unix_len
)))
3169 return STATUS_NO_MEMORY
;
3170 strcpy( unix_name
, config_dir
);
3171 strcat( unix_name
, "/dosdevices/" );
3172 pos
= strlen(unix_name
);
3174 ret
= ntdll_wcstoumbs( 0, prefix
, prefix_len
, unix_name
+ pos
, unix_len
- pos
- 1,
3175 NULL
, &used_default
);
3176 if (!ret
|| used_default
)
3178 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
3179 return STATUS_OBJECT_NAME_INVALID
;
3183 /* check if prefix exists (except for DOS drives to avoid extra stat calls) */
3185 if (prefix_len
!= 2 || prefix
[1] != ':')
3188 if (lstat( unix_name
, &st
) == -1 && errno
== ENOENT
)
3192 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
3193 return STATUS_BAD_DEVICE_TYPE
;
3195 pos
= 0; /* fall back to unix root */
3199 status
= lookup_unix_name( name
, name_len
, &unix_name
, unix_len
, pos
, disposition
, check_case
);
3200 if (status
== STATUS_SUCCESS
|| status
== STATUS_NO_SUCH_FILE
)
3202 TRACE( "%s -> %s\n", debugstr_us(nameW
), debugstr_a(unix_name
) );
3203 unix_name_ret
->Buffer
= unix_name
;
3204 unix_name_ret
->Length
= strlen(unix_name
);
3205 unix_name_ret
->MaximumLength
= unix_len
;
3209 TRACE( "%s not found in %s\n", debugstr_w(name
), unix_name
);
3210 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
3216 /******************************************************************
3217 * RtlWow64EnableFsRedirection (NTDLL.@)
3219 NTSTATUS WINAPI
RtlWow64EnableFsRedirection( BOOLEAN enable
)
3221 if (!is_wow64
) return STATUS_NOT_IMPLEMENTED
;
3222 ntdll_get_thread_data()->wow64_redir
= enable
;
3223 return STATUS_SUCCESS
;
3227 /******************************************************************
3228 * RtlWow64EnableFsRedirectionEx (NTDLL.@)
3230 NTSTATUS WINAPI
RtlWow64EnableFsRedirectionEx( ULONG disable
, ULONG
*old_value
)
3232 if (!is_wow64
) return STATUS_NOT_IMPLEMENTED
;
3233 if (((ULONG_PTR
)old_value
>> 16) == 0) return STATUS_ACCESS_VIOLATION
;
3235 *old_value
= !ntdll_get_thread_data()->wow64_redir
;
3236 ntdll_get_thread_data()->wow64_redir
= !disable
;
3237 return STATUS_SUCCESS
;
3241 /******************************************************************
3242 * RtlDoesFileExists_U (NTDLL.@)
3244 BOOLEAN WINAPI
RtlDoesFileExists_U(LPCWSTR file_name
)
3246 UNICODE_STRING nt_name
;
3247 FILE_BASIC_INFORMATION basic_info
;
3248 OBJECT_ATTRIBUTES attr
;
3251 if (!RtlDosPathNameToNtPathName_U( file_name
, &nt_name
, NULL
, NULL
)) return FALSE
;
3253 attr
.Length
= sizeof(attr
);
3254 attr
.RootDirectory
= 0;
3255 attr
.ObjectName
= &nt_name
;
3256 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
3257 attr
.SecurityDescriptor
= NULL
;
3258 attr
.SecurityQualityOfService
= NULL
;
3260 ret
= NtQueryAttributesFile(&attr
, &basic_info
) == STATUS_SUCCESS
;
3262 RtlFreeUnicodeString( &nt_name
);
3267 /***********************************************************************
3268 * DIR_unmount_device
3270 * Unmount the specified device.
3272 NTSTATUS
DIR_unmount_device( HANDLE handle
)
3275 int unix_fd
, needs_close
;
3277 if (!(status
= server_get_unix_fd( handle
, 0, &unix_fd
, &needs_close
, NULL
, NULL
)))
3280 char *mount_point
= NULL
;
3282 if (fstat( unix_fd
, &st
) == -1 || !is_valid_mounted_device( &st
))
3283 status
= STATUS_INVALID_PARAMETER
;
3286 if ((mount_point
= get_device_mount_point( st
.st_rdev
)))
3289 static const char umount
[] = "diskutil unmount >/dev/null 2>&1 ";
3291 static const char umount
[] = "umount >/dev/null 2>&1 ";
3293 char *cmd
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(mount_point
)+sizeof(umount
));
3296 strcpy( cmd
, umount
);
3297 strcat( cmd
, mount_point
);
3299 RtlFreeHeap( GetProcessHeap(), 0, cmd
);
3301 /* umount will fail to release the loop device since we still have
3302 a handle to it, so we release it here */
3303 if (major(st
.st_rdev
) == LOOP_MAJOR
) ioctl( unix_fd
, 0x4c01 /*LOOP_CLR_FD*/, 0 );
3306 RtlFreeHeap( GetProcessHeap(), 0, mount_point
);
3309 if (needs_close
) close( unix_fd
);
3315 /******************************************************************************
3318 * Retrieve the Unix name of the current directory; helper for wine_unix_to_nt_file_name.
3319 * Returned value must be freed by caller.
3321 NTSTATUS
DIR_get_unix_cwd( char **cwd
)
3323 int old_cwd
, unix_fd
, needs_close
;
3328 RtlAcquirePebLock();
3330 if (NtCurrentTeb()->Tib
.SubSystemTib
) /* FIXME: hack */
3331 curdir
= &((WIN16_SUBSYSTEM_TIB
*)NtCurrentTeb()->Tib
.SubSystemTib
)->curdir
;
3333 curdir
= &NtCurrentTeb()->Peb
->ProcessParameters
->CurrentDirectory
;
3335 if (!(handle
= curdir
->Handle
))
3337 UNICODE_STRING dirW
;
3338 OBJECT_ATTRIBUTES attr
;
3341 if (!RtlDosPathNameToNtPathName_U( curdir
->DosPath
.Buffer
, &dirW
, NULL
, NULL
))
3343 status
= STATUS_OBJECT_NAME_INVALID
;
3346 attr
.Length
= sizeof(attr
);
3347 attr
.RootDirectory
= 0;
3348 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
3349 attr
.ObjectName
= &dirW
;
3350 attr
.SecurityDescriptor
= NULL
;
3351 attr
.SecurityQualityOfService
= NULL
;
3353 status
= NtOpenFile( &handle
, SYNCHRONIZE
, &attr
, &io
, 0,
3354 FILE_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
);
3355 RtlFreeUnicodeString( &dirW
);
3356 if (status
!= STATUS_SUCCESS
) goto done
;
3359 if ((status
= server_get_unix_fd( handle
, 0, &unix_fd
, &needs_close
, NULL
, NULL
)) == STATUS_SUCCESS
)
3361 RtlEnterCriticalSection( &dir_section
);
3363 if ((old_cwd
= open(".", O_RDONLY
)) != -1 && fchdir( unix_fd
) != -1)
3365 unsigned int size
= 512;
3369 if (!(*cwd
= RtlAllocateHeap( GetProcessHeap(), 0, size
)))
3371 status
= STATUS_NO_MEMORY
;
3374 if (getcwd( *cwd
, size
)) break;
3375 RtlFreeHeap( GetProcessHeap(), 0, *cwd
);
3376 if (errno
!= ERANGE
)
3378 status
= STATUS_OBJECT_PATH_INVALID
;
3383 if (fchdir( old_cwd
) == -1) chdir( "/" );
3385 else status
= FILE_GetNtStatus();
3387 RtlLeaveCriticalSection( &dir_section
);
3388 if (old_cwd
!= -1) close( old_cwd
);
3389 if (needs_close
) close( unix_fd
);
3391 if (!curdir
->Handle
) NtClose( handle
);
3394 RtlReleasePebLock();