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_IOCTL_H
45 #include <sys/ioctl.h>
47 #ifdef HAVE_LINUX_IOCTL_H
48 #include <linux/ioctl.h>
50 #ifdef HAVE_LINUX_MAJOR_H
51 # include <linux/major.h>
53 #ifdef HAVE_SYS_PARAM_H
54 #include <sys/param.h>
56 #ifdef HAVE_SYS_MOUNT_H
57 #include <sys/mount.h>
64 #define NONAMELESSUNION
65 #define NONAMELESSSTRUCT
67 #define WIN32_NO_STATUS
71 #include "ntdll_misc.h"
72 #include "wine/unicode.h"
73 #include "wine/server.h"
74 #include "wine/library.h"
75 #include "wine/debug.h"
77 WINE_DEFAULT_DEBUG_CHANNEL(file);
80 #undef VFAT_IOCTL_READDIR_BOTH
85 /* We want the real kernel dirent structure, not the libc one */
90 unsigned short d_reclen;
94 /* Define the VFAT ioctl to get both short and long file names */
95 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, KERNEL_DIRENT [2] )
98 # define O_DIRECTORY 0200000 /* must be directory */
107 unsigned short d_reclen;
108 unsigned char d_type;
112 static inline int getdents64( int fd, char *de, unsigned int size )
115 __asm__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
117 : "0" (220 /*NR_getdents64*/), "r" (fd), "c" (de), "d" (size)
132 #define IS_OPTION_TRUE(ch) ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
133 #define IS_SEPARATOR(ch) ((ch) == '\\' || (ch) == '/')
135 #define INVALID_NT_CHARS '*','?','<','>','|','"'
136 #define INVALID_DOS_CHARS INVALID_NT_CHARS,'+','=',',',';','[',']',' ','\345'
138 #define MAX_DIR_ENTRY_LEN 255 /* max length of a directory entry in chars */
140 #define MAX_IGNORED_FILES 4
146 } ignored_files[MAX_IGNORED_FILES];
147 static int ignored_files_count;
149 static const unsigned int max_dir_info_size = FIELD_OFFSET( FILE_BOTH_DIR_INFORMATION, FileName[MAX_DIR_ENTRY_LEN] );
151 static int show_dot_files = -1;
153 /* at some point we may want to allow Winelib apps to set this */
154 static const int is_case_sensitive = FALSE;
156 static RTL_CRITICAL_SECTION dir_section;
157 static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
160 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
161 0, 0, { (DWORD_PTR)(__FILE__ ": dir_section") }
163 static RTL_CRITICAL_SECTION dir_section = { &critsect_debug, -1, 0, 0, 0, 0 };
166 /* check if a given Unicode char is OK in a DOS short name */
167 static inline BOOL is_invalid_dos_char( WCHAR ch )
169 static const WCHAR invalid_chars[] = { INVALID_DOS_CHARS,'~','.',0 };
170 if (ch > 0x7f) return TRUE;
171 return strchrW( invalid_chars, ch ) != NULL;
174 /* check if the device can be a mounted volume */
175 static inline int is_valid_mounted_device( const struct stat *st )
177 #if defined(linux) || defined(__sun__)
178 return S_ISBLK( st->st_mode );
180 /* disks are char devices on *BSD */
181 return S_ISCHR( st->st_mode );
185 static inline void ignore_file( const char *name )
188 assert( ignored_files_count < MAX_IGNORED_FILES );
189 if (!stat( name, &st ))
191 ignored_files[ignored_files_count].dev = st.st_dev;
192 ignored_files[ignored_files_count].ino = st.st_ino;
193 ignored_files_count++;
197 static inline BOOL is_ignored_file( const struct stat *st )
201 for (i = 0; i < ignored_files_count; i++)
202 if (ignored_files[i].dev == st->st_dev && ignored_files[i].ino == st->st_ino)
207 /***********************************************************************
208 * get_default_com_device
210 * Return the default device to use for serial ports.
212 static char *get_default_com_device( int num )
216 if (!num || num > 9) return ret;
218 ret = RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/ttyS0") );
221 strcpy( ret, "/dev/ttyS0" );
222 ret[strlen(ret) - 1] = '0' + num - 1;
224 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
225 ret = RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/cuad0") );
228 strcpy( ret, "/dev/cuad0" );
229 ret[strlen(ret) - 1] = '0' + num - 1;
232 FIXME( "no known default for device com%d\n", num );
238 /***********************************************************************
239 * get_default_lpt_device
241 * Return the default device to use for parallel ports.
243 static char *get_default_lpt_device( int num )
247 if (!num || num > 9) return ret;
249 ret = RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/lp0") );
252 strcpy( ret, "/dev/lp0" );
253 ret[strlen(ret) - 1] = '0' + num - 1;
256 FIXME( "no known default for device lpt%d\n", num );
262 /***********************************************************************
263 * DIR_get_drives_info
265 * Retrieve device/inode number for all the drives. Helper for find_drive_root.
267 unsigned int DIR_get_drives_info( struct drive_info info[MAX_DOS_DRIVES] )
269 static struct drive_info cache[MAX_DOS_DRIVES];
270 static time_t last_update;
271 static unsigned int nb_drives;
273 time_t now = time(NULL);
275 RtlEnterCriticalSection( &dir_section );
276 if (now != last_update)
278 const char *config_dir = wine_get_config_dir();
283 if ((buffer = RtlAllocateHeap( GetProcessHeap(), 0,
284 strlen(config_dir) + sizeof("/dosdevices/a:") )))
286 strcpy( buffer, config_dir );
287 strcat( buffer, "/dosdevices/a:" );
288 p = buffer + strlen(buffer) - 2;
290 for (i = nb_drives = 0; i < MAX_DOS_DRIVES; i++)
293 if (!stat( buffer, &st ))
295 cache[i].dev = st.st_dev;
296 cache[i].ino = st.st_ino;
305 RtlFreeHeap( GetProcessHeap(), 0, buffer );
309 memcpy( info, cache, sizeof(cache) );
311 RtlLeaveCriticalSection( &dir_section );
316 /***********************************************************************
317 * parse_mount_entries
319 * Parse mount entries looking for a given device. Helper for get_default_drive_device.
323 #include <sys/vfstab.h>
324 static char *parse_vfstab_entries( FILE *f, dev_t dev, ino_t ino)
330 while (! getvfsent( f, &entry ))
332 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
333 if (!strcmp( entry.vfs_fstype, "nfs" ) ||
334 !strcmp( entry.vfs_fstype, "smbfs" ) ||
335 !strcmp( entry.vfs_fstype, "ncpfs" )) continue;
337 if (stat( entry.vfs_mountp, &st ) == -1) continue;
338 if (st.st_dev != dev || st.st_ino != ino) continue;
339 if (!strcmp( entry.vfs_fstype, "fd" ))
341 if ((device = strstr( entry.vfs_mntopts, "dev=" )))
343 char *p = strchr( device + 4, ',' );
349 return entry.vfs_special;
356 static char *parse_mount_entries( FILE *f, dev_t dev, ino_t ino )
358 struct mntent *entry;
362 while ((entry = getmntent( f )))
364 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
365 if (!strcmp( entry->mnt_type, "nfs" ) ||
366 !strcmp( entry->mnt_type, "smbfs" ) ||
367 !strcmp( entry->mnt_type, "ncpfs" )) continue;
369 if (stat( entry->mnt_dir, &st ) == -1) continue;
370 if (st.st_dev != dev || st.st_ino != ino) continue;
371 if (!strcmp( entry->mnt_type, "supermount" ))
373 if ((device = strstr( entry->mnt_opts, "dev=" )))
375 char *p = strchr( device + 4, ',' );
380 else if (!stat( entry->mnt_fsname, &st ) && S_ISREG(st.st_mode))
382 /* if device is a regular file check for a loop mount */
383 if ((device = strstr( entry->mnt_opts, "loop=" )))
385 char *p = strchr( device + 5, ',' );
391 return entry->mnt_fsname;
397 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
399 static char *parse_mount_entries( FILE *f, dev_t dev, ino_t ino )
404 while ((entry = getfsent()))
406 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
407 if (!strcmp( entry->fs_vfstype, "nfs" ) ||
408 !strcmp( entry->fs_vfstype, "smbfs" ) ||
409 !strcmp( entry->fs_vfstype, "ncpfs" )) continue;
411 if (stat( entry->fs_file, &st ) == -1) continue;
412 if (st.st_dev != dev || st.st_ino != ino) continue;
413 return entry->fs_spec;
420 #include <sys/mnttab.h>
421 static char *parse_mount_entries( FILE *f, dev_t dev, ino_t ino )
428 while (( ! getmntent( f, &entry) ))
430 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
431 if (!strcmp( entry.mnt_fstype, "nfs" ) ||
432 !strcmp( entry.mnt_fstype, "smbfs" ) ||
433 !strcmp( entry.mnt_fstype, "ncpfs" )) continue;
435 if (stat( entry.mnt_mountp, &st ) == -1) continue;
436 if (st.st_dev != dev || st.st_ino != ino) continue;
437 if (!strcmp( entry.mnt_fstype, "fd" ))
439 if ((device = strstr( entry.mnt_mntopts, "dev=" )))
441 char *p = strchr( device + 4, ',' );
447 return entry.mnt_special;
453 /***********************************************************************
454 * get_default_drive_device
456 * Return the default device to use for a given drive mount point.
458 static char *get_default_drive_device( const char *root )
468 /* try to open it first to force it to get mounted */
469 if ((fd = open( root, O_RDONLY | O_DIRECTORY )) != -1)
471 res = fstat( fd, &st );
474 /* now try normal stat just in case */
475 if (res == -1) res = stat( root, &st );
476 if (res == -1) return NULL;
478 RtlEnterCriticalSection( &dir_section );
480 if ((f = fopen( "/etc/mtab", "r" )))
482 device = parse_mount_entries( f, st.st_dev, st.st_ino );
485 /* look through fstab too in case it's not mounted (for instance if it's an audio CD) */
486 if (!device && (f = fopen( "/etc/fstab", "r" )))
488 device = parse_mount_entries( f, st.st_dev, st.st_ino );
493 ret = RtlAllocateHeap( GetProcessHeap(), 0, strlen(device) + 1 );
494 if (ret) strcpy( ret, device );
496 RtlLeaveCriticalSection( &dir_section );
498 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__ )
503 /* try to open it first to force it to get mounted */
504 if ((fd = open( root, O_RDONLY )) != -1)
506 res = fstat( fd, &st );
509 /* now try normal stat just in case */
510 if (res == -1) res = stat( root, &st );
511 if (res == -1) return NULL;
513 RtlEnterCriticalSection( &dir_section );
515 /* The FreeBSD parse_mount_entries doesn't require a file argument, so just
516 * pass NULL. Leave the argument in for symmetry.
518 device = parse_mount_entries( NULL, st.st_dev, st.st_ino );
521 ret = RtlAllocateHeap( GetProcessHeap(), 0, strlen(device) + 1 );
522 if (ret) strcpy( ret, device );
524 RtlLeaveCriticalSection( &dir_section );
532 /* try to open it first to force it to get mounted */
533 if ((fd = open( root, O_RDONLY )) != -1)
535 res = fstat( fd, &st );
538 /* now try normal stat just in case */
539 if (res == -1) res = stat( root, &st );
540 if (res == -1) return NULL;
542 RtlEnterCriticalSection( &dir_section );
544 if ((f = fopen( "/etc/mnttab", "r" )))
546 device = parse_mount_entries( f, st.st_dev, st.st_ino);
549 /* look through fstab too in case it's not mounted (for instance if it's an audio CD) */
550 if (!device && (f = fopen( "/etc/vfstab", "r" )))
552 device = parse_vfstab_entries( f, st.st_dev, st.st_ino );
557 ret = RtlAllocateHeap( GetProcessHeap(), 0, strlen(device) + 1 );
558 if (ret) strcpy( ret, device );
560 RtlLeaveCriticalSection( &dir_section );
562 #elif defined(__APPLE__)
563 struct statfs *mntStat;
569 static const char path_bsd_device[] = "/dev/disk";
572 res = stat( root, &st );
573 if (res == -1) return NULL;
578 RtlEnterCriticalSection( &dir_section );
580 mntSize = getmntinfo(&mntStat, MNT_NOWAIT);
582 for (i = 0; i < mntSize && !ret; i++)
584 if (stat(mntStat[i].f_mntonname, &st ) == -1) continue;
585 if (st.st_dev != dev || st.st_ino != ino) continue;
587 /* FIXME add support for mounted network drive */
588 if ( strncmp(mntStat[i].f_mntfromname, path_bsd_device, strlen(path_bsd_device)) == 0)
590 /* set return value to the corresponding raw BSD node */
591 ret = RtlAllocateHeap( GetProcessHeap(), 0, strlen(mntStat[i].f_mntfromname) + 2 /* 2 : r and \0 */ );
594 strcpy(ret, "/dev/r");
595 strcat(ret, mntStat[i].f_mntfromname+sizeof("/dev/")-1);
599 RtlLeaveCriticalSection( &dir_section );
602 if (!warned++) FIXME( "auto detection of DOS devices not supported on this platform\n" );
608 /***********************************************************************
609 * get_device_mount_point
611 * Return the current mount point for a device.
613 static char *get_device_mount_point( dev_t dev )
620 RtlEnterCriticalSection( &dir_section );
622 if ((f = fopen( "/etc/mtab", "r" )))
624 struct mntent *entry;
628 while ((entry = getmntent( f )))
630 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
631 if (!strcmp( entry->mnt_type, "nfs" ) ||
632 !strcmp( entry->mnt_type, "smbfs" ) ||
633 !strcmp( entry->mnt_type, "ncpfs" )) continue;
635 if (!strcmp( entry->mnt_type, "supermount" ))
637 if ((device = strstr( entry->mnt_opts, "dev=" )))
640 if ((p = strchr( device, ',' ))) *p = 0;
643 else if (!stat( entry->mnt_fsname, &st ) && S_ISREG(st.st_mode))
645 /* if device is a regular file check for a loop mount */
646 if ((device = strstr( entry->mnt_opts, "loop=" )))
649 if ((p = strchr( device, ',' ))) *p = 0;
652 else device = entry->mnt_fsname;
654 if (device && !stat( device, &st ) && S_ISBLK(st.st_mode) && st.st_rdev == dev)
656 ret = RtlAllocateHeap( GetProcessHeap(), 0, strlen(entry->mnt_dir) + 1 );
657 if (ret) strcpy( ret, entry->mnt_dir );
663 RtlLeaveCriticalSection( &dir_section );
664 #elif defined(__APPLE__)
665 struct statfs *entry;
669 RtlEnterCriticalSection( &dir_section );
671 size = getmntinfo( &entry, MNT_NOWAIT );
672 for (i = 0; i < size; i++)
674 if (stat( entry[i].f_mntfromname, &st ) == -1) continue;
675 if (S_ISBLK(st.st_mode) && st.st_rdev == dev)
677 ret = RtlAllocateHeap( GetProcessHeap(), 0, strlen(entry[i].f_mntfromname) + 1 );
678 if (ret) strcpy( ret, entry[i].f_mntfromname );
682 RtlLeaveCriticalSection( &dir_section );
685 if (!warned++) FIXME( "unmounting devices not supported on this platform\n" );
691 /***********************************************************************
694 * Initialize the show_dot_files options.
696 static void init_options(void)
698 static const WCHAR WineW[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e',0};
699 static const WCHAR ShowDotFilesW[] = {'S','h','o','w','D','o','t','F','i','l','e','s',0};
703 OBJECT_ATTRIBUTES attr;
704 UNICODE_STRING nameW;
708 RtlOpenCurrentUser( KEY_ALL_ACCESS, &root );
709 attr.Length = sizeof(attr);
710 attr.RootDirectory = root;
711 attr.ObjectName = &nameW;
713 attr.SecurityDescriptor = NULL;
714 attr.SecurityQualityOfService = NULL;
715 RtlInitUnicodeString( &nameW, WineW );
717 /* @@ Wine registry key: HKCU\Software\Wine */
718 if (!NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ))
720 RtlInitUnicodeString( &nameW, ShowDotFilesW );
721 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, tmp, sizeof(tmp), &dummy ))
723 WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
724 show_dot_files = IS_OPTION_TRUE( str[0] );
730 /* a couple of directories that we don't want to return in directory searches */
731 ignore_file( wine_get_config_dir() );
732 ignore_file( "/dev" );
733 ignore_file( "/proc" );
735 ignore_file( "/sys" );
740 /***********************************************************************
743 * Check if the specified file should be hidden based on its name and the show dot files option.
745 BOOL DIR_is_hidden_file( const UNICODE_STRING *name )
749 if (show_dot_files == -1) init_options();
750 if (show_dot_files) return FALSE;
752 end = p = name->Buffer + name->Length/sizeof(WCHAR);
753 while (p > name->Buffer && IS_SEPARATOR(p[-1])) p--;
754 while (p > name->Buffer && !IS_SEPARATOR(p[-1])) p--;
755 if (p == end || *p != '.') return FALSE;
756 /* make sure it isn't '.' or '..' */
757 if (p + 1 == end) return FALSE;
758 if (p[1] == '.' && p + 2 == end) return FALSE;
763 /***********************************************************************
764 * hash_short_file_name
766 * Transform a Unix file name into a hashed DOS name. If the name is a valid
767 * DOS name, it is converted to upper-case; otherwise it is replaced by a
768 * hashed version that fits in 8.3 format.
769 * 'buffer' must be at least 12 characters long.
770 * Returns length of short name in bytes; short name is NOT null-terminated.
772 static ULONG hash_short_file_name( const UNICODE_STRING *name, LPWSTR buffer )
774 static const char hash_chars[32] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345";
776 LPCWSTR p, ext, end = name->Buffer + name->Length / sizeof(WCHAR);
781 /* Compute the hash code of the file name */
782 /* If you know something about hash functions, feel free to */
783 /* insert a better algorithm here... */
784 if (!is_case_sensitive)
786 for (p = name->Buffer, hash = 0xbeef; p < end - 1; p++)
787 hash = (hash<<3) ^ (hash>>5) ^ tolowerW(*p) ^ (tolowerW(p[1]) << 8);
788 hash = (hash<<3) ^ (hash>>5) ^ tolowerW(*p); /* Last character */
792 for (p = name->Buffer, hash = 0xbeef; p < end - 1; p++)
793 hash = (hash << 3) ^ (hash >> 5) ^ *p ^ (p[1] << 8);
794 hash = (hash << 3) ^ (hash >> 5) ^ *p; /* Last character */
797 /* Find last dot for start of the extension */
798 for (p = name->Buffer + 1, ext = NULL; p < end - 1; p++) if (*p == '.') ext = p;
800 /* Copy first 4 chars, replacing invalid chars with '_' */
801 for (i = 4, p = name->Buffer, dst = buffer; i > 0; i--, p++)
803 if (p == end || p == ext) break;
804 *dst++ = is_invalid_dos_char(*p) ? '_' : toupperW(*p);
806 /* Pad to 5 chars with '~' */
807 while (i-- >= 0) *dst++ = '~';
809 /* Insert hash code converted to 3 ASCII chars */
810 *dst++ = hash_chars[(hash >> 10) & 0x1f];
811 *dst++ = hash_chars[(hash >> 5) & 0x1f];
812 *dst++ = hash_chars[hash & 0x1f];
814 /* Copy the first 3 chars of the extension (if any) */
818 for (i = 3, ext++; (i > 0) && ext < end; i--, ext++)
819 *dst++ = is_invalid_dos_char(*ext) ? '_' : toupperW(*ext);
825 /***********************************************************************
828 * Check a long file name against a mask.
830 * Tests (done in W95 DOS shell - case insensitive):
831 * *.txt test1.test.txt *
833 * *.t??????.t* test1.ta.tornado.txt *
834 * *tornado* test1.ta.tornado.txt *
835 * t*t test1.ta.tornado.txt *
837 * ?est??? test1.txt -
838 * *test1.txt* test1.txt *
839 * h?l?o*t.dat hellothisisatest.dat *
841 static BOOLEAN match_filename( const UNICODE_STRING *name_str, const UNICODE_STRING *mask_str )
844 const WCHAR *name = name_str->Buffer;
845 const WCHAR *mask = mask_str->Buffer;
846 const WCHAR *name_end = name + name_str->Length / sizeof(WCHAR);
847 const WCHAR *mask_end = mask + mask_str->Length / sizeof(WCHAR);
848 const WCHAR *lastjoker = NULL;
849 const WCHAR *next_to_retry = NULL;
851 TRACE("(%s, %s)\n", debugstr_us(name_str), debugstr_us(mask_str));
853 while (name < name_end && mask < mask_end)
859 while (mask < mask_end && *mask == '*') mask++; /* Skip consecutive '*' */
860 if (mask == mask_end) return TRUE; /* end of mask is all '*', so match */
863 /* skip to the next match after the joker(s) */
864 if (is_case_sensitive)
865 while (name < name_end && (*name != *mask)) name++;
867 while (name < name_end && (toupperW(*name) != toupperW(*mask))) name++;
868 next_to_retry = name;
875 if (is_case_sensitive) mismatch = (*mask != *name);
876 else mismatch = (toupperW(*mask) != toupperW(*name));
882 if (mask == mask_end)
884 if (name == name_end) return TRUE;
885 if (lastjoker) mask = lastjoker;
888 else /* mismatch ! */
890 if (lastjoker) /* we had an '*', so we can try unlimitedly */
894 /* this scan sequence was a mismatch, so restart
895 * 1 char after the first char we checked last time */
897 name = next_to_retry;
899 else return FALSE; /* bad luck */
904 while (mask < mask_end && ((*mask == '.') || (*mask == '*')))
905 mask++; /* Ignore trailing '.' or '*' in mask */
906 return (name == name_end && mask == mask_end);
910 /***********************************************************************
913 * helper for NtQueryDirectoryFile
915 static FILE_BOTH_DIR_INFORMATION *append_entry( void *info_ptr, ULONG_PTR *pos, ULONG max_length,
916 const char *long_name, const char *short_name,
917 const UNICODE_STRING *mask )
919 FILE_BOTH_DIR_INFORMATION *info;
920 int i, long_len, short_len, total_len;
922 WCHAR long_nameW[MAX_DIR_ENTRY_LEN];
923 WCHAR short_nameW[12];
926 long_len = ntdll_umbstowcs( 0, long_name, strlen(long_name), long_nameW, MAX_DIR_ENTRY_LEN );
927 if (long_len == -1) return NULL;
929 str.Buffer = long_nameW;
930 str.Length = long_len * sizeof(WCHAR);
931 str.MaximumLength = sizeof(long_nameW);
935 short_len = ntdll_umbstowcs( 0, short_name, strlen(short_name),
936 short_nameW, sizeof(short_nameW) / sizeof(WCHAR) );
937 if (short_len == -1) short_len = sizeof(short_nameW) / sizeof(WCHAR);
939 else /* generate a short name if necessary */
944 if (!RtlIsNameLegalDOS8Dot3( &str, NULL, &spaces ) || spaces)
945 short_len = hash_short_file_name( &str, short_nameW );
948 TRACE( "long %s short %s mask %s\n",
949 debugstr_us(&str), debugstr_wn(short_nameW, short_len), debugstr_us(mask) );
951 if (mask && !match_filename( &str, mask ))
953 if (!short_len) return NULL; /* no short name to match */
954 str.Buffer = short_nameW;
955 str.Length = short_len * sizeof(WCHAR);
956 str.MaximumLength = sizeof(short_nameW);
957 if (!match_filename( &str, mask )) return NULL;
960 total_len = (sizeof(*info) - sizeof(info->FileName) + long_len*sizeof(WCHAR) + 3) & ~3;
961 info = (FILE_BOTH_DIR_INFORMATION *)((char *)info_ptr + *pos);
963 if (*pos + total_len > max_length) total_len = max_length - *pos;
965 info->FileAttributes = 0;
966 if (lstat( long_name, &st ) == -1) return NULL;
967 if (S_ISLNK( st.st_mode ))
969 if (stat( long_name, &st ) == -1) return NULL;
970 if (S_ISDIR( st.st_mode )) info->FileAttributes |= FILE_ATTRIBUTE_REPARSE_POINT;
972 if (is_ignored_file( &st ))
974 TRACE( "ignoring file %s\n", long_name );
978 info->NextEntryOffset = total_len;
979 info->FileIndex = 0; /* NTFS always has 0 here, so let's not bother with it */
981 RtlSecondsSince1970ToTime( st.st_mtime, &info->CreationTime );
982 RtlSecondsSince1970ToTime( st.st_mtime, &info->LastWriteTime );
983 RtlSecondsSince1970ToTime( st.st_atime, &info->LastAccessTime );
984 RtlSecondsSince1970ToTime( st.st_ctime, &info->ChangeTime );
986 if (S_ISDIR(st.st_mode))
988 info->EndOfFile.QuadPart = info->AllocationSize.QuadPart = 0;
989 info->FileAttributes |= FILE_ATTRIBUTE_DIRECTORY;
993 info->EndOfFile.QuadPart = st.st_size;
994 info->AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
995 info->FileAttributes |= FILE_ATTRIBUTE_ARCHIVE;
998 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
999 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
1001 if (!show_dot_files && long_name[0] == '.' && long_name[1] && (long_name[1] != '.' || long_name[2]))
1002 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
1004 info->EaSize = 0; /* FIXME */
1005 info->ShortNameLength = short_len * sizeof(WCHAR);
1006 for (i = 0; i < short_len; i++) info->ShortName[i] = toupperW(short_nameW[i]);
1007 info->FileNameLength = long_len * sizeof(WCHAR);
1008 memcpy( info->FileName, long_nameW,
1009 min( info->FileNameLength, total_len-sizeof(*info)+sizeof(info->FileName) ));
1016 #ifdef VFAT_IOCTL_READDIR_BOTH
1018 /***********************************************************************
1021 * Wrapper for the VFAT ioctl to work around various kernel bugs.
1022 * dir_section must be held by caller.
1024 static KERNEL_DIRENT *start_vfat_ioctl( int fd )
1026 static KERNEL_DIRENT *de;
1031 const size_t page_size = getpagesize();
1032 SIZE_T size = 2 * sizeof(*de) + page_size;
1035 if (NtAllocateVirtualMemory( GetCurrentProcess(), &addr, 1, &size, MEM_RESERVE, PAGE_READWRITE ))
1037 /* commit only the size needed for the dir entries */
1038 /* this leaves an extra unaccessible page, which should make the kernel */
1039 /* fail with -EFAULT before it stomps all over our memory */
1041 size = 2 * sizeof(*de);
1042 NtAllocateVirtualMemory( GetCurrentProcess(), &addr, 1, &size, MEM_COMMIT, PAGE_READWRITE );
1045 /* set d_reclen to 65535 to work around an AFS kernel bug */
1046 de[0].d_reclen = 65535;
1047 res = ioctl( fd, VFAT_IOCTL_READDIR_BOTH, (long)de );
1050 if (errno != ENOENT) return NULL; /* VFAT ioctl probably not supported */
1051 de[0].d_reclen = 0; /* eof */
1053 else if (!res && de[0].d_reclen == 65535) return NULL; /* AFS bug */
1059 /***********************************************************************
1060 * read_directory_vfat
1062 * Read a directory using the VFAT ioctl; helper for NtQueryDirectoryFile.
1064 static int read_directory_vfat( int fd, IO_STATUS_BLOCK *io, void *buffer, ULONG length,
1065 BOOLEAN single_entry, const UNICODE_STRING *mask,
1066 BOOLEAN restart_scan )
1071 FILE_BOTH_DIR_INFORMATION *info, *last_info = NULL;
1073 io->u.Status = STATUS_SUCCESS;
1075 if (restart_scan) lseek( fd, 0, SEEK_SET );
1077 if (length < max_dir_info_size) /* we may have to return a partial entry here */
1079 off_t old_pos = lseek( fd, 0, SEEK_CUR );
1081 if (!(de = start_vfat_ioctl( fd ))) return -1; /* not supported */
1083 while (de[0].d_reclen)
1085 /* make sure names are null-terminated to work around an x86-64 kernel bug */
1086 len = min(de[0].d_reclen, sizeof(de[0].d_name) - 1 );
1087 de[0].d_name[len] = 0;
1088 len = min(de[1].d_reclen, sizeof(de[1].d_name) - 1 );
1089 de[1].d_name[len] = 0;
1091 if (de[1].d_name[0])
1092 info = append_entry( buffer, &io->Information, length,
1093 de[1].d_name, de[0].d_name, mask );
1095 info = append_entry( buffer, &io->Information, length,
1096 de[0].d_name, NULL, mask );
1100 if ((char *)info->FileName + info->FileNameLength > (char *)buffer + length)
1102 io->u.Status = STATUS_BUFFER_OVERFLOW;
1103 lseek( fd, old_pos, SEEK_SET ); /* restore pos to previous entry */
1107 old_pos = lseek( fd, 0, SEEK_CUR );
1108 if (ioctl( fd, VFAT_IOCTL_READDIR_BOTH, (long)de ) == -1) break;
1111 else /* we'll only return full entries, no need to worry about overflow */
1113 if (!(de = start_vfat_ioctl( fd ))) return -1; /* not supported */
1115 while (de[0].d_reclen)
1117 /* make sure names are null-terminated to work around an x86-64 kernel bug */
1118 len = min(de[0].d_reclen, sizeof(de[0].d_name) - 1 );
1119 de[0].d_name[len] = 0;
1120 len = min(de[1].d_reclen, sizeof(de[1].d_name) - 1 );
1121 de[1].d_name[len] = 0;
1123 if (de[1].d_name[0])
1124 info = append_entry( buffer, &io->Information, length,
1125 de[1].d_name, de[0].d_name, mask );
1127 info = append_entry( buffer, &io->Information, length,
1128 de[0].d_name, NULL, mask );
1132 if (single_entry) break;
1133 /* check if we still have enough space for the largest possible entry */
1134 if (io->Information + max_dir_info_size > length) break;
1136 if (ioctl( fd, VFAT_IOCTL_READDIR_BOTH, (long)de ) == -1) break;
1140 if (last_info) last_info->NextEntryOffset = 0;
1141 else io->u.Status = restart_scan ? STATUS_NO_SUCH_FILE : STATUS_NO_MORE_FILES;
1144 #endif /* VFAT_IOCTL_READDIR_BOTH */
1147 /***********************************************************************
1148 * read_directory_getdents
1150 * Read a directory using the Linux getdents64 system call; helper for NtQueryDirectoryFile.
1153 static int read_directory_getdents( int fd, IO_STATUS_BLOCK *io, void *buffer, ULONG length,
1154 BOOLEAN single_entry, const UNICODE_STRING *mask,
1155 BOOLEAN restart_scan )
1158 size_t size = length;
1159 int res, fake_dot_dot = 1;
1160 char *data, local_buffer[8192];
1161 KERNEL_DIRENT64 *de;
1162 FILE_BOTH_DIR_INFORMATION *info, *last_info = NULL;
1164 if (size <= sizeof(local_buffer) || !(data = RtlAllocateHeap( GetProcessHeap(), 0, size )))
1166 size = sizeof(local_buffer);
1167 data = local_buffer;
1170 if (restart_scan) lseek( fd, 0, SEEK_SET );
1171 else if (length < max_dir_info_size) /* we may have to return a partial entry here */
1173 old_pos = lseek( fd, 0, SEEK_CUR );
1174 if (old_pos == -1 && errno == ENOENT)
1176 io->u.Status = STATUS_NO_MORE_FILES;
1182 io->u.Status = STATUS_SUCCESS;
1184 res = getdents64( fd, data, size );
1187 if (errno != ENOSYS)
1189 io->u.Status = FILE_GetNtStatus();
1195 de = (KERNEL_DIRENT64 *)data;
1199 /* check if we got . and .. from getdents */
1202 if (!strcmp( de->d_name, "." ) && res > de->d_reclen)
1204 KERNEL_DIRENT64 *next_de = (KERNEL_DIRENT64 *)(data + de->d_reclen);
1205 if (!strcmp( next_de->d_name, ".." )) fake_dot_dot = 0;
1208 /* make sure we have enough room for both entries */
1211 static const ULONG min_info_size = (FIELD_OFFSET(FILE_BOTH_DIR_INFORMATION, FileName[1]) +
1212 FIELD_OFFSET(FILE_BOTH_DIR_INFORMATION, FileName[2]) + 3) & ~3;
1213 if (length < min_info_size || single_entry)
1215 FIXME( "not enough room %u/%u for fake . and .. entries\n", length, single_entry );
1222 if ((info = append_entry( buffer, &io->Information, length, ".", NULL, mask )))
1224 if ((info = append_entry( buffer, &io->Information, length, "..", NULL, mask )))
1227 /* check if we still have enough space for the largest possible entry */
1228 if (last_info && io->Information + max_dir_info_size > length)
1230 lseek( fd, 0, SEEK_SET ); /* reset pos to first entry */
1238 res -= de->d_reclen;
1240 !(fake_dot_dot && (!strcmp( de->d_name, "." ) || !strcmp( de->d_name, ".." ))) &&
1241 (info = append_entry( buffer, &io->Information, length, de->d_name, NULL, mask )))
1244 if ((char *)info->FileName + info->FileNameLength > (char *)buffer + length)
1246 io->u.Status = STATUS_BUFFER_OVERFLOW;
1247 lseek( fd, old_pos, SEEK_SET ); /* restore pos to previous entry */
1250 /* check if we still have enough space for the largest possible entry */
1251 if (single_entry || io->Information + max_dir_info_size > length)
1253 if (res > 0) lseek( fd, de->d_off, SEEK_SET ); /* set pos to next entry */
1257 old_pos = de->d_off;
1258 /* move on to the next entry */
1259 if (res > 0) de = (KERNEL_DIRENT64 *)((char *)de + de->d_reclen);
1262 res = getdents64( fd, data, size );
1263 de = (KERNEL_DIRENT64 *)data;
1267 if (last_info) last_info->NextEntryOffset = 0;
1268 else io->u.Status = restart_scan ? STATUS_NO_SUCH_FILE : STATUS_NO_MORE_FILES;
1271 if (data != local_buffer) RtlFreeHeap( GetProcessHeap(), 0, data );
1275 #elif defined HAVE_GETDIRENTRIES
1277 #if _DARWIN_FEATURE_64_BIT_INODE
1279 /* Darwin doesn't provide a version of getdirentries with support for 64-bit
1280 * inodes. When 64-bit inodes are enabled, the getdirentries symbol is mapped
1281 * to _getdirentries_is_not_available_when_64_bit_inodes_are_in_effect so that
1282 * we get link errors if we try to use it. We still need getdirentries, but we
1283 * don't need it to support 64-bit inodes. So, we use the legacy getdirentries
1284 * with 32-bit inodes. We have to be careful to use a corresponding dirent
1287 int darwin_legacy_getdirentries(int, char *, int, long *) __asm("_getdirentries");
1288 #define getdirentries darwin_legacy_getdirentries
1290 struct darwin_legacy_dirent {
1292 __uint16_t d_reclen;
1295 char d_name[__DARWIN_MAXNAMLEN + 1];
1297 #define dirent darwin_legacy_dirent
1301 /***********************************************************************
1302 * wine_getdirentries
1304 * Wrapper for the BSD getdirentries system call to fix a bug in the
1305 * Mac OS X version. For some file systems (at least Apple Filing
1306 * Protocol a.k.a. AFP), getdirentries resets the file position to 0
1307 * when it's about to return 0 (no more entries). So, a subsequent
1308 * getdirentries call starts over at the beginning again, causing an
1311 static inline int wine_getdirentries(int fd, char *buf, int nbytes, long *basep)
1313 int res = getdirentries(fd, buf, nbytes, basep);
1316 lseek(fd, *basep, SEEK_SET);
1321 /***********************************************************************
1322 * read_directory_getdirentries
1324 * Read a directory using the BSD getdirentries system call; helper for NtQueryDirectoryFile.
1326 static int read_directory_getdirentries( int fd, IO_STATUS_BLOCK *io, void *buffer, ULONG length,
1327 BOOLEAN single_entry, const UNICODE_STRING *mask,
1328 BOOLEAN restart_scan )
1331 ULONG_PTR restart_info_pos = 0;
1332 size_t size, initial_size = length;
1333 int res, fake_dot_dot = 1;
1334 char *data, local_buffer[8192];
1336 FILE_BOTH_DIR_INFORMATION *info, *last_info = NULL, *restart_last_info = NULL;
1338 size = initial_size;
1339 data = local_buffer;
1340 if (size > sizeof(local_buffer) && !(data = RtlAllocateHeap( GetProcessHeap(), 0, size )))
1342 io->u.Status = STATUS_NO_MEMORY;
1343 return io->u.Status;
1346 if (restart_scan) lseek( fd, 0, SEEK_SET );
1348 io->u.Status = STATUS_SUCCESS;
1350 /* FIXME: should make sure size is larger than filesystem block size */
1351 res = wine_getdirentries( fd, data, size, &restart_pos );
1354 io->u.Status = FILE_GetNtStatus();
1359 de = (struct dirent *)data;
1363 /* check if we got . and .. from getdirentries */
1366 if (!strcmp( de->d_name, "." ) && res > de->d_reclen)
1368 struct dirent *next_de = (struct dirent *)(data + de->d_reclen);
1369 if (!strcmp( next_de->d_name, ".." )) fake_dot_dot = 0;
1372 /* make sure we have enough room for both entries */
1375 static const ULONG min_info_size = (FIELD_OFFSET(FILE_BOTH_DIR_INFORMATION, FileName[1]) +
1376 FIELD_OFFSET(FILE_BOTH_DIR_INFORMATION, FileName[2]) + 3) & ~3;
1377 if (length < min_info_size || single_entry)
1379 FIXME( "not enough room %u/%u for fake . and .. entries\n", length, single_entry );
1386 if ((info = append_entry( buffer, &io->Information, length, ".", NULL, mask )))
1388 if ((info = append_entry( buffer, &io->Information, length, "..", NULL, mask )))
1391 restart_last_info = last_info;
1392 restart_info_pos = io->Information;
1394 /* check if we still have enough space for the largest possible entry */
1395 if (last_info && io->Information + max_dir_info_size > length)
1397 lseek( fd, 0, SEEK_SET ); /* reset pos to first entry */
1405 res -= de->d_reclen;
1407 !(fake_dot_dot && (!strcmp( de->d_name, "." ) || !strcmp( de->d_name, ".." ))) &&
1408 ((info = append_entry( buffer, &io->Information, length, de->d_name, NULL, mask ))))
1411 if ((char *)info->FileName + info->FileNameLength > (char *)buffer + length)
1413 lseek( fd, (unsigned long)restart_pos, SEEK_SET );
1414 if (restart_info_pos) /* if we have a complete read already, return it */
1416 io->Information = restart_info_pos;
1417 last_info = restart_last_info;
1420 /* otherwise restart from the start with a smaller size */
1421 size = (char *)de - data;
1424 io->u.Status = STATUS_BUFFER_OVERFLOW;
1427 io->Information = 0;
1431 /* if we have to return but the buffer contains more data, restart with a smaller size */
1432 if (res > 0 && (single_entry || io->Information + max_dir_info_size > length))
1434 lseek( fd, (unsigned long)restart_pos, SEEK_SET );
1435 size = (char *)de - data;
1436 io->Information = restart_info_pos;
1437 last_info = restart_last_info;
1441 /* move on to the next entry */
1444 de = (struct dirent *)((char *)de + de->d_reclen);
1447 if (size < initial_size) break; /* already restarted once, give up now */
1448 size = min( size, length - io->Information );
1449 /* if size is too small don't bother to continue */
1450 if (size < max_dir_info_size && last_info) break;
1451 restart_last_info = last_info;
1452 restart_info_pos = io->Information;
1454 res = wine_getdirentries( fd, data, size, &restart_pos );
1455 de = (struct dirent *)data;
1458 if (last_info) last_info->NextEntryOffset = 0;
1459 else io->u.Status = restart_scan ? STATUS_NO_SUCH_FILE : STATUS_NO_MORE_FILES;
1462 if (data != local_buffer) RtlFreeHeap( GetProcessHeap(), 0, data );
1466 #if _DARWIN_FEATURE_64_BIT_INODE
1467 #undef getdirentries
1471 #endif /* HAVE_GETDIRENTRIES */
1474 /***********************************************************************
1475 * read_directory_readdir
1477 * Read a directory using the POSIX readdir interface; helper for NtQueryDirectoryFile.
1479 static void read_directory_readdir( int fd, IO_STATUS_BLOCK *io, void *buffer, ULONG length,
1480 BOOLEAN single_entry, const UNICODE_STRING *mask,
1481 BOOLEAN restart_scan )
1484 off_t i, old_pos = 0;
1486 FILE_BOTH_DIR_INFORMATION *info, *last_info = NULL;
1488 if (!(dir = opendir( "." )))
1490 io->u.Status = FILE_GetNtStatus();
1496 old_pos = lseek( fd, 0, SEEK_CUR );
1497 /* skip the right number of entries */
1498 for (i = 0; i < old_pos - 2; i++)
1500 if (!readdir( dir ))
1503 io->u.Status = STATUS_NO_MORE_FILES;
1508 io->u.Status = STATUS_SUCCESS;
1513 info = append_entry( buffer, &io->Information, length, ".", NULL, mask );
1514 else if (old_pos == 1)
1515 info = append_entry( buffer, &io->Information, length, "..", NULL, mask );
1516 else if ((de = readdir( dir )))
1518 if (strcmp( de->d_name, "." ) && strcmp( de->d_name, ".." ))
1519 info = append_entry( buffer, &io->Information, length, de->d_name, NULL, mask );
1529 if ((char *)info->FileName + info->FileNameLength > (char *)buffer + length)
1531 io->u.Status = STATUS_BUFFER_OVERFLOW;
1532 old_pos--; /* restore pos to previous entry */
1535 if (single_entry) break;
1536 /* check if we still have enough space for the largest possible entry */
1537 if (io->Information + max_dir_info_size > length) break;
1541 lseek( fd, old_pos, SEEK_SET ); /* store dir offset as filepos for fd */
1544 if (last_info) last_info->NextEntryOffset = 0;
1545 else io->u.Status = restart_scan ? STATUS_NO_SUCH_FILE : STATUS_NO_MORE_FILES;
1548 /***********************************************************************
1549 * read_directory_stat
1551 * Read a single file from a directory by determining whether the file
1552 * identified by mask exists using stat.
1554 static int read_directory_stat( int fd, IO_STATUS_BLOCK *io, void *buffer, ULONG length,
1555 BOOLEAN single_entry, const UNICODE_STRING *mask,
1556 BOOLEAN restart_scan )
1558 int unix_len, ret, used_default;
1562 TRACE("trying optimisation for file %s\n", debugstr_us( mask ));
1564 unix_len = ntdll_wcstoumbs( 0, mask->Buffer, mask->Length / sizeof(WCHAR), NULL, 0, NULL, NULL );
1565 if (!(unix_name = RtlAllocateHeap( GetProcessHeap(), 0, unix_len + 1)))
1567 io->u.Status = STATUS_NO_MEMORY;
1570 ret = ntdll_wcstoumbs( 0, mask->Buffer, mask->Length / sizeof(WCHAR), unix_name, unix_len,
1571 NULL, &used_default );
1572 if (ret > 0 && !used_default)
1577 lseek( fd, 0, SEEK_SET );
1579 else if (lseek( fd, 0, SEEK_CUR ) != 0)
1581 io->u.Status = STATUS_NO_MORE_FILES;
1586 ret = stat( unix_name, &st );
1589 FILE_BOTH_DIR_INFORMATION *info = append_entry( buffer, &io->Information, length, unix_name, NULL, NULL );
1592 info->NextEntryOffset = 0;
1593 if ((char *)info->FileName + info->FileNameLength > (char *)buffer + length)
1594 io->u.Status = STATUS_BUFFER_OVERFLOW;
1596 lseek( fd, 1, SEEK_CUR );
1598 else io->u.Status = STATUS_NO_MORE_FILES;
1604 RtlFreeHeap( GetProcessHeap(), 0, unix_name );
1606 TRACE("returning %d\n", ret);
1612 static inline WCHAR *mempbrkW( const WCHAR *ptr, const WCHAR *accept, size_t n )
1615 for (end = ptr + n; ptr < end; ptr++) if (strchrW( accept, *ptr )) return (WCHAR *)ptr;
1619 /******************************************************************************
1620 * NtQueryDirectoryFile [NTDLL.@]
1621 * ZwQueryDirectoryFile [NTDLL.@]
1623 NTSTATUS WINAPI NtQueryDirectoryFile( HANDLE handle, HANDLE event,
1624 PIO_APC_ROUTINE apc_routine, PVOID apc_context,
1625 PIO_STATUS_BLOCK io,
1626 PVOID buffer, ULONG length,
1627 FILE_INFORMATION_CLASS info_class,
1628 BOOLEAN single_entry,
1629 PUNICODE_STRING mask,
1630 BOOLEAN restart_scan )
1632 int cwd, fd, needs_close;
1633 static const WCHAR wszWildcards[] = { '*','?',0 };
1635 TRACE("(%p %p %p %p %p %p 0x%08x 0x%08x 0x%08x %s 0x%08x\n",
1636 handle, event, apc_routine, apc_context, io, buffer,
1637 length, info_class, single_entry, debugstr_us(mask),
1640 if (length < sizeof(FILE_BOTH_DIR_INFORMATION)) return STATUS_INFO_LENGTH_MISMATCH;
1642 if (event || apc_routine)
1644 FIXME( "Unsupported yet option\n" );
1645 return io->u.Status = STATUS_NOT_IMPLEMENTED;
1647 if (info_class != FileBothDirectoryInformation)
1649 FIXME( "Unsupported file info class %d\n", info_class );
1650 return io->u.Status = STATUS_NOT_IMPLEMENTED;
1653 if ((io->u.Status = server_get_unix_fd( handle, FILE_LIST_DIRECTORY, &fd, &needs_close, NULL, NULL )) != STATUS_SUCCESS)
1654 return io->u.Status;
1656 io->Information = 0;
1658 RtlEnterCriticalSection( &dir_section );
1660 if (show_dot_files == -1) init_options();
1662 cwd = open( ".", O_RDONLY );
1663 if (fchdir( fd ) != -1)
1665 #ifdef VFAT_IOCTL_READDIR_BOTH
1666 if ((read_directory_vfat( fd, io, buffer, length, single_entry, mask, restart_scan )) != -1)
1669 if (mask && !mempbrkW( mask->Buffer, wszWildcards, mask->Length / sizeof(WCHAR) ) &&
1670 read_directory_stat( fd, io, buffer, length, single_entry, mask, restart_scan ) != -1)
1673 if ((read_directory_getdents( fd, io, buffer, length, single_entry, mask, restart_scan )) != -1)
1675 #elif defined HAVE_GETDIRENTRIES
1676 if ((read_directory_getdirentries( fd, io, buffer, length, single_entry, mask, restart_scan )) != -1)
1679 read_directory_readdir( fd, io, buffer, length, single_entry, mask, restart_scan );
1682 if (cwd == -1 || fchdir( cwd ) == -1) chdir( "/" );
1684 else io->u.Status = FILE_GetNtStatus();
1686 RtlLeaveCriticalSection( &dir_section );
1688 if (needs_close) close( fd );
1689 if (cwd != -1) close( cwd );
1690 TRACE( "=> %x (%ld)\n", io->u.Status, io->Information );
1691 return io->u.Status;
1695 /***********************************************************************
1698 * Find a file in a directory the hard way, by doing a case-insensitive search.
1699 * The file found is appended to unix_name at pos.
1700 * There must be at least MAX_DIR_ENTRY_LEN+2 chars available at pos.
1702 static NTSTATUS find_file_in_dir( char *unix_name, int pos, const WCHAR *name, int length,
1705 WCHAR buffer[MAX_DIR_ENTRY_LEN];
1711 int ret, used_default, is_name_8_dot_3;
1713 /* try a shortcut for this directory */
1715 unix_name[pos++] = '/';
1716 ret = ntdll_wcstoumbs( 0, name, length, unix_name + pos, MAX_DIR_ENTRY_LEN,
1717 NULL, &used_default );
1718 /* if we used the default char, the Unix name won't round trip properly back to Unicode */
1719 /* so it cannot match the file we are looking for */
1720 if (ret >= 0 && !used_default)
1722 unix_name[pos + ret] = 0;
1723 if (!stat( unix_name, &st )) return STATUS_SUCCESS;
1725 if (check_case) goto not_found; /* we want an exact match */
1727 if (pos > 1) unix_name[pos - 1] = 0;
1728 else unix_name[1] = 0; /* keep the initial slash */
1730 /* check if it fits in 8.3 so that we don't look for short names if we won't need them */
1732 str.Buffer = (WCHAR *)name;
1733 str.Length = length * sizeof(WCHAR);
1734 str.MaximumLength = str.Length;
1735 is_name_8_dot_3 = RtlIsNameLegalDOS8Dot3( &str, NULL, &spaces ) && !spaces;
1737 /* now look for it through the directory */
1739 #ifdef VFAT_IOCTL_READDIR_BOTH
1740 if (is_name_8_dot_3)
1742 int fd = open( unix_name, O_RDONLY | O_DIRECTORY );
1747 RtlEnterCriticalSection( &dir_section );
1748 if ((de = start_vfat_ioctl( fd )))
1750 unix_name[pos - 1] = '/';
1751 while (de[0].d_reclen)
1753 /* make sure names are null-terminated to work around an x86-64 kernel bug */
1754 size_t len = min(de[0].d_reclen, sizeof(de[0].d_name) - 1 );
1755 de[0].d_name[len] = 0;
1756 len = min(de[1].d_reclen, sizeof(de[1].d_name) - 1 );
1757 de[1].d_name[len] = 0;
1759 if (de[1].d_name[0])
1761 ret = ntdll_umbstowcs( 0, de[1].d_name, strlen(de[1].d_name),
1762 buffer, MAX_DIR_ENTRY_LEN );
1763 if (ret == length && !memicmpW( buffer, name, length))
1765 strcpy( unix_name + pos, de[1].d_name );
1766 RtlLeaveCriticalSection( &dir_section );
1768 return STATUS_SUCCESS;
1771 ret = ntdll_umbstowcs( 0, de[0].d_name, strlen(de[0].d_name),
1772 buffer, MAX_DIR_ENTRY_LEN );
1773 if (ret == length && !memicmpW( buffer, name, length))
1775 strcpy( unix_name + pos,
1776 de[1].d_name[0] ? de[1].d_name : de[0].d_name );
1777 RtlLeaveCriticalSection( &dir_section );
1779 return STATUS_SUCCESS;
1781 if (ioctl( fd, VFAT_IOCTL_READDIR_BOTH, (long)de ) == -1)
1783 RtlLeaveCriticalSection( &dir_section );
1789 RtlLeaveCriticalSection( &dir_section );
1792 /* fall through to normal handling */
1794 #endif /* VFAT_IOCTL_READDIR_BOTH */
1796 if (!(dir = opendir( unix_name )))
1798 if (errno == ENOENT) return STATUS_OBJECT_PATH_NOT_FOUND;
1799 else return FILE_GetNtStatus();
1801 unix_name[pos - 1] = '/';
1802 str.Buffer = buffer;
1803 str.MaximumLength = sizeof(buffer);
1804 while ((de = readdir( dir )))
1806 ret = ntdll_umbstowcs( 0, de->d_name, strlen(de->d_name), buffer, MAX_DIR_ENTRY_LEN );
1807 if (ret == length && !memicmpW( buffer, name, length ))
1809 strcpy( unix_name + pos, de->d_name );
1811 return STATUS_SUCCESS;
1814 if (!is_name_8_dot_3) continue;
1816 str.Length = ret * sizeof(WCHAR);
1817 if (!RtlIsNameLegalDOS8Dot3( &str, NULL, &spaces ) || spaces)
1819 WCHAR short_nameW[12];
1820 ret = hash_short_file_name( &str, short_nameW );
1821 if (ret == length && !memicmpW( short_nameW, name, length ))
1823 strcpy( unix_name + pos, de->d_name );
1825 return STATUS_SUCCESS;
1830 goto not_found; /* avoid warning */
1833 unix_name[pos - 1] = 0;
1834 return STATUS_OBJECT_PATH_NOT_FOUND;
1838 /******************************************************************************
1841 * Get the Unix path of a DOS device.
1843 static NTSTATUS get_dos_device( const WCHAR *name, UINT name_len, ANSI_STRING *unix_name_ret )
1845 const char *config_dir = wine_get_config_dir();
1847 char *unix_name, *new_name, *dev;
1851 /* make sure the device name is ASCII */
1852 for (i = 0; i < name_len; i++)
1853 if (name[i] <= 32 || name[i] >= 127) return STATUS_BAD_DEVICE_TYPE;
1855 unix_len = strlen(config_dir) + sizeof("/dosdevices/") + name_len + 1;
1857 if (!(unix_name = RtlAllocateHeap( GetProcessHeap(), 0, unix_len )))
1858 return STATUS_NO_MEMORY;
1860 strcpy( unix_name, config_dir );
1861 strcat( unix_name, "/dosdevices/" );
1862 dev = unix_name + strlen(unix_name);
1864 for (i = 0; i < name_len; i++) dev[i] = (char)tolowerW(name[i]);
1867 /* special case for drive devices */
1868 if (name_len == 2 && dev[1] == ':')
1876 if (!stat( unix_name, &st ))
1878 TRACE( "%s -> %s\n", debugstr_wn(name,name_len), debugstr_a(unix_name) );
1879 unix_name_ret->Buffer = unix_name;
1880 unix_name_ret->Length = strlen(unix_name);
1881 unix_name_ret->MaximumLength = unix_len;
1882 return STATUS_SUCCESS;
1886 /* now try some defaults for it */
1887 if (!strcmp( dev, "aux" ))
1889 strcpy( dev, "com1" );
1892 if (!strcmp( dev, "prn" ))
1894 strcpy( dev, "lpt1" );
1897 if (!strcmp( dev, "nul" ))
1899 strcpy( unix_name, "/dev/null" );
1900 dev = NULL; /* last try */
1905 if (dev[1] == ':' && dev[2] == ':') /* drive device */
1907 dev[2] = 0; /* remove last ':' to get the drive mount point symlink */
1908 new_name = get_default_drive_device( unix_name );
1910 else if (!strncmp( dev, "com", 3 )) new_name = get_default_com_device( atoi(dev + 3 ));
1911 else if (!strncmp( dev, "lpt", 3 )) new_name = get_default_lpt_device( atoi(dev + 3 ));
1913 if (!new_name) break;
1915 RtlFreeHeap( GetProcessHeap(), 0, unix_name );
1916 unix_name = new_name;
1917 unix_len = strlen(unix_name) + 1;
1918 dev = NULL; /* last try */
1920 RtlFreeHeap( GetProcessHeap(), 0, unix_name );
1921 return STATUS_BAD_DEVICE_TYPE;
1925 /* return the length of the DOS namespace prefix if any */
1926 static inline int get_dos_prefix_len( const UNICODE_STRING *name )
1928 static const WCHAR nt_prefixW[] = {'\\','?','?','\\'};
1929 static const WCHAR dosdev_prefixW[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\'};
1931 if (name->Length > sizeof(nt_prefixW) &&
1932 !memcmp( name->Buffer, nt_prefixW, sizeof(nt_prefixW) ))
1933 return sizeof(nt_prefixW) / sizeof(WCHAR);
1935 if (name->Length > sizeof(dosdev_prefixW) &&
1936 !memicmpW( name->Buffer, dosdev_prefixW, sizeof(dosdev_prefixW)/sizeof(WCHAR) ))
1937 return sizeof(dosdev_prefixW) / sizeof(WCHAR);
1943 /******************************************************************************
1944 * wine_nt_to_unix_file_name (NTDLL.@) Not a Windows API
1946 * Convert a file name from NT namespace to Unix namespace.
1948 * If disposition is not FILE_OPEN or FILE_OVERWRITE, the last path
1949 * element doesn't have to exist; in that case STATUS_NO_SUCH_FILE is
1950 * returned, but the unix name is still filled in properly.
1952 NTSTATUS CDECL wine_nt_to_unix_file_name( const UNICODE_STRING *nameW, ANSI_STRING *unix_name_ret,
1953 UINT disposition, BOOLEAN check_case )
1955 static const WCHAR unixW[] = {'u','n','i','x'};
1956 static const WCHAR invalid_charsW[] = { INVALID_NT_CHARS, 0 };
1958 NTSTATUS status = STATUS_SUCCESS;
1959 const char *config_dir = wine_get_config_dir();
1960 const WCHAR *name, *p;
1963 int pos, ret, name_len, unix_len, prefix_len, used_default;
1964 WCHAR prefix[MAX_DIR_ENTRY_LEN];
1965 BOOLEAN is_unix = FALSE;
1967 name = nameW->Buffer;
1968 name_len = nameW->Length / sizeof(WCHAR);
1970 if (!name_len || !IS_SEPARATOR(name[0])) return STATUS_OBJECT_PATH_SYNTAX_BAD;
1972 if (!(pos = get_dos_prefix_len( nameW )))
1973 return STATUS_BAD_DEVICE_TYPE; /* no DOS prefix, assume NT native name */
1978 /* check for sub-directory */
1979 for (pos = 0; pos < name_len; pos++)
1981 if (IS_SEPARATOR(name[pos])) break;
1982 if (name[pos] < 32 || strchrW( invalid_charsW, name[pos] ))
1983 return STATUS_OBJECT_NAME_INVALID;
1985 if (pos > MAX_DIR_ENTRY_LEN)
1986 return STATUS_OBJECT_NAME_INVALID;
1988 if (pos == name_len) /* no subdir, plain DOS device */
1989 return get_dos_device( name, name_len, unix_name_ret );
1991 for (prefix_len = 0; prefix_len < pos; prefix_len++)
1992 prefix[prefix_len] = tolowerW(name[prefix_len]);
1995 name_len -= prefix_len;
1997 /* check for invalid characters (all chars except 0 are valid for unix) */
1998 is_unix = (prefix_len == 4 && !memcmp( prefix, unixW, sizeof(unixW) ));
2001 for (p = name; p < name + name_len; p++)
2002 if (!*p) return STATUS_OBJECT_NAME_INVALID;
2007 for (p = name; p < name + name_len; p++)
2008 if (*p < 32 || strchrW( invalid_charsW, *p )) return STATUS_OBJECT_NAME_INVALID;
2011 unix_len = ntdll_wcstoumbs( 0, prefix, prefix_len, NULL, 0, NULL, NULL );
2012 unix_len += ntdll_wcstoumbs( 0, name, name_len, NULL, 0, NULL, NULL );
2013 unix_len += MAX_DIR_ENTRY_LEN + 3;
2014 unix_len += strlen(config_dir) + sizeof("/dosdevices/");
2015 if (!(unix_name = RtlAllocateHeap( GetProcessHeap(), 0, unix_len )))
2016 return STATUS_NO_MEMORY;
2017 strcpy( unix_name, config_dir );
2018 strcat( unix_name, "/dosdevices/" );
2019 pos = strlen(unix_name);
2021 ret = ntdll_wcstoumbs( 0, prefix, prefix_len, unix_name + pos, unix_len - pos - 1,
2022 NULL, &used_default );
2023 if (!ret || used_default)
2025 RtlFreeHeap( GetProcessHeap(), 0, unix_name );
2026 return STATUS_OBJECT_NAME_INVALID;
2030 /* check if prefix exists (except for DOS drives to avoid extra stat calls) */
2032 if (prefix_len != 2 || prefix[1] != ':')
2035 if (lstat( unix_name, &st ) == -1 && errno == ENOENT)
2039 RtlFreeHeap( GetProcessHeap(), 0, unix_name );
2040 return STATUS_BAD_DEVICE_TYPE;
2042 pos = 0; /* fall back to unix root */
2046 /* try a shortcut first */
2048 ret = ntdll_wcstoumbs( 0, name, name_len, unix_name + pos, unix_len - pos - 1,
2049 NULL, &used_default );
2051 while (name_len && IS_SEPARATOR(*name))
2057 if (ret > 0 && !used_default) /* if we used the default char the name didn't convert properly */
2060 unix_name[pos + ret] = 0;
2061 for (p = unix_name + pos ; *p; p++) if (*p == '\\') *p = '/';
2062 if (!stat( unix_name, &st ))
2064 /* creation fails with STATUS_ACCESS_DENIED for the root of the drive */
2065 if (disposition == FILE_CREATE)
2067 RtlFreeHeap( GetProcessHeap(), 0, unix_name );
2068 return name_len ? STATUS_OBJECT_NAME_COLLISION : STATUS_ACCESS_DENIED;
2074 if (!name_len) /* empty name -> drive root doesn't exist */
2076 RtlFreeHeap( GetProcessHeap(), 0, unix_name );
2077 return STATUS_OBJECT_PATH_NOT_FOUND;
2079 if (check_case && (disposition == FILE_OPEN || disposition == FILE_OVERWRITE))
2081 RtlFreeHeap( GetProcessHeap(), 0, unix_name );
2082 return STATUS_OBJECT_NAME_NOT_FOUND;
2085 /* now do it component by component */
2089 const WCHAR *end, *next;
2092 while (end < name + name_len && !IS_SEPARATOR(*end)) end++;
2094 while (next < name + name_len && IS_SEPARATOR(*next)) next++;
2095 name_len -= next - name;
2097 /* grow the buffer if needed */
2099 if (unix_len - pos < MAX_DIR_ENTRY_LEN + 2)
2102 unix_len += 2 * MAX_DIR_ENTRY_LEN;
2103 if (!(new_name = RtlReAllocateHeap( GetProcessHeap(), 0, unix_name, unix_len )))
2105 RtlFreeHeap( GetProcessHeap(), 0, unix_name );
2106 return STATUS_NO_MEMORY;
2108 unix_name = new_name;
2111 status = find_file_in_dir( unix_name, pos, name, end - name, check_case );
2113 /* if this is the last element, not finding it is not necessarily fatal */
2116 if (status == STATUS_OBJECT_PATH_NOT_FOUND)
2118 status = STATUS_OBJECT_NAME_NOT_FOUND;
2119 if (disposition != FILE_OPEN && disposition != FILE_OVERWRITE)
2121 ret = ntdll_wcstoumbs( 0, name, end - name, unix_name + pos + 1,
2122 MAX_DIR_ENTRY_LEN, NULL, &used_default );
2123 if (ret > 0 && !used_default)
2125 unix_name[pos] = '/';
2126 unix_name[pos + 1 + ret] = 0;
2127 status = STATUS_NO_SUCH_FILE;
2132 else if (status == STATUS_SUCCESS && disposition == FILE_CREATE)
2134 status = STATUS_OBJECT_NAME_COLLISION;
2138 if (status != STATUS_SUCCESS)
2140 /* couldn't find it at all, fail */
2141 WARN( "%s not found in %s\n", debugstr_w(name), unix_name );
2142 RtlFreeHeap( GetProcessHeap(), 0, unix_name );
2146 pos += strlen( unix_name + pos );
2150 WARN( "%s -> %s required a case-insensitive search\n",
2151 debugstr_us(nameW), debugstr_a(unix_name) );
2154 TRACE( "%s -> %s\n", debugstr_us(nameW), debugstr_a(unix_name) );
2155 unix_name_ret->Buffer = unix_name;
2156 unix_name_ret->Length = strlen(unix_name);
2157 unix_name_ret->MaximumLength = unix_len;
2162 /******************************************************************
2163 * RtlWow64EnableFsRedirection (NTDLL.@)
2165 NTSTATUS WINAPI RtlWow64EnableFsRedirection( BOOLEAN enable )
2167 if (!is_wow64) return STATUS_NOT_IMPLEMENTED;
2168 ntdll_get_thread_data()->wow64_redir = enable;
2169 return STATUS_SUCCESS;
2173 /******************************************************************
2174 * RtlWow64EnableFsRedirectionEx (NTDLL.@)
2176 NTSTATUS WINAPI RtlWow64EnableFsRedirectionEx( ULONG enable, ULONG *old_value )
2178 if (!is_wow64) return STATUS_NOT_IMPLEMENTED;
2179 *old_value = ntdll_get_thread_data()->wow64_redir;
2180 ntdll_get_thread_data()->wow64_redir = enable;
2181 return STATUS_SUCCESS;
2185 /******************************************************************
2186 * RtlDoesFileExists_U (NTDLL.@)
2188 BOOLEAN WINAPI RtlDoesFileExists_U(LPCWSTR file_name)
2190 UNICODE_STRING nt_name;
2191 FILE_BASIC_INFORMATION basic_info;
2192 OBJECT_ATTRIBUTES attr;
2195 if (!RtlDosPathNameToNtPathName_U( file_name, &nt_name, NULL, NULL )) return FALSE;
2197 attr.Length = sizeof(attr);
2198 attr.RootDirectory = 0;
2199 attr.ObjectName = &nt_name;
2200 attr.Attributes = OBJ_CASE_INSENSITIVE;
2201 attr.SecurityDescriptor = NULL;
2202 attr.SecurityQualityOfService = NULL;
2204 ret = NtQueryAttributesFile(&attr, &basic_info) == STATUS_SUCCESS;
2206 RtlFreeUnicodeString( &nt_name );
2211 /***********************************************************************
2212 * DIR_unmount_device
2214 * Unmount the specified device.
2216 NTSTATUS DIR_unmount_device( HANDLE handle )
2219 int unix_fd, needs_close;
2221 if (!(status = server_get_unix_fd( handle, 0, &unix_fd, &needs_close, NULL, NULL )))
2224 char *mount_point = NULL;
2226 if (fstat( unix_fd, &st ) == -1 || !is_valid_mounted_device( &st ))
2227 status = STATUS_INVALID_PARAMETER;
2230 if ((mount_point = get_device_mount_point( st.st_rdev )))
2233 static const char umount[] = "diskutil unmount >/dev/null 2>&1 ";
2235 static const char umount[] = "umount >/dev/null 2>&1 ";
2237 char *cmd = RtlAllocateHeap( GetProcessHeap(), 0, strlen(mount_point)+sizeof(umount));
2240 strcpy( cmd, umount );
2241 strcat( cmd, mount_point );
2243 RtlFreeHeap( GetProcessHeap(), 0, cmd );
2245 /* umount will fail to release the loop device since we still have
2246 a handle to it, so we release it here */
2247 if (major(st.st_rdev) == LOOP_MAJOR) ioctl( unix_fd, 0x4c01 /*LOOP_CLR_FD*/, 0 );
2250 RtlFreeHeap( GetProcessHeap(), 0, mount_point );
2253 if (needs_close) close( unix_fd );
2259 /******************************************************************************
2262 * Retrieve the Unix name of the current directory; helper for wine_unix_to_nt_file_name.
2263 * Returned value must be freed by caller.
2265 NTSTATUS DIR_get_unix_cwd( char **cwd )
2267 int old_cwd, unix_fd, needs_close;
2272 RtlAcquirePebLock();
2274 if (NtCurrentTeb()->Tib.SubSystemTib) /* FIXME: hack */
2275 curdir = &((WIN16_SUBSYSTEM_TIB *)NtCurrentTeb()->Tib.SubSystemTib)->curdir;
2277 curdir = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectory;
2279 if (!(handle = curdir->Handle))
2281 UNICODE_STRING dirW;
2282 OBJECT_ATTRIBUTES attr;
2285 if (!RtlDosPathNameToNtPathName_U( curdir->DosPath.Buffer, &dirW, NULL, NULL ))
2287 status = STATUS_OBJECT_NAME_INVALID;
2290 attr.Length = sizeof(attr);
2291 attr.RootDirectory = 0;
2292 attr.Attributes = OBJ_CASE_INSENSITIVE;
2293 attr.ObjectName = &dirW;
2294 attr.SecurityDescriptor = NULL;
2295 attr.SecurityQualityOfService = NULL;
2297 status = NtOpenFile( &handle, 0, &attr, &io, 0,
2298 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
2299 RtlFreeUnicodeString( &dirW );
2300 if (status != STATUS_SUCCESS) goto done;
2303 if ((status = server_get_unix_fd( handle, 0, &unix_fd, &needs_close, NULL, NULL )) == STATUS_SUCCESS)
2305 RtlEnterCriticalSection( &dir_section );
2307 if ((old_cwd = open(".", O_RDONLY)) != -1 && fchdir( unix_fd ) != -1)
2309 unsigned int size = 512;
2313 if (!(*cwd = RtlAllocateHeap( GetProcessHeap(), 0, size )))
2315 status = STATUS_NO_MEMORY;
2318 if (getcwd( *cwd, size )) break;
2319 RtlFreeHeap( GetProcessHeap(), 0, *cwd );
2320 if (errno != ERANGE)
2322 status = STATUS_OBJECT_PATH_INVALID;
2327 if (fchdir( old_cwd ) == -1) chdir( "/" );
2329 else status = FILE_GetNtStatus();
2331 RtlLeaveCriticalSection( &dir_section );
2332 if (needs_close) close( unix_fd );
2334 if (!curdir->Handle) NtClose( handle );
2337 RtlReleasePebLock();
2341 struct read_changes_info
2346 PIO_APC_ROUTINE apc;
2350 /* callback for ioctl user APC */
2351 static void WINAPI read_changes_user_apc( void *arg, IO_STATUS_BLOCK *io, ULONG reserved )
2353 struct read_changes_info *info = arg;
2354 if (info->apc) info->apc( info->apc_arg, io, reserved );
2355 RtlFreeHeap( GetProcessHeap(), 0, info );
2358 static NTSTATUS read_changes_apc( void *user, PIO_STATUS_BLOCK iosb, NTSTATUS status, void **apc )
2360 struct read_changes_info *info = user;
2361 char path[PATH_MAX];
2362 NTSTATUS ret = STATUS_SUCCESS;
2365 SERVER_START_REQ( read_change )
2367 req->handle = wine_server_obj_handle( info->FileHandle );
2368 wine_server_set_reply( req, path, PATH_MAX );
2369 ret = wine_server_call( req );
2370 action = reply->action;
2371 len = wine_server_reply_size( reply );
2375 if (ret == STATUS_SUCCESS && info->Buffer &&
2376 (info->BufferSize > (sizeof (FILE_NOTIFY_INFORMATION) + len*sizeof(WCHAR))))
2378 PFILE_NOTIFY_INFORMATION pfni;
2380 pfni = info->Buffer;
2382 /* convert to an NT style path */
2383 for (i=0; i<len; i++)
2387 len = ntdll_umbstowcs( 0, path, len, pfni->FileName,
2388 info->BufferSize - sizeof (*pfni) );
2390 pfni->NextEntryOffset = 0;
2391 pfni->Action = action;
2392 pfni->FileNameLength = len * sizeof (WCHAR);
2393 pfni->FileName[len] = 0;
2394 len = sizeof (*pfni) - sizeof (DWORD) + pfni->FileNameLength;
2398 ret = STATUS_NOTIFY_ENUM_DIR;
2402 iosb->u.Status = ret;
2403 iosb->Information = len;
2404 *apc = read_changes_user_apc;
2408 #define FILE_NOTIFY_ALL ( \
2409 FILE_NOTIFY_CHANGE_FILE_NAME | \
2410 FILE_NOTIFY_CHANGE_DIR_NAME | \
2411 FILE_NOTIFY_CHANGE_ATTRIBUTES | \
2412 FILE_NOTIFY_CHANGE_SIZE | \
2413 FILE_NOTIFY_CHANGE_LAST_WRITE | \
2414 FILE_NOTIFY_CHANGE_LAST_ACCESS | \
2415 FILE_NOTIFY_CHANGE_CREATION | \
2416 FILE_NOTIFY_CHANGE_SECURITY )
2418 /******************************************************************************
2419 * NtNotifyChangeDirectoryFile [NTDLL.@]
2422 NtNotifyChangeDirectoryFile( HANDLE FileHandle, HANDLE Event,
2423 PIO_APC_ROUTINE ApcRoutine, PVOID ApcContext,
2424 PIO_STATUS_BLOCK IoStatusBlock, PVOID Buffer,
2425 ULONG BufferSize, ULONG CompletionFilter, BOOLEAN WatchTree )
2427 struct read_changes_info *info;
2429 ULONG_PTR cvalue = ApcRoutine ? 0 : (ULONG_PTR)ApcContext;
2431 TRACE("%p %p %p %p %p %p %u %u %d\n",
2432 FileHandle, Event, ApcRoutine, ApcContext, IoStatusBlock,
2433 Buffer, BufferSize, CompletionFilter, WatchTree );
2436 return STATUS_ACCESS_VIOLATION;
2438 if (CompletionFilter == 0 || (CompletionFilter & ~FILE_NOTIFY_ALL))
2439 return STATUS_INVALID_PARAMETER;
2441 info = RtlAllocateHeap( GetProcessHeap(), 0, sizeof *info );
2443 return STATUS_NO_MEMORY;
2445 info->FileHandle = FileHandle;
2446 info->Buffer = Buffer;
2447 info->BufferSize = BufferSize;
2448 info->apc = ApcRoutine;
2449 info->apc_arg = ApcContext;
2451 SERVER_START_REQ( read_directory_changes )
2453 req->filter = CompletionFilter;
2454 req->want_data = (Buffer != NULL);
2455 req->subtree = WatchTree;
2456 req->async.handle = wine_server_obj_handle( FileHandle );
2457 req->async.callback = wine_server_client_ptr( read_changes_apc );
2458 req->async.iosb = wine_server_client_ptr( IoStatusBlock );
2459 req->async.arg = wine_server_client_ptr( info );
2460 req->async.event = wine_server_obj_handle( Event );
2461 req->async.cvalue = cvalue;
2462 status = wine_server_call( req );
2466 if (status != STATUS_PENDING)
2467 RtlFreeHeap( GetProcessHeap(), 0, info );