2 * NTDLL directory functions
4 * Copyright 1993 Erik Bos
5 * Copyright 2003 Eric Pouech
6 * Copyright 1996, 2004 Alexandre Julliard
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/port.h"
27 #include <sys/types.h>
41 #ifdef HAVE_SYS_STAT_H
42 # include <sys/stat.h>
44 #ifdef HAVE_SYS_SYSCALL_H
45 # include <sys/syscall.h>
47 #ifdef HAVE_SYS_ATTR_H
50 #ifdef HAVE_SYS_IOCTL_H
51 #include <sys/ioctl.h>
53 #ifdef HAVE_LINUX_IOCTL_H
54 #include <linux/ioctl.h>
56 #ifdef HAVE_LINUX_MAJOR_H
57 # include <linux/major.h>
59 #ifdef HAVE_SYS_PARAM_H
60 #include <sys/param.h>
62 #ifdef HAVE_SYS_MOUNT_H
63 #include <sys/mount.h>
65 #ifdef HAVE_SYS_STATFS_H
66 #include <sys/statfs.h>
73 #define NONAMELESSUNION
74 #define NONAMELESSSTRUCT
76 #define WIN32_NO_STATUS
81 #include "ntdll_misc.h"
82 #include "wine/unicode.h"
83 #include "wine/server.h"
84 #include "wine/list.h"
85 #include "wine/library.h"
86 #include "wine/debug.h"
88 WINE_DEFAULT_DEBUG_CHANNEL(file
);
91 #undef VFAT_IOCTL_READDIR_BOTH
96 /* We want the real kernel dirent structure, not the libc one */
101 unsigned short d_reclen
;
105 /* Define the VFAT ioctl to get both short and long file names */
106 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, KERNEL_DIRENT [2] )
109 # define O_DIRECTORY 0200000 /* must be directory */
112 #ifdef __NR_getdents64
117 unsigned short d_reclen
;
118 unsigned char d_type
;
122 static inline int getdents64( int fd
, char *de
, unsigned int size
)
124 return syscall( __NR_getdents64
, fd
, de
, size
);
131 #define IS_OPTION_TRUE(ch) ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
132 #define IS_SEPARATOR(ch) ((ch) == '\\' || (ch) == '/')
134 #define INVALID_NT_CHARS '*','?','<','>','|','"'
135 #define INVALID_DOS_CHARS INVALID_NT_CHARS,'+','=',',',';','[',']',' ','\345'
137 #define MAX_DIR_ENTRY_LEN 255 /* max length of a directory entry in chars */
139 #define MAX_IGNORED_FILES 4
147 static struct file_identity ignored_files
[MAX_IGNORED_FILES
];
148 static unsigned int ignored_files_count
;
150 union file_directory_info
153 FILE_DIRECTORY_INFORMATION dir
;
154 FILE_BOTH_DIRECTORY_INFORMATION both
;
155 FILE_FULL_DIRECTORY_INFORMATION full
;
156 FILE_ID_BOTH_DIRECTORY_INFORMATION id_both
;
157 FILE_ID_FULL_DIRECTORY_INFORMATION id_full
;
160 static BOOL show_dot_files
;
161 static RTL_RUN_ONCE init_once
= RTL_RUN_ONCE_INIT
;
163 /* at some point we may want to allow Winelib apps to set this */
164 static const BOOL is_case_sensitive
= FALSE
;
166 UNICODE_STRING system_dir
= { 0, 0, NULL
}; /* system directory */
168 static struct file_identity curdir
;
169 static struct file_identity windir
;
171 static RTL_CRITICAL_SECTION dir_section
;
172 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
175 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
176 0, 0, { (DWORD_PTR
)(__FILE__
": dir_section") }
178 static RTL_CRITICAL_SECTION dir_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
181 /* check if a given Unicode char is OK in a DOS short name */
182 static inline BOOL
is_invalid_dos_char( WCHAR ch
)
184 static const WCHAR invalid_chars
[] = { INVALID_DOS_CHARS
,'~','.',0 };
185 if (ch
> 0x7f) return TRUE
;
186 return strchrW( invalid_chars
, ch
) != NULL
;
189 /* check if the device can be a mounted volume */
190 static inline BOOL
is_valid_mounted_device( const struct stat
*st
)
192 #if defined(linux) || defined(__sun__)
193 return S_ISBLK( st
->st_mode
);
195 /* disks are char devices on *BSD */
196 return S_ISCHR( st
->st_mode
);
200 static inline void ignore_file( const char *name
)
203 assert( ignored_files_count
< MAX_IGNORED_FILES
);
204 if (!stat( name
, &st
))
206 ignored_files
[ignored_files_count
].dev
= st
.st_dev
;
207 ignored_files
[ignored_files_count
].ino
= st
.st_ino
;
208 ignored_files_count
++;
212 static inline BOOL
is_same_file( const struct file_identity
*file
, const struct stat
*st
)
214 return st
->st_dev
== file
->dev
&& st
->st_ino
== file
->ino
;
217 static inline BOOL
is_ignored_file( const struct stat
*st
)
221 for (i
= 0; i
< ignored_files_count
; i
++)
222 if (is_same_file( &ignored_files
[i
], st
)) return TRUE
;
226 static inline unsigned int dir_info_size( FILE_INFORMATION_CLASS
class, unsigned int len
)
230 case FileDirectoryInformation
:
231 return (FIELD_OFFSET( FILE_DIRECTORY_INFORMATION
, FileName
[len
] ) + 3) & ~3;
232 case FileBothDirectoryInformation
:
233 return (FIELD_OFFSET( FILE_BOTH_DIRECTORY_INFORMATION
, FileName
[len
] ) + 3) & ~3;
234 case FileFullDirectoryInformation
:
235 return (FIELD_OFFSET( FILE_FULL_DIRECTORY_INFORMATION
, FileName
[len
] ) + 3) & ~3;
236 case FileIdBothDirectoryInformation
:
237 return (FIELD_OFFSET( FILE_ID_BOTH_DIRECTORY_INFORMATION
, FileName
[len
] ) + 3) & ~3;
238 case FileIdFullDirectoryInformation
:
239 return (FIELD_OFFSET( FILE_ID_FULL_DIRECTORY_INFORMATION
, FileName
[len
] ) + 3) & ~3;
246 static inline unsigned int max_dir_info_size( FILE_INFORMATION_CLASS
class )
248 return dir_info_size( class, MAX_DIR_ENTRY_LEN
);
251 static inline BOOL
has_wildcard( const UNICODE_STRING
*mask
)
254 memchrW( mask
->Buffer
, '*', mask
->Length
/ sizeof(WCHAR
) ) ||
255 memchrW( mask
->Buffer
, '?', mask
->Length
/ sizeof(WCHAR
) ));
259 /* support for a directory queue for filesystem searches */
267 static struct list dir_queue
= LIST_INIT( dir_queue
);
269 static NTSTATUS
add_dir_to_queue( const char *name
)
271 int len
= strlen( name
) + 1;
272 struct dir_name
*dir
= RtlAllocateHeap( GetProcessHeap(), 0,
273 FIELD_OFFSET( struct dir_name
, name
[len
] ));
274 if (!dir
) return STATUS_NO_MEMORY
;
275 strcpy( dir
->name
, name
);
276 list_add_tail( &dir_queue
, &dir
->entry
);
277 return STATUS_SUCCESS
;
280 static NTSTATUS
next_dir_in_queue( char *name
)
282 struct list
*head
= list_head( &dir_queue
);
285 struct dir_name
*dir
= LIST_ENTRY( head
, struct dir_name
, entry
);
286 strcpy( name
, dir
->name
);
287 list_remove( &dir
->entry
);
288 RtlFreeHeap( GetProcessHeap(), 0, dir
);
289 return STATUS_SUCCESS
;
291 return STATUS_OBJECT_NAME_NOT_FOUND
;
294 static void flush_dir_queue(void)
298 while ((head
= list_head( &dir_queue
)))
300 struct dir_name
*dir
= LIST_ENTRY( head
, struct dir_name
, entry
);
301 list_remove( &dir
->entry
);
302 RtlFreeHeap( GetProcessHeap(), 0, dir
);
307 /***********************************************************************
308 * get_default_com_device
310 * Return the default device to use for serial ports.
312 static char *get_default_com_device( int num
)
316 if (!num
|| num
> 9) return ret
;
318 ret
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/ttyS0") );
321 strcpy( ret
, "/dev/ttyS0" );
322 ret
[strlen(ret
) - 1] = '0' + num
- 1;
324 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
325 ret
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/cuau0") );
328 strcpy( ret
, "/dev/cuau0" );
329 ret
[strlen(ret
) - 1] = '0' + num
- 1;
331 #elif defined(__DragonFly__)
332 ret
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/cuaa0") );
335 strcpy( ret
, "/dev/cuaa0" );
336 ret
[strlen(ret
) - 1] = '0' + num
- 1;
339 FIXME( "no known default for device com%d\n", num
);
345 /***********************************************************************
346 * get_default_lpt_device
348 * Return the default device to use for parallel ports.
350 static char *get_default_lpt_device( int num
)
354 if (!num
|| num
> 9) return ret
;
356 ret
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/lp0") );
359 strcpy( ret
, "/dev/lp0" );
360 ret
[strlen(ret
) - 1] = '0' + num
- 1;
363 FIXME( "no known default for device lpt%d\n", num
);
369 /***********************************************************************
370 * DIR_get_drives_info
372 * Retrieve device/inode number for all the drives. Helper for find_drive_root.
374 unsigned int DIR_get_drives_info( struct drive_info info
[MAX_DOS_DRIVES
] )
376 static struct drive_info cache
[MAX_DOS_DRIVES
];
377 static time_t last_update
;
378 static unsigned int nb_drives
;
380 time_t now
= time(NULL
);
382 RtlEnterCriticalSection( &dir_section
);
383 if (now
!= last_update
)
385 const char *config_dir
= wine_get_config_dir();
390 if ((buffer
= RtlAllocateHeap( GetProcessHeap(), 0,
391 strlen(config_dir
) + sizeof("/dosdevices/a:") )))
393 strcpy( buffer
, config_dir
);
394 strcat( buffer
, "/dosdevices/a:" );
395 p
= buffer
+ strlen(buffer
) - 2;
397 for (i
= nb_drives
= 0; i
< MAX_DOS_DRIVES
; i
++)
400 if (!stat( buffer
, &st
))
402 cache
[i
].dev
= st
.st_dev
;
403 cache
[i
].ino
= st
.st_ino
;
412 RtlFreeHeap( GetProcessHeap(), 0, buffer
);
416 memcpy( info
, cache
, sizeof(cache
) );
418 RtlLeaveCriticalSection( &dir_section
);
423 /***********************************************************************
424 * parse_mount_entries
426 * Parse mount entries looking for a given device. Helper for get_default_drive_device.
430 #include <sys/vfstab.h>
431 static char *parse_vfstab_entries( FILE *f
, dev_t dev
, ino_t ino
)
437 while (! getvfsent( f
, &entry
))
439 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
440 if (!strcmp( entry
.vfs_fstype
, "nfs" ) ||
441 !strcmp( entry
.vfs_fstype
, "smbfs" ) ||
442 !strcmp( entry
.vfs_fstype
, "ncpfs" )) continue;
444 if (stat( entry
.vfs_mountp
, &st
) == -1) continue;
445 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
446 if (!strcmp( entry
.vfs_fstype
, "fd" ))
448 if ((device
= strstr( entry
.vfs_mntopts
, "dev=" )))
450 char *p
= strchr( device
+ 4, ',' );
456 return entry
.vfs_special
;
463 static char *parse_mount_entries( FILE *f
, dev_t dev
, ino_t ino
)
465 struct mntent
*entry
;
469 while ((entry
= getmntent( f
)))
471 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
472 if (!strcmp( entry
->mnt_type
, "nfs" ) ||
473 !strcmp( entry
->mnt_type
, "smbfs" ) ||
474 !strcmp( entry
->mnt_type
, "ncpfs" )) continue;
476 if (stat( entry
->mnt_dir
, &st
) == -1) continue;
477 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
478 if (!strcmp( entry
->mnt_type
, "supermount" ))
480 if ((device
= strstr( entry
->mnt_opts
, "dev=" )))
482 char *p
= strchr( device
+ 4, ',' );
487 else if (!stat( entry
->mnt_fsname
, &st
) && S_ISREG(st
.st_mode
))
489 /* if device is a regular file check for a loop mount */
490 if ((device
= strstr( entry
->mnt_opts
, "loop=" )))
492 char *p
= strchr( device
+ 5, ',' );
498 return entry
->mnt_fsname
;
504 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
506 static char *parse_mount_entries( FILE *f
, dev_t dev
, ino_t ino
)
511 while ((entry
= getfsent()))
513 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
514 if (!strcmp( entry
->fs_vfstype
, "nfs" ) ||
515 !strcmp( entry
->fs_vfstype
, "smbfs" ) ||
516 !strcmp( entry
->fs_vfstype
, "ncpfs" )) continue;
518 if (stat( entry
->fs_file
, &st
) == -1) continue;
519 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
520 return entry
->fs_spec
;
527 #include <sys/mnttab.h>
528 static char *parse_mount_entries( FILE *f
, dev_t dev
, ino_t ino
)
535 while (( ! getmntent( f
, &entry
) ))
537 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
538 if (!strcmp( entry
.mnt_fstype
, "nfs" ) ||
539 !strcmp( entry
.mnt_fstype
, "smbfs" ) ||
540 !strcmp( entry
.mnt_fstype
, "ncpfs" )) continue;
542 if (stat( entry
.mnt_mountp
, &st
) == -1) continue;
543 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
544 if (!strcmp( entry
.mnt_fstype
, "fd" ))
546 if ((device
= strstr( entry
.mnt_mntopts
, "dev=" )))
548 char *p
= strchr( device
+ 4, ',' );
554 return entry
.mnt_special
;
560 /***********************************************************************
561 * get_default_drive_device
563 * Return the default device to use for a given drive mount point.
565 static char *get_default_drive_device( const char *root
)
575 /* try to open it first to force it to get mounted */
576 if ((fd
= open( root
, O_RDONLY
| O_DIRECTORY
)) != -1)
578 res
= fstat( fd
, &st
);
581 /* now try normal stat just in case */
582 if (res
== -1) res
= stat( root
, &st
);
583 if (res
== -1) return NULL
;
585 RtlEnterCriticalSection( &dir_section
);
587 if ((f
= fopen( "/etc/mtab", "r" )))
589 device
= parse_mount_entries( f
, st
.st_dev
, st
.st_ino
);
592 /* look through fstab too in case it's not mounted (for instance if it's an audio CD) */
593 if (!device
&& (f
= fopen( "/etc/fstab", "r" )))
595 device
= parse_mount_entries( f
, st
.st_dev
, st
.st_ino
);
600 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(device
) + 1 );
601 if (ret
) strcpy( ret
, device
);
603 RtlLeaveCriticalSection( &dir_section
);
605 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__ ) || defined(__DragonFly__)
610 /* try to open it first to force it to get mounted */
611 if ((fd
= open( root
, O_RDONLY
)) != -1)
613 res
= fstat( fd
, &st
);
616 /* now try normal stat just in case */
617 if (res
== -1) res
= stat( root
, &st
);
618 if (res
== -1) return NULL
;
620 RtlEnterCriticalSection( &dir_section
);
622 /* The FreeBSD parse_mount_entries doesn't require a file argument, so just
623 * pass NULL. Leave the argument in for symmetry.
625 device
= parse_mount_entries( NULL
, st
.st_dev
, st
.st_ino
);
628 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(device
) + 1 );
629 if (ret
) strcpy( ret
, device
);
631 RtlLeaveCriticalSection( &dir_section
);
639 /* try to open it first to force it to get mounted */
640 if ((fd
= open( root
, O_RDONLY
)) != -1)
642 res
= fstat( fd
, &st
);
645 /* now try normal stat just in case */
646 if (res
== -1) res
= stat( root
, &st
);
647 if (res
== -1) return NULL
;
649 RtlEnterCriticalSection( &dir_section
);
651 if ((f
= fopen( "/etc/mnttab", "r" )))
653 device
= parse_mount_entries( f
, st
.st_dev
, st
.st_ino
);
656 /* look through fstab too in case it's not mounted (for instance if it's an audio CD) */
657 if (!device
&& (f
= fopen( "/etc/vfstab", "r" )))
659 device
= parse_vfstab_entries( f
, st
.st_dev
, st
.st_ino
);
664 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(device
) + 1 );
665 if (ret
) strcpy( ret
, device
);
667 RtlLeaveCriticalSection( &dir_section
);
669 #elif defined(__APPLE__)
670 struct statfs
*mntStat
;
676 static const char path_bsd_device
[] = "/dev/disk";
679 res
= stat( root
, &st
);
680 if (res
== -1) return NULL
;
685 RtlEnterCriticalSection( &dir_section
);
687 mntSize
= getmntinfo(&mntStat
, MNT_NOWAIT
);
689 for (i
= 0; i
< mntSize
&& !ret
; i
++)
691 if (stat(mntStat
[i
].f_mntonname
, &st
) == -1) continue;
692 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
694 /* FIXME add support for mounted network drive */
695 if ( strncmp(mntStat
[i
].f_mntfromname
, path_bsd_device
, strlen(path_bsd_device
)) == 0)
697 /* set return value to the corresponding raw BSD node */
698 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(mntStat
[i
].f_mntfromname
) + 2 /* 2 : r and \0 */ );
701 strcpy(ret
, "/dev/r");
702 strcat(ret
, mntStat
[i
].f_mntfromname
+sizeof("/dev/")-1);
706 RtlLeaveCriticalSection( &dir_section
);
709 if (!warned
++) FIXME( "auto detection of DOS devices not supported on this platform\n" );
715 /***********************************************************************
716 * get_device_mount_point
718 * Return the current mount point for a device.
720 static char *get_device_mount_point( dev_t dev
)
727 RtlEnterCriticalSection( &dir_section
);
729 if ((f
= fopen( "/etc/mtab", "r" )))
731 struct mntent
*entry
;
735 while ((entry
= getmntent( f
)))
737 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
738 if (!strcmp( entry
->mnt_type
, "nfs" ) ||
739 !strcmp( entry
->mnt_type
, "smbfs" ) ||
740 !strcmp( entry
->mnt_type
, "ncpfs" )) continue;
742 if (!strcmp( entry
->mnt_type
, "supermount" ))
744 if ((device
= strstr( entry
->mnt_opts
, "dev=" )))
747 if ((p
= strchr( device
, ',' ))) *p
= 0;
750 else if (!stat( entry
->mnt_fsname
, &st
) && S_ISREG(st
.st_mode
))
752 /* if device is a regular file check for a loop mount */
753 if ((device
= strstr( entry
->mnt_opts
, "loop=" )))
756 if ((p
= strchr( device
, ',' ))) *p
= 0;
759 else device
= entry
->mnt_fsname
;
761 if (device
&& !stat( device
, &st
) && S_ISBLK(st
.st_mode
) && st
.st_rdev
== dev
)
763 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(entry
->mnt_dir
) + 1 );
764 if (ret
) strcpy( ret
, entry
->mnt_dir
);
770 RtlLeaveCriticalSection( &dir_section
);
771 #elif defined(__APPLE__)
772 struct statfs
*entry
;
776 RtlEnterCriticalSection( &dir_section
);
778 size
= getmntinfo( &entry
, MNT_NOWAIT
);
779 for (i
= 0; i
< size
; i
++)
781 if (stat( entry
[i
].f_mntfromname
, &st
) == -1) continue;
782 if (S_ISBLK(st
.st_mode
) && st
.st_rdev
== dev
)
784 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(entry
[i
].f_mntonname
) + 1 );
785 if (ret
) strcpy( ret
, entry
[i
].f_mntonname
);
789 RtlLeaveCriticalSection( &dir_section
);
792 if (!warned
++) FIXME( "unmounting devices not supported on this platform\n" );
798 #if defined(HAVE_GETATTRLIST) && defined(ATTR_VOL_CAPABILITIES) && \
799 defined(VOL_CAPABILITIES_FORMAT) && defined(VOL_CAP_FMT_CASE_SENSITIVE)
812 BOOLEAN case_sensitive
;
818 vol_capabilities_attr_t caps
;
821 /***********************************************************************
824 * Checks if the specified file system is in the cache.
826 static struct fs_cache
*look_up_fs_cache( dev_t dev
)
829 for (i
= 0; i
< sizeof(fs_cache
)/sizeof(fs_cache
[0]); i
++)
830 if (fs_cache
[i
].dev
== dev
)
835 /***********************************************************************
838 * Adds the specified file system to the cache.
840 static void add_fs_cache( dev_t dev
, fsid_t fsid
, BOOLEAN case_sensitive
)
843 struct fs_cache
*entry
= look_up_fs_cache( dev
);
847 /* Update the cache */
849 entry
->case_sensitive
= case_sensitive
;
853 /* Add a new entry */
854 for (i
= 0; i
< sizeof(fs_cache
)/sizeof(fs_cache
[0]); i
++)
855 if (fs_cache
[i
].dev
== 0)
857 /* This entry is empty, use it */
858 fs_cache
[i
].dev
= dev
;
859 fs_cache
[i
].fsid
= fsid
;
860 fs_cache
[i
].case_sensitive
= case_sensitive
;
864 /* Cache is out of space, warn */
866 WARN( "FS cache is out of space, expect performance problems\n" );
869 /***********************************************************************
870 * get_dir_case_sensitivity_attr
872 * Checks if the volume containing the specified directory is case
873 * sensitive or not. Uses getattrlist(2).
875 static int get_dir_case_sensitivity_attr( const char *dir
)
877 char *mntpoint
= NULL
;
878 struct attrlist attr
;
879 struct vol_caps caps
;
880 struct get_fsid get_fsid
;
881 struct fs_cache
*entry
;
883 /* First get the FS ID of the volume */
884 attr
.bitmapcount
= ATTR_BIT_MAP_COUNT
;
886 attr
.commonattr
= ATTR_CMN_DEVID
|ATTR_CMN_FSID
;
887 attr
.volattr
= attr
.dirattr
= attr
.fileattr
= attr
.forkattr
= 0;
889 if (getattrlist( dir
, &attr
, &get_fsid
, sizeof(get_fsid
), 0 ) != 0 ||
890 get_fsid
.size
!= sizeof(get_fsid
))
892 /* Try to look it up in the cache */
893 entry
= look_up_fs_cache( get_fsid
.dev
);
894 if (entry
&& !memcmp( &entry
->fsid
, &get_fsid
.fsid
, sizeof(fsid_t
) ))
895 /* Cache lookup succeeded */
896 return entry
->case_sensitive
;
897 /* Cache is stale at this point, we have to update it */
899 mntpoint
= get_device_mount_point( get_fsid
.dev
);
900 /* Now look up the case-sensitivity */
902 attr
.volattr
= ATTR_VOL_INFO
|ATTR_VOL_CAPABILITIES
;
903 if (getattrlist( mntpoint
, &attr
, &caps
, sizeof(caps
), 0 ) < 0)
905 RtlFreeHeap( GetProcessHeap(), 0, mntpoint
);
906 add_fs_cache( get_fsid
.dev
, get_fsid
.fsid
, TRUE
);
909 RtlFreeHeap( GetProcessHeap(), 0, mntpoint
);
910 if (caps
.size
== sizeof(caps
) &&
911 (caps
.caps
.valid
[VOL_CAPABILITIES_FORMAT
] &
912 (VOL_CAP_FMT_CASE_SENSITIVE
| VOL_CAP_FMT_CASE_PRESERVING
)) ==
913 (VOL_CAP_FMT_CASE_SENSITIVE
| VOL_CAP_FMT_CASE_PRESERVING
))
917 if ((caps
.caps
.capabilities
[VOL_CAPABILITIES_FORMAT
] &
918 VOL_CAP_FMT_CASE_SENSITIVE
) != VOL_CAP_FMT_CASE_SENSITIVE
)
922 /* Update the cache */
923 add_fs_cache( get_fsid
.dev
, get_fsid
.fsid
, ret
);
930 /***********************************************************************
931 * get_dir_case_sensitivity_stat
933 * Checks if the volume containing the specified directory is case
934 * sensitive or not. Uses statfs(2) or statvfs(2).
936 static BOOLEAN
get_dir_case_sensitivity_stat( const char *dir
)
938 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
941 if (statfs( dir
, &stfs
) == -1) return FALSE
;
942 /* Assume these file systems are always case insensitive on Mac OS.
943 * For FreeBSD, only assume CIOPFS is case insensitive (AFAIK, Mac OS
944 * is the only UNIX that supports case-insensitive lookup).
946 if (!strcmp( stfs
.f_fstypename
, "fusefs" ) &&
947 !strncmp( stfs
.f_mntfromname
, "ciopfs", 5 ))
950 if (!strcmp( stfs
.f_fstypename
, "msdos" ) ||
951 !strcmp( stfs
.f_fstypename
, "cd9660" ) ||
952 !strcmp( stfs
.f_fstypename
, "udf" ) ||
953 !strcmp( stfs
.f_fstypename
, "ntfs" ) ||
954 !strcmp( stfs
.f_fstypename
, "smbfs" ))
956 #ifdef _DARWIN_FEATURE_64_BIT_INODE
957 if (!strcmp( stfs
.f_fstypename
, "hfs" ) && (stfs
.f_fssubtype
== 0 ||
958 stfs
.f_fssubtype
== 1 ||
959 stfs
.f_fssubtype
== 128))
962 /* The field says "reserved", but a quick look at the kernel source
963 * tells us that this "reserved" field is really the same as the
964 * "fssubtype" field from the inode64 structure (see munge_statfs()
965 * in <xnu-source>/bsd/vfs/vfs_syscalls.c).
967 if (!strcmp( stfs
.f_fstypename
, "hfs" ) && (stfs
.f_reserved1
== 0 ||
968 stfs
.f_reserved1
== 1 ||
969 stfs
.f_reserved1
== 128))
975 #elif defined(__NetBSD__)
978 if (statvfs( dir
, &stfs
) == -1) return FALSE
;
979 /* Only assume CIOPFS is case insensitive. */
980 if (strcmp( stfs
.f_fstypename
, "fusefs" ) ||
981 strncmp( stfs
.f_mntfromname
, "ciopfs", 5 ))
985 #elif defined(__linux__)
990 /* Only assume CIOPFS is case insensitive. */
991 if (statfs( dir
, &stfs
) == -1) return FALSE
;
992 if (stfs
.f_type
!= 0x65735546 /* FUSE_SUPER_MAGIC */)
994 /* Normally, we'd have to parse the mtab to find out exactly what
995 * kind of FUSE FS this is. But, someone on wine-devel suggested
996 * a shortcut. We'll stat a special file in the directory. If it's
997 * there, we'll assume it's a CIOPFS, else not.
998 * This will break if somebody puts a file named ".ciopfs" in a non-
1001 cifile
= RtlAllocateHeap( GetProcessHeap(), 0, strlen( dir
)+sizeof("/.ciopfs") );
1002 if (!cifile
) return TRUE
;
1003 strcpy( cifile
, dir
);
1004 strcat( cifile
, "/.ciopfs" );
1005 if (stat( cifile
, &st
) == 0)
1007 RtlFreeHeap( GetProcessHeap(), 0, cifile
);
1010 RtlFreeHeap( GetProcessHeap(), 0, cifile
);
1018 /***********************************************************************
1019 * get_dir_case_sensitivity
1021 * Checks if the volume containing the specified directory is case
1022 * sensitive or not. Uses statfs(2) or statvfs(2).
1024 static BOOLEAN
get_dir_case_sensitivity( const char *dir
)
1026 #if defined(HAVE_GETATTRLIST) && defined(ATTR_VOL_CAPABILITIES) && \
1027 defined(VOL_CAPABILITIES_FORMAT) && defined(VOL_CAP_FMT_CASE_SENSITIVE)
1028 int case_sensitive
= get_dir_case_sensitivity_attr( dir
);
1029 if (case_sensitive
!= -1) return case_sensitive
;
1031 return get_dir_case_sensitivity_stat( dir
);
1035 /***********************************************************************
1038 * Initialize the show_dot_files options.
1040 static DWORD WINAPI
init_options( RTL_RUN_ONCE
*once
, void *param
, void **context
)
1042 static const WCHAR WineW
[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e',0};
1043 static const WCHAR ShowDotFilesW
[] = {'S','h','o','w','D','o','t','F','i','l','e','s',0};
1047 OBJECT_ATTRIBUTES attr
;
1048 UNICODE_STRING nameW
;
1050 RtlOpenCurrentUser( KEY_ALL_ACCESS
, &root
);
1051 attr
.Length
= sizeof(attr
);
1052 attr
.RootDirectory
= root
;
1053 attr
.ObjectName
= &nameW
;
1054 attr
.Attributes
= 0;
1055 attr
.SecurityDescriptor
= NULL
;
1056 attr
.SecurityQualityOfService
= NULL
;
1057 RtlInitUnicodeString( &nameW
, WineW
);
1059 /* @@ Wine registry key: HKCU\Software\Wine */
1060 if (!NtOpenKey( &hkey
, KEY_ALL_ACCESS
, &attr
))
1062 RtlInitUnicodeString( &nameW
, ShowDotFilesW
);
1063 if (!NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
, tmp
, sizeof(tmp
), &dummy
))
1065 WCHAR
*str
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)tmp
)->Data
;
1066 show_dot_files
= IS_OPTION_TRUE( str
[0] );
1072 /* a couple of directories that we don't want to return in directory searches */
1073 ignore_file( wine_get_config_dir() );
1074 ignore_file( "/dev" );
1075 ignore_file( "/proc" );
1077 ignore_file( "/sys" );
1083 /***********************************************************************
1084 * DIR_is_hidden_file
1086 * Check if the specified file should be hidden based on its name and the show dot files option.
1088 BOOL
DIR_is_hidden_file( const UNICODE_STRING
*name
)
1092 RtlRunOnceExecuteOnce( &init_once
, init_options
, NULL
, NULL
);
1094 if (show_dot_files
) return FALSE
;
1096 end
= p
= name
->Buffer
+ name
->Length
/sizeof(WCHAR
);
1097 while (p
> name
->Buffer
&& IS_SEPARATOR(p
[-1])) p
--;
1098 while (p
> name
->Buffer
&& !IS_SEPARATOR(p
[-1])) p
--;
1099 if (p
== end
|| *p
!= '.') return FALSE
;
1100 /* make sure it isn't '.' or '..' */
1101 if (p
+ 1 == end
) return FALSE
;
1102 if (p
[1] == '.' && p
+ 2 == end
) return FALSE
;
1107 /***********************************************************************
1108 * hash_short_file_name
1110 * Transform a Unix file name into a hashed DOS name. If the name is a valid
1111 * DOS name, it is converted to upper-case; otherwise it is replaced by a
1112 * hashed version that fits in 8.3 format.
1113 * 'buffer' must be at least 12 characters long.
1114 * Returns length of short name in bytes; short name is NOT null-terminated.
1116 static ULONG
hash_short_file_name( const UNICODE_STRING
*name
, LPWSTR buffer
)
1118 static const char hash_chars
[32] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345";
1120 LPCWSTR p
, ext
, end
= name
->Buffer
+ name
->Length
/ sizeof(WCHAR
);
1122 unsigned short hash
;
1125 /* Compute the hash code of the file name */
1126 /* If you know something about hash functions, feel free to */
1127 /* insert a better algorithm here... */
1128 if (!is_case_sensitive
)
1130 for (p
= name
->Buffer
, hash
= 0xbeef; p
< end
- 1; p
++)
1131 hash
= (hash
<<3) ^ (hash
>>5) ^ tolowerW(*p
) ^ (tolowerW(p
[1]) << 8);
1132 hash
= (hash
<<3) ^ (hash
>>5) ^ tolowerW(*p
); /* Last character */
1136 for (p
= name
->Buffer
, hash
= 0xbeef; p
< end
- 1; p
++)
1137 hash
= (hash
<< 3) ^ (hash
>> 5) ^ *p
^ (p
[1] << 8);
1138 hash
= (hash
<< 3) ^ (hash
>> 5) ^ *p
; /* Last character */
1141 /* Find last dot for start of the extension */
1142 for (p
= name
->Buffer
+ 1, ext
= NULL
; p
< end
- 1; p
++) if (*p
== '.') ext
= p
;
1144 /* Copy first 4 chars, replacing invalid chars with '_' */
1145 for (i
= 4, p
= name
->Buffer
, dst
= buffer
; i
> 0; i
--, p
++)
1147 if (p
== end
|| p
== ext
) break;
1148 *dst
++ = is_invalid_dos_char(*p
) ? '_' : toupperW(*p
);
1150 /* Pad to 5 chars with '~' */
1151 while (i
-- >= 0) *dst
++ = '~';
1153 /* Insert hash code converted to 3 ASCII chars */
1154 *dst
++ = hash_chars
[(hash
>> 10) & 0x1f];
1155 *dst
++ = hash_chars
[(hash
>> 5) & 0x1f];
1156 *dst
++ = hash_chars
[hash
& 0x1f];
1158 /* Copy the first 3 chars of the extension (if any) */
1162 for (i
= 3, ext
++; (i
> 0) && ext
< end
; i
--, ext
++)
1163 *dst
++ = is_invalid_dos_char(*ext
) ? '_' : toupperW(*ext
);
1165 return dst
- buffer
;
1169 /***********************************************************************
1172 * Check a long file name against a mask.
1174 * Tests (done in W95 DOS shell - case insensitive):
1175 * *.txt test1.test.txt *
1177 * *.t??????.t* test1.ta.tornado.txt *
1178 * *tornado* test1.ta.tornado.txt *
1179 * t*t test1.ta.tornado.txt *
1181 * ?est??? test1.txt -
1182 * *test1.txt* test1.txt *
1183 * h?l?o*t.dat hellothisisatest.dat *
1185 static BOOLEAN
match_filename( const UNICODE_STRING
*name_str
, const UNICODE_STRING
*mask_str
)
1188 const WCHAR
*name
= name_str
->Buffer
;
1189 const WCHAR
*mask
= mask_str
->Buffer
;
1190 const WCHAR
*name_end
= name
+ name_str
->Length
/ sizeof(WCHAR
);
1191 const WCHAR
*mask_end
= mask
+ mask_str
->Length
/ sizeof(WCHAR
);
1192 const WCHAR
*lastjoker
= NULL
;
1193 const WCHAR
*next_to_retry
= NULL
;
1195 TRACE("(%s, %s)\n", debugstr_us(name_str
), debugstr_us(mask_str
));
1197 while (name
< name_end
&& mask
< mask_end
)
1203 while (mask
< mask_end
&& *mask
== '*') mask
++; /* Skip consecutive '*' */
1204 if (mask
== mask_end
) return TRUE
; /* end of mask is all '*', so match */
1207 /* skip to the next match after the joker(s) */
1208 if (is_case_sensitive
)
1209 while (name
< name_end
&& (*name
!= *mask
)) name
++;
1211 while (name
< name_end
&& (toupperW(*name
) != toupperW(*mask
))) name
++;
1212 next_to_retry
= name
;
1219 if (is_case_sensitive
) mismatch
= (*mask
!= *name
);
1220 else mismatch
= (toupperW(*mask
) != toupperW(*name
));
1226 if (mask
== mask_end
)
1228 if (name
== name_end
) return TRUE
;
1229 if (lastjoker
) mask
= lastjoker
;
1232 else /* mismatch ! */
1234 if (lastjoker
) /* we had an '*', so we can try unlimitedly */
1238 /* this scan sequence was a mismatch, so restart
1239 * 1 char after the first char we checked last time */
1241 name
= next_to_retry
;
1243 else return FALSE
; /* bad luck */
1248 while (mask
< mask_end
&& ((*mask
== '.') || (*mask
== '*')))
1249 mask
++; /* Ignore trailing '.' or '*' in mask */
1250 return (name
== name_end
&& mask
== mask_end
);
1254 /***********************************************************************
1257 * helper for NtQueryDirectoryFile
1259 static union file_directory_info
*append_entry( void *info_ptr
, IO_STATUS_BLOCK
*io
, ULONG max_length
,
1260 const char *long_name
, const char *short_name
,
1261 const UNICODE_STRING
*mask
, FILE_INFORMATION_CLASS
class )
1263 union file_directory_info
*info
;
1264 int i
, long_len
, short_len
, total_len
;
1266 WCHAR long_nameW
[MAX_DIR_ENTRY_LEN
];
1267 WCHAR short_nameW
[12];
1270 ULONG attributes
= 0;
1272 io
->u
.Status
= STATUS_SUCCESS
;
1273 long_len
= ntdll_umbstowcs( 0, long_name
, strlen(long_name
), long_nameW
, MAX_DIR_ENTRY_LEN
);
1274 if (long_len
== -1) return NULL
;
1276 str
.Buffer
= long_nameW
;
1277 str
.Length
= long_len
* sizeof(WCHAR
);
1278 str
.MaximumLength
= sizeof(long_nameW
);
1282 short_len
= ntdll_umbstowcs( 0, short_name
, strlen(short_name
),
1283 short_nameW
, sizeof(short_nameW
) / sizeof(WCHAR
) );
1284 if (short_len
== -1) short_len
= sizeof(short_nameW
) / sizeof(WCHAR
);
1286 else /* generate a short name if necessary */
1291 if (!RtlIsNameLegalDOS8Dot3( &str
, NULL
, &spaces
) || spaces
)
1292 short_len
= hash_short_file_name( &str
, short_nameW
);
1295 TRACE( "long %s short %s mask %s\n",
1296 debugstr_us(&str
), debugstr_wn(short_nameW
, short_len
), debugstr_us(mask
) );
1298 if (mask
&& !match_filename( &str
, mask
))
1300 if (!short_len
) return NULL
; /* no short name to match */
1301 str
.Buffer
= short_nameW
;
1302 str
.Length
= short_len
* sizeof(WCHAR
);
1303 str
.MaximumLength
= sizeof(short_nameW
);
1304 if (!match_filename( &str
, mask
)) return NULL
;
1307 if (lstat( long_name
, &st
) == -1) return NULL
;
1308 if (S_ISLNK( st
.st_mode
))
1310 if (stat( long_name
, &st
) == -1) return NULL
;
1311 if (S_ISDIR( st
.st_mode
)) attributes
|= FILE_ATTRIBUTE_REPARSE_POINT
;
1313 if (is_ignored_file( &st
))
1315 TRACE( "ignoring file %s\n", long_name
);
1318 if (!show_dot_files
&& long_name
[0] == '.' && long_name
[1] && (long_name
[1] != '.' || long_name
[2]))
1319 attributes
|= FILE_ATTRIBUTE_HIDDEN
;
1321 total_len
= dir_info_size( class, long_len
);
1322 if (io
->Information
+ total_len
> max_length
)
1324 total_len
= max_length
- io
->Information
;
1325 io
->u
.Status
= STATUS_BUFFER_OVERFLOW
;
1327 info
= (union file_directory_info
*)((char *)info_ptr
+ io
->Information
);
1328 if (st
.st_dev
!= curdir
.dev
) st
.st_ino
= 0; /* ignore inode if on a different device */
1329 /* all the structures start with a FileDirectoryInformation layout */
1330 fill_stat_info( &st
, info
, class );
1331 info
->dir
.NextEntryOffset
= total_len
;
1332 info
->dir
.FileIndex
= 0; /* NTFS always has 0 here, so let's not bother with it */
1333 info
->dir
.FileAttributes
|= attributes
;
1337 case FileDirectoryInformation
:
1338 info
->dir
.FileNameLength
= long_len
* sizeof(WCHAR
);
1339 filename
= info
->dir
.FileName
;
1342 case FileFullDirectoryInformation
:
1343 info
->full
.EaSize
= 0; /* FIXME */
1344 info
->full
.FileNameLength
= long_len
* sizeof(WCHAR
);
1345 filename
= info
->full
.FileName
;
1348 case FileIdFullDirectoryInformation
:
1349 info
->id_full
.EaSize
= 0; /* FIXME */
1350 info
->id_full
.FileNameLength
= long_len
* sizeof(WCHAR
);
1351 filename
= info
->id_full
.FileName
;
1354 case FileBothDirectoryInformation
:
1355 info
->both
.EaSize
= 0; /* FIXME */
1356 info
->both
.ShortNameLength
= short_len
* sizeof(WCHAR
);
1357 for (i
= 0; i
< short_len
; i
++) info
->both
.ShortName
[i
] = toupperW(short_nameW
[i
]);
1358 info
->both
.FileNameLength
= long_len
* sizeof(WCHAR
);
1359 filename
= info
->both
.FileName
;
1362 case FileIdBothDirectoryInformation
:
1363 info
->id_both
.EaSize
= 0; /* FIXME */
1364 info
->id_both
.ShortNameLength
= short_len
* sizeof(WCHAR
);
1365 for (i
= 0; i
< short_len
; i
++) info
->id_both
.ShortName
[i
] = toupperW(short_nameW
[i
]);
1366 info
->id_both
.FileNameLength
= long_len
* sizeof(WCHAR
);
1367 filename
= info
->id_both
.FileName
;
1374 memcpy( filename
, long_nameW
, total_len
- ((char *)filename
- (char *)info
) );
1375 io
->Information
+= total_len
;
1380 #ifdef VFAT_IOCTL_READDIR_BOTH
1382 /***********************************************************************
1385 * Wrapper for the VFAT ioctl to work around various kernel bugs.
1386 * dir_section must be held by caller.
1388 static KERNEL_DIRENT
*start_vfat_ioctl( int fd
)
1390 static KERNEL_DIRENT
*de
;
1395 SIZE_T size
= 2 * sizeof(*de
) + page_size
;
1398 if (NtAllocateVirtualMemory( GetCurrentProcess(), &addr
, 1, &size
, MEM_RESERVE
, PAGE_READWRITE
))
1400 /* commit only the size needed for the dir entries */
1401 /* this leaves an extra unaccessible page, which should make the kernel */
1402 /* fail with -EFAULT before it stomps all over our memory */
1404 size
= 2 * sizeof(*de
);
1405 NtAllocateVirtualMemory( GetCurrentProcess(), &addr
, 1, &size
, MEM_COMMIT
, PAGE_READWRITE
);
1408 /* set d_reclen to 65535 to work around an AFS kernel bug */
1409 de
[0].d_reclen
= 65535;
1410 res
= ioctl( fd
, VFAT_IOCTL_READDIR_BOTH
, (long)de
);
1413 if (errno
!= ENOENT
) return NULL
; /* VFAT ioctl probably not supported */
1414 de
[0].d_reclen
= 0; /* eof */
1416 else if (!res
&& de
[0].d_reclen
== 65535) return NULL
; /* AFS bug */
1422 /***********************************************************************
1423 * read_directory_vfat
1425 * Read a directory using the VFAT ioctl; helper for NtQueryDirectoryFile.
1427 static int read_directory_vfat( int fd
, IO_STATUS_BLOCK
*io
, void *buffer
, ULONG length
,
1428 BOOLEAN single_entry
, const UNICODE_STRING
*mask
,
1429 BOOLEAN restart_scan
, FILE_INFORMATION_CLASS
class )
1434 union file_directory_info
*info
, *last_info
= NULL
;
1436 io
->u
.Status
= STATUS_SUCCESS
;
1438 if (restart_scan
) lseek( fd
, 0, SEEK_SET
);
1440 if (length
< max_dir_info_size(class)) /* we may have to return a partial entry here */
1442 off_t old_pos
= lseek( fd
, 0, SEEK_CUR
);
1444 if (!(de
= start_vfat_ioctl( fd
))) return -1; /* not supported */
1446 while (de
[0].d_reclen
)
1448 /* make sure names are null-terminated to work around an x86-64 kernel bug */
1449 len
= min(de
[0].d_reclen
, sizeof(de
[0].d_name
) - 1 );
1450 de
[0].d_name
[len
] = 0;
1451 len
= min(de
[1].d_reclen
, sizeof(de
[1].d_name
) - 1 );
1452 de
[1].d_name
[len
] = 0;
1454 if (de
[1].d_name
[0])
1455 info
= append_entry( buffer
, io
, length
, de
[1].d_name
, de
[0].d_name
, mask
, class );
1457 info
= append_entry( buffer
, io
, length
, de
[0].d_name
, NULL
, mask
, class );
1461 if (io
->u
.Status
== STATUS_BUFFER_OVERFLOW
)
1462 lseek( fd
, old_pos
, SEEK_SET
); /* restore pos to previous entry */
1465 old_pos
= lseek( fd
, 0, SEEK_CUR
);
1466 if (ioctl( fd
, VFAT_IOCTL_READDIR_BOTH
, (long)de
) == -1) break;
1469 else /* we'll only return full entries, no need to worry about overflow */
1471 if (!(de
= start_vfat_ioctl( fd
))) return -1; /* not supported */
1473 while (de
[0].d_reclen
)
1475 /* make sure names are null-terminated to work around an x86-64 kernel bug */
1476 len
= min(de
[0].d_reclen
, sizeof(de
[0].d_name
) - 1 );
1477 de
[0].d_name
[len
] = 0;
1478 len
= min(de
[1].d_reclen
, sizeof(de
[1].d_name
) - 1 );
1479 de
[1].d_name
[len
] = 0;
1481 if (de
[1].d_name
[0])
1482 info
= append_entry( buffer
, io
, length
, de
[1].d_name
, de
[0].d_name
, mask
, class );
1484 info
= append_entry( buffer
, io
, length
, de
[0].d_name
, NULL
, mask
, class );
1488 if (single_entry
) break;
1489 /* check if we still have enough space for the largest possible entry */
1490 if (io
->Information
+ max_dir_info_size(class) > length
) break;
1492 if (ioctl( fd
, VFAT_IOCTL_READDIR_BOTH
, (long)de
) == -1) break;
1496 if (last_info
) last_info
->next
= 0;
1497 else io
->u
.Status
= restart_scan
? STATUS_NO_SUCH_FILE
: STATUS_NO_MORE_FILES
;
1500 #endif /* VFAT_IOCTL_READDIR_BOTH */
1504 /***********************************************************************
1505 * read_first_dent_name
1507 * reads name of first or second dentry (if they have inodes).
1509 static char *read_first_dent_name( int which
, int fd
, off_t second_offs
, KERNEL_DIRENT64
*de_first_two
,
1510 char *buffer
, size_t size
, BOOL
*buffer_changed
)
1512 KERNEL_DIRENT64
*de
;
1519 de
= (KERNEL_DIRENT64
*)((char *)de
+ de
->d_reclen
);
1521 return de
->d_ino
? de
->d_name
: NULL
;
1524 *buffer_changed
= TRUE
;
1525 lseek( fd
, which
== 1 ? second_offs
: 0, SEEK_SET
);
1526 res
= getdents64( fd
, buffer
, size
);
1530 de
= (KERNEL_DIRENT64
*)buffer
;
1531 return de
->d_ino
? de
->d_name
: NULL
;
1534 /***********************************************************************
1535 * read_directory_getdents
1537 * Read a directory using the Linux getdents64 system call; helper for NtQueryDirectoryFile.
1539 static int read_directory_getdents( int fd
, IO_STATUS_BLOCK
*io
, void *buffer
, ULONG length
,
1540 BOOLEAN single_entry
, const UNICODE_STRING
*mask
,
1541 BOOLEAN restart_scan
, FILE_INFORMATION_CLASS
class )
1543 static off_t second_entry_pos
;
1544 static struct file_identity last_dir_id
;
1545 off_t old_pos
= 0, next_pos
;
1546 size_t size
= length
;
1547 char *data
, local_buffer
[8192];
1548 KERNEL_DIRENT64
*de
, *de_first_two
= NULL
;
1549 union file_directory_info
*info
, *last_info
= NULL
;
1550 const char *filename
;
1551 BOOL data_buffer_changed
;
1554 if (size
<= sizeof(local_buffer
) || !(data
= RtlAllocateHeap( GetProcessHeap(), 0, size
)))
1556 size
= sizeof(local_buffer
);
1557 data
= local_buffer
;
1560 if (restart_scan
) lseek( fd
, 0, SEEK_SET
);
1563 old_pos
= lseek( fd
, 0, SEEK_CUR
);
1566 io
->u
.Status
= (errno
== ENOENT
) ? STATUS_NO_MORE_FILES
: FILE_GetNtStatus();
1572 io
->u
.Status
= STATUS_SUCCESS
;
1573 de
= (KERNEL_DIRENT64
*)data
;
1575 /* if old_pos is not 0 we don't know how many entries have been returned already,
1576 * so maintain second_entry_pos to know when to return '..' */
1577 if (old_pos
!= 0 && (last_dir_id
.dev
!= curdir
.dev
|| last_dir_id
.ino
!= curdir
.ino
))
1579 lseek( fd
, 0, SEEK_SET
);
1580 res
= getdents64( fd
, data
, size
);
1583 second_entry_pos
= de
->d_off
;
1584 last_dir_id
= curdir
;
1586 lseek( fd
, old_pos
, SEEK_SET
);
1589 res
= getdents64( fd
, data
, size
);
1592 if (errno
!= ENOSYS
)
1594 io
->u
.Status
= FILE_GetNtStatus();
1600 if (old_pos
== 0 && res
> 0)
1602 second_entry_pos
= de
->d_off
;
1603 last_dir_id
= curdir
;
1604 if (res
> de
->d_reclen
)
1610 res
-= de
->d_reclen
;
1611 next_pos
= de
->d_off
;
1614 /* we must return first 2 entries as "." and "..", but getdents64()
1615 * can return them anywhere, so swap first entries with "." and ".." */
1618 else if (old_pos
== second_entry_pos
)
1620 else if (!strcmp( de
->d_name
, "." ) || !strcmp( de
->d_name
, ".." ))
1622 swap_to
= !strcmp( de
->d_name
, "." ) ? 0 : 1;
1623 data_buffer_changed
= FALSE
;
1625 filename
= read_first_dent_name( swap_to
, fd
, second_entry_pos
, de_first_two
,
1626 data
, size
, &data_buffer_changed
);
1627 if (filename
!= NULL
&& (!strcmp( filename
, "." ) || !strcmp( filename
, ".." )))
1628 filename
= read_first_dent_name( swap_to
^ 1, fd
, second_entry_pos
, NULL
,
1629 data
, size
, &data_buffer_changed
);
1630 if (data_buffer_changed
)
1632 lseek( fd
, next_pos
, SEEK_SET
);
1637 filename
= de
->d_name
;
1639 if (filename
&& (info
= append_entry( buffer
, io
, length
, filename
, NULL
, mask
, class )))
1642 if (io
->u
.Status
== STATUS_BUFFER_OVERFLOW
)
1644 lseek( fd
, old_pos
, SEEK_SET
); /* restore pos to previous entry */
1647 /* check if we still have enough space for the largest possible entry */
1648 if (single_entry
|| io
->Information
+ max_dir_info_size(class) > length
)
1650 if (res
> 0) lseek( fd
, next_pos
, SEEK_SET
); /* set pos to next entry */
1655 /* move on to the next entry */
1656 if (res
> 0) de
= (KERNEL_DIRENT64
*)((char *)de
+ de
->d_reclen
);
1659 res
= getdents64( fd
, data
, size
);
1660 de
= (KERNEL_DIRENT64
*)data
;
1661 de_first_two
= NULL
;
1665 if (last_info
) last_info
->next
= 0;
1666 else io
->u
.Status
= restart_scan
? STATUS_NO_SUCH_FILE
: STATUS_NO_MORE_FILES
;
1669 if (data
!= local_buffer
) RtlFreeHeap( GetProcessHeap(), 0, data
);
1673 #elif defined HAVE_GETDIRENTRIES
1675 #ifdef _DARWIN_FEATURE_64_BIT_INODE
1677 /* Darwin doesn't provide a version of getdirentries with support for 64-bit
1678 * inodes. When 64-bit inodes are enabled, the getdirentries symbol is mapped
1679 * to _getdirentries_is_not_available_when_64_bit_inodes_are_in_effect so that
1680 * we get link errors if we try to use it. We still need getdirentries, but we
1681 * don't need it to support 64-bit inodes. So, we use the legacy getdirentries
1682 * with 32-bit inodes. We have to be careful to use a corresponding dirent
1685 int darwin_legacy_getdirentries(int, char *, int, long *) __asm("_getdirentries");
1686 #define getdirentries darwin_legacy_getdirentries
1688 struct darwin_legacy_dirent
{
1690 __uint16_t d_reclen
;
1693 char d_name
[__DARWIN_MAXNAMLEN
+ 1];
1695 #define dirent darwin_legacy_dirent
1699 /***********************************************************************
1700 * wine_getdirentries
1702 * Wrapper for the BSD getdirentries system call to fix a bug in the
1703 * Mac OS X version. For some file systems (at least Apple Filing
1704 * Protocol a.k.a. AFP), getdirentries resets the file position to 0
1705 * when it's about to return 0 (no more entries). So, a subsequent
1706 * getdirentries call starts over at the beginning again, causing an
1709 static inline int wine_getdirentries(int fd
, char *buf
, int nbytes
, long *basep
)
1711 int res
= getdirentries(fd
, buf
, nbytes
, basep
);
1714 lseek(fd
, *basep
, SEEK_SET
);
1719 static inline int dir_reclen(struct dirent
*de
)
1721 #ifdef HAVE_STRUCT_DIRENT_D_RECLEN
1722 return de
->d_reclen
;
1724 return _DIRENT_RECLEN(de
->d_namlen
);
1728 /***********************************************************************
1729 * read_directory_getdirentries
1731 * Read a directory using the BSD getdirentries system call; helper for NtQueryDirectoryFile.
1733 static int read_directory_getdirentries( int fd
, IO_STATUS_BLOCK
*io
, void *buffer
, ULONG length
,
1734 BOOLEAN single_entry
, const UNICODE_STRING
*mask
,
1735 BOOLEAN restart_scan
, FILE_INFORMATION_CLASS
class )
1738 ULONG_PTR restart_info_pos
= 0;
1739 size_t size
, initial_size
= length
;
1740 int res
, fake_dot_dot
= 1;
1741 char *data
, local_buffer
[8192];
1743 union file_directory_info
*info
, *last_info
= NULL
, *restart_last_info
= NULL
;
1745 size
= initial_size
;
1746 data
= local_buffer
;
1747 if (size
> sizeof(local_buffer
) && !(data
= RtlAllocateHeap( GetProcessHeap(), 0, size
)))
1749 io
->u
.Status
= STATUS_NO_MEMORY
;
1750 return io
->u
.Status
;
1753 if (restart_scan
) lseek( fd
, 0, SEEK_SET
);
1755 io
->u
.Status
= STATUS_SUCCESS
;
1757 /* FIXME: should make sure size is larger than filesystem block size */
1758 res
= wine_getdirentries( fd
, data
, size
, &restart_pos
);
1761 io
->u
.Status
= FILE_GetNtStatus();
1766 de
= (struct dirent
*)data
;
1770 /* check if we got . and .. from getdirentries */
1773 if (!strcmp( de
->d_name
, "." ) && res
> dir_reclen(de
))
1775 struct dirent
*next_de
= (struct dirent
*)(data
+ dir_reclen(de
));
1776 if (!strcmp( next_de
->d_name
, ".." )) fake_dot_dot
= 0;
1779 /* make sure we have enough room for both entries */
1782 const ULONG min_info_size
= dir_info_size( class, 1 ) + dir_info_size( class, 2 );
1783 if (length
< min_info_size
|| single_entry
)
1785 FIXME( "not enough room %u/%u for fake . and .. entries\n", length
, single_entry
);
1792 if ((info
= append_entry( buffer
, io
, length
, ".", NULL
, mask
, class )))
1794 if ((info
= append_entry( buffer
, io
, length
, "..", NULL
, mask
, class )))
1797 restart_last_info
= last_info
;
1798 restart_info_pos
= io
->Information
;
1800 /* check if we still have enough space for the largest possible entry */
1801 if (last_info
&& io
->Information
+ max_dir_info_size(class) > length
)
1803 lseek( fd
, 0, SEEK_SET
); /* reset pos to first entry */
1811 res
-= dir_reclen(de
);
1813 !(fake_dot_dot
&& (!strcmp( de
->d_name
, "." ) || !strcmp( de
->d_name
, ".." ))) &&
1814 ((info
= append_entry( buffer
, io
, length
, de
->d_name
, NULL
, mask
, class ))))
1817 if (io
->u
.Status
== STATUS_BUFFER_OVERFLOW
)
1819 lseek( fd
, (unsigned long)restart_pos
, SEEK_SET
);
1820 if (restart_info_pos
) /* if we have a complete read already, return it */
1822 io
->u
.Status
= STATUS_SUCCESS
;
1823 io
->Information
= restart_info_pos
;
1824 last_info
= restart_last_info
;
1827 /* otherwise restart from the start with a smaller size */
1828 size
= (char *)de
- data
;
1830 io
->Information
= 0;
1834 if (!has_wildcard( mask
)) break;
1835 /* if we have to return but the buffer contains more data, restart with a smaller size */
1836 if (res
> 0 && (single_entry
|| io
->Information
+ max_dir_info_size(class) > length
))
1838 lseek( fd
, (unsigned long)restart_pos
, SEEK_SET
);
1839 size
= (char *)de
+ dir_reclen(de
) - data
;
1840 io
->Information
= restart_info_pos
;
1841 last_info
= restart_last_info
;
1845 /* move on to the next entry */
1848 de
= (struct dirent
*)((char *)de
+ dir_reclen(de
));
1851 if (size
< initial_size
) break; /* already restarted once, give up now */
1852 restart_last_info
= last_info
;
1853 restart_info_pos
= io
->Information
;
1855 res
= wine_getdirentries( fd
, data
, size
, &restart_pos
);
1856 de
= (struct dirent
*)data
;
1859 if (last_info
) last_info
->next
= 0;
1860 else io
->u
.Status
= restart_scan
? STATUS_NO_SUCH_FILE
: STATUS_NO_MORE_FILES
;
1863 if (data
!= local_buffer
) RtlFreeHeap( GetProcessHeap(), 0, data
);
1867 #ifdef _DARWIN_FEATURE_64_BIT_INODE
1868 #undef getdirentries
1872 #endif /* HAVE_GETDIRENTRIES */
1875 /***********************************************************************
1876 * read_directory_readdir
1878 * Read a directory using the POSIX readdir interface; helper for NtQueryDirectoryFile.
1880 static void read_directory_readdir( int fd
, IO_STATUS_BLOCK
*io
, void *buffer
, ULONG length
,
1881 BOOLEAN single_entry
, const UNICODE_STRING
*mask
,
1882 BOOLEAN restart_scan
, FILE_INFORMATION_CLASS
class )
1885 off_t i
, old_pos
= 0;
1887 union file_directory_info
*info
, *last_info
= NULL
;
1889 if (!(dir
= opendir( "." )))
1891 io
->u
.Status
= FILE_GetNtStatus();
1897 old_pos
= lseek( fd
, 0, SEEK_CUR
);
1898 /* skip the right number of entries */
1899 for (i
= 0; i
< old_pos
- 2; i
++)
1901 if (!readdir( dir
))
1904 io
->u
.Status
= STATUS_NO_MORE_FILES
;
1909 io
->u
.Status
= STATUS_SUCCESS
;
1914 info
= append_entry( buffer
, io
, length
, ".", NULL
, mask
, class );
1915 else if (old_pos
== 1)
1916 info
= append_entry( buffer
, io
, length
, "..", NULL
, mask
, class );
1917 else if ((de
= readdir( dir
)))
1919 if (strcmp( de
->d_name
, "." ) && strcmp( de
->d_name
, ".." ))
1920 info
= append_entry( buffer
, io
, length
, de
->d_name
, NULL
, mask
, class );
1930 if (io
->u
.Status
== STATUS_BUFFER_OVERFLOW
)
1932 old_pos
--; /* restore pos to previous entry */
1935 if (single_entry
) break;
1936 /* check if we still have enough space for the largest possible entry */
1937 if (io
->Information
+ max_dir_info_size(class) > length
) break;
1941 lseek( fd
, old_pos
, SEEK_SET
); /* store dir offset as filepos for fd */
1944 if (last_info
) last_info
->next
= 0;
1945 else io
->u
.Status
= restart_scan
? STATUS_NO_SUCH_FILE
: STATUS_NO_MORE_FILES
;
1948 /***********************************************************************
1949 * read_directory_stat
1951 * Read a single file from a directory by determining whether the file
1952 * identified by mask exists using stat.
1954 static int read_directory_stat( int fd
, IO_STATUS_BLOCK
*io
, void *buffer
, ULONG length
,
1955 BOOLEAN single_entry
, const UNICODE_STRING
*mask
,
1956 BOOLEAN restart_scan
, FILE_INFORMATION_CLASS
class )
1958 int unix_len
, ret
, used_default
;
1962 TRACE("trying optimisation for file %s\n", debugstr_us( mask
));
1964 unix_len
= ntdll_wcstoumbs( 0, mask
->Buffer
, mask
->Length
/ sizeof(WCHAR
), NULL
, 0, NULL
, NULL
);
1965 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, unix_len
+ 1)))
1967 io
->u
.Status
= STATUS_NO_MEMORY
;
1970 ret
= ntdll_wcstoumbs( 0, mask
->Buffer
, mask
->Length
/ sizeof(WCHAR
), unix_name
, unix_len
,
1971 NULL
, &used_default
);
1972 if (ret
> 0 && !used_default
)
1977 lseek( fd
, 0, SEEK_SET
);
1979 else if (lseek( fd
, 0, SEEK_CUR
) != 0)
1981 io
->u
.Status
= STATUS_NO_MORE_FILES
;
1986 ret
= stat( unix_name
, &st
);
1989 union file_directory_info
*info
= append_entry( buffer
, io
, length
, unix_name
, NULL
, NULL
, class );
1993 if (io
->u
.Status
!= STATUS_BUFFER_OVERFLOW
) lseek( fd
, 1, SEEK_CUR
);
1995 else io
->u
.Status
= STATUS_NO_MORE_FILES
;
2001 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2003 TRACE("returning %d\n", ret
);
2009 /******************************************************************************
2010 * NtQueryDirectoryFile [NTDLL.@]
2011 * ZwQueryDirectoryFile [NTDLL.@]
2013 NTSTATUS WINAPI
NtQueryDirectoryFile( HANDLE handle
, HANDLE event
,
2014 PIO_APC_ROUTINE apc_routine
, PVOID apc_context
,
2015 PIO_STATUS_BLOCK io
,
2016 PVOID buffer
, ULONG length
,
2017 FILE_INFORMATION_CLASS info_class
,
2018 BOOLEAN single_entry
,
2019 PUNICODE_STRING mask
,
2020 BOOLEAN restart_scan
)
2022 int cwd
, fd
, needs_close
;
2024 TRACE("(%p %p %p %p %p %p 0x%08x 0x%08x 0x%08x %s 0x%08x\n",
2025 handle
, event
, apc_routine
, apc_context
, io
, buffer
,
2026 length
, info_class
, single_entry
, debugstr_us(mask
),
2029 if (event
|| apc_routine
)
2031 FIXME( "Unsupported yet option\n" );
2032 return io
->u
.Status
= STATUS_NOT_IMPLEMENTED
;
2036 case FileDirectoryInformation
:
2037 case FileBothDirectoryInformation
:
2038 case FileFullDirectoryInformation
:
2039 case FileIdBothDirectoryInformation
:
2040 case FileIdFullDirectoryInformation
:
2041 if (length
< dir_info_size( info_class
, 1 )) return io
->u
.Status
= STATUS_INFO_LENGTH_MISMATCH
;
2042 if (!buffer
) return io
->u
.Status
= STATUS_ACCESS_VIOLATION
;
2045 FIXME( "Unsupported file info class %d\n", info_class
);
2046 return io
->u
.Status
= STATUS_NOT_IMPLEMENTED
;
2049 if ((io
->u
.Status
= server_get_unix_fd( handle
, FILE_LIST_DIRECTORY
, &fd
, &needs_close
, NULL
, NULL
)) != STATUS_SUCCESS
)
2050 return io
->u
.Status
;
2052 io
->Information
= 0;
2054 RtlRunOnceExecuteOnce( &init_once
, init_options
, NULL
, NULL
);
2056 RtlEnterCriticalSection( &dir_section
);
2058 cwd
= open( ".", O_RDONLY
);
2059 if (fchdir( fd
) != -1)
2063 curdir
.dev
= st
.st_dev
;
2064 curdir
.ino
= st
.st_ino
;
2065 #ifdef VFAT_IOCTL_READDIR_BOTH
2066 if ((read_directory_vfat( fd
, io
, buffer
, length
, single_entry
,
2067 mask
, restart_scan
, info_class
)) != -1) goto done
;
2069 if (!has_wildcard( mask
) &&
2070 read_directory_stat( fd
, io
, buffer
, length
, single_entry
,
2071 mask
, restart_scan
, info_class
) != -1) goto done
;
2073 if ((read_directory_getdents( fd
, io
, buffer
, length
, single_entry
,
2074 mask
, restart_scan
, info_class
)) != -1) goto done
;
2075 #elif defined HAVE_GETDIRENTRIES
2076 if ((read_directory_getdirentries( fd
, io
, buffer
, length
, single_entry
,
2077 mask
, restart_scan
, info_class
)) != -1) goto done
;
2079 read_directory_readdir( fd
, io
, buffer
, length
, single_entry
, mask
, restart_scan
, info_class
);
2082 if (cwd
== -1 || fchdir( cwd
) == -1) chdir( "/" );
2084 else io
->u
.Status
= FILE_GetNtStatus();
2086 RtlLeaveCriticalSection( &dir_section
);
2088 if (needs_close
) close( fd
);
2089 if (cwd
!= -1) close( cwd
);
2090 TRACE( "=> %x (%ld)\n", io
->u
.Status
, io
->Information
);
2091 return io
->u
.Status
;
2095 /***********************************************************************
2098 * Find a file in a directory the hard way, by doing a case-insensitive search.
2099 * The file found is appended to unix_name at pos.
2100 * There must be at least MAX_DIR_ENTRY_LEN+2 chars available at pos.
2102 static NTSTATUS
find_file_in_dir( char *unix_name
, int pos
, const WCHAR
*name
, int length
,
2103 BOOLEAN check_case
, BOOLEAN
*is_win_dir
)
2105 WCHAR buffer
[MAX_DIR_ENTRY_LEN
];
2107 BOOLEAN spaces
, is_name_8_dot_3
;
2111 int ret
, used_default
;
2113 /* try a shortcut for this directory */
2115 unix_name
[pos
++] = '/';
2116 ret
= ntdll_wcstoumbs( 0, name
, length
, unix_name
+ pos
, MAX_DIR_ENTRY_LEN
,
2117 NULL
, &used_default
);
2118 /* if we used the default char, the Unix name won't round trip properly back to Unicode */
2119 /* so it cannot match the file we are looking for */
2120 if (ret
>= 0 && !used_default
)
2122 unix_name
[pos
+ ret
] = 0;
2123 if (!stat( unix_name
, &st
))
2125 if (is_win_dir
) *is_win_dir
= is_same_file( &windir
, &st
);
2126 return STATUS_SUCCESS
;
2129 if (check_case
) goto not_found
; /* we want an exact match */
2131 if (pos
> 1) unix_name
[pos
- 1] = 0;
2132 else unix_name
[1] = 0; /* keep the initial slash */
2134 /* check if it fits in 8.3 so that we don't look for short names if we won't need them */
2136 str
.Buffer
= (WCHAR
*)name
;
2137 str
.Length
= length
* sizeof(WCHAR
);
2138 str
.MaximumLength
= str
.Length
;
2139 is_name_8_dot_3
= RtlIsNameLegalDOS8Dot3( &str
, NULL
, &spaces
) && !spaces
;
2141 if (!is_name_8_dot_3
&& !get_dir_case_sensitivity( unix_name
)) goto not_found
;
2143 /* now look for it through the directory */
2145 #ifdef VFAT_IOCTL_READDIR_BOTH
2146 if (is_name_8_dot_3
)
2148 int fd
= open( unix_name
, O_RDONLY
| O_DIRECTORY
);
2153 RtlEnterCriticalSection( &dir_section
);
2154 if ((kde
= start_vfat_ioctl( fd
)))
2156 unix_name
[pos
- 1] = '/';
2157 while (kde
[0].d_reclen
)
2159 /* make sure names are null-terminated to work around an x86-64 kernel bug */
2160 size_t len
= min(kde
[0].d_reclen
, sizeof(kde
[0].d_name
) - 1 );
2161 kde
[0].d_name
[len
] = 0;
2162 len
= min(kde
[1].d_reclen
, sizeof(kde
[1].d_name
) - 1 );
2163 kde
[1].d_name
[len
] = 0;
2165 if (kde
[1].d_name
[0])
2167 ret
= ntdll_umbstowcs( 0, kde
[1].d_name
, strlen(kde
[1].d_name
),
2168 buffer
, MAX_DIR_ENTRY_LEN
);
2169 if (ret
== length
&& !memicmpW( buffer
, name
, length
))
2171 strcpy( unix_name
+ pos
, kde
[1].d_name
);
2172 RtlLeaveCriticalSection( &dir_section
);
2177 ret
= ntdll_umbstowcs( 0, kde
[0].d_name
, strlen(kde
[0].d_name
),
2178 buffer
, MAX_DIR_ENTRY_LEN
);
2179 if (ret
== length
&& !memicmpW( buffer
, name
, length
))
2181 strcpy( unix_name
+ pos
,
2182 kde
[1].d_name
[0] ? kde
[1].d_name
: kde
[0].d_name
);
2183 RtlLeaveCriticalSection( &dir_section
);
2187 if (ioctl( fd
, VFAT_IOCTL_READDIR_BOTH
, (long)kde
) == -1)
2189 RtlLeaveCriticalSection( &dir_section
);
2195 RtlLeaveCriticalSection( &dir_section
);
2198 /* fall through to normal handling */
2200 #endif /* VFAT_IOCTL_READDIR_BOTH */
2202 if (!(dir
= opendir( unix_name
)))
2204 if (errno
== ENOENT
) return STATUS_OBJECT_PATH_NOT_FOUND
;
2205 else return FILE_GetNtStatus();
2207 unix_name
[pos
- 1] = '/';
2208 str
.Buffer
= buffer
;
2209 str
.MaximumLength
= sizeof(buffer
);
2210 while ((de
= readdir( dir
)))
2212 ret
= ntdll_umbstowcs( 0, de
->d_name
, strlen(de
->d_name
), buffer
, MAX_DIR_ENTRY_LEN
);
2213 if (ret
== length
&& !memicmpW( buffer
, name
, length
))
2215 strcpy( unix_name
+ pos
, de
->d_name
);
2220 if (!is_name_8_dot_3
) continue;
2222 str
.Length
= ret
* sizeof(WCHAR
);
2223 if (!RtlIsNameLegalDOS8Dot3( &str
, NULL
, &spaces
) || spaces
)
2225 WCHAR short_nameW
[12];
2226 ret
= hash_short_file_name( &str
, short_nameW
);
2227 if (ret
== length
&& !memicmpW( short_nameW
, name
, length
))
2229 strcpy( unix_name
+ pos
, de
->d_name
);
2238 unix_name
[pos
- 1] = 0;
2239 return STATUS_OBJECT_PATH_NOT_FOUND
;
2242 if (is_win_dir
&& !stat( unix_name
, &st
)) *is_win_dir
= is_same_file( &windir
, &st
);
2243 return STATUS_SUCCESS
;
2249 static const WCHAR catrootW
[] = {'s','y','s','t','e','m','3','2','\\','c','a','t','r','o','o','t',0};
2250 static const WCHAR catroot2W
[] = {'s','y','s','t','e','m','3','2','\\','c','a','t','r','o','o','t','2',0};
2251 static const WCHAR driversstoreW
[] = {'s','y','s','t','e','m','3','2','\\','d','r','i','v','e','r','s','s','t','o','r','e',0};
2252 static const WCHAR driversetcW
[] = {'s','y','s','t','e','m','3','2','\\','d','r','i','v','e','r','s','\\','e','t','c',0};
2253 static const WCHAR logfilesW
[] = {'s','y','s','t','e','m','3','2','\\','l','o','g','f','i','l','e','s',0};
2254 static const WCHAR spoolW
[] = {'s','y','s','t','e','m','3','2','\\','s','p','o','o','l',0};
2255 static const WCHAR system32W
[] = {'s','y','s','t','e','m','3','2',0};
2256 static const WCHAR syswow64W
[] = {'s','y','s','w','o','w','6','4',0};
2257 static const WCHAR sysnativeW
[] = {'s','y','s','n','a','t','i','v','e',0};
2258 static const WCHAR regeditW
[] = {'r','e','g','e','d','i','t','.','e','x','e',0};
2259 static const WCHAR wow_regeditW
[] = {'s','y','s','w','o','w','6','4','\\','r','e','g','e','d','i','t','.','e','x','e',0};
2263 const WCHAR
*source
;
2264 const WCHAR
*dos_target
;
2265 const char *unix_target
;
2268 { catrootW
, NULL
, NULL
},
2269 { catroot2W
, NULL
, NULL
},
2270 { driversstoreW
, NULL
, NULL
},
2271 { driversetcW
, NULL
, NULL
},
2272 { logfilesW
, NULL
, NULL
},
2273 { spoolW
, NULL
, NULL
},
2274 { system32W
, syswow64W
, NULL
},
2275 { sysnativeW
, system32W
, NULL
},
2276 { regeditW
, wow_regeditW
, NULL
}
2279 static unsigned int nb_redirects
;
2282 /***********************************************************************
2283 * get_redirect_target
2285 * Find the target unix name for a redirected dir.
2287 static const char *get_redirect_target( const char *windows_dir
, const WCHAR
*name
)
2289 int used_default
, len
, pos
, win_len
= strlen( windows_dir
);
2290 char *unix_name
, *unix_target
= NULL
;
2293 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, win_len
+ MAX_DIR_ENTRY_LEN
+ 2 )))
2295 memcpy( unix_name
, windows_dir
, win_len
);
2300 const WCHAR
*end
, *next
;
2302 for (end
= name
; *end
; end
++) if (IS_SEPARATOR(*end
)) break;
2303 for (next
= end
; *next
; next
++) if (!IS_SEPARATOR(*next
)) break;
2305 status
= find_file_in_dir( unix_name
, pos
, name
, end
- name
, FALSE
, NULL
);
2306 if (status
== STATUS_OBJECT_PATH_NOT_FOUND
&& !*next
) /* not finding last element is ok */
2308 len
= ntdll_wcstoumbs( 0, name
, end
- name
, unix_name
+ pos
+ 1,
2309 MAX_DIR_ENTRY_LEN
- (pos
- win_len
), NULL
, &used_default
);
2310 if (len
> 0 && !used_default
)
2312 unix_name
[pos
] = '/';
2318 if (status
) goto done
;
2319 pos
+= strlen( unix_name
+ pos
);
2323 if ((unix_target
= RtlAllocateHeap( GetProcessHeap(), 0, pos
- win_len
)))
2324 memcpy( unix_target
, unix_name
+ win_len
+ 1, pos
- win_len
);
2327 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2332 /***********************************************************************
2335 static void init_redirects(void)
2337 UNICODE_STRING nt_name
;
2338 ANSI_STRING unix_name
;
2343 if (!RtlDosPathNameToNtPathName_U( user_shared_data
->NtSystemRoot
, &nt_name
, NULL
, NULL
))
2345 ERR( "can't convert %s\n", debugstr_w(user_shared_data
->NtSystemRoot
) );
2348 status
= wine_nt_to_unix_file_name( &nt_name
, &unix_name
, FILE_OPEN_IF
, FALSE
);
2349 RtlFreeUnicodeString( &nt_name
);
2352 ERR( "cannot open %s (%x)\n", debugstr_w(user_shared_data
->NtSystemRoot
), status
);
2355 if (!stat( unix_name
.Buffer
, &st
))
2357 windir
.dev
= st
.st_dev
;
2358 windir
.ino
= st
.st_ino
;
2359 nb_redirects
= sizeof(redirects
) / sizeof(redirects
[0]);
2360 for (i
= 0; i
< nb_redirects
; i
++)
2362 if (!redirects
[i
].dos_target
) continue;
2363 redirects
[i
].unix_target
= get_redirect_target( unix_name
.Buffer
, redirects
[i
].dos_target
);
2364 TRACE( "%s -> %s\n", debugstr_w(redirects
[i
].source
), redirects
[i
].unix_target
);
2367 RtlFreeAnsiString( &unix_name
);
2372 /***********************************************************************
2375 * Check if path matches a redirect name. If yes, return matched length.
2377 static int match_redirect( const WCHAR
*path
, int len
, const WCHAR
*redir
, BOOLEAN check_case
)
2381 while (i
< len
&& *redir
)
2383 if (IS_SEPARATOR(path
[i
]))
2385 if (*redir
++ != '\\') return 0;
2386 while (i
< len
&& IS_SEPARATOR(path
[i
])) i
++;
2387 continue; /* move on to next path component */
2389 else if (check_case
)
2391 if (path
[i
] != *redir
) return 0;
2395 if (tolowerW(path
[i
]) != tolowerW(*redir
)) return 0;
2400 if (*redir
) return 0;
2401 if (i
< len
&& !IS_SEPARATOR(path
[i
])) return 0;
2402 while (i
< len
&& IS_SEPARATOR(path
[i
])) i
++;
2407 /***********************************************************************
2410 * Retrieve the Unix path corresponding to a redirected path if any.
2412 static int get_redirect_path( char *unix_name
, int pos
, const WCHAR
*name
, int length
, BOOLEAN check_case
)
2417 for (i
= 0; i
< nb_redirects
; i
++)
2419 if ((len
= match_redirect( name
, length
, redirects
[i
].source
, check_case
)))
2421 if (!redirects
[i
].unix_target
) break;
2422 unix_name
[pos
++] = '/';
2423 strcpy( unix_name
+ pos
, redirects
[i
].unix_target
);
2432 /* there are no redirects on 64-bit */
2434 static const unsigned int nb_redirects
= 0;
2436 static int get_redirect_path( char *unix_name
, int pos
, const WCHAR
*name
, int length
, BOOLEAN check_case
)
2443 /***********************************************************************
2444 * DIR_init_windows_dir
2446 void DIR_init_windows_dir( const WCHAR
*win
, const WCHAR
*sys
)
2448 /* FIXME: should probably store paths as NT file names */
2450 RtlCreateUnicodeString( &system_dir
, sys
);
2453 if (is_wow64
) init_redirects();
2458 /******************************************************************************
2461 * Get the Unix path of a DOS device.
2463 static NTSTATUS
get_dos_device( const WCHAR
*name
, UINT name_len
, ANSI_STRING
*unix_name_ret
)
2465 const char *config_dir
= wine_get_config_dir();
2467 char *unix_name
, *new_name
, *dev
;
2471 /* make sure the device name is ASCII */
2472 for (i
= 0; i
< name_len
; i
++)
2473 if (name
[i
] <= 32 || name
[i
] >= 127) return STATUS_BAD_DEVICE_TYPE
;
2475 unix_len
= strlen(config_dir
) + sizeof("/dosdevices/") + name_len
+ 1;
2477 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, unix_len
)))
2478 return STATUS_NO_MEMORY
;
2480 strcpy( unix_name
, config_dir
);
2481 strcat( unix_name
, "/dosdevices/" );
2482 dev
= unix_name
+ strlen(unix_name
);
2484 for (i
= 0; i
< name_len
; i
++) dev
[i
] = (char)tolowerW(name
[i
]);
2487 /* special case for drive devices */
2488 if (name_len
== 2 && dev
[1] == ':')
2496 if (!stat( unix_name
, &st
))
2498 TRACE( "%s -> %s\n", debugstr_wn(name
,name_len
), debugstr_a(unix_name
) );
2499 unix_name_ret
->Buffer
= unix_name
;
2500 unix_name_ret
->Length
= strlen(unix_name
);
2501 unix_name_ret
->MaximumLength
= unix_len
;
2502 return STATUS_SUCCESS
;
2506 /* now try some defaults for it */
2507 if (!strcmp( dev
, "aux" ))
2509 strcpy( dev
, "com1" );
2512 if (!strcmp( dev
, "prn" ))
2514 strcpy( dev
, "lpt1" );
2517 if (!strcmp( dev
, "nul" ))
2519 strcpy( unix_name
, "/dev/null" );
2520 dev
= NULL
; /* last try */
2525 if (dev
[1] == ':' && dev
[2] == ':') /* drive device */
2527 dev
[2] = 0; /* remove last ':' to get the drive mount point symlink */
2528 new_name
= get_default_drive_device( unix_name
);
2530 else if (!strncmp( dev
, "com", 3 )) new_name
= get_default_com_device( atoi(dev
+ 3 ));
2531 else if (!strncmp( dev
, "lpt", 3 )) new_name
= get_default_lpt_device( atoi(dev
+ 3 ));
2533 if (!new_name
) break;
2535 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2536 unix_name
= new_name
;
2537 unix_len
= strlen(unix_name
) + 1;
2538 dev
= NULL
; /* last try */
2540 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2541 return STATUS_BAD_DEVICE_TYPE
;
2545 /* return the length of the DOS namespace prefix if any */
2546 static inline int get_dos_prefix_len( const UNICODE_STRING
*name
)
2548 static const WCHAR nt_prefixW
[] = {'\\','?','?','\\'};
2549 static const WCHAR dosdev_prefixW
[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\'};
2551 if (name
->Length
> sizeof(nt_prefixW
) &&
2552 !memcmp( name
->Buffer
, nt_prefixW
, sizeof(nt_prefixW
) ))
2553 return sizeof(nt_prefixW
) / sizeof(WCHAR
);
2555 if (name
->Length
> sizeof(dosdev_prefixW
) &&
2556 !memicmpW( name
->Buffer
, dosdev_prefixW
, sizeof(dosdev_prefixW
)/sizeof(WCHAR
) ))
2557 return sizeof(dosdev_prefixW
) / sizeof(WCHAR
);
2563 /******************************************************************************
2566 * Recursively search directories from the dir queue for a given inode.
2568 static NTSTATUS
find_file_id( ANSI_STRING
*unix_name
, ULONGLONG file_id
, dev_t dev
)
2576 while (!(status
= next_dir_in_queue( unix_name
->Buffer
)))
2578 if (!(dir
= opendir( unix_name
->Buffer
))) continue;
2579 TRACE( "searching %s for %s\n", unix_name
->Buffer
, wine_dbgstr_longlong(file_id
) );
2580 pos
= strlen( unix_name
->Buffer
);
2581 if (pos
+ MAX_DIR_ENTRY_LEN
>= unix_name
->MaximumLength
/sizeof(WCHAR
))
2583 char *new = RtlReAllocateHeap( GetProcessHeap(), 0, unix_name
->Buffer
,
2584 unix_name
->MaximumLength
* 2 );
2588 return STATUS_NO_MEMORY
;
2590 unix_name
->MaximumLength
*= 2;
2591 unix_name
->Buffer
= new;
2593 unix_name
->Buffer
[pos
++] = '/';
2594 while ((de
= readdir( dir
)))
2596 if (!strcmp( de
->d_name
, "." ) || !strcmp( de
->d_name
, ".." )) continue;
2597 strcpy( unix_name
->Buffer
+ pos
, de
->d_name
);
2598 if (lstat( unix_name
->Buffer
, &st
) == -1) continue;
2599 if (st
.st_dev
!= dev
) continue;
2600 if (st
.st_ino
== file_id
)
2603 return STATUS_SUCCESS
;
2605 if (!S_ISDIR( st
.st_mode
)) continue;
2606 if ((status
= add_dir_to_queue( unix_name
->Buffer
)) != STATUS_SUCCESS
)
2618 /******************************************************************************
2619 * file_id_to_unix_file_name
2621 * Lookup a file from its file id instead of its name.
2623 NTSTATUS
file_id_to_unix_file_name( const OBJECT_ATTRIBUTES
*attr
, ANSI_STRING
*unix_name
)
2625 enum server_fd_type type
;
2626 int old_cwd
, root_fd
, needs_close
;
2629 struct stat st
, root_st
;
2631 if (attr
->ObjectName
->Length
!= sizeof(ULONGLONG
)) return STATUS_OBJECT_PATH_SYNTAX_BAD
;
2632 if (!attr
->RootDirectory
) return STATUS_INVALID_PARAMETER
;
2633 memcpy( &file_id
, attr
->ObjectName
->Buffer
, sizeof(file_id
) );
2635 unix_name
->MaximumLength
= 2 * MAX_DIR_ENTRY_LEN
+ 4;
2636 if (!(unix_name
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, unix_name
->MaximumLength
)))
2637 return STATUS_NO_MEMORY
;
2638 strcpy( unix_name
->Buffer
, "." );
2640 if ((status
= server_get_unix_fd( attr
->RootDirectory
, 0, &root_fd
, &needs_close
, &type
, NULL
)))
2643 if (type
!= FD_TYPE_DIR
)
2645 status
= STATUS_OBJECT_TYPE_MISMATCH
;
2649 fstat( root_fd
, &root_st
);
2650 if (root_st
.st_ino
== file_id
) /* shortcut for "." */
2652 status
= STATUS_SUCCESS
;
2656 RtlEnterCriticalSection( &dir_section
);
2657 if ((old_cwd
= open( ".", O_RDONLY
)) != -1 && fchdir( root_fd
) != -1)
2659 /* shortcut for ".." */
2660 if (!stat( "..", &st
) && st
.st_dev
== root_st
.st_dev
&& st
.st_ino
== file_id
)
2662 strcpy( unix_name
->Buffer
, ".." );
2663 status
= STATUS_SUCCESS
;
2667 status
= add_dir_to_queue( "." );
2669 status
= find_file_id( unix_name
, file_id
, root_st
.st_dev
);
2670 if (!status
) /* get rid of "./" prefix */
2671 memmove( unix_name
->Buffer
, unix_name
->Buffer
+ 2, strlen(unix_name
->Buffer
) - 1 );
2674 if (fchdir( old_cwd
) == -1) chdir( "/" );
2676 else status
= FILE_GetNtStatus();
2677 RtlLeaveCriticalSection( &dir_section
);
2678 if (old_cwd
!= -1) close( old_cwd
);
2681 if (status
== STATUS_SUCCESS
)
2683 TRACE( "%s -> %s\n", wine_dbgstr_longlong(file_id
), debugstr_a(unix_name
->Buffer
) );
2684 unix_name
->Length
= strlen( unix_name
->Buffer
);
2688 TRACE( "%s not found in dir %p\n", wine_dbgstr_longlong(file_id
), attr
->RootDirectory
);
2689 RtlFreeHeap( GetProcessHeap(), 0, unix_name
->Buffer
);
2691 if (needs_close
) close( root_fd
);
2696 /******************************************************************************
2699 * Helper for nt_to_unix_file_name
2701 static NTSTATUS
lookup_unix_name( const WCHAR
*name
, int name_len
, char **buffer
, int unix_len
, int pos
,
2702 UINT disposition
, BOOLEAN check_case
)
2705 int ret
, used_default
, len
;
2707 char *unix_name
= *buffer
;
2708 const BOOL redirect
= nb_redirects
&& ntdll_get_thread_data()->wow64_redir
;
2710 /* try a shortcut first */
2712 ret
= ntdll_wcstoumbs( 0, name
, name_len
, unix_name
+ pos
, unix_len
- pos
- 1,
2713 NULL
, &used_default
);
2715 while (name_len
&& IS_SEPARATOR(*name
))
2721 if (ret
>= 0 && !used_default
) /* if we used the default char the name didn't convert properly */
2724 unix_name
[pos
+ ret
] = 0;
2725 for (p
= unix_name
+ pos
; *p
; p
++) if (*p
== '\\') *p
= '/';
2726 if (!redirect
|| (!strstr( unix_name
, "/windows/") && strncmp( unix_name
, "windows/", 8 )))
2728 if (!stat( unix_name
, &st
))
2730 /* creation fails with STATUS_ACCESS_DENIED for the root of the drive */
2731 if (disposition
== FILE_CREATE
)
2732 return name_len
? STATUS_OBJECT_NAME_COLLISION
: STATUS_ACCESS_DENIED
;
2733 return STATUS_SUCCESS
;
2738 if (!name_len
) /* empty name -> drive root doesn't exist */
2739 return STATUS_OBJECT_PATH_NOT_FOUND
;
2740 if (check_case
&& !redirect
&& (disposition
== FILE_OPEN
|| disposition
== FILE_OVERWRITE
))
2741 return STATUS_OBJECT_NAME_NOT_FOUND
;
2743 /* now do it component by component */
2747 const WCHAR
*end
, *next
;
2748 BOOLEAN is_win_dir
= FALSE
;
2751 while (end
< name
+ name_len
&& !IS_SEPARATOR(*end
)) end
++;
2753 while (next
< name
+ name_len
&& IS_SEPARATOR(*next
)) next
++;
2754 name_len
-= next
- name
;
2756 /* grow the buffer if needed */
2758 if (unix_len
- pos
< MAX_DIR_ENTRY_LEN
+ 2)
2761 unix_len
+= 2 * MAX_DIR_ENTRY_LEN
;
2762 if (!(new_name
= RtlReAllocateHeap( GetProcessHeap(), 0, unix_name
, unix_len
)))
2763 return STATUS_NO_MEMORY
;
2764 unix_name
= *buffer
= new_name
;
2767 status
= find_file_in_dir( unix_name
, pos
, name
, end
- name
,
2768 check_case
, redirect
? &is_win_dir
: NULL
);
2770 /* if this is the last element, not finding it is not necessarily fatal */
2773 if (status
== STATUS_OBJECT_PATH_NOT_FOUND
)
2775 status
= STATUS_OBJECT_NAME_NOT_FOUND
;
2776 if (disposition
!= FILE_OPEN
&& disposition
!= FILE_OVERWRITE
)
2778 ret
= ntdll_wcstoumbs( 0, name
, end
- name
, unix_name
+ pos
+ 1,
2779 MAX_DIR_ENTRY_LEN
, NULL
, &used_default
);
2780 if (ret
> 0 && !used_default
)
2782 unix_name
[pos
] = '/';
2783 unix_name
[pos
+ 1 + ret
] = 0;
2784 status
= STATUS_NO_SUCH_FILE
;
2789 else if (status
== STATUS_SUCCESS
&& disposition
== FILE_CREATE
)
2791 status
= STATUS_OBJECT_NAME_COLLISION
;
2795 if (status
!= STATUS_SUCCESS
) break;
2797 pos
+= strlen( unix_name
+ pos
);
2800 if (is_win_dir
&& (len
= get_redirect_path( unix_name
, pos
, name
, name_len
, check_case
)))
2804 pos
+= strlen( unix_name
+ pos
);
2805 TRACE( "redirecting -> %s + %s\n", debugstr_a(unix_name
), debugstr_w(name
) );
2813 /******************************************************************************
2814 * nt_to_unix_file_name_attr
2816 NTSTATUS
nt_to_unix_file_name_attr( const OBJECT_ATTRIBUTES
*attr
, ANSI_STRING
*unix_name_ret
,
2819 static const WCHAR invalid_charsW
[] = { INVALID_NT_CHARS
, 0 };
2820 enum server_fd_type type
;
2821 int old_cwd
, root_fd
, needs_close
;
2822 const WCHAR
*name
, *p
;
2824 int name_len
, unix_len
;
2826 BOOLEAN check_case
= !(attr
->Attributes
& OBJ_CASE_INSENSITIVE
);
2828 if (!attr
->RootDirectory
) /* without root dir fall back to normal lookup */
2829 return wine_nt_to_unix_file_name( attr
->ObjectName
, unix_name_ret
, disposition
, check_case
);
2831 name
= attr
->ObjectName
->Buffer
;
2832 name_len
= attr
->ObjectName
->Length
/ sizeof(WCHAR
);
2834 if (name_len
&& IS_SEPARATOR(name
[0])) return STATUS_INVALID_PARAMETER
;
2836 /* check for invalid characters */
2837 for (p
= name
; p
< name
+ name_len
; p
++)
2838 if (*p
< 32 || strchrW( invalid_charsW
, *p
)) return STATUS_OBJECT_NAME_INVALID
;
2840 unix_len
= ntdll_wcstoumbs( 0, name
, name_len
, NULL
, 0, NULL
, NULL
);
2841 unix_len
+= MAX_DIR_ENTRY_LEN
+ 3;
2842 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, unix_len
)))
2843 return STATUS_NO_MEMORY
;
2846 if (!(status
= server_get_unix_fd( attr
->RootDirectory
, 0, &root_fd
, &needs_close
, &type
, NULL
)))
2848 if (type
!= FD_TYPE_DIR
)
2850 if (needs_close
) close( root_fd
);
2851 status
= STATUS_BAD_DEVICE_TYPE
;
2855 RtlEnterCriticalSection( &dir_section
);
2856 if ((old_cwd
= open( ".", O_RDONLY
)) != -1 && fchdir( root_fd
) != -1)
2858 status
= lookup_unix_name( name
, name_len
, &unix_name
, unix_len
, 1,
2859 disposition
, check_case
);
2860 if (fchdir( old_cwd
) == -1) chdir( "/" );
2862 else status
= FILE_GetNtStatus();
2863 RtlLeaveCriticalSection( &dir_section
);
2864 if (old_cwd
!= -1) close( old_cwd
);
2865 if (needs_close
) close( root_fd
);
2868 else if (status
== STATUS_OBJECT_TYPE_MISMATCH
) status
= STATUS_BAD_DEVICE_TYPE
;
2870 if (status
== STATUS_SUCCESS
|| status
== STATUS_NO_SUCH_FILE
)
2872 TRACE( "%s -> %s\n", debugstr_us(attr
->ObjectName
), debugstr_a(unix_name
) );
2873 unix_name_ret
->Buffer
= unix_name
;
2874 unix_name_ret
->Length
= strlen(unix_name
);
2875 unix_name_ret
->MaximumLength
= unix_len
;
2879 TRACE( "%s not found in %s\n", debugstr_w(name
), unix_name
);
2880 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2886 /******************************************************************************
2887 * wine_nt_to_unix_file_name (NTDLL.@) Not a Windows API
2889 * Convert a file name from NT namespace to Unix namespace.
2891 * If disposition is not FILE_OPEN or FILE_OVERWRITE, the last path
2892 * element doesn't have to exist; in that case STATUS_NO_SUCH_FILE is
2893 * returned, but the unix name is still filled in properly.
2895 NTSTATUS CDECL
wine_nt_to_unix_file_name( const UNICODE_STRING
*nameW
, ANSI_STRING
*unix_name_ret
,
2896 UINT disposition
, BOOLEAN check_case
)
2898 static const WCHAR unixW
[] = {'u','n','i','x'};
2899 static const WCHAR invalid_charsW
[] = { INVALID_NT_CHARS
, 0 };
2901 NTSTATUS status
= STATUS_SUCCESS
;
2902 const char *config_dir
= wine_get_config_dir();
2903 const WCHAR
*name
, *p
;
2906 int pos
, ret
, name_len
, unix_len
, prefix_len
, used_default
;
2907 WCHAR prefix
[MAX_DIR_ENTRY_LEN
];
2908 BOOLEAN is_unix
= FALSE
;
2910 name
= nameW
->Buffer
;
2911 name_len
= nameW
->Length
/ sizeof(WCHAR
);
2913 if (!name_len
|| !IS_SEPARATOR(name
[0])) return STATUS_OBJECT_PATH_SYNTAX_BAD
;
2915 if (!(pos
= get_dos_prefix_len( nameW
)))
2916 return STATUS_BAD_DEVICE_TYPE
; /* no DOS prefix, assume NT native name */
2921 /* check for sub-directory */
2922 for (pos
= 0; pos
< name_len
; pos
++)
2924 if (IS_SEPARATOR(name
[pos
])) break;
2925 if (name
[pos
] < 32 || strchrW( invalid_charsW
, name
[pos
] ))
2926 return STATUS_OBJECT_NAME_INVALID
;
2928 if (pos
> MAX_DIR_ENTRY_LEN
)
2929 return STATUS_OBJECT_NAME_INVALID
;
2931 if (pos
== name_len
) /* no subdir, plain DOS device */
2932 return get_dos_device( name
, name_len
, unix_name_ret
);
2934 for (prefix_len
= 0; prefix_len
< pos
; prefix_len
++)
2935 prefix
[prefix_len
] = tolowerW(name
[prefix_len
]);
2938 name_len
-= prefix_len
;
2940 /* check for invalid characters (all chars except 0 are valid for unix) */
2941 is_unix
= (prefix_len
== 4 && !memcmp( prefix
, unixW
, sizeof(unixW
) ));
2944 for (p
= name
; p
< name
+ name_len
; p
++)
2945 if (!*p
) return STATUS_OBJECT_NAME_INVALID
;
2950 for (p
= name
; p
< name
+ name_len
; p
++)
2951 if (*p
< 32 || strchrW( invalid_charsW
, *p
)) return STATUS_OBJECT_NAME_INVALID
;
2954 unix_len
= ntdll_wcstoumbs( 0, prefix
, prefix_len
, NULL
, 0, NULL
, NULL
);
2955 unix_len
+= ntdll_wcstoumbs( 0, name
, name_len
, NULL
, 0, NULL
, NULL
);
2956 unix_len
+= MAX_DIR_ENTRY_LEN
+ 3;
2957 unix_len
+= strlen(config_dir
) + sizeof("/dosdevices/");
2958 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, unix_len
)))
2959 return STATUS_NO_MEMORY
;
2960 strcpy( unix_name
, config_dir
);
2961 strcat( unix_name
, "/dosdevices/" );
2962 pos
= strlen(unix_name
);
2964 ret
= ntdll_wcstoumbs( 0, prefix
, prefix_len
, unix_name
+ pos
, unix_len
- pos
- 1,
2965 NULL
, &used_default
);
2966 if (!ret
|| used_default
)
2968 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2969 return STATUS_OBJECT_NAME_INVALID
;
2973 /* check if prefix exists (except for DOS drives to avoid extra stat calls) */
2975 if (prefix_len
!= 2 || prefix
[1] != ':')
2978 if (lstat( unix_name
, &st
) == -1 && errno
== ENOENT
)
2982 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2983 return STATUS_BAD_DEVICE_TYPE
;
2985 pos
= 0; /* fall back to unix root */
2989 status
= lookup_unix_name( name
, name_len
, &unix_name
, unix_len
, pos
, disposition
, check_case
);
2990 if (status
== STATUS_SUCCESS
|| status
== STATUS_NO_SUCH_FILE
)
2992 TRACE( "%s -> %s\n", debugstr_us(nameW
), debugstr_a(unix_name
) );
2993 unix_name_ret
->Buffer
= unix_name
;
2994 unix_name_ret
->Length
= strlen(unix_name
);
2995 unix_name_ret
->MaximumLength
= unix_len
;
2999 TRACE( "%s not found in %s\n", debugstr_w(name
), unix_name
);
3000 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
3006 /******************************************************************
3007 * RtlWow64EnableFsRedirection (NTDLL.@)
3009 NTSTATUS WINAPI
RtlWow64EnableFsRedirection( BOOLEAN enable
)
3011 if (!is_wow64
) return STATUS_NOT_IMPLEMENTED
;
3012 ntdll_get_thread_data()->wow64_redir
= enable
;
3013 return STATUS_SUCCESS
;
3017 /******************************************************************
3018 * RtlWow64EnableFsRedirectionEx (NTDLL.@)
3020 NTSTATUS WINAPI
RtlWow64EnableFsRedirectionEx( ULONG disable
, ULONG
*old_value
)
3022 if (!is_wow64
) return STATUS_NOT_IMPLEMENTED
;
3023 if (((ULONG_PTR
)old_value
>> 16) == 0) return STATUS_ACCESS_VIOLATION
;
3025 *old_value
= !ntdll_get_thread_data()->wow64_redir
;
3026 ntdll_get_thread_data()->wow64_redir
= !disable
;
3027 return STATUS_SUCCESS
;
3031 /******************************************************************
3032 * RtlDoesFileExists_U (NTDLL.@)
3034 BOOLEAN WINAPI
RtlDoesFileExists_U(LPCWSTR file_name
)
3036 UNICODE_STRING nt_name
;
3037 FILE_BASIC_INFORMATION basic_info
;
3038 OBJECT_ATTRIBUTES attr
;
3041 if (!RtlDosPathNameToNtPathName_U( file_name
, &nt_name
, NULL
, NULL
)) return FALSE
;
3043 attr
.Length
= sizeof(attr
);
3044 attr
.RootDirectory
= 0;
3045 attr
.ObjectName
= &nt_name
;
3046 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
3047 attr
.SecurityDescriptor
= NULL
;
3048 attr
.SecurityQualityOfService
= NULL
;
3050 ret
= NtQueryAttributesFile(&attr
, &basic_info
) == STATUS_SUCCESS
;
3052 RtlFreeUnicodeString( &nt_name
);
3057 /***********************************************************************
3058 * DIR_unmount_device
3060 * Unmount the specified device.
3062 NTSTATUS
DIR_unmount_device( HANDLE handle
)
3065 int unix_fd
, needs_close
;
3067 if (!(status
= server_get_unix_fd( handle
, 0, &unix_fd
, &needs_close
, NULL
, NULL
)))
3070 char *mount_point
= NULL
;
3072 if (fstat( unix_fd
, &st
) == -1 || !is_valid_mounted_device( &st
))
3073 status
= STATUS_INVALID_PARAMETER
;
3076 if ((mount_point
= get_device_mount_point( st
.st_rdev
)))
3079 static const char umount
[] = "diskutil unmount >/dev/null 2>&1 ";
3081 static const char umount
[] = "umount >/dev/null 2>&1 ";
3083 char *cmd
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(mount_point
)+sizeof(umount
));
3086 strcpy( cmd
, umount
);
3087 strcat( cmd
, mount_point
);
3089 RtlFreeHeap( GetProcessHeap(), 0, cmd
);
3091 /* umount will fail to release the loop device since we still have
3092 a handle to it, so we release it here */
3093 if (major(st
.st_rdev
) == LOOP_MAJOR
) ioctl( unix_fd
, 0x4c01 /*LOOP_CLR_FD*/, 0 );
3096 RtlFreeHeap( GetProcessHeap(), 0, mount_point
);
3099 if (needs_close
) close( unix_fd
);
3105 /******************************************************************************
3108 * Retrieve the Unix name of the current directory; helper for wine_unix_to_nt_file_name.
3109 * Returned value must be freed by caller.
3111 NTSTATUS
DIR_get_unix_cwd( char **cwd
)
3113 int old_cwd
, unix_fd
, needs_close
;
3118 RtlAcquirePebLock();
3120 if (NtCurrentTeb()->Tib
.SubSystemTib
) /* FIXME: hack */
3121 curdir
= &((WIN16_SUBSYSTEM_TIB
*)NtCurrentTeb()->Tib
.SubSystemTib
)->curdir
;
3123 curdir
= &NtCurrentTeb()->Peb
->ProcessParameters
->CurrentDirectory
;
3125 if (!(handle
= curdir
->Handle
))
3127 UNICODE_STRING dirW
;
3128 OBJECT_ATTRIBUTES attr
;
3131 if (!RtlDosPathNameToNtPathName_U( curdir
->DosPath
.Buffer
, &dirW
, NULL
, NULL
))
3133 status
= STATUS_OBJECT_NAME_INVALID
;
3136 attr
.Length
= sizeof(attr
);
3137 attr
.RootDirectory
= 0;
3138 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
3139 attr
.ObjectName
= &dirW
;
3140 attr
.SecurityDescriptor
= NULL
;
3141 attr
.SecurityQualityOfService
= NULL
;
3143 status
= NtOpenFile( &handle
, 0, &attr
, &io
, 0,
3144 FILE_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
);
3145 RtlFreeUnicodeString( &dirW
);
3146 if (status
!= STATUS_SUCCESS
) goto done
;
3149 if ((status
= server_get_unix_fd( handle
, 0, &unix_fd
, &needs_close
, NULL
, NULL
)) == STATUS_SUCCESS
)
3151 RtlEnterCriticalSection( &dir_section
);
3153 if ((old_cwd
= open(".", O_RDONLY
)) != -1 && fchdir( unix_fd
) != -1)
3155 unsigned int size
= 512;
3159 if (!(*cwd
= RtlAllocateHeap( GetProcessHeap(), 0, size
)))
3161 status
= STATUS_NO_MEMORY
;
3164 if (getcwd( *cwd
, size
)) break;
3165 RtlFreeHeap( GetProcessHeap(), 0, *cwd
);
3166 if (errno
!= ERANGE
)
3168 status
= STATUS_OBJECT_PATH_INVALID
;
3173 if (fchdir( old_cwd
) == -1) chdir( "/" );
3175 else status
= FILE_GetNtStatus();
3177 RtlLeaveCriticalSection( &dir_section
);
3178 if (old_cwd
!= -1) close( old_cwd
);
3179 if (needs_close
) close( unix_fd
);
3181 if (!curdir
->Handle
) NtClose( handle
);
3184 RtlReleasePebLock();
3188 struct read_changes_info
3193 PIO_APC_ROUTINE apc
;
3197 /* callback for ioctl user APC */
3198 static void WINAPI
read_changes_user_apc( void *arg
, IO_STATUS_BLOCK
*io
, ULONG reserved
)
3200 struct read_changes_info
*info
= arg
;
3201 if (info
->apc
) info
->apc( info
->apc_arg
, io
, reserved
);
3202 RtlFreeHeap( GetProcessHeap(), 0, info
);
3205 static NTSTATUS
read_changes_apc( void *user
, PIO_STATUS_BLOCK iosb
, NTSTATUS status
, void **apc
)
3207 struct read_changes_info
*info
= user
;
3208 char data
[PATH_MAX
];
3212 SERVER_START_REQ( read_change
)
3214 req
->handle
= wine_server_obj_handle( info
->FileHandle
);
3215 wine_server_set_reply( req
, data
, PATH_MAX
);
3216 ret
= wine_server_call( req
);
3217 size
= wine_server_reply_size( reply
);
3221 if (ret
== STATUS_SUCCESS
&& info
->Buffer
)
3223 PFILE_NOTIFY_INFORMATION pfni
= info
->Buffer
;
3224 int i
, left
= info
->BufferSize
;
3225 DWORD
*last_entry_offset
= NULL
;
3226 struct filesystem_event
*event
= (struct filesystem_event
*)data
;
3228 while (size
&& left
>= sizeof(*pfni
))
3230 /* convert to an NT style path */
3231 for (i
=0; i
<event
->len
; i
++)
3232 if (event
->name
[i
] == '/')
3233 event
->name
[i
] = '\\';
3235 pfni
->Action
= event
->action
;
3236 pfni
->FileNameLength
= ntdll_umbstowcs( 0, event
->name
, event
->len
, pfni
->FileName
,
3237 (left
- offsetof(FILE_NOTIFY_INFORMATION
, FileName
)) / sizeof(WCHAR
));
3238 last_entry_offset
= &pfni
->NextEntryOffset
;
3240 if(pfni
->FileNameLength
== -1 || pfni
->FileNameLength
== -2)
3243 i
= offsetof(FILE_NOTIFY_INFORMATION
, FileName
[pfni
->FileNameLength
]);
3244 pfni
->FileNameLength
*= sizeof(WCHAR
);
3245 pfni
->NextEntryOffset
= i
;
3246 pfni
= (FILE_NOTIFY_INFORMATION
*)((char*)pfni
+ i
);
3249 i
= (offsetof(struct filesystem_event
, name
[event
->len
])
3250 + sizeof(int)-1) / sizeof(int) * sizeof(int);
3251 event
= (struct filesystem_event
*)((char*)event
+ i
);
3257 ret
= STATUS_NOTIFY_ENUM_DIR
;
3262 *last_entry_offset
= 0;
3263 size
= info
->BufferSize
- left
;
3268 ret
= STATUS_NOTIFY_ENUM_DIR
;
3272 iosb
->u
.Status
= ret
;
3273 iosb
->Information
= size
;
3274 *apc
= read_changes_user_apc
;
3278 #define FILE_NOTIFY_ALL ( \
3279 FILE_NOTIFY_CHANGE_FILE_NAME | \
3280 FILE_NOTIFY_CHANGE_DIR_NAME | \
3281 FILE_NOTIFY_CHANGE_ATTRIBUTES | \
3282 FILE_NOTIFY_CHANGE_SIZE | \
3283 FILE_NOTIFY_CHANGE_LAST_WRITE | \
3284 FILE_NOTIFY_CHANGE_LAST_ACCESS | \
3285 FILE_NOTIFY_CHANGE_CREATION | \
3286 FILE_NOTIFY_CHANGE_SECURITY )
3288 /******************************************************************************
3289 * NtNotifyChangeDirectoryFile [NTDLL.@]
3292 NtNotifyChangeDirectoryFile( HANDLE FileHandle
, HANDLE Event
,
3293 PIO_APC_ROUTINE ApcRoutine
, PVOID ApcContext
,
3294 PIO_STATUS_BLOCK IoStatusBlock
, PVOID Buffer
,
3295 ULONG BufferSize
, ULONG CompletionFilter
, BOOLEAN WatchTree
)
3297 struct read_changes_info
*info
;
3299 ULONG_PTR cvalue
= ApcRoutine
? 0 : (ULONG_PTR
)ApcContext
;
3301 TRACE("%p %p %p %p %p %p %u %u %d\n",
3302 FileHandle
, Event
, ApcRoutine
, ApcContext
, IoStatusBlock
,
3303 Buffer
, BufferSize
, CompletionFilter
, WatchTree
);
3306 return STATUS_ACCESS_VIOLATION
;
3308 if (CompletionFilter
== 0 || (CompletionFilter
& ~FILE_NOTIFY_ALL
))
3309 return STATUS_INVALID_PARAMETER
;
3311 info
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof *info
);
3313 return STATUS_NO_MEMORY
;
3315 info
->FileHandle
= FileHandle
;
3316 info
->Buffer
= Buffer
;
3317 info
->BufferSize
= BufferSize
;
3318 info
->apc
= ApcRoutine
;
3319 info
->apc_arg
= ApcContext
;
3321 SERVER_START_REQ( read_directory_changes
)
3323 req
->filter
= CompletionFilter
;
3324 req
->want_data
= (Buffer
!= NULL
);
3325 req
->subtree
= WatchTree
;
3326 req
->async
.handle
= wine_server_obj_handle( FileHandle
);
3327 req
->async
.callback
= wine_server_client_ptr( read_changes_apc
);
3328 req
->async
.iosb
= wine_server_client_ptr( IoStatusBlock
);
3329 req
->async
.arg
= wine_server_client_ptr( info
);
3330 req
->async
.event
= wine_server_obj_handle( Event
);
3331 req
->async
.cvalue
= cvalue
;
3332 status
= wine_server_call( req
);
3336 if (status
!= STATUS_PENDING
)
3337 RtlFreeHeap( GetProcessHeap(), 0, info
);