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
50 #ifdef HAVE_SYS_IOCTL_H
51 #include <sys/ioctl.h>
53 #ifdef HAVE_LINUX_IOCTL_H
54 #include <linux/ioctl.h>
56 #ifdef HAVE_LINUX_MAJOR_H
57 # include <linux/major.h>
59 #ifdef HAVE_SYS_PARAM_H
60 #include <sys/param.h>
62 #ifdef HAVE_SYS_MOUNT_H
63 #include <sys/mount.h>
65 #ifdef HAVE_SYS_STATFS_H
66 #include <sys/statfs.h>
73 #define NONAMELESSUNION
74 #define NONAMELESSSTRUCT
76 #define WIN32_NO_STATUS
81 #include "ntdll_misc.h"
82 #include "wine/unicode.h"
83 #include "wine/server.h"
84 #include "wine/list.h"
85 #include "wine/library.h"
86 #include "wine/debug.h"
88 WINE_DEFAULT_DEBUG_CHANNEL(file);
91 #undef VFAT_IOCTL_READDIR_BOTH
96 /* We want the real kernel dirent structure, not the libc one */
101 unsigned short d_reclen;
105 /* Define the VFAT ioctl to get both short and long file names */
106 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, KERNEL_DIRENT [2] )
109 # define O_DIRECTORY 0200000 /* must be directory */
112 #ifdef SYS_getdents64
117 unsigned short d_reclen;
118 unsigned char d_type;
122 static inline int getdents64( int fd, char *de, unsigned int size )
124 return syscall( SYS_getdents64, fd, de, size );
131 #define IS_OPTION_TRUE(ch) ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
132 #define IS_SEPARATOR(ch) ((ch) == '\\' || (ch) == '/')
134 #define INVALID_NT_CHARS '*','?','<','>','|','"'
135 #define INVALID_DOS_CHARS INVALID_NT_CHARS,'+','=',',',';','[',']',' ','\345'
137 #define MAX_DIR_ENTRY_LEN 255 /* max length of a directory entry in chars */
139 #define MAX_IGNORED_FILES 4
147 static struct file_identity ignored_files[MAX_IGNORED_FILES];
148 static int ignored_files_count;
150 union file_directory_info
153 FILE_DIRECTORY_INFORMATION dir;
154 FILE_BOTH_DIRECTORY_INFORMATION both;
155 FILE_FULL_DIRECTORY_INFORMATION full;
156 FILE_ID_BOTH_DIRECTORY_INFORMATION id_both;
157 FILE_ID_FULL_DIRECTORY_INFORMATION id_full;
160 static int show_dot_files = -1;
162 /* at some point we may want to allow Winelib apps to set this */
163 static const int is_case_sensitive = FALSE;
165 UNICODE_STRING system_dir = { 0, 0, NULL }; /* system directory */
167 static struct file_identity curdir;
168 static struct file_identity windir;
170 static RTL_CRITICAL_SECTION dir_section;
171 static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
174 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
175 0, 0, { (DWORD_PTR)(__FILE__ ": dir_section") }
177 static RTL_CRITICAL_SECTION dir_section = { &critsect_debug, -1, 0, 0, 0, 0 };
180 /* check if a given Unicode char is OK in a DOS short name */
181 static inline BOOL is_invalid_dos_char( WCHAR ch )
183 static const WCHAR invalid_chars[] = { INVALID_DOS_CHARS,'~','.',0 };
184 if (ch > 0x7f) return TRUE;
185 return strchrW( invalid_chars, ch ) != NULL;
188 /* check if the device can be a mounted volume */
189 static inline int is_valid_mounted_device( const struct stat *st )
191 #if defined(linux) || defined(__sun__)
192 return S_ISBLK( st->st_mode );
194 /* disks are char devices on *BSD */
195 return S_ISCHR( st->st_mode );
199 static inline void ignore_file( const char *name )
202 assert( ignored_files_count < MAX_IGNORED_FILES );
203 if (!stat( name, &st ))
205 ignored_files[ignored_files_count].dev = st.st_dev;
206 ignored_files[ignored_files_count].ino = st.st_ino;
207 ignored_files_count++;
211 static inline BOOL is_same_file( const struct file_identity *file, const struct stat *st )
213 return st->st_dev == file->dev && st->st_ino == file->ino;
216 static inline BOOL is_ignored_file( const struct stat *st )
220 for (i = 0; i < ignored_files_count; i++)
221 if (is_same_file( &ignored_files[i], st )) return TRUE;
225 static inline unsigned int dir_info_size( FILE_INFORMATION_CLASS class, unsigned int len )
229 case FileDirectoryInformation:
230 return (FIELD_OFFSET( FILE_DIRECTORY_INFORMATION, FileName[len] ) + 3) & ~3;
231 case FileBothDirectoryInformation:
232 return (FIELD_OFFSET( FILE_BOTH_DIRECTORY_INFORMATION, FileName[len] ) + 3) & ~3;
233 case FileFullDirectoryInformation:
234 return (FIELD_OFFSET( FILE_FULL_DIRECTORY_INFORMATION, FileName[len] ) + 3) & ~3;
235 case FileIdBothDirectoryInformation:
236 return (FIELD_OFFSET( FILE_ID_BOTH_DIRECTORY_INFORMATION, FileName[len] ) + 3) & ~3;
237 case FileIdFullDirectoryInformation:
238 return (FIELD_OFFSET( FILE_ID_FULL_DIRECTORY_INFORMATION, FileName[len] ) + 3) & ~3;
245 static inline unsigned int max_dir_info_size( FILE_INFORMATION_CLASS class )
247 return dir_info_size( class, MAX_DIR_ENTRY_LEN );
251 /* support for a directory queue for filesystem searches */
259 static struct list dir_queue = LIST_INIT( dir_queue );
261 static NTSTATUS add_dir_to_queue( const char *name )
263 int len = strlen( name ) + 1;
264 struct dir_name *dir = RtlAllocateHeap( GetProcessHeap(), 0,
265 FIELD_OFFSET( struct dir_name, name[len] ));
266 if (!dir) return STATUS_NO_MEMORY;
267 strcpy( dir->name, name );
268 list_add_tail( &dir_queue, &dir->entry );
269 return STATUS_SUCCESS;
272 static NTSTATUS next_dir_in_queue( char *name )
274 struct list *head = list_head( &dir_queue );
277 struct dir_name *dir = LIST_ENTRY( head, struct dir_name, entry );
278 strcpy( name, dir->name );
279 list_remove( &dir->entry );
280 RtlFreeHeap( GetProcessHeap(), 0, dir );
281 return STATUS_SUCCESS;
283 return STATUS_OBJECT_NAME_NOT_FOUND;
286 static void flush_dir_queue(void)
290 while ((head = list_head( &dir_queue )))
292 struct dir_name *dir = LIST_ENTRY( head, struct dir_name, entry );
293 list_remove( &dir->entry );
294 RtlFreeHeap( GetProcessHeap(), 0, dir );
299 /***********************************************************************
300 * get_default_com_device
302 * Return the default device to use for serial ports.
304 static char *get_default_com_device( int num )
308 if (!num || num > 9) return ret;
310 ret = RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/ttyS0") );
313 strcpy( ret, "/dev/ttyS0" );
314 ret[strlen(ret) - 1] = '0' + num - 1;
316 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
317 ret = RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/cuau0") );
320 strcpy( ret, "/dev/cuau0" );
321 ret[strlen(ret) - 1] = '0' + num - 1;
323 #elif defined(__DragonFly__)
324 ret = RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/cuaa0") );
327 strcpy( ret, "/dev/cuaa0" );
328 ret[strlen(ret) - 1] = '0' + num - 1;
331 FIXME( "no known default for device com%d\n", num );
337 /***********************************************************************
338 * get_default_lpt_device
340 * Return the default device to use for parallel ports.
342 static char *get_default_lpt_device( int num )
346 if (!num || num > 9) return ret;
348 ret = RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/lp0") );
351 strcpy( ret, "/dev/lp0" );
352 ret[strlen(ret) - 1] = '0' + num - 1;
355 FIXME( "no known default for device lpt%d\n", num );
361 /***********************************************************************
362 * DIR_get_drives_info
364 * Retrieve device/inode number for all the drives. Helper for find_drive_root.
366 unsigned int DIR_get_drives_info( struct drive_info info[MAX_DOS_DRIVES] )
368 static struct drive_info cache[MAX_DOS_DRIVES];
369 static time_t last_update;
370 static unsigned int nb_drives;
372 time_t now = time(NULL);
374 RtlEnterCriticalSection( &dir_section );
375 if (now != last_update)
377 const char *config_dir = wine_get_config_dir();
382 if ((buffer = RtlAllocateHeap( GetProcessHeap(), 0,
383 strlen(config_dir) + sizeof("/dosdevices/a:") )))
385 strcpy( buffer, config_dir );
386 strcat( buffer, "/dosdevices/a:" );
387 p = buffer + strlen(buffer) - 2;
389 for (i = nb_drives = 0; i < MAX_DOS_DRIVES; i++)
392 if (!stat( buffer, &st ))
394 cache[i].dev = st.st_dev;
395 cache[i].ino = st.st_ino;
404 RtlFreeHeap( GetProcessHeap(), 0, buffer );
408 memcpy( info, cache, sizeof(cache) );
410 RtlLeaveCriticalSection( &dir_section );
415 /***********************************************************************
416 * parse_mount_entries
418 * Parse mount entries looking for a given device. Helper for get_default_drive_device.
422 #include <sys/vfstab.h>
423 static char *parse_vfstab_entries( FILE *f, dev_t dev, ino_t ino)
429 while (! getvfsent( f, &entry ))
431 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
432 if (!strcmp( entry.vfs_fstype, "nfs" ) ||
433 !strcmp( entry.vfs_fstype, "smbfs" ) ||
434 !strcmp( entry.vfs_fstype, "ncpfs" )) continue;
436 if (stat( entry.vfs_mountp, &st ) == -1) continue;
437 if (st.st_dev != dev || st.st_ino != ino) continue;
438 if (!strcmp( entry.vfs_fstype, "fd" ))
440 if ((device = strstr( entry.vfs_mntopts, "dev=" )))
442 char *p = strchr( device + 4, ',' );
448 return entry.vfs_special;
455 static char *parse_mount_entries( FILE *f, dev_t dev, ino_t ino )
457 struct mntent *entry;
461 while ((entry = getmntent( f )))
463 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
464 if (!strcmp( entry->mnt_type, "nfs" ) ||
465 !strcmp( entry->mnt_type, "smbfs" ) ||
466 !strcmp( entry->mnt_type, "ncpfs" )) continue;
468 if (stat( entry->mnt_dir, &st ) == -1) continue;
469 if (st.st_dev != dev || st.st_ino != ino) continue;
470 if (!strcmp( entry->mnt_type, "supermount" ))
472 if ((device = strstr( entry->mnt_opts, "dev=" )))
474 char *p = strchr( device + 4, ',' );
479 else if (!stat( entry->mnt_fsname, &st ) && S_ISREG(st.st_mode))
481 /* if device is a regular file check for a loop mount */
482 if ((device = strstr( entry->mnt_opts, "loop=" )))
484 char *p = strchr( device + 5, ',' );
490 return entry->mnt_fsname;
496 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
498 static char *parse_mount_entries( FILE *f, dev_t dev, ino_t ino )
503 while ((entry = getfsent()))
505 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
506 if (!strcmp( entry->fs_vfstype, "nfs" ) ||
507 !strcmp( entry->fs_vfstype, "smbfs" ) ||
508 !strcmp( entry->fs_vfstype, "ncpfs" )) continue;
510 if (stat( entry->fs_file, &st ) == -1) continue;
511 if (st.st_dev != dev || st.st_ino != ino) continue;
512 return entry->fs_spec;
519 #include <sys/mnttab.h>
520 static char *parse_mount_entries( FILE *f, dev_t dev, ino_t ino )
527 while (( ! getmntent( f, &entry) ))
529 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
530 if (!strcmp( entry.mnt_fstype, "nfs" ) ||
531 !strcmp( entry.mnt_fstype, "smbfs" ) ||
532 !strcmp( entry.mnt_fstype, "ncpfs" )) continue;
534 if (stat( entry.mnt_mountp, &st ) == -1) continue;
535 if (st.st_dev != dev || st.st_ino != ino) continue;
536 if (!strcmp( entry.mnt_fstype, "fd" ))
538 if ((device = strstr( entry.mnt_mntopts, "dev=" )))
540 char *p = strchr( device + 4, ',' );
546 return entry.mnt_special;
552 /***********************************************************************
553 * get_default_drive_device
555 * Return the default device to use for a given drive mount point.
557 static char *get_default_drive_device( const char *root )
567 /* try to open it first to force it to get mounted */
568 if ((fd = open( root, O_RDONLY | O_DIRECTORY )) != -1)
570 res = fstat( fd, &st );
573 /* now try normal stat just in case */
574 if (res == -1) res = stat( root, &st );
575 if (res == -1) return NULL;
577 RtlEnterCriticalSection( &dir_section );
579 if ((f = fopen( "/etc/mtab", "r" )))
581 device = parse_mount_entries( f, st.st_dev, st.st_ino );
584 /* look through fstab too in case it's not mounted (for instance if it's an audio CD) */
585 if (!device && (f = fopen( "/etc/fstab", "r" )))
587 device = parse_mount_entries( f, st.st_dev, st.st_ino );
592 ret = RtlAllocateHeap( GetProcessHeap(), 0, strlen(device) + 1 );
593 if (ret) strcpy( ret, device );
595 RtlLeaveCriticalSection( &dir_section );
597 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__ ) || defined(__DragonFly__)
602 /* try to open it first to force it to get mounted */
603 if ((fd = open( root, O_RDONLY )) != -1)
605 res = fstat( fd, &st );
608 /* now try normal stat just in case */
609 if (res == -1) res = stat( root, &st );
610 if (res == -1) return NULL;
612 RtlEnterCriticalSection( &dir_section );
614 /* The FreeBSD parse_mount_entries doesn't require a file argument, so just
615 * pass NULL. Leave the argument in for symmetry.
617 device = parse_mount_entries( NULL, st.st_dev, st.st_ino );
620 ret = RtlAllocateHeap( GetProcessHeap(), 0, strlen(device) + 1 );
621 if (ret) strcpy( ret, device );
623 RtlLeaveCriticalSection( &dir_section );
631 /* try to open it first to force it to get mounted */
632 if ((fd = open( root, O_RDONLY )) != -1)
634 res = fstat( fd, &st );
637 /* now try normal stat just in case */
638 if (res == -1) res = stat( root, &st );
639 if (res == -1) return NULL;
641 RtlEnterCriticalSection( &dir_section );
643 if ((f = fopen( "/etc/mnttab", "r" )))
645 device = parse_mount_entries( f, st.st_dev, st.st_ino);
648 /* look through fstab too in case it's not mounted (for instance if it's an audio CD) */
649 if (!device && (f = fopen( "/etc/vfstab", "r" )))
651 device = parse_vfstab_entries( f, st.st_dev, st.st_ino );
656 ret = RtlAllocateHeap( GetProcessHeap(), 0, strlen(device) + 1 );
657 if (ret) strcpy( ret, device );
659 RtlLeaveCriticalSection( &dir_section );
661 #elif defined(__APPLE__)
662 struct statfs *mntStat;
668 static const char path_bsd_device[] = "/dev/disk";
671 res = stat( root, &st );
672 if (res == -1) return NULL;
677 RtlEnterCriticalSection( &dir_section );
679 mntSize = getmntinfo(&mntStat, MNT_NOWAIT);
681 for (i = 0; i < mntSize && !ret; i++)
683 if (stat(mntStat[i].f_mntonname, &st ) == -1) continue;
684 if (st.st_dev != dev || st.st_ino != ino) continue;
686 /* FIXME add support for mounted network drive */
687 if ( strncmp(mntStat[i].f_mntfromname, path_bsd_device, strlen(path_bsd_device)) == 0)
689 /* set return value to the corresponding raw BSD node */
690 ret = RtlAllocateHeap( GetProcessHeap(), 0, strlen(mntStat[i].f_mntfromname) + 2 /* 2 : r and \0 */ );
693 strcpy(ret, "/dev/r");
694 strcat(ret, mntStat[i].f_mntfromname+sizeof("/dev/")-1);
698 RtlLeaveCriticalSection( &dir_section );
701 if (!warned++) FIXME( "auto detection of DOS devices not supported on this platform\n" );
707 /***********************************************************************
708 * get_device_mount_point
710 * Return the current mount point for a device.
712 static char *get_device_mount_point( dev_t dev )
719 RtlEnterCriticalSection( &dir_section );
721 if ((f = fopen( "/etc/mtab", "r" )))
723 struct mntent *entry;
727 while ((entry = getmntent( f )))
729 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
730 if (!strcmp( entry->mnt_type, "nfs" ) ||
731 !strcmp( entry->mnt_type, "smbfs" ) ||
732 !strcmp( entry->mnt_type, "ncpfs" )) continue;
734 if (!strcmp( entry->mnt_type, "supermount" ))
736 if ((device = strstr( entry->mnt_opts, "dev=" )))
739 if ((p = strchr( device, ',' ))) *p = 0;
742 else if (!stat( entry->mnt_fsname, &st ) && S_ISREG(st.st_mode))
744 /* if device is a regular file check for a loop mount */
745 if ((device = strstr( entry->mnt_opts, "loop=" )))
748 if ((p = strchr( device, ',' ))) *p = 0;
751 else device = entry->mnt_fsname;
753 if (device && !stat( device, &st ) && S_ISBLK(st.st_mode) && st.st_rdev == dev)
755 ret = RtlAllocateHeap( GetProcessHeap(), 0, strlen(entry->mnt_dir) + 1 );
756 if (ret) strcpy( ret, entry->mnt_dir );
762 RtlLeaveCriticalSection( &dir_section );
763 #elif defined(__APPLE__)
764 struct statfs *entry;
768 RtlEnterCriticalSection( &dir_section );
770 size = getmntinfo( &entry, MNT_NOWAIT );
771 for (i = 0; i < size; i++)
773 if (stat( entry[i].f_mntfromname, &st ) == -1) continue;
774 if (S_ISBLK(st.st_mode) && st.st_rdev == dev)
776 ret = RtlAllocateHeap( GetProcessHeap(), 0, strlen(entry[i].f_mntfromname) + 1 );
777 if (ret) strcpy( ret, entry[i].f_mntfromname );
781 RtlLeaveCriticalSection( &dir_section );
784 if (!warned++) FIXME( "unmounting devices not supported on this platform\n" );
790 #if defined(HAVE_GETATTRLIST) && defined(ATTR_VOL_CAPABILITIES) && \
791 defined(VOL_CAPABILITIES_FORMAT) && defined(VOL_CAP_FMT_CASE_SENSITIVE)
804 BOOLEAN case_sensitive;
810 vol_capabilities_attr_t caps;
813 /***********************************************************************
816 * Checks if the specified file system is in the cache.
818 static struct fs_cache *look_up_fs_cache( dev_t dev )
821 for (i = 0; i < sizeof(fs_cache)/sizeof(fs_cache[0]); i++)
822 if (fs_cache[i].dev == dev)
827 /***********************************************************************
830 * Adds the specified file system to the cache.
832 static void add_fs_cache( dev_t dev, fsid_t fsid, BOOLEAN case_sensitive )
835 struct fs_cache *entry = look_up_fs_cache( dev );
839 /* Update the cache */
841 entry->case_sensitive = case_sensitive;
845 /* Add a new entry */
846 for (i = 0; i < sizeof(fs_cache)/sizeof(fs_cache[0]); i++)
847 if (fs_cache[i].dev == 0)
849 /* This entry is empty, use it */
850 fs_cache[i].dev = dev;
851 fs_cache[i].fsid = fsid;
852 fs_cache[i].case_sensitive = case_sensitive;
856 /* Cache is out of space, warn */
858 WARN( "FS cache is out of space, expect performance problems\n" );
861 /***********************************************************************
862 * get_dir_case_sensitivity_attr
864 * Checks if the volume containing the specified directory is case
865 * sensitive or not. Uses getattrlist(2).
867 static int get_dir_case_sensitivity_attr( const char *dir )
869 char *mntpoint = NULL;
870 struct attrlist attr;
871 struct vol_caps caps;
872 struct get_fsid get_fsid;
873 struct fs_cache *entry;
875 /* First get the FS ID of the volume */
876 attr.bitmapcount = ATTR_BIT_MAP_COUNT;
878 attr.commonattr = ATTR_CMN_DEVID|ATTR_CMN_FSID;
879 attr.volattr = attr.dirattr = attr.fileattr = attr.forkattr = 0;
881 if (getattrlist( dir, &attr, &get_fsid, sizeof(get_fsid), 0 ) != 0 ||
882 get_fsid.size != sizeof(get_fsid))
884 /* Try to look it up in the cache */
885 entry = look_up_fs_cache( get_fsid.dev );
886 if (entry && !memcmp( &entry->fsid, &get_fsid.fsid, sizeof(fsid_t) ))
887 /* Cache lookup succeeded */
888 return entry->case_sensitive;
889 /* Cache is stale at this point, we have to update it */
891 mntpoint = get_device_mount_point( get_fsid.dev );
892 /* Now look up the case-sensitivity */
894 attr.volattr = ATTR_VOL_INFO|ATTR_VOL_CAPABILITIES;
895 if (getattrlist( mntpoint, &attr, &caps, sizeof(caps), 0 ) < 0)
897 RtlFreeHeap( GetProcessHeap(), 0, mntpoint );
898 add_fs_cache( get_fsid.dev, get_fsid.fsid, TRUE );
901 RtlFreeHeap( GetProcessHeap(), 0, mntpoint );
902 if (caps.size == sizeof(caps) &&
903 (caps.caps.valid[VOL_CAPABILITIES_FORMAT] &
904 (VOL_CAP_FMT_CASE_SENSITIVE | VOL_CAP_FMT_CASE_PRESERVING)) ==
905 (VOL_CAP_FMT_CASE_SENSITIVE | VOL_CAP_FMT_CASE_PRESERVING))
909 if ((caps.caps.capabilities[VOL_CAPABILITIES_FORMAT] &
910 VOL_CAP_FMT_CASE_SENSITIVE) != VOL_CAP_FMT_CASE_SENSITIVE)
914 /* Update the cache */
915 add_fs_cache( get_fsid.dev, get_fsid.fsid, ret );
922 /***********************************************************************
923 * get_dir_case_sensitivity_stat
925 * Checks if the volume containing the specified directory is case
926 * sensitive or not. Uses statfs(2) or statvfs(2).
928 static BOOLEAN get_dir_case_sensitivity_stat( const char *dir )
930 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
933 if (statfs( dir, &stfs ) == -1) return FALSE;
934 /* Assume these file systems are always case insensitive on Mac OS.
935 * For FreeBSD, only assume CIOPFS is case insensitive (AFAIK, Mac OS
936 * is the only UNIX that supports case-insensitive lookup).
938 if (!strcmp( stfs.f_fstypename, "fusefs" ) &&
939 !strncmp( stfs.f_mntfromname, "ciopfs", 5 ))
942 if (!strcmp( stfs.f_fstypename, "msdos" ) ||
943 !strcmp( stfs.f_fstypename, "cd9660" ) ||
944 !strcmp( stfs.f_fstypename, "udf" ) ||
945 !strcmp( stfs.f_fstypename, "ntfs" ) ||
946 !strcmp( stfs.f_fstypename, "smbfs" ))
948 #ifdef _DARWIN_FEATURE_64_BIT_INODE
949 if (!strcmp( stfs.f_fstypename, "hfs" ) && (stfs.f_fssubtype == 0 ||
950 stfs.f_fssubtype == 1 ||
951 stfs.f_fssubtype == 128))
954 /* The field says "reserved", but a quick look at the kernel source
955 * tells us that this "reserved" field is really the same as the
956 * "fssubtype" field from the inode64 structure (see munge_statfs()
957 * in <xnu-source>/bsd/vfs/vfs_syscalls.c).
959 if (!strcmp( stfs.f_fstypename, "hfs" ) && (stfs.f_reserved1 == 0 ||
960 stfs.f_reserved1 == 1 ||
961 stfs.f_reserved1 == 128))
967 #elif defined(__NetBSD__)
970 if (statvfs( dir, &stfs ) == -1) return FALSE;
971 /* Only assume CIOPFS is case insensitive. */
972 if (strcmp( stfs.f_fstypename, "fusefs" ) ||
973 strncmp( stfs.f_mntfromname, "ciopfs", 5 ))
977 #elif defined(__linux__)
982 /* Only assume CIOPFS is case insensitive. */
983 if (statfs( dir, &stfs ) == -1) return FALSE;
984 if (stfs.f_type != 0x65735546 /* FUSE_SUPER_MAGIC */)
986 /* Normally, we'd have to parse the mtab to find out exactly what
987 * kind of FUSE FS this is. But, someone on wine-devel suggested
988 * a shortcut. We'll stat a special file in the directory. If it's
989 * there, we'll assume it's a CIOPFS, else not.
990 * This will break if somebody puts a file named ".ciopfs" in a non-
993 cifile = RtlAllocateHeap( GetProcessHeap(), 0, strlen( dir )+sizeof("/.ciopfs") );
994 if (!cifile) return TRUE;
995 strcpy( cifile, dir );
996 strcat( cifile, "/.ciopfs" );
997 if (stat( cifile, &st ) == 0)
999 RtlFreeHeap( GetProcessHeap(), 0, cifile );
1002 RtlFreeHeap( GetProcessHeap(), 0, cifile );
1010 /***********************************************************************
1011 * get_dir_case_sensitivity
1013 * Checks if the volume containing the specified directory is case
1014 * sensitive or not. Uses statfs(2) or statvfs(2).
1016 static BOOLEAN get_dir_case_sensitivity( const char *dir )
1018 #if defined(HAVE_GETATTRLIST) && defined(ATTR_VOL_CAPABILITIES) && \
1019 defined(VOL_CAPABILITIES_FORMAT) && defined(VOL_CAP_FMT_CASE_SENSITIVE)
1020 int case_sensitive = get_dir_case_sensitivity_attr( dir );
1021 if (case_sensitive != -1) return case_sensitive;
1023 return get_dir_case_sensitivity_stat( dir );
1027 /***********************************************************************
1030 * Initialize the show_dot_files options.
1032 static void init_options(void)
1034 static const WCHAR WineW[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e',0};
1035 static const WCHAR ShowDotFilesW[] = {'S','h','o','w','D','o','t','F','i','l','e','s',0};
1039 OBJECT_ATTRIBUTES attr;
1040 UNICODE_STRING nameW;
1044 RtlOpenCurrentUser( KEY_ALL_ACCESS, &root );
1045 attr.Length = sizeof(attr);
1046 attr.RootDirectory = root;
1047 attr.ObjectName = &nameW;
1048 attr.Attributes = 0;
1049 attr.SecurityDescriptor = NULL;
1050 attr.SecurityQualityOfService = NULL;
1051 RtlInitUnicodeString( &nameW, WineW );
1053 /* @@ Wine registry key: HKCU\Software\Wine */
1054 if (!NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ))
1056 RtlInitUnicodeString( &nameW, ShowDotFilesW );
1057 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, tmp, sizeof(tmp), &dummy ))
1059 WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
1060 show_dot_files = IS_OPTION_TRUE( str[0] );
1066 /* a couple of directories that we don't want to return in directory searches */
1067 ignore_file( wine_get_config_dir() );
1068 ignore_file( "/dev" );
1069 ignore_file( "/proc" );
1071 ignore_file( "/sys" );
1076 /***********************************************************************
1077 * DIR_is_hidden_file
1079 * Check if the specified file should be hidden based on its name and the show dot files option.
1081 BOOL DIR_is_hidden_file( const UNICODE_STRING *name )
1085 if (show_dot_files == -1) init_options();
1086 if (show_dot_files) return FALSE;
1088 end = p = name->Buffer + name->Length/sizeof(WCHAR);
1089 while (p > name->Buffer && IS_SEPARATOR(p[-1])) p--;
1090 while (p > name->Buffer && !IS_SEPARATOR(p[-1])) p--;
1091 if (p == end || *p != '.') return FALSE;
1092 /* make sure it isn't '.' or '..' */
1093 if (p + 1 == end) return FALSE;
1094 if (p[1] == '.' && p + 2 == end) return FALSE;
1099 /***********************************************************************
1100 * hash_short_file_name
1102 * Transform a Unix file name into a hashed DOS name. If the name is a valid
1103 * DOS name, it is converted to upper-case; otherwise it is replaced by a
1104 * hashed version that fits in 8.3 format.
1105 * 'buffer' must be at least 12 characters long.
1106 * Returns length of short name in bytes; short name is NOT null-terminated.
1108 static ULONG hash_short_file_name( const UNICODE_STRING *name, LPWSTR buffer )
1110 static const char hash_chars[32] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345";
1112 LPCWSTR p, ext, end = name->Buffer + name->Length / sizeof(WCHAR);
1114 unsigned short hash;
1117 /* Compute the hash code of the file name */
1118 /* If you know something about hash functions, feel free to */
1119 /* insert a better algorithm here... */
1120 if (!is_case_sensitive)
1122 for (p = name->Buffer, hash = 0xbeef; p < end - 1; p++)
1123 hash = (hash<<3) ^ (hash>>5) ^ tolowerW(*p) ^ (tolowerW(p[1]) << 8);
1124 hash = (hash<<3) ^ (hash>>5) ^ tolowerW(*p); /* Last character */
1128 for (p = name->Buffer, hash = 0xbeef; p < end - 1; p++)
1129 hash = (hash << 3) ^ (hash >> 5) ^ *p ^ (p[1] << 8);
1130 hash = (hash << 3) ^ (hash >> 5) ^ *p; /* Last character */
1133 /* Find last dot for start of the extension */
1134 for (p = name->Buffer + 1, ext = NULL; p < end - 1; p++) if (*p == '.') ext = p;
1136 /* Copy first 4 chars, replacing invalid chars with '_' */
1137 for (i = 4, p = name->Buffer, dst = buffer; i > 0; i--, p++)
1139 if (p == end || p == ext) break;
1140 *dst++ = is_invalid_dos_char(*p) ? '_' : toupperW(*p);
1142 /* Pad to 5 chars with '~' */
1143 while (i-- >= 0) *dst++ = '~';
1145 /* Insert hash code converted to 3 ASCII chars */
1146 *dst++ = hash_chars[(hash >> 10) & 0x1f];
1147 *dst++ = hash_chars[(hash >> 5) & 0x1f];
1148 *dst++ = hash_chars[hash & 0x1f];
1150 /* Copy the first 3 chars of the extension (if any) */
1154 for (i = 3, ext++; (i > 0) && ext < end; i--, ext++)
1155 *dst++ = is_invalid_dos_char(*ext) ? '_' : toupperW(*ext);
1157 return dst - buffer;
1161 /***********************************************************************
1164 * Check a long file name against a mask.
1166 * Tests (done in W95 DOS shell - case insensitive):
1167 * *.txt test1.test.txt *
1169 * *.t??????.t* test1.ta.tornado.txt *
1170 * *tornado* test1.ta.tornado.txt *
1171 * t*t test1.ta.tornado.txt *
1173 * ?est??? test1.txt -
1174 * *test1.txt* test1.txt *
1175 * h?l?o*t.dat hellothisisatest.dat *
1177 static BOOLEAN match_filename( const UNICODE_STRING *name_str, const UNICODE_STRING *mask_str )
1180 const WCHAR *name = name_str->Buffer;
1181 const WCHAR *mask = mask_str->Buffer;
1182 const WCHAR *name_end = name + name_str->Length / sizeof(WCHAR);
1183 const WCHAR *mask_end = mask + mask_str->Length / sizeof(WCHAR);
1184 const WCHAR *lastjoker = NULL;
1185 const WCHAR *next_to_retry = NULL;
1187 TRACE("(%s, %s)\n", debugstr_us(name_str), debugstr_us(mask_str));
1189 while (name < name_end && mask < mask_end)
1195 while (mask < mask_end && *mask == '*') mask++; /* Skip consecutive '*' */
1196 if (mask == mask_end) return TRUE; /* end of mask is all '*', so match */
1199 /* skip to the next match after the joker(s) */
1200 if (is_case_sensitive)
1201 while (name < name_end && (*name != *mask)) name++;
1203 while (name < name_end && (toupperW(*name) != toupperW(*mask))) name++;
1204 next_to_retry = name;
1211 if (is_case_sensitive) mismatch = (*mask != *name);
1212 else mismatch = (toupperW(*mask) != toupperW(*name));
1218 if (mask == mask_end)
1220 if (name == name_end) return TRUE;
1221 if (lastjoker) mask = lastjoker;
1224 else /* mismatch ! */
1226 if (lastjoker) /* we had an '*', so we can try unlimitedly */
1230 /* this scan sequence was a mismatch, so restart
1231 * 1 char after the first char we checked last time */
1233 name = next_to_retry;
1235 else return FALSE; /* bad luck */
1240 while (mask < mask_end && ((*mask == '.') || (*mask == '*')))
1241 mask++; /* Ignore trailing '.' or '*' in mask */
1242 return (name == name_end && mask == mask_end);
1246 /***********************************************************************
1249 * helper for NtQueryDirectoryFile
1251 static union file_directory_info *append_entry( void *info_ptr, IO_STATUS_BLOCK *io, ULONG max_length,
1252 const char *long_name, const char *short_name,
1253 const UNICODE_STRING *mask, FILE_INFORMATION_CLASS class )
1255 union file_directory_info *info;
1256 int i, long_len, short_len, total_len;
1258 WCHAR long_nameW[MAX_DIR_ENTRY_LEN];
1259 WCHAR short_nameW[12];
1262 ULONG attributes = 0;
1264 io->u.Status = STATUS_SUCCESS;
1265 long_len = ntdll_umbstowcs( 0, long_name, strlen(long_name), long_nameW, MAX_DIR_ENTRY_LEN );
1266 if (long_len == -1) return NULL;
1268 str.Buffer = long_nameW;
1269 str.Length = long_len * sizeof(WCHAR);
1270 str.MaximumLength = sizeof(long_nameW);
1274 short_len = ntdll_umbstowcs( 0, short_name, strlen(short_name),
1275 short_nameW, sizeof(short_nameW) / sizeof(WCHAR) );
1276 if (short_len == -1) short_len = sizeof(short_nameW) / sizeof(WCHAR);
1278 else /* generate a short name if necessary */
1283 if (!RtlIsNameLegalDOS8Dot3( &str, NULL, &spaces ) || spaces)
1284 short_len = hash_short_file_name( &str, short_nameW );
1287 TRACE( "long %s short %s mask %s\n",
1288 debugstr_us(&str), debugstr_wn(short_nameW, short_len), debugstr_us(mask) );
1290 if (mask && !match_filename( &str, mask ))
1292 if (!short_len) return NULL; /* no short name to match */
1293 str.Buffer = short_nameW;
1294 str.Length = short_len * sizeof(WCHAR);
1295 str.MaximumLength = sizeof(short_nameW);
1296 if (!match_filename( &str, mask )) return NULL;
1299 if (lstat( long_name, &st ) == -1) return NULL;
1300 if (S_ISLNK( st.st_mode ))
1302 if (stat( long_name, &st ) == -1) return NULL;
1303 if (S_ISDIR( st.st_mode )) attributes |= FILE_ATTRIBUTE_REPARSE_POINT;
1305 if (is_ignored_file( &st ))
1307 TRACE( "ignoring file %s\n", long_name );
1310 if (!show_dot_files && long_name[0] == '.' && long_name[1] && (long_name[1] != '.' || long_name[2]))
1311 attributes |= FILE_ATTRIBUTE_HIDDEN;
1313 total_len = dir_info_size( class, long_len );
1314 if (io->Information + total_len > max_length)
1316 total_len = max_length - io->Information;
1317 io->u.Status = STATUS_BUFFER_OVERFLOW;
1319 info = (union file_directory_info *)((char *)info_ptr + io->Information);
1320 if (st.st_dev != curdir.dev) st.st_ino = 0; /* ignore inode if on a different device */
1321 /* all the structures start with a FileDirectoryInformation layout */
1322 fill_stat_info( &st, info, class );
1323 info->dir.NextEntryOffset = total_len;
1324 info->dir.FileIndex = 0; /* NTFS always has 0 here, so let's not bother with it */
1325 info->dir.FileAttributes |= attributes;
1329 case FileDirectoryInformation:
1330 info->dir.FileNameLength = long_len * sizeof(WCHAR);
1331 filename = info->dir.FileName;
1334 case FileFullDirectoryInformation:
1335 info->full.EaSize = 0; /* FIXME */
1336 info->full.FileNameLength = long_len * sizeof(WCHAR);
1337 filename = info->full.FileName;
1340 case FileIdFullDirectoryInformation:
1341 info->id_full.EaSize = 0; /* FIXME */
1342 info->id_full.FileNameLength = long_len * sizeof(WCHAR);
1343 filename = info->id_full.FileName;
1346 case FileBothDirectoryInformation:
1347 info->both.EaSize = 0; /* FIXME */
1348 info->both.ShortNameLength = short_len * sizeof(WCHAR);
1349 for (i = 0; i < short_len; i++) info->both.ShortName[i] = toupperW(short_nameW[i]);
1350 info->both.FileNameLength = long_len * sizeof(WCHAR);
1351 filename = info->both.FileName;
1354 case FileIdBothDirectoryInformation:
1355 info->id_both.EaSize = 0; /* FIXME */
1356 info->id_both.ShortNameLength = short_len * sizeof(WCHAR);
1357 for (i = 0; i < short_len; i++) info->id_both.ShortName[i] = toupperW(short_nameW[i]);
1358 info->id_both.FileNameLength = long_len * sizeof(WCHAR);
1359 filename = info->id_both.FileName;
1366 memcpy( filename, long_nameW, total_len - ((char *)filename - (char *)info) );
1367 io->Information += total_len;
1372 #ifdef VFAT_IOCTL_READDIR_BOTH
1374 /***********************************************************************
1377 * Wrapper for the VFAT ioctl to work around various kernel bugs.
1378 * dir_section must be held by caller.
1380 static KERNEL_DIRENT *start_vfat_ioctl( int fd )
1382 static KERNEL_DIRENT *de;
1387 const size_t page_size = getpagesize();
1388 SIZE_T size = 2 * sizeof(*de) + page_size;
1391 if (NtAllocateVirtualMemory( GetCurrentProcess(), &addr, 1, &size, MEM_RESERVE, PAGE_READWRITE ))
1393 /* commit only the size needed for the dir entries */
1394 /* this leaves an extra unaccessible page, which should make the kernel */
1395 /* fail with -EFAULT before it stomps all over our memory */
1397 size = 2 * sizeof(*de);
1398 NtAllocateVirtualMemory( GetCurrentProcess(), &addr, 1, &size, MEM_COMMIT, PAGE_READWRITE );
1401 /* set d_reclen to 65535 to work around an AFS kernel bug */
1402 de[0].d_reclen = 65535;
1403 res = ioctl( fd, VFAT_IOCTL_READDIR_BOTH, (long)de );
1406 if (errno != ENOENT) return NULL; /* VFAT ioctl probably not supported */
1407 de[0].d_reclen = 0; /* eof */
1409 else if (!res && de[0].d_reclen == 65535) return NULL; /* AFS bug */
1415 /***********************************************************************
1416 * read_directory_vfat
1418 * Read a directory using the VFAT ioctl; helper for NtQueryDirectoryFile.
1420 static int read_directory_vfat( int fd, IO_STATUS_BLOCK *io, void *buffer, ULONG length,
1421 BOOLEAN single_entry, const UNICODE_STRING *mask,
1422 BOOLEAN restart_scan, FILE_INFORMATION_CLASS class )
1427 union file_directory_info *info, *last_info = NULL;
1429 io->u.Status = STATUS_SUCCESS;
1431 if (restart_scan) lseek( fd, 0, SEEK_SET );
1433 if (length < max_dir_info_size(class)) /* we may have to return a partial entry here */
1435 off_t old_pos = lseek( fd, 0, SEEK_CUR );
1437 if (!(de = start_vfat_ioctl( fd ))) return -1; /* not supported */
1439 while (de[0].d_reclen)
1441 /* make sure names are null-terminated to work around an x86-64 kernel bug */
1442 len = min(de[0].d_reclen, sizeof(de[0].d_name) - 1 );
1443 de[0].d_name[len] = 0;
1444 len = min(de[1].d_reclen, sizeof(de[1].d_name) - 1 );
1445 de[1].d_name[len] = 0;
1447 if (de[1].d_name[0])
1448 info = append_entry( buffer, io, length, de[1].d_name, de[0].d_name, mask, class );
1450 info = append_entry( buffer, io, length, de[0].d_name, NULL, mask, class );
1454 if (io->u.Status == STATUS_BUFFER_OVERFLOW)
1455 lseek( fd, old_pos, SEEK_SET ); /* restore pos to previous entry */
1458 old_pos = lseek( fd, 0, SEEK_CUR );
1459 if (ioctl( fd, VFAT_IOCTL_READDIR_BOTH, (long)de ) == -1) break;
1462 else /* we'll only return full entries, no need to worry about overflow */
1464 if (!(de = start_vfat_ioctl( fd ))) return -1; /* not supported */
1466 while (de[0].d_reclen)
1468 /* make sure names are null-terminated to work around an x86-64 kernel bug */
1469 len = min(de[0].d_reclen, sizeof(de[0].d_name) - 1 );
1470 de[0].d_name[len] = 0;
1471 len = min(de[1].d_reclen, sizeof(de[1].d_name) - 1 );
1472 de[1].d_name[len] = 0;
1474 if (de[1].d_name[0])
1475 info = append_entry( buffer, io, length, de[1].d_name, de[0].d_name, mask, class );
1477 info = append_entry( buffer, io, length, de[0].d_name, NULL, mask, class );
1481 if (single_entry) break;
1482 /* check if we still have enough space for the largest possible entry */
1483 if (io->Information + max_dir_info_size(class) > length) break;
1485 if (ioctl( fd, VFAT_IOCTL_READDIR_BOTH, (long)de ) == -1) break;
1489 if (last_info) last_info->next = 0;
1490 else io->u.Status = restart_scan ? STATUS_NO_SUCH_FILE : STATUS_NO_MORE_FILES;
1493 #endif /* VFAT_IOCTL_READDIR_BOTH */
1497 /***********************************************************************
1498 * read_first_dent_name
1500 * reads name of first or second dentry (if they have inodes).
1502 static char *read_first_dent_name( int which, int fd, off_t second_offs, KERNEL_DIRENT64 *de_first_two,
1503 char *buffer, size_t size, BOOL *buffer_changed )
1505 KERNEL_DIRENT64 *de;
1512 de = (KERNEL_DIRENT64 *)((char *)de + de->d_reclen);
1514 return de->d_ino ? de->d_name : NULL;
1517 *buffer_changed = TRUE;
1518 lseek( fd, which == 1 ? second_offs : 0, SEEK_SET );
1519 res = getdents64( fd, buffer, size );
1523 de = (KERNEL_DIRENT64 *)buffer;
1524 return de->d_ino ? de->d_name : NULL;
1527 /***********************************************************************
1528 * read_directory_getdents
1530 * Read a directory using the Linux getdents64 system call; helper for NtQueryDirectoryFile.
1532 static int read_directory_getdents( int fd, IO_STATUS_BLOCK *io, void *buffer, ULONG length,
1533 BOOLEAN single_entry, const UNICODE_STRING *mask,
1534 BOOLEAN restart_scan, FILE_INFORMATION_CLASS class )
1536 static off_t second_entry_pos;
1537 static struct file_identity last_dir_id;
1538 off_t old_pos = 0, next_pos;
1539 size_t size = length;
1540 char *data, local_buffer[8192];
1541 KERNEL_DIRENT64 *de, *de_first_two = NULL;
1542 union file_directory_info *info, *last_info = NULL;
1543 const char *filename;
1544 BOOL data_buffer_changed;
1547 if (size <= sizeof(local_buffer) || !(data = RtlAllocateHeap( GetProcessHeap(), 0, size )))
1549 size = sizeof(local_buffer);
1550 data = local_buffer;
1553 if (restart_scan) lseek( fd, 0, SEEK_SET );
1556 old_pos = lseek( fd, 0, SEEK_CUR );
1559 io->u.Status = (errno == ENOENT) ? STATUS_NO_MORE_FILES : FILE_GetNtStatus();
1565 io->u.Status = STATUS_SUCCESS;
1566 de = (KERNEL_DIRENT64 *)data;
1568 /* if old_pos is not 0 we don't know how many entries have been returned already,
1569 * so maintain second_entry_pos to know when to return '..' */
1570 if (old_pos != 0 && (last_dir_id.dev != curdir.dev || last_dir_id.ino != curdir.ino))
1572 lseek( fd, 0, SEEK_SET );
1573 res = getdents64( fd, data, size );
1576 second_entry_pos = de->d_off;
1577 last_dir_id = curdir;
1579 lseek( fd, old_pos, SEEK_SET );
1582 res = getdents64( fd, data, size );
1585 if (errno != ENOSYS)
1587 io->u.Status = FILE_GetNtStatus();
1593 if (old_pos == 0 && res > 0)
1595 second_entry_pos = de->d_off;
1596 last_dir_id = curdir;
1597 if (res > de->d_reclen)
1603 res -= de->d_reclen;
1604 next_pos = de->d_off;
1607 /* we must return first 2 entries as "." and "..", but getdents64()
1608 * can return them anywhere, so swap first entries with "." and ".." */
1611 else if (old_pos == second_entry_pos)
1613 else if (!strcmp( de->d_name, "." ) || !strcmp( de->d_name, ".." ))
1615 swap_to = !strcmp( de->d_name, "." ) ? 0 : 1;
1616 data_buffer_changed = FALSE;
1618 filename = read_first_dent_name( swap_to, fd, second_entry_pos, de_first_two,
1619 data, size, &data_buffer_changed );
1620 if (filename != NULL && (!strcmp( filename, "." ) || !strcmp( filename, ".." )))
1621 filename = read_first_dent_name( swap_to ^ 1, fd, second_entry_pos, NULL,
1622 data, size, &data_buffer_changed );
1623 if (data_buffer_changed)
1625 lseek( fd, next_pos, SEEK_SET );
1630 filename = de->d_name;
1632 if (filename && (info = append_entry( buffer, io, length, filename, NULL, mask, class )))
1635 if (io->u.Status == STATUS_BUFFER_OVERFLOW)
1637 lseek( fd, old_pos, SEEK_SET ); /* restore pos to previous entry */
1640 /* check if we still have enough space for the largest possible entry */
1641 if (single_entry || io->Information + max_dir_info_size(class) > length)
1643 if (res > 0) lseek( fd, next_pos, SEEK_SET ); /* set pos to next entry */
1648 /* move on to the next entry */
1649 if (res > 0) de = (KERNEL_DIRENT64 *)((char *)de + de->d_reclen);
1652 res = getdents64( fd, data, size );
1653 de = (KERNEL_DIRENT64 *)data;
1654 de_first_two = NULL;
1658 if (last_info) last_info->next = 0;
1659 else io->u.Status = restart_scan ? STATUS_NO_SUCH_FILE : STATUS_NO_MORE_FILES;
1662 if (data != local_buffer) RtlFreeHeap( GetProcessHeap(), 0, data );
1666 #elif defined HAVE_GETDIRENTRIES
1668 #ifdef _DARWIN_FEATURE_64_BIT_INODE
1670 /* Darwin doesn't provide a version of getdirentries with support for 64-bit
1671 * inodes. When 64-bit inodes are enabled, the getdirentries symbol is mapped
1672 * to _getdirentries_is_not_available_when_64_bit_inodes_are_in_effect so that
1673 * we get link errors if we try to use it. We still need getdirentries, but we
1674 * don't need it to support 64-bit inodes. So, we use the legacy getdirentries
1675 * with 32-bit inodes. We have to be careful to use a corresponding dirent
1678 int darwin_legacy_getdirentries(int, char *, int, long *) __asm("_getdirentries");
1679 #define getdirentries darwin_legacy_getdirentries
1681 struct darwin_legacy_dirent {
1683 __uint16_t d_reclen;
1686 char d_name[__DARWIN_MAXNAMLEN + 1];
1688 #define dirent darwin_legacy_dirent
1692 /***********************************************************************
1693 * wine_getdirentries
1695 * Wrapper for the BSD getdirentries system call to fix a bug in the
1696 * Mac OS X version. For some file systems (at least Apple Filing
1697 * Protocol a.k.a. AFP), getdirentries resets the file position to 0
1698 * when it's about to return 0 (no more entries). So, a subsequent
1699 * getdirentries call starts over at the beginning again, causing an
1702 static inline int wine_getdirentries(int fd, char *buf, int nbytes, long *basep)
1704 int res = getdirentries(fd, buf, nbytes, basep);
1707 lseek(fd, *basep, SEEK_SET);
1712 /***********************************************************************
1713 * read_directory_getdirentries
1715 * Read a directory using the BSD getdirentries system call; helper for NtQueryDirectoryFile.
1717 static int read_directory_getdirentries( int fd, IO_STATUS_BLOCK *io, void *buffer, ULONG length,
1718 BOOLEAN single_entry, const UNICODE_STRING *mask,
1719 BOOLEAN restart_scan, FILE_INFORMATION_CLASS class )
1722 ULONG_PTR restart_info_pos = 0;
1723 size_t size, initial_size = length;
1724 int res, fake_dot_dot = 1;
1725 char *data, local_buffer[8192];
1727 union file_directory_info *info, *last_info = NULL, *restart_last_info = NULL;
1729 size = initial_size;
1730 data = local_buffer;
1731 if (size > sizeof(local_buffer) && !(data = RtlAllocateHeap( GetProcessHeap(), 0, size )))
1733 io->u.Status = STATUS_NO_MEMORY;
1734 return io->u.Status;
1737 if (restart_scan) lseek( fd, 0, SEEK_SET );
1739 io->u.Status = STATUS_SUCCESS;
1741 /* FIXME: should make sure size is larger than filesystem block size */
1742 res = wine_getdirentries( fd, data, size, &restart_pos );
1745 io->u.Status = FILE_GetNtStatus();
1750 de = (struct dirent *)data;
1754 /* check if we got . and .. from getdirentries */
1757 if (!strcmp( de->d_name, "." ) && res > de->d_reclen)
1759 struct dirent *next_de = (struct dirent *)(data + de->d_reclen);
1760 if (!strcmp( next_de->d_name, ".." )) fake_dot_dot = 0;
1763 /* make sure we have enough room for both entries */
1766 const ULONG min_info_size = dir_info_size( class, 1 ) + dir_info_size( class, 2 );
1767 if (length < min_info_size || single_entry)
1769 FIXME( "not enough room %u/%u for fake . and .. entries\n", length, single_entry );
1776 if ((info = append_entry( buffer, io, length, ".", NULL, mask, class )))
1778 if ((info = append_entry( buffer, io, length, "..", NULL, mask, class )))
1781 restart_last_info = last_info;
1782 restart_info_pos = io->Information;
1784 /* check if we still have enough space for the largest possible entry */
1785 if (last_info && io->Information + max_dir_info_size(class) > length)
1787 lseek( fd, 0, SEEK_SET ); /* reset pos to first entry */
1795 res -= de->d_reclen;
1797 !(fake_dot_dot && (!strcmp( de->d_name, "." ) || !strcmp( de->d_name, ".." ))) &&
1798 ((info = append_entry( buffer, io, length, de->d_name, NULL, mask, class ))))
1801 if (io->u.Status == STATUS_BUFFER_OVERFLOW)
1803 lseek( fd, (unsigned long)restart_pos, SEEK_SET );
1804 if (restart_info_pos) /* if we have a complete read already, return it */
1806 io->u.Status = STATUS_SUCCESS;
1807 io->Information = restart_info_pos;
1808 last_info = restart_last_info;
1811 /* otherwise restart from the start with a smaller size */
1812 size = (char *)de - data;
1814 io->Information = 0;
1818 /* if we have to return but the buffer contains more data, restart with a smaller size */
1819 if (res > 0 && (single_entry || io->Information + max_dir_info_size(class) > length))
1821 lseek( fd, (unsigned long)restart_pos, SEEK_SET );
1822 size = (char *)de + de->d_reclen - data;
1823 io->Information = restart_info_pos;
1824 last_info = restart_last_info;
1828 /* move on to the next entry */
1831 de = (struct dirent *)((char *)de + de->d_reclen);
1834 if (size < initial_size) break; /* already restarted once, give up now */
1835 size = min( size, length - io->Information );
1836 /* if size is too small don't bother to continue */
1837 if (size < max_dir_info_size(class) && last_info) break;
1838 restart_last_info = last_info;
1839 restart_info_pos = io->Information;
1841 res = wine_getdirentries( fd, data, size, &restart_pos );
1842 de = (struct dirent *)data;
1845 if (last_info) last_info->next = 0;
1846 else io->u.Status = restart_scan ? STATUS_NO_SUCH_FILE : STATUS_NO_MORE_FILES;
1849 if (data != local_buffer) RtlFreeHeap( GetProcessHeap(), 0, data );
1853 #ifdef _DARWIN_FEATURE_64_BIT_INODE
1854 #undef getdirentries
1858 #endif /* HAVE_GETDIRENTRIES */
1861 /***********************************************************************
1862 * read_directory_readdir
1864 * Read a directory using the POSIX readdir interface; helper for NtQueryDirectoryFile.
1866 static void read_directory_readdir( int fd, IO_STATUS_BLOCK *io, void *buffer, ULONG length,
1867 BOOLEAN single_entry, const UNICODE_STRING *mask,
1868 BOOLEAN restart_scan, FILE_INFORMATION_CLASS class )
1871 off_t i, old_pos = 0;
1873 union file_directory_info *info, *last_info = NULL;
1875 if (!(dir = opendir( "." )))
1877 io->u.Status = FILE_GetNtStatus();
1883 old_pos = lseek( fd, 0, SEEK_CUR );
1884 /* skip the right number of entries */
1885 for (i = 0; i < old_pos - 2; i++)
1887 if (!readdir( dir ))
1890 io->u.Status = STATUS_NO_MORE_FILES;
1895 io->u.Status = STATUS_SUCCESS;
1900 info = append_entry( buffer, io, length, ".", NULL, mask, class );
1901 else if (old_pos == 1)
1902 info = append_entry( buffer, io, length, "..", NULL, mask, class );
1903 else if ((de = readdir( dir )))
1905 if (strcmp( de->d_name, "." ) && strcmp( de->d_name, ".." ))
1906 info = append_entry( buffer, io, length, de->d_name, NULL, mask, class );
1916 if (io->u.Status == STATUS_BUFFER_OVERFLOW)
1918 old_pos--; /* restore pos to previous entry */
1921 if (single_entry) break;
1922 /* check if we still have enough space for the largest possible entry */
1923 if (io->Information + max_dir_info_size(class) > length) break;
1927 lseek( fd, old_pos, SEEK_SET ); /* store dir offset as filepos for fd */
1930 if (last_info) last_info->next = 0;
1931 else io->u.Status = restart_scan ? STATUS_NO_SUCH_FILE : STATUS_NO_MORE_FILES;
1934 /***********************************************************************
1935 * read_directory_stat
1937 * Read a single file from a directory by determining whether the file
1938 * identified by mask exists using stat.
1940 static int read_directory_stat( int fd, IO_STATUS_BLOCK *io, void *buffer, ULONG length,
1941 BOOLEAN single_entry, const UNICODE_STRING *mask,
1942 BOOLEAN restart_scan, FILE_INFORMATION_CLASS class )
1944 int unix_len, ret, used_default;
1948 TRACE("trying optimisation for file %s\n", debugstr_us( mask ));
1950 unix_len = ntdll_wcstoumbs( 0, mask->Buffer, mask->Length / sizeof(WCHAR), NULL, 0, NULL, NULL );
1951 if (!(unix_name = RtlAllocateHeap( GetProcessHeap(), 0, unix_len + 1)))
1953 io->u.Status = STATUS_NO_MEMORY;
1956 ret = ntdll_wcstoumbs( 0, mask->Buffer, mask->Length / sizeof(WCHAR), unix_name, unix_len,
1957 NULL, &used_default );
1958 if (ret > 0 && !used_default)
1963 lseek( fd, 0, SEEK_SET );
1965 else if (lseek( fd, 0, SEEK_CUR ) != 0)
1967 io->u.Status = STATUS_NO_MORE_FILES;
1972 ret = stat( unix_name, &st );
1975 union file_directory_info *info = append_entry( buffer, io, length, unix_name, NULL, NULL, class );
1979 if (io->u.Status != STATUS_BUFFER_OVERFLOW) lseek( fd, 1, SEEK_CUR );
1981 else io->u.Status = STATUS_NO_MORE_FILES;
1987 RtlFreeHeap( GetProcessHeap(), 0, unix_name );
1989 TRACE("returning %d\n", ret);
1995 static inline WCHAR *mempbrkW( const WCHAR *ptr, const WCHAR *accept, size_t n )
1998 for (end = ptr + n; ptr < end; ptr++) if (strchrW( accept, *ptr )) return (WCHAR *)ptr;
2002 /******************************************************************************
2003 * NtQueryDirectoryFile [NTDLL.@]
2004 * ZwQueryDirectoryFile [NTDLL.@]
2006 NTSTATUS WINAPI NtQueryDirectoryFile( HANDLE handle, HANDLE event,
2007 PIO_APC_ROUTINE apc_routine, PVOID apc_context,
2008 PIO_STATUS_BLOCK io,
2009 PVOID buffer, ULONG length,
2010 FILE_INFORMATION_CLASS info_class,
2011 BOOLEAN single_entry,
2012 PUNICODE_STRING mask,
2013 BOOLEAN restart_scan )
2015 int cwd, fd, needs_close;
2016 static const WCHAR wszWildcards[] = { '*','?',0 };
2018 TRACE("(%p %p %p %p %p %p 0x%08x 0x%08x 0x%08x %s 0x%08x\n",
2019 handle, event, apc_routine, apc_context, io, buffer,
2020 length, info_class, single_entry, debugstr_us(mask),
2023 if (event || apc_routine)
2025 FIXME( "Unsupported yet option\n" );
2026 return io->u.Status = STATUS_NOT_IMPLEMENTED;
2030 case FileDirectoryInformation:
2031 case FileBothDirectoryInformation:
2032 case FileFullDirectoryInformation:
2033 case FileIdBothDirectoryInformation:
2034 case FileIdFullDirectoryInformation:
2035 if (length < dir_info_size( info_class, 1 )) return io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
2038 FIXME( "Unsupported file info class %d\n", info_class );
2039 return io->u.Status = STATUS_NOT_IMPLEMENTED;
2042 if ((io->u.Status = server_get_unix_fd( handle, FILE_LIST_DIRECTORY, &fd, &needs_close, NULL, NULL )) != STATUS_SUCCESS)
2043 return io->u.Status;
2045 io->Information = 0;
2047 RtlEnterCriticalSection( &dir_section );
2049 if (show_dot_files == -1) init_options();
2051 cwd = open( ".", O_RDONLY );
2052 if (fchdir( fd ) != -1)
2056 curdir.dev = st.st_dev;
2057 curdir.ino = st.st_ino;
2058 #ifdef VFAT_IOCTL_READDIR_BOTH
2059 if ((read_directory_vfat( fd, io, buffer, length, single_entry,
2060 mask, restart_scan, info_class )) != -1) goto done;
2062 if (mask && !mempbrkW( mask->Buffer, wszWildcards, mask->Length / sizeof(WCHAR) ) &&
2063 read_directory_stat( fd, io, buffer, length, single_entry,
2064 mask, restart_scan, info_class ) != -1) goto done;
2066 if ((read_directory_getdents( fd, io, buffer, length, single_entry,
2067 mask, restart_scan, info_class )) != -1) goto done;
2068 #elif defined HAVE_GETDIRENTRIES
2069 if ((read_directory_getdirentries( fd, io, buffer, length, single_entry,
2070 mask, restart_scan, info_class )) != -1) goto done;
2072 read_directory_readdir( fd, io, buffer, length, single_entry, mask, restart_scan, info_class );
2075 if (cwd == -1 || fchdir( cwd ) == -1) chdir( "/" );
2077 else io->u.Status = FILE_GetNtStatus();
2079 RtlLeaveCriticalSection( &dir_section );
2081 if (needs_close) close( fd );
2082 if (cwd != -1) close( cwd );
2083 TRACE( "=> %x (%ld)\n", io->u.Status, io->Information );
2084 return io->u.Status;
2088 /***********************************************************************
2091 * Find a file in a directory the hard way, by doing a case-insensitive search.
2092 * The file found is appended to unix_name at pos.
2093 * There must be at least MAX_DIR_ENTRY_LEN+2 chars available at pos.
2095 static NTSTATUS find_file_in_dir( char *unix_name, int pos, const WCHAR *name, int length,
2096 int check_case, int *is_win_dir )
2098 WCHAR buffer[MAX_DIR_ENTRY_LEN];
2104 int ret, used_default, is_name_8_dot_3;
2106 /* try a shortcut for this directory */
2108 unix_name[pos++] = '/';
2109 ret = ntdll_wcstoumbs( 0, name, length, unix_name + pos, MAX_DIR_ENTRY_LEN,
2110 NULL, &used_default );
2111 /* if we used the default char, the Unix name won't round trip properly back to Unicode */
2112 /* so it cannot match the file we are looking for */
2113 if (ret >= 0 && !used_default)
2115 unix_name[pos + ret] = 0;
2116 if (!stat( unix_name, &st ))
2118 if (is_win_dir) *is_win_dir = is_same_file( &windir, &st );
2119 return STATUS_SUCCESS;
2122 if (check_case) goto not_found; /* we want an exact match */
2124 if (pos > 1) unix_name[pos - 1] = 0;
2125 else unix_name[1] = 0; /* keep the initial slash */
2127 /* check if it fits in 8.3 so that we don't look for short names if we won't need them */
2129 str.Buffer = (WCHAR *)name;
2130 str.Length = length * sizeof(WCHAR);
2131 str.MaximumLength = str.Length;
2132 is_name_8_dot_3 = RtlIsNameLegalDOS8Dot3( &str, NULL, &spaces ) && !spaces;
2134 if (!is_name_8_dot_3 && !get_dir_case_sensitivity( unix_name )) goto not_found;
2136 /* now look for it through the directory */
2138 #ifdef VFAT_IOCTL_READDIR_BOTH
2139 if (is_name_8_dot_3)
2141 int fd = open( unix_name, O_RDONLY | O_DIRECTORY );
2146 RtlEnterCriticalSection( &dir_section );
2147 if ((kde = start_vfat_ioctl( fd )))
2149 unix_name[pos - 1] = '/';
2150 while (kde[0].d_reclen)
2152 /* make sure names are null-terminated to work around an x86-64 kernel bug */
2153 size_t len = min(kde[0].d_reclen, sizeof(kde[0].d_name) - 1 );
2154 kde[0].d_name[len] = 0;
2155 len = min(kde[1].d_reclen, sizeof(kde[1].d_name) - 1 );
2156 kde[1].d_name[len] = 0;
2158 if (kde[1].d_name[0])
2160 ret = ntdll_umbstowcs( 0, kde[1].d_name, strlen(kde[1].d_name),
2161 buffer, MAX_DIR_ENTRY_LEN );
2162 if (ret == length && !memicmpW( buffer, name, length))
2164 strcpy( unix_name + pos, kde[1].d_name );
2165 RtlLeaveCriticalSection( &dir_section );
2170 ret = ntdll_umbstowcs( 0, kde[0].d_name, strlen(kde[0].d_name),
2171 buffer, MAX_DIR_ENTRY_LEN );
2172 if (ret == length && !memicmpW( buffer, name, length))
2174 strcpy( unix_name + pos,
2175 kde[1].d_name[0] ? kde[1].d_name : kde[0].d_name );
2176 RtlLeaveCriticalSection( &dir_section );
2180 if (ioctl( fd, VFAT_IOCTL_READDIR_BOTH, (long)kde ) == -1)
2182 RtlLeaveCriticalSection( &dir_section );
2188 RtlLeaveCriticalSection( &dir_section );
2191 /* fall through to normal handling */
2193 #endif /* VFAT_IOCTL_READDIR_BOTH */
2195 if (!(dir = opendir( unix_name )))
2197 if (errno == ENOENT) return STATUS_OBJECT_PATH_NOT_FOUND;
2198 else return FILE_GetNtStatus();
2200 unix_name[pos - 1] = '/';
2201 str.Buffer = buffer;
2202 str.MaximumLength = sizeof(buffer);
2203 while ((de = readdir( dir )))
2205 ret = ntdll_umbstowcs( 0, de->d_name, strlen(de->d_name), buffer, MAX_DIR_ENTRY_LEN );
2206 if (ret == length && !memicmpW( buffer, name, length ))
2208 strcpy( unix_name + pos, de->d_name );
2213 if (!is_name_8_dot_3) continue;
2215 str.Length = ret * sizeof(WCHAR);
2216 if (!RtlIsNameLegalDOS8Dot3( &str, NULL, &spaces ) || spaces)
2218 WCHAR short_nameW[12];
2219 ret = hash_short_file_name( &str, short_nameW );
2220 if (ret == length && !memicmpW( short_nameW, name, length ))
2222 strcpy( unix_name + pos, de->d_name );
2231 unix_name[pos - 1] = 0;
2232 return STATUS_OBJECT_PATH_NOT_FOUND;
2235 if (is_win_dir && !stat( unix_name, &st )) *is_win_dir = is_same_file( &windir, &st );
2236 return STATUS_SUCCESS;
2242 static const WCHAR catrootW[] = {'s','y','s','t','e','m','3','2','\\','c','a','t','r','o','o','t',0};
2243 static const WCHAR catroot2W[] = {'s','y','s','t','e','m','3','2','\\','c','a','t','r','o','o','t','2',0};
2244 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};
2245 static const WCHAR driversetcW[] = {'s','y','s','t','e','m','3','2','\\','d','r','i','v','e','r','s','\\','e','t','c',0};
2246 static const WCHAR logfilesW[] = {'s','y','s','t','e','m','3','2','\\','l','o','g','f','i','l','e','s',0};
2247 static const WCHAR spoolW[] = {'s','y','s','t','e','m','3','2','\\','s','p','o','o','l',0};
2248 static const WCHAR system32W[] = {'s','y','s','t','e','m','3','2',0};
2249 static const WCHAR syswow64W[] = {'s','y','s','w','o','w','6','4',0};
2250 static const WCHAR sysnativeW[] = {'s','y','s','n','a','t','i','v','e',0};
2251 static const WCHAR regeditW[] = {'r','e','g','e','d','i','t','.','e','x','e',0};
2252 static const WCHAR wow_regeditW[] = {'s','y','s','w','o','w','6','4','\\','r','e','g','e','d','i','t','.','e','x','e',0};
2256 const WCHAR *source;
2257 const WCHAR *dos_target;
2258 const char *unix_target;
2261 { catrootW, NULL, NULL },
2262 { catroot2W, NULL, NULL },
2263 { driversstoreW, NULL, NULL },
2264 { driversetcW, NULL, NULL },
2265 { logfilesW, NULL, NULL },
2266 { spoolW, NULL, NULL },
2267 { system32W, syswow64W, NULL },
2268 { sysnativeW, system32W, NULL },
2269 { regeditW, wow_regeditW, NULL }
2272 static unsigned int nb_redirects;
2275 /***********************************************************************
2276 * get_redirect_target
2278 * Find the target unix name for a redirected dir.
2280 static const char *get_redirect_target( const char *windows_dir, const WCHAR *name )
2282 int used_default, len, pos, win_len = strlen( windows_dir );
2283 char *unix_name, *unix_target = NULL;
2286 if (!(unix_name = RtlAllocateHeap( GetProcessHeap(), 0, win_len + MAX_DIR_ENTRY_LEN + 2 )))
2288 memcpy( unix_name, windows_dir, win_len );
2293 const WCHAR *end, *next;
2295 for (end = name; *end; end++) if (IS_SEPARATOR(*end)) break;
2296 for (next = end; *next; next++) if (!IS_SEPARATOR(*next)) break;
2298 status = find_file_in_dir( unix_name, pos, name, end - name, FALSE, NULL );
2299 if (status == STATUS_OBJECT_PATH_NOT_FOUND && !*next) /* not finding last element is ok */
2301 len = ntdll_wcstoumbs( 0, name, end - name, unix_name + pos + 1,
2302 MAX_DIR_ENTRY_LEN - (pos - win_len), NULL, &used_default );
2303 if (len > 0 && !used_default)
2305 unix_name[pos] = '/';
2311 if (status) goto done;
2312 pos += strlen( unix_name + pos );
2316 if ((unix_target = RtlAllocateHeap( GetProcessHeap(), 0, pos - win_len )))
2317 memcpy( unix_target, unix_name + win_len + 1, pos - win_len );
2320 RtlFreeHeap( GetProcessHeap(), 0, unix_name );
2325 /***********************************************************************
2328 static void init_redirects(void)
2330 UNICODE_STRING nt_name;
2331 ANSI_STRING unix_name;
2336 if (!RtlDosPathNameToNtPathName_U( user_shared_data->NtSystemRoot, &nt_name, NULL, NULL ))
2338 ERR( "can't convert %s\n", debugstr_w(user_shared_data->NtSystemRoot) );
2341 status = wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN_IF, FALSE );
2342 RtlFreeUnicodeString( &nt_name );
2345 ERR( "cannot open %s (%x)\n", debugstr_w(user_shared_data->NtSystemRoot), status );
2348 if (!stat( unix_name.Buffer, &st ))
2350 windir.dev = st.st_dev;
2351 windir.ino = st.st_ino;
2352 nb_redirects = sizeof(redirects) / sizeof(redirects[0]);
2353 for (i = 0; i < nb_redirects; i++)
2355 if (!redirects[i].dos_target) continue;
2356 redirects[i].unix_target = get_redirect_target( unix_name.Buffer, redirects[i].dos_target );
2357 TRACE( "%s -> %s\n", debugstr_w(redirects[i].source), redirects[i].unix_target );
2360 RtlFreeAnsiString( &unix_name );
2365 /***********************************************************************
2368 * Check if path matches a redirect name. If yes, return matched length.
2370 static int match_redirect( const WCHAR *path, int len, const WCHAR *redir, int check_case )
2374 while (i < len && *redir)
2376 if (IS_SEPARATOR(path[i]))
2378 if (*redir++ != '\\') return 0;
2379 while (i < len && IS_SEPARATOR(path[i])) i++;
2380 continue; /* move on to next path component */
2382 else if (check_case)
2384 if (path[i] != *redir) return 0;
2388 if (tolowerW(path[i]) != tolowerW(*redir)) return 0;
2393 if (*redir) return 0;
2394 if (i < len && !IS_SEPARATOR(path[i])) return 0;
2395 while (i < len && IS_SEPARATOR(path[i])) i++;
2400 /***********************************************************************
2403 * Retrieve the Unix path corresponding to a redirected path if any.
2405 static int get_redirect_path( char *unix_name, int pos, const WCHAR *name, int length, int check_case )
2410 for (i = 0; i < nb_redirects; i++)
2412 if ((len = match_redirect( name, length, redirects[i].source, check_case )))
2414 if (!redirects[i].unix_target) break;
2415 unix_name[pos++] = '/';
2416 strcpy( unix_name + pos, redirects[i].unix_target );
2425 /* there are no redirects on 64-bit */
2427 static const unsigned int nb_redirects = 0;
2429 static int get_redirect_path( char *unix_name, int pos, const WCHAR *name, int length, int check_case )
2436 /***********************************************************************
2437 * DIR_init_windows_dir
2439 void DIR_init_windows_dir( const WCHAR *win, const WCHAR *sys )
2441 /* FIXME: should probably store paths as NT file names */
2443 RtlCreateUnicodeString( &system_dir, sys );
2446 if (is_wow64) init_redirects();
2451 /******************************************************************************
2454 * Get the Unix path of a DOS device.
2456 static NTSTATUS get_dos_device( const WCHAR *name, UINT name_len, ANSI_STRING *unix_name_ret )
2458 const char *config_dir = wine_get_config_dir();
2460 char *unix_name, *new_name, *dev;
2464 /* make sure the device name is ASCII */
2465 for (i = 0; i < name_len; i++)
2466 if (name[i] <= 32 || name[i] >= 127) return STATUS_BAD_DEVICE_TYPE;
2468 unix_len = strlen(config_dir) + sizeof("/dosdevices/") + name_len + 1;
2470 if (!(unix_name = RtlAllocateHeap( GetProcessHeap(), 0, unix_len )))
2471 return STATUS_NO_MEMORY;
2473 strcpy( unix_name, config_dir );
2474 strcat( unix_name, "/dosdevices/" );
2475 dev = unix_name + strlen(unix_name);
2477 for (i = 0; i < name_len; i++) dev[i] = (char)tolowerW(name[i]);
2480 /* special case for drive devices */
2481 if (name_len == 2 && dev[1] == ':')
2489 if (!stat( unix_name, &st ))
2491 TRACE( "%s -> %s\n", debugstr_wn(name,name_len), debugstr_a(unix_name) );
2492 unix_name_ret->Buffer = unix_name;
2493 unix_name_ret->Length = strlen(unix_name);
2494 unix_name_ret->MaximumLength = unix_len;
2495 return STATUS_SUCCESS;
2499 /* now try some defaults for it */
2500 if (!strcmp( dev, "aux" ))
2502 strcpy( dev, "com1" );
2505 if (!strcmp( dev, "prn" ))
2507 strcpy( dev, "lpt1" );
2510 if (!strcmp( dev, "nul" ))
2512 strcpy( unix_name, "/dev/null" );
2513 dev = NULL; /* last try */
2518 if (dev[1] == ':' && dev[2] == ':') /* drive device */
2520 dev[2] = 0; /* remove last ':' to get the drive mount point symlink */
2521 new_name = get_default_drive_device( unix_name );
2523 else if (!strncmp( dev, "com", 3 )) new_name = get_default_com_device( atoi(dev + 3 ));
2524 else if (!strncmp( dev, "lpt", 3 )) new_name = get_default_lpt_device( atoi(dev + 3 ));
2526 if (!new_name) break;
2528 RtlFreeHeap( GetProcessHeap(), 0, unix_name );
2529 unix_name = new_name;
2530 unix_len = strlen(unix_name) + 1;
2531 dev = NULL; /* last try */
2533 RtlFreeHeap( GetProcessHeap(), 0, unix_name );
2534 return STATUS_BAD_DEVICE_TYPE;
2538 /* return the length of the DOS namespace prefix if any */
2539 static inline int get_dos_prefix_len( const UNICODE_STRING *name )
2541 static const WCHAR nt_prefixW[] = {'\\','?','?','\\'};
2542 static const WCHAR dosdev_prefixW[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\'};
2544 if (name->Length > sizeof(nt_prefixW) &&
2545 !memcmp( name->Buffer, nt_prefixW, sizeof(nt_prefixW) ))
2546 return sizeof(nt_prefixW) / sizeof(WCHAR);
2548 if (name->Length > sizeof(dosdev_prefixW) &&
2549 !memicmpW( name->Buffer, dosdev_prefixW, sizeof(dosdev_prefixW)/sizeof(WCHAR) ))
2550 return sizeof(dosdev_prefixW) / sizeof(WCHAR);
2556 /******************************************************************************
2559 * Recursively search directories from the dir queue for a given inode.
2561 static NTSTATUS find_file_id( ANSI_STRING *unix_name, ULONGLONG file_id, dev_t dev )
2569 while (!(status = next_dir_in_queue( unix_name->Buffer )))
2571 if (!(dir = opendir( unix_name->Buffer ))) continue;
2572 TRACE( "searching %s for %s\n", unix_name->Buffer, wine_dbgstr_longlong(file_id) );
2573 pos = strlen( unix_name->Buffer );
2574 if (pos + MAX_DIR_ENTRY_LEN >= unix_name->MaximumLength/sizeof(WCHAR))
2576 char *new = RtlReAllocateHeap( GetProcessHeap(), 0, unix_name->Buffer,
2577 unix_name->MaximumLength * 2 );
2581 return STATUS_NO_MEMORY;
2583 unix_name->MaximumLength *= 2;
2584 unix_name->Buffer = new;
2586 unix_name->Buffer[pos++] = '/';
2587 while ((de = readdir( dir )))
2589 if (!strcmp( de->d_name, "." ) || !strcmp( de->d_name, ".." )) continue;
2590 strcpy( unix_name->Buffer + pos, de->d_name );
2591 if (lstat( unix_name->Buffer, &st ) == -1) continue;
2592 if (st.st_dev != dev) continue;
2593 if (st.st_ino == file_id)
2596 return STATUS_SUCCESS;
2598 if (!S_ISDIR( st.st_mode )) continue;
2599 if ((status = add_dir_to_queue( unix_name->Buffer )) != STATUS_SUCCESS)
2611 /******************************************************************************
2612 * file_id_to_unix_file_name
2614 * Lookup a file from its file id instead of its name.
2616 NTSTATUS file_id_to_unix_file_name( const OBJECT_ATTRIBUTES *attr, ANSI_STRING *unix_name )
2618 enum server_fd_type type;
2619 int old_cwd, root_fd, needs_close;
2622 struct stat st, root_st;
2624 if (attr->ObjectName->Length != sizeof(ULONGLONG)) return STATUS_OBJECT_PATH_SYNTAX_BAD;
2625 if (!attr->RootDirectory) return STATUS_INVALID_PARAMETER;
2626 memcpy( &file_id, attr->ObjectName->Buffer, sizeof(file_id) );
2628 unix_name->MaximumLength = 2 * MAX_DIR_ENTRY_LEN + 4;
2629 if (!(unix_name->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, unix_name->MaximumLength )))
2630 return STATUS_NO_MEMORY;
2631 strcpy( unix_name->Buffer, "." );
2633 if ((status = server_get_unix_fd( attr->RootDirectory, 0, &root_fd, &needs_close, &type, NULL )))
2636 if (type != FD_TYPE_DIR)
2638 status = STATUS_OBJECT_TYPE_MISMATCH;
2642 fstat( root_fd, &root_st );
2643 if (root_st.st_ino == file_id) /* shortcut for "." */
2645 status = STATUS_SUCCESS;
2649 RtlEnterCriticalSection( &dir_section );
2650 if ((old_cwd = open( ".", O_RDONLY )) != -1 && fchdir( root_fd ) != -1)
2652 /* shortcut for ".." */
2653 if (!stat( "..", &st ) && st.st_dev == root_st.st_dev && st.st_ino == file_id)
2655 strcpy( unix_name->Buffer, ".." );
2656 status = STATUS_SUCCESS;
2660 status = add_dir_to_queue( "." );
2662 status = find_file_id( unix_name, file_id, root_st.st_dev );
2663 if (!status) /* get rid of "./" prefix */
2664 memmove( unix_name->Buffer, unix_name->Buffer + 2, strlen(unix_name->Buffer) - 1 );
2667 if (fchdir( old_cwd ) == -1) chdir( "/" );
2669 else status = FILE_GetNtStatus();
2670 RtlLeaveCriticalSection( &dir_section );
2671 if (old_cwd != -1) close( old_cwd );
2674 if (status == STATUS_SUCCESS)
2676 TRACE( "%s -> %s\n", wine_dbgstr_longlong(file_id), debugstr_a(unix_name->Buffer) );
2677 unix_name->Length = strlen( unix_name->Buffer );
2681 TRACE( "%s not found in dir %p\n", wine_dbgstr_longlong(file_id), attr->RootDirectory );
2682 RtlFreeHeap( GetProcessHeap(), 0, unix_name->Buffer );
2684 if (needs_close) close( root_fd );
2689 /******************************************************************************
2692 * Helper for nt_to_unix_file_name
2694 static NTSTATUS lookup_unix_name( const WCHAR *name, int name_len, char **buffer, int unix_len, int pos,
2695 UINT disposition, BOOLEAN check_case )
2698 int ret, used_default, len;
2700 char *unix_name = *buffer;
2701 const BOOL redirect = nb_redirects && ntdll_get_thread_data()->wow64_redir;
2703 /* try a shortcut first */
2705 ret = ntdll_wcstoumbs( 0, name, name_len, unix_name + pos, unix_len - pos - 1,
2706 NULL, &used_default );
2708 while (name_len && IS_SEPARATOR(*name))
2714 if (ret >= 0 && !used_default) /* if we used the default char the name didn't convert properly */
2717 unix_name[pos + ret] = 0;
2718 for (p = unix_name + pos ; *p; p++) if (*p == '\\') *p = '/';
2719 if (!redirect || (!strstr( unix_name, "/windows/") && strncmp( unix_name, "windows/", 8 )))
2721 if (!stat( unix_name, &st ))
2723 /* creation fails with STATUS_ACCESS_DENIED for the root of the drive */
2724 if (disposition == FILE_CREATE)
2725 return name_len ? STATUS_OBJECT_NAME_COLLISION : STATUS_ACCESS_DENIED;
2726 return STATUS_SUCCESS;
2731 if (!name_len) /* empty name -> drive root doesn't exist */
2732 return STATUS_OBJECT_PATH_NOT_FOUND;
2733 if (check_case && !redirect && (disposition == FILE_OPEN || disposition == FILE_OVERWRITE))
2734 return STATUS_OBJECT_NAME_NOT_FOUND;
2736 /* now do it component by component */
2740 const WCHAR *end, *next;
2744 while (end < name + name_len && !IS_SEPARATOR(*end)) end++;
2746 while (next < name + name_len && IS_SEPARATOR(*next)) next++;
2747 name_len -= next - name;
2749 /* grow the buffer if needed */
2751 if (unix_len - pos < MAX_DIR_ENTRY_LEN + 2)
2754 unix_len += 2 * MAX_DIR_ENTRY_LEN;
2755 if (!(new_name = RtlReAllocateHeap( GetProcessHeap(), 0, unix_name, unix_len )))
2756 return STATUS_NO_MEMORY;
2757 unix_name = *buffer = new_name;
2760 status = find_file_in_dir( unix_name, pos, name, end - name,
2761 check_case, redirect ? &is_win_dir : NULL );
2763 /* if this is the last element, not finding it is not necessarily fatal */
2766 if (status == STATUS_OBJECT_PATH_NOT_FOUND)
2768 status = STATUS_OBJECT_NAME_NOT_FOUND;
2769 if (disposition != FILE_OPEN && disposition != FILE_OVERWRITE)
2771 ret = ntdll_wcstoumbs( 0, name, end - name, unix_name + pos + 1,
2772 MAX_DIR_ENTRY_LEN, NULL, &used_default );
2773 if (ret > 0 && !used_default)
2775 unix_name[pos] = '/';
2776 unix_name[pos + 1 + ret] = 0;
2777 status = STATUS_NO_SUCH_FILE;
2782 else if (status == STATUS_SUCCESS && disposition == FILE_CREATE)
2784 status = STATUS_OBJECT_NAME_COLLISION;
2788 if (status != STATUS_SUCCESS) break;
2790 pos += strlen( unix_name + pos );
2793 if (is_win_dir && (len = get_redirect_path( unix_name, pos, name, name_len, check_case )))
2797 pos += strlen( unix_name + pos );
2798 TRACE( "redirecting -> %s + %s\n", debugstr_a(unix_name), debugstr_w(name) );
2806 /******************************************************************************
2807 * nt_to_unix_file_name_attr
2809 NTSTATUS nt_to_unix_file_name_attr( const OBJECT_ATTRIBUTES *attr, ANSI_STRING *unix_name_ret,
2812 static const WCHAR invalid_charsW[] = { INVALID_NT_CHARS, 0 };
2813 enum server_fd_type type;
2814 int old_cwd, root_fd, needs_close;
2815 const WCHAR *name, *p;
2817 int name_len, unix_len;
2819 BOOLEAN check_case = !(attr->Attributes & OBJ_CASE_INSENSITIVE);
2821 if (!attr->RootDirectory) /* without root dir fall back to normal lookup */
2822 return wine_nt_to_unix_file_name( attr->ObjectName, unix_name_ret, disposition, check_case );
2824 name = attr->ObjectName->Buffer;
2825 name_len = attr->ObjectName->Length / sizeof(WCHAR);
2827 if (name_len && IS_SEPARATOR(name[0])) return STATUS_INVALID_PARAMETER;
2829 /* check for invalid characters */
2830 for (p = name; p < name + name_len; p++)
2831 if (*p < 32 || strchrW( invalid_charsW, *p )) return STATUS_OBJECT_NAME_INVALID;
2833 unix_len = ntdll_wcstoumbs( 0, name, name_len, NULL, 0, NULL, NULL );
2834 unix_len += MAX_DIR_ENTRY_LEN + 3;
2835 if (!(unix_name = RtlAllocateHeap( GetProcessHeap(), 0, unix_len )))
2836 return STATUS_NO_MEMORY;
2839 if (!(status = server_get_unix_fd( attr->RootDirectory, 0, &root_fd, &needs_close, &type, NULL )))
2841 if (type != FD_TYPE_DIR)
2843 if (needs_close) close( root_fd );
2844 status = STATUS_BAD_DEVICE_TYPE;
2848 RtlEnterCriticalSection( &dir_section );
2849 if ((old_cwd = open( ".", O_RDONLY )) != -1 && fchdir( root_fd ) != -1)
2851 status = lookup_unix_name( name, name_len, &unix_name, unix_len, 1,
2852 disposition, check_case );
2853 if (fchdir( old_cwd ) == -1) chdir( "/" );
2855 else status = FILE_GetNtStatus();
2856 RtlLeaveCriticalSection( &dir_section );
2857 if (old_cwd != -1) close( old_cwd );
2858 if (needs_close) close( root_fd );
2861 else if (status == STATUS_OBJECT_TYPE_MISMATCH) status = STATUS_BAD_DEVICE_TYPE;
2863 if (status == STATUS_SUCCESS || status == STATUS_NO_SUCH_FILE)
2865 TRACE( "%s -> %s\n", debugstr_us(attr->ObjectName), debugstr_a(unix_name) );
2866 unix_name_ret->Buffer = unix_name;
2867 unix_name_ret->Length = strlen(unix_name);
2868 unix_name_ret->MaximumLength = unix_len;
2872 TRACE( "%s not found in %s\n", debugstr_w(name), unix_name );
2873 RtlFreeHeap( GetProcessHeap(), 0, unix_name );
2879 /******************************************************************************
2880 * wine_nt_to_unix_file_name (NTDLL.@) Not a Windows API
2882 * Convert a file name from NT namespace to Unix namespace.
2884 * If disposition is not FILE_OPEN or FILE_OVERWRITE, the last path
2885 * element doesn't have to exist; in that case STATUS_NO_SUCH_FILE is
2886 * returned, but the unix name is still filled in properly.
2888 NTSTATUS CDECL wine_nt_to_unix_file_name( const UNICODE_STRING *nameW, ANSI_STRING *unix_name_ret,
2889 UINT disposition, BOOLEAN check_case )
2891 static const WCHAR unixW[] = {'u','n','i','x'};
2892 static const WCHAR invalid_charsW[] = { INVALID_NT_CHARS, 0 };
2894 NTSTATUS status = STATUS_SUCCESS;
2895 const char *config_dir = wine_get_config_dir();
2896 const WCHAR *name, *p;
2899 int pos, ret, name_len, unix_len, prefix_len, used_default;
2900 WCHAR prefix[MAX_DIR_ENTRY_LEN];
2901 BOOLEAN is_unix = FALSE;
2903 name = nameW->Buffer;
2904 name_len = nameW->Length / sizeof(WCHAR);
2906 if (!name_len || !IS_SEPARATOR(name[0])) return STATUS_OBJECT_PATH_SYNTAX_BAD;
2908 if (!(pos = get_dos_prefix_len( nameW )))
2909 return STATUS_BAD_DEVICE_TYPE; /* no DOS prefix, assume NT native name */
2914 /* check for sub-directory */
2915 for (pos = 0; pos < name_len; pos++)
2917 if (IS_SEPARATOR(name[pos])) break;
2918 if (name[pos] < 32 || strchrW( invalid_charsW, name[pos] ))
2919 return STATUS_OBJECT_NAME_INVALID;
2921 if (pos > MAX_DIR_ENTRY_LEN)
2922 return STATUS_OBJECT_NAME_INVALID;
2924 if (pos == name_len) /* no subdir, plain DOS device */
2925 return get_dos_device( name, name_len, unix_name_ret );
2927 for (prefix_len = 0; prefix_len < pos; prefix_len++)
2928 prefix[prefix_len] = tolowerW(name[prefix_len]);
2931 name_len -= prefix_len;
2933 /* check for invalid characters (all chars except 0 are valid for unix) */
2934 is_unix = (prefix_len == 4 && !memcmp( prefix, unixW, sizeof(unixW) ));
2937 for (p = name; p < name + name_len; p++)
2938 if (!*p) return STATUS_OBJECT_NAME_INVALID;
2943 for (p = name; p < name + name_len; p++)
2944 if (*p < 32 || strchrW( invalid_charsW, *p )) return STATUS_OBJECT_NAME_INVALID;
2947 unix_len = ntdll_wcstoumbs( 0, prefix, prefix_len, NULL, 0, NULL, NULL );
2948 unix_len += ntdll_wcstoumbs( 0, name, name_len, NULL, 0, NULL, NULL );
2949 unix_len += MAX_DIR_ENTRY_LEN + 3;
2950 unix_len += strlen(config_dir) + sizeof("/dosdevices/");
2951 if (!(unix_name = RtlAllocateHeap( GetProcessHeap(), 0, unix_len )))
2952 return STATUS_NO_MEMORY;
2953 strcpy( unix_name, config_dir );
2954 strcat( unix_name, "/dosdevices/" );
2955 pos = strlen(unix_name);
2957 ret = ntdll_wcstoumbs( 0, prefix, prefix_len, unix_name + pos, unix_len - pos - 1,
2958 NULL, &used_default );
2959 if (!ret || used_default)
2961 RtlFreeHeap( GetProcessHeap(), 0, unix_name );
2962 return STATUS_OBJECT_NAME_INVALID;
2966 /* check if prefix exists (except for DOS drives to avoid extra stat calls) */
2968 if (prefix_len != 2 || prefix[1] != ':')
2971 if (lstat( unix_name, &st ) == -1 && errno == ENOENT)
2975 RtlFreeHeap( GetProcessHeap(), 0, unix_name );
2976 return STATUS_BAD_DEVICE_TYPE;
2978 pos = 0; /* fall back to unix root */
2982 status = lookup_unix_name( name, name_len, &unix_name, unix_len, pos, disposition, check_case );
2983 if (status == STATUS_SUCCESS || status == STATUS_NO_SUCH_FILE)
2985 TRACE( "%s -> %s\n", debugstr_us(nameW), debugstr_a(unix_name) );
2986 unix_name_ret->Buffer = unix_name;
2987 unix_name_ret->Length = strlen(unix_name);
2988 unix_name_ret->MaximumLength = unix_len;
2992 TRACE( "%s not found in %s\n", debugstr_w(name), unix_name );
2993 RtlFreeHeap( GetProcessHeap(), 0, unix_name );
2999 /******************************************************************
3000 * RtlWow64EnableFsRedirection (NTDLL.@)
3002 NTSTATUS WINAPI RtlWow64EnableFsRedirection( BOOLEAN enable )
3004 if (!is_wow64) return STATUS_NOT_IMPLEMENTED;
3005 ntdll_get_thread_data()->wow64_redir = enable;
3006 return STATUS_SUCCESS;
3010 /******************************************************************
3011 * RtlWow64EnableFsRedirectionEx (NTDLL.@)
3013 NTSTATUS WINAPI RtlWow64EnableFsRedirectionEx( ULONG disable, ULONG *old_value )
3015 if (!is_wow64) return STATUS_NOT_IMPLEMENTED;
3016 *old_value = !ntdll_get_thread_data()->wow64_redir;
3017 ntdll_get_thread_data()->wow64_redir = !disable;
3018 return STATUS_SUCCESS;
3022 /******************************************************************
3023 * RtlDoesFileExists_U (NTDLL.@)
3025 BOOLEAN WINAPI RtlDoesFileExists_U(LPCWSTR file_name)
3027 UNICODE_STRING nt_name;
3028 FILE_BASIC_INFORMATION basic_info;
3029 OBJECT_ATTRIBUTES attr;
3032 if (!RtlDosPathNameToNtPathName_U( file_name, &nt_name, NULL, NULL )) return FALSE;
3034 attr.Length = sizeof(attr);
3035 attr.RootDirectory = 0;
3036 attr.ObjectName = &nt_name;
3037 attr.Attributes = OBJ_CASE_INSENSITIVE;
3038 attr.SecurityDescriptor = NULL;
3039 attr.SecurityQualityOfService = NULL;
3041 ret = NtQueryAttributesFile(&attr, &basic_info) == STATUS_SUCCESS;
3043 RtlFreeUnicodeString( &nt_name );
3048 /***********************************************************************
3049 * DIR_unmount_device
3051 * Unmount the specified device.
3053 NTSTATUS DIR_unmount_device( HANDLE handle )
3056 int unix_fd, needs_close;
3058 if (!(status = server_get_unix_fd( handle, 0, &unix_fd, &needs_close, NULL, NULL )))
3061 char *mount_point = NULL;
3063 if (fstat( unix_fd, &st ) == -1 || !is_valid_mounted_device( &st ))
3064 status = STATUS_INVALID_PARAMETER;
3067 if ((mount_point = get_device_mount_point( st.st_rdev )))
3070 static const char umount[] = "diskutil unmount >/dev/null 2>&1 ";
3072 static const char umount[] = "umount >/dev/null 2>&1 ";
3074 char *cmd = RtlAllocateHeap( GetProcessHeap(), 0, strlen(mount_point)+sizeof(umount));
3077 strcpy( cmd, umount );
3078 strcat( cmd, mount_point );
3080 RtlFreeHeap( GetProcessHeap(), 0, cmd );
3082 /* umount will fail to release the loop device since we still have
3083 a handle to it, so we release it here */
3084 if (major(st.st_rdev) == LOOP_MAJOR) ioctl( unix_fd, 0x4c01 /*LOOP_CLR_FD*/, 0 );
3087 RtlFreeHeap( GetProcessHeap(), 0, mount_point );
3090 if (needs_close) close( unix_fd );
3096 /******************************************************************************
3099 * Retrieve the Unix name of the current directory; helper for wine_unix_to_nt_file_name.
3100 * Returned value must be freed by caller.
3102 NTSTATUS DIR_get_unix_cwd( char **cwd )
3104 int old_cwd, unix_fd, needs_close;
3109 RtlAcquirePebLock();
3111 if (NtCurrentTeb()->Tib.SubSystemTib) /* FIXME: hack */
3112 curdir = &((WIN16_SUBSYSTEM_TIB *)NtCurrentTeb()->Tib.SubSystemTib)->curdir;
3114 curdir = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectory;
3116 if (!(handle = curdir->Handle))
3118 UNICODE_STRING dirW;
3119 OBJECT_ATTRIBUTES attr;
3122 if (!RtlDosPathNameToNtPathName_U( curdir->DosPath.Buffer, &dirW, NULL, NULL ))
3124 status = STATUS_OBJECT_NAME_INVALID;
3127 attr.Length = sizeof(attr);
3128 attr.RootDirectory = 0;
3129 attr.Attributes = OBJ_CASE_INSENSITIVE;
3130 attr.ObjectName = &dirW;
3131 attr.SecurityDescriptor = NULL;
3132 attr.SecurityQualityOfService = NULL;
3134 status = NtOpenFile( &handle, 0, &attr, &io, 0,
3135 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
3136 RtlFreeUnicodeString( &dirW );
3137 if (status != STATUS_SUCCESS) goto done;
3140 if ((status = server_get_unix_fd( handle, 0, &unix_fd, &needs_close, NULL, NULL )) == STATUS_SUCCESS)
3142 RtlEnterCriticalSection( &dir_section );
3144 if ((old_cwd = open(".", O_RDONLY)) != -1 && fchdir( unix_fd ) != -1)
3146 unsigned int size = 512;
3150 if (!(*cwd = RtlAllocateHeap( GetProcessHeap(), 0, size )))
3152 status = STATUS_NO_MEMORY;
3155 if (getcwd( *cwd, size )) break;
3156 RtlFreeHeap( GetProcessHeap(), 0, *cwd );
3157 if (errno != ERANGE)
3159 status = STATUS_OBJECT_PATH_INVALID;
3164 if (fchdir( old_cwd ) == -1) chdir( "/" );
3166 else status = FILE_GetNtStatus();
3168 RtlLeaveCriticalSection( &dir_section );
3169 if (old_cwd != -1) close( old_cwd );
3170 if (needs_close) close( unix_fd );
3172 if (!curdir->Handle) NtClose( handle );
3175 RtlReleasePebLock();
3179 struct read_changes_info
3184 PIO_APC_ROUTINE apc;
3188 /* callback for ioctl user APC */
3189 static void WINAPI read_changes_user_apc( void *arg, IO_STATUS_BLOCK *io, ULONG reserved )
3191 struct read_changes_info *info = arg;
3192 if (info->apc) info->apc( info->apc_arg, io, reserved );
3193 RtlFreeHeap( GetProcessHeap(), 0, info );
3196 static NTSTATUS read_changes_apc( void *user, PIO_STATUS_BLOCK iosb, NTSTATUS status, void **apc )
3198 struct read_changes_info *info = user;
3199 char data[PATH_MAX];
3203 SERVER_START_REQ( read_change )
3205 req->handle = wine_server_obj_handle( info->FileHandle );
3206 wine_server_set_reply( req, data, PATH_MAX );
3207 ret = wine_server_call( req );
3208 size = wine_server_reply_size( reply );
3212 if (ret == STATUS_SUCCESS && info->Buffer)
3214 PFILE_NOTIFY_INFORMATION pfni = info->Buffer;
3215 int i, left = info->BufferSize;
3216 DWORD *last_entry_offset = NULL;
3217 struct filesystem_event *event = (struct filesystem_event*)data;
3219 while (size && left >= sizeof(*pfni))
3221 /* convert to an NT style path */
3222 for (i=0; i<event->len; i++)
3223 if (event->name[i] == '/')
3224 event->name[i] = '\\';
3226 pfni->Action = event->action;
3227 pfni->FileNameLength = ntdll_umbstowcs( 0, event->name, event->len, pfni->FileName,
3228 (left - offsetof(FILE_NOTIFY_INFORMATION, FileName)) / sizeof(WCHAR));
3229 last_entry_offset = &pfni->NextEntryOffset;
3231 if(pfni->FileNameLength == -1 || pfni->FileNameLength == -2)
3234 i = offsetof(FILE_NOTIFY_INFORMATION, FileName[pfni->FileNameLength]);
3235 pfni->FileNameLength *= sizeof(WCHAR);
3236 pfni->NextEntryOffset = i;
3237 pfni = (FILE_NOTIFY_INFORMATION*)((char*)pfni + i);
3240 i = (offsetof(struct filesystem_event, name[event->len])
3241 + sizeof(int)-1) / sizeof(int) * sizeof(int);
3242 event = (struct filesystem_event*)((char*)event + i);
3248 ret = STATUS_NOTIFY_ENUM_DIR;
3253 *last_entry_offset = 0;
3254 size = info->BufferSize - left;
3259 ret = STATUS_NOTIFY_ENUM_DIR;
3263 iosb->u.Status = ret;
3264 iosb->Information = size;
3265 *apc = read_changes_user_apc;
3269 #define FILE_NOTIFY_ALL ( \
3270 FILE_NOTIFY_CHANGE_FILE_NAME | \
3271 FILE_NOTIFY_CHANGE_DIR_NAME | \
3272 FILE_NOTIFY_CHANGE_ATTRIBUTES | \
3273 FILE_NOTIFY_CHANGE_SIZE | \
3274 FILE_NOTIFY_CHANGE_LAST_WRITE | \
3275 FILE_NOTIFY_CHANGE_LAST_ACCESS | \
3276 FILE_NOTIFY_CHANGE_CREATION | \
3277 FILE_NOTIFY_CHANGE_SECURITY )
3279 /******************************************************************************
3280 * NtNotifyChangeDirectoryFile [NTDLL.@]
3283 NtNotifyChangeDirectoryFile( HANDLE FileHandle, HANDLE Event,
3284 PIO_APC_ROUTINE ApcRoutine, PVOID ApcContext,
3285 PIO_STATUS_BLOCK IoStatusBlock, PVOID Buffer,
3286 ULONG BufferSize, ULONG CompletionFilter, BOOLEAN WatchTree )
3288 struct read_changes_info *info;
3290 ULONG_PTR cvalue = ApcRoutine ? 0 : (ULONG_PTR)ApcContext;
3292 TRACE("%p %p %p %p %p %p %u %u %d\n",
3293 FileHandle, Event, ApcRoutine, ApcContext, IoStatusBlock,
3294 Buffer, BufferSize, CompletionFilter, WatchTree );
3297 return STATUS_ACCESS_VIOLATION;
3299 if (CompletionFilter == 0 || (CompletionFilter & ~FILE_NOTIFY_ALL))
3300 return STATUS_INVALID_PARAMETER;
3302 info = RtlAllocateHeap( GetProcessHeap(), 0, sizeof *info );
3304 return STATUS_NO_MEMORY;
3306 info->FileHandle = FileHandle;
3307 info->Buffer = Buffer;
3308 info->BufferSize = BufferSize;
3309 info->apc = ApcRoutine;
3310 info->apc_arg = ApcContext;
3312 SERVER_START_REQ( read_directory_changes )
3314 req->filter = CompletionFilter;
3315 req->want_data = (Buffer != NULL);
3316 req->subtree = WatchTree;
3317 req->async.handle = wine_server_obj_handle( FileHandle );
3318 req->async.callback = wine_server_client_ptr( read_changes_apc );
3319 req->async.iosb = wine_server_client_ptr( IoStatusBlock );
3320 req->async.arg = wine_server_client_ptr( info );
3321 req->async.event = wine_server_obj_handle( Event );
3322 req->async.cvalue = cvalue;
3323 status = wine_server_call( req );
3327 if (status != STATUS_PENDING)
3328 RtlFreeHeap( GetProcessHeap(), 0, info );