2 * NTDLL directory functions
4 * Copyright 1993 Erik Bos
5 * Copyright 2003 Eric Pouech
6 * Copyright 1996, 2004 Alexandre Julliard
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/port.h"
27 #include <sys/types.h>
41 #ifdef HAVE_SYS_STAT_H
42 # include <sys/stat.h>
44 #ifdef HAVE_SYS_SYSCALL_H
45 # include <sys/syscall.h>
47 #ifdef HAVE_SYS_ATTR_H
50 #ifdef HAVE_SYS_IOCTL_H
51 #include <sys/ioctl.h>
53 #ifdef HAVE_LINUX_IOCTL_H
54 #include <linux/ioctl.h>
56 #ifdef HAVE_LINUX_MAJOR_H
57 # include <linux/major.h>
59 #ifdef HAVE_SYS_PARAM_H
60 #include <sys/param.h>
62 #ifdef HAVE_SYS_MOUNT_H
63 #include <sys/mount.h>
65 #ifdef HAVE_SYS_STATFS_H
66 #include <sys/statfs.h>
73 #define NONAMELESSUNION
74 #define NONAMELESSSTRUCT
76 #define WIN32_NO_STATUS
81 #include "ntdll_misc.h"
82 #include "wine/unicode.h"
83 #include "wine/server.h"
84 #include "wine/list.h"
85 #include "wine/library.h"
86 #include "wine/debug.h"
88 WINE_DEFAULT_DEBUG_CHANNEL(file
);
91 #undef VFAT_IOCTL_READDIR_BOTH
96 /* We want the real kernel dirent structure, not the libc one */
101 unsigned short d_reclen
;
105 /* Define the VFAT ioctl to get both short and long file names */
106 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, KERNEL_DIRENT [2] )
109 # define O_DIRECTORY 0200000 /* must be directory */
112 #ifdef SYS_getdents64
117 unsigned short d_reclen
;
118 unsigned char d_type
;
122 static inline int getdents64( int fd
, char *de
, unsigned int size
)
124 return syscall( SYS_getdents64
, fd
, de
, size
);
131 #define IS_OPTION_TRUE(ch) ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
132 #define IS_SEPARATOR(ch) ((ch) == '\\' || (ch) == '/')
134 #define INVALID_NT_CHARS '*','?','<','>','|','"'
135 #define INVALID_DOS_CHARS INVALID_NT_CHARS,'+','=',',',';','[',']',' ','\345'
137 #define MAX_DIR_ENTRY_LEN 255 /* max length of a directory entry in chars */
139 #define MAX_IGNORED_FILES 4
147 static struct file_identity ignored_files
[MAX_IGNORED_FILES
];
148 static int ignored_files_count
;
150 union file_directory_info
153 FILE_DIRECTORY_INFORMATION dir
;
154 FILE_BOTH_DIRECTORY_INFORMATION both
;
155 FILE_FULL_DIRECTORY_INFORMATION full
;
156 FILE_ID_BOTH_DIRECTORY_INFORMATION id_both
;
157 FILE_ID_FULL_DIRECTORY_INFORMATION id_full
;
160 static int show_dot_files
= -1;
162 /* at some point we may want to allow Winelib apps to set this */
163 static const int is_case_sensitive
= FALSE
;
165 UNICODE_STRING system_dir
= { 0, 0, NULL
}; /* system directory */
167 static struct file_identity curdir
;
168 static struct file_identity windir
;
170 static RTL_CRITICAL_SECTION dir_section
;
171 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
174 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
175 0, 0, { (DWORD_PTR
)(__FILE__
": dir_section") }
177 static RTL_CRITICAL_SECTION dir_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
180 /* check if a given Unicode char is OK in a DOS short name */
181 static inline BOOL
is_invalid_dos_char( WCHAR ch
)
183 static const WCHAR invalid_chars
[] = { INVALID_DOS_CHARS
,'~','.',0 };
184 if (ch
> 0x7f) return TRUE
;
185 return strchrW( invalid_chars
, ch
) != NULL
;
188 /* check if the device can be a mounted volume */
189 static inline int is_valid_mounted_device( const struct stat
*st
)
191 #if defined(linux) || defined(__sun__)
192 return S_ISBLK( st
->st_mode
);
194 /* disks are char devices on *BSD */
195 return S_ISCHR( st
->st_mode
);
199 static inline void ignore_file( const char *name
)
202 assert( ignored_files_count
< MAX_IGNORED_FILES
);
203 if (!stat( name
, &st
))
205 ignored_files
[ignored_files_count
].dev
= st
.st_dev
;
206 ignored_files
[ignored_files_count
].ino
= st
.st_ino
;
207 ignored_files_count
++;
211 static inline BOOL
is_same_file( const struct file_identity
*file
, const struct stat
*st
)
213 return st
->st_dev
== file
->dev
&& st
->st_ino
== file
->ino
;
216 static inline BOOL
is_ignored_file( const struct stat
*st
)
220 for (i
= 0; i
< ignored_files_count
; i
++)
221 if (is_same_file( &ignored_files
[i
], st
)) return TRUE
;
225 static inline unsigned int dir_info_size( FILE_INFORMATION_CLASS
class, unsigned int len
)
229 case FileDirectoryInformation
:
230 return (FIELD_OFFSET( FILE_DIRECTORY_INFORMATION
, FileName
[len
] ) + 3) & ~3;
231 case FileBothDirectoryInformation
:
232 return (FIELD_OFFSET( FILE_BOTH_DIRECTORY_INFORMATION
, FileName
[len
] ) + 3) & ~3;
233 case FileFullDirectoryInformation
:
234 return (FIELD_OFFSET( FILE_FULL_DIRECTORY_INFORMATION
, FileName
[len
] ) + 3) & ~3;
235 case FileIdBothDirectoryInformation
:
236 return (FIELD_OFFSET( FILE_ID_BOTH_DIRECTORY_INFORMATION
, FileName
[len
] ) + 3) & ~3;
237 case FileIdFullDirectoryInformation
:
238 return (FIELD_OFFSET( FILE_ID_FULL_DIRECTORY_INFORMATION
, FileName
[len
] ) + 3) & ~3;
245 static inline unsigned int max_dir_info_size( FILE_INFORMATION_CLASS
class )
247 return dir_info_size( class, MAX_DIR_ENTRY_LEN
);
251 /* support for a directory queue for filesystem searches */
259 static struct list dir_queue
= LIST_INIT( dir_queue
);
261 static NTSTATUS
add_dir_to_queue( const char *name
)
263 int len
= strlen( name
) + 1;
264 struct dir_name
*dir
= RtlAllocateHeap( GetProcessHeap(), 0,
265 FIELD_OFFSET( struct dir_name
, name
[len
] ));
266 if (!dir
) return STATUS_NO_MEMORY
;
267 strcpy( dir
->name
, name
);
268 list_add_tail( &dir_queue
, &dir
->entry
);
269 return STATUS_SUCCESS
;
272 static NTSTATUS
next_dir_in_queue( char *name
)
274 struct list
*head
= list_head( &dir_queue
);
277 struct dir_name
*dir
= LIST_ENTRY( head
, struct dir_name
, entry
);
278 strcpy( name
, dir
->name
);
279 list_remove( &dir
->entry
);
280 RtlFreeHeap( GetProcessHeap(), 0, dir
);
281 return STATUS_SUCCESS
;
283 return STATUS_OBJECT_NAME_NOT_FOUND
;
286 static void flush_dir_queue(void)
290 while ((head
= list_head( &dir_queue
)))
292 struct dir_name
*dir
= LIST_ENTRY( head
, struct dir_name
, entry
);
293 list_remove( &dir
->entry
);
294 RtlFreeHeap( GetProcessHeap(), 0, dir
);
299 /***********************************************************************
300 * get_default_com_device
302 * Return the default device to use for serial ports.
304 static char *get_default_com_device( int num
)
308 if (!num
|| num
> 9) return ret
;
310 ret
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/ttyS0") );
313 strcpy( ret
, "/dev/ttyS0" );
314 ret
[strlen(ret
) - 1] = '0' + num
- 1;
316 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
317 ret
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/cuad0") );
320 strcpy( ret
, "/dev/cuad0" );
321 ret
[strlen(ret
) - 1] = '0' + num
- 1;
324 FIXME( "no known default for device com%d\n", num
);
330 /***********************************************************************
331 * get_default_lpt_device
333 * Return the default device to use for parallel ports.
335 static char *get_default_lpt_device( int num
)
339 if (!num
|| num
> 9) return ret
;
341 ret
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/lp0") );
344 strcpy( ret
, "/dev/lp0" );
345 ret
[strlen(ret
) - 1] = '0' + num
- 1;
348 FIXME( "no known default for device lpt%d\n", num
);
354 /***********************************************************************
355 * DIR_get_drives_info
357 * Retrieve device/inode number for all the drives. Helper for find_drive_root.
359 unsigned int DIR_get_drives_info( struct drive_info info
[MAX_DOS_DRIVES
] )
361 static struct drive_info cache
[MAX_DOS_DRIVES
];
362 static time_t last_update
;
363 static unsigned int nb_drives
;
365 time_t now
= time(NULL
);
367 RtlEnterCriticalSection( &dir_section
);
368 if (now
!= last_update
)
370 const char *config_dir
= wine_get_config_dir();
375 if ((buffer
= RtlAllocateHeap( GetProcessHeap(), 0,
376 strlen(config_dir
) + sizeof("/dosdevices/a:") )))
378 strcpy( buffer
, config_dir
);
379 strcat( buffer
, "/dosdevices/a:" );
380 p
= buffer
+ strlen(buffer
) - 2;
382 for (i
= nb_drives
= 0; i
< MAX_DOS_DRIVES
; i
++)
385 if (!stat( buffer
, &st
))
387 cache
[i
].dev
= st
.st_dev
;
388 cache
[i
].ino
= st
.st_ino
;
397 RtlFreeHeap( GetProcessHeap(), 0, buffer
);
401 memcpy( info
, cache
, sizeof(cache
) );
403 RtlLeaveCriticalSection( &dir_section
);
408 /***********************************************************************
409 * parse_mount_entries
411 * Parse mount entries looking for a given device. Helper for get_default_drive_device.
415 #include <sys/vfstab.h>
416 static char *parse_vfstab_entries( FILE *f
, dev_t dev
, ino_t ino
)
422 while (! getvfsent( f
, &entry
))
424 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
425 if (!strcmp( entry
.vfs_fstype
, "nfs" ) ||
426 !strcmp( entry
.vfs_fstype
, "smbfs" ) ||
427 !strcmp( entry
.vfs_fstype
, "ncpfs" )) continue;
429 if (stat( entry
.vfs_mountp
, &st
) == -1) continue;
430 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
431 if (!strcmp( entry
.vfs_fstype
, "fd" ))
433 if ((device
= strstr( entry
.vfs_mntopts
, "dev=" )))
435 char *p
= strchr( device
+ 4, ',' );
441 return entry
.vfs_special
;
448 static char *parse_mount_entries( FILE *f
, dev_t dev
, ino_t ino
)
450 struct mntent
*entry
;
454 while ((entry
= getmntent( f
)))
456 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
457 if (!strcmp( entry
->mnt_type
, "nfs" ) ||
458 !strcmp( entry
->mnt_type
, "smbfs" ) ||
459 !strcmp( entry
->mnt_type
, "ncpfs" )) continue;
461 if (stat( entry
->mnt_dir
, &st
) == -1) continue;
462 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
463 if (!strcmp( entry
->mnt_type
, "supermount" ))
465 if ((device
= strstr( entry
->mnt_opts
, "dev=" )))
467 char *p
= strchr( device
+ 4, ',' );
472 else if (!stat( entry
->mnt_fsname
, &st
) && S_ISREG(st
.st_mode
))
474 /* if device is a regular file check for a loop mount */
475 if ((device
= strstr( entry
->mnt_opts
, "loop=" )))
477 char *p
= strchr( device
+ 5, ',' );
483 return entry
->mnt_fsname
;
489 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
491 static char *parse_mount_entries( FILE *f
, dev_t dev
, ino_t ino
)
496 while ((entry
= getfsent()))
498 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
499 if (!strcmp( entry
->fs_vfstype
, "nfs" ) ||
500 !strcmp( entry
->fs_vfstype
, "smbfs" ) ||
501 !strcmp( entry
->fs_vfstype
, "ncpfs" )) continue;
503 if (stat( entry
->fs_file
, &st
) == -1) continue;
504 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
505 return entry
->fs_spec
;
512 #include <sys/mnttab.h>
513 static char *parse_mount_entries( FILE *f
, dev_t dev
, ino_t ino
)
520 while (( ! getmntent( f
, &entry
) ))
522 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
523 if (!strcmp( entry
.mnt_fstype
, "nfs" ) ||
524 !strcmp( entry
.mnt_fstype
, "smbfs" ) ||
525 !strcmp( entry
.mnt_fstype
, "ncpfs" )) continue;
527 if (stat( entry
.mnt_mountp
, &st
) == -1) continue;
528 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
529 if (!strcmp( entry
.mnt_fstype
, "fd" ))
531 if ((device
= strstr( entry
.mnt_mntopts
, "dev=" )))
533 char *p
= strchr( device
+ 4, ',' );
539 return entry
.mnt_special
;
545 /***********************************************************************
546 * get_default_drive_device
548 * Return the default device to use for a given drive mount point.
550 static char *get_default_drive_device( const char *root
)
560 /* try to open it first to force it to get mounted */
561 if ((fd
= open( root
, O_RDONLY
| O_DIRECTORY
)) != -1)
563 res
= fstat( fd
, &st
);
566 /* now try normal stat just in case */
567 if (res
== -1) res
= stat( root
, &st
);
568 if (res
== -1) return NULL
;
570 RtlEnterCriticalSection( &dir_section
);
572 if ((f
= fopen( "/etc/mtab", "r" )))
574 device
= parse_mount_entries( f
, st
.st_dev
, st
.st_ino
);
577 /* look through fstab too in case it's not mounted (for instance if it's an audio CD) */
578 if (!device
&& (f
= fopen( "/etc/fstab", "r" )))
580 device
= parse_mount_entries( f
, st
.st_dev
, st
.st_ino
);
585 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(device
) + 1 );
586 if (ret
) strcpy( ret
, device
);
588 RtlLeaveCriticalSection( &dir_section
);
590 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__ )
595 /* try to open it first to force it to get mounted */
596 if ((fd
= open( root
, O_RDONLY
)) != -1)
598 res
= fstat( fd
, &st
);
601 /* now try normal stat just in case */
602 if (res
== -1) res
= stat( root
, &st
);
603 if (res
== -1) return NULL
;
605 RtlEnterCriticalSection( &dir_section
);
607 /* The FreeBSD parse_mount_entries doesn't require a file argument, so just
608 * pass NULL. Leave the argument in for symmetry.
610 device
= parse_mount_entries( NULL
, st
.st_dev
, st
.st_ino
);
613 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(device
) + 1 );
614 if (ret
) strcpy( ret
, device
);
616 RtlLeaveCriticalSection( &dir_section
);
624 /* try to open it first to force it to get mounted */
625 if ((fd
= open( root
, O_RDONLY
)) != -1)
627 res
= fstat( fd
, &st
);
630 /* now try normal stat just in case */
631 if (res
== -1) res
= stat( root
, &st
);
632 if (res
== -1) return NULL
;
634 RtlEnterCriticalSection( &dir_section
);
636 if ((f
= fopen( "/etc/mnttab", "r" )))
638 device
= parse_mount_entries( f
, st
.st_dev
, st
.st_ino
);
641 /* look through fstab too in case it's not mounted (for instance if it's an audio CD) */
642 if (!device
&& (f
= fopen( "/etc/vfstab", "r" )))
644 device
= parse_vfstab_entries( f
, st
.st_dev
, st
.st_ino
);
649 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(device
) + 1 );
650 if (ret
) strcpy( ret
, device
);
652 RtlLeaveCriticalSection( &dir_section
);
654 #elif defined(__APPLE__)
655 struct statfs
*mntStat
;
661 static const char path_bsd_device
[] = "/dev/disk";
664 res
= stat( root
, &st
);
665 if (res
== -1) return NULL
;
670 RtlEnterCriticalSection( &dir_section
);
672 mntSize
= getmntinfo(&mntStat
, MNT_NOWAIT
);
674 for (i
= 0; i
< mntSize
&& !ret
; i
++)
676 if (stat(mntStat
[i
].f_mntonname
, &st
) == -1) continue;
677 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
679 /* FIXME add support for mounted network drive */
680 if ( strncmp(mntStat
[i
].f_mntfromname
, path_bsd_device
, strlen(path_bsd_device
)) == 0)
682 /* set return value to the corresponding raw BSD node */
683 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(mntStat
[i
].f_mntfromname
) + 2 /* 2 : r and \0 */ );
686 strcpy(ret
, "/dev/r");
687 strcat(ret
, mntStat
[i
].f_mntfromname
+sizeof("/dev/")-1);
691 RtlLeaveCriticalSection( &dir_section
);
694 if (!warned
++) FIXME( "auto detection of DOS devices not supported on this platform\n" );
700 /***********************************************************************
701 * get_device_mount_point
703 * Return the current mount point for a device.
705 static char *get_device_mount_point( dev_t dev
)
712 RtlEnterCriticalSection( &dir_section
);
714 if ((f
= fopen( "/etc/mtab", "r" )))
716 struct mntent
*entry
;
720 while ((entry
= getmntent( f
)))
722 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
723 if (!strcmp( entry
->mnt_type
, "nfs" ) ||
724 !strcmp( entry
->mnt_type
, "smbfs" ) ||
725 !strcmp( entry
->mnt_type
, "ncpfs" )) continue;
727 if (!strcmp( entry
->mnt_type
, "supermount" ))
729 if ((device
= strstr( entry
->mnt_opts
, "dev=" )))
732 if ((p
= strchr( device
, ',' ))) *p
= 0;
735 else if (!stat( entry
->mnt_fsname
, &st
) && S_ISREG(st
.st_mode
))
737 /* if device is a regular file check for a loop mount */
738 if ((device
= strstr( entry
->mnt_opts
, "loop=" )))
741 if ((p
= strchr( device
, ',' ))) *p
= 0;
744 else device
= entry
->mnt_fsname
;
746 if (device
&& !stat( device
, &st
) && S_ISBLK(st
.st_mode
) && st
.st_rdev
== dev
)
748 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(entry
->mnt_dir
) + 1 );
749 if (ret
) strcpy( ret
, entry
->mnt_dir
);
755 RtlLeaveCriticalSection( &dir_section
);
756 #elif defined(__APPLE__)
757 struct statfs
*entry
;
761 RtlEnterCriticalSection( &dir_section
);
763 size
= getmntinfo( &entry
, MNT_NOWAIT
);
764 for (i
= 0; i
< size
; i
++)
766 if (stat( entry
[i
].f_mntfromname
, &st
) == -1) continue;
767 if (S_ISBLK(st
.st_mode
) && st
.st_rdev
== dev
)
769 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(entry
[i
].f_mntfromname
) + 1 );
770 if (ret
) strcpy( ret
, entry
[i
].f_mntfromname
);
774 RtlLeaveCriticalSection( &dir_section
);
777 if (!warned
++) FIXME( "unmounting devices not supported on this platform\n" );
783 #if defined(HAVE_GETATTRLIST) && defined(ATTR_VOL_CAPABILITIES) && \
784 defined(VOL_CAPABILITIES_FORMAT) && defined(VOL_CAP_FMT_CASE_SENSITIVE)
797 BOOLEAN case_sensitive
;
803 vol_capabilities_attr_t caps
;
806 /***********************************************************************
809 * Checks if the specified file system is in the cache.
811 static struct fs_cache
*look_up_fs_cache( dev_t dev
)
814 for (i
= 0; i
< sizeof(fs_cache
)/sizeof(fs_cache
[0]); i
++)
815 if (fs_cache
[i
].dev
== dev
)
820 /***********************************************************************
823 * Adds the specified file system to the cache.
825 static void add_fs_cache( dev_t dev
, fsid_t fsid
, BOOLEAN case_sensitive
)
828 struct fs_cache
*entry
= look_up_fs_cache( dev
);
832 /* Update the cache */
834 entry
->case_sensitive
= case_sensitive
;
838 /* Add a new entry */
839 for (i
= 0; i
< sizeof(fs_cache
)/sizeof(fs_cache
[0]); i
++)
840 if (fs_cache
[i
].dev
== 0)
842 /* This entry is empty, use it */
843 fs_cache
[i
].dev
= dev
;
844 fs_cache
[i
].fsid
= fsid
;
845 fs_cache
[i
].case_sensitive
= case_sensitive
;
849 /* Cache is out of space, warn */
851 WARN( "FS cache is out of space, expect performance problems\n" );
854 /***********************************************************************
855 * get_dir_case_sensitivity_attr
857 * Checks if the volume containing the specified directory is case
858 * sensitive or not. Uses getattrlist(2).
860 static int get_dir_case_sensitivity_attr( const char *dir
)
862 char *mntpoint
= NULL
;
863 struct attrlist attr
;
864 struct vol_caps caps
;
865 struct get_fsid get_fsid
;
866 struct fs_cache
*entry
;
868 /* First get the FS ID of the volume */
869 attr
.bitmapcount
= ATTR_BIT_MAP_COUNT
;
871 attr
.commonattr
= ATTR_CMN_DEVID
|ATTR_CMN_FSID
;
872 attr
.volattr
= attr
.dirattr
= attr
.fileattr
= attr
.forkattr
= 0;
874 if (getattrlist( dir
, &attr
, &get_fsid
, sizeof(get_fsid
), 0 ) != 0 ||
875 get_fsid
.size
!= sizeof(get_fsid
))
877 /* Try to look it up in the cache */
878 entry
= look_up_fs_cache( get_fsid
.dev
);
879 if (entry
&& !memcmp( &entry
->fsid
, &get_fsid
.fsid
, sizeof(fsid_t
) ))
880 /* Cache lookup succeeded */
881 return entry
->case_sensitive
;
882 /* Cache is stale at this point, we have to update it */
884 mntpoint
= get_device_mount_point( get_fsid
.dev
);
885 /* Now look up the case-sensitivity */
887 attr
.volattr
= ATTR_VOL_INFO
|ATTR_VOL_CAPABILITIES
;
888 if (getattrlist( mntpoint
, &attr
, &caps
, sizeof(caps
), 0 ) < 0)
890 RtlFreeHeap( GetProcessHeap(), 0, mntpoint
);
891 add_fs_cache( get_fsid
.dev
, get_fsid
.fsid
, TRUE
);
894 RtlFreeHeap( GetProcessHeap(), 0, mntpoint
);
895 if (caps
.size
== sizeof(caps
) &&
896 (caps
.caps
.valid
[VOL_CAPABILITIES_FORMAT
] &
897 (VOL_CAP_FMT_CASE_SENSITIVE
| VOL_CAP_FMT_CASE_PRESERVING
)) ==
898 (VOL_CAP_FMT_CASE_SENSITIVE
| VOL_CAP_FMT_CASE_PRESERVING
))
902 if ((caps
.caps
.capabilities
[VOL_CAPABILITIES_FORMAT
] &
903 VOL_CAP_FMT_CASE_SENSITIVE
) != VOL_CAP_FMT_CASE_SENSITIVE
)
907 /* Update the cache */
908 add_fs_cache( get_fsid
.dev
, get_fsid
.fsid
, ret
);
915 /***********************************************************************
916 * get_dir_case_sensitivity_stat
918 * Checks if the volume containing the specified directory is case
919 * sensitive or not. Uses statfs(2) or statvfs(2).
921 static BOOLEAN
get_dir_case_sensitivity_stat( const char *dir
)
923 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
926 if (statfs( dir
, &stfs
) == -1) return FALSE
;
927 /* Assume these file systems are always case insensitive on Mac OS.
928 * For FreeBSD, only assume CIOPFS is case insensitive (AFAIK, Mac OS
929 * is the only UNIX that supports case-insensitive lookup).
931 if (!strcmp( stfs
.f_fstypename
, "fusefs" ) &&
932 !strncmp( stfs
.f_mntfromname
, "ciopfs", 5 ))
935 if (!strcmp( stfs
.f_fstypename
, "msdos" ) ||
936 !strcmp( stfs
.f_fstypename
, "cd9660" ) ||
937 !strcmp( stfs
.f_fstypename
, "udf" ) ||
938 !strcmp( stfs
.f_fstypename
, "ntfs" ) ||
939 !strcmp( stfs
.f_fstypename
, "smbfs" ))
941 #ifdef _DARWIN_FEATURE_64_BIT_INODE
942 if (!strcmp( stfs
.f_fstypename
, "hfs" ) && (stfs
.f_fssubtype
== 0 ||
943 stfs
.f_fssubtype
== 1 ||
944 stfs
.f_fssubtype
== 128))
947 /* The field says "reserved", but a quick look at the kernel source
948 * tells us that this "reserved" field is really the same as the
949 * "fssubtype" field from the inode64 structure (see munge_statfs()
950 * in <xnu-source>/bsd/vfs/vfs_syscalls.c).
952 if (!strcmp( stfs
.f_fstypename
, "hfs" ) && (stfs
.f_reserved1
== 0 ||
953 stfs
.f_reserved1
== 1 ||
954 stfs
.f_reserved1
== 128))
960 #elif defined(__NetBSD__)
963 if (statvfs( dir
, &stfs
) == -1) return FALSE
;
964 /* Only assume CIOPFS is case insensitive. */
965 if (strcmp( stfs
.f_fstypename
, "fusefs" ) ||
966 strncmp( stfs
.f_mntfromname
, "ciopfs", 5 ))
970 #elif defined(__linux__)
975 /* Only assume CIOPFS is case insensitive. */
976 if (statfs( dir
, &stfs
) == -1) return FALSE
;
977 if (stfs
.f_type
!= 0x65735546 /* FUSE_SUPER_MAGIC */)
979 /* Normally, we'd have to parse the mtab to find out exactly what
980 * kind of FUSE FS this is. But, someone on wine-devel suggested
981 * a shortcut. We'll stat a special file in the directory. If it's
982 * there, we'll assume it's a CIOPFS, else not.
983 * This will break if somebody puts a file named ".ciopfs" in a non-
986 cifile
= RtlAllocateHeap( GetProcessHeap(), 0, strlen( dir
)+sizeof("/.ciopfs") );
987 if (!cifile
) return TRUE
;
988 strcpy( cifile
, dir
);
989 strcat( cifile
, "/.ciopfs" );
990 if (stat( cifile
, &st
) == 0)
992 RtlFreeHeap( GetProcessHeap(), 0, cifile
);
995 RtlFreeHeap( GetProcessHeap(), 0, cifile
);
1003 /***********************************************************************
1004 * get_dir_case_sensitivity
1006 * Checks if the volume containing the specified directory is case
1007 * sensitive or not. Uses statfs(2) or statvfs(2).
1009 static BOOLEAN
get_dir_case_sensitivity( const char *dir
)
1011 #if defined(HAVE_GETATTRLIST) && defined(ATTR_VOL_CAPABILITIES) && \
1012 defined(VOL_CAPABILITIES_FORMAT) && defined(VOL_CAP_FMT_CASE_SENSITIVE)
1013 int case_sensitive
= get_dir_case_sensitivity_attr( dir
);
1014 if (case_sensitive
!= -1) return case_sensitive
;
1016 return get_dir_case_sensitivity_stat( dir
);
1020 /***********************************************************************
1023 * Initialize the show_dot_files options.
1025 static void init_options(void)
1027 static const WCHAR WineW
[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e',0};
1028 static const WCHAR ShowDotFilesW
[] = {'S','h','o','w','D','o','t','F','i','l','e','s',0};
1032 OBJECT_ATTRIBUTES attr
;
1033 UNICODE_STRING nameW
;
1037 RtlOpenCurrentUser( KEY_ALL_ACCESS
, &root
);
1038 attr
.Length
= sizeof(attr
);
1039 attr
.RootDirectory
= root
;
1040 attr
.ObjectName
= &nameW
;
1041 attr
.Attributes
= 0;
1042 attr
.SecurityDescriptor
= NULL
;
1043 attr
.SecurityQualityOfService
= NULL
;
1044 RtlInitUnicodeString( &nameW
, WineW
);
1046 /* @@ Wine registry key: HKCU\Software\Wine */
1047 if (!NtOpenKey( &hkey
, KEY_ALL_ACCESS
, &attr
))
1049 RtlInitUnicodeString( &nameW
, ShowDotFilesW
);
1050 if (!NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
, tmp
, sizeof(tmp
), &dummy
))
1052 WCHAR
*str
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)tmp
)->Data
;
1053 show_dot_files
= IS_OPTION_TRUE( str
[0] );
1059 /* a couple of directories that we don't want to return in directory searches */
1060 ignore_file( wine_get_config_dir() );
1061 ignore_file( "/dev" );
1062 ignore_file( "/proc" );
1064 ignore_file( "/sys" );
1069 /***********************************************************************
1070 * DIR_is_hidden_file
1072 * Check if the specified file should be hidden based on its name and the show dot files option.
1074 BOOL
DIR_is_hidden_file( const UNICODE_STRING
*name
)
1078 if (show_dot_files
== -1) init_options();
1079 if (show_dot_files
) return FALSE
;
1081 end
= p
= name
->Buffer
+ name
->Length
/sizeof(WCHAR
);
1082 while (p
> name
->Buffer
&& IS_SEPARATOR(p
[-1])) p
--;
1083 while (p
> name
->Buffer
&& !IS_SEPARATOR(p
[-1])) p
--;
1084 if (p
== end
|| *p
!= '.') return FALSE
;
1085 /* make sure it isn't '.' or '..' */
1086 if (p
+ 1 == end
) return FALSE
;
1087 if (p
[1] == '.' && p
+ 2 == end
) return FALSE
;
1092 /***********************************************************************
1093 * hash_short_file_name
1095 * Transform a Unix file name into a hashed DOS name. If the name is a valid
1096 * DOS name, it is converted to upper-case; otherwise it is replaced by a
1097 * hashed version that fits in 8.3 format.
1098 * 'buffer' must be at least 12 characters long.
1099 * Returns length of short name in bytes; short name is NOT null-terminated.
1101 static ULONG
hash_short_file_name( const UNICODE_STRING
*name
, LPWSTR buffer
)
1103 static const char hash_chars
[32] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345";
1105 LPCWSTR p
, ext
, end
= name
->Buffer
+ name
->Length
/ sizeof(WCHAR
);
1107 unsigned short hash
;
1110 /* Compute the hash code of the file name */
1111 /* If you know something about hash functions, feel free to */
1112 /* insert a better algorithm here... */
1113 if (!is_case_sensitive
)
1115 for (p
= name
->Buffer
, hash
= 0xbeef; p
< end
- 1; p
++)
1116 hash
= (hash
<<3) ^ (hash
>>5) ^ tolowerW(*p
) ^ (tolowerW(p
[1]) << 8);
1117 hash
= (hash
<<3) ^ (hash
>>5) ^ tolowerW(*p
); /* Last character */
1121 for (p
= name
->Buffer
, hash
= 0xbeef; p
< end
- 1; p
++)
1122 hash
= (hash
<< 3) ^ (hash
>> 5) ^ *p
^ (p
[1] << 8);
1123 hash
= (hash
<< 3) ^ (hash
>> 5) ^ *p
; /* Last character */
1126 /* Find last dot for start of the extension */
1127 for (p
= name
->Buffer
+ 1, ext
= NULL
; p
< end
- 1; p
++) if (*p
== '.') ext
= p
;
1129 /* Copy first 4 chars, replacing invalid chars with '_' */
1130 for (i
= 4, p
= name
->Buffer
, dst
= buffer
; i
> 0; i
--, p
++)
1132 if (p
== end
|| p
== ext
) break;
1133 *dst
++ = is_invalid_dos_char(*p
) ? '_' : toupperW(*p
);
1135 /* Pad to 5 chars with '~' */
1136 while (i
-- >= 0) *dst
++ = '~';
1138 /* Insert hash code converted to 3 ASCII chars */
1139 *dst
++ = hash_chars
[(hash
>> 10) & 0x1f];
1140 *dst
++ = hash_chars
[(hash
>> 5) & 0x1f];
1141 *dst
++ = hash_chars
[hash
& 0x1f];
1143 /* Copy the first 3 chars of the extension (if any) */
1147 for (i
= 3, ext
++; (i
> 0) && ext
< end
; i
--, ext
++)
1148 *dst
++ = is_invalid_dos_char(*ext
) ? '_' : toupperW(*ext
);
1150 return dst
- buffer
;
1154 /***********************************************************************
1157 * Check a long file name against a mask.
1159 * Tests (done in W95 DOS shell - case insensitive):
1160 * *.txt test1.test.txt *
1162 * *.t??????.t* test1.ta.tornado.txt *
1163 * *tornado* test1.ta.tornado.txt *
1164 * t*t test1.ta.tornado.txt *
1166 * ?est??? test1.txt -
1167 * *test1.txt* test1.txt *
1168 * h?l?o*t.dat hellothisisatest.dat *
1170 static BOOLEAN
match_filename( const UNICODE_STRING
*name_str
, const UNICODE_STRING
*mask_str
)
1173 const WCHAR
*name
= name_str
->Buffer
;
1174 const WCHAR
*mask
= mask_str
->Buffer
;
1175 const WCHAR
*name_end
= name
+ name_str
->Length
/ sizeof(WCHAR
);
1176 const WCHAR
*mask_end
= mask
+ mask_str
->Length
/ sizeof(WCHAR
);
1177 const WCHAR
*lastjoker
= NULL
;
1178 const WCHAR
*next_to_retry
= NULL
;
1180 TRACE("(%s, %s)\n", debugstr_us(name_str
), debugstr_us(mask_str
));
1182 while (name
< name_end
&& mask
< mask_end
)
1188 while (mask
< mask_end
&& *mask
== '*') mask
++; /* Skip consecutive '*' */
1189 if (mask
== mask_end
) return TRUE
; /* end of mask is all '*', so match */
1192 /* skip to the next match after the joker(s) */
1193 if (is_case_sensitive
)
1194 while (name
< name_end
&& (*name
!= *mask
)) name
++;
1196 while (name
< name_end
&& (toupperW(*name
) != toupperW(*mask
))) name
++;
1197 next_to_retry
= name
;
1204 if (is_case_sensitive
) mismatch
= (*mask
!= *name
);
1205 else mismatch
= (toupperW(*mask
) != toupperW(*name
));
1211 if (mask
== mask_end
)
1213 if (name
== name_end
) return TRUE
;
1214 if (lastjoker
) mask
= lastjoker
;
1217 else /* mismatch ! */
1219 if (lastjoker
) /* we had an '*', so we can try unlimitedly */
1223 /* this scan sequence was a mismatch, so restart
1224 * 1 char after the first char we checked last time */
1226 name
= next_to_retry
;
1228 else return FALSE
; /* bad luck */
1233 while (mask
< mask_end
&& ((*mask
== '.') || (*mask
== '*')))
1234 mask
++; /* Ignore trailing '.' or '*' in mask */
1235 return (name
== name_end
&& mask
== mask_end
);
1239 /***********************************************************************
1242 * helper for NtQueryDirectoryFile
1244 static union file_directory_info
*append_entry( void *info_ptr
, IO_STATUS_BLOCK
*io
, ULONG max_length
,
1245 const char *long_name
, const char *short_name
,
1246 const UNICODE_STRING
*mask
, FILE_INFORMATION_CLASS
class )
1248 union file_directory_info
*info
;
1249 int i
, long_len
, short_len
, total_len
;
1251 WCHAR long_nameW
[MAX_DIR_ENTRY_LEN
];
1252 WCHAR short_nameW
[12];
1255 ULONG attributes
= 0;
1257 io
->u
.Status
= STATUS_SUCCESS
;
1258 long_len
= ntdll_umbstowcs( 0, long_name
, strlen(long_name
), long_nameW
, MAX_DIR_ENTRY_LEN
);
1259 if (long_len
== -1) return NULL
;
1261 str
.Buffer
= long_nameW
;
1262 str
.Length
= long_len
* sizeof(WCHAR
);
1263 str
.MaximumLength
= sizeof(long_nameW
);
1267 short_len
= ntdll_umbstowcs( 0, short_name
, strlen(short_name
),
1268 short_nameW
, sizeof(short_nameW
) / sizeof(WCHAR
) );
1269 if (short_len
== -1) short_len
= sizeof(short_nameW
) / sizeof(WCHAR
);
1271 else /* generate a short name if necessary */
1276 if (!RtlIsNameLegalDOS8Dot3( &str
, NULL
, &spaces
) || spaces
)
1277 short_len
= hash_short_file_name( &str
, short_nameW
);
1280 TRACE( "long %s short %s mask %s\n",
1281 debugstr_us(&str
), debugstr_wn(short_nameW
, short_len
), debugstr_us(mask
) );
1283 if (mask
&& !match_filename( &str
, mask
))
1285 if (!short_len
) return NULL
; /* no short name to match */
1286 str
.Buffer
= short_nameW
;
1287 str
.Length
= short_len
* sizeof(WCHAR
);
1288 str
.MaximumLength
= sizeof(short_nameW
);
1289 if (!match_filename( &str
, mask
)) return NULL
;
1292 if (lstat( long_name
, &st
) == -1) return NULL
;
1293 if (S_ISLNK( st
.st_mode
))
1295 if (stat( long_name
, &st
) == -1) return NULL
;
1296 if (S_ISDIR( st
.st_mode
)) attributes
|= FILE_ATTRIBUTE_REPARSE_POINT
;
1298 if (is_ignored_file( &st
))
1300 TRACE( "ignoring file %s\n", long_name
);
1303 if (!show_dot_files
&& long_name
[0] == '.' && long_name
[1] && (long_name
[1] != '.' || long_name
[2]))
1304 attributes
|= FILE_ATTRIBUTE_HIDDEN
;
1306 total_len
= dir_info_size( class, long_len
);
1307 if (io
->Information
+ total_len
> max_length
)
1309 total_len
= max_length
- io
->Information
;
1310 io
->u
.Status
= STATUS_BUFFER_OVERFLOW
;
1312 info
= (union file_directory_info
*)((char *)info_ptr
+ io
->Information
);
1313 if (st
.st_dev
!= curdir
.dev
) st
.st_ino
= 0; /* ignore inode if on a different device */
1314 /* all the structures start with a FileDirectoryInformation layout */
1315 fill_stat_info( &st
, info
, class );
1316 info
->dir
.NextEntryOffset
= total_len
;
1317 info
->dir
.FileIndex
= 0; /* NTFS always has 0 here, so let's not bother with it */
1318 info
->dir
.FileAttributes
|= attributes
;
1322 case FileDirectoryInformation
:
1323 info
->dir
.FileNameLength
= long_len
* sizeof(WCHAR
);
1324 filename
= info
->dir
.FileName
;
1327 case FileFullDirectoryInformation
:
1328 info
->full
.EaSize
= 0; /* FIXME */
1329 info
->full
.FileNameLength
= long_len
* sizeof(WCHAR
);
1330 filename
= info
->full
.FileName
;
1333 case FileIdFullDirectoryInformation
:
1334 info
->id_full
.EaSize
= 0; /* FIXME */
1335 info
->id_full
.FileNameLength
= long_len
* sizeof(WCHAR
);
1336 filename
= info
->id_full
.FileName
;
1339 case FileBothDirectoryInformation
:
1340 info
->both
.EaSize
= 0; /* FIXME */
1341 info
->both
.ShortNameLength
= short_len
* sizeof(WCHAR
);
1342 for (i
= 0; i
< short_len
; i
++) info
->both
.ShortName
[i
] = toupperW(short_nameW
[i
]);
1343 info
->both
.FileNameLength
= long_len
* sizeof(WCHAR
);
1344 filename
= info
->both
.FileName
;
1347 case FileIdBothDirectoryInformation
:
1348 info
->id_both
.EaSize
= 0; /* FIXME */
1349 info
->id_both
.ShortNameLength
= short_len
* sizeof(WCHAR
);
1350 for (i
= 0; i
< short_len
; i
++) info
->id_both
.ShortName
[i
] = toupperW(short_nameW
[i
]);
1351 info
->id_both
.FileNameLength
= long_len
* sizeof(WCHAR
);
1352 filename
= info
->id_both
.FileName
;
1359 memcpy( filename
, long_nameW
, total_len
- ((char *)filename
- (char *)info
) );
1360 io
->Information
+= total_len
;
1365 #ifdef VFAT_IOCTL_READDIR_BOTH
1367 /***********************************************************************
1370 * Wrapper for the VFAT ioctl to work around various kernel bugs.
1371 * dir_section must be held by caller.
1373 static KERNEL_DIRENT
*start_vfat_ioctl( int fd
)
1375 static KERNEL_DIRENT
*de
;
1380 const size_t page_size
= getpagesize();
1381 SIZE_T size
= 2 * sizeof(*de
) + page_size
;
1384 if (NtAllocateVirtualMemory( GetCurrentProcess(), &addr
, 1, &size
, MEM_RESERVE
, PAGE_READWRITE
))
1386 /* commit only the size needed for the dir entries */
1387 /* this leaves an extra unaccessible page, which should make the kernel */
1388 /* fail with -EFAULT before it stomps all over our memory */
1390 size
= 2 * sizeof(*de
);
1391 NtAllocateVirtualMemory( GetCurrentProcess(), &addr
, 1, &size
, MEM_COMMIT
, PAGE_READWRITE
);
1394 /* set d_reclen to 65535 to work around an AFS kernel bug */
1395 de
[0].d_reclen
= 65535;
1396 res
= ioctl( fd
, VFAT_IOCTL_READDIR_BOTH
, (long)de
);
1399 if (errno
!= ENOENT
) return NULL
; /* VFAT ioctl probably not supported */
1400 de
[0].d_reclen
= 0; /* eof */
1402 else if (!res
&& de
[0].d_reclen
== 65535) return NULL
; /* AFS bug */
1408 /***********************************************************************
1409 * read_directory_vfat
1411 * Read a directory using the VFAT ioctl; helper for NtQueryDirectoryFile.
1413 static int read_directory_vfat( int fd
, IO_STATUS_BLOCK
*io
, void *buffer
, ULONG length
,
1414 BOOLEAN single_entry
, const UNICODE_STRING
*mask
,
1415 BOOLEAN restart_scan
, FILE_INFORMATION_CLASS
class )
1420 union file_directory_info
*info
, *last_info
= NULL
;
1422 io
->u
.Status
= STATUS_SUCCESS
;
1424 if (restart_scan
) lseek( fd
, 0, SEEK_SET
);
1426 if (length
< max_dir_info_size(class)) /* we may have to return a partial entry here */
1428 off_t old_pos
= lseek( fd
, 0, SEEK_CUR
);
1430 if (!(de
= start_vfat_ioctl( fd
))) return -1; /* not supported */
1432 while (de
[0].d_reclen
)
1434 /* make sure names are null-terminated to work around an x86-64 kernel bug */
1435 len
= min(de
[0].d_reclen
, sizeof(de
[0].d_name
) - 1 );
1436 de
[0].d_name
[len
] = 0;
1437 len
= min(de
[1].d_reclen
, sizeof(de
[1].d_name
) - 1 );
1438 de
[1].d_name
[len
] = 0;
1440 if (de
[1].d_name
[0])
1441 info
= append_entry( buffer
, io
, length
, de
[1].d_name
, de
[0].d_name
, mask
, class );
1443 info
= append_entry( buffer
, io
, length
, de
[0].d_name
, NULL
, mask
, class );
1447 if (io
->u
.Status
== STATUS_BUFFER_OVERFLOW
)
1448 lseek( fd
, old_pos
, SEEK_SET
); /* restore pos to previous entry */
1451 old_pos
= lseek( fd
, 0, SEEK_CUR
);
1452 if (ioctl( fd
, VFAT_IOCTL_READDIR_BOTH
, (long)de
) == -1) break;
1455 else /* we'll only return full entries, no need to worry about overflow */
1457 if (!(de
= start_vfat_ioctl( fd
))) return -1; /* not supported */
1459 while (de
[0].d_reclen
)
1461 /* make sure names are null-terminated to work around an x86-64 kernel bug */
1462 len
= min(de
[0].d_reclen
, sizeof(de
[0].d_name
) - 1 );
1463 de
[0].d_name
[len
] = 0;
1464 len
= min(de
[1].d_reclen
, sizeof(de
[1].d_name
) - 1 );
1465 de
[1].d_name
[len
] = 0;
1467 if (de
[1].d_name
[0])
1468 info
= append_entry( buffer
, io
, length
, de
[1].d_name
, de
[0].d_name
, mask
, class );
1470 info
= append_entry( buffer
, io
, length
, de
[0].d_name
, NULL
, mask
, class );
1474 if (single_entry
) break;
1475 /* check if we still have enough space for the largest possible entry */
1476 if (io
->Information
+ max_dir_info_size(class) > length
) break;
1478 if (ioctl( fd
, VFAT_IOCTL_READDIR_BOTH
, (long)de
) == -1) break;
1482 if (last_info
) last_info
->next
= 0;
1483 else io
->u
.Status
= restart_scan
? STATUS_NO_SUCH_FILE
: STATUS_NO_MORE_FILES
;
1486 #endif /* VFAT_IOCTL_READDIR_BOTH */
1490 /***********************************************************************
1491 * read_first_dent_name
1493 * reads name of first or second dentry (if they have inodes).
1495 static char *read_first_dent_name( int which
, int fd
, off_t second_offs
, KERNEL_DIRENT64
*de_first_two
,
1496 char *buffer
, size_t size
, BOOL
*buffer_changed
)
1498 KERNEL_DIRENT64
*de
;
1505 de
= (KERNEL_DIRENT64
*)((char *)de
+ de
->d_reclen
);
1507 return de
->d_ino
? de
->d_name
: NULL
;
1510 *buffer_changed
= TRUE
;
1511 lseek( fd
, which
== 1 ? second_offs
: 0, SEEK_SET
);
1512 res
= getdents64( fd
, buffer
, size
);
1516 de
= (KERNEL_DIRENT64
*)buffer
;
1517 return de
->d_ino
? de
->d_name
: NULL
;
1520 /***********************************************************************
1521 * read_directory_getdents
1523 * Read a directory using the Linux getdents64 system call; helper for NtQueryDirectoryFile.
1525 static int read_directory_getdents( int fd
, IO_STATUS_BLOCK
*io
, void *buffer
, ULONG length
,
1526 BOOLEAN single_entry
, const UNICODE_STRING
*mask
,
1527 BOOLEAN restart_scan
, FILE_INFORMATION_CLASS
class )
1529 static off_t second_entry_pos
;
1530 static struct file_identity last_dir_id
;
1531 off_t old_pos
= 0, next_pos
;
1532 size_t size
= length
;
1533 char *data
, local_buffer
[8192];
1534 KERNEL_DIRENT64
*de
, *de_first_two
= NULL
;
1535 union file_directory_info
*info
, *last_info
= NULL
;
1536 const char *filename
;
1537 BOOL data_buffer_changed
;
1540 if (size
<= sizeof(local_buffer
) || !(data
= RtlAllocateHeap( GetProcessHeap(), 0, size
)))
1542 size
= sizeof(local_buffer
);
1543 data
= local_buffer
;
1546 if (restart_scan
) lseek( fd
, 0, SEEK_SET
);
1549 old_pos
= lseek( fd
, 0, SEEK_CUR
);
1552 io
->u
.Status
= (errno
== ENOENT
) ? STATUS_NO_MORE_FILES
: FILE_GetNtStatus();
1558 io
->u
.Status
= STATUS_SUCCESS
;
1559 de
= (KERNEL_DIRENT64
*)data
;
1561 /* if old_pos is not 0 we don't know how many entries have been returned already,
1562 * so maintain second_entry_pos to know when to return '..' */
1563 if (old_pos
!= 0 && (last_dir_id
.dev
!= curdir
.dev
|| last_dir_id
.ino
!= curdir
.ino
))
1565 lseek( fd
, 0, SEEK_SET
);
1566 res
= getdents64( fd
, data
, size
);
1569 second_entry_pos
= de
->d_off
;
1570 last_dir_id
= curdir
;
1572 lseek( fd
, old_pos
, SEEK_SET
);
1575 res
= getdents64( fd
, data
, size
);
1578 if (errno
!= ENOSYS
)
1580 io
->u
.Status
= FILE_GetNtStatus();
1586 if (old_pos
== 0 && res
> 0)
1588 second_entry_pos
= de
->d_off
;
1589 last_dir_id
= curdir
;
1590 if (res
> de
->d_reclen
)
1596 res
-= de
->d_reclen
;
1597 next_pos
= de
->d_off
;
1600 /* we must return first 2 entries as "." and "..", but getdents64()
1601 * can return them anywhere, so swap first entries with "." and ".." */
1604 else if (old_pos
== second_entry_pos
)
1606 else if (!strcmp( de
->d_name
, "." ) || !strcmp( de
->d_name
, ".." ))
1608 swap_to
= !strcmp( de
->d_name
, "." ) ? 0 : 1;
1609 data_buffer_changed
= FALSE
;
1611 filename
= read_first_dent_name( swap_to
, fd
, second_entry_pos
, de_first_two
,
1612 data
, size
, &data_buffer_changed
);
1613 if (filename
!= NULL
&& (!strcmp( filename
, "." ) || !strcmp( filename
, ".." )))
1614 filename
= read_first_dent_name( swap_to
^ 1, fd
, second_entry_pos
, NULL
,
1615 data
, size
, &data_buffer_changed
);
1616 if (data_buffer_changed
)
1618 lseek( fd
, next_pos
, SEEK_SET
);
1623 filename
= de
->d_name
;
1625 if (filename
&& (info
= append_entry( buffer
, io
, length
, filename
, NULL
, mask
, class )))
1628 if (io
->u
.Status
== STATUS_BUFFER_OVERFLOW
)
1630 lseek( fd
, old_pos
, SEEK_SET
); /* restore pos to previous entry */
1633 /* check if we still have enough space for the largest possible entry */
1634 if (single_entry
|| io
->Information
+ max_dir_info_size(class) > length
)
1636 if (res
> 0) lseek( fd
, next_pos
, SEEK_SET
); /* set pos to next entry */
1641 /* move on to the next entry */
1642 if (res
> 0) de
= (KERNEL_DIRENT64
*)((char *)de
+ de
->d_reclen
);
1645 res
= getdents64( fd
, data
, size
);
1646 de
= (KERNEL_DIRENT64
*)data
;
1647 de_first_two
= NULL
;
1651 if (last_info
) last_info
->next
= 0;
1652 else io
->u
.Status
= restart_scan
? STATUS_NO_SUCH_FILE
: STATUS_NO_MORE_FILES
;
1655 if (data
!= local_buffer
) RtlFreeHeap( GetProcessHeap(), 0, data
);
1659 #elif defined HAVE_GETDIRENTRIES
1661 #ifdef _DARWIN_FEATURE_64_BIT_INODE
1663 /* Darwin doesn't provide a version of getdirentries with support for 64-bit
1664 * inodes. When 64-bit inodes are enabled, the getdirentries symbol is mapped
1665 * to _getdirentries_is_not_available_when_64_bit_inodes_are_in_effect so that
1666 * we get link errors if we try to use it. We still need getdirentries, but we
1667 * don't need it to support 64-bit inodes. So, we use the legacy getdirentries
1668 * with 32-bit inodes. We have to be careful to use a corresponding dirent
1671 int darwin_legacy_getdirentries(int, char *, int, long *) __asm("_getdirentries");
1672 #define getdirentries darwin_legacy_getdirentries
1674 struct darwin_legacy_dirent
{
1676 __uint16_t d_reclen
;
1679 char d_name
[__DARWIN_MAXNAMLEN
+ 1];
1681 #define dirent darwin_legacy_dirent
1685 /***********************************************************************
1686 * wine_getdirentries
1688 * Wrapper for the BSD getdirentries system call to fix a bug in the
1689 * Mac OS X version. For some file systems (at least Apple Filing
1690 * Protocol a.k.a. AFP), getdirentries resets the file position to 0
1691 * when it's about to return 0 (no more entries). So, a subsequent
1692 * getdirentries call starts over at the beginning again, causing an
1695 static inline int wine_getdirentries(int fd
, char *buf
, int nbytes
, long *basep
)
1697 int res
= getdirentries(fd
, buf
, nbytes
, basep
);
1700 lseek(fd
, *basep
, SEEK_SET
);
1705 /***********************************************************************
1706 * read_directory_getdirentries
1708 * Read a directory using the BSD getdirentries system call; helper for NtQueryDirectoryFile.
1710 static int read_directory_getdirentries( int fd
, IO_STATUS_BLOCK
*io
, void *buffer
, ULONG length
,
1711 BOOLEAN single_entry
, const UNICODE_STRING
*mask
,
1712 BOOLEAN restart_scan
, FILE_INFORMATION_CLASS
class )
1715 ULONG_PTR restart_info_pos
= 0;
1716 size_t size
, initial_size
= length
;
1717 int res
, fake_dot_dot
= 1;
1718 char *data
, local_buffer
[8192];
1720 union file_directory_info
*info
, *last_info
= NULL
, *restart_last_info
= NULL
;
1722 size
= initial_size
;
1723 data
= local_buffer
;
1724 if (size
> sizeof(local_buffer
) && !(data
= RtlAllocateHeap( GetProcessHeap(), 0, size
)))
1726 io
->u
.Status
= STATUS_NO_MEMORY
;
1727 return io
->u
.Status
;
1730 if (restart_scan
) lseek( fd
, 0, SEEK_SET
);
1732 io
->u
.Status
= STATUS_SUCCESS
;
1734 /* FIXME: should make sure size is larger than filesystem block size */
1735 res
= wine_getdirentries( fd
, data
, size
, &restart_pos
);
1738 io
->u
.Status
= FILE_GetNtStatus();
1743 de
= (struct dirent
*)data
;
1747 /* check if we got . and .. from getdirentries */
1750 if (!strcmp( de
->d_name
, "." ) && res
> de
->d_reclen
)
1752 struct dirent
*next_de
= (struct dirent
*)(data
+ de
->d_reclen
);
1753 if (!strcmp( next_de
->d_name
, ".." )) fake_dot_dot
= 0;
1756 /* make sure we have enough room for both entries */
1759 const ULONG min_info_size
= dir_info_size( class, 1 ) + dir_info_size( class, 2 );
1760 if (length
< min_info_size
|| single_entry
)
1762 FIXME( "not enough room %u/%u for fake . and .. entries\n", length
, single_entry
);
1769 if ((info
= append_entry( buffer
, io
, length
, ".", NULL
, mask
, class )))
1771 if ((info
= append_entry( buffer
, io
, length
, "..", NULL
, mask
, class )))
1774 restart_last_info
= last_info
;
1775 restart_info_pos
= io
->Information
;
1777 /* check if we still have enough space for the largest possible entry */
1778 if (last_info
&& io
->Information
+ max_dir_info_size(class) > length
)
1780 lseek( fd
, 0, SEEK_SET
); /* reset pos to first entry */
1788 res
-= de
->d_reclen
;
1790 !(fake_dot_dot
&& (!strcmp( de
->d_name
, "." ) || !strcmp( de
->d_name
, ".." ))) &&
1791 ((info
= append_entry( buffer
, io
, length
, de
->d_name
, NULL
, mask
, class ))))
1794 if (io
->u
.Status
== STATUS_BUFFER_OVERFLOW
)
1796 lseek( fd
, (unsigned long)restart_pos
, SEEK_SET
);
1797 if (restart_info_pos
) /* if we have a complete read already, return it */
1799 io
->u
.Status
= STATUS_SUCCESS
;
1800 io
->Information
= restart_info_pos
;
1801 last_info
= restart_last_info
;
1804 /* otherwise restart from the start with a smaller size */
1805 size
= (char *)de
- data
;
1807 io
->Information
= 0;
1811 /* if we have to return but the buffer contains more data, restart with a smaller size */
1812 if (res
> 0 && (single_entry
|| io
->Information
+ max_dir_info_size(class) > length
))
1814 lseek( fd
, (unsigned long)restart_pos
, SEEK_SET
);
1815 size
= (char *)de
- data
;
1816 io
->Information
= restart_info_pos
;
1817 last_info
= restart_last_info
;
1821 /* move on to the next entry */
1824 de
= (struct dirent
*)((char *)de
+ de
->d_reclen
);
1827 if (size
< initial_size
) break; /* already restarted once, give up now */
1828 size
= min( size
, length
- io
->Information
);
1829 /* if size is too small don't bother to continue */
1830 if (size
< max_dir_info_size(class) && last_info
) break;
1831 restart_last_info
= last_info
;
1832 restart_info_pos
= io
->Information
;
1834 res
= wine_getdirentries( fd
, data
, size
, &restart_pos
);
1835 de
= (struct dirent
*)data
;
1838 if (last_info
) last_info
->next
= 0;
1839 else io
->u
.Status
= restart_scan
? STATUS_NO_SUCH_FILE
: STATUS_NO_MORE_FILES
;
1842 if (data
!= local_buffer
) RtlFreeHeap( GetProcessHeap(), 0, data
);
1846 #ifdef _DARWIN_FEATURE_64_BIT_INODE
1847 #undef getdirentries
1851 #endif /* HAVE_GETDIRENTRIES */
1854 /***********************************************************************
1855 * read_directory_readdir
1857 * Read a directory using the POSIX readdir interface; helper for NtQueryDirectoryFile.
1859 static void read_directory_readdir( int fd
, IO_STATUS_BLOCK
*io
, void *buffer
, ULONG length
,
1860 BOOLEAN single_entry
, const UNICODE_STRING
*mask
,
1861 BOOLEAN restart_scan
, FILE_INFORMATION_CLASS
class )
1864 off_t i
, old_pos
= 0;
1866 union file_directory_info
*info
, *last_info
= NULL
;
1868 if (!(dir
= opendir( "." )))
1870 io
->u
.Status
= FILE_GetNtStatus();
1876 old_pos
= lseek( fd
, 0, SEEK_CUR
);
1877 /* skip the right number of entries */
1878 for (i
= 0; i
< old_pos
- 2; i
++)
1880 if (!readdir( dir
))
1883 io
->u
.Status
= STATUS_NO_MORE_FILES
;
1888 io
->u
.Status
= STATUS_SUCCESS
;
1893 info
= append_entry( buffer
, io
, length
, ".", NULL
, mask
, class );
1894 else if (old_pos
== 1)
1895 info
= append_entry( buffer
, io
, length
, "..", NULL
, mask
, class );
1896 else if ((de
= readdir( dir
)))
1898 if (strcmp( de
->d_name
, "." ) && strcmp( de
->d_name
, ".." ))
1899 info
= append_entry( buffer
, io
, length
, de
->d_name
, NULL
, mask
, class );
1909 if (io
->u
.Status
== STATUS_BUFFER_OVERFLOW
)
1911 old_pos
--; /* restore pos to previous entry */
1914 if (single_entry
) break;
1915 /* check if we still have enough space for the largest possible entry */
1916 if (io
->Information
+ max_dir_info_size(class) > length
) break;
1920 lseek( fd
, old_pos
, SEEK_SET
); /* store dir offset as filepos for fd */
1923 if (last_info
) last_info
->next
= 0;
1924 else io
->u
.Status
= restart_scan
? STATUS_NO_SUCH_FILE
: STATUS_NO_MORE_FILES
;
1927 /***********************************************************************
1928 * read_directory_stat
1930 * Read a single file from a directory by determining whether the file
1931 * identified by mask exists using stat.
1933 static int read_directory_stat( int fd
, IO_STATUS_BLOCK
*io
, void *buffer
, ULONG length
,
1934 BOOLEAN single_entry
, const UNICODE_STRING
*mask
,
1935 BOOLEAN restart_scan
, FILE_INFORMATION_CLASS
class )
1937 int unix_len
, ret
, used_default
;
1941 TRACE("trying optimisation for file %s\n", debugstr_us( mask
));
1943 unix_len
= ntdll_wcstoumbs( 0, mask
->Buffer
, mask
->Length
/ sizeof(WCHAR
), NULL
, 0, NULL
, NULL
);
1944 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, unix_len
+ 1)))
1946 io
->u
.Status
= STATUS_NO_MEMORY
;
1949 ret
= ntdll_wcstoumbs( 0, mask
->Buffer
, mask
->Length
/ sizeof(WCHAR
), unix_name
, unix_len
,
1950 NULL
, &used_default
);
1951 if (ret
> 0 && !used_default
)
1956 lseek( fd
, 0, SEEK_SET
);
1958 else if (lseek( fd
, 0, SEEK_CUR
) != 0)
1960 io
->u
.Status
= STATUS_NO_MORE_FILES
;
1965 ret
= stat( unix_name
, &st
);
1968 union file_directory_info
*info
= append_entry( buffer
, io
, length
, unix_name
, NULL
, NULL
, class );
1972 if (io
->u
.Status
!= STATUS_BUFFER_OVERFLOW
) lseek( fd
, 1, SEEK_CUR
);
1974 else io
->u
.Status
= STATUS_NO_MORE_FILES
;
1980 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
1982 TRACE("returning %d\n", ret
);
1988 static inline WCHAR
*mempbrkW( const WCHAR
*ptr
, const WCHAR
*accept
, size_t n
)
1991 for (end
= ptr
+ n
; ptr
< end
; ptr
++) if (strchrW( accept
, *ptr
)) return (WCHAR
*)ptr
;
1995 /******************************************************************************
1996 * NtQueryDirectoryFile [NTDLL.@]
1997 * ZwQueryDirectoryFile [NTDLL.@]
1999 NTSTATUS WINAPI
NtQueryDirectoryFile( HANDLE handle
, HANDLE event
,
2000 PIO_APC_ROUTINE apc_routine
, PVOID apc_context
,
2001 PIO_STATUS_BLOCK io
,
2002 PVOID buffer
, ULONG length
,
2003 FILE_INFORMATION_CLASS info_class
,
2004 BOOLEAN single_entry
,
2005 PUNICODE_STRING mask
,
2006 BOOLEAN restart_scan
)
2008 int cwd
, fd
, needs_close
;
2009 static const WCHAR wszWildcards
[] = { '*','?',0 };
2011 TRACE("(%p %p %p %p %p %p 0x%08x 0x%08x 0x%08x %s 0x%08x\n",
2012 handle
, event
, apc_routine
, apc_context
, io
, buffer
,
2013 length
, info_class
, single_entry
, debugstr_us(mask
),
2016 if (event
|| apc_routine
)
2018 FIXME( "Unsupported yet option\n" );
2019 return io
->u
.Status
= STATUS_NOT_IMPLEMENTED
;
2023 case FileDirectoryInformation
:
2024 case FileBothDirectoryInformation
:
2025 case FileFullDirectoryInformation
:
2026 case FileIdBothDirectoryInformation
:
2027 case FileIdFullDirectoryInformation
:
2028 if (length
< dir_info_size( info_class
, 1 )) return io
->u
.Status
= STATUS_INFO_LENGTH_MISMATCH
;
2031 FIXME( "Unsupported file info class %d\n", info_class
);
2032 return io
->u
.Status
= STATUS_NOT_IMPLEMENTED
;
2035 if ((io
->u
.Status
= server_get_unix_fd( handle
, FILE_LIST_DIRECTORY
, &fd
, &needs_close
, NULL
, NULL
)) != STATUS_SUCCESS
)
2036 return io
->u
.Status
;
2038 io
->Information
= 0;
2040 RtlEnterCriticalSection( &dir_section
);
2042 if (show_dot_files
== -1) init_options();
2044 cwd
= open( ".", O_RDONLY
);
2045 if (fchdir( fd
) != -1)
2049 curdir
.dev
= st
.st_dev
;
2050 curdir
.ino
= st
.st_ino
;
2051 #ifdef VFAT_IOCTL_READDIR_BOTH
2052 if ((read_directory_vfat( fd
, io
, buffer
, length
, single_entry
,
2053 mask
, restart_scan
, info_class
)) != -1) goto done
;
2055 if (mask
&& !mempbrkW( mask
->Buffer
, wszWildcards
, mask
->Length
/ sizeof(WCHAR
) ) &&
2056 read_directory_stat( fd
, io
, buffer
, length
, single_entry
,
2057 mask
, restart_scan
, info_class
) != -1) goto done
;
2059 if ((read_directory_getdents( fd
, io
, buffer
, length
, single_entry
,
2060 mask
, restart_scan
, info_class
)) != -1) goto done
;
2061 #elif defined HAVE_GETDIRENTRIES
2062 if ((read_directory_getdirentries( fd
, io
, buffer
, length
, single_entry
,
2063 mask
, restart_scan
, info_class
)) != -1) goto done
;
2065 read_directory_readdir( fd
, io
, buffer
, length
, single_entry
, mask
, restart_scan
, info_class
);
2068 if (cwd
== -1 || fchdir( cwd
) == -1) chdir( "/" );
2070 else io
->u
.Status
= FILE_GetNtStatus();
2072 RtlLeaveCriticalSection( &dir_section
);
2074 if (needs_close
) close( fd
);
2075 if (cwd
!= -1) close( cwd
);
2076 TRACE( "=> %x (%ld)\n", io
->u
.Status
, io
->Information
);
2077 return io
->u
.Status
;
2081 /***********************************************************************
2084 * Find a file in a directory the hard way, by doing a case-insensitive search.
2085 * The file found is appended to unix_name at pos.
2086 * There must be at least MAX_DIR_ENTRY_LEN+2 chars available at pos.
2088 static NTSTATUS
find_file_in_dir( char *unix_name
, int pos
, const WCHAR
*name
, int length
,
2089 int check_case
, int *is_win_dir
)
2091 WCHAR buffer
[MAX_DIR_ENTRY_LEN
];
2097 int ret
, used_default
, is_name_8_dot_3
;
2099 /* try a shortcut for this directory */
2101 unix_name
[pos
++] = '/';
2102 ret
= ntdll_wcstoumbs( 0, name
, length
, unix_name
+ pos
, MAX_DIR_ENTRY_LEN
,
2103 NULL
, &used_default
);
2104 /* if we used the default char, the Unix name won't round trip properly back to Unicode */
2105 /* so it cannot match the file we are looking for */
2106 if (ret
>= 0 && !used_default
)
2108 unix_name
[pos
+ ret
] = 0;
2109 if (!stat( unix_name
, &st
))
2111 if (is_win_dir
) *is_win_dir
= is_same_file( &windir
, &st
);
2112 return STATUS_SUCCESS
;
2115 if (check_case
) goto not_found
; /* we want an exact match */
2117 if (pos
> 1) unix_name
[pos
- 1] = 0;
2118 else unix_name
[1] = 0; /* keep the initial slash */
2120 /* check if it fits in 8.3 so that we don't look for short names if we won't need them */
2122 str
.Buffer
= (WCHAR
*)name
;
2123 str
.Length
= length
* sizeof(WCHAR
);
2124 str
.MaximumLength
= str
.Length
;
2125 is_name_8_dot_3
= RtlIsNameLegalDOS8Dot3( &str
, NULL
, &spaces
) && !spaces
;
2127 if (!is_name_8_dot_3
&& !get_dir_case_sensitivity( unix_name
)) goto not_found
;
2129 /* now look for it through the directory */
2131 #ifdef VFAT_IOCTL_READDIR_BOTH
2132 if (is_name_8_dot_3
)
2134 int fd
= open( unix_name
, O_RDONLY
| O_DIRECTORY
);
2139 RtlEnterCriticalSection( &dir_section
);
2140 if ((de
= start_vfat_ioctl( fd
)))
2142 unix_name
[pos
- 1] = '/';
2143 while (de
[0].d_reclen
)
2145 /* make sure names are null-terminated to work around an x86-64 kernel bug */
2146 size_t len
= min(de
[0].d_reclen
, sizeof(de
[0].d_name
) - 1 );
2147 de
[0].d_name
[len
] = 0;
2148 len
= min(de
[1].d_reclen
, sizeof(de
[1].d_name
) - 1 );
2149 de
[1].d_name
[len
] = 0;
2151 if (de
[1].d_name
[0])
2153 ret
= ntdll_umbstowcs( 0, de
[1].d_name
, strlen(de
[1].d_name
),
2154 buffer
, MAX_DIR_ENTRY_LEN
);
2155 if (ret
== length
&& !memicmpW( buffer
, name
, length
))
2157 strcpy( unix_name
+ pos
, de
[1].d_name
);
2158 RtlLeaveCriticalSection( &dir_section
);
2163 ret
= ntdll_umbstowcs( 0, de
[0].d_name
, strlen(de
[0].d_name
),
2164 buffer
, MAX_DIR_ENTRY_LEN
);
2165 if (ret
== length
&& !memicmpW( buffer
, name
, length
))
2167 strcpy( unix_name
+ pos
,
2168 de
[1].d_name
[0] ? de
[1].d_name
: de
[0].d_name
);
2169 RtlLeaveCriticalSection( &dir_section
);
2173 if (ioctl( fd
, VFAT_IOCTL_READDIR_BOTH
, (long)de
) == -1)
2175 RtlLeaveCriticalSection( &dir_section
);
2181 RtlLeaveCriticalSection( &dir_section
);
2184 /* fall through to normal handling */
2186 #endif /* VFAT_IOCTL_READDIR_BOTH */
2188 if (!(dir
= opendir( unix_name
)))
2190 if (errno
== ENOENT
) return STATUS_OBJECT_PATH_NOT_FOUND
;
2191 else return FILE_GetNtStatus();
2193 unix_name
[pos
- 1] = '/';
2194 str
.Buffer
= buffer
;
2195 str
.MaximumLength
= sizeof(buffer
);
2196 while ((de
= readdir( dir
)))
2198 ret
= ntdll_umbstowcs( 0, de
->d_name
, strlen(de
->d_name
), buffer
, MAX_DIR_ENTRY_LEN
);
2199 if (ret
== length
&& !memicmpW( buffer
, name
, length
))
2201 strcpy( unix_name
+ pos
, de
->d_name
);
2206 if (!is_name_8_dot_3
) continue;
2208 str
.Length
= ret
* sizeof(WCHAR
);
2209 if (!RtlIsNameLegalDOS8Dot3( &str
, NULL
, &spaces
) || spaces
)
2211 WCHAR short_nameW
[12];
2212 ret
= hash_short_file_name( &str
, short_nameW
);
2213 if (ret
== length
&& !memicmpW( short_nameW
, name
, length
))
2215 strcpy( unix_name
+ pos
, de
->d_name
);
2224 unix_name
[pos
- 1] = 0;
2225 return STATUS_OBJECT_PATH_NOT_FOUND
;
2228 if (is_win_dir
&& !stat( unix_name
, &st
)) *is_win_dir
= is_same_file( &windir
, &st
);
2229 return STATUS_SUCCESS
;
2235 static const WCHAR catrootW
[] = {'s','y','s','t','e','m','3','2','\\','c','a','t','r','o','o','t',0};
2236 static const WCHAR catroot2W
[] = {'s','y','s','t','e','m','3','2','\\','c','a','t','r','o','o','t','2',0};
2237 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};
2238 static const WCHAR driversetcW
[] = {'s','y','s','t','e','m','3','2','\\','d','r','i','v','e','r','s','\\','e','t','c',0};
2239 static const WCHAR logfilesW
[] = {'s','y','s','t','e','m','3','2','\\','l','o','g','f','i','l','e','s',0};
2240 static const WCHAR spoolW
[] = {'s','y','s','t','e','m','3','2','\\','s','p','o','o','l',0};
2241 static const WCHAR system32W
[] = {'s','y','s','t','e','m','3','2',0};
2242 static const WCHAR syswow64W
[] = {'s','y','s','w','o','w','6','4',0};
2243 static const WCHAR sysnativeW
[] = {'s','y','s','n','a','t','i','v','e',0};
2244 static const WCHAR regeditW
[] = {'r','e','g','e','d','i','t','.','e','x','e',0};
2245 static const WCHAR wow_regeditW
[] = {'s','y','s','w','o','w','6','4','\\','r','e','g','e','d','i','t','.','e','x','e',0};
2249 const WCHAR
*source
;
2250 const WCHAR
*dos_target
;
2251 const char *unix_target
;
2254 { catrootW
, NULL
, NULL
},
2255 { catroot2W
, NULL
, NULL
},
2256 { driversstoreW
, NULL
, NULL
},
2257 { driversetcW
, NULL
, NULL
},
2258 { logfilesW
, NULL
, NULL
},
2259 { spoolW
, NULL
, NULL
},
2260 { system32W
, syswow64W
, NULL
},
2261 { sysnativeW
, system32W
, NULL
},
2262 { regeditW
, wow_regeditW
, NULL
}
2265 static unsigned int nb_redirects
;
2268 /***********************************************************************
2269 * get_redirect_target
2271 * Find the target unix name for a redirected dir.
2273 static const char *get_redirect_target( const char *windows_dir
, const WCHAR
*name
)
2275 int used_default
, len
, pos
, win_len
= strlen( windows_dir
);
2276 char *unix_name
, *unix_target
= NULL
;
2279 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, win_len
+ MAX_DIR_ENTRY_LEN
+ 2 )))
2281 memcpy( unix_name
, windows_dir
, win_len
);
2286 const WCHAR
*end
, *next
;
2288 for (end
= name
; *end
; end
++) if (IS_SEPARATOR(*end
)) break;
2289 for (next
= end
; *next
; next
++) if (!IS_SEPARATOR(*next
)) break;
2291 status
= find_file_in_dir( unix_name
, pos
, name
, end
- name
, FALSE
, NULL
);
2292 if (status
== STATUS_OBJECT_PATH_NOT_FOUND
&& !*next
) /* not finding last element is ok */
2294 len
= ntdll_wcstoumbs( 0, name
, end
- name
, unix_name
+ pos
+ 1,
2295 MAX_DIR_ENTRY_LEN
- (pos
- win_len
), NULL
, &used_default
);
2296 if (len
> 0 && !used_default
)
2298 unix_name
[pos
] = '/';
2304 if (status
) goto done
;
2305 pos
+= strlen( unix_name
+ pos
);
2309 if ((unix_target
= RtlAllocateHeap( GetProcessHeap(), 0, pos
- win_len
)))
2310 memcpy( unix_target
, unix_name
+ win_len
+ 1, pos
- win_len
);
2313 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2318 /***********************************************************************
2321 static void init_redirects(void)
2323 UNICODE_STRING nt_name
;
2324 ANSI_STRING unix_name
;
2329 if (!RtlDosPathNameToNtPathName_U( user_shared_data
->NtSystemRoot
, &nt_name
, NULL
, NULL
))
2331 ERR( "can't convert %s\n", debugstr_w(user_shared_data
->NtSystemRoot
) );
2334 status
= wine_nt_to_unix_file_name( &nt_name
, &unix_name
, FILE_OPEN_IF
, FALSE
);
2335 RtlFreeUnicodeString( &nt_name
);
2338 ERR( "cannot open %s (%x)\n", debugstr_w(user_shared_data
->NtSystemRoot
), status
);
2341 if (!stat( unix_name
.Buffer
, &st
))
2343 windir
.dev
= st
.st_dev
;
2344 windir
.ino
= st
.st_ino
;
2345 nb_redirects
= sizeof(redirects
) / sizeof(redirects
[0]);
2346 for (i
= 0; i
< nb_redirects
; i
++)
2348 if (!redirects
[i
].dos_target
) continue;
2349 redirects
[i
].unix_target
= get_redirect_target( unix_name
.Buffer
, redirects
[i
].dos_target
);
2350 TRACE( "%s -> %s\n", debugstr_w(redirects
[i
].source
), redirects
[i
].unix_target
);
2353 RtlFreeAnsiString( &unix_name
);
2358 /***********************************************************************
2361 * Check if path matches a redirect name. If yes, return matched length.
2363 static int match_redirect( const WCHAR
*path
, int len
, const WCHAR
*redir
, int check_case
)
2367 while (i
< len
&& *redir
)
2369 if (IS_SEPARATOR(path
[i
]))
2371 if (*redir
++ != '\\') return 0;
2372 while (i
< len
&& IS_SEPARATOR(path
[i
])) i
++;
2373 continue; /* move on to next path component */
2375 else if (check_case
)
2377 if (path
[i
] != *redir
) return 0;
2381 if (tolowerW(path
[i
]) != tolowerW(*redir
)) return 0;
2386 if (*redir
) return 0;
2387 if (i
< len
&& !IS_SEPARATOR(path
[i
])) return 0;
2388 while (i
< len
&& IS_SEPARATOR(path
[i
])) i
++;
2393 /***********************************************************************
2396 * Retrieve the Unix path corresponding to a redirected path if any.
2398 static int get_redirect_path( char *unix_name
, int pos
, const WCHAR
*name
, int length
, int check_case
)
2403 for (i
= 0; i
< nb_redirects
; i
++)
2405 if ((len
= match_redirect( name
, length
, redirects
[i
].source
, check_case
)))
2407 if (!redirects
[i
].unix_target
) break;
2408 unix_name
[pos
++] = '/';
2409 strcpy( unix_name
+ pos
, redirects
[i
].unix_target
);
2418 /* there are no redirects on 64-bit */
2420 static const unsigned int nb_redirects
= 0;
2422 static int get_redirect_path( char *unix_name
, int pos
, const WCHAR
*name
, int length
, int check_case
)
2429 /***********************************************************************
2430 * DIR_init_windows_dir
2432 void DIR_init_windows_dir( const WCHAR
*win
, const WCHAR
*sys
)
2434 /* FIXME: should probably store paths as NT file names */
2436 RtlCreateUnicodeString( &system_dir
, sys
);
2439 if (is_wow64
) init_redirects();
2444 /******************************************************************************
2447 * Get the Unix path of a DOS device.
2449 static NTSTATUS
get_dos_device( const WCHAR
*name
, UINT name_len
, ANSI_STRING
*unix_name_ret
)
2451 const char *config_dir
= wine_get_config_dir();
2453 char *unix_name
, *new_name
, *dev
;
2457 /* make sure the device name is ASCII */
2458 for (i
= 0; i
< name_len
; i
++)
2459 if (name
[i
] <= 32 || name
[i
] >= 127) return STATUS_BAD_DEVICE_TYPE
;
2461 unix_len
= strlen(config_dir
) + sizeof("/dosdevices/") + name_len
+ 1;
2463 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, unix_len
)))
2464 return STATUS_NO_MEMORY
;
2466 strcpy( unix_name
, config_dir
);
2467 strcat( unix_name
, "/dosdevices/" );
2468 dev
= unix_name
+ strlen(unix_name
);
2470 for (i
= 0; i
< name_len
; i
++) dev
[i
] = (char)tolowerW(name
[i
]);
2473 /* special case for drive devices */
2474 if (name_len
== 2 && dev
[1] == ':')
2482 if (!stat( unix_name
, &st
))
2484 TRACE( "%s -> %s\n", debugstr_wn(name
,name_len
), debugstr_a(unix_name
) );
2485 unix_name_ret
->Buffer
= unix_name
;
2486 unix_name_ret
->Length
= strlen(unix_name
);
2487 unix_name_ret
->MaximumLength
= unix_len
;
2488 return STATUS_SUCCESS
;
2492 /* now try some defaults for it */
2493 if (!strcmp( dev
, "aux" ))
2495 strcpy( dev
, "com1" );
2498 if (!strcmp( dev
, "prn" ))
2500 strcpy( dev
, "lpt1" );
2503 if (!strcmp( dev
, "nul" ))
2505 strcpy( unix_name
, "/dev/null" );
2506 dev
= NULL
; /* last try */
2511 if (dev
[1] == ':' && dev
[2] == ':') /* drive device */
2513 dev
[2] = 0; /* remove last ':' to get the drive mount point symlink */
2514 new_name
= get_default_drive_device( unix_name
);
2516 else if (!strncmp( dev
, "com", 3 )) new_name
= get_default_com_device( atoi(dev
+ 3 ));
2517 else if (!strncmp( dev
, "lpt", 3 )) new_name
= get_default_lpt_device( atoi(dev
+ 3 ));
2519 if (!new_name
) break;
2521 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2522 unix_name
= new_name
;
2523 unix_len
= strlen(unix_name
) + 1;
2524 dev
= NULL
; /* last try */
2526 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2527 return STATUS_BAD_DEVICE_TYPE
;
2531 /* return the length of the DOS namespace prefix if any */
2532 static inline int get_dos_prefix_len( const UNICODE_STRING
*name
)
2534 static const WCHAR nt_prefixW
[] = {'\\','?','?','\\'};
2535 static const WCHAR dosdev_prefixW
[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\'};
2537 if (name
->Length
> sizeof(nt_prefixW
) &&
2538 !memcmp( name
->Buffer
, nt_prefixW
, sizeof(nt_prefixW
) ))
2539 return sizeof(nt_prefixW
) / sizeof(WCHAR
);
2541 if (name
->Length
> sizeof(dosdev_prefixW
) &&
2542 !memicmpW( name
->Buffer
, dosdev_prefixW
, sizeof(dosdev_prefixW
)/sizeof(WCHAR
) ))
2543 return sizeof(dosdev_prefixW
) / sizeof(WCHAR
);
2549 /******************************************************************************
2552 * Recursively search directories from the dir queue for a given inode.
2554 static NTSTATUS
find_file_id( ANSI_STRING
*unix_name
, ULONGLONG file_id
, dev_t dev
)
2562 while (!(status
= next_dir_in_queue( unix_name
->Buffer
)))
2564 if (!(dir
= opendir( unix_name
->Buffer
))) continue;
2565 TRACE( "searching %s for %s\n", unix_name
->Buffer
, wine_dbgstr_longlong(file_id
) );
2566 pos
= strlen( unix_name
->Buffer
);
2567 if (pos
+ MAX_DIR_ENTRY_LEN
>= unix_name
->MaximumLength
/sizeof(WCHAR
))
2569 char *new = RtlReAllocateHeap( GetProcessHeap(), 0, unix_name
->Buffer
,
2570 unix_name
->MaximumLength
* 2 );
2574 return STATUS_NO_MEMORY
;
2576 unix_name
->MaximumLength
*= 2;
2577 unix_name
->Buffer
= new;
2579 unix_name
->Buffer
[pos
++] = '/';
2580 while ((de
= readdir( dir
)))
2582 if (!strcmp( de
->d_name
, "." ) || !strcmp( de
->d_name
, ".." )) continue;
2583 strcpy( unix_name
->Buffer
+ pos
, de
->d_name
);
2584 if (lstat( unix_name
->Buffer
, &st
) == -1) continue;
2585 if (st
.st_dev
!= dev
) continue;
2586 if (st
.st_ino
== file_id
)
2589 return STATUS_SUCCESS
;
2591 if (!S_ISDIR( st
.st_mode
)) continue;
2592 if ((status
= add_dir_to_queue( unix_name
->Buffer
)) != STATUS_SUCCESS
)
2604 /******************************************************************************
2605 * file_id_to_unix_file_name
2607 * Lookup a file from its file id instead of its name.
2609 NTSTATUS
file_id_to_unix_file_name( const OBJECT_ATTRIBUTES
*attr
, ANSI_STRING
*unix_name
)
2611 enum server_fd_type type
;
2612 int old_cwd
, root_fd
, needs_close
;
2615 struct stat st
, root_st
;
2617 if (attr
->ObjectName
->Length
!= sizeof(ULONGLONG
)) return STATUS_OBJECT_PATH_SYNTAX_BAD
;
2618 if (!attr
->RootDirectory
) return STATUS_INVALID_PARAMETER
;
2619 memcpy( &file_id
, attr
->ObjectName
->Buffer
, sizeof(file_id
) );
2621 unix_name
->MaximumLength
= 2 * MAX_DIR_ENTRY_LEN
+ 4;
2622 if (!(unix_name
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, unix_name
->MaximumLength
)))
2623 return STATUS_NO_MEMORY
;
2624 strcpy( unix_name
->Buffer
, "." );
2626 if ((status
= server_get_unix_fd( attr
->RootDirectory
, 0, &root_fd
, &needs_close
, &type
, NULL
)))
2629 if (type
!= FD_TYPE_DIR
)
2631 status
= STATUS_OBJECT_TYPE_MISMATCH
;
2635 fstat( root_fd
, &root_st
);
2636 if (root_st
.st_ino
== file_id
) /* shortcut for "." */
2638 status
= STATUS_SUCCESS
;
2642 RtlEnterCriticalSection( &dir_section
);
2643 if ((old_cwd
= open( ".", O_RDONLY
)) != -1 && fchdir( root_fd
) != -1)
2645 /* shortcut for ".." */
2646 if (!stat( "..", &st
) && st
.st_dev
== root_st
.st_dev
&& st
.st_ino
== file_id
)
2648 strcpy( unix_name
->Buffer
, ".." );
2649 status
= STATUS_SUCCESS
;
2653 status
= add_dir_to_queue( "." );
2655 status
= find_file_id( unix_name
, file_id
, root_st
.st_dev
);
2656 if (!status
) /* get rid of "./" prefix */
2657 memmove( unix_name
->Buffer
, unix_name
->Buffer
+ 2, strlen(unix_name
->Buffer
) - 1 );
2660 if (fchdir( old_cwd
) == -1) chdir( "/" );
2662 else status
= FILE_GetNtStatus();
2663 RtlLeaveCriticalSection( &dir_section
);
2664 if (old_cwd
!= -1) close( old_cwd
);
2667 if (status
== STATUS_SUCCESS
)
2669 TRACE( "%s -> %s\n", wine_dbgstr_longlong(file_id
), debugstr_a(unix_name
->Buffer
) );
2670 unix_name
->Length
= strlen( unix_name
->Buffer
);
2674 TRACE( "%s not found in dir %p\n", wine_dbgstr_longlong(file_id
), attr
->RootDirectory
);
2675 RtlFreeHeap( GetProcessHeap(), 0, unix_name
->Buffer
);
2677 if (needs_close
) close( root_fd
);
2682 /******************************************************************************
2685 * Helper for nt_to_unix_file_name
2687 static NTSTATUS
lookup_unix_name( const WCHAR
*name
, int name_len
, char **buffer
, int unix_len
, int pos
,
2688 UINT disposition
, BOOLEAN check_case
)
2691 int ret
, used_default
, len
;
2693 char *unix_name
= *buffer
;
2694 const BOOL redirect
= nb_redirects
&& ntdll_get_thread_data()->wow64_redir
;
2696 /* try a shortcut first */
2698 ret
= ntdll_wcstoumbs( 0, name
, name_len
, unix_name
+ pos
, unix_len
- pos
- 1,
2699 NULL
, &used_default
);
2701 while (name_len
&& IS_SEPARATOR(*name
))
2707 if (ret
>= 0 && !used_default
) /* if we used the default char the name didn't convert properly */
2710 unix_name
[pos
+ ret
] = 0;
2711 for (p
= unix_name
+ pos
; *p
; p
++) if (*p
== '\\') *p
= '/';
2712 if (!redirect
|| (!strstr( unix_name
, "/windows/") && strncmp( unix_name
, "windows/", 8 )))
2714 if (!stat( unix_name
, &st
))
2716 /* creation fails with STATUS_ACCESS_DENIED for the root of the drive */
2717 if (disposition
== FILE_CREATE
)
2718 return name_len
? STATUS_OBJECT_NAME_COLLISION
: STATUS_ACCESS_DENIED
;
2719 return STATUS_SUCCESS
;
2724 if (!name_len
) /* empty name -> drive root doesn't exist */
2725 return STATUS_OBJECT_PATH_NOT_FOUND
;
2726 if (check_case
&& !redirect
&& (disposition
== FILE_OPEN
|| disposition
== FILE_OVERWRITE
))
2727 return STATUS_OBJECT_NAME_NOT_FOUND
;
2729 /* now do it component by component */
2733 const WCHAR
*end
, *next
;
2737 while (end
< name
+ name_len
&& !IS_SEPARATOR(*end
)) end
++;
2739 while (next
< name
+ name_len
&& IS_SEPARATOR(*next
)) next
++;
2740 name_len
-= next
- name
;
2742 /* grow the buffer if needed */
2744 if (unix_len
- pos
< MAX_DIR_ENTRY_LEN
+ 2)
2747 unix_len
+= 2 * MAX_DIR_ENTRY_LEN
;
2748 if (!(new_name
= RtlReAllocateHeap( GetProcessHeap(), 0, unix_name
, unix_len
)))
2749 return STATUS_NO_MEMORY
;
2750 unix_name
= *buffer
= new_name
;
2753 status
= find_file_in_dir( unix_name
, pos
, name
, end
- name
,
2754 check_case
, redirect
? &is_win_dir
: NULL
);
2756 /* if this is the last element, not finding it is not necessarily fatal */
2759 if (status
== STATUS_OBJECT_PATH_NOT_FOUND
)
2761 status
= STATUS_OBJECT_NAME_NOT_FOUND
;
2762 if (disposition
!= FILE_OPEN
&& disposition
!= FILE_OVERWRITE
)
2764 ret
= ntdll_wcstoumbs( 0, name
, end
- name
, unix_name
+ pos
+ 1,
2765 MAX_DIR_ENTRY_LEN
, NULL
, &used_default
);
2766 if (ret
> 0 && !used_default
)
2768 unix_name
[pos
] = '/';
2769 unix_name
[pos
+ 1 + ret
] = 0;
2770 status
= STATUS_NO_SUCH_FILE
;
2775 else if (status
== STATUS_SUCCESS
&& disposition
== FILE_CREATE
)
2777 status
= STATUS_OBJECT_NAME_COLLISION
;
2781 if (status
!= STATUS_SUCCESS
) break;
2783 pos
+= strlen( unix_name
+ pos
);
2786 if (is_win_dir
&& (len
= get_redirect_path( unix_name
, pos
, name
, name_len
, check_case
)))
2790 pos
+= strlen( unix_name
+ pos
);
2791 TRACE( "redirecting -> %s + %s\n", debugstr_a(unix_name
), debugstr_w(name
) );
2799 /******************************************************************************
2800 * nt_to_unix_file_name_attr
2802 NTSTATUS
nt_to_unix_file_name_attr( const OBJECT_ATTRIBUTES
*attr
, ANSI_STRING
*unix_name_ret
,
2805 static const WCHAR invalid_charsW
[] = { INVALID_NT_CHARS
, 0 };
2806 enum server_fd_type type
;
2807 int old_cwd
, root_fd
, needs_close
;
2808 const WCHAR
*name
, *p
;
2810 int name_len
, unix_len
;
2812 BOOLEAN check_case
= !(attr
->Attributes
& OBJ_CASE_INSENSITIVE
);
2814 if (!attr
->RootDirectory
) /* without root dir fall back to normal lookup */
2815 return wine_nt_to_unix_file_name( attr
->ObjectName
, unix_name_ret
, disposition
, check_case
);
2817 name
= attr
->ObjectName
->Buffer
;
2818 name_len
= attr
->ObjectName
->Length
/ sizeof(WCHAR
);
2820 if (name_len
&& IS_SEPARATOR(name
[0])) return STATUS_INVALID_PARAMETER
;
2822 /* check for invalid characters */
2823 for (p
= name
; p
< name
+ name_len
; p
++)
2824 if (*p
< 32 || strchrW( invalid_charsW
, *p
)) return STATUS_OBJECT_NAME_INVALID
;
2826 unix_len
= ntdll_wcstoumbs( 0, name
, name_len
, NULL
, 0, NULL
, NULL
);
2827 unix_len
+= MAX_DIR_ENTRY_LEN
+ 3;
2828 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, unix_len
)))
2829 return STATUS_NO_MEMORY
;
2832 if (!(status
= server_get_unix_fd( attr
->RootDirectory
, 0, &root_fd
, &needs_close
, &type
, NULL
)))
2834 if (type
!= FD_TYPE_DIR
)
2836 if (needs_close
) close( root_fd
);
2837 status
= STATUS_BAD_DEVICE_TYPE
;
2841 RtlEnterCriticalSection( &dir_section
);
2842 if ((old_cwd
= open( ".", O_RDONLY
)) != -1 && fchdir( root_fd
) != -1)
2844 status
= lookup_unix_name( name
, name_len
, &unix_name
, unix_len
, 1,
2845 disposition
, check_case
);
2846 if (fchdir( old_cwd
) == -1) chdir( "/" );
2848 else status
= FILE_GetNtStatus();
2849 RtlLeaveCriticalSection( &dir_section
);
2850 if (old_cwd
!= -1) close( old_cwd
);
2851 if (needs_close
) close( root_fd
);
2854 else if (status
== STATUS_OBJECT_TYPE_MISMATCH
) status
= STATUS_BAD_DEVICE_TYPE
;
2856 if (status
== STATUS_SUCCESS
|| status
== STATUS_NO_SUCH_FILE
)
2858 TRACE( "%s -> %s\n", debugstr_us(attr
->ObjectName
), debugstr_a(unix_name
) );
2859 unix_name_ret
->Buffer
= unix_name
;
2860 unix_name_ret
->Length
= strlen(unix_name
);
2861 unix_name_ret
->MaximumLength
= unix_len
;
2865 TRACE( "%s not found in %s\n", debugstr_w(name
), unix_name
);
2866 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2872 /******************************************************************************
2873 * wine_nt_to_unix_file_name (NTDLL.@) Not a Windows API
2875 * Convert a file name from NT namespace to Unix namespace.
2877 * If disposition is not FILE_OPEN or FILE_OVERWRITE, the last path
2878 * element doesn't have to exist; in that case STATUS_NO_SUCH_FILE is
2879 * returned, but the unix name is still filled in properly.
2881 NTSTATUS CDECL
wine_nt_to_unix_file_name( const UNICODE_STRING
*nameW
, ANSI_STRING
*unix_name_ret
,
2882 UINT disposition
, BOOLEAN check_case
)
2884 static const WCHAR unixW
[] = {'u','n','i','x'};
2885 static const WCHAR invalid_charsW
[] = { INVALID_NT_CHARS
, 0 };
2887 NTSTATUS status
= STATUS_SUCCESS
;
2888 const char *config_dir
= wine_get_config_dir();
2889 const WCHAR
*name
, *p
;
2892 int pos
, ret
, name_len
, unix_len
, prefix_len
, used_default
;
2893 WCHAR prefix
[MAX_DIR_ENTRY_LEN
];
2894 BOOLEAN is_unix
= FALSE
;
2896 name
= nameW
->Buffer
;
2897 name_len
= nameW
->Length
/ sizeof(WCHAR
);
2899 if (!name_len
|| !IS_SEPARATOR(name
[0])) return STATUS_OBJECT_PATH_SYNTAX_BAD
;
2901 if (!(pos
= get_dos_prefix_len( nameW
)))
2902 return STATUS_BAD_DEVICE_TYPE
; /* no DOS prefix, assume NT native name */
2907 /* check for sub-directory */
2908 for (pos
= 0; pos
< name_len
; pos
++)
2910 if (IS_SEPARATOR(name
[pos
])) break;
2911 if (name
[pos
] < 32 || strchrW( invalid_charsW
, name
[pos
] ))
2912 return STATUS_OBJECT_NAME_INVALID
;
2914 if (pos
> MAX_DIR_ENTRY_LEN
)
2915 return STATUS_OBJECT_NAME_INVALID
;
2917 if (pos
== name_len
) /* no subdir, plain DOS device */
2918 return get_dos_device( name
, name_len
, unix_name_ret
);
2920 for (prefix_len
= 0; prefix_len
< pos
; prefix_len
++)
2921 prefix
[prefix_len
] = tolowerW(name
[prefix_len
]);
2924 name_len
-= prefix_len
;
2926 /* check for invalid characters (all chars except 0 are valid for unix) */
2927 is_unix
= (prefix_len
== 4 && !memcmp( prefix
, unixW
, sizeof(unixW
) ));
2930 for (p
= name
; p
< name
+ name_len
; p
++)
2931 if (!*p
) return STATUS_OBJECT_NAME_INVALID
;
2936 for (p
= name
; p
< name
+ name_len
; p
++)
2937 if (*p
< 32 || strchrW( invalid_charsW
, *p
)) return STATUS_OBJECT_NAME_INVALID
;
2940 unix_len
= ntdll_wcstoumbs( 0, prefix
, prefix_len
, NULL
, 0, NULL
, NULL
);
2941 unix_len
+= ntdll_wcstoumbs( 0, name
, name_len
, NULL
, 0, NULL
, NULL
);
2942 unix_len
+= MAX_DIR_ENTRY_LEN
+ 3;
2943 unix_len
+= strlen(config_dir
) + sizeof("/dosdevices/");
2944 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, unix_len
)))
2945 return STATUS_NO_MEMORY
;
2946 strcpy( unix_name
, config_dir
);
2947 strcat( unix_name
, "/dosdevices/" );
2948 pos
= strlen(unix_name
);
2950 ret
= ntdll_wcstoumbs( 0, prefix
, prefix_len
, unix_name
+ pos
, unix_len
- pos
- 1,
2951 NULL
, &used_default
);
2952 if (!ret
|| used_default
)
2954 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2955 return STATUS_OBJECT_NAME_INVALID
;
2959 /* check if prefix exists (except for DOS drives to avoid extra stat calls) */
2961 if (prefix_len
!= 2 || prefix
[1] != ':')
2964 if (lstat( unix_name
, &st
) == -1 && errno
== ENOENT
)
2968 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2969 return STATUS_BAD_DEVICE_TYPE
;
2971 pos
= 0; /* fall back to unix root */
2975 status
= lookup_unix_name( name
, name_len
, &unix_name
, unix_len
, pos
, disposition
, check_case
);
2976 if (status
== STATUS_SUCCESS
|| status
== STATUS_NO_SUCH_FILE
)
2978 TRACE( "%s -> %s\n", debugstr_us(nameW
), debugstr_a(unix_name
) );
2979 unix_name_ret
->Buffer
= unix_name
;
2980 unix_name_ret
->Length
= strlen(unix_name
);
2981 unix_name_ret
->MaximumLength
= unix_len
;
2985 TRACE( "%s not found in %s\n", debugstr_w(name
), unix_name
);
2986 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2992 /******************************************************************
2993 * RtlWow64EnableFsRedirection (NTDLL.@)
2995 NTSTATUS WINAPI
RtlWow64EnableFsRedirection( BOOLEAN enable
)
2997 if (!is_wow64
) return STATUS_NOT_IMPLEMENTED
;
2998 ntdll_get_thread_data()->wow64_redir
= enable
;
2999 return STATUS_SUCCESS
;
3003 /******************************************************************
3004 * RtlWow64EnableFsRedirectionEx (NTDLL.@)
3006 NTSTATUS WINAPI
RtlWow64EnableFsRedirectionEx( ULONG disable
, ULONG
*old_value
)
3008 if (!is_wow64
) return STATUS_NOT_IMPLEMENTED
;
3009 *old_value
= !ntdll_get_thread_data()->wow64_redir
;
3010 ntdll_get_thread_data()->wow64_redir
= !disable
;
3011 return STATUS_SUCCESS
;
3015 /******************************************************************
3016 * RtlDoesFileExists_U (NTDLL.@)
3018 BOOLEAN WINAPI
RtlDoesFileExists_U(LPCWSTR file_name
)
3020 UNICODE_STRING nt_name
;
3021 FILE_BASIC_INFORMATION basic_info
;
3022 OBJECT_ATTRIBUTES attr
;
3025 if (!RtlDosPathNameToNtPathName_U( file_name
, &nt_name
, NULL
, NULL
)) return FALSE
;
3027 attr
.Length
= sizeof(attr
);
3028 attr
.RootDirectory
= 0;
3029 attr
.ObjectName
= &nt_name
;
3030 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
3031 attr
.SecurityDescriptor
= NULL
;
3032 attr
.SecurityQualityOfService
= NULL
;
3034 ret
= NtQueryAttributesFile(&attr
, &basic_info
) == STATUS_SUCCESS
;
3036 RtlFreeUnicodeString( &nt_name
);
3041 /***********************************************************************
3042 * DIR_unmount_device
3044 * Unmount the specified device.
3046 NTSTATUS
DIR_unmount_device( HANDLE handle
)
3049 int unix_fd
, needs_close
;
3051 if (!(status
= server_get_unix_fd( handle
, 0, &unix_fd
, &needs_close
, NULL
, NULL
)))
3054 char *mount_point
= NULL
;
3056 if (fstat( unix_fd
, &st
) == -1 || !is_valid_mounted_device( &st
))
3057 status
= STATUS_INVALID_PARAMETER
;
3060 if ((mount_point
= get_device_mount_point( st
.st_rdev
)))
3063 static const char umount
[] = "diskutil unmount >/dev/null 2>&1 ";
3065 static const char umount
[] = "umount >/dev/null 2>&1 ";
3067 char *cmd
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(mount_point
)+sizeof(umount
));
3070 strcpy( cmd
, umount
);
3071 strcat( cmd
, mount_point
);
3073 RtlFreeHeap( GetProcessHeap(), 0, cmd
);
3075 /* umount will fail to release the loop device since we still have
3076 a handle to it, so we release it here */
3077 if (major(st
.st_rdev
) == LOOP_MAJOR
) ioctl( unix_fd
, 0x4c01 /*LOOP_CLR_FD*/, 0 );
3080 RtlFreeHeap( GetProcessHeap(), 0, mount_point
);
3083 if (needs_close
) close( unix_fd
);
3089 /******************************************************************************
3092 * Retrieve the Unix name of the current directory; helper for wine_unix_to_nt_file_name.
3093 * Returned value must be freed by caller.
3095 NTSTATUS
DIR_get_unix_cwd( char **cwd
)
3097 int old_cwd
, unix_fd
, needs_close
;
3102 RtlAcquirePebLock();
3104 if (NtCurrentTeb()->Tib
.SubSystemTib
) /* FIXME: hack */
3105 curdir
= &((WIN16_SUBSYSTEM_TIB
*)NtCurrentTeb()->Tib
.SubSystemTib
)->curdir
;
3107 curdir
= &NtCurrentTeb()->Peb
->ProcessParameters
->CurrentDirectory
;
3109 if (!(handle
= curdir
->Handle
))
3111 UNICODE_STRING dirW
;
3112 OBJECT_ATTRIBUTES attr
;
3115 if (!RtlDosPathNameToNtPathName_U( curdir
->DosPath
.Buffer
, &dirW
, NULL
, NULL
))
3117 status
= STATUS_OBJECT_NAME_INVALID
;
3120 attr
.Length
= sizeof(attr
);
3121 attr
.RootDirectory
= 0;
3122 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
3123 attr
.ObjectName
= &dirW
;
3124 attr
.SecurityDescriptor
= NULL
;
3125 attr
.SecurityQualityOfService
= NULL
;
3127 status
= NtOpenFile( &handle
, 0, &attr
, &io
, 0,
3128 FILE_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
);
3129 RtlFreeUnicodeString( &dirW
);
3130 if (status
!= STATUS_SUCCESS
) goto done
;
3133 if ((status
= server_get_unix_fd( handle
, 0, &unix_fd
, &needs_close
, NULL
, NULL
)) == STATUS_SUCCESS
)
3135 RtlEnterCriticalSection( &dir_section
);
3137 if ((old_cwd
= open(".", O_RDONLY
)) != -1 && fchdir( unix_fd
) != -1)
3139 unsigned int size
= 512;
3143 if (!(*cwd
= RtlAllocateHeap( GetProcessHeap(), 0, size
)))
3145 status
= STATUS_NO_MEMORY
;
3148 if (getcwd( *cwd
, size
)) break;
3149 RtlFreeHeap( GetProcessHeap(), 0, *cwd
);
3150 if (errno
!= ERANGE
)
3152 status
= STATUS_OBJECT_PATH_INVALID
;
3157 if (fchdir( old_cwd
) == -1) chdir( "/" );
3159 else status
= FILE_GetNtStatus();
3161 RtlLeaveCriticalSection( &dir_section
);
3162 if (old_cwd
!= -1) close( old_cwd
);
3163 if (needs_close
) close( unix_fd
);
3165 if (!curdir
->Handle
) NtClose( handle
);
3168 RtlReleasePebLock();
3172 struct read_changes_info
3177 PIO_APC_ROUTINE apc
;
3181 /* callback for ioctl user APC */
3182 static void WINAPI
read_changes_user_apc( void *arg
, IO_STATUS_BLOCK
*io
, ULONG reserved
)
3184 struct read_changes_info
*info
= arg
;
3185 if (info
->apc
) info
->apc( info
->apc_arg
, io
, reserved
);
3186 RtlFreeHeap( GetProcessHeap(), 0, info
);
3189 static NTSTATUS
read_changes_apc( void *user
, PIO_STATUS_BLOCK iosb
, NTSTATUS status
, void **apc
)
3191 struct read_changes_info
*info
= user
;
3192 char data
[PATH_MAX
];
3196 SERVER_START_REQ( read_change
)
3198 req
->handle
= wine_server_obj_handle( info
->FileHandle
);
3199 wine_server_set_reply( req
, data
, PATH_MAX
);
3200 ret
= wine_server_call( req
);
3201 size
= wine_server_reply_size( reply
);
3205 if (ret
== STATUS_SUCCESS
&& info
->Buffer
)
3207 PFILE_NOTIFY_INFORMATION pfni
= info
->Buffer
;
3208 int i
, left
= info
->BufferSize
;
3209 DWORD
*last_entry_offset
= NULL
;
3210 struct filesystem_event
*event
= (struct filesystem_event
*)data
;
3212 while (size
&& left
>= sizeof(*pfni
))
3214 /* convert to an NT style path */
3215 for (i
=0; i
<event
->len
; i
++)
3216 if (event
->name
[i
] == '/')
3217 event
->name
[i
] = '\\';
3219 pfni
->Action
= event
->action
;
3220 pfni
->FileNameLength
= ntdll_umbstowcs( 0, event
->name
, event
->len
, pfni
->FileName
,
3221 (left
- offsetof(FILE_NOTIFY_INFORMATION
, FileName
)) / sizeof(WCHAR
));
3222 last_entry_offset
= &pfni
->NextEntryOffset
;
3224 if(pfni
->FileNameLength
== -1 || pfni
->FileNameLength
== -2)
3227 i
= offsetof(FILE_NOTIFY_INFORMATION
, FileName
[pfni
->FileNameLength
]);
3228 pfni
->FileNameLength
*= sizeof(WCHAR
);
3229 pfni
->NextEntryOffset
= i
;
3230 pfni
= (FILE_NOTIFY_INFORMATION
*)((char*)pfni
+ i
);
3233 i
= (offsetof(struct filesystem_event
, name
[event
->len
])
3234 + sizeof(int)-1) / sizeof(int) * sizeof(int);
3235 event
= (struct filesystem_event
*)((char*)event
+ i
);
3241 ret
= STATUS_NOTIFY_ENUM_DIR
;
3246 *last_entry_offset
= 0;
3247 size
= info
->BufferSize
- left
;
3252 ret
= STATUS_NOTIFY_ENUM_DIR
;
3256 iosb
->u
.Status
= ret
;
3257 iosb
->Information
= size
;
3258 *apc
= read_changes_user_apc
;
3262 #define FILE_NOTIFY_ALL ( \
3263 FILE_NOTIFY_CHANGE_FILE_NAME | \
3264 FILE_NOTIFY_CHANGE_DIR_NAME | \
3265 FILE_NOTIFY_CHANGE_ATTRIBUTES | \
3266 FILE_NOTIFY_CHANGE_SIZE | \
3267 FILE_NOTIFY_CHANGE_LAST_WRITE | \
3268 FILE_NOTIFY_CHANGE_LAST_ACCESS | \
3269 FILE_NOTIFY_CHANGE_CREATION | \
3270 FILE_NOTIFY_CHANGE_SECURITY )
3272 /******************************************************************************
3273 * NtNotifyChangeDirectoryFile [NTDLL.@]
3276 NtNotifyChangeDirectoryFile( HANDLE FileHandle
, HANDLE Event
,
3277 PIO_APC_ROUTINE ApcRoutine
, PVOID ApcContext
,
3278 PIO_STATUS_BLOCK IoStatusBlock
, PVOID Buffer
,
3279 ULONG BufferSize
, ULONG CompletionFilter
, BOOLEAN WatchTree
)
3281 struct read_changes_info
*info
;
3283 ULONG_PTR cvalue
= ApcRoutine
? 0 : (ULONG_PTR
)ApcContext
;
3285 TRACE("%p %p %p %p %p %p %u %u %d\n",
3286 FileHandle
, Event
, ApcRoutine
, ApcContext
, IoStatusBlock
,
3287 Buffer
, BufferSize
, CompletionFilter
, WatchTree
);
3290 return STATUS_ACCESS_VIOLATION
;
3292 if (CompletionFilter
== 0 || (CompletionFilter
& ~FILE_NOTIFY_ALL
))
3293 return STATUS_INVALID_PARAMETER
;
3295 info
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof *info
);
3297 return STATUS_NO_MEMORY
;
3299 info
->FileHandle
= FileHandle
;
3300 info
->Buffer
= Buffer
;
3301 info
->BufferSize
= BufferSize
;
3302 info
->apc
= ApcRoutine
;
3303 info
->apc_arg
= ApcContext
;
3305 SERVER_START_REQ( read_directory_changes
)
3307 req
->filter
= CompletionFilter
;
3308 req
->want_data
= (Buffer
!= NULL
);
3309 req
->subtree
= WatchTree
;
3310 req
->async
.handle
= wine_server_obj_handle( FileHandle
);
3311 req
->async
.callback
= wine_server_client_ptr( read_changes_apc
);
3312 req
->async
.iosb
= wine_server_client_ptr( IoStatusBlock
);
3313 req
->async
.arg
= wine_server_client_ptr( info
);
3314 req
->async
.event
= wine_server_obj_handle( Event
);
3315 req
->async
.cvalue
= cvalue
;
3316 status
= wine_server_call( req
);
3320 if (status
!= STATUS_PENDING
)
3321 RtlFreeHeap( GetProcessHeap(), 0, info
);