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_VNODE_H
51 #include <sys/vnode.h>
53 #ifdef HAVE_SYS_IOCTL_H
54 #include <sys/ioctl.h>
56 #ifdef HAVE_LINUX_IOCTL_H
57 #include <linux/ioctl.h>
59 #ifdef HAVE_LINUX_MAJOR_H
60 # include <linux/major.h>
62 #ifdef HAVE_SYS_PARAM_H
63 #include <sys/param.h>
65 #ifdef HAVE_SYS_MOUNT_H
66 #include <sys/mount.h>
68 #ifdef HAVE_SYS_STATFS_H
69 #include <sys/statfs.h>
77 #define WIN32_NO_STATUS
78 #define NONAMELESSUNION
83 #include "ntdll_misc.h"
84 #include "wine/unicode.h"
85 #include "wine/server.h"
86 #include "wine/list.h"
87 #include "wine/library.h"
88 #include "wine/debug.h"
90 WINE_DEFAULT_DEBUG_CHANNEL(file
);
93 #undef VFAT_IOCTL_READDIR_BOTH
98 /* We want the real kernel dirent structure, not the libc one */
103 unsigned short d_reclen
;
107 /* Define the VFAT ioctl to get both short and long file names */
108 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, KERNEL_DIRENT [2] )
111 # define O_DIRECTORY 0200000 /* must be directory */
114 #ifdef __NR_getdents64
119 unsigned short d_reclen
;
120 unsigned char d_type
;
125 static inline int getdents64( int fd
, char *de
, unsigned int size
)
127 return syscall( __NR_getdents64
, fd
, de
, size
);
134 #define IS_OPTION_TRUE(ch) ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
135 #define IS_SEPARATOR(ch) ((ch) == '\\' || (ch) == '/')
137 #define INVALID_NT_CHARS '*','?','<','>','|','"'
138 #define INVALID_DOS_CHARS INVALID_NT_CHARS,'+','=',',',';','[',']',' ','\345'
140 #define MAX_DIR_ENTRY_LEN 255 /* max length of a directory entry in chars */
142 #define MAX_IGNORED_FILES 4
150 static struct file_identity ignored_files
[MAX_IGNORED_FILES
];
151 static unsigned int ignored_files_count
;
153 union file_directory_info
156 FILE_DIRECTORY_INFORMATION dir
;
157 FILE_BOTH_DIRECTORY_INFORMATION both
;
158 FILE_FULL_DIRECTORY_INFORMATION full
;
159 FILE_ID_BOTH_DIRECTORY_INFORMATION id_both
;
160 FILE_ID_FULL_DIRECTORY_INFORMATION id_full
;
163 static BOOL show_dot_files
;
164 static RTL_RUN_ONCE init_once
= RTL_RUN_ONCE_INIT
;
166 /* at some point we may want to allow Winelib apps to set this */
167 static const BOOL is_case_sensitive
= FALSE
;
169 UNICODE_STRING system_dir
= { 0, 0, NULL
}; /* system directory */
171 static struct file_identity curdir
;
172 static struct file_identity windir
;
174 static RTL_CRITICAL_SECTION dir_section
;
175 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
178 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
179 0, 0, { (DWORD_PTR
)(__FILE__
": dir_section") }
181 static RTL_CRITICAL_SECTION dir_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
184 /* check if a given Unicode char is OK in a DOS short name */
185 static inline BOOL
is_invalid_dos_char( WCHAR ch
)
187 static const WCHAR invalid_chars
[] = { INVALID_DOS_CHARS
,'~','.',0 };
188 if (ch
> 0x7f) return TRUE
;
189 return strchrW( invalid_chars
, ch
) != NULL
;
192 /* check if the device can be a mounted volume */
193 static inline BOOL
is_valid_mounted_device( const struct stat
*st
)
195 #if defined(linux) || defined(__sun__)
196 return S_ISBLK( st
->st_mode
);
198 /* disks are char devices on *BSD */
199 return S_ISCHR( st
->st_mode
);
203 static inline void ignore_file( const char *name
)
206 assert( ignored_files_count
< MAX_IGNORED_FILES
);
207 if (!stat( name
, &st
))
209 ignored_files
[ignored_files_count
].dev
= st
.st_dev
;
210 ignored_files
[ignored_files_count
].ino
= st
.st_ino
;
211 ignored_files_count
++;
215 static inline BOOL
is_same_file( const struct file_identity
*file
, const struct stat
*st
)
217 return st
->st_dev
== file
->dev
&& st
->st_ino
== file
->ino
;
220 static inline BOOL
is_ignored_file( const struct stat
*st
)
224 for (i
= 0; i
< ignored_files_count
; i
++)
225 if (is_same_file( &ignored_files
[i
], st
)) return TRUE
;
229 static inline unsigned int dir_info_size( FILE_INFORMATION_CLASS
class, unsigned int len
)
233 case FileDirectoryInformation
:
234 return (FIELD_OFFSET( FILE_DIRECTORY_INFORMATION
, FileName
[len
] ) + 7) & ~7;
235 case FileBothDirectoryInformation
:
236 return (FIELD_OFFSET( FILE_BOTH_DIRECTORY_INFORMATION
, FileName
[len
] ) + 7) & ~7;
237 case FileFullDirectoryInformation
:
238 return (FIELD_OFFSET( FILE_FULL_DIRECTORY_INFORMATION
, FileName
[len
] ) + 7) & ~7;
239 case FileIdBothDirectoryInformation
:
240 return (FIELD_OFFSET( FILE_ID_BOTH_DIRECTORY_INFORMATION
, FileName
[len
] ) + 7) & ~7;
241 case FileIdFullDirectoryInformation
:
242 return (FIELD_OFFSET( FILE_ID_FULL_DIRECTORY_INFORMATION
, FileName
[len
] ) + 7) & ~7;
249 static inline unsigned int max_dir_info_size( FILE_INFORMATION_CLASS
class )
251 return dir_info_size( class, MAX_DIR_ENTRY_LEN
);
254 static inline BOOL
has_wildcard( const UNICODE_STRING
*mask
)
257 memchrW( mask
->Buffer
, '*', mask
->Length
/ sizeof(WCHAR
) ) ||
258 memchrW( mask
->Buffer
, '?', mask
->Length
/ sizeof(WCHAR
) ));
262 /* support for a directory queue for filesystem searches */
270 static struct list dir_queue
= LIST_INIT( dir_queue
);
272 static NTSTATUS
add_dir_to_queue( const char *name
)
274 int len
= strlen( name
) + 1;
275 struct dir_name
*dir
= RtlAllocateHeap( GetProcessHeap(), 0,
276 FIELD_OFFSET( struct dir_name
, name
[len
] ));
277 if (!dir
) return STATUS_NO_MEMORY
;
278 strcpy( dir
->name
, name
);
279 list_add_tail( &dir_queue
, &dir
->entry
);
280 return STATUS_SUCCESS
;
283 static NTSTATUS
next_dir_in_queue( char *name
)
285 struct list
*head
= list_head( &dir_queue
);
288 struct dir_name
*dir
= LIST_ENTRY( head
, struct dir_name
, entry
);
289 strcpy( name
, dir
->name
);
290 list_remove( &dir
->entry
);
291 RtlFreeHeap( GetProcessHeap(), 0, dir
);
292 return STATUS_SUCCESS
;
294 return STATUS_OBJECT_NAME_NOT_FOUND
;
297 static void flush_dir_queue(void)
301 while ((head
= list_head( &dir_queue
)))
303 struct dir_name
*dir
= LIST_ENTRY( head
, struct dir_name
, entry
);
304 list_remove( &dir
->entry
);
305 RtlFreeHeap( GetProcessHeap(), 0, dir
);
310 /***********************************************************************
311 * get_default_com_device
313 * Return the default device to use for serial ports.
315 static char *get_default_com_device( int num
)
319 if (!num
|| num
> 9) return ret
;
321 ret
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/ttyS0") );
324 strcpy( ret
, "/dev/ttyS0" );
325 ret
[strlen(ret
) - 1] = '0' + num
- 1;
327 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
328 ret
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/cuau0") );
331 strcpy( ret
, "/dev/cuau0" );
332 ret
[strlen(ret
) - 1] = '0' + num
- 1;
334 #elif defined(__DragonFly__)
335 ret
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/cuaa0") );
338 strcpy( ret
, "/dev/cuaa0" );
339 ret
[strlen(ret
) - 1] = '0' + num
- 1;
342 FIXME( "no known default for device com%d\n", num
);
348 /***********************************************************************
349 * get_default_lpt_device
351 * Return the default device to use for parallel ports.
353 static char *get_default_lpt_device( int num
)
357 if (!num
|| num
> 9) return ret
;
359 ret
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/lp0") );
362 strcpy( ret
, "/dev/lp0" );
363 ret
[strlen(ret
) - 1] = '0' + num
- 1;
366 FIXME( "no known default for device lpt%d\n", num
);
373 static char *unescape_field( char *str
)
377 for (in
= out
= str
; *in
; in
++, out
++)
387 else if (in
[1] == '0' && in
[2] == '4' && in
[3] == '0')
392 else if (in
[1] == '0' && in
[2] == '1' && in
[3] == '1')
397 else if (in
[1] == '0' && in
[2] == '1' && in
[3] == '2')
402 else if (in
[1] == '1' && in
[2] == '3' && in
[3] == '4')
414 static inline char *get_field( char **str
)
418 ret
= strsep( str
, " \t" );
419 if (*str
) *str
+= strspn( *str
, " \t" );
423 /************************************************************************
424 * getmntent_replacement
426 * getmntent replacement for Android.
428 * NB returned static buffer is not thread safe; protect with dir_section.
430 static struct mntent
*getmntent_replacement( FILE *f
)
432 static struct mntent entry
;
433 static char buf
[4096];
438 if (!fgets( buf
, sizeof(buf
), f
)) return NULL
;
439 p
= strchr( buf
, '\n' );
441 else /* Partially unread line, move file ptr to end */
444 while (fgets( tmp
, sizeof(tmp
), f
))
445 if (strchr( tmp
, '\n' )) break;
447 start
= buf
+ strspn( buf
, " \t" );
448 } while (start
[0] == '\0' || start
[0] == '#');
450 p
= get_field( &start
);
451 entry
.mnt_fsname
= p
? unescape_field( p
) : (char *)"";
453 p
= get_field( &start
);
454 entry
.mnt_dir
= p
? unescape_field( p
) : (char *)"";
456 p
= get_field( &start
);
457 entry
.mnt_type
= p
? unescape_field( p
) : (char *)"";
459 p
= get_field( &start
);
460 entry
.mnt_opts
= p
? unescape_field( p
) : (char *)"";
462 p
= get_field( &start
);
463 entry
.mnt_freq
= p
? atoi(p
) : 0;
465 p
= get_field( &start
);
466 entry
.mnt_passno
= p
? atoi(p
) : 0;
470 #define getmntent getmntent_replacement
473 /***********************************************************************
474 * DIR_get_drives_info
476 * Retrieve device/inode number for all the drives. Helper for find_drive_root.
478 unsigned int DIR_get_drives_info( struct drive_info info
[MAX_DOS_DRIVES
] )
480 static struct drive_info cache
[MAX_DOS_DRIVES
];
481 static time_t last_update
;
482 static unsigned int nb_drives
;
484 time_t now
= time(NULL
);
486 RtlEnterCriticalSection( &dir_section
);
487 if (now
!= last_update
)
489 const char *config_dir
= wine_get_config_dir();
494 if ((buffer
= RtlAllocateHeap( GetProcessHeap(), 0,
495 strlen(config_dir
) + sizeof("/dosdevices/a:") )))
497 strcpy( buffer
, config_dir
);
498 strcat( buffer
, "/dosdevices/a:" );
499 p
= buffer
+ strlen(buffer
) - 2;
501 for (i
= nb_drives
= 0; i
< MAX_DOS_DRIVES
; i
++)
504 if (!stat( buffer
, &st
))
506 cache
[i
].dev
= st
.st_dev
;
507 cache
[i
].ino
= st
.st_ino
;
516 RtlFreeHeap( GetProcessHeap(), 0, buffer
);
520 memcpy( info
, cache
, sizeof(cache
) );
522 RtlLeaveCriticalSection( &dir_section
);
527 /***********************************************************************
528 * parse_mount_entries
530 * Parse mount entries looking for a given device. Helper for get_default_drive_device.
534 #include <sys/vfstab.h>
535 static char *parse_vfstab_entries( FILE *f
, dev_t dev
, ino_t ino
)
541 while (! getvfsent( f
, &entry
))
543 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
544 if (!strcmp( entry
.vfs_fstype
, "nfs" ) ||
545 !strcmp( entry
.vfs_fstype
, "smbfs" ) ||
546 !strcmp( entry
.vfs_fstype
, "ncpfs" )) continue;
548 if (stat( entry
.vfs_mountp
, &st
) == -1) continue;
549 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
550 if (!strcmp( entry
.vfs_fstype
, "fd" ))
552 if ((device
= strstr( entry
.vfs_mntopts
, "dev=" )))
554 char *p
= strchr( device
+ 4, ',' );
560 return entry
.vfs_special
;
567 static char *parse_mount_entries( FILE *f
, dev_t dev
, ino_t ino
)
569 struct mntent
*entry
;
573 while ((entry
= getmntent( f
)))
575 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
576 if (!strcmp( entry
->mnt_type
, "nfs" ) ||
577 !strcmp( entry
->mnt_type
, "smbfs" ) ||
578 !strcmp( entry
->mnt_type
, "ncpfs" )) continue;
580 if (stat( entry
->mnt_dir
, &st
) == -1) continue;
581 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
582 if (!strcmp( entry
->mnt_type
, "supermount" ))
584 if ((device
= strstr( entry
->mnt_opts
, "dev=" )))
586 char *p
= strchr( device
+ 4, ',' );
591 else if (!stat( entry
->mnt_fsname
, &st
) && S_ISREG(st
.st_mode
))
593 /* if device is a regular file check for a loop mount */
594 if ((device
= strstr( entry
->mnt_opts
, "loop=" )))
596 char *p
= strchr( device
+ 5, ',' );
602 return entry
->mnt_fsname
;
608 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
610 static char *parse_mount_entries( FILE *f
, dev_t dev
, ino_t ino
)
615 while ((entry
= getfsent()))
617 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
618 if (!strcmp( entry
->fs_vfstype
, "nfs" ) ||
619 !strcmp( entry
->fs_vfstype
, "smbfs" ) ||
620 !strcmp( entry
->fs_vfstype
, "ncpfs" )) continue;
622 if (stat( entry
->fs_file
, &st
) == -1) continue;
623 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
624 return entry
->fs_spec
;
631 #include <sys/mnttab.h>
632 static char *parse_mount_entries( FILE *f
, dev_t dev
, ino_t ino
)
639 while (( ! getmntent( f
, &entry
) ))
641 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
642 if (!strcmp( entry
.mnt_fstype
, "nfs" ) ||
643 !strcmp( entry
.mnt_fstype
, "smbfs" ) ||
644 !strcmp( entry
.mnt_fstype
, "ncpfs" )) continue;
646 if (stat( entry
.mnt_mountp
, &st
) == -1) continue;
647 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
648 if (!strcmp( entry
.mnt_fstype
, "fd" ))
650 if ((device
= strstr( entry
.mnt_mntopts
, "dev=" )))
652 char *p
= strchr( device
+ 4, ',' );
658 return entry
.mnt_special
;
664 /***********************************************************************
665 * get_default_drive_device
667 * Return the default device to use for a given drive mount point.
669 static char *get_default_drive_device( const char *root
)
679 /* try to open it first to force it to get mounted */
680 if ((fd
= open( root
, O_RDONLY
| O_DIRECTORY
)) != -1)
682 res
= fstat( fd
, &st
);
685 /* now try normal stat just in case */
686 if (res
== -1) res
= stat( root
, &st
);
687 if (res
== -1) return NULL
;
689 RtlEnterCriticalSection( &dir_section
);
692 if ((f
= fopen( "/proc/mounts", "r" )))
694 device
= parse_mount_entries( f
, st
.st_dev
, st
.st_ino
);
698 if ((f
= fopen( "/etc/mtab", "r" )))
700 device
= parse_mount_entries( f
, st
.st_dev
, st
.st_ino
);
703 /* look through fstab too in case it's not mounted (for instance if it's an audio CD) */
704 if (!device
&& (f
= fopen( "/etc/fstab", "r" )))
706 device
= parse_mount_entries( f
, st
.st_dev
, st
.st_ino
);
712 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(device
) + 1 );
713 if (ret
) strcpy( ret
, device
);
715 RtlLeaveCriticalSection( &dir_section
);
717 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__ ) || defined(__DragonFly__)
722 /* try to open it first to force it to get mounted */
723 if ((fd
= open( root
, O_RDONLY
)) != -1)
725 res
= fstat( fd
, &st
);
728 /* now try normal stat just in case */
729 if (res
== -1) res
= stat( root
, &st
);
730 if (res
== -1) return NULL
;
732 RtlEnterCriticalSection( &dir_section
);
734 /* The FreeBSD parse_mount_entries doesn't require a file argument, so just
735 * pass NULL. Leave the argument in for symmetry.
737 device
= parse_mount_entries( NULL
, st
.st_dev
, st
.st_ino
);
740 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(device
) + 1 );
741 if (ret
) strcpy( ret
, device
);
743 RtlLeaveCriticalSection( &dir_section
);
751 /* try to open it first to force it to get mounted */
752 if ((fd
= open( root
, O_RDONLY
)) != -1)
754 res
= fstat( fd
, &st
);
757 /* now try normal stat just in case */
758 if (res
== -1) res
= stat( root
, &st
);
759 if (res
== -1) return NULL
;
761 RtlEnterCriticalSection( &dir_section
);
763 if ((f
= fopen( "/etc/mnttab", "r" )))
765 device
= parse_mount_entries( f
, st
.st_dev
, st
.st_ino
);
768 /* look through fstab too in case it's not mounted (for instance if it's an audio CD) */
769 if (!device
&& (f
= fopen( "/etc/vfstab", "r" )))
771 device
= parse_vfstab_entries( f
, st
.st_dev
, st
.st_ino
);
776 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(device
) + 1 );
777 if (ret
) strcpy( ret
, device
);
779 RtlLeaveCriticalSection( &dir_section
);
781 #elif defined(__APPLE__)
782 struct statfs
*mntStat
;
788 static const char path_bsd_device
[] = "/dev/disk";
791 res
= stat( root
, &st
);
792 if (res
== -1) return NULL
;
797 RtlEnterCriticalSection( &dir_section
);
799 mntSize
= getmntinfo(&mntStat
, MNT_NOWAIT
);
801 for (i
= 0; i
< mntSize
&& !ret
; i
++)
803 if (stat(mntStat
[i
].f_mntonname
, &st
) == -1) continue;
804 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
806 /* FIXME add support for mounted network drive */
807 if ( strncmp(mntStat
[i
].f_mntfromname
, path_bsd_device
, strlen(path_bsd_device
)) == 0)
809 /* set return value to the corresponding raw BSD node */
810 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(mntStat
[i
].f_mntfromname
) + 2 /* 2 : r and \0 */ );
813 strcpy(ret
, "/dev/r");
814 strcat(ret
, mntStat
[i
].f_mntfromname
+sizeof("/dev/")-1);
818 RtlLeaveCriticalSection( &dir_section
);
821 if (!warned
++) FIXME( "auto detection of DOS devices not supported on this platform\n" );
827 /***********************************************************************
828 * get_device_mount_point
830 * Return the current mount point for a device.
832 static char *get_device_mount_point( dev_t dev
)
839 RtlEnterCriticalSection( &dir_section
);
842 if ((f
= fopen( "/proc/mounts", "r" )))
844 if ((f
= fopen( "/etc/mtab", "r" )))
847 struct mntent
*entry
;
851 while ((entry
= getmntent( f
)))
853 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
854 if (!strcmp( entry
->mnt_type
, "nfs" ) ||
855 !strcmp( entry
->mnt_type
, "smbfs" ) ||
856 !strcmp( entry
->mnt_type
, "ncpfs" )) continue;
858 if (!strcmp( entry
->mnt_type
, "supermount" ))
860 if ((device
= strstr( entry
->mnt_opts
, "dev=" )))
863 if ((p
= strchr( device
, ',' ))) *p
= 0;
866 else if (!stat( entry
->mnt_fsname
, &st
) && S_ISREG(st
.st_mode
))
868 /* if device is a regular file check for a loop mount */
869 if ((device
= strstr( entry
->mnt_opts
, "loop=" )))
872 if ((p
= strchr( device
, ',' ))) *p
= 0;
875 else device
= entry
->mnt_fsname
;
877 if (device
&& !stat( device
, &st
) && S_ISBLK(st
.st_mode
) && st
.st_rdev
== dev
)
879 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(entry
->mnt_dir
) + 1 );
880 if (ret
) strcpy( ret
, entry
->mnt_dir
);
886 RtlLeaveCriticalSection( &dir_section
);
887 #elif defined(__APPLE__)
888 struct statfs
*entry
;
892 RtlEnterCriticalSection( &dir_section
);
894 size
= getmntinfo( &entry
, MNT_NOWAIT
);
895 for (i
= 0; i
< size
; i
++)
897 if (stat( entry
[i
].f_mntfromname
, &st
) == -1) continue;
898 if (S_ISBLK(st
.st_mode
) && st
.st_rdev
== dev
)
900 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(entry
[i
].f_mntonname
) + 1 );
901 if (ret
) strcpy( ret
, entry
[i
].f_mntonname
);
905 RtlLeaveCriticalSection( &dir_section
);
908 if (!warned
++) FIXME( "unmounting devices not supported on this platform\n" );
914 #if defined(HAVE_GETATTRLIST) && defined(ATTR_VOL_CAPABILITIES) && \
915 defined(VOL_CAPABILITIES_FORMAT) && defined(VOL_CAP_FMT_CASE_SENSITIVE)
928 BOOLEAN case_sensitive
;
934 vol_capabilities_attr_t caps
;
937 /***********************************************************************
940 * Checks if the specified file system is in the cache.
942 static struct fs_cache
*look_up_fs_cache( dev_t dev
)
945 for (i
= 0; i
< sizeof(fs_cache
)/sizeof(fs_cache
[0]); i
++)
946 if (fs_cache
[i
].dev
== dev
)
951 /***********************************************************************
954 * Adds the specified file system to the cache.
956 static void add_fs_cache( dev_t dev
, fsid_t fsid
, BOOLEAN case_sensitive
)
959 struct fs_cache
*entry
= look_up_fs_cache( dev
);
963 /* Update the cache */
965 entry
->case_sensitive
= case_sensitive
;
969 /* Add a new entry */
970 for (i
= 0; i
< sizeof(fs_cache
)/sizeof(fs_cache
[0]); i
++)
971 if (fs_cache
[i
].dev
== 0)
973 /* This entry is empty, use it */
974 fs_cache
[i
].dev
= dev
;
975 fs_cache
[i
].fsid
= fsid
;
976 fs_cache
[i
].case_sensitive
= case_sensitive
;
980 /* Cache is out of space, warn */
982 WARN( "FS cache is out of space, expect performance problems\n" );
985 /***********************************************************************
986 * get_dir_case_sensitivity_attr_by_id
988 * Checks if the volume with the specified device and file system IDs
989 * is case sensitive or not. Uses getattrlist(2).
991 static int get_dir_case_sensitivity_attr_by_id( dev_t dev
, fsid_t fsid
)
993 char *mntpoint
= NULL
;
994 struct attrlist attr
;
995 struct vol_caps caps
;
996 struct fs_cache
*entry
;
998 /* Try to look it up in the cache */
999 entry
= look_up_fs_cache( dev
);
1000 if (entry
&& !memcmp( &entry
->fsid
, &fsid
, sizeof(fsid_t
) ))
1001 /* Cache lookup succeeded */
1002 return entry
->case_sensitive
;
1003 /* Cache is stale at this point, we have to update it */
1005 mntpoint
= get_device_mount_point( dev
);
1006 /* Now look up the case-sensitivity */
1007 attr
.bitmapcount
= ATTR_BIT_MAP_COUNT
;
1008 attr
.reserved
= attr
.commonattr
= 0;
1009 attr
.volattr
= ATTR_VOL_INFO
|ATTR_VOL_CAPABILITIES
;
1010 attr
.dirattr
= attr
.fileattr
= attr
.forkattr
= 0;
1011 if (getattrlist( mntpoint
, &attr
, &caps
, sizeof(caps
), 0 ) < 0)
1013 RtlFreeHeap( GetProcessHeap(), 0, mntpoint
);
1014 add_fs_cache( dev
, fsid
, TRUE
);
1017 RtlFreeHeap( GetProcessHeap(), 0, mntpoint
);
1018 if (caps
.size
== sizeof(caps
) &&
1019 (caps
.caps
.valid
[VOL_CAPABILITIES_FORMAT
] &
1020 (VOL_CAP_FMT_CASE_SENSITIVE
| VOL_CAP_FMT_CASE_PRESERVING
)) ==
1021 (VOL_CAP_FMT_CASE_SENSITIVE
| VOL_CAP_FMT_CASE_PRESERVING
))
1025 if ((caps
.caps
.capabilities
[VOL_CAPABILITIES_FORMAT
] &
1026 VOL_CAP_FMT_CASE_SENSITIVE
) != VOL_CAP_FMT_CASE_SENSITIVE
)
1030 /* Update the cache */
1031 add_fs_cache( dev
, fsid
, ret
);
1037 /***********************************************************************
1038 * get_dir_case_sensitivity_attr
1040 * Checks if the volume containing the specified directory is case
1041 * sensitive or not. Uses getattrlist(2).
1043 static int get_dir_case_sensitivity_attr( const char *dir
)
1045 struct attrlist attr
;
1046 struct get_fsid get_fsid
;
1048 /* First get the FS ID of the volume */
1049 attr
.bitmapcount
= ATTR_BIT_MAP_COUNT
;
1051 attr
.commonattr
= ATTR_CMN_DEVID
|ATTR_CMN_FSID
;
1052 attr
.volattr
= attr
.dirattr
= attr
.fileattr
= attr
.forkattr
= 0;
1054 if (getattrlist( dir
, &attr
, &get_fsid
, sizeof(get_fsid
), 0 ) != 0 ||
1055 get_fsid
.size
!= sizeof(get_fsid
))
1057 return get_dir_case_sensitivity_attr_by_id( get_fsid
.dev
, get_fsid
.fsid
);
1061 /***********************************************************************
1062 * get_dir_case_sensitivity_stat
1064 * Checks if the volume containing the specified directory is case
1065 * sensitive or not. Uses statfs(2) or statvfs(2).
1067 static BOOLEAN
get_dir_case_sensitivity_stat( const char *dir
)
1069 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1072 if (statfs( dir
, &stfs
) == -1) return FALSE
;
1073 /* Assume these file systems are always case insensitive on Mac OS.
1074 * For FreeBSD, only assume CIOPFS is case insensitive (AFAIK, Mac OS
1075 * is the only UNIX that supports case-insensitive lookup).
1077 if (!strcmp( stfs
.f_fstypename
, "fusefs" ) &&
1078 !strncmp( stfs
.f_mntfromname
, "ciopfs", 5 ))
1081 if (!strcmp( stfs
.f_fstypename
, "msdos" ) ||
1082 !strcmp( stfs
.f_fstypename
, "cd9660" ) ||
1083 !strcmp( stfs
.f_fstypename
, "udf" ) ||
1084 !strcmp( stfs
.f_fstypename
, "ntfs" ) ||
1085 !strcmp( stfs
.f_fstypename
, "smbfs" ))
1087 #ifdef _DARWIN_FEATURE_64_BIT_INODE
1088 if (!strcmp( stfs
.f_fstypename
, "hfs" ) && (stfs
.f_fssubtype
== 0 ||
1089 stfs
.f_fssubtype
== 1 ||
1090 stfs
.f_fssubtype
== 128))
1093 /* The field says "reserved", but a quick look at the kernel source
1094 * tells us that this "reserved" field is really the same as the
1095 * "fssubtype" field from the inode64 structure (see munge_statfs()
1096 * in <xnu-source>/bsd/vfs/vfs_syscalls.c).
1098 if (!strcmp( stfs
.f_fstypename
, "hfs" ) && (stfs
.f_reserved1
== 0 ||
1099 stfs
.f_reserved1
== 1 ||
1100 stfs
.f_reserved1
== 128))
1106 #elif defined(__NetBSD__)
1107 struct statvfs stfs
;
1109 if (statvfs( dir
, &stfs
) == -1) return FALSE
;
1110 /* Only assume CIOPFS is case insensitive. */
1111 if (strcmp( stfs
.f_fstypename
, "fusefs" ) ||
1112 strncmp( stfs
.f_mntfromname
, "ciopfs", 5 ))
1116 #elif defined(__linux__)
1121 /* Only assume CIOPFS is case insensitive. */
1122 if (statfs( dir
, &stfs
) == -1) return FALSE
;
1123 if (stfs
.f_type
!= 0x65735546 /* FUSE_SUPER_MAGIC */)
1125 /* Normally, we'd have to parse the mtab to find out exactly what
1126 * kind of FUSE FS this is. But, someone on wine-devel suggested
1127 * a shortcut. We'll stat a special file in the directory. If it's
1128 * there, we'll assume it's a CIOPFS, else not.
1129 * This will break if somebody puts a file named ".ciopfs" in a non-
1132 cifile
= RtlAllocateHeap( GetProcessHeap(), 0, strlen( dir
)+sizeof("/.ciopfs") );
1133 if (!cifile
) return TRUE
;
1134 strcpy( cifile
, dir
);
1135 strcat( cifile
, "/.ciopfs" );
1136 if (stat( cifile
, &st
) == 0)
1138 RtlFreeHeap( GetProcessHeap(), 0, cifile
);
1141 RtlFreeHeap( GetProcessHeap(), 0, cifile
);
1149 /***********************************************************************
1150 * get_dir_case_sensitivity
1152 * Checks if the volume containing the specified directory is case
1153 * sensitive or not. Uses statfs(2) or statvfs(2).
1155 static BOOLEAN
get_dir_case_sensitivity( const char *dir
)
1157 #if defined(HAVE_GETATTRLIST) && defined(ATTR_VOL_CAPABILITIES) && \
1158 defined(VOL_CAPABILITIES_FORMAT) && defined(VOL_CAP_FMT_CASE_SENSITIVE)
1159 int case_sensitive
= get_dir_case_sensitivity_attr( dir
);
1160 if (case_sensitive
!= -1) return case_sensitive
;
1162 return get_dir_case_sensitivity_stat( dir
);
1166 /***********************************************************************
1169 * Initialize the show_dot_files options.
1171 static DWORD WINAPI
init_options( RTL_RUN_ONCE
*once
, void *param
, void **context
)
1173 static const WCHAR WineW
[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e',0};
1174 static const WCHAR ShowDotFilesW
[] = {'S','h','o','w','D','o','t','F','i','l','e','s',0};
1178 OBJECT_ATTRIBUTES attr
;
1179 UNICODE_STRING nameW
;
1181 RtlOpenCurrentUser( KEY_ALL_ACCESS
, &root
);
1182 attr
.Length
= sizeof(attr
);
1183 attr
.RootDirectory
= root
;
1184 attr
.ObjectName
= &nameW
;
1185 attr
.Attributes
= 0;
1186 attr
.SecurityDescriptor
= NULL
;
1187 attr
.SecurityQualityOfService
= NULL
;
1188 RtlInitUnicodeString( &nameW
, WineW
);
1190 /* @@ Wine registry key: HKCU\Software\Wine */
1191 if (!NtOpenKey( &hkey
, KEY_ALL_ACCESS
, &attr
))
1193 RtlInitUnicodeString( &nameW
, ShowDotFilesW
);
1194 if (!NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
, tmp
, sizeof(tmp
), &dummy
))
1196 WCHAR
*str
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)tmp
)->Data
;
1197 show_dot_files
= IS_OPTION_TRUE( str
[0] );
1203 /* a couple of directories that we don't want to return in directory searches */
1204 ignore_file( wine_get_config_dir() );
1205 ignore_file( "/dev" );
1206 ignore_file( "/proc" );
1208 ignore_file( "/sys" );
1214 /***********************************************************************
1215 * DIR_is_hidden_file
1217 * Check if the specified file should be hidden based on its name and the show dot files option.
1219 BOOL
DIR_is_hidden_file( const UNICODE_STRING
*name
)
1223 RtlRunOnceExecuteOnce( &init_once
, init_options
, NULL
, NULL
);
1225 if (show_dot_files
) return FALSE
;
1227 end
= p
= name
->Buffer
+ name
->Length
/sizeof(WCHAR
);
1228 while (p
> name
->Buffer
&& IS_SEPARATOR(p
[-1])) p
--;
1229 while (p
> name
->Buffer
&& !IS_SEPARATOR(p
[-1])) p
--;
1230 if (p
== end
|| *p
!= '.') return FALSE
;
1231 /* make sure it isn't '.' or '..' */
1232 if (p
+ 1 == end
) return FALSE
;
1233 if (p
[1] == '.' && p
+ 2 == end
) return FALSE
;
1238 /***********************************************************************
1239 * hash_short_file_name
1241 * Transform a Unix file name into a hashed DOS name. If the name is a valid
1242 * DOS name, it is converted to upper-case; otherwise it is replaced by a
1243 * hashed version that fits in 8.3 format.
1244 * 'buffer' must be at least 12 characters long.
1245 * Returns length of short name in bytes; short name is NOT null-terminated.
1247 static ULONG
hash_short_file_name( const UNICODE_STRING
*name
, LPWSTR buffer
)
1249 static const char hash_chars
[32] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345";
1251 LPCWSTR p
, ext
, end
= name
->Buffer
+ name
->Length
/ sizeof(WCHAR
);
1253 unsigned short hash
;
1256 /* Compute the hash code of the file name */
1257 /* If you know something about hash functions, feel free to */
1258 /* insert a better algorithm here... */
1259 if (!is_case_sensitive
)
1261 for (p
= name
->Buffer
, hash
= 0xbeef; p
< end
- 1; p
++)
1262 hash
= (hash
<<3) ^ (hash
>>5) ^ tolowerW(*p
) ^ (tolowerW(p
[1]) << 8);
1263 hash
= (hash
<<3) ^ (hash
>>5) ^ tolowerW(*p
); /* Last character */
1267 for (p
= name
->Buffer
, hash
= 0xbeef; p
< end
- 1; p
++)
1268 hash
= (hash
<< 3) ^ (hash
>> 5) ^ *p
^ (p
[1] << 8);
1269 hash
= (hash
<< 3) ^ (hash
>> 5) ^ *p
; /* Last character */
1272 /* Find last dot for start of the extension */
1273 for (p
= name
->Buffer
+ 1, ext
= NULL
; p
< end
- 1; p
++) if (*p
== '.') ext
= p
;
1275 /* Copy first 4 chars, replacing invalid chars with '_' */
1276 for (i
= 4, p
= name
->Buffer
, dst
= buffer
; i
> 0; i
--, p
++)
1278 if (p
== end
|| p
== ext
) break;
1279 *dst
++ = is_invalid_dos_char(*p
) ? '_' : toupperW(*p
);
1281 /* Pad to 5 chars with '~' */
1282 while (i
-- >= 0) *dst
++ = '~';
1284 /* Insert hash code converted to 3 ASCII chars */
1285 *dst
++ = hash_chars
[(hash
>> 10) & 0x1f];
1286 *dst
++ = hash_chars
[(hash
>> 5) & 0x1f];
1287 *dst
++ = hash_chars
[hash
& 0x1f];
1289 /* Copy the first 3 chars of the extension (if any) */
1293 for (i
= 3, ext
++; (i
> 0) && ext
< end
; i
--, ext
++)
1294 *dst
++ = is_invalid_dos_char(*ext
) ? '_' : toupperW(*ext
);
1296 return dst
- buffer
;
1300 /***********************************************************************
1303 * Check a long file name against a mask.
1305 * Tests (done in W95 DOS shell - case insensitive):
1306 * *.txt test1.test.txt *
1308 * *.t??????.t* test1.ta.tornado.txt *
1309 * *tornado* test1.ta.tornado.txt *
1310 * t*t test1.ta.tornado.txt *
1312 * ?est??? test1.txt -
1313 * *test1.txt* test1.txt *
1314 * h?l?o*t.dat hellothisisatest.dat *
1316 static BOOLEAN
match_filename( const UNICODE_STRING
*name_str
, const UNICODE_STRING
*mask_str
)
1319 const WCHAR
*name
= name_str
->Buffer
;
1320 const WCHAR
*mask
= mask_str
->Buffer
;
1321 const WCHAR
*name_end
= name
+ name_str
->Length
/ sizeof(WCHAR
);
1322 const WCHAR
*mask_end
= mask
+ mask_str
->Length
/ sizeof(WCHAR
);
1323 const WCHAR
*lastjoker
= NULL
;
1324 const WCHAR
*next_to_retry
= NULL
;
1326 TRACE("(%s, %s)\n", debugstr_us(name_str
), debugstr_us(mask_str
));
1328 while (name
< name_end
&& mask
< mask_end
)
1334 while (mask
< mask_end
&& *mask
== '*') mask
++; /* Skip consecutive '*' */
1335 if (mask
== mask_end
) return TRUE
; /* end of mask is all '*', so match */
1338 /* skip to the next match after the joker(s) */
1339 if (is_case_sensitive
)
1340 while (name
< name_end
&& (*name
!= *mask
)) name
++;
1342 while (name
< name_end
&& (toupperW(*name
) != toupperW(*mask
))) name
++;
1343 next_to_retry
= name
;
1350 if (is_case_sensitive
) mismatch
= (*mask
!= *name
);
1351 else mismatch
= (toupperW(*mask
) != toupperW(*name
));
1357 if (mask
== mask_end
)
1359 if (name
== name_end
) return TRUE
;
1360 if (lastjoker
) mask
= lastjoker
;
1363 else /* mismatch ! */
1365 if (lastjoker
) /* we had an '*', so we can try unlimitedly */
1369 /* this scan sequence was a mismatch, so restart
1370 * 1 char after the first char we checked last time */
1372 name
= next_to_retry
;
1374 else return FALSE
; /* bad luck */
1379 while (mask
< mask_end
&& ((*mask
== '.') || (*mask
== '*')))
1380 mask
++; /* Ignore trailing '.' or '*' in mask */
1381 return (name
== name_end
&& mask
== mask_end
);
1385 /***********************************************************************
1388 * helper for NtQueryDirectoryFile
1390 static union file_directory_info
*append_entry( void *info_ptr
, IO_STATUS_BLOCK
*io
, ULONG max_length
,
1391 const char *long_name
, const char *short_name
,
1392 const UNICODE_STRING
*mask
, FILE_INFORMATION_CLASS
class )
1394 union file_directory_info
*info
;
1395 int i
, long_len
, short_len
, total_len
;
1397 WCHAR long_nameW
[MAX_DIR_ENTRY_LEN
];
1398 WCHAR short_nameW
[12];
1403 io
->u
.Status
= STATUS_SUCCESS
;
1404 long_len
= ntdll_umbstowcs( 0, long_name
, strlen(long_name
), long_nameW
, MAX_DIR_ENTRY_LEN
);
1405 if (long_len
== -1) return NULL
;
1407 str
.Buffer
= long_nameW
;
1408 str
.Length
= long_len
* sizeof(WCHAR
);
1409 str
.MaximumLength
= sizeof(long_nameW
);
1413 short_len
= ntdll_umbstowcs( 0, short_name
, strlen(short_name
),
1414 short_nameW
, sizeof(short_nameW
) / sizeof(WCHAR
) );
1415 if (short_len
== -1) short_len
= sizeof(short_nameW
) / sizeof(WCHAR
);
1417 else /* generate a short name if necessary */
1422 if (!RtlIsNameLegalDOS8Dot3( &str
, NULL
, &spaces
) || spaces
)
1423 short_len
= hash_short_file_name( &str
, short_nameW
);
1426 TRACE( "long %s short %s mask %s\n",
1427 debugstr_us(&str
), debugstr_wn(short_nameW
, short_len
), debugstr_us(mask
) );
1429 if (mask
&& !match_filename( &str
, mask
))
1431 if (!short_len
) return NULL
; /* no short name to match */
1432 str
.Buffer
= short_nameW
;
1433 str
.Length
= short_len
* sizeof(WCHAR
);
1434 str
.MaximumLength
= sizeof(short_nameW
);
1435 if (!match_filename( &str
, mask
)) return NULL
;
1438 if (get_file_info( long_name
, &st
, &attributes
) == -1) return NULL
;
1439 if (is_ignored_file( &st
))
1441 TRACE( "ignoring file %s\n", long_name
);
1444 if (!show_dot_files
&& long_name
[0] == '.' && long_name
[1] && (long_name
[1] != '.' || long_name
[2]))
1445 attributes
|= FILE_ATTRIBUTE_HIDDEN
;
1447 total_len
= dir_info_size( class, long_len
);
1448 if (io
->Information
+ total_len
> max_length
)
1450 total_len
= max_length
- io
->Information
;
1451 io
->u
.Status
= STATUS_BUFFER_OVERFLOW
;
1453 info
= (union file_directory_info
*)((char *)info_ptr
+ io
->Information
);
1454 if (st
.st_dev
!= curdir
.dev
) st
.st_ino
= 0; /* ignore inode if on a different device */
1455 /* all the structures start with a FileDirectoryInformation layout */
1456 fill_file_info( &st
, attributes
, info
, class );
1457 info
->dir
.NextEntryOffset
= total_len
;
1458 info
->dir
.FileIndex
= 0; /* NTFS always has 0 here, so let's not bother with it */
1462 case FileDirectoryInformation
:
1463 info
->dir
.FileNameLength
= long_len
* sizeof(WCHAR
);
1464 filename
= info
->dir
.FileName
;
1467 case FileFullDirectoryInformation
:
1468 info
->full
.EaSize
= 0; /* FIXME */
1469 info
->full
.FileNameLength
= long_len
* sizeof(WCHAR
);
1470 filename
= info
->full
.FileName
;
1473 case FileIdFullDirectoryInformation
:
1474 info
->id_full
.EaSize
= 0; /* FIXME */
1475 info
->id_full
.FileNameLength
= long_len
* sizeof(WCHAR
);
1476 filename
= info
->id_full
.FileName
;
1479 case FileBothDirectoryInformation
:
1480 info
->both
.EaSize
= 0; /* FIXME */
1481 info
->both
.ShortNameLength
= short_len
* sizeof(WCHAR
);
1482 for (i
= 0; i
< short_len
; i
++) info
->both
.ShortName
[i
] = toupperW(short_nameW
[i
]);
1483 info
->both
.FileNameLength
= long_len
* sizeof(WCHAR
);
1484 filename
= info
->both
.FileName
;
1487 case FileIdBothDirectoryInformation
:
1488 info
->id_both
.EaSize
= 0; /* FIXME */
1489 info
->id_both
.ShortNameLength
= short_len
* sizeof(WCHAR
);
1490 for (i
= 0; i
< short_len
; i
++) info
->id_both
.ShortName
[i
] = toupperW(short_nameW
[i
]);
1491 info
->id_both
.FileNameLength
= long_len
* sizeof(WCHAR
);
1492 filename
= info
->id_both
.FileName
;
1499 memcpy( filename
, long_nameW
, long_len
* sizeof(WCHAR
) );
1500 io
->Information
+= total_len
;
1505 #ifdef VFAT_IOCTL_READDIR_BOTH
1507 /***********************************************************************
1510 * Wrapper for the VFAT ioctl to work around various kernel bugs.
1511 * dir_section must be held by caller.
1513 static KERNEL_DIRENT
*start_vfat_ioctl( int fd
)
1515 static KERNEL_DIRENT
*de
;
1520 SIZE_T size
= 2 * sizeof(*de
) + page_size
;
1523 if (NtAllocateVirtualMemory( GetCurrentProcess(), &addr
, 1, &size
, MEM_RESERVE
, PAGE_READWRITE
))
1525 /* commit only the size needed for the dir entries */
1526 /* this leaves an extra unaccessible page, which should make the kernel */
1527 /* fail with -EFAULT before it stomps all over our memory */
1529 size
= 2 * sizeof(*de
);
1530 NtAllocateVirtualMemory( GetCurrentProcess(), &addr
, 1, &size
, MEM_COMMIT
, PAGE_READWRITE
);
1533 /* set d_reclen to 65535 to work around an AFS kernel bug */
1534 de
[0].d_reclen
= 65535;
1535 res
= ioctl( fd
, VFAT_IOCTL_READDIR_BOTH
, (long)de
);
1538 if (errno
!= ENOENT
) return NULL
; /* VFAT ioctl probably not supported */
1539 de
[0].d_reclen
= 0; /* eof */
1541 else if (!res
&& de
[0].d_reclen
== 65535) return NULL
; /* AFS bug */
1547 /***********************************************************************
1548 * read_directory_vfat
1550 * Read a directory using the VFAT ioctl; helper for NtQueryDirectoryFile.
1552 static int read_directory_vfat( int fd
, IO_STATUS_BLOCK
*io
, void *buffer
, ULONG length
,
1553 BOOLEAN single_entry
, const UNICODE_STRING
*mask
,
1554 BOOLEAN restart_scan
, FILE_INFORMATION_CLASS
class )
1559 union file_directory_info
*info
, *last_info
= NULL
;
1561 io
->u
.Status
= STATUS_SUCCESS
;
1563 if (restart_scan
) lseek( fd
, 0, SEEK_SET
);
1565 if (length
< max_dir_info_size(class)) /* we may have to return a partial entry here */
1567 off_t old_pos
= lseek( fd
, 0, SEEK_CUR
);
1569 if (!(de
= start_vfat_ioctl( fd
))) return -1; /* not supported */
1571 while (de
[0].d_reclen
)
1573 /* make sure names are null-terminated to work around an x86-64 kernel bug */
1574 len
= min(de
[0].d_reclen
, sizeof(de
[0].d_name
) - 1 );
1575 de
[0].d_name
[len
] = 0;
1576 len
= min(de
[1].d_reclen
, sizeof(de
[1].d_name
) - 1 );
1577 de
[1].d_name
[len
] = 0;
1579 if (de
[1].d_name
[0])
1580 info
= append_entry( buffer
, io
, length
, de
[1].d_name
, de
[0].d_name
, mask
, class );
1582 info
= append_entry( buffer
, io
, length
, de
[0].d_name
, NULL
, mask
, class );
1586 if (io
->u
.Status
== STATUS_BUFFER_OVERFLOW
)
1587 lseek( fd
, old_pos
, SEEK_SET
); /* restore pos to previous entry */
1590 old_pos
= lseek( fd
, 0, SEEK_CUR
);
1591 if (ioctl( fd
, VFAT_IOCTL_READDIR_BOTH
, (long)de
) == -1) break;
1594 else /* we'll only return full entries, no need to worry about overflow */
1596 if (!(de
= start_vfat_ioctl( fd
))) return -1; /* not supported */
1598 while (de
[0].d_reclen
)
1600 /* make sure names are null-terminated to work around an x86-64 kernel bug */
1601 len
= min(de
[0].d_reclen
, sizeof(de
[0].d_name
) - 1 );
1602 de
[0].d_name
[len
] = 0;
1603 len
= min(de
[1].d_reclen
, sizeof(de
[1].d_name
) - 1 );
1604 de
[1].d_name
[len
] = 0;
1606 if (de
[1].d_name
[0])
1607 info
= append_entry( buffer
, io
, length
, de
[1].d_name
, de
[0].d_name
, mask
, class );
1609 info
= append_entry( buffer
, io
, length
, de
[0].d_name
, NULL
, mask
, class );
1613 if (single_entry
) break;
1614 /* check if we still have enough space for the largest possible entry */
1615 if (io
->Information
+ max_dir_info_size(class) > length
) break;
1617 if (ioctl( fd
, VFAT_IOCTL_READDIR_BOTH
, (long)de
) == -1) break;
1621 if (last_info
) last_info
->next
= 0;
1622 else io
->u
.Status
= restart_scan
? STATUS_NO_SUCH_FILE
: STATUS_NO_MORE_FILES
;
1625 #endif /* VFAT_IOCTL_READDIR_BOTH */
1629 /***********************************************************************
1630 * read_first_dent_name
1632 * reads name of first or second dentry (if they have inodes).
1634 static char *read_first_dent_name( int which
, int fd
, off_t second_offs
, KERNEL_DIRENT64
*de_first_two
,
1635 char *buffer
, size_t size
, BOOL
*buffer_changed
)
1637 KERNEL_DIRENT64
*de
;
1644 de
= (KERNEL_DIRENT64
*)((char *)de
+ de
->d_reclen
);
1646 return de
->d_ino
? de
->d_name
: NULL
;
1649 *buffer_changed
= TRUE
;
1650 lseek( fd
, which
== 1 ? second_offs
: 0, SEEK_SET
);
1651 res
= getdents64( fd
, buffer
, size
);
1655 de
= (KERNEL_DIRENT64
*)buffer
;
1656 return de
->d_ino
? de
->d_name
: NULL
;
1659 /***********************************************************************
1660 * read_directory_getdents
1662 * Read a directory using the Linux getdents64 system call; helper for NtQueryDirectoryFile.
1664 static int read_directory_getdents( int fd
, IO_STATUS_BLOCK
*io
, void *buffer
, ULONG length
,
1665 BOOLEAN single_entry
, const UNICODE_STRING
*mask
,
1666 BOOLEAN restart_scan
, FILE_INFORMATION_CLASS
class )
1668 static off_t second_entry_pos
;
1669 static struct file_identity last_dir_id
;
1670 off_t old_pos
= 0, next_pos
;
1671 size_t size
= length
;
1672 char *data
, local_buffer
[8192];
1673 KERNEL_DIRENT64
*de
, *de_first_two
= NULL
;
1674 union file_directory_info
*info
, *last_info
= NULL
;
1675 const char *filename
;
1676 BOOL data_buffer_changed
;
1679 if (size
<= sizeof(local_buffer
) || !(data
= RtlAllocateHeap( GetProcessHeap(), 0, size
)))
1681 size
= sizeof(local_buffer
);
1682 data
= local_buffer
;
1685 if (restart_scan
) lseek( fd
, 0, SEEK_SET
);
1688 old_pos
= lseek( fd
, 0, SEEK_CUR
);
1691 io
->u
.Status
= (errno
== ENOENT
) ? STATUS_NO_MORE_FILES
: FILE_GetNtStatus();
1697 io
->u
.Status
= STATUS_SUCCESS
;
1698 de
= (KERNEL_DIRENT64
*)data
;
1700 /* if old_pos is not 0 we don't know how many entries have been returned already,
1701 * so maintain second_entry_pos to know when to return '..' */
1702 if (old_pos
!= 0 && (last_dir_id
.dev
!= curdir
.dev
|| last_dir_id
.ino
!= curdir
.ino
))
1704 lseek( fd
, 0, SEEK_SET
);
1705 res
= getdents64( fd
, data
, size
);
1708 second_entry_pos
= de
->d_off
;
1709 last_dir_id
= curdir
;
1711 lseek( fd
, old_pos
, SEEK_SET
);
1714 res
= getdents64( fd
, data
, size
);
1717 if (errno
!= ENOSYS
)
1719 io
->u
.Status
= FILE_GetNtStatus();
1725 if (old_pos
== 0 && res
> 0)
1727 second_entry_pos
= de
->d_off
;
1728 last_dir_id
= curdir
;
1729 if (res
> de
->d_reclen
)
1735 res
-= de
->d_reclen
;
1736 next_pos
= de
->d_off
;
1739 /* we must return first 2 entries as "." and "..", but getdents64()
1740 * can return them anywhere, so swap first entries with "." and ".." */
1743 else if (old_pos
== second_entry_pos
)
1745 else if (!strcmp( de
->d_name
, "." ) || !strcmp( de
->d_name
, ".." ))
1747 swap_to
= !strcmp( de
->d_name
, "." ) ? 0 : 1;
1748 data_buffer_changed
= FALSE
;
1750 filename
= read_first_dent_name( swap_to
, fd
, second_entry_pos
, de_first_two
,
1751 data
, size
, &data_buffer_changed
);
1752 if (filename
!= NULL
&& (!strcmp( filename
, "." ) || !strcmp( filename
, ".." )))
1753 filename
= read_first_dent_name( swap_to
^ 1, fd
, second_entry_pos
, NULL
,
1754 data
, size
, &data_buffer_changed
);
1755 if (data_buffer_changed
)
1757 lseek( fd
, next_pos
, SEEK_SET
);
1762 filename
= de
->d_name
;
1764 if (filename
&& (info
= append_entry( buffer
, io
, length
, filename
, NULL
, mask
, class )))
1767 if (io
->u
.Status
== STATUS_BUFFER_OVERFLOW
)
1769 lseek( fd
, old_pos
, SEEK_SET
); /* restore pos to previous entry */
1772 /* check if we still have enough space for the largest possible entry */
1773 if (single_entry
|| io
->Information
+ max_dir_info_size(class) > length
)
1775 if (res
> 0) lseek( fd
, next_pos
, SEEK_SET
); /* set pos to next entry */
1780 /* move on to the next entry */
1781 if (res
> 0) de
= (KERNEL_DIRENT64
*)((char *)de
+ de
->d_reclen
);
1784 res
= getdents64( fd
, data
, size
);
1785 de
= (KERNEL_DIRENT64
*)data
;
1786 de_first_two
= NULL
;
1790 if (last_info
) last_info
->next
= 0;
1791 else io
->u
.Status
= restart_scan
? STATUS_NO_SUCH_FILE
: STATUS_NO_MORE_FILES
;
1794 if (data
!= local_buffer
) RtlFreeHeap( GetProcessHeap(), 0, data
);
1798 #elif defined HAVE_GETDIRENTRIES
1800 #ifdef _DARWIN_FEATURE_64_BIT_INODE
1802 /* Darwin doesn't provide a version of getdirentries with support for 64-bit
1803 * inodes. When 64-bit inodes are enabled, the getdirentries symbol is mapped
1804 * to _getdirentries_is_not_available_when_64_bit_inodes_are_in_effect so that
1805 * we get link errors if we try to use it. We still need getdirentries, but we
1806 * don't need it to support 64-bit inodes. So, we use the legacy getdirentries
1807 * with 32-bit inodes. We have to be careful to use a corresponding dirent
1810 int darwin_legacy_getdirentries(int, char *, int, long *) __asm("_getdirentries");
1811 #define getdirentries darwin_legacy_getdirentries
1813 struct darwin_legacy_dirent
{
1815 __uint16_t d_reclen
;
1818 char d_name
[__DARWIN_MAXNAMLEN
+ 1];
1820 #define dirent darwin_legacy_dirent
1824 /***********************************************************************
1825 * wine_getdirentries
1827 * Wrapper for the BSD getdirentries system call to fix a bug in the
1828 * Mac OS X version. For some file systems (at least Apple Filing
1829 * Protocol a.k.a. AFP), getdirentries resets the file position to 0
1830 * when it's about to return 0 (no more entries). So, a subsequent
1831 * getdirentries call starts over at the beginning again, causing an
1834 static inline int wine_getdirentries(int fd
, char *buf
, int nbytes
, long *basep
)
1836 int res
= getdirentries(fd
, buf
, nbytes
, basep
);
1839 lseek(fd
, *basep
, SEEK_SET
);
1844 static inline int dir_reclen(struct dirent
*de
)
1846 #ifdef HAVE_STRUCT_DIRENT_D_RECLEN
1847 return de
->d_reclen
;
1849 return _DIRENT_RECLEN(de
->d_namlen
);
1853 /***********************************************************************
1854 * read_directory_getdirentries
1856 * Read a directory using the BSD getdirentries system call; helper for NtQueryDirectoryFile.
1858 static int read_directory_getdirentries( int fd
, IO_STATUS_BLOCK
*io
, void *buffer
, ULONG length
,
1859 BOOLEAN single_entry
, const UNICODE_STRING
*mask
,
1860 BOOLEAN restart_scan
, FILE_INFORMATION_CLASS
class )
1863 ULONG_PTR restart_info_pos
= 0;
1864 size_t size
, initial_size
= length
;
1865 int res
, fake_dot_dot
= 1;
1866 char *data
, local_buffer
[8192];
1868 union file_directory_info
*info
, *last_info
= NULL
, *restart_last_info
= NULL
;
1870 size
= initial_size
;
1871 data
= local_buffer
;
1872 if (size
> sizeof(local_buffer
) && !(data
= RtlAllocateHeap( GetProcessHeap(), 0, size
)))
1874 io
->u
.Status
= STATUS_NO_MEMORY
;
1875 return io
->u
.Status
;
1878 if (restart_scan
) lseek( fd
, 0, SEEK_SET
);
1880 io
->u
.Status
= STATUS_SUCCESS
;
1882 /* FIXME: should make sure size is larger than filesystem block size */
1883 res
= wine_getdirentries( fd
, data
, size
, &restart_pos
);
1886 io
->u
.Status
= FILE_GetNtStatus();
1891 de
= (struct dirent
*)data
;
1895 /* check if we got . and .. from getdirentries */
1898 if (!strcmp( de
->d_name
, "." ) && res
> dir_reclen(de
))
1900 struct dirent
*next_de
= (struct dirent
*)(data
+ dir_reclen(de
));
1901 if (!strcmp( next_de
->d_name
, ".." )) fake_dot_dot
= 0;
1904 /* make sure we have enough room for both entries */
1907 const ULONG min_info_size
= dir_info_size( class, 1 ) + dir_info_size( class, 2 );
1908 if (length
< min_info_size
|| single_entry
)
1910 FIXME( "not enough room %u/%u for fake . and .. entries\n", length
, single_entry
);
1917 if ((info
= append_entry( buffer
, io
, length
, ".", NULL
, mask
, class )))
1919 if ((info
= append_entry( buffer
, io
, length
, "..", NULL
, mask
, class )))
1922 restart_last_info
= last_info
;
1923 restart_info_pos
= io
->Information
;
1925 /* check if we still have enough space for the largest possible entry */
1926 if (last_info
&& io
->Information
+ max_dir_info_size(class) > length
)
1928 lseek( fd
, 0, SEEK_SET
); /* reset pos to first entry */
1936 res
-= dir_reclen(de
);
1938 !(fake_dot_dot
&& (!strcmp( de
->d_name
, "." ) || !strcmp( de
->d_name
, ".." ))) &&
1939 ((info
= append_entry( buffer
, io
, length
, de
->d_name
, NULL
, mask
, class ))))
1942 if (io
->u
.Status
== STATUS_BUFFER_OVERFLOW
)
1944 lseek( fd
, (unsigned long)restart_pos
, SEEK_SET
);
1945 if (restart_info_pos
) /* if we have a complete read already, return it */
1947 io
->u
.Status
= STATUS_SUCCESS
;
1948 io
->Information
= restart_info_pos
;
1949 last_info
= restart_last_info
;
1952 /* otherwise restart from the start with a smaller size */
1953 size
= (char *)de
- data
;
1955 io
->Information
= 0;
1959 if (!has_wildcard( mask
)) break;
1960 /* if we have to return but the buffer contains more data, restart with a smaller size */
1961 if (res
> 0 && (single_entry
|| io
->Information
+ max_dir_info_size(class) > length
))
1963 lseek( fd
, (unsigned long)restart_pos
, SEEK_SET
);
1964 size
= (char *)de
+ dir_reclen(de
) - data
;
1965 io
->Information
= restart_info_pos
;
1966 last_info
= restart_last_info
;
1970 /* move on to the next entry */
1973 de
= (struct dirent
*)((char *)de
+ dir_reclen(de
));
1976 if (size
< initial_size
) break; /* already restarted once, give up now */
1977 restart_last_info
= last_info
;
1978 restart_info_pos
= io
->Information
;
1980 res
= wine_getdirentries( fd
, data
, size
, &restart_pos
);
1981 de
= (struct dirent
*)data
;
1984 if (last_info
) last_info
->next
= 0;
1985 else io
->u
.Status
= restart_scan
? STATUS_NO_SUCH_FILE
: STATUS_NO_MORE_FILES
;
1988 if (data
!= local_buffer
) RtlFreeHeap( GetProcessHeap(), 0, data
);
1992 #ifdef _DARWIN_FEATURE_64_BIT_INODE
1993 #undef getdirentries
1997 #endif /* HAVE_GETDIRENTRIES */
2000 /***********************************************************************
2001 * read_directory_readdir
2003 * Read a directory using the POSIX readdir interface; helper for NtQueryDirectoryFile.
2005 static void read_directory_readdir( int fd
, IO_STATUS_BLOCK
*io
, void *buffer
, ULONG length
,
2006 BOOLEAN single_entry
, const UNICODE_STRING
*mask
,
2007 BOOLEAN restart_scan
, FILE_INFORMATION_CLASS
class )
2010 off_t i
, old_pos
= 0;
2012 union file_directory_info
*info
, *last_info
= NULL
;
2014 if (!(dir
= opendir( "." )))
2016 io
->u
.Status
= FILE_GetNtStatus();
2022 old_pos
= lseek( fd
, 0, SEEK_CUR
);
2023 /* skip the right number of entries */
2024 for (i
= 0; i
< old_pos
- 2; i
++)
2026 if (!readdir( dir
))
2029 io
->u
.Status
= STATUS_NO_MORE_FILES
;
2034 io
->u
.Status
= STATUS_SUCCESS
;
2039 info
= append_entry( buffer
, io
, length
, ".", NULL
, mask
, class );
2040 else if (old_pos
== 1)
2041 info
= append_entry( buffer
, io
, length
, "..", NULL
, mask
, class );
2042 else if ((de
= readdir( dir
)))
2044 if (strcmp( de
->d_name
, "." ) && strcmp( de
->d_name
, ".." ))
2045 info
= append_entry( buffer
, io
, length
, de
->d_name
, NULL
, mask
, class );
2055 if (io
->u
.Status
== STATUS_BUFFER_OVERFLOW
)
2057 old_pos
--; /* restore pos to previous entry */
2060 if (single_entry
) break;
2061 /* check if we still have enough space for the largest possible entry */
2062 if (io
->Information
+ max_dir_info_size(class) > length
) break;
2066 lseek( fd
, old_pos
, SEEK_SET
); /* store dir offset as filepos for fd */
2069 if (last_info
) last_info
->next
= 0;
2070 else io
->u
.Status
= restart_scan
? STATUS_NO_SUCH_FILE
: STATUS_NO_MORE_FILES
;
2073 /***********************************************************************
2074 * read_directory_stat
2076 * Read a single file from a directory by determining whether the file
2077 * identified by mask exists using stat.
2079 static int read_directory_stat( int fd
, IO_STATUS_BLOCK
*io
, void *buffer
, ULONG length
,
2080 BOOLEAN single_entry
, const UNICODE_STRING
*mask
,
2081 BOOLEAN restart_scan
, FILE_INFORMATION_CLASS
class )
2083 int unix_len
, ret
, used_default
;
2086 BOOL case_sensitive
= get_dir_case_sensitivity(".");
2088 TRACE("looking up file %s\n", debugstr_us( mask
));
2090 unix_len
= ntdll_wcstoumbs( 0, mask
->Buffer
, mask
->Length
/ sizeof(WCHAR
), NULL
, 0, NULL
, NULL
);
2091 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, unix_len
+ 1)))
2093 io
->u
.Status
= STATUS_NO_MEMORY
;
2096 ret
= ntdll_wcstoumbs( 0, mask
->Buffer
, mask
->Length
/ sizeof(WCHAR
), unix_name
, unix_len
,
2097 NULL
, &used_default
);
2098 if (ret
> 0 && !used_default
)
2103 lseek( fd
, 0, SEEK_SET
);
2105 else if (lseek( fd
, 0, SEEK_CUR
) != 0)
2107 io
->u
.Status
= STATUS_NO_MORE_FILES
;
2112 ret
= stat( unix_name
, &st
);
2113 if (case_sensitive
&& !ret
)
2115 union file_directory_info
*info
= append_entry( buffer
, io
, length
, unix_name
, NULL
, NULL
, class );
2119 if (io
->u
.Status
!= STATUS_BUFFER_OVERFLOW
) lseek( fd
, 1, SEEK_CUR
);
2121 else io
->u
.Status
= STATUS_NO_MORE_FILES
;
2123 else if (!case_sensitive
&& ret
&& (errno
== ENOENT
|| errno
== ENOTDIR
))
2125 /* If the file does not exist, return that info.
2126 * If the file DOES exist, return failure and fallback to the next
2127 * read_directory_* function (we need to return the case-preserved
2128 * filename stored on the filesystem). */
2130 io
->u
.Status
= STATUS_NO_MORE_FILES
;
2140 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2142 TRACE("returning %d\n", ret
);
2147 #ifdef HAVE_GETATTRLIST
2148 /***********************************************************************
2149 * read_directory_getattrlist
2151 * Read a single file from a directory by determining whether the file
2152 * identified by mask exists using getattrlist.
2154 static int read_directory_getattrlist( int fd
, IO_STATUS_BLOCK
*io
, void *buffer
, ULONG length
,
2155 BOOLEAN single_entry
, const UNICODE_STRING
*mask
,
2156 BOOLEAN restart_scan
, FILE_INFORMATION_CLASS
class )
2158 int unix_len
, ret
, used_default
;
2160 struct attrlist attrlist
;
2161 #include "pshpack4.h"
2165 struct attrreference name_reference
;
2169 char name
[NAME_MAX
* 3 + 1];
2171 #include "poppack.h"
2173 TRACE("looking up file %s\n", debugstr_us( mask
));
2175 unix_len
= ntdll_wcstoumbs( 0, mask
->Buffer
, mask
->Length
/ sizeof(WCHAR
), NULL
, 0, NULL
, NULL
);
2176 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, unix_len
+ 1)))
2178 io
->u
.Status
= STATUS_NO_MEMORY
;
2181 ret
= ntdll_wcstoumbs( 0, mask
->Buffer
, mask
->Length
/ sizeof(WCHAR
), unix_name
, unix_len
,
2182 NULL
, &used_default
);
2183 if (ret
> 0 && !used_default
)
2188 lseek( fd
, 0, SEEK_SET
);
2190 else if (lseek( fd
, 0, SEEK_CUR
) != 0)
2192 io
->u
.Status
= STATUS_NO_MORE_FILES
;
2197 memset( &attrlist
, 0, sizeof(attrlist
) );
2198 attrlist
.bitmapcount
= ATTR_BIT_MAP_COUNT
;
2199 attrlist
.commonattr
= ATTR_CMN_NAME
| ATTR_CMN_DEVID
| ATTR_CMN_FSID
| ATTR_CMN_OBJTYPE
;
2200 ret
= getattrlist( unix_name
, &attrlist
, &attrlist_buffer
, sizeof(attrlist_buffer
), FSOPT_NOFOLLOW
);
2201 /* If unix_name named a symlink, the above may have succeeded even if the symlink is broken.
2202 Check that with another call without FSOPT_NOFOLLOW. We don't ask for any attributes. */
2203 if (!ret
&& attrlist_buffer
.type
== VLNK
)
2206 attrlist
.commonattr
= 0;
2207 ret
= getattrlist( unix_name
, &attrlist
, &dummy
, sizeof(dummy
), 0 );
2211 union file_directory_info
*info
= append_entry( buffer
, io
, length
, attrlist_buffer
.name
, NULL
, NULL
, class );
2215 if (io
->u
.Status
!= STATUS_BUFFER_OVERFLOW
) lseek( fd
, 1, SEEK_CUR
);
2217 else io
->u
.Status
= STATUS_NO_MORE_FILES
;
2219 else if ((errno
== ENOENT
|| errno
== ENOTDIR
) &&
2220 !get_dir_case_sensitivity_attr_by_id( attrlist_buffer
.devid
, attrlist_buffer
.fsid
))
2222 io
->u
.Status
= STATUS_NO_MORE_FILES
;
2229 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2231 TRACE("returning %d\n", ret
);
2238 /******************************************************************************
2239 * NtQueryDirectoryFile [NTDLL.@]
2240 * ZwQueryDirectoryFile [NTDLL.@]
2242 NTSTATUS WINAPI
NtQueryDirectoryFile( HANDLE handle
, HANDLE event
,
2243 PIO_APC_ROUTINE apc_routine
, PVOID apc_context
,
2244 PIO_STATUS_BLOCK io
,
2245 PVOID buffer
, ULONG length
,
2246 FILE_INFORMATION_CLASS info_class
,
2247 BOOLEAN single_entry
,
2248 PUNICODE_STRING mask
,
2249 BOOLEAN restart_scan
)
2251 int cwd
, fd
, needs_close
;
2253 TRACE("(%p %p %p %p %p %p 0x%08x 0x%08x 0x%08x %s 0x%08x\n",
2254 handle
, event
, apc_routine
, apc_context
, io
, buffer
,
2255 length
, info_class
, single_entry
, debugstr_us(mask
),
2258 if (event
|| apc_routine
)
2260 FIXME( "Unsupported yet option\n" );
2261 return io
->u
.Status
= STATUS_NOT_IMPLEMENTED
;
2265 case FileDirectoryInformation
:
2266 case FileBothDirectoryInformation
:
2267 case FileFullDirectoryInformation
:
2268 case FileIdBothDirectoryInformation
:
2269 case FileIdFullDirectoryInformation
:
2270 if (length
< dir_info_size( info_class
, 1 )) return io
->u
.Status
= STATUS_INFO_LENGTH_MISMATCH
;
2271 if (!buffer
) return io
->u
.Status
= STATUS_ACCESS_VIOLATION
;
2274 FIXME( "Unsupported file info class %d\n", info_class
);
2275 return io
->u
.Status
= STATUS_NOT_IMPLEMENTED
;
2278 if ((io
->u
.Status
= server_get_unix_fd( handle
, FILE_LIST_DIRECTORY
, &fd
, &needs_close
, NULL
, NULL
)) != STATUS_SUCCESS
)
2279 return io
->u
.Status
;
2281 io
->Information
= 0;
2283 RtlRunOnceExecuteOnce( &init_once
, init_options
, NULL
, NULL
);
2285 RtlEnterCriticalSection( &dir_section
);
2287 cwd
= open( ".", O_RDONLY
);
2288 if (fchdir( fd
) != -1)
2292 curdir
.dev
= st
.st_dev
;
2293 curdir
.ino
= st
.st_ino
;
2294 #ifdef VFAT_IOCTL_READDIR_BOTH
2295 if ((read_directory_vfat( fd
, io
, buffer
, length
, single_entry
,
2296 mask
, restart_scan
, info_class
)) != -1) goto done
;
2298 if (!has_wildcard( mask
))
2300 #ifdef HAVE_GETATTRLIST
2301 if (read_directory_getattrlist( fd
, io
, buffer
, length
, single_entry
,
2302 mask
, restart_scan
, info_class
) != -1) goto done
;
2304 if (read_directory_stat( fd
, io
, buffer
, length
, single_entry
,
2305 mask
, restart_scan
, info_class
) != -1) goto done
;
2308 if ((read_directory_getdents( fd
, io
, buffer
, length
, single_entry
,
2309 mask
, restart_scan
, info_class
)) != -1) goto done
;
2310 #elif defined HAVE_GETDIRENTRIES
2311 if ((read_directory_getdirentries( fd
, io
, buffer
, length
, single_entry
,
2312 mask
, restart_scan
, info_class
)) != -1) goto done
;
2314 read_directory_readdir( fd
, io
, buffer
, length
, single_entry
, mask
, restart_scan
, info_class
);
2317 if (cwd
== -1 || fchdir( cwd
) == -1) chdir( "/" );
2319 else io
->u
.Status
= FILE_GetNtStatus();
2321 RtlLeaveCriticalSection( &dir_section
);
2323 if (needs_close
) close( fd
);
2324 if (cwd
!= -1) close( cwd
);
2325 TRACE( "=> %x (%ld)\n", io
->u
.Status
, io
->Information
);
2326 return io
->u
.Status
;
2330 /***********************************************************************
2333 * Find a file in a directory the hard way, by doing a case-insensitive search.
2334 * The file found is appended to unix_name at pos.
2335 * There must be at least MAX_DIR_ENTRY_LEN+2 chars available at pos.
2337 static NTSTATUS
find_file_in_dir( char *unix_name
, int pos
, const WCHAR
*name
, int length
,
2338 BOOLEAN check_case
, BOOLEAN
*is_win_dir
)
2340 WCHAR buffer
[MAX_DIR_ENTRY_LEN
];
2342 BOOLEAN spaces
, is_name_8_dot_3
;
2346 int ret
, used_default
;
2348 /* try a shortcut for this directory */
2350 unix_name
[pos
++] = '/';
2351 ret
= ntdll_wcstoumbs( 0, name
, length
, unix_name
+ pos
, MAX_DIR_ENTRY_LEN
,
2352 NULL
, &used_default
);
2353 /* if we used the default char, the Unix name won't round trip properly back to Unicode */
2354 /* so it cannot match the file we are looking for */
2355 if (ret
>= 0 && !used_default
)
2357 unix_name
[pos
+ ret
] = 0;
2358 if (!stat( unix_name
, &st
))
2360 if (is_win_dir
) *is_win_dir
= is_same_file( &windir
, &st
);
2361 return STATUS_SUCCESS
;
2364 if (check_case
) goto not_found
; /* we want an exact match */
2366 if (pos
> 1) unix_name
[pos
- 1] = 0;
2367 else unix_name
[1] = 0; /* keep the initial slash */
2369 /* check if it fits in 8.3 so that we don't look for short names if we won't need them */
2371 str
.Buffer
= (WCHAR
*)name
;
2372 str
.Length
= length
* sizeof(WCHAR
);
2373 str
.MaximumLength
= str
.Length
;
2374 is_name_8_dot_3
= RtlIsNameLegalDOS8Dot3( &str
, NULL
, &spaces
) && !spaces
;
2375 #ifndef VFAT_IOCTL_READDIR_BOTH
2376 is_name_8_dot_3
= is_name_8_dot_3
&& length
>= 8 && name
[4] == '~';
2379 if (!is_name_8_dot_3
&& !get_dir_case_sensitivity( unix_name
)) goto not_found
;
2381 /* now look for it through the directory */
2383 #ifdef VFAT_IOCTL_READDIR_BOTH
2384 if (is_name_8_dot_3
)
2386 int fd
= open( unix_name
, O_RDONLY
| O_DIRECTORY
);
2391 RtlEnterCriticalSection( &dir_section
);
2392 if ((kde
= start_vfat_ioctl( fd
)))
2394 unix_name
[pos
- 1] = '/';
2395 while (kde
[0].d_reclen
)
2397 /* make sure names are null-terminated to work around an x86-64 kernel bug */
2398 size_t len
= min(kde
[0].d_reclen
, sizeof(kde
[0].d_name
) - 1 );
2399 kde
[0].d_name
[len
] = 0;
2400 len
= min(kde
[1].d_reclen
, sizeof(kde
[1].d_name
) - 1 );
2401 kde
[1].d_name
[len
] = 0;
2403 if (kde
[1].d_name
[0])
2405 ret
= ntdll_umbstowcs( 0, kde
[1].d_name
, strlen(kde
[1].d_name
),
2406 buffer
, MAX_DIR_ENTRY_LEN
);
2407 if (ret
== length
&& !memicmpW( buffer
, name
, length
))
2409 strcpy( unix_name
+ pos
, kde
[1].d_name
);
2410 RtlLeaveCriticalSection( &dir_section
);
2415 ret
= ntdll_umbstowcs( 0, kde
[0].d_name
, strlen(kde
[0].d_name
),
2416 buffer
, MAX_DIR_ENTRY_LEN
);
2417 if (ret
== length
&& !memicmpW( buffer
, name
, length
))
2419 strcpy( unix_name
+ pos
,
2420 kde
[1].d_name
[0] ? kde
[1].d_name
: kde
[0].d_name
);
2421 RtlLeaveCriticalSection( &dir_section
);
2425 if (ioctl( fd
, VFAT_IOCTL_READDIR_BOTH
, (long)kde
) == -1)
2427 RtlLeaveCriticalSection( &dir_section
);
2433 RtlLeaveCriticalSection( &dir_section
);
2436 /* fall through to normal handling */
2438 #endif /* VFAT_IOCTL_READDIR_BOTH */
2440 if (!(dir
= opendir( unix_name
)))
2442 if (errno
== ENOENT
) return STATUS_OBJECT_PATH_NOT_FOUND
;
2443 else return FILE_GetNtStatus();
2445 unix_name
[pos
- 1] = '/';
2446 str
.Buffer
= buffer
;
2447 str
.MaximumLength
= sizeof(buffer
);
2448 while ((de
= readdir( dir
)))
2450 ret
= ntdll_umbstowcs( 0, de
->d_name
, strlen(de
->d_name
), buffer
, MAX_DIR_ENTRY_LEN
);
2451 if (ret
== length
&& !memicmpW( buffer
, name
, length
))
2453 strcpy( unix_name
+ pos
, de
->d_name
);
2458 if (!is_name_8_dot_3
) continue;
2460 str
.Length
= ret
* sizeof(WCHAR
);
2461 if (!RtlIsNameLegalDOS8Dot3( &str
, NULL
, &spaces
) || spaces
)
2463 WCHAR short_nameW
[12];
2464 ret
= hash_short_file_name( &str
, short_nameW
);
2465 if (ret
== length
&& !memicmpW( short_nameW
, name
, length
))
2467 strcpy( unix_name
+ pos
, de
->d_name
);
2476 unix_name
[pos
- 1] = 0;
2477 return STATUS_OBJECT_PATH_NOT_FOUND
;
2480 if (is_win_dir
&& !stat( unix_name
, &st
)) *is_win_dir
= is_same_file( &windir
, &st
);
2481 return STATUS_SUCCESS
;
2487 static const WCHAR catrootW
[] = {'s','y','s','t','e','m','3','2','\\','c','a','t','r','o','o','t',0};
2488 static const WCHAR catroot2W
[] = {'s','y','s','t','e','m','3','2','\\','c','a','t','r','o','o','t','2',0};
2489 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};
2490 static const WCHAR driversetcW
[] = {'s','y','s','t','e','m','3','2','\\','d','r','i','v','e','r','s','\\','e','t','c',0};
2491 static const WCHAR logfilesW
[] = {'s','y','s','t','e','m','3','2','\\','l','o','g','f','i','l','e','s',0};
2492 static const WCHAR spoolW
[] = {'s','y','s','t','e','m','3','2','\\','s','p','o','o','l',0};
2493 static const WCHAR system32W
[] = {'s','y','s','t','e','m','3','2',0};
2494 static const WCHAR syswow64W
[] = {'s','y','s','w','o','w','6','4',0};
2495 static const WCHAR sysnativeW
[] = {'s','y','s','n','a','t','i','v','e',0};
2496 static const WCHAR regeditW
[] = {'r','e','g','e','d','i','t','.','e','x','e',0};
2497 static const WCHAR wow_regeditW
[] = {'s','y','s','w','o','w','6','4','\\','r','e','g','e','d','i','t','.','e','x','e',0};
2501 const WCHAR
*source
;
2502 const WCHAR
*dos_target
;
2503 const char *unix_target
;
2506 { catrootW
, NULL
, NULL
},
2507 { catroot2W
, NULL
, NULL
},
2508 { driversstoreW
, NULL
, NULL
},
2509 { driversetcW
, NULL
, NULL
},
2510 { logfilesW
, NULL
, NULL
},
2511 { spoolW
, NULL
, NULL
},
2512 { system32W
, syswow64W
, NULL
},
2513 { sysnativeW
, system32W
, NULL
},
2514 { regeditW
, wow_regeditW
, NULL
}
2517 static unsigned int nb_redirects
;
2520 /***********************************************************************
2521 * get_redirect_target
2523 * Find the target unix name for a redirected dir.
2525 static const char *get_redirect_target( const char *windows_dir
, const WCHAR
*name
)
2527 int used_default
, len
, pos
, win_len
= strlen( windows_dir
);
2528 char *unix_name
, *unix_target
= NULL
;
2531 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, win_len
+ MAX_DIR_ENTRY_LEN
+ 2 )))
2533 memcpy( unix_name
, windows_dir
, win_len
);
2538 const WCHAR
*end
, *next
;
2540 for (end
= name
; *end
; end
++) if (IS_SEPARATOR(*end
)) break;
2541 for (next
= end
; *next
; next
++) if (!IS_SEPARATOR(*next
)) break;
2543 status
= find_file_in_dir( unix_name
, pos
, name
, end
- name
, FALSE
, NULL
);
2544 if (status
== STATUS_OBJECT_PATH_NOT_FOUND
&& !*next
) /* not finding last element is ok */
2546 len
= ntdll_wcstoumbs( 0, name
, end
- name
, unix_name
+ pos
+ 1,
2547 MAX_DIR_ENTRY_LEN
- (pos
- win_len
), NULL
, &used_default
);
2548 if (len
> 0 && !used_default
)
2550 unix_name
[pos
] = '/';
2556 if (status
) goto done
;
2557 pos
+= strlen( unix_name
+ pos
);
2561 if ((unix_target
= RtlAllocateHeap( GetProcessHeap(), 0, pos
- win_len
)))
2562 memcpy( unix_target
, unix_name
+ win_len
+ 1, pos
- win_len
);
2565 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2570 /***********************************************************************
2573 static void init_redirects(void)
2575 UNICODE_STRING nt_name
;
2576 ANSI_STRING unix_name
;
2581 if (!RtlDosPathNameToNtPathName_U( user_shared_data
->NtSystemRoot
, &nt_name
, NULL
, NULL
))
2583 ERR( "can't convert %s\n", debugstr_w(user_shared_data
->NtSystemRoot
) );
2586 status
= wine_nt_to_unix_file_name( &nt_name
, &unix_name
, FILE_OPEN_IF
, FALSE
);
2587 RtlFreeUnicodeString( &nt_name
);
2590 ERR( "cannot open %s (%x)\n", debugstr_w(user_shared_data
->NtSystemRoot
), status
);
2593 if (!stat( unix_name
.Buffer
, &st
))
2595 windir
.dev
= st
.st_dev
;
2596 windir
.ino
= st
.st_ino
;
2597 nb_redirects
= sizeof(redirects
) / sizeof(redirects
[0]);
2598 for (i
= 0; i
< nb_redirects
; i
++)
2600 if (!redirects
[i
].dos_target
) continue;
2601 redirects
[i
].unix_target
= get_redirect_target( unix_name
.Buffer
, redirects
[i
].dos_target
);
2602 TRACE( "%s -> %s\n", debugstr_w(redirects
[i
].source
), redirects
[i
].unix_target
);
2605 RtlFreeAnsiString( &unix_name
);
2610 /***********************************************************************
2613 * Check if path matches a redirect name. If yes, return matched length.
2615 static int match_redirect( const WCHAR
*path
, int len
, const WCHAR
*redir
, BOOLEAN check_case
)
2619 while (i
< len
&& *redir
)
2621 if (IS_SEPARATOR(path
[i
]))
2623 if (*redir
++ != '\\') return 0;
2624 while (i
< len
&& IS_SEPARATOR(path
[i
])) i
++;
2625 continue; /* move on to next path component */
2627 else if (check_case
)
2629 if (path
[i
] != *redir
) return 0;
2633 if (tolowerW(path
[i
]) != tolowerW(*redir
)) return 0;
2638 if (*redir
) return 0;
2639 if (i
< len
&& !IS_SEPARATOR(path
[i
])) return 0;
2640 while (i
< len
&& IS_SEPARATOR(path
[i
])) i
++;
2645 /***********************************************************************
2648 * Retrieve the Unix path corresponding to a redirected path if any.
2650 static int get_redirect_path( char *unix_name
, int pos
, const WCHAR
*name
, int length
, BOOLEAN check_case
)
2655 for (i
= 0; i
< nb_redirects
; i
++)
2657 if ((len
= match_redirect( name
, length
, redirects
[i
].source
, check_case
)))
2659 if (!redirects
[i
].unix_target
) break;
2660 unix_name
[pos
++] = '/';
2661 strcpy( unix_name
+ pos
, redirects
[i
].unix_target
);
2670 /* there are no redirects on 64-bit */
2672 static const unsigned int nb_redirects
= 0;
2674 static int get_redirect_path( char *unix_name
, int pos
, const WCHAR
*name
, int length
, BOOLEAN check_case
)
2681 /***********************************************************************
2682 * DIR_init_windows_dir
2684 void DIR_init_windows_dir( const WCHAR
*win
, const WCHAR
*sys
)
2686 /* FIXME: should probably store paths as NT file names */
2688 RtlCreateUnicodeString( &system_dir
, sys
);
2691 if (is_wow64
) init_redirects();
2696 /******************************************************************************
2699 * Get the Unix path of a DOS device.
2701 static NTSTATUS
get_dos_device( const WCHAR
*name
, UINT name_len
, ANSI_STRING
*unix_name_ret
)
2703 const char *config_dir
= wine_get_config_dir();
2705 char *unix_name
, *new_name
, *dev
;
2709 /* make sure the device name is ASCII */
2710 for (i
= 0; i
< name_len
; i
++)
2711 if (name
[i
] <= 32 || name
[i
] >= 127) return STATUS_BAD_DEVICE_TYPE
;
2713 unix_len
= strlen(config_dir
) + sizeof("/dosdevices/") + name_len
+ 1;
2715 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, unix_len
)))
2716 return STATUS_NO_MEMORY
;
2718 strcpy( unix_name
, config_dir
);
2719 strcat( unix_name
, "/dosdevices/" );
2720 dev
= unix_name
+ strlen(unix_name
);
2722 for (i
= 0; i
< name_len
; i
++) dev
[i
] = (char)tolowerW(name
[i
]);
2725 /* special case for drive devices */
2726 if (name_len
== 2 && dev
[1] == ':')
2734 if (!stat( unix_name
, &st
))
2736 TRACE( "%s -> %s\n", debugstr_wn(name
,name_len
), debugstr_a(unix_name
) );
2737 unix_name_ret
->Buffer
= unix_name
;
2738 unix_name_ret
->Length
= strlen(unix_name
);
2739 unix_name_ret
->MaximumLength
= unix_len
;
2740 return STATUS_SUCCESS
;
2744 /* now try some defaults for it */
2745 if (!strcmp( dev
, "aux" ))
2747 strcpy( dev
, "com1" );
2750 if (!strcmp( dev
, "prn" ))
2752 strcpy( dev
, "lpt1" );
2757 if (dev
[1] == ':' && dev
[2] == ':') /* drive device */
2759 dev
[2] = 0; /* remove last ':' to get the drive mount point symlink */
2760 new_name
= get_default_drive_device( unix_name
);
2762 else if (!strncmp( dev
, "com", 3 )) new_name
= get_default_com_device( atoi(dev
+ 3 ));
2763 else if (!strncmp( dev
, "lpt", 3 )) new_name
= get_default_lpt_device( atoi(dev
+ 3 ));
2765 if (!new_name
) break;
2767 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2768 unix_name
= new_name
;
2769 unix_len
= strlen(unix_name
) + 1;
2770 dev
= NULL
; /* last try */
2772 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2773 return STATUS_BAD_DEVICE_TYPE
;
2777 /* return the length of the DOS namespace prefix if any */
2778 static inline int get_dos_prefix_len( const UNICODE_STRING
*name
)
2780 static const WCHAR nt_prefixW
[] = {'\\','?','?','\\'};
2781 static const WCHAR dosdev_prefixW
[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\'};
2783 if (name
->Length
> sizeof(nt_prefixW
) &&
2784 !memcmp( name
->Buffer
, nt_prefixW
, sizeof(nt_prefixW
) ))
2785 return sizeof(nt_prefixW
) / sizeof(WCHAR
);
2787 if (name
->Length
> sizeof(dosdev_prefixW
) &&
2788 !memicmpW( name
->Buffer
, dosdev_prefixW
, sizeof(dosdev_prefixW
)/sizeof(WCHAR
) ))
2789 return sizeof(dosdev_prefixW
) / sizeof(WCHAR
);
2795 /******************************************************************************
2798 * Recursively search directories from the dir queue for a given inode.
2800 static NTSTATUS
find_file_id( ANSI_STRING
*unix_name
, ULONGLONG file_id
, dev_t dev
)
2808 while (!(status
= next_dir_in_queue( unix_name
->Buffer
)))
2810 if (!(dir
= opendir( unix_name
->Buffer
))) continue;
2811 TRACE( "searching %s for %s\n", unix_name
->Buffer
, wine_dbgstr_longlong(file_id
) );
2812 pos
= strlen( unix_name
->Buffer
);
2813 if (pos
+ MAX_DIR_ENTRY_LEN
>= unix_name
->MaximumLength
/sizeof(WCHAR
))
2815 char *new = RtlReAllocateHeap( GetProcessHeap(), 0, unix_name
->Buffer
,
2816 unix_name
->MaximumLength
* 2 );
2820 return STATUS_NO_MEMORY
;
2822 unix_name
->MaximumLength
*= 2;
2823 unix_name
->Buffer
= new;
2825 unix_name
->Buffer
[pos
++] = '/';
2826 while ((de
= readdir( dir
)))
2828 if (!strcmp( de
->d_name
, "." ) || !strcmp( de
->d_name
, ".." )) continue;
2829 strcpy( unix_name
->Buffer
+ pos
, de
->d_name
);
2830 if (lstat( unix_name
->Buffer
, &st
) == -1) continue;
2831 if (st
.st_dev
!= dev
) continue;
2832 if (st
.st_ino
== file_id
)
2835 return STATUS_SUCCESS
;
2837 if (!S_ISDIR( st
.st_mode
)) continue;
2838 if ((status
= add_dir_to_queue( unix_name
->Buffer
)) != STATUS_SUCCESS
)
2850 /******************************************************************************
2851 * file_id_to_unix_file_name
2853 * Lookup a file from its file id instead of its name.
2855 NTSTATUS
file_id_to_unix_file_name( const OBJECT_ATTRIBUTES
*attr
, ANSI_STRING
*unix_name
)
2857 enum server_fd_type type
;
2858 int old_cwd
, root_fd
, needs_close
;
2861 struct stat st
, root_st
;
2863 if (attr
->ObjectName
->Length
!= sizeof(ULONGLONG
)) return STATUS_OBJECT_PATH_SYNTAX_BAD
;
2864 if (!attr
->RootDirectory
) return STATUS_INVALID_PARAMETER
;
2865 memcpy( &file_id
, attr
->ObjectName
->Buffer
, sizeof(file_id
) );
2867 unix_name
->MaximumLength
= 2 * MAX_DIR_ENTRY_LEN
+ 4;
2868 if (!(unix_name
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, unix_name
->MaximumLength
)))
2869 return STATUS_NO_MEMORY
;
2870 strcpy( unix_name
->Buffer
, "." );
2872 if ((status
= server_get_unix_fd( attr
->RootDirectory
, 0, &root_fd
, &needs_close
, &type
, NULL
)))
2875 if (type
!= FD_TYPE_DIR
)
2877 status
= STATUS_OBJECT_TYPE_MISMATCH
;
2881 fstat( root_fd
, &root_st
);
2882 if (root_st
.st_ino
== file_id
) /* shortcut for "." */
2884 status
= STATUS_SUCCESS
;
2888 RtlEnterCriticalSection( &dir_section
);
2889 if ((old_cwd
= open( ".", O_RDONLY
)) != -1 && fchdir( root_fd
) != -1)
2891 /* shortcut for ".." */
2892 if (!stat( "..", &st
) && st
.st_dev
== root_st
.st_dev
&& st
.st_ino
== file_id
)
2894 strcpy( unix_name
->Buffer
, ".." );
2895 status
= STATUS_SUCCESS
;
2899 status
= add_dir_to_queue( "." );
2901 status
= find_file_id( unix_name
, file_id
, root_st
.st_dev
);
2902 if (!status
) /* get rid of "./" prefix */
2903 memmove( unix_name
->Buffer
, unix_name
->Buffer
+ 2, strlen(unix_name
->Buffer
) - 1 );
2906 if (fchdir( old_cwd
) == -1) chdir( "/" );
2908 else status
= FILE_GetNtStatus();
2909 RtlLeaveCriticalSection( &dir_section
);
2910 if (old_cwd
!= -1) close( old_cwd
);
2913 if (status
== STATUS_SUCCESS
)
2915 TRACE( "%s -> %s\n", wine_dbgstr_longlong(file_id
), debugstr_a(unix_name
->Buffer
) );
2916 unix_name
->Length
= strlen( unix_name
->Buffer
);
2920 TRACE( "%s not found in dir %p\n", wine_dbgstr_longlong(file_id
), attr
->RootDirectory
);
2921 RtlFreeHeap( GetProcessHeap(), 0, unix_name
->Buffer
);
2923 if (needs_close
) close( root_fd
);
2928 /******************************************************************************
2931 * Helper for nt_to_unix_file_name
2933 static NTSTATUS
lookup_unix_name( const WCHAR
*name
, int name_len
, char **buffer
, int unix_len
, int pos
,
2934 UINT disposition
, BOOLEAN check_case
)
2937 int ret
, used_default
, len
;
2939 char *unix_name
= *buffer
;
2940 const BOOL redirect
= nb_redirects
&& ntdll_get_thread_data()->wow64_redir
;
2942 /* try a shortcut first */
2944 ret
= ntdll_wcstoumbs( 0, name
, name_len
, unix_name
+ pos
, unix_len
- pos
- 1,
2945 NULL
, &used_default
);
2947 while (name_len
&& IS_SEPARATOR(*name
))
2953 if (ret
>= 0 && !used_default
) /* if we used the default char the name didn't convert properly */
2956 unix_name
[pos
+ ret
] = 0;
2957 for (p
= unix_name
+ pos
; *p
; p
++) if (*p
== '\\') *p
= '/';
2958 if (!redirect
|| (!strstr( unix_name
, "/windows/") && strncmp( unix_name
, "windows/", 8 )))
2960 if (!stat( unix_name
, &st
))
2962 /* creation fails with STATUS_ACCESS_DENIED for the root of the drive */
2963 if (disposition
== FILE_CREATE
)
2964 return name_len
? STATUS_OBJECT_NAME_COLLISION
: STATUS_ACCESS_DENIED
;
2965 return STATUS_SUCCESS
;
2970 if (!name_len
) /* empty name -> drive root doesn't exist */
2971 return STATUS_OBJECT_PATH_NOT_FOUND
;
2972 if (check_case
&& !redirect
&& (disposition
== FILE_OPEN
|| disposition
== FILE_OVERWRITE
))
2973 return STATUS_OBJECT_NAME_NOT_FOUND
;
2975 /* now do it component by component */
2979 const WCHAR
*end
, *next
;
2980 BOOLEAN is_win_dir
= FALSE
;
2983 while (end
< name
+ name_len
&& !IS_SEPARATOR(*end
)) end
++;
2985 while (next
< name
+ name_len
&& IS_SEPARATOR(*next
)) next
++;
2986 name_len
-= next
- name
;
2988 /* grow the buffer if needed */
2990 if (unix_len
- pos
< MAX_DIR_ENTRY_LEN
+ 2)
2993 unix_len
+= 2 * MAX_DIR_ENTRY_LEN
;
2994 if (!(new_name
= RtlReAllocateHeap( GetProcessHeap(), 0, unix_name
, unix_len
)))
2995 return STATUS_NO_MEMORY
;
2996 unix_name
= *buffer
= new_name
;
2999 status
= find_file_in_dir( unix_name
, pos
, name
, end
- name
,
3000 check_case
, redirect
? &is_win_dir
: NULL
);
3002 /* if this is the last element, not finding it is not necessarily fatal */
3005 if (status
== STATUS_OBJECT_PATH_NOT_FOUND
)
3007 status
= STATUS_OBJECT_NAME_NOT_FOUND
;
3008 if (disposition
!= FILE_OPEN
&& disposition
!= FILE_OVERWRITE
)
3010 ret
= ntdll_wcstoumbs( 0, name
, end
- name
, unix_name
+ pos
+ 1,
3011 MAX_DIR_ENTRY_LEN
, NULL
, &used_default
);
3012 if (ret
> 0 && !used_default
)
3014 unix_name
[pos
] = '/';
3015 unix_name
[pos
+ 1 + ret
] = 0;
3016 status
= STATUS_NO_SUCH_FILE
;
3021 else if (status
== STATUS_SUCCESS
&& disposition
== FILE_CREATE
)
3023 status
= STATUS_OBJECT_NAME_COLLISION
;
3027 if (status
!= STATUS_SUCCESS
) break;
3029 pos
+= strlen( unix_name
+ pos
);
3032 if (is_win_dir
&& (len
= get_redirect_path( unix_name
, pos
, name
, name_len
, check_case
)))
3036 pos
+= strlen( unix_name
+ pos
);
3037 TRACE( "redirecting -> %s + %s\n", debugstr_a(unix_name
), debugstr_w(name
) );
3045 /******************************************************************************
3046 * nt_to_unix_file_name_attr
3048 NTSTATUS
nt_to_unix_file_name_attr( const OBJECT_ATTRIBUTES
*attr
, ANSI_STRING
*unix_name_ret
,
3051 static const WCHAR invalid_charsW
[] = { INVALID_NT_CHARS
, 0 };
3052 enum server_fd_type type
;
3053 int old_cwd
, root_fd
, needs_close
;
3054 const WCHAR
*name
, *p
;
3056 int name_len
, unix_len
;
3058 BOOLEAN check_case
= !(attr
->Attributes
& OBJ_CASE_INSENSITIVE
);
3060 if (!attr
->RootDirectory
) /* without root dir fall back to normal lookup */
3061 return wine_nt_to_unix_file_name( attr
->ObjectName
, unix_name_ret
, disposition
, check_case
);
3063 name
= attr
->ObjectName
->Buffer
;
3064 name_len
= attr
->ObjectName
->Length
/ sizeof(WCHAR
);
3066 if (name_len
&& IS_SEPARATOR(name
[0])) return STATUS_INVALID_PARAMETER
;
3068 /* check for invalid characters */
3069 for (p
= name
; p
< name
+ name_len
; p
++)
3070 if (*p
< 32 || strchrW( invalid_charsW
, *p
)) return STATUS_OBJECT_NAME_INVALID
;
3072 unix_len
= ntdll_wcstoumbs( 0, name
, name_len
, NULL
, 0, NULL
, NULL
);
3073 unix_len
+= MAX_DIR_ENTRY_LEN
+ 3;
3074 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, unix_len
)))
3075 return STATUS_NO_MEMORY
;
3078 if (!(status
= server_get_unix_fd( attr
->RootDirectory
, 0, &root_fd
, &needs_close
, &type
, NULL
)))
3080 if (type
!= FD_TYPE_DIR
)
3082 if (needs_close
) close( root_fd
);
3083 status
= STATUS_BAD_DEVICE_TYPE
;
3087 RtlEnterCriticalSection( &dir_section
);
3088 if ((old_cwd
= open( ".", O_RDONLY
)) != -1 && fchdir( root_fd
) != -1)
3090 status
= lookup_unix_name( name
, name_len
, &unix_name
, unix_len
, 1,
3091 disposition
, check_case
);
3092 if (fchdir( old_cwd
) == -1) chdir( "/" );
3094 else status
= FILE_GetNtStatus();
3095 RtlLeaveCriticalSection( &dir_section
);
3096 if (old_cwd
!= -1) close( old_cwd
);
3097 if (needs_close
) close( root_fd
);
3100 else if (status
== STATUS_OBJECT_TYPE_MISMATCH
) status
= STATUS_BAD_DEVICE_TYPE
;
3102 if (status
== STATUS_SUCCESS
|| status
== STATUS_NO_SUCH_FILE
)
3104 TRACE( "%s -> %s\n", debugstr_us(attr
->ObjectName
), debugstr_a(unix_name
) );
3105 unix_name_ret
->Buffer
= unix_name
;
3106 unix_name_ret
->Length
= strlen(unix_name
);
3107 unix_name_ret
->MaximumLength
= unix_len
;
3111 TRACE( "%s not found in %s\n", debugstr_w(name
), unix_name
);
3112 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
3118 /******************************************************************************
3119 * wine_nt_to_unix_file_name (NTDLL.@) Not a Windows API
3121 * Convert a file name from NT namespace to Unix namespace.
3123 * If disposition is not FILE_OPEN or FILE_OVERWRITE, the last path
3124 * element doesn't have to exist; in that case STATUS_NO_SUCH_FILE is
3125 * returned, but the unix name is still filled in properly.
3127 NTSTATUS CDECL
wine_nt_to_unix_file_name( const UNICODE_STRING
*nameW
, ANSI_STRING
*unix_name_ret
,
3128 UINT disposition
, BOOLEAN check_case
)
3130 static const WCHAR unixW
[] = {'u','n','i','x'};
3131 static const WCHAR invalid_charsW
[] = { INVALID_NT_CHARS
, 0 };
3133 NTSTATUS status
= STATUS_SUCCESS
;
3134 const char *config_dir
= wine_get_config_dir();
3135 const WCHAR
*name
, *p
;
3138 int pos
, ret
, name_len
, unix_len
, prefix_len
, used_default
;
3139 WCHAR prefix
[MAX_DIR_ENTRY_LEN
];
3140 BOOLEAN is_unix
= FALSE
;
3142 name
= nameW
->Buffer
;
3143 name_len
= nameW
->Length
/ sizeof(WCHAR
);
3145 if (!name_len
|| !IS_SEPARATOR(name
[0])) return STATUS_OBJECT_PATH_SYNTAX_BAD
;
3147 if (!(pos
= get_dos_prefix_len( nameW
)))
3148 return STATUS_BAD_DEVICE_TYPE
; /* no DOS prefix, assume NT native name */
3153 /* check for sub-directory */
3154 for (pos
= 0; pos
< name_len
; pos
++)
3156 if (IS_SEPARATOR(name
[pos
])) break;
3157 if (name
[pos
] < 32 || strchrW( invalid_charsW
, name
[pos
] ))
3158 return STATUS_OBJECT_NAME_INVALID
;
3160 if (pos
> MAX_DIR_ENTRY_LEN
)
3161 return STATUS_OBJECT_NAME_INVALID
;
3163 if (pos
== name_len
) /* no subdir, plain DOS device */
3164 return get_dos_device( name
, name_len
, unix_name_ret
);
3166 for (prefix_len
= 0; prefix_len
< pos
; prefix_len
++)
3167 prefix
[prefix_len
] = tolowerW(name
[prefix_len
]);
3170 name_len
-= prefix_len
;
3172 /* check for invalid characters (all chars except 0 are valid for unix) */
3173 is_unix
= (prefix_len
== 4 && !memcmp( prefix
, unixW
, sizeof(unixW
) ));
3176 for (p
= name
; p
< name
+ name_len
; p
++)
3177 if (!*p
) return STATUS_OBJECT_NAME_INVALID
;
3182 for (p
= name
; p
< name
+ name_len
; p
++)
3183 if (*p
< 32 || strchrW( invalid_charsW
, *p
)) return STATUS_OBJECT_NAME_INVALID
;
3186 unix_len
= ntdll_wcstoumbs( 0, prefix
, prefix_len
, NULL
, 0, NULL
, NULL
);
3187 unix_len
+= ntdll_wcstoumbs( 0, name
, name_len
, NULL
, 0, NULL
, NULL
);
3188 unix_len
+= MAX_DIR_ENTRY_LEN
+ 3;
3189 unix_len
+= strlen(config_dir
) + sizeof("/dosdevices/");
3190 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, unix_len
)))
3191 return STATUS_NO_MEMORY
;
3192 strcpy( unix_name
, config_dir
);
3193 strcat( unix_name
, "/dosdevices/" );
3194 pos
= strlen(unix_name
);
3196 ret
= ntdll_wcstoumbs( 0, prefix
, prefix_len
, unix_name
+ pos
, unix_len
- pos
- 1,
3197 NULL
, &used_default
);
3198 if (!ret
|| used_default
)
3200 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
3201 return STATUS_OBJECT_NAME_INVALID
;
3205 /* check if prefix exists (except for DOS drives to avoid extra stat calls) */
3207 if (prefix_len
!= 2 || prefix
[1] != ':')
3210 if (lstat( unix_name
, &st
) == -1 && errno
== ENOENT
)
3214 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
3215 return STATUS_BAD_DEVICE_TYPE
;
3217 pos
= 0; /* fall back to unix root */
3221 status
= lookup_unix_name( name
, name_len
, &unix_name
, unix_len
, pos
, disposition
, check_case
);
3222 if (status
== STATUS_SUCCESS
|| status
== STATUS_NO_SUCH_FILE
)
3224 TRACE( "%s -> %s\n", debugstr_us(nameW
), debugstr_a(unix_name
) );
3225 unix_name_ret
->Buffer
= unix_name
;
3226 unix_name_ret
->Length
= strlen(unix_name
);
3227 unix_name_ret
->MaximumLength
= unix_len
;
3231 TRACE( "%s not found in %s\n", debugstr_w(name
), unix_name
);
3232 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
3238 /******************************************************************
3239 * RtlWow64EnableFsRedirection (NTDLL.@)
3241 NTSTATUS WINAPI
RtlWow64EnableFsRedirection( BOOLEAN enable
)
3243 if (!is_wow64
) return STATUS_NOT_IMPLEMENTED
;
3244 ntdll_get_thread_data()->wow64_redir
= enable
;
3245 return STATUS_SUCCESS
;
3249 /******************************************************************
3250 * RtlWow64EnableFsRedirectionEx (NTDLL.@)
3252 NTSTATUS WINAPI
RtlWow64EnableFsRedirectionEx( ULONG disable
, ULONG
*old_value
)
3254 if (!is_wow64
) return STATUS_NOT_IMPLEMENTED
;
3255 if (((ULONG_PTR
)old_value
>> 16) == 0) return STATUS_ACCESS_VIOLATION
;
3257 *old_value
= !ntdll_get_thread_data()->wow64_redir
;
3258 ntdll_get_thread_data()->wow64_redir
= !disable
;
3259 return STATUS_SUCCESS
;
3263 /******************************************************************
3264 * RtlDoesFileExists_U (NTDLL.@)
3266 BOOLEAN WINAPI
RtlDoesFileExists_U(LPCWSTR file_name
)
3268 UNICODE_STRING nt_name
;
3269 FILE_BASIC_INFORMATION basic_info
;
3270 OBJECT_ATTRIBUTES attr
;
3273 if (!RtlDosPathNameToNtPathName_U( file_name
, &nt_name
, NULL
, NULL
)) return FALSE
;
3275 attr
.Length
= sizeof(attr
);
3276 attr
.RootDirectory
= 0;
3277 attr
.ObjectName
= &nt_name
;
3278 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
3279 attr
.SecurityDescriptor
= NULL
;
3280 attr
.SecurityQualityOfService
= NULL
;
3282 ret
= NtQueryAttributesFile(&attr
, &basic_info
) == STATUS_SUCCESS
;
3284 RtlFreeUnicodeString( &nt_name
);
3289 /***********************************************************************
3290 * DIR_unmount_device
3292 * Unmount the specified device.
3294 NTSTATUS
DIR_unmount_device( HANDLE handle
)
3297 int unix_fd
, needs_close
;
3299 if (!(status
= server_get_unix_fd( handle
, 0, &unix_fd
, &needs_close
, NULL
, NULL
)))
3302 char *mount_point
= NULL
;
3304 if (fstat( unix_fd
, &st
) == -1 || !is_valid_mounted_device( &st
))
3305 status
= STATUS_INVALID_PARAMETER
;
3308 if ((mount_point
= get_device_mount_point( st
.st_rdev
)))
3311 static const char umount
[] = "diskutil unmount >/dev/null 2>&1 ";
3313 static const char umount
[] = "umount >/dev/null 2>&1 ";
3315 char *cmd
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(mount_point
)+sizeof(umount
));
3318 strcpy( cmd
, umount
);
3319 strcat( cmd
, mount_point
);
3321 RtlFreeHeap( GetProcessHeap(), 0, cmd
);
3323 /* umount will fail to release the loop device since we still have
3324 a handle to it, so we release it here */
3325 if (major(st
.st_rdev
) == LOOP_MAJOR
) ioctl( unix_fd
, 0x4c01 /*LOOP_CLR_FD*/, 0 );
3328 RtlFreeHeap( GetProcessHeap(), 0, mount_point
);
3331 if (needs_close
) close( unix_fd
);
3337 /******************************************************************************
3340 * Retrieve the Unix name of the current directory; helper for wine_unix_to_nt_file_name.
3341 * Returned value must be freed by caller.
3343 NTSTATUS
DIR_get_unix_cwd( char **cwd
)
3345 int old_cwd
, unix_fd
, needs_close
;
3350 RtlAcquirePebLock();
3352 if (NtCurrentTeb()->Tib
.SubSystemTib
) /* FIXME: hack */
3353 curdir
= &((WIN16_SUBSYSTEM_TIB
*)NtCurrentTeb()->Tib
.SubSystemTib
)->curdir
;
3355 curdir
= &NtCurrentTeb()->Peb
->ProcessParameters
->CurrentDirectory
;
3357 if (!(handle
= curdir
->Handle
))
3359 UNICODE_STRING dirW
;
3360 OBJECT_ATTRIBUTES attr
;
3363 if (!RtlDosPathNameToNtPathName_U( curdir
->DosPath
.Buffer
, &dirW
, NULL
, NULL
))
3365 status
= STATUS_OBJECT_NAME_INVALID
;
3368 attr
.Length
= sizeof(attr
);
3369 attr
.RootDirectory
= 0;
3370 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
3371 attr
.ObjectName
= &dirW
;
3372 attr
.SecurityDescriptor
= NULL
;
3373 attr
.SecurityQualityOfService
= NULL
;
3375 status
= NtOpenFile( &handle
, 0, &attr
, &io
, 0,
3376 FILE_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
);
3377 RtlFreeUnicodeString( &dirW
);
3378 if (status
!= STATUS_SUCCESS
) goto done
;
3381 if ((status
= server_get_unix_fd( handle
, 0, &unix_fd
, &needs_close
, NULL
, NULL
)) == STATUS_SUCCESS
)
3383 RtlEnterCriticalSection( &dir_section
);
3385 if ((old_cwd
= open(".", O_RDONLY
)) != -1 && fchdir( unix_fd
) != -1)
3387 unsigned int size
= 512;
3391 if (!(*cwd
= RtlAllocateHeap( GetProcessHeap(), 0, size
)))
3393 status
= STATUS_NO_MEMORY
;
3396 if (getcwd( *cwd
, size
)) break;
3397 RtlFreeHeap( GetProcessHeap(), 0, *cwd
);
3398 if (errno
!= ERANGE
)
3400 status
= STATUS_OBJECT_PATH_INVALID
;
3405 if (fchdir( old_cwd
) == -1) chdir( "/" );
3407 else status
= FILE_GetNtStatus();
3409 RtlLeaveCriticalSection( &dir_section
);
3410 if (old_cwd
!= -1) close( old_cwd
);
3411 if (needs_close
) close( unix_fd
);
3413 if (!curdir
->Handle
) NtClose( handle
);
3416 RtlReleasePebLock();