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"
26 #include <sys/types.h>
40 #ifdef HAVE_SYS_STAT_H
41 # include <sys/stat.h>
43 #ifdef HAVE_SYS_IOCTL_H
44 #include <sys/ioctl.h>
46 #ifdef HAVE_LINUX_IOCTL_H
47 #include <linux/ioctl.h>
49 #ifdef HAVE_LINUX_MAJOR_H
50 # include <linux/major.h>
52 #ifdef HAVE_SYS_PARAM_H
53 #include <sys/param.h>
55 #ifdef HAVE_SYS_MOUNT_H
56 #include <sys/mount.h>
63 #define NONAMELESSUNION
64 #define NONAMELESSSTRUCT
66 #define WIN32_NO_STATUS
70 #include "ntdll_misc.h"
71 #include "wine/unicode.h"
72 #include "wine/server.h"
73 #include "wine/library.h"
74 #include "wine/debug.h"
76 WINE_DEFAULT_DEBUG_CHANNEL(file
);
79 #undef VFAT_IOCTL_READDIR_BOTH
84 /* We want the real kernel dirent structure, not the libc one */
89 unsigned short d_reclen
;
93 /* Define the VFAT ioctl to get both short and long file names */
94 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, KERNEL_DIRENT [2] )
97 # define O_DIRECTORY 0200000 /* must be directory */
106 unsigned short d_reclen
;
107 unsigned char d_type
;
111 static inline int getdents64( int fd
, char *de
, unsigned int size
)
114 __asm__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
116 : "0" (220 /*NR_getdents64*/), "r" (fd
), "c" (de
), "d" (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 static const unsigned int max_dir_info_size
= FIELD_OFFSET( FILE_BOTH_DIR_INFORMATION
, FileName
[MAX_DIR_ENTRY_LEN
] );
141 static int show_dot_files
= -1;
143 /* at some point we may want to allow Winelib apps to set this */
144 static const int is_case_sensitive
= FALSE
;
146 static RTL_CRITICAL_SECTION dir_section
;
147 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
150 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
151 0, 0, { (DWORD_PTR
)(__FILE__
": dir_section") }
153 static RTL_CRITICAL_SECTION dir_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
156 /* check if a given Unicode char is OK in a DOS short name */
157 static inline BOOL
is_invalid_dos_char( WCHAR ch
)
159 static const WCHAR invalid_chars
[] = { INVALID_DOS_CHARS
,'~','.',0 };
160 if (ch
> 0x7f) return TRUE
;
161 return strchrW( invalid_chars
, ch
) != NULL
;
164 /* check if the device can be a mounted volume */
165 static inline int is_valid_mounted_device( const struct stat
*st
)
167 #if defined(linux) || defined(__sun__)
168 return S_ISBLK( st
->st_mode
);
170 /* disks are char devices on *BSD */
171 return S_ISCHR( st
->st_mode
);
175 /***********************************************************************
176 * get_default_com_device
178 * Return the default device to use for serial ports.
180 static char *get_default_com_device( int num
)
184 if (!num
|| num
> 9) return ret
;
186 ret
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/ttyS0") );
189 strcpy( ret
, "/dev/ttyS0" );
190 ret
[strlen(ret
) - 1] = '0' + num
- 1;
192 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
193 ret
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/cuad0") );
196 strcpy( ret
, "/dev/cuad0" );
197 ret
[strlen(ret
) - 1] = '0' + num
- 1;
200 FIXME( "no known default for device com%d\n", num
);
206 /***********************************************************************
207 * get_default_lpt_device
209 * Return the default device to use for parallel ports.
211 static char *get_default_lpt_device( int num
)
215 if (!num
|| num
> 9) return ret
;
217 ret
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/lp0") );
220 strcpy( ret
, "/dev/lp0" );
221 ret
[strlen(ret
) - 1] = '0' + num
- 1;
224 FIXME( "no known default for device lpt%d\n", num
);
230 /***********************************************************************
231 * DIR_get_drives_info
233 * Retrieve device/inode number for all the drives. Helper for find_drive_root.
235 unsigned int DIR_get_drives_info( struct drive_info info
[MAX_DOS_DRIVES
] )
237 static struct drive_info cache
[MAX_DOS_DRIVES
];
238 static time_t last_update
;
239 static unsigned int nb_drives
;
241 time_t now
= time(NULL
);
243 RtlEnterCriticalSection( &dir_section
);
244 if (now
!= last_update
)
246 const char *config_dir
= wine_get_config_dir();
251 if ((buffer
= RtlAllocateHeap( GetProcessHeap(), 0,
252 strlen(config_dir
) + sizeof("/dosdevices/a:") )))
254 strcpy( buffer
, config_dir
);
255 strcat( buffer
, "/dosdevices/a:" );
256 p
= buffer
+ strlen(buffer
) - 2;
258 for (i
= nb_drives
= 0; i
< MAX_DOS_DRIVES
; i
++)
261 if (!stat( buffer
, &st
))
263 cache
[i
].dev
= st
.st_dev
;
264 cache
[i
].ino
= st
.st_ino
;
273 RtlFreeHeap( GetProcessHeap(), 0, buffer
);
277 memcpy( info
, cache
, sizeof(cache
) );
279 RtlLeaveCriticalSection( &dir_section
);
284 /***********************************************************************
285 * parse_mount_entries
287 * Parse mount entries looking for a given device. Helper for get_default_drive_device.
291 #include <sys/vfstab.h>
292 static char *parse_vfstab_entries( FILE *f
, dev_t dev
, ino_t ino
)
298 while (! getvfsent( f
, &entry
))
300 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
301 if (!strcmp( entry
.vfs_fstype
, "nfs" ) ||
302 !strcmp( entry
.vfs_fstype
, "smbfs" ) ||
303 !strcmp( entry
.vfs_fstype
, "ncpfs" )) continue;
305 if (stat( entry
.vfs_mountp
, &st
) == -1) continue;
306 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
307 if (!strcmp( entry
.vfs_fstype
, "fd" ))
309 if ((device
= strstr( entry
.vfs_mntopts
, "dev=" )))
311 char *p
= strchr( device
+ 4, ',' );
317 return entry
.vfs_special
;
324 static char *parse_mount_entries( FILE *f
, dev_t dev
, ino_t ino
)
326 struct mntent
*entry
;
330 while ((entry
= getmntent( f
)))
332 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
333 if (!strcmp( entry
->mnt_type
, "nfs" ) ||
334 !strcmp( entry
->mnt_type
, "smbfs" ) ||
335 !strcmp( entry
->mnt_type
, "ncpfs" )) continue;
337 if (stat( entry
->mnt_dir
, &st
) == -1) continue;
338 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
339 if (!strcmp( entry
->mnt_type
, "supermount" ))
341 if ((device
= strstr( entry
->mnt_opts
, "dev=" )))
343 char *p
= strchr( device
+ 4, ',' );
348 else if (!stat( entry
->mnt_fsname
, &st
) && S_ISREG(st
.st_mode
))
350 /* if device is a regular file check for a loop mount */
351 if ((device
= strstr( entry
->mnt_opts
, "loop=" )))
353 char *p
= strchr( device
+ 5, ',' );
359 return entry
->mnt_fsname
;
365 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
367 static char *parse_mount_entries( FILE *f
, dev_t dev
, ino_t ino
)
372 while ((entry
= getfsent()))
374 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
375 if (!strcmp( entry
->fs_vfstype
, "nfs" ) ||
376 !strcmp( entry
->fs_vfstype
, "smbfs" ) ||
377 !strcmp( entry
->fs_vfstype
, "ncpfs" )) continue;
379 if (stat( entry
->fs_file
, &st
) == -1) continue;
380 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
381 return entry
->fs_spec
;
388 #include <sys/mnttab.h>
389 static char *parse_mount_entries( FILE *f
, dev_t dev
, ino_t ino
)
396 while (( ! getmntent( f
, &entry
) ))
398 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
399 if (!strcmp( entry
.mnt_fstype
, "nfs" ) ||
400 !strcmp( entry
.mnt_fstype
, "smbfs" ) ||
401 !strcmp( entry
.mnt_fstype
, "ncpfs" )) continue;
403 if (stat( entry
.mnt_mountp
, &st
) == -1) continue;
404 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
405 if (!strcmp( entry
.mnt_fstype
, "fd" ))
407 if ((device
= strstr( entry
.mnt_mntopts
, "dev=" )))
409 char *p
= strchr( device
+ 4, ',' );
415 return entry
.mnt_special
;
421 /***********************************************************************
422 * get_default_drive_device
424 * Return the default device to use for a given drive mount point.
426 static char *get_default_drive_device( const char *root
)
436 /* try to open it first to force it to get mounted */
437 if ((fd
= open( root
, O_RDONLY
| O_DIRECTORY
)) != -1)
439 res
= fstat( fd
, &st
);
442 /* now try normal stat just in case */
443 if (res
== -1) res
= stat( root
, &st
);
444 if (res
== -1) return NULL
;
446 RtlEnterCriticalSection( &dir_section
);
448 if ((f
= fopen( "/etc/mtab", "r" )))
450 device
= parse_mount_entries( f
, st
.st_dev
, st
.st_ino
);
453 /* look through fstab too in case it's not mounted (for instance if it's an audio CD) */
454 if (!device
&& (f
= fopen( "/etc/fstab", "r" )))
456 device
= parse_mount_entries( f
, st
.st_dev
, st
.st_ino
);
461 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(device
) + 1 );
462 if (ret
) strcpy( ret
, device
);
464 RtlLeaveCriticalSection( &dir_section
);
466 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__ )
471 /* try to open it first to force it to get mounted */
472 if ((fd
= open( root
, O_RDONLY
)) != -1)
474 res
= fstat( fd
, &st
);
477 /* now try normal stat just in case */
478 if (res
== -1) res
= stat( root
, &st
);
479 if (res
== -1) return NULL
;
481 RtlEnterCriticalSection( &dir_section
);
483 /* The FreeBSD parse_mount_entries doesn't require a file argument, so just
484 * pass NULL. Leave the argument in for symmetry.
486 device
= parse_mount_entries( NULL
, st
.st_dev
, st
.st_ino
);
489 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(device
) + 1 );
490 if (ret
) strcpy( ret
, device
);
492 RtlLeaveCriticalSection( &dir_section
);
500 /* try to open it first to force it to get mounted */
501 if ((fd
= open( root
, O_RDONLY
)) != -1)
503 res
= fstat( fd
, &st
);
506 /* now try normal stat just in case */
507 if (res
== -1) res
= stat( root
, &st
);
508 if (res
== -1) return NULL
;
510 RtlEnterCriticalSection( &dir_section
);
512 if ((f
= fopen( "/etc/mnttab", "r" )))
514 device
= parse_mount_entries( f
, st
.st_dev
, st
.st_ino
);
517 /* look through fstab too in case it's not mounted (for instance if it's an audio CD) */
518 if (!device
&& (f
= fopen( "/etc/vfstab", "r" )))
520 device
= parse_vfstab_entries( f
, st
.st_dev
, st
.st_ino
);
525 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(device
) + 1 );
526 if (ret
) strcpy( ret
, device
);
528 RtlLeaveCriticalSection( &dir_section
);
530 #elif defined(__APPLE__)
531 struct statfs
*mntStat
;
537 static const char path_bsd_device
[] = "/dev/disk";
540 res
= stat( root
, &st
);
541 if (res
== -1) return NULL
;
546 RtlEnterCriticalSection( &dir_section
);
548 mntSize
= getmntinfo(&mntStat
, MNT_NOWAIT
);
550 for (i
= 0; i
< mntSize
&& !ret
; i
++)
552 if (stat(mntStat
[i
].f_mntonname
, &st
) == -1) continue;
553 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
555 /* FIXME add support for mounted network drive */
556 if ( strncmp(mntStat
[i
].f_mntfromname
, path_bsd_device
, strlen(path_bsd_device
)) == 0)
558 /* set return value to the corresponding raw BSD node */
559 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(mntStat
[i
].f_mntfromname
) + 2 /* 2 : r and \0 */ );
562 strcpy(ret
, "/dev/r");
563 strcat(ret
, mntStat
[i
].f_mntfromname
+sizeof("/dev/")-1);
567 RtlLeaveCriticalSection( &dir_section
);
570 if (!warned
++) FIXME( "auto detection of DOS devices not supported on this platform\n" );
576 /***********************************************************************
577 * get_device_mount_point
579 * Return the current mount point for a device.
581 static char *get_device_mount_point( dev_t dev
)
588 RtlEnterCriticalSection( &dir_section
);
590 if ((f
= fopen( "/etc/mtab", "r" )))
592 struct mntent
*entry
;
596 while ((entry
= getmntent( f
)))
598 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
599 if (!strcmp( entry
->mnt_type
, "nfs" ) ||
600 !strcmp( entry
->mnt_type
, "smbfs" ) ||
601 !strcmp( entry
->mnt_type
, "ncpfs" )) continue;
603 if (!strcmp( entry
->mnt_type
, "supermount" ))
605 if ((device
= strstr( entry
->mnt_opts
, "dev=" )))
608 if ((p
= strchr( device
, ',' ))) *p
= 0;
611 else if (!stat( entry
->mnt_fsname
, &st
) && S_ISREG(st
.st_mode
))
613 /* if device is a regular file check for a loop mount */
614 if ((device
= strstr( entry
->mnt_opts
, "loop=" )))
617 if ((p
= strchr( device
, ',' ))) *p
= 0;
620 else device
= entry
->mnt_fsname
;
622 if (device
&& !stat( device
, &st
) && S_ISBLK(st
.st_mode
) && st
.st_rdev
== dev
)
624 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(entry
->mnt_dir
) + 1 );
625 if (ret
) strcpy( ret
, entry
->mnt_dir
);
631 RtlLeaveCriticalSection( &dir_section
);
632 #elif defined(__APPLE__)
633 struct statfs
*entry
;
637 RtlEnterCriticalSection( &dir_section
);
639 size
= getmntinfo( &entry
, MNT_NOWAIT
);
640 for (i
= 0; i
< size
; i
++)
642 if (stat( entry
[i
].f_mntfromname
, &st
) == -1) continue;
643 if (S_ISBLK(st
.st_mode
) && st
.st_rdev
== dev
)
645 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(entry
[i
].f_mntfromname
) + 1 );
646 if (ret
) strcpy( ret
, entry
[i
].f_mntfromname
);
650 RtlLeaveCriticalSection( &dir_section
);
653 if (!warned
++) FIXME( "unmounting devices not supported on this platform\n" );
659 /***********************************************************************
662 * Initialize the show_dot_files options.
664 static void init_options(void)
666 static const WCHAR WineW
[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e',0};
667 static const WCHAR ShowDotFilesW
[] = {'S','h','o','w','D','o','t','F','i','l','e','s',0};
671 OBJECT_ATTRIBUTES attr
;
672 UNICODE_STRING nameW
;
676 RtlOpenCurrentUser( KEY_ALL_ACCESS
, &root
);
677 attr
.Length
= sizeof(attr
);
678 attr
.RootDirectory
= root
;
679 attr
.ObjectName
= &nameW
;
681 attr
.SecurityDescriptor
= NULL
;
682 attr
.SecurityQualityOfService
= NULL
;
683 RtlInitUnicodeString( &nameW
, WineW
);
685 /* @@ Wine registry key: HKCU\Software\Wine */
686 if (!NtOpenKey( &hkey
, KEY_ALL_ACCESS
, &attr
))
688 RtlInitUnicodeString( &nameW
, ShowDotFilesW
);
689 if (!NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
, tmp
, sizeof(tmp
), &dummy
))
691 WCHAR
*str
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)tmp
)->Data
;
692 show_dot_files
= IS_OPTION_TRUE( str
[0] );
700 /***********************************************************************
703 * Check if the specified file should be hidden based on its name and the show dot files option.
705 BOOL
DIR_is_hidden_file( const UNICODE_STRING
*name
)
709 if (show_dot_files
== -1) init_options();
710 if (show_dot_files
) return FALSE
;
712 end
= p
= name
->Buffer
+ name
->Length
/sizeof(WCHAR
);
713 while (p
> name
->Buffer
&& IS_SEPARATOR(p
[-1])) p
--;
714 while (p
> name
->Buffer
&& !IS_SEPARATOR(p
[-1])) p
--;
715 if (p
== end
|| *p
!= '.') return FALSE
;
716 /* make sure it isn't '.' or '..' */
717 if (p
+ 1 == end
) return FALSE
;
718 if (p
[1] == '.' && p
+ 2 == end
) return FALSE
;
723 /***********************************************************************
724 * hash_short_file_name
726 * Transform a Unix file name into a hashed DOS name. If the name is a valid
727 * DOS name, it is converted to upper-case; otherwise it is replaced by a
728 * hashed version that fits in 8.3 format.
729 * 'buffer' must be at least 12 characters long.
730 * Returns length of short name in bytes; short name is NOT null-terminated.
732 static ULONG
hash_short_file_name( const UNICODE_STRING
*name
, LPWSTR buffer
)
734 static const char hash_chars
[32] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345";
736 LPCWSTR p
, ext
, end
= name
->Buffer
+ name
->Length
/ sizeof(WCHAR
);
741 /* Compute the hash code of the file name */
742 /* If you know something about hash functions, feel free to */
743 /* insert a better algorithm here... */
744 if (!is_case_sensitive
)
746 for (p
= name
->Buffer
, hash
= 0xbeef; p
< end
- 1; p
++)
747 hash
= (hash
<<3) ^ (hash
>>5) ^ tolowerW(*p
) ^ (tolowerW(p
[1]) << 8);
748 hash
= (hash
<<3) ^ (hash
>>5) ^ tolowerW(*p
); /* Last character */
752 for (p
= name
->Buffer
, hash
= 0xbeef; p
< end
- 1; p
++)
753 hash
= (hash
<< 3) ^ (hash
>> 5) ^ *p
^ (p
[1] << 8);
754 hash
= (hash
<< 3) ^ (hash
>> 5) ^ *p
; /* Last character */
757 /* Find last dot for start of the extension */
758 for (p
= name
->Buffer
+ 1, ext
= NULL
; p
< end
- 1; p
++) if (*p
== '.') ext
= p
;
760 /* Copy first 4 chars, replacing invalid chars with '_' */
761 for (i
= 4, p
= name
->Buffer
, dst
= buffer
; i
> 0; i
--, p
++)
763 if (p
== end
|| p
== ext
) break;
764 *dst
++ = is_invalid_dos_char(*p
) ? '_' : toupperW(*p
);
766 /* Pad to 5 chars with '~' */
767 while (i
-- >= 0) *dst
++ = '~';
769 /* Insert hash code converted to 3 ASCII chars */
770 *dst
++ = hash_chars
[(hash
>> 10) & 0x1f];
771 *dst
++ = hash_chars
[(hash
>> 5) & 0x1f];
772 *dst
++ = hash_chars
[hash
& 0x1f];
774 /* Copy the first 3 chars of the extension (if any) */
778 for (i
= 3, ext
++; (i
> 0) && ext
< end
; i
--, ext
++)
779 *dst
++ = is_invalid_dos_char(*ext
) ? '_' : toupperW(*ext
);
785 /***********************************************************************
788 * Check a long file name against a mask.
790 * Tests (done in W95 DOS shell - case insensitive):
791 * *.txt test1.test.txt *
793 * *.t??????.t* test1.ta.tornado.txt *
794 * *tornado* test1.ta.tornado.txt *
795 * t*t test1.ta.tornado.txt *
797 * ?est??? test1.txt -
798 * *test1.txt* test1.txt *
799 * h?l?o*t.dat hellothisisatest.dat *
801 static BOOLEAN
match_filename( const UNICODE_STRING
*name_str
, const UNICODE_STRING
*mask_str
)
804 const WCHAR
*name
= name_str
->Buffer
;
805 const WCHAR
*mask
= mask_str
->Buffer
;
806 const WCHAR
*name_end
= name
+ name_str
->Length
/ sizeof(WCHAR
);
807 const WCHAR
*mask_end
= mask
+ mask_str
->Length
/ sizeof(WCHAR
);
808 const WCHAR
*lastjoker
= NULL
;
809 const WCHAR
*next_to_retry
= NULL
;
811 TRACE("(%s, %s)\n", debugstr_us(name_str
), debugstr_us(mask_str
));
813 while (name
< name_end
&& mask
< mask_end
)
819 while (mask
< mask_end
&& *mask
== '*') mask
++; /* Skip consecutive '*' */
820 if (mask
== mask_end
) return TRUE
; /* end of mask is all '*', so match */
823 /* skip to the next match after the joker(s) */
824 if (is_case_sensitive
)
825 while (name
< name_end
&& (*name
!= *mask
)) name
++;
827 while (name
< name_end
&& (toupperW(*name
) != toupperW(*mask
))) name
++;
828 next_to_retry
= name
;
835 if (is_case_sensitive
) mismatch
= (*mask
!= *name
);
836 else mismatch
= (toupperW(*mask
) != toupperW(*name
));
842 if (mask
== mask_end
)
844 if (name
== name_end
) return TRUE
;
845 if (lastjoker
) mask
= lastjoker
;
848 else /* mismatch ! */
850 if (lastjoker
) /* we had an '*', so we can try unlimitedly */
854 /* this scan sequence was a mismatch, so restart
855 * 1 char after the first char we checked last time */
857 name
= next_to_retry
;
859 else return FALSE
; /* bad luck */
864 while (mask
< mask_end
&& ((*mask
== '.') || (*mask
== '*')))
865 mask
++; /* Ignore trailing '.' or '*' in mask */
866 return (name
== name_end
&& mask
== mask_end
);
870 /***********************************************************************
873 * helper for NtQueryDirectoryFile
875 static FILE_BOTH_DIR_INFORMATION
*append_entry( void *info_ptr
, ULONG_PTR
*pos
, ULONG max_length
,
876 const char *long_name
, const char *short_name
,
877 const UNICODE_STRING
*mask
)
879 FILE_BOTH_DIR_INFORMATION
*info
;
880 int i
, long_len
, short_len
, total_len
;
882 WCHAR long_nameW
[MAX_DIR_ENTRY_LEN
];
883 WCHAR short_nameW
[12];
886 long_len
= ntdll_umbstowcs( 0, long_name
, strlen(long_name
), long_nameW
, MAX_DIR_ENTRY_LEN
);
887 if (long_len
== -1) return NULL
;
889 str
.Buffer
= long_nameW
;
890 str
.Length
= long_len
* sizeof(WCHAR
);
891 str
.MaximumLength
= sizeof(long_nameW
);
895 short_len
= ntdll_umbstowcs( 0, short_name
, strlen(short_name
),
896 short_nameW
, sizeof(short_nameW
) / sizeof(WCHAR
) );
897 if (short_len
== -1) short_len
= sizeof(short_nameW
) / sizeof(WCHAR
);
899 else /* generate a short name if necessary */
904 if (!RtlIsNameLegalDOS8Dot3( &str
, NULL
, &spaces
) || spaces
)
905 short_len
= hash_short_file_name( &str
, short_nameW
);
908 TRACE( "long %s short %s mask %s\n",
909 debugstr_us(&str
), debugstr_wn(short_nameW
, short_len
), debugstr_us(mask
) );
911 if (mask
&& !match_filename( &str
, mask
))
913 if (!short_len
) return NULL
; /* no short name to match */
914 str
.Buffer
= short_nameW
;
915 str
.Length
= short_len
* sizeof(WCHAR
);
916 str
.MaximumLength
= sizeof(short_nameW
);
917 if (!match_filename( &str
, mask
)) return NULL
;
920 total_len
= (sizeof(*info
) - sizeof(info
->FileName
) + long_len
*sizeof(WCHAR
) + 3) & ~3;
921 info
= (FILE_BOTH_DIR_INFORMATION
*)((char *)info_ptr
+ *pos
);
923 if (*pos
+ total_len
> max_length
) total_len
= max_length
- *pos
;
925 info
->FileAttributes
= 0;
926 if (lstat( long_name
, &st
) == -1) return NULL
;
927 if (S_ISLNK( st
.st_mode
))
929 if (stat( long_name
, &st
) == -1) return NULL
;
930 if (S_ISDIR( st
.st_mode
)) info
->FileAttributes
|= FILE_ATTRIBUTE_REPARSE_POINT
;
933 info
->NextEntryOffset
= total_len
;
934 info
->FileIndex
= 0; /* NTFS always has 0 here, so let's not bother with it */
936 RtlSecondsSince1970ToTime( st
.st_mtime
, &info
->CreationTime
);
937 RtlSecondsSince1970ToTime( st
.st_mtime
, &info
->LastWriteTime
);
938 RtlSecondsSince1970ToTime( st
.st_atime
, &info
->LastAccessTime
);
939 RtlSecondsSince1970ToTime( st
.st_ctime
, &info
->ChangeTime
);
941 if (S_ISDIR(st
.st_mode
))
943 info
->EndOfFile
.QuadPart
= info
->AllocationSize
.QuadPart
= 0;
944 info
->FileAttributes
|= FILE_ATTRIBUTE_DIRECTORY
;
948 info
->EndOfFile
.QuadPart
= st
.st_size
;
949 info
->AllocationSize
.QuadPart
= (ULONGLONG
)st
.st_blocks
* 512;
950 info
->FileAttributes
|= FILE_ATTRIBUTE_ARCHIVE
;
953 if (!(st
.st_mode
& (S_IWUSR
| S_IWGRP
| S_IWOTH
)))
954 info
->FileAttributes
|= FILE_ATTRIBUTE_READONLY
;
956 if (!show_dot_files
&& long_name
[0] == '.' && long_name
[1] && (long_name
[1] != '.' || long_name
[2]))
957 info
->FileAttributes
|= FILE_ATTRIBUTE_HIDDEN
;
959 info
->EaSize
= 0; /* FIXME */
960 info
->ShortNameLength
= short_len
* sizeof(WCHAR
);
961 for (i
= 0; i
< short_len
; i
++) info
->ShortName
[i
] = toupperW(short_nameW
[i
]);
962 info
->FileNameLength
= long_len
* sizeof(WCHAR
);
963 memcpy( info
->FileName
, long_nameW
,
964 min( info
->FileNameLength
, total_len
-sizeof(*info
)+sizeof(info
->FileName
) ));
971 #ifdef VFAT_IOCTL_READDIR_BOTH
973 /***********************************************************************
976 * Wrapper for the VFAT ioctl to work around various kernel bugs.
977 * dir_section must be held by caller.
979 static KERNEL_DIRENT
*start_vfat_ioctl( int fd
)
981 static KERNEL_DIRENT
*de
;
986 const size_t page_size
= getpagesize();
987 SIZE_T size
= 2 * sizeof(*de
) + page_size
;
990 if (NtAllocateVirtualMemory( GetCurrentProcess(), &addr
, 1, &size
, MEM_RESERVE
, PAGE_READWRITE
))
992 /* commit only the size needed for the dir entries */
993 /* this leaves an extra unaccessible page, which should make the kernel */
994 /* fail with -EFAULT before it stomps all over our memory */
996 size
= 2 * sizeof(*de
);
997 NtAllocateVirtualMemory( GetCurrentProcess(), &addr
, 1, &size
, MEM_COMMIT
, PAGE_READWRITE
);
1000 /* set d_reclen to 65535 to work around an AFS kernel bug */
1001 de
[0].d_reclen
= 65535;
1002 res
= ioctl( fd
, VFAT_IOCTL_READDIR_BOTH
, (long)de
);
1005 if (errno
!= ENOENT
) return NULL
; /* VFAT ioctl probably not supported */
1006 de
[0].d_reclen
= 0; /* eof */
1008 else if (!res
&& de
[0].d_reclen
== 65535) return NULL
; /* AFS bug */
1014 /***********************************************************************
1015 * read_directory_vfat
1017 * Read a directory using the VFAT ioctl; helper for NtQueryDirectoryFile.
1019 static int read_directory_vfat( int fd
, IO_STATUS_BLOCK
*io
, void *buffer
, ULONG length
,
1020 BOOLEAN single_entry
, const UNICODE_STRING
*mask
,
1021 BOOLEAN restart_scan
)
1026 FILE_BOTH_DIR_INFORMATION
*info
, *last_info
= NULL
;
1028 io
->u
.Status
= STATUS_SUCCESS
;
1030 if (restart_scan
) lseek( fd
, 0, SEEK_SET
);
1032 if (length
< max_dir_info_size
) /* we may have to return a partial entry here */
1034 off_t old_pos
= lseek( fd
, 0, SEEK_CUR
);
1036 if (!(de
= start_vfat_ioctl( fd
))) return -1; /* not supported */
1038 while (de
[0].d_reclen
)
1040 /* make sure names are null-terminated to work around an x86-64 kernel bug */
1041 len
= min(de
[0].d_reclen
, sizeof(de
[0].d_name
) - 1 );
1042 de
[0].d_name
[len
] = 0;
1043 len
= min(de
[1].d_reclen
, sizeof(de
[1].d_name
) - 1 );
1044 de
[1].d_name
[len
] = 0;
1046 if (de
[1].d_name
[0])
1047 info
= append_entry( buffer
, &io
->Information
, length
,
1048 de
[1].d_name
, de
[0].d_name
, mask
);
1050 info
= append_entry( buffer
, &io
->Information
, length
,
1051 de
[0].d_name
, NULL
, mask
);
1055 if ((char *)info
->FileName
+ info
->FileNameLength
> (char *)buffer
+ length
)
1057 io
->u
.Status
= STATUS_BUFFER_OVERFLOW
;
1058 lseek( fd
, old_pos
, SEEK_SET
); /* restore pos to previous entry */
1062 old_pos
= lseek( fd
, 0, SEEK_CUR
);
1063 if (ioctl( fd
, VFAT_IOCTL_READDIR_BOTH
, (long)de
) == -1) break;
1066 else /* we'll only return full entries, no need to worry about overflow */
1068 if (!(de
= start_vfat_ioctl( fd
))) return -1; /* not supported */
1070 while (de
[0].d_reclen
)
1072 /* make sure names are null-terminated to work around an x86-64 kernel bug */
1073 len
= min(de
[0].d_reclen
, sizeof(de
[0].d_name
) - 1 );
1074 de
[0].d_name
[len
] = 0;
1075 len
= min(de
[1].d_reclen
, sizeof(de
[1].d_name
) - 1 );
1076 de
[1].d_name
[len
] = 0;
1078 if (de
[1].d_name
[0])
1079 info
= append_entry( buffer
, &io
->Information
, length
,
1080 de
[1].d_name
, de
[0].d_name
, mask
);
1082 info
= append_entry( buffer
, &io
->Information
, length
,
1083 de
[0].d_name
, NULL
, mask
);
1087 if (single_entry
) break;
1088 /* check if we still have enough space for the largest possible entry */
1089 if (io
->Information
+ max_dir_info_size
> length
) break;
1091 if (ioctl( fd
, VFAT_IOCTL_READDIR_BOTH
, (long)de
) == -1) break;
1095 if (last_info
) last_info
->NextEntryOffset
= 0;
1096 else io
->u
.Status
= restart_scan
? STATUS_NO_SUCH_FILE
: STATUS_NO_MORE_FILES
;
1099 #endif /* VFAT_IOCTL_READDIR_BOTH */
1102 /***********************************************************************
1103 * read_directory_getdents
1105 * Read a directory using the Linux getdents64 system call; helper for NtQueryDirectoryFile.
1108 static int read_directory_getdents( int fd
, IO_STATUS_BLOCK
*io
, void *buffer
, ULONG length
,
1109 BOOLEAN single_entry
, const UNICODE_STRING
*mask
,
1110 BOOLEAN restart_scan
)
1113 size_t size
= length
;
1114 int res
, fake_dot_dot
= 1;
1115 char *data
, local_buffer
[8192];
1116 KERNEL_DIRENT64
*de
;
1117 FILE_BOTH_DIR_INFORMATION
*info
, *last_info
= NULL
;
1119 if (size
<= sizeof(local_buffer
) || !(data
= RtlAllocateHeap( GetProcessHeap(), 0, size
)))
1121 size
= sizeof(local_buffer
);
1122 data
= local_buffer
;
1125 if (restart_scan
) lseek( fd
, 0, SEEK_SET
);
1126 else if (length
< max_dir_info_size
) /* we may have to return a partial entry here */
1128 old_pos
= lseek( fd
, 0, SEEK_CUR
);
1129 if (old_pos
== -1 && errno
== ENOENT
)
1131 io
->u
.Status
= STATUS_NO_MORE_FILES
;
1137 io
->u
.Status
= STATUS_SUCCESS
;
1139 res
= getdents64( fd
, data
, size
);
1142 if (errno
!= ENOSYS
)
1144 io
->u
.Status
= FILE_GetNtStatus();
1150 de
= (KERNEL_DIRENT64
*)data
;
1154 /* check if we got . and .. from getdents */
1157 if (!strcmp( de
->d_name
, "." ) && res
> de
->d_reclen
)
1159 KERNEL_DIRENT64
*next_de
= (KERNEL_DIRENT64
*)(data
+ de
->d_reclen
);
1160 if (!strcmp( next_de
->d_name
, ".." )) fake_dot_dot
= 0;
1163 /* make sure we have enough room for both entries */
1166 static const ULONG min_info_size
= (FIELD_OFFSET(FILE_BOTH_DIR_INFORMATION
, FileName
[1]) +
1167 FIELD_OFFSET(FILE_BOTH_DIR_INFORMATION
, FileName
[2]) + 3) & ~3;
1168 if (length
< min_info_size
|| single_entry
)
1170 FIXME( "not enough room %u/%u for fake . and .. entries\n", length
, single_entry
);
1177 if ((info
= append_entry( buffer
, &io
->Information
, length
, ".", NULL
, mask
)))
1179 if ((info
= append_entry( buffer
, &io
->Information
, length
, "..", NULL
, mask
)))
1182 /* check if we still have enough space for the largest possible entry */
1183 if (last_info
&& io
->Information
+ max_dir_info_size
> length
)
1185 lseek( fd
, 0, SEEK_SET
); /* reset pos to first entry */
1193 res
-= de
->d_reclen
;
1194 if (!(fake_dot_dot
&& (!strcmp( de
->d_name
, "." ) || !strcmp( de
->d_name
, ".." ))) &&
1195 (info
= append_entry( buffer
, &io
->Information
, length
, de
->d_name
, NULL
, mask
)))
1198 if ((char *)info
->FileName
+ info
->FileNameLength
> (char *)buffer
+ length
)
1200 io
->u
.Status
= STATUS_BUFFER_OVERFLOW
;
1201 lseek( fd
, old_pos
, SEEK_SET
); /* restore pos to previous entry */
1204 /* check if we still have enough space for the largest possible entry */
1205 if (single_entry
|| io
->Information
+ max_dir_info_size
> length
)
1207 if (res
> 0) lseek( fd
, de
->d_off
, SEEK_SET
); /* set pos to next entry */
1211 old_pos
= de
->d_off
;
1212 /* move on to the next entry */
1213 if (res
> 0) de
= (KERNEL_DIRENT64
*)((char *)de
+ de
->d_reclen
);
1216 res
= getdents64( fd
, data
, size
);
1217 de
= (KERNEL_DIRENT64
*)data
;
1221 if (last_info
) last_info
->NextEntryOffset
= 0;
1222 else io
->u
.Status
= restart_scan
? STATUS_NO_SUCH_FILE
: STATUS_NO_MORE_FILES
;
1225 if (data
!= local_buffer
) RtlFreeHeap( GetProcessHeap(), 0, data
);
1229 #elif defined HAVE_GETDIRENTRIES
1231 /***********************************************************************
1232 * wine_getdirentries
1234 * Wrapper for the BSD getdirentries system call to fix a bug in the
1235 * Mac OS X version. For some file systems (at least Apple Filing
1236 * Protocol a.k.a. AFP), getdirentries resets the file position to 0
1237 * when it's about to return 0 (no more entries). So, a subsequent
1238 * getdirentries call starts over at the beginning again, causing an
1241 static inline int wine_getdirentries(int fd
, char *buf
, int nbytes
, long *basep
)
1243 int res
= getdirentries(fd
, buf
, nbytes
, basep
);
1246 lseek(fd
, *basep
, SEEK_SET
);
1251 /***********************************************************************
1252 * read_directory_getdirentries
1254 * Read a directory using the BSD getdirentries system call; helper for NtQueryDirectoryFile.
1256 static int read_directory_getdirentries( int fd
, IO_STATUS_BLOCK
*io
, void *buffer
, ULONG length
,
1257 BOOLEAN single_entry
, const UNICODE_STRING
*mask
,
1258 BOOLEAN restart_scan
)
1261 ULONG_PTR restart_info_pos
= 0;
1262 size_t size
, initial_size
= length
;
1263 int res
, fake_dot_dot
= 1;
1264 char *data
, local_buffer
[8192];
1266 FILE_BOTH_DIR_INFORMATION
*info
, *last_info
= NULL
, *restart_last_info
= NULL
;
1268 size
= initial_size
;
1269 data
= local_buffer
;
1270 if (size
> sizeof(local_buffer
) && !(data
= RtlAllocateHeap( GetProcessHeap(), 0, size
)))
1272 io
->u
.Status
= STATUS_NO_MEMORY
;
1273 return io
->u
.Status
;
1276 if (restart_scan
) lseek( fd
, 0, SEEK_SET
);
1278 io
->u
.Status
= STATUS_SUCCESS
;
1280 /* FIXME: should make sure size is larger than filesystem block size */
1281 res
= wine_getdirentries( fd
, data
, size
, &restart_pos
);
1284 io
->u
.Status
= FILE_GetNtStatus();
1289 de
= (struct dirent
*)data
;
1293 /* check if we got . and .. from getdirentries */
1296 if (!strcmp( de
->d_name
, "." ) && res
> de
->d_reclen
)
1298 struct dirent
*next_de
= (struct dirent
*)(data
+ de
->d_reclen
);
1299 if (!strcmp( next_de
->d_name
, ".." )) fake_dot_dot
= 0;
1302 /* make sure we have enough room for both entries */
1305 static const ULONG min_info_size
= (FIELD_OFFSET(FILE_BOTH_DIR_INFORMATION
, FileName
[1]) +
1306 FIELD_OFFSET(FILE_BOTH_DIR_INFORMATION
, FileName
[2]) + 3) & ~3;
1307 if (length
< min_info_size
|| single_entry
)
1309 FIXME( "not enough room %u/%u for fake . and .. entries\n", length
, single_entry
);
1316 if ((info
= append_entry( buffer
, &io
->Information
, length
, ".", NULL
, mask
)))
1318 if ((info
= append_entry( buffer
, &io
->Information
, length
, "..", NULL
, mask
)))
1321 restart_last_info
= last_info
;
1322 restart_info_pos
= io
->Information
;
1324 /* check if we still have enough space for the largest possible entry */
1325 if (last_info
&& io
->Information
+ max_dir_info_size
> length
)
1327 lseek( fd
, 0, SEEK_SET
); /* reset pos to first entry */
1335 res
-= de
->d_reclen
;
1337 !(fake_dot_dot
&& (!strcmp( de
->d_name
, "." ) || !strcmp( de
->d_name
, ".." ))) &&
1338 ((info
= append_entry( buffer
, &io
->Information
, length
, de
->d_name
, NULL
, mask
))))
1341 if ((char *)info
->FileName
+ info
->FileNameLength
> (char *)buffer
+ length
)
1343 lseek( fd
, (unsigned long)restart_pos
, SEEK_SET
);
1344 if (restart_info_pos
) /* if we have a complete read already, return it */
1346 io
->Information
= restart_info_pos
;
1347 last_info
= restart_last_info
;
1350 /* otherwise restart from the start with a smaller size */
1351 size
= (char *)de
- data
;
1354 io
->u
.Status
= STATUS_BUFFER_OVERFLOW
;
1357 io
->Information
= 0;
1361 /* if we have to return but the buffer contains more data, restart with a smaller size */
1362 if (res
> 0 && (single_entry
|| io
->Information
+ max_dir_info_size
> length
))
1364 lseek( fd
, (unsigned long)restart_pos
, SEEK_SET
);
1365 size
= (char *)de
- data
;
1366 io
->Information
= restart_info_pos
;
1367 last_info
= restart_last_info
;
1371 /* move on to the next entry */
1374 de
= (struct dirent
*)((char *)de
+ de
->d_reclen
);
1377 if (size
< initial_size
) break; /* already restarted once, give up now */
1378 size
= min( size
, length
- io
->Information
);
1379 /* if size is too small don't bother to continue */
1380 if (size
< max_dir_info_size
&& last_info
) break;
1381 restart_last_info
= last_info
;
1382 restart_info_pos
= io
->Information
;
1384 res
= wine_getdirentries( fd
, data
, size
, &restart_pos
);
1385 de
= (struct dirent
*)data
;
1388 if (last_info
) last_info
->NextEntryOffset
= 0;
1389 else io
->u
.Status
= restart_scan
? STATUS_NO_SUCH_FILE
: STATUS_NO_MORE_FILES
;
1392 if (data
!= local_buffer
) RtlFreeHeap( GetProcessHeap(), 0, data
);
1395 #endif /* HAVE_GETDIRENTRIES */
1398 /***********************************************************************
1399 * read_directory_readdir
1401 * Read a directory using the POSIX readdir interface; helper for NtQueryDirectoryFile.
1403 static void read_directory_readdir( int fd
, IO_STATUS_BLOCK
*io
, void *buffer
, ULONG length
,
1404 BOOLEAN single_entry
, const UNICODE_STRING
*mask
,
1405 BOOLEAN restart_scan
)
1408 off_t i
, old_pos
= 0;
1410 FILE_BOTH_DIR_INFORMATION
*info
, *last_info
= NULL
;
1412 if (!(dir
= opendir( "." )))
1414 io
->u
.Status
= FILE_GetNtStatus();
1420 old_pos
= lseek( fd
, 0, SEEK_CUR
);
1421 /* skip the right number of entries */
1422 for (i
= 0; i
< old_pos
- 2; i
++)
1424 if (!readdir( dir
))
1427 io
->u
.Status
= STATUS_NO_MORE_FILES
;
1432 io
->u
.Status
= STATUS_SUCCESS
;
1437 info
= append_entry( buffer
, &io
->Information
, length
, ".", NULL
, mask
);
1438 else if (old_pos
== 1)
1439 info
= append_entry( buffer
, &io
->Information
, length
, "..", NULL
, mask
);
1440 else if ((de
= readdir( dir
)))
1442 if (strcmp( de
->d_name
, "." ) && strcmp( de
->d_name
, ".." ))
1443 info
= append_entry( buffer
, &io
->Information
, length
, de
->d_name
, NULL
, mask
);
1453 if ((char *)info
->FileName
+ info
->FileNameLength
> (char *)buffer
+ length
)
1455 io
->u
.Status
= STATUS_BUFFER_OVERFLOW
;
1456 old_pos
--; /* restore pos to previous entry */
1459 if (single_entry
) break;
1460 /* check if we still have enough space for the largest possible entry */
1461 if (io
->Information
+ max_dir_info_size
> length
) break;
1465 lseek( fd
, old_pos
, SEEK_SET
); /* store dir offset as filepos for fd */
1468 if (last_info
) last_info
->NextEntryOffset
= 0;
1469 else io
->u
.Status
= restart_scan
? STATUS_NO_SUCH_FILE
: STATUS_NO_MORE_FILES
;
1472 /***********************************************************************
1473 * read_directory_stat
1475 * Read a single file from a directory by determining whether the file
1476 * identified by mask exists using stat.
1478 static int read_directory_stat( int fd
, IO_STATUS_BLOCK
*io
, void *buffer
, ULONG length
,
1479 BOOLEAN single_entry
, const UNICODE_STRING
*mask
,
1480 BOOLEAN restart_scan
)
1482 int unix_len
, ret
, used_default
;
1486 TRACE("trying optimisation for file %s\n", debugstr_us( mask
));
1488 unix_len
= ntdll_wcstoumbs( 0, mask
->Buffer
, mask
->Length
/ sizeof(WCHAR
), NULL
, 0, NULL
, NULL
);
1489 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, unix_len
+ 1)))
1491 io
->u
.Status
= STATUS_NO_MEMORY
;
1494 ret
= ntdll_wcstoumbs( 0, mask
->Buffer
, mask
->Length
/ sizeof(WCHAR
), unix_name
, unix_len
,
1495 NULL
, &used_default
);
1496 if (ret
> 0 && !used_default
)
1501 lseek( fd
, 0, SEEK_SET
);
1503 else if (lseek( fd
, 0, SEEK_CUR
) != 0)
1505 io
->u
.Status
= STATUS_NO_MORE_FILES
;
1510 ret
= stat( unix_name
, &st
);
1513 FILE_BOTH_DIR_INFORMATION
*info
= append_entry( buffer
, &io
->Information
, length
, unix_name
, NULL
, NULL
);
1516 info
->NextEntryOffset
= 0;
1517 if ((char *)info
->FileName
+ info
->FileNameLength
> (char *)buffer
+ length
)
1518 io
->u
.Status
= STATUS_BUFFER_OVERFLOW
;
1520 lseek( fd
, 1, SEEK_CUR
);
1522 else io
->u
.Status
= STATUS_NO_MORE_FILES
;
1528 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
1530 TRACE("returning %d\n", ret
);
1536 static inline WCHAR
*mempbrkW( const WCHAR
*ptr
, const WCHAR
*accept
, size_t n
)
1539 for (end
= ptr
+ n
; ptr
< end
; ptr
++) if (strchrW( accept
, *ptr
)) return (WCHAR
*)ptr
;
1543 /******************************************************************************
1544 * NtQueryDirectoryFile [NTDLL.@]
1545 * ZwQueryDirectoryFile [NTDLL.@]
1547 NTSTATUS WINAPI
NtQueryDirectoryFile( HANDLE handle
, HANDLE event
,
1548 PIO_APC_ROUTINE apc_routine
, PVOID apc_context
,
1549 PIO_STATUS_BLOCK io
,
1550 PVOID buffer
, ULONG length
,
1551 FILE_INFORMATION_CLASS info_class
,
1552 BOOLEAN single_entry
,
1553 PUNICODE_STRING mask
,
1554 BOOLEAN restart_scan
)
1556 int cwd
, fd
, needs_close
;
1557 static const WCHAR wszWildcards
[] = { '*','?',0 };
1559 TRACE("(%p %p %p %p %p %p 0x%08x 0x%08x 0x%08x %s 0x%08x\n",
1560 handle
, event
, apc_routine
, apc_context
, io
, buffer
,
1561 length
, info_class
, single_entry
, debugstr_us(mask
),
1564 if (length
< sizeof(FILE_BOTH_DIR_INFORMATION
)) return STATUS_INFO_LENGTH_MISMATCH
;
1566 if (event
|| apc_routine
)
1568 FIXME( "Unsupported yet option\n" );
1569 return io
->u
.Status
= STATUS_NOT_IMPLEMENTED
;
1571 if (info_class
!= FileBothDirectoryInformation
)
1573 FIXME( "Unsupported file info class %d\n", info_class
);
1574 return io
->u
.Status
= STATUS_NOT_IMPLEMENTED
;
1577 if ((io
->u
.Status
= server_get_unix_fd( handle
, FILE_LIST_DIRECTORY
, &fd
, &needs_close
, NULL
, NULL
)) != STATUS_SUCCESS
)
1578 return io
->u
.Status
;
1580 io
->Information
= 0;
1582 RtlEnterCriticalSection( &dir_section
);
1584 if (show_dot_files
== -1) init_options();
1586 cwd
= open( ".", O_RDONLY
);
1587 if (fchdir( fd
) != -1)
1589 #ifdef VFAT_IOCTL_READDIR_BOTH
1590 if ((read_directory_vfat( fd
, io
, buffer
, length
, single_entry
, mask
, restart_scan
)) != -1)
1593 if (mask
&& !mempbrkW( mask
->Buffer
, wszWildcards
, mask
->Length
/ sizeof(WCHAR
) ) &&
1594 read_directory_stat( fd
, io
, buffer
, length
, single_entry
, mask
, restart_scan
) != -1)
1597 if ((read_directory_getdents( fd
, io
, buffer
, length
, single_entry
, mask
, restart_scan
)) != -1)
1599 #elif defined HAVE_GETDIRENTRIES
1600 if ((read_directory_getdirentries( fd
, io
, buffer
, length
, single_entry
, mask
, restart_scan
)) != -1)
1603 read_directory_readdir( fd
, io
, buffer
, length
, single_entry
, mask
, restart_scan
);
1606 if (cwd
== -1 || fchdir( cwd
) == -1) chdir( "/" );
1608 else io
->u
.Status
= FILE_GetNtStatus();
1610 RtlLeaveCriticalSection( &dir_section
);
1612 if (needs_close
) close( fd
);
1613 if (cwd
!= -1) close( cwd
);
1614 TRACE( "=> %x (%ld)\n", io
->u
.Status
, io
->Information
);
1615 return io
->u
.Status
;
1619 /***********************************************************************
1622 * Find a file in a directory the hard way, by doing a case-insensitive search.
1623 * The file found is appended to unix_name at pos.
1624 * There must be at least MAX_DIR_ENTRY_LEN+2 chars available at pos.
1626 static NTSTATUS
find_file_in_dir( char *unix_name
, int pos
, const WCHAR
*name
, int length
,
1629 WCHAR buffer
[MAX_DIR_ENTRY_LEN
];
1635 int ret
, used_default
, is_name_8_dot_3
;
1637 /* try a shortcut for this directory */
1639 unix_name
[pos
++] = '/';
1640 ret
= ntdll_wcstoumbs( 0, name
, length
, unix_name
+ pos
, MAX_DIR_ENTRY_LEN
,
1641 NULL
, &used_default
);
1642 /* if we used the default char, the Unix name won't round trip properly back to Unicode */
1643 /* so it cannot match the file we are looking for */
1644 if (ret
>= 0 && !used_default
)
1646 unix_name
[pos
+ ret
] = 0;
1647 if (!stat( unix_name
, &st
)) return STATUS_SUCCESS
;
1649 if (check_case
) goto not_found
; /* we want an exact match */
1651 if (pos
> 1) unix_name
[pos
- 1] = 0;
1652 else unix_name
[1] = 0; /* keep the initial slash */
1654 /* check if it fits in 8.3 so that we don't look for short names if we won't need them */
1656 str
.Buffer
= (WCHAR
*)name
;
1657 str
.Length
= length
* sizeof(WCHAR
);
1658 str
.MaximumLength
= str
.Length
;
1659 is_name_8_dot_3
= RtlIsNameLegalDOS8Dot3( &str
, NULL
, &spaces
) && !spaces
;
1661 /* now look for it through the directory */
1663 #ifdef VFAT_IOCTL_READDIR_BOTH
1664 if (is_name_8_dot_3
)
1666 int fd
= open( unix_name
, O_RDONLY
| O_DIRECTORY
);
1671 RtlEnterCriticalSection( &dir_section
);
1672 if ((de
= start_vfat_ioctl( fd
)))
1674 unix_name
[pos
- 1] = '/';
1675 while (de
[0].d_reclen
)
1677 /* make sure names are null-terminated to work around an x86-64 kernel bug */
1678 size_t len
= min(de
[0].d_reclen
, sizeof(de
[0].d_name
) - 1 );
1679 de
[0].d_name
[len
] = 0;
1680 len
= min(de
[1].d_reclen
, sizeof(de
[1].d_name
) - 1 );
1681 de
[1].d_name
[len
] = 0;
1683 if (de
[1].d_name
[0])
1685 ret
= ntdll_umbstowcs( 0, de
[1].d_name
, strlen(de
[1].d_name
),
1686 buffer
, MAX_DIR_ENTRY_LEN
);
1687 if (ret
== length
&& !memicmpW( buffer
, name
, length
))
1689 strcpy( unix_name
+ pos
, de
[1].d_name
);
1690 RtlLeaveCriticalSection( &dir_section
);
1692 return STATUS_SUCCESS
;
1695 ret
= ntdll_umbstowcs( 0, de
[0].d_name
, strlen(de
[0].d_name
),
1696 buffer
, MAX_DIR_ENTRY_LEN
);
1697 if (ret
== length
&& !memicmpW( buffer
, name
, length
))
1699 strcpy( unix_name
+ pos
,
1700 de
[1].d_name
[0] ? de
[1].d_name
: de
[0].d_name
);
1701 RtlLeaveCriticalSection( &dir_section
);
1703 return STATUS_SUCCESS
;
1705 if (ioctl( fd
, VFAT_IOCTL_READDIR_BOTH
, (long)de
) == -1)
1707 RtlLeaveCriticalSection( &dir_section
);
1713 RtlLeaveCriticalSection( &dir_section
);
1716 /* fall through to normal handling */
1718 #endif /* VFAT_IOCTL_READDIR_BOTH */
1720 if (!(dir
= opendir( unix_name
)))
1722 if (errno
== ENOENT
) return STATUS_OBJECT_PATH_NOT_FOUND
;
1723 else return FILE_GetNtStatus();
1725 unix_name
[pos
- 1] = '/';
1726 str
.Buffer
= buffer
;
1727 str
.MaximumLength
= sizeof(buffer
);
1728 while ((de
= readdir( dir
)))
1730 ret
= ntdll_umbstowcs( 0, de
->d_name
, strlen(de
->d_name
), buffer
, MAX_DIR_ENTRY_LEN
);
1731 if (ret
== length
&& !memicmpW( buffer
, name
, length
))
1733 strcpy( unix_name
+ pos
, de
->d_name
);
1735 return STATUS_SUCCESS
;
1738 if (!is_name_8_dot_3
) continue;
1740 str
.Length
= ret
* sizeof(WCHAR
);
1741 if (!RtlIsNameLegalDOS8Dot3( &str
, NULL
, &spaces
) || spaces
)
1743 WCHAR short_nameW
[12];
1744 ret
= hash_short_file_name( &str
, short_nameW
);
1745 if (ret
== length
&& !memicmpW( short_nameW
, name
, length
))
1747 strcpy( unix_name
+ pos
, de
->d_name
);
1749 return STATUS_SUCCESS
;
1754 goto not_found
; /* avoid warning */
1757 unix_name
[pos
- 1] = 0;
1758 return STATUS_OBJECT_PATH_NOT_FOUND
;
1762 /******************************************************************************
1765 * Get the Unix path of a DOS device.
1767 static NTSTATUS
get_dos_device( const WCHAR
*name
, UINT name_len
, ANSI_STRING
*unix_name_ret
)
1769 const char *config_dir
= wine_get_config_dir();
1771 char *unix_name
, *new_name
, *dev
;
1775 /* make sure the device name is ASCII */
1776 for (i
= 0; i
< name_len
; i
++)
1777 if (name
[i
] <= 32 || name
[i
] >= 127) return STATUS_BAD_DEVICE_TYPE
;
1779 unix_len
= strlen(config_dir
) + sizeof("/dosdevices/") + name_len
+ 1;
1781 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, unix_len
)))
1782 return STATUS_NO_MEMORY
;
1784 strcpy( unix_name
, config_dir
);
1785 strcat( unix_name
, "/dosdevices/" );
1786 dev
= unix_name
+ strlen(unix_name
);
1788 for (i
= 0; i
< name_len
; i
++) dev
[i
] = (char)tolowerW(name
[i
]);
1791 /* special case for drive devices */
1792 if (name_len
== 2 && dev
[1] == ':')
1800 if (!stat( unix_name
, &st
))
1802 TRACE( "%s -> %s\n", debugstr_wn(name
,name_len
), debugstr_a(unix_name
) );
1803 unix_name_ret
->Buffer
= unix_name
;
1804 unix_name_ret
->Length
= strlen(unix_name
);
1805 unix_name_ret
->MaximumLength
= unix_len
;
1806 return STATUS_SUCCESS
;
1810 /* now try some defaults for it */
1811 if (!strcmp( dev
, "aux" ))
1813 strcpy( dev
, "com1" );
1816 if (!strcmp( dev
, "prn" ))
1818 strcpy( dev
, "lpt1" );
1821 if (!strcmp( dev
, "nul" ))
1823 strcpy( unix_name
, "/dev/null" );
1824 dev
= NULL
; /* last try */
1829 if (dev
[1] == ':' && dev
[2] == ':') /* drive device */
1831 dev
[2] = 0; /* remove last ':' to get the drive mount point symlink */
1832 new_name
= get_default_drive_device( unix_name
);
1834 else if (!strncmp( dev
, "com", 3 )) new_name
= get_default_com_device( dev
[3] - '0' );
1835 else if (!strncmp( dev
, "lpt", 3 )) new_name
= get_default_lpt_device( dev
[3] - '0' );
1837 if (!new_name
) break;
1839 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
1840 unix_name
= new_name
;
1841 unix_len
= strlen(unix_name
) + 1;
1842 dev
= NULL
; /* last try */
1844 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
1845 return STATUS_BAD_DEVICE_TYPE
;
1849 /* return the length of the DOS namespace prefix if any */
1850 static inline int get_dos_prefix_len( const UNICODE_STRING
*name
)
1852 static const WCHAR nt_prefixW
[] = {'\\','?','?','\\'};
1853 static const WCHAR dosdev_prefixW
[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\'};
1855 if (name
->Length
> sizeof(nt_prefixW
) &&
1856 !memcmp( name
->Buffer
, nt_prefixW
, sizeof(nt_prefixW
) ))
1857 return sizeof(nt_prefixW
) / sizeof(WCHAR
);
1859 if (name
->Length
> sizeof(dosdev_prefixW
) &&
1860 !memicmpW( name
->Buffer
, dosdev_prefixW
, sizeof(dosdev_prefixW
)/sizeof(WCHAR
) ))
1861 return sizeof(dosdev_prefixW
) / sizeof(WCHAR
);
1867 /******************************************************************************
1868 * wine_nt_to_unix_file_name (NTDLL.@) Not a Windows API
1870 * Convert a file name from NT namespace to Unix namespace.
1872 * If disposition is not FILE_OPEN or FILE_OVERWRITE, the last path
1873 * element doesn't have to exist; in that case STATUS_NO_SUCH_FILE is
1874 * returned, but the unix name is still filled in properly.
1876 NTSTATUS
wine_nt_to_unix_file_name( const UNICODE_STRING
*nameW
, ANSI_STRING
*unix_name_ret
,
1877 UINT disposition
, BOOLEAN check_case
)
1879 static const WCHAR unixW
[] = {'u','n','i','x'};
1880 static const WCHAR invalid_charsW
[] = { INVALID_NT_CHARS
, 0 };
1882 NTSTATUS status
= STATUS_SUCCESS
;
1883 const char *config_dir
= wine_get_config_dir();
1884 const WCHAR
*name
, *p
;
1887 int pos
, ret
, name_len
, unix_len
, prefix_len
, used_default
;
1888 WCHAR prefix
[MAX_DIR_ENTRY_LEN
];
1889 BOOLEAN is_unix
= FALSE
;
1891 name
= nameW
->Buffer
;
1892 name_len
= nameW
->Length
/ sizeof(WCHAR
);
1894 if (!name_len
|| !IS_SEPARATOR(name
[0])) return STATUS_OBJECT_PATH_SYNTAX_BAD
;
1896 if (!(pos
= get_dos_prefix_len( nameW
)))
1897 return STATUS_BAD_DEVICE_TYPE
; /* no DOS prefix, assume NT native name */
1902 /* check for sub-directory */
1903 for (pos
= 0; pos
< name_len
; pos
++)
1905 if (IS_SEPARATOR(name
[pos
])) break;
1906 if (name
[pos
] < 32 || strchrW( invalid_charsW
, name
[pos
] ))
1907 return STATUS_OBJECT_NAME_INVALID
;
1909 if (pos
> MAX_DIR_ENTRY_LEN
)
1910 return STATUS_OBJECT_NAME_INVALID
;
1912 if (pos
== name_len
) /* no subdir, plain DOS device */
1913 return get_dos_device( name
, name_len
, unix_name_ret
);
1915 for (prefix_len
= 0; prefix_len
< pos
; prefix_len
++)
1916 prefix
[prefix_len
] = tolowerW(name
[prefix_len
]);
1919 name_len
-= prefix_len
;
1921 /* check for invalid characters (all chars except 0 are valid for unix) */
1922 is_unix
= (prefix_len
== 4 && !memcmp( prefix
, unixW
, sizeof(unixW
) ));
1925 for (p
= name
; p
< name
+ name_len
; p
++)
1926 if (!*p
) return STATUS_OBJECT_NAME_INVALID
;
1931 for (p
= name
; p
< name
+ name_len
; p
++)
1932 if (*p
< 32 || strchrW( invalid_charsW
, *p
)) return STATUS_OBJECT_NAME_INVALID
;
1935 unix_len
= ntdll_wcstoumbs( 0, prefix
, prefix_len
, NULL
, 0, NULL
, NULL
);
1936 unix_len
+= ntdll_wcstoumbs( 0, name
, name_len
, NULL
, 0, NULL
, NULL
);
1937 unix_len
+= MAX_DIR_ENTRY_LEN
+ 3;
1938 unix_len
+= strlen(config_dir
) + sizeof("/dosdevices/");
1939 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, unix_len
)))
1940 return STATUS_NO_MEMORY
;
1941 strcpy( unix_name
, config_dir
);
1942 strcat( unix_name
, "/dosdevices/" );
1943 pos
= strlen(unix_name
);
1945 ret
= ntdll_wcstoumbs( 0, prefix
, prefix_len
, unix_name
+ pos
, unix_len
- pos
- 1,
1946 NULL
, &used_default
);
1947 if (!ret
|| used_default
)
1949 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
1950 return STATUS_OBJECT_NAME_INVALID
;
1954 /* check if prefix exists (except for DOS drives to avoid extra stat calls) */
1956 if (prefix_len
!= 2 || prefix
[1] != ':')
1959 if (lstat( unix_name
, &st
) == -1 && errno
== ENOENT
)
1963 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
1964 return STATUS_BAD_DEVICE_TYPE
;
1966 pos
= 0; /* fall back to unix root */
1970 /* try a shortcut first */
1972 ret
= ntdll_wcstoumbs( 0, name
, name_len
, unix_name
+ pos
, unix_len
- pos
- 1,
1973 NULL
, &used_default
);
1975 while (name_len
&& IS_SEPARATOR(*name
))
1981 if (ret
> 0 && !used_default
) /* if we used the default char the name didn't convert properly */
1984 unix_name
[pos
+ ret
] = 0;
1985 for (p
= unix_name
+ pos
; *p
; p
++) if (*p
== '\\') *p
= '/';
1986 if (!stat( unix_name
, &st
))
1988 /* creation fails with STATUS_ACCESS_DENIED for the root of the drive */
1989 if (disposition
== FILE_CREATE
)
1991 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
1992 return name_len
? STATUS_OBJECT_NAME_COLLISION
: STATUS_ACCESS_DENIED
;
1998 if (!name_len
) /* empty name -> drive root doesn't exist */
2000 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2001 return STATUS_OBJECT_PATH_NOT_FOUND
;
2003 if (check_case
&& (disposition
== FILE_OPEN
|| disposition
== FILE_OVERWRITE
))
2005 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2006 return STATUS_OBJECT_NAME_NOT_FOUND
;
2009 /* now do it component by component */
2013 const WCHAR
*end
, *next
;
2016 while (end
< name
+ name_len
&& !IS_SEPARATOR(*end
)) end
++;
2018 while (next
< name
+ name_len
&& IS_SEPARATOR(*next
)) next
++;
2019 name_len
-= next
- name
;
2021 /* grow the buffer if needed */
2023 if (unix_len
- pos
< MAX_DIR_ENTRY_LEN
+ 2)
2026 unix_len
+= 2 * MAX_DIR_ENTRY_LEN
;
2027 if (!(new_name
= RtlReAllocateHeap( GetProcessHeap(), 0, unix_name
, unix_len
)))
2029 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2030 return STATUS_NO_MEMORY
;
2032 unix_name
= new_name
;
2035 status
= find_file_in_dir( unix_name
, pos
, name
, end
- name
, check_case
);
2037 /* if this is the last element, not finding it is not necessarily fatal */
2040 if (status
== STATUS_OBJECT_PATH_NOT_FOUND
)
2042 status
= STATUS_OBJECT_NAME_NOT_FOUND
;
2043 if (disposition
!= FILE_OPEN
&& disposition
!= FILE_OVERWRITE
)
2045 ret
= ntdll_wcstoumbs( 0, name
, end
- name
, unix_name
+ pos
+ 1,
2046 MAX_DIR_ENTRY_LEN
, NULL
, &used_default
);
2047 if (ret
> 0 && !used_default
)
2049 unix_name
[pos
] = '/';
2050 unix_name
[pos
+ 1 + ret
] = 0;
2051 status
= STATUS_NO_SUCH_FILE
;
2056 else if (status
== STATUS_SUCCESS
&& disposition
== FILE_CREATE
)
2058 status
= STATUS_OBJECT_NAME_COLLISION
;
2062 if (status
!= STATUS_SUCCESS
)
2064 /* couldn't find it at all, fail */
2065 WARN( "%s not found in %s\n", debugstr_w(name
), unix_name
);
2066 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2070 pos
+= strlen( unix_name
+ pos
);
2074 WARN( "%s -> %s required a case-insensitive search\n",
2075 debugstr_us(nameW
), debugstr_a(unix_name
) );
2078 TRACE( "%s -> %s\n", debugstr_us(nameW
), debugstr_a(unix_name
) );
2079 unix_name_ret
->Buffer
= unix_name
;
2080 unix_name_ret
->Length
= strlen(unix_name
);
2081 unix_name_ret
->MaximumLength
= unix_len
;
2086 /******************************************************************
2087 * RtlDoesFileExists_U (NTDLL.@)
2089 BOOLEAN WINAPI
RtlDoesFileExists_U(LPCWSTR file_name
)
2091 UNICODE_STRING nt_name
;
2092 FILE_BASIC_INFORMATION basic_info
;
2093 OBJECT_ATTRIBUTES attr
;
2096 if (!RtlDosPathNameToNtPathName_U( file_name
, &nt_name
, NULL
, NULL
)) return FALSE
;
2098 attr
.Length
= sizeof(attr
);
2099 attr
.RootDirectory
= 0;
2100 attr
.ObjectName
= &nt_name
;
2101 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2102 attr
.SecurityDescriptor
= NULL
;
2103 attr
.SecurityQualityOfService
= NULL
;
2105 ret
= NtQueryAttributesFile(&attr
, &basic_info
) == STATUS_SUCCESS
;
2107 RtlFreeUnicodeString( &nt_name
);
2112 /***********************************************************************
2113 * DIR_unmount_device
2115 * Unmount the specified device.
2117 NTSTATUS
DIR_unmount_device( HANDLE handle
)
2120 int unix_fd
, needs_close
;
2122 if (!(status
= server_get_unix_fd( handle
, 0, &unix_fd
, &needs_close
, NULL
, NULL
)))
2125 char *mount_point
= NULL
;
2127 if (fstat( unix_fd
, &st
) == -1 || !is_valid_mounted_device( &st
))
2128 status
= STATUS_INVALID_PARAMETER
;
2131 if ((mount_point
= get_device_mount_point( st
.st_rdev
)))
2134 static const char umount
[] = "diskutil unmount >/dev/null 2>&1 ";
2136 static const char umount
[] = "umount >/dev/null 2>&1 ";
2138 char *cmd
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(mount_point
)+sizeof(umount
));
2141 strcpy( cmd
, umount
);
2142 strcat( cmd
, mount_point
);
2144 RtlFreeHeap( GetProcessHeap(), 0, cmd
);
2146 /* umount will fail to release the loop device since we still have
2147 a handle to it, so we release it here */
2148 if (major(st
.st_rdev
) == LOOP_MAJOR
) ioctl( unix_fd
, 0x4c01 /*LOOP_CLR_FD*/, 0 );
2151 RtlFreeHeap( GetProcessHeap(), 0, mount_point
);
2154 if (needs_close
) close( unix_fd
);
2160 /******************************************************************************
2163 * Retrieve the Unix name of the current directory; helper for wine_unix_to_nt_file_name.
2164 * Returned value must be freed by caller.
2166 NTSTATUS
DIR_get_unix_cwd( char **cwd
)
2168 int old_cwd
, unix_fd
, needs_close
;
2173 RtlAcquirePebLock();
2175 if (NtCurrentTeb()->Tib
.SubSystemTib
) /* FIXME: hack */
2176 curdir
= &((WIN16_SUBSYSTEM_TIB
*)NtCurrentTeb()->Tib
.SubSystemTib
)->curdir
;
2178 curdir
= &NtCurrentTeb()->Peb
->ProcessParameters
->CurrentDirectory
;
2180 if (!(handle
= curdir
->Handle
))
2182 UNICODE_STRING dirW
;
2183 OBJECT_ATTRIBUTES attr
;
2186 if (!RtlDosPathNameToNtPathName_U( curdir
->DosPath
.Buffer
, &dirW
, NULL
, NULL
))
2188 status
= STATUS_OBJECT_NAME_INVALID
;
2191 attr
.Length
= sizeof(attr
);
2192 attr
.RootDirectory
= 0;
2193 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2194 attr
.ObjectName
= &dirW
;
2195 attr
.SecurityDescriptor
= NULL
;
2196 attr
.SecurityQualityOfService
= NULL
;
2198 status
= NtOpenFile( &handle
, 0, &attr
, &io
, 0,
2199 FILE_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
);
2200 RtlFreeUnicodeString( &dirW
);
2201 if (status
!= STATUS_SUCCESS
) goto done
;
2204 if ((status
= server_get_unix_fd( handle
, 0, &unix_fd
, &needs_close
, NULL
, NULL
)) == STATUS_SUCCESS
)
2206 RtlEnterCriticalSection( &dir_section
);
2208 if ((old_cwd
= open(".", O_RDONLY
)) != -1 && fchdir( unix_fd
) != -1)
2210 unsigned int size
= 512;
2214 if (!(*cwd
= RtlAllocateHeap( GetProcessHeap(), 0, size
)))
2216 status
= STATUS_NO_MEMORY
;
2219 if (getcwd( *cwd
, size
)) break;
2220 RtlFreeHeap( GetProcessHeap(), 0, *cwd
);
2221 if (errno
!= ERANGE
)
2223 status
= STATUS_OBJECT_PATH_INVALID
;
2228 if (fchdir( old_cwd
) == -1) chdir( "/" );
2230 else status
= FILE_GetNtStatus();
2232 RtlLeaveCriticalSection( &dir_section
);
2233 if (needs_close
) close( unix_fd
);
2235 if (!curdir
->Handle
) NtClose( handle
);
2238 RtlReleasePebLock();
2242 struct read_changes_info
2247 PIO_APC_ROUTINE apc
;
2251 /* callback for ioctl user APC */
2252 static void WINAPI
read_changes_user_apc( void *arg
, IO_STATUS_BLOCK
*io
, ULONG reserved
)
2254 struct read_changes_info
*info
= arg
;
2255 if (info
->apc
) info
->apc( info
->apc_arg
, io
, reserved
);
2256 RtlFreeHeap( GetProcessHeap(), 0, info
);
2259 static NTSTATUS
read_changes_apc( void *user
, PIO_STATUS_BLOCK iosb
, NTSTATUS status
, ULONG_PTR
*total
)
2261 struct read_changes_info
*info
= user
;
2262 char path
[PATH_MAX
];
2263 NTSTATUS ret
= STATUS_SUCCESS
;
2266 SERVER_START_REQ( read_change
)
2268 req
->handle
= info
->FileHandle
;
2269 wine_server_set_reply( req
, path
, PATH_MAX
);
2270 ret
= wine_server_call( req
);
2271 action
= reply
->action
;
2272 len
= wine_server_reply_size( reply
);
2276 if (ret
== STATUS_SUCCESS
&& info
->Buffer
&&
2277 (info
->BufferSize
> (sizeof (FILE_NOTIFY_INFORMATION
) + len
*sizeof(WCHAR
))))
2279 PFILE_NOTIFY_INFORMATION pfni
;
2281 pfni
= (PFILE_NOTIFY_INFORMATION
) info
->Buffer
;
2283 /* convert to an NT style path */
2284 for (i
=0; i
<len
; i
++)
2288 len
= ntdll_umbstowcs( 0, path
, len
, pfni
->FileName
,
2289 info
->BufferSize
- sizeof (*pfni
) );
2291 pfni
->NextEntryOffset
= 0;
2292 pfni
->Action
= action
;
2293 pfni
->FileNameLength
= len
* sizeof (WCHAR
);
2294 pfni
->FileName
[len
] = 0;
2295 len
= sizeof (*pfni
) - sizeof (DWORD
) + pfni
->FileNameLength
;
2299 ret
= STATUS_NOTIFY_ENUM_DIR
;
2303 iosb
->u
.Status
= ret
;
2304 iosb
->Information
= *total
= len
;
2308 #define FILE_NOTIFY_ALL ( \
2309 FILE_NOTIFY_CHANGE_FILE_NAME | \
2310 FILE_NOTIFY_CHANGE_DIR_NAME | \
2311 FILE_NOTIFY_CHANGE_ATTRIBUTES | \
2312 FILE_NOTIFY_CHANGE_SIZE | \
2313 FILE_NOTIFY_CHANGE_LAST_WRITE | \
2314 FILE_NOTIFY_CHANGE_LAST_ACCESS | \
2315 FILE_NOTIFY_CHANGE_CREATION | \
2316 FILE_NOTIFY_CHANGE_SECURITY )
2318 /******************************************************************************
2319 * NtNotifyChangeDirectoryFile [NTDLL.@]
2322 NtNotifyChangeDirectoryFile( HANDLE FileHandle
, HANDLE Event
,
2323 PIO_APC_ROUTINE ApcRoutine
, PVOID ApcContext
,
2324 PIO_STATUS_BLOCK IoStatusBlock
, PVOID Buffer
,
2325 ULONG BufferSize
, ULONG CompletionFilter
, BOOLEAN WatchTree
)
2327 struct read_changes_info
*info
;
2329 ULONG_PTR cvalue
= ApcRoutine
? 0 : (ULONG_PTR
)ApcContext
;
2331 TRACE("%p %p %p %p %p %p %u %u %d\n",
2332 FileHandle
, Event
, ApcRoutine
, ApcContext
, IoStatusBlock
,
2333 Buffer
, BufferSize
, CompletionFilter
, WatchTree
);
2336 return STATUS_ACCESS_VIOLATION
;
2338 if (CompletionFilter
== 0 || (CompletionFilter
& ~FILE_NOTIFY_ALL
))
2339 return STATUS_INVALID_PARAMETER
;
2341 info
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof *info
);
2343 return STATUS_NO_MEMORY
;
2345 info
->FileHandle
= FileHandle
;
2346 info
->Buffer
= Buffer
;
2347 info
->BufferSize
= BufferSize
;
2348 info
->apc
= ApcRoutine
;
2349 info
->apc_arg
= ApcContext
;
2351 SERVER_START_REQ( read_directory_changes
)
2353 req
->handle
= FileHandle
;
2354 req
->filter
= CompletionFilter
;
2355 req
->want_data
= (Buffer
!= NULL
);
2356 req
->subtree
= WatchTree
;
2357 req
->async
.callback
= read_changes_apc
;
2358 req
->async
.iosb
= IoStatusBlock
;
2359 req
->async
.arg
= info
;
2360 req
->async
.apc
= read_changes_user_apc
;
2361 req
->async
.event
= Event
;
2362 req
->async
.cvalue
= cvalue
;
2363 status
= wine_server_call( req
);
2367 if (status
!= STATUS_PENDING
)
2368 RtlFreeHeap( GetProcessHeap(), 0, info
);