2 * NTDLL directory functions
4 * Copyright 1993 Erik Bos
5 * Copyright 2003 Eric Pouech
6 * Copyright 1996, 2004 Alexandre Julliard
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/port.h"
27 #include <sys/types.h>
41 #ifdef HAVE_SYS_STAT_H
42 # include <sys/stat.h>
44 #ifdef HAVE_SYS_SYSCALL_H
45 # include <sys/syscall.h>
47 #ifdef HAVE_SYS_ATTR_H
50 #ifdef HAVE_SYS_IOCTL_H
51 #include <sys/ioctl.h>
53 #ifdef HAVE_LINUX_IOCTL_H
54 #include <linux/ioctl.h>
56 #ifdef HAVE_LINUX_MAJOR_H
57 # include <linux/major.h>
59 #ifdef HAVE_SYS_PARAM_H
60 #include <sys/param.h>
62 #ifdef HAVE_SYS_MOUNT_H
63 #include <sys/mount.h>
65 #ifdef HAVE_SYS_STATFS_H
66 #include <sys/statfs.h>
73 #define NONAMELESSUNION
74 #define NONAMELESSSTRUCT
76 #define WIN32_NO_STATUS
81 #include "ntdll_misc.h"
82 #include "wine/unicode.h"
83 #include "wine/server.h"
84 #include "wine/list.h"
85 #include "wine/library.h"
86 #include "wine/debug.h"
88 WINE_DEFAULT_DEBUG_CHANNEL(file
);
91 #undef VFAT_IOCTL_READDIR_BOTH
96 /* We want the real kernel dirent structure, not the libc one */
101 unsigned short d_reclen
;
105 /* Define the VFAT ioctl to get both short and long file names */
106 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, KERNEL_DIRENT [2] )
109 # define O_DIRECTORY 0200000 /* must be directory */
112 #ifdef __NR_getdents64
117 unsigned short d_reclen
;
118 unsigned char d_type
;
123 static inline int getdents64( int fd
, char *de
, unsigned int size
)
125 return syscall( __NR_getdents64
, fd
, de
, size
);
132 #define IS_OPTION_TRUE(ch) ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
133 #define IS_SEPARATOR(ch) ((ch) == '\\' || (ch) == '/')
135 #define INVALID_NT_CHARS '*','?','<','>','|','"'
136 #define INVALID_DOS_CHARS INVALID_NT_CHARS,'+','=',',',';','[',']',' ','\345'
138 #define MAX_DIR_ENTRY_LEN 255 /* max length of a directory entry in chars */
140 #define MAX_IGNORED_FILES 4
148 static struct file_identity ignored_files
[MAX_IGNORED_FILES
];
149 static unsigned int ignored_files_count
;
151 union file_directory_info
154 FILE_DIRECTORY_INFORMATION dir
;
155 FILE_BOTH_DIRECTORY_INFORMATION both
;
156 FILE_FULL_DIRECTORY_INFORMATION full
;
157 FILE_ID_BOTH_DIRECTORY_INFORMATION id_both
;
158 FILE_ID_FULL_DIRECTORY_INFORMATION id_full
;
161 static BOOL show_dot_files
;
162 static RTL_RUN_ONCE init_once
= RTL_RUN_ONCE_INIT
;
164 /* at some point we may want to allow Winelib apps to set this */
165 static const BOOL is_case_sensitive
= FALSE
;
167 UNICODE_STRING system_dir
= { 0, 0, NULL
}; /* system directory */
169 static struct file_identity curdir
;
170 static struct file_identity windir
;
172 static RTL_CRITICAL_SECTION dir_section
;
173 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
176 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
177 0, 0, { (DWORD_PTR
)(__FILE__
": dir_section") }
179 static RTL_CRITICAL_SECTION dir_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
182 /* check if a given Unicode char is OK in a DOS short name */
183 static inline BOOL
is_invalid_dos_char( WCHAR ch
)
185 static const WCHAR invalid_chars
[] = { INVALID_DOS_CHARS
,'~','.',0 };
186 if (ch
> 0x7f) return TRUE
;
187 return strchrW( invalid_chars
, ch
) != NULL
;
190 /* check if the device can be a mounted volume */
191 static inline BOOL
is_valid_mounted_device( const struct stat
*st
)
193 #if defined(linux) || defined(__sun__)
194 return S_ISBLK( st
->st_mode
);
196 /* disks are char devices on *BSD */
197 return S_ISCHR( st
->st_mode
);
201 static inline void ignore_file( const char *name
)
204 assert( ignored_files_count
< MAX_IGNORED_FILES
);
205 if (!stat( name
, &st
))
207 ignored_files
[ignored_files_count
].dev
= st
.st_dev
;
208 ignored_files
[ignored_files_count
].ino
= st
.st_ino
;
209 ignored_files_count
++;
213 static inline BOOL
is_same_file( const struct file_identity
*file
, const struct stat
*st
)
215 return st
->st_dev
== file
->dev
&& st
->st_ino
== file
->ino
;
218 static inline BOOL
is_ignored_file( const struct stat
*st
)
222 for (i
= 0; i
< ignored_files_count
; i
++)
223 if (is_same_file( &ignored_files
[i
], st
)) return TRUE
;
227 static inline unsigned int dir_info_size( FILE_INFORMATION_CLASS
class, unsigned int len
)
231 case FileDirectoryInformation
:
232 return (FIELD_OFFSET( FILE_DIRECTORY_INFORMATION
, FileName
[len
] ) + 7) & ~7;
233 case FileBothDirectoryInformation
:
234 return (FIELD_OFFSET( FILE_BOTH_DIRECTORY_INFORMATION
, FileName
[len
] ) + 7) & ~7;
235 case FileFullDirectoryInformation
:
236 return (FIELD_OFFSET( FILE_FULL_DIRECTORY_INFORMATION
, FileName
[len
] ) + 7) & ~7;
237 case FileIdBothDirectoryInformation
:
238 return (FIELD_OFFSET( FILE_ID_BOTH_DIRECTORY_INFORMATION
, FileName
[len
] ) + 7) & ~7;
239 case FileIdFullDirectoryInformation
:
240 return (FIELD_OFFSET( FILE_ID_FULL_DIRECTORY_INFORMATION
, FileName
[len
] ) + 7) & ~7;
247 static inline unsigned int max_dir_info_size( FILE_INFORMATION_CLASS
class )
249 return dir_info_size( class, MAX_DIR_ENTRY_LEN
);
252 static inline BOOL
has_wildcard( const UNICODE_STRING
*mask
)
255 memchrW( mask
->Buffer
, '*', mask
->Length
/ sizeof(WCHAR
) ) ||
256 memchrW( mask
->Buffer
, '?', mask
->Length
/ sizeof(WCHAR
) ));
260 /* support for a directory queue for filesystem searches */
268 static struct list dir_queue
= LIST_INIT( dir_queue
);
270 static NTSTATUS
add_dir_to_queue( const char *name
)
272 int len
= strlen( name
) + 1;
273 struct dir_name
*dir
= RtlAllocateHeap( GetProcessHeap(), 0,
274 FIELD_OFFSET( struct dir_name
, name
[len
] ));
275 if (!dir
) return STATUS_NO_MEMORY
;
276 strcpy( dir
->name
, name
);
277 list_add_tail( &dir_queue
, &dir
->entry
);
278 return STATUS_SUCCESS
;
281 static NTSTATUS
next_dir_in_queue( char *name
)
283 struct list
*head
= list_head( &dir_queue
);
286 struct dir_name
*dir
= LIST_ENTRY( head
, struct dir_name
, entry
);
287 strcpy( name
, dir
->name
);
288 list_remove( &dir
->entry
);
289 RtlFreeHeap( GetProcessHeap(), 0, dir
);
290 return STATUS_SUCCESS
;
292 return STATUS_OBJECT_NAME_NOT_FOUND
;
295 static void flush_dir_queue(void)
299 while ((head
= list_head( &dir_queue
)))
301 struct dir_name
*dir
= LIST_ENTRY( head
, struct dir_name
, entry
);
302 list_remove( &dir
->entry
);
303 RtlFreeHeap( GetProcessHeap(), 0, dir
);
308 /***********************************************************************
309 * get_default_com_device
311 * Return the default device to use for serial ports.
313 static char *get_default_com_device( int num
)
317 if (!num
|| num
> 9) return ret
;
319 ret
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/ttyS0") );
322 strcpy( ret
, "/dev/ttyS0" );
323 ret
[strlen(ret
) - 1] = '0' + num
- 1;
325 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
326 ret
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/cuau0") );
329 strcpy( ret
, "/dev/cuau0" );
330 ret
[strlen(ret
) - 1] = '0' + num
- 1;
332 #elif defined(__DragonFly__)
333 ret
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/cuaa0") );
336 strcpy( ret
, "/dev/cuaa0" );
337 ret
[strlen(ret
) - 1] = '0' + num
- 1;
340 FIXME( "no known default for device com%d\n", num
);
346 /***********************************************************************
347 * get_default_lpt_device
349 * Return the default device to use for parallel ports.
351 static char *get_default_lpt_device( int num
)
355 if (!num
|| num
> 9) return ret
;
357 ret
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/lp0") );
360 strcpy( ret
, "/dev/lp0" );
361 ret
[strlen(ret
) - 1] = '0' + num
- 1;
364 FIXME( "no known default for device lpt%d\n", num
);
371 static char *unescape_field( char *str
)
375 for (in
= out
= str
; *in
; in
++, out
++)
385 else if (in
[1] == '0' && in
[2] == '4' && in
[3] == '0')
390 else if (in
[1] == '0' && in
[2] == '1' && in
[3] == '1')
395 else if (in
[1] == '0' && in
[2] == '1' && in
[3] == '2')
400 else if (in
[1] == '1' && in
[2] == '3' && in
[3] == '4')
412 static inline char *get_field( char **str
)
416 ret
= strsep( str
, " \t" );
417 if (*str
) *str
+= strspn( *str
, " \t" );
421 /************************************************************************
422 * getmntent_replacement
424 * getmntent replacement for Android.
426 * NB returned static buffer is not thread safe; protect with dir_section.
428 static struct mntent
*getmntent_replacement( FILE *f
)
430 static struct mntent entry
;
431 static char buf
[4096];
436 if (!fgets( buf
, sizeof(buf
), f
)) return NULL
;
437 p
= strchr( buf
, '\n' );
439 else /* Partially unread line, move file ptr to end */
442 while (fgets( tmp
, sizeof(tmp
), f
))
443 if (strchr( tmp
, '\n' )) break;
445 start
= buf
+ strspn( buf
, " \t" );
446 } while (start
[0] == '\0' || start
[0] == '#');
448 p
= get_field( &start
);
449 entry
.mnt_fsname
= p
? unescape_field( p
) : (char *)"";
451 p
= get_field( &start
);
452 entry
.mnt_dir
= p
? unescape_field( p
) : (char *)"";
454 p
= get_field( &start
);
455 entry
.mnt_type
= p
? unescape_field( p
) : (char *)"";
457 p
= get_field( &start
);
458 entry
.mnt_opts
= p
? unescape_field( p
) : (char *)"";
460 p
= get_field( &start
);
461 entry
.mnt_freq
= p
? atoi(p
) : 0;
463 p
= get_field( &start
);
464 entry
.mnt_passno
= p
? atoi(p
) : 0;
468 #define getmntent getmntent_replacement
471 /***********************************************************************
472 * DIR_get_drives_info
474 * Retrieve device/inode number for all the drives. Helper for find_drive_root.
476 unsigned int DIR_get_drives_info( struct drive_info info
[MAX_DOS_DRIVES
] )
478 static struct drive_info cache
[MAX_DOS_DRIVES
];
479 static time_t last_update
;
480 static unsigned int nb_drives
;
482 time_t now
= time(NULL
);
484 RtlEnterCriticalSection( &dir_section
);
485 if (now
!= last_update
)
487 const char *config_dir
= wine_get_config_dir();
492 if ((buffer
= RtlAllocateHeap( GetProcessHeap(), 0,
493 strlen(config_dir
) + sizeof("/dosdevices/a:") )))
495 strcpy( buffer
, config_dir
);
496 strcat( buffer
, "/dosdevices/a:" );
497 p
= buffer
+ strlen(buffer
) - 2;
499 for (i
= nb_drives
= 0; i
< MAX_DOS_DRIVES
; i
++)
502 if (!stat( buffer
, &st
))
504 cache
[i
].dev
= st
.st_dev
;
505 cache
[i
].ino
= st
.st_ino
;
514 RtlFreeHeap( GetProcessHeap(), 0, buffer
);
518 memcpy( info
, cache
, sizeof(cache
) );
520 RtlLeaveCriticalSection( &dir_section
);
525 /***********************************************************************
526 * parse_mount_entries
528 * Parse mount entries looking for a given device. Helper for get_default_drive_device.
532 #include <sys/vfstab.h>
533 static char *parse_vfstab_entries( FILE *f
, dev_t dev
, ino_t ino
)
539 while (! getvfsent( f
, &entry
))
541 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
542 if (!strcmp( entry
.vfs_fstype
, "nfs" ) ||
543 !strcmp( entry
.vfs_fstype
, "smbfs" ) ||
544 !strcmp( entry
.vfs_fstype
, "ncpfs" )) continue;
546 if (stat( entry
.vfs_mountp
, &st
) == -1) continue;
547 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
548 if (!strcmp( entry
.vfs_fstype
, "fd" ))
550 if ((device
= strstr( entry
.vfs_mntopts
, "dev=" )))
552 char *p
= strchr( device
+ 4, ',' );
558 return entry
.vfs_special
;
565 static char *parse_mount_entries( FILE *f
, dev_t dev
, ino_t ino
)
567 struct mntent
*entry
;
571 while ((entry
= getmntent( f
)))
573 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
574 if (!strcmp( entry
->mnt_type
, "nfs" ) ||
575 !strcmp( entry
->mnt_type
, "smbfs" ) ||
576 !strcmp( entry
->mnt_type
, "ncpfs" )) continue;
578 if (stat( entry
->mnt_dir
, &st
) == -1) continue;
579 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
580 if (!strcmp( entry
->mnt_type
, "supermount" ))
582 if ((device
= strstr( entry
->mnt_opts
, "dev=" )))
584 char *p
= strchr( device
+ 4, ',' );
589 else if (!stat( entry
->mnt_fsname
, &st
) && S_ISREG(st
.st_mode
))
591 /* if device is a regular file check for a loop mount */
592 if ((device
= strstr( entry
->mnt_opts
, "loop=" )))
594 char *p
= strchr( device
+ 5, ',' );
600 return entry
->mnt_fsname
;
606 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
608 static char *parse_mount_entries( FILE *f
, dev_t dev
, ino_t ino
)
613 while ((entry
= getfsent()))
615 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
616 if (!strcmp( entry
->fs_vfstype
, "nfs" ) ||
617 !strcmp( entry
->fs_vfstype
, "smbfs" ) ||
618 !strcmp( entry
->fs_vfstype
, "ncpfs" )) continue;
620 if (stat( entry
->fs_file
, &st
) == -1) continue;
621 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
622 return entry
->fs_spec
;
629 #include <sys/mnttab.h>
630 static char *parse_mount_entries( FILE *f
, dev_t dev
, ino_t ino
)
637 while (( ! getmntent( f
, &entry
) ))
639 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
640 if (!strcmp( entry
.mnt_fstype
, "nfs" ) ||
641 !strcmp( entry
.mnt_fstype
, "smbfs" ) ||
642 !strcmp( entry
.mnt_fstype
, "ncpfs" )) continue;
644 if (stat( entry
.mnt_mountp
, &st
) == -1) continue;
645 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
646 if (!strcmp( entry
.mnt_fstype
, "fd" ))
648 if ((device
= strstr( entry
.mnt_mntopts
, "dev=" )))
650 char *p
= strchr( device
+ 4, ',' );
656 return entry
.mnt_special
;
662 /***********************************************************************
663 * get_default_drive_device
665 * Return the default device to use for a given drive mount point.
667 static char *get_default_drive_device( const char *root
)
677 /* try to open it first to force it to get mounted */
678 if ((fd
= open( root
, O_RDONLY
| O_DIRECTORY
)) != -1)
680 res
= fstat( fd
, &st
);
683 /* now try normal stat just in case */
684 if (res
== -1) res
= stat( root
, &st
);
685 if (res
== -1) return NULL
;
687 RtlEnterCriticalSection( &dir_section
);
690 if ((f
= fopen( "/proc/mounts", "r" )))
692 device
= parse_mount_entries( f
, st
.st_dev
, st
.st_ino
);
696 if ((f
= fopen( "/etc/mtab", "r" )))
698 device
= parse_mount_entries( f
, st
.st_dev
, st
.st_ino
);
701 /* look through fstab too in case it's not mounted (for instance if it's an audio CD) */
702 if (!device
&& (f
= fopen( "/etc/fstab", "r" )))
704 device
= parse_mount_entries( f
, st
.st_dev
, st
.st_ino
);
710 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(device
) + 1 );
711 if (ret
) strcpy( ret
, device
);
713 RtlLeaveCriticalSection( &dir_section
);
715 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__ ) || defined(__DragonFly__)
720 /* try to open it first to force it to get mounted */
721 if ((fd
= open( root
, O_RDONLY
)) != -1)
723 res
= fstat( fd
, &st
);
726 /* now try normal stat just in case */
727 if (res
== -1) res
= stat( root
, &st
);
728 if (res
== -1) return NULL
;
730 RtlEnterCriticalSection( &dir_section
);
732 /* The FreeBSD parse_mount_entries doesn't require a file argument, so just
733 * pass NULL. Leave the argument in for symmetry.
735 device
= parse_mount_entries( NULL
, st
.st_dev
, st
.st_ino
);
738 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(device
) + 1 );
739 if (ret
) strcpy( ret
, device
);
741 RtlLeaveCriticalSection( &dir_section
);
749 /* try to open it first to force it to get mounted */
750 if ((fd
= open( root
, O_RDONLY
)) != -1)
752 res
= fstat( fd
, &st
);
755 /* now try normal stat just in case */
756 if (res
== -1) res
= stat( root
, &st
);
757 if (res
== -1) return NULL
;
759 RtlEnterCriticalSection( &dir_section
);
761 if ((f
= fopen( "/etc/mnttab", "r" )))
763 device
= parse_mount_entries( f
, st
.st_dev
, st
.st_ino
);
766 /* look through fstab too in case it's not mounted (for instance if it's an audio CD) */
767 if (!device
&& (f
= fopen( "/etc/vfstab", "r" )))
769 device
= parse_vfstab_entries( f
, st
.st_dev
, st
.st_ino
);
774 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(device
) + 1 );
775 if (ret
) strcpy( ret
, device
);
777 RtlLeaveCriticalSection( &dir_section
);
779 #elif defined(__APPLE__)
780 struct statfs
*mntStat
;
786 static const char path_bsd_device
[] = "/dev/disk";
789 res
= stat( root
, &st
);
790 if (res
== -1) return NULL
;
795 RtlEnterCriticalSection( &dir_section
);
797 mntSize
= getmntinfo(&mntStat
, MNT_NOWAIT
);
799 for (i
= 0; i
< mntSize
&& !ret
; i
++)
801 if (stat(mntStat
[i
].f_mntonname
, &st
) == -1) continue;
802 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
804 /* FIXME add support for mounted network drive */
805 if ( strncmp(mntStat
[i
].f_mntfromname
, path_bsd_device
, strlen(path_bsd_device
)) == 0)
807 /* set return value to the corresponding raw BSD node */
808 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(mntStat
[i
].f_mntfromname
) + 2 /* 2 : r and \0 */ );
811 strcpy(ret
, "/dev/r");
812 strcat(ret
, mntStat
[i
].f_mntfromname
+sizeof("/dev/")-1);
816 RtlLeaveCriticalSection( &dir_section
);
819 if (!warned
++) FIXME( "auto detection of DOS devices not supported on this platform\n" );
825 /***********************************************************************
826 * get_device_mount_point
828 * Return the current mount point for a device.
830 static char *get_device_mount_point( dev_t dev
)
837 RtlEnterCriticalSection( &dir_section
);
840 if ((f
= fopen( "/proc/mounts", "r" )))
842 if ((f
= fopen( "/etc/mtab", "r" )))
845 struct mntent
*entry
;
849 while ((entry
= getmntent( f
)))
851 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
852 if (!strcmp( entry
->mnt_type
, "nfs" ) ||
853 !strcmp( entry
->mnt_type
, "smbfs" ) ||
854 !strcmp( entry
->mnt_type
, "ncpfs" )) continue;
856 if (!strcmp( entry
->mnt_type
, "supermount" ))
858 if ((device
= strstr( entry
->mnt_opts
, "dev=" )))
861 if ((p
= strchr( device
, ',' ))) *p
= 0;
864 else if (!stat( entry
->mnt_fsname
, &st
) && S_ISREG(st
.st_mode
))
866 /* if device is a regular file check for a loop mount */
867 if ((device
= strstr( entry
->mnt_opts
, "loop=" )))
870 if ((p
= strchr( device
, ',' ))) *p
= 0;
873 else device
= entry
->mnt_fsname
;
875 if (device
&& !stat( device
, &st
) && S_ISBLK(st
.st_mode
) && st
.st_rdev
== dev
)
877 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(entry
->mnt_dir
) + 1 );
878 if (ret
) strcpy( ret
, entry
->mnt_dir
);
884 RtlLeaveCriticalSection( &dir_section
);
885 #elif defined(__APPLE__)
886 struct statfs
*entry
;
890 RtlEnterCriticalSection( &dir_section
);
892 size
= getmntinfo( &entry
, MNT_NOWAIT
);
893 for (i
= 0; i
< size
; i
++)
895 if (stat( entry
[i
].f_mntfromname
, &st
) == -1) continue;
896 if (S_ISBLK(st
.st_mode
) && st
.st_rdev
== dev
)
898 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(entry
[i
].f_mntonname
) + 1 );
899 if (ret
) strcpy( ret
, entry
[i
].f_mntonname
);
903 RtlLeaveCriticalSection( &dir_section
);
906 if (!warned
++) FIXME( "unmounting devices not supported on this platform\n" );
912 #if defined(HAVE_GETATTRLIST) && defined(ATTR_VOL_CAPABILITIES) && \
913 defined(VOL_CAPABILITIES_FORMAT) && defined(VOL_CAP_FMT_CASE_SENSITIVE)
926 BOOLEAN case_sensitive
;
932 vol_capabilities_attr_t caps
;
935 /***********************************************************************
938 * Checks if the specified file system is in the cache.
940 static struct fs_cache
*look_up_fs_cache( dev_t dev
)
943 for (i
= 0; i
< sizeof(fs_cache
)/sizeof(fs_cache
[0]); i
++)
944 if (fs_cache
[i
].dev
== dev
)
949 /***********************************************************************
952 * Adds the specified file system to the cache.
954 static void add_fs_cache( dev_t dev
, fsid_t fsid
, BOOLEAN case_sensitive
)
957 struct fs_cache
*entry
= look_up_fs_cache( dev
);
961 /* Update the cache */
963 entry
->case_sensitive
= case_sensitive
;
967 /* Add a new entry */
968 for (i
= 0; i
< sizeof(fs_cache
)/sizeof(fs_cache
[0]); i
++)
969 if (fs_cache
[i
].dev
== 0)
971 /* This entry is empty, use it */
972 fs_cache
[i
].dev
= dev
;
973 fs_cache
[i
].fsid
= fsid
;
974 fs_cache
[i
].case_sensitive
= case_sensitive
;
978 /* Cache is out of space, warn */
980 WARN( "FS cache is out of space, expect performance problems\n" );
983 /***********************************************************************
984 * get_dir_case_sensitivity_attr
986 * Checks if the volume containing the specified directory is case
987 * sensitive or not. Uses getattrlist(2).
989 static int get_dir_case_sensitivity_attr( const char *dir
)
991 char *mntpoint
= NULL
;
992 struct attrlist attr
;
993 struct vol_caps caps
;
994 struct get_fsid get_fsid
;
995 struct fs_cache
*entry
;
997 /* First get the FS ID of the volume */
998 attr
.bitmapcount
= ATTR_BIT_MAP_COUNT
;
1000 attr
.commonattr
= ATTR_CMN_DEVID
|ATTR_CMN_FSID
;
1001 attr
.volattr
= attr
.dirattr
= attr
.fileattr
= attr
.forkattr
= 0;
1003 if (getattrlist( dir
, &attr
, &get_fsid
, sizeof(get_fsid
), 0 ) != 0 ||
1004 get_fsid
.size
!= sizeof(get_fsid
))
1006 /* Try to look it up in the cache */
1007 entry
= look_up_fs_cache( get_fsid
.dev
);
1008 if (entry
&& !memcmp( &entry
->fsid
, &get_fsid
.fsid
, sizeof(fsid_t
) ))
1009 /* Cache lookup succeeded */
1010 return entry
->case_sensitive
;
1011 /* Cache is stale at this point, we have to update it */
1013 mntpoint
= get_device_mount_point( get_fsid
.dev
);
1014 /* Now look up the case-sensitivity */
1015 attr
.commonattr
= 0;
1016 attr
.volattr
= ATTR_VOL_INFO
|ATTR_VOL_CAPABILITIES
;
1017 if (getattrlist( mntpoint
, &attr
, &caps
, sizeof(caps
), 0 ) < 0)
1019 RtlFreeHeap( GetProcessHeap(), 0, mntpoint
);
1020 add_fs_cache( get_fsid
.dev
, get_fsid
.fsid
, TRUE
);
1023 RtlFreeHeap( GetProcessHeap(), 0, mntpoint
);
1024 if (caps
.size
== sizeof(caps
) &&
1025 (caps
.caps
.valid
[VOL_CAPABILITIES_FORMAT
] &
1026 (VOL_CAP_FMT_CASE_SENSITIVE
| VOL_CAP_FMT_CASE_PRESERVING
)) ==
1027 (VOL_CAP_FMT_CASE_SENSITIVE
| VOL_CAP_FMT_CASE_PRESERVING
))
1031 if ((caps
.caps
.capabilities
[VOL_CAPABILITIES_FORMAT
] &
1032 VOL_CAP_FMT_CASE_SENSITIVE
) != VOL_CAP_FMT_CASE_SENSITIVE
)
1036 /* Update the cache */
1037 add_fs_cache( get_fsid
.dev
, get_fsid
.fsid
, ret
);
1044 /***********************************************************************
1045 * get_dir_case_sensitivity_stat
1047 * Checks if the volume containing the specified directory is case
1048 * sensitive or not. Uses statfs(2) or statvfs(2).
1050 static BOOLEAN
get_dir_case_sensitivity_stat( const char *dir
)
1052 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1055 if (statfs( dir
, &stfs
) == -1) return FALSE
;
1056 /* Assume these file systems are always case insensitive on Mac OS.
1057 * For FreeBSD, only assume CIOPFS is case insensitive (AFAIK, Mac OS
1058 * is the only UNIX that supports case-insensitive lookup).
1060 if (!strcmp( stfs
.f_fstypename
, "fusefs" ) &&
1061 !strncmp( stfs
.f_mntfromname
, "ciopfs", 5 ))
1064 if (!strcmp( stfs
.f_fstypename
, "msdos" ) ||
1065 !strcmp( stfs
.f_fstypename
, "cd9660" ) ||
1066 !strcmp( stfs
.f_fstypename
, "udf" ) ||
1067 !strcmp( stfs
.f_fstypename
, "ntfs" ) ||
1068 !strcmp( stfs
.f_fstypename
, "smbfs" ))
1070 #ifdef _DARWIN_FEATURE_64_BIT_INODE
1071 if (!strcmp( stfs
.f_fstypename
, "hfs" ) && (stfs
.f_fssubtype
== 0 ||
1072 stfs
.f_fssubtype
== 1 ||
1073 stfs
.f_fssubtype
== 128))
1076 /* The field says "reserved", but a quick look at the kernel source
1077 * tells us that this "reserved" field is really the same as the
1078 * "fssubtype" field from the inode64 structure (see munge_statfs()
1079 * in <xnu-source>/bsd/vfs/vfs_syscalls.c).
1081 if (!strcmp( stfs
.f_fstypename
, "hfs" ) && (stfs
.f_reserved1
== 0 ||
1082 stfs
.f_reserved1
== 1 ||
1083 stfs
.f_reserved1
== 128))
1089 #elif defined(__NetBSD__)
1090 struct statvfs stfs
;
1092 if (statvfs( dir
, &stfs
) == -1) return FALSE
;
1093 /* Only assume CIOPFS is case insensitive. */
1094 if (strcmp( stfs
.f_fstypename
, "fusefs" ) ||
1095 strncmp( stfs
.f_mntfromname
, "ciopfs", 5 ))
1099 #elif defined(__linux__)
1104 /* Only assume CIOPFS is case insensitive. */
1105 if (statfs( dir
, &stfs
) == -1) return FALSE
;
1106 if (stfs
.f_type
!= 0x65735546 /* FUSE_SUPER_MAGIC */)
1108 /* Normally, we'd have to parse the mtab to find out exactly what
1109 * kind of FUSE FS this is. But, someone on wine-devel suggested
1110 * a shortcut. We'll stat a special file in the directory. If it's
1111 * there, we'll assume it's a CIOPFS, else not.
1112 * This will break if somebody puts a file named ".ciopfs" in a non-
1115 cifile
= RtlAllocateHeap( GetProcessHeap(), 0, strlen( dir
)+sizeof("/.ciopfs") );
1116 if (!cifile
) return TRUE
;
1117 strcpy( cifile
, dir
);
1118 strcat( cifile
, "/.ciopfs" );
1119 if (stat( cifile
, &st
) == 0)
1121 RtlFreeHeap( GetProcessHeap(), 0, cifile
);
1124 RtlFreeHeap( GetProcessHeap(), 0, cifile
);
1132 /***********************************************************************
1133 * get_dir_case_sensitivity
1135 * Checks if the volume containing the specified directory is case
1136 * sensitive or not. Uses statfs(2) or statvfs(2).
1138 static BOOLEAN
get_dir_case_sensitivity( const char *dir
)
1140 #if defined(HAVE_GETATTRLIST) && defined(ATTR_VOL_CAPABILITIES) && \
1141 defined(VOL_CAPABILITIES_FORMAT) && defined(VOL_CAP_FMT_CASE_SENSITIVE)
1142 int case_sensitive
= get_dir_case_sensitivity_attr( dir
);
1143 if (case_sensitive
!= -1) return case_sensitive
;
1145 return get_dir_case_sensitivity_stat( dir
);
1149 /***********************************************************************
1152 * Initialize the show_dot_files options.
1154 static DWORD WINAPI
init_options( RTL_RUN_ONCE
*once
, void *param
, void **context
)
1156 static const WCHAR WineW
[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e',0};
1157 static const WCHAR ShowDotFilesW
[] = {'S','h','o','w','D','o','t','F','i','l','e','s',0};
1161 OBJECT_ATTRIBUTES attr
;
1162 UNICODE_STRING nameW
;
1164 RtlOpenCurrentUser( KEY_ALL_ACCESS
, &root
);
1165 attr
.Length
= sizeof(attr
);
1166 attr
.RootDirectory
= root
;
1167 attr
.ObjectName
= &nameW
;
1168 attr
.Attributes
= 0;
1169 attr
.SecurityDescriptor
= NULL
;
1170 attr
.SecurityQualityOfService
= NULL
;
1171 RtlInitUnicodeString( &nameW
, WineW
);
1173 /* @@ Wine registry key: HKCU\Software\Wine */
1174 if (!NtOpenKey( &hkey
, KEY_ALL_ACCESS
, &attr
))
1176 RtlInitUnicodeString( &nameW
, ShowDotFilesW
);
1177 if (!NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
, tmp
, sizeof(tmp
), &dummy
))
1179 WCHAR
*str
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)tmp
)->Data
;
1180 show_dot_files
= IS_OPTION_TRUE( str
[0] );
1186 /* a couple of directories that we don't want to return in directory searches */
1187 ignore_file( wine_get_config_dir() );
1188 ignore_file( "/dev" );
1189 ignore_file( "/proc" );
1191 ignore_file( "/sys" );
1197 /***********************************************************************
1198 * DIR_is_hidden_file
1200 * Check if the specified file should be hidden based on its name and the show dot files option.
1202 BOOL
DIR_is_hidden_file( const UNICODE_STRING
*name
)
1206 RtlRunOnceExecuteOnce( &init_once
, init_options
, NULL
, NULL
);
1208 if (show_dot_files
) return FALSE
;
1210 end
= p
= name
->Buffer
+ name
->Length
/sizeof(WCHAR
);
1211 while (p
> name
->Buffer
&& IS_SEPARATOR(p
[-1])) p
--;
1212 while (p
> name
->Buffer
&& !IS_SEPARATOR(p
[-1])) p
--;
1213 if (p
== end
|| *p
!= '.') return FALSE
;
1214 /* make sure it isn't '.' or '..' */
1215 if (p
+ 1 == end
) return FALSE
;
1216 if (p
[1] == '.' && p
+ 2 == end
) return FALSE
;
1221 /***********************************************************************
1222 * hash_short_file_name
1224 * Transform a Unix file name into a hashed DOS name. If the name is a valid
1225 * DOS name, it is converted to upper-case; otherwise it is replaced by a
1226 * hashed version that fits in 8.3 format.
1227 * 'buffer' must be at least 12 characters long.
1228 * Returns length of short name in bytes; short name is NOT null-terminated.
1230 static ULONG
hash_short_file_name( const UNICODE_STRING
*name
, LPWSTR buffer
)
1232 static const char hash_chars
[32] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345";
1234 LPCWSTR p
, ext
, end
= name
->Buffer
+ name
->Length
/ sizeof(WCHAR
);
1236 unsigned short hash
;
1239 /* Compute the hash code of the file name */
1240 /* If you know something about hash functions, feel free to */
1241 /* insert a better algorithm here... */
1242 if (!is_case_sensitive
)
1244 for (p
= name
->Buffer
, hash
= 0xbeef; p
< end
- 1; p
++)
1245 hash
= (hash
<<3) ^ (hash
>>5) ^ tolowerW(*p
) ^ (tolowerW(p
[1]) << 8);
1246 hash
= (hash
<<3) ^ (hash
>>5) ^ tolowerW(*p
); /* Last character */
1250 for (p
= name
->Buffer
, hash
= 0xbeef; p
< end
- 1; p
++)
1251 hash
= (hash
<< 3) ^ (hash
>> 5) ^ *p
^ (p
[1] << 8);
1252 hash
= (hash
<< 3) ^ (hash
>> 5) ^ *p
; /* Last character */
1255 /* Find last dot for start of the extension */
1256 for (p
= name
->Buffer
+ 1, ext
= NULL
; p
< end
- 1; p
++) if (*p
== '.') ext
= p
;
1258 /* Copy first 4 chars, replacing invalid chars with '_' */
1259 for (i
= 4, p
= name
->Buffer
, dst
= buffer
; i
> 0; i
--, p
++)
1261 if (p
== end
|| p
== ext
) break;
1262 *dst
++ = is_invalid_dos_char(*p
) ? '_' : toupperW(*p
);
1264 /* Pad to 5 chars with '~' */
1265 while (i
-- >= 0) *dst
++ = '~';
1267 /* Insert hash code converted to 3 ASCII chars */
1268 *dst
++ = hash_chars
[(hash
>> 10) & 0x1f];
1269 *dst
++ = hash_chars
[(hash
>> 5) & 0x1f];
1270 *dst
++ = hash_chars
[hash
& 0x1f];
1272 /* Copy the first 3 chars of the extension (if any) */
1276 for (i
= 3, ext
++; (i
> 0) && ext
< end
; i
--, ext
++)
1277 *dst
++ = is_invalid_dos_char(*ext
) ? '_' : toupperW(*ext
);
1279 return dst
- buffer
;
1283 /***********************************************************************
1286 * Check a long file name against a mask.
1288 * Tests (done in W95 DOS shell - case insensitive):
1289 * *.txt test1.test.txt *
1291 * *.t??????.t* test1.ta.tornado.txt *
1292 * *tornado* test1.ta.tornado.txt *
1293 * t*t test1.ta.tornado.txt *
1295 * ?est??? test1.txt -
1296 * *test1.txt* test1.txt *
1297 * h?l?o*t.dat hellothisisatest.dat *
1299 static BOOLEAN
match_filename( const UNICODE_STRING
*name_str
, const UNICODE_STRING
*mask_str
)
1302 const WCHAR
*name
= name_str
->Buffer
;
1303 const WCHAR
*mask
= mask_str
->Buffer
;
1304 const WCHAR
*name_end
= name
+ name_str
->Length
/ sizeof(WCHAR
);
1305 const WCHAR
*mask_end
= mask
+ mask_str
->Length
/ sizeof(WCHAR
);
1306 const WCHAR
*lastjoker
= NULL
;
1307 const WCHAR
*next_to_retry
= NULL
;
1309 TRACE("(%s, %s)\n", debugstr_us(name_str
), debugstr_us(mask_str
));
1311 while (name
< name_end
&& mask
< mask_end
)
1317 while (mask
< mask_end
&& *mask
== '*') mask
++; /* Skip consecutive '*' */
1318 if (mask
== mask_end
) return TRUE
; /* end of mask is all '*', so match */
1321 /* skip to the next match after the joker(s) */
1322 if (is_case_sensitive
)
1323 while (name
< name_end
&& (*name
!= *mask
)) name
++;
1325 while (name
< name_end
&& (toupperW(*name
) != toupperW(*mask
))) name
++;
1326 next_to_retry
= name
;
1333 if (is_case_sensitive
) mismatch
= (*mask
!= *name
);
1334 else mismatch
= (toupperW(*mask
) != toupperW(*name
));
1340 if (mask
== mask_end
)
1342 if (name
== name_end
) return TRUE
;
1343 if (lastjoker
) mask
= lastjoker
;
1346 else /* mismatch ! */
1348 if (lastjoker
) /* we had an '*', so we can try unlimitedly */
1352 /* this scan sequence was a mismatch, so restart
1353 * 1 char after the first char we checked last time */
1355 name
= next_to_retry
;
1357 else return FALSE
; /* bad luck */
1362 while (mask
< mask_end
&& ((*mask
== '.') || (*mask
== '*')))
1363 mask
++; /* Ignore trailing '.' or '*' in mask */
1364 return (name
== name_end
&& mask
== mask_end
);
1368 /***********************************************************************
1371 * helper for NtQueryDirectoryFile
1373 static union file_directory_info
*append_entry( void *info_ptr
, IO_STATUS_BLOCK
*io
, ULONG max_length
,
1374 const char *long_name
, const char *short_name
,
1375 const UNICODE_STRING
*mask
, FILE_INFORMATION_CLASS
class )
1377 union file_directory_info
*info
;
1378 int i
, long_len
, short_len
, total_len
;
1380 WCHAR long_nameW
[MAX_DIR_ENTRY_LEN
];
1381 WCHAR short_nameW
[12];
1386 io
->u
.Status
= STATUS_SUCCESS
;
1387 long_len
= ntdll_umbstowcs( 0, long_name
, strlen(long_name
), long_nameW
, MAX_DIR_ENTRY_LEN
);
1388 if (long_len
== -1) return NULL
;
1390 str
.Buffer
= long_nameW
;
1391 str
.Length
= long_len
* sizeof(WCHAR
);
1392 str
.MaximumLength
= sizeof(long_nameW
);
1396 short_len
= ntdll_umbstowcs( 0, short_name
, strlen(short_name
),
1397 short_nameW
, sizeof(short_nameW
) / sizeof(WCHAR
) );
1398 if (short_len
== -1) short_len
= sizeof(short_nameW
) / sizeof(WCHAR
);
1400 else /* generate a short name if necessary */
1405 if (!RtlIsNameLegalDOS8Dot3( &str
, NULL
, &spaces
) || spaces
)
1406 short_len
= hash_short_file_name( &str
, short_nameW
);
1409 TRACE( "long %s short %s mask %s\n",
1410 debugstr_us(&str
), debugstr_wn(short_nameW
, short_len
), debugstr_us(mask
) );
1412 if (mask
&& !match_filename( &str
, mask
))
1414 if (!short_len
) return NULL
; /* no short name to match */
1415 str
.Buffer
= short_nameW
;
1416 str
.Length
= short_len
* sizeof(WCHAR
);
1417 str
.MaximumLength
= sizeof(short_nameW
);
1418 if (!match_filename( &str
, mask
)) return NULL
;
1421 if (get_file_info( long_name
, &st
, &attributes
) == -1) return NULL
;
1422 if (is_ignored_file( &st
))
1424 TRACE( "ignoring file %s\n", long_name
);
1427 if (!show_dot_files
&& long_name
[0] == '.' && long_name
[1] && (long_name
[1] != '.' || long_name
[2]))
1428 attributes
|= FILE_ATTRIBUTE_HIDDEN
;
1430 total_len
= dir_info_size( class, long_len
);
1431 if (io
->Information
+ total_len
> max_length
)
1433 total_len
= max_length
- io
->Information
;
1434 io
->u
.Status
= STATUS_BUFFER_OVERFLOW
;
1436 info
= (union file_directory_info
*)((char *)info_ptr
+ io
->Information
);
1437 if (st
.st_dev
!= curdir
.dev
) st
.st_ino
= 0; /* ignore inode if on a different device */
1438 /* all the structures start with a FileDirectoryInformation layout */
1439 fill_file_info( &st
, attributes
, info
, class );
1440 info
->dir
.NextEntryOffset
= total_len
;
1441 info
->dir
.FileIndex
= 0; /* NTFS always has 0 here, so let's not bother with it */
1445 case FileDirectoryInformation
:
1446 info
->dir
.FileNameLength
= long_len
* sizeof(WCHAR
);
1447 filename
= info
->dir
.FileName
;
1450 case FileFullDirectoryInformation
:
1451 info
->full
.EaSize
= 0; /* FIXME */
1452 info
->full
.FileNameLength
= long_len
* sizeof(WCHAR
);
1453 filename
= info
->full
.FileName
;
1456 case FileIdFullDirectoryInformation
:
1457 info
->id_full
.EaSize
= 0; /* FIXME */
1458 info
->id_full
.FileNameLength
= long_len
* sizeof(WCHAR
);
1459 filename
= info
->id_full
.FileName
;
1462 case FileBothDirectoryInformation
:
1463 info
->both
.EaSize
= 0; /* FIXME */
1464 info
->both
.ShortNameLength
= short_len
* sizeof(WCHAR
);
1465 for (i
= 0; i
< short_len
; i
++) info
->both
.ShortName
[i
] = toupperW(short_nameW
[i
]);
1466 info
->both
.FileNameLength
= long_len
* sizeof(WCHAR
);
1467 filename
= info
->both
.FileName
;
1470 case FileIdBothDirectoryInformation
:
1471 info
->id_both
.EaSize
= 0; /* FIXME */
1472 info
->id_both
.ShortNameLength
= short_len
* sizeof(WCHAR
);
1473 for (i
= 0; i
< short_len
; i
++) info
->id_both
.ShortName
[i
] = toupperW(short_nameW
[i
]);
1474 info
->id_both
.FileNameLength
= long_len
* sizeof(WCHAR
);
1475 filename
= info
->id_both
.FileName
;
1482 memcpy( filename
, long_nameW
, long_len
* sizeof(WCHAR
) );
1483 io
->Information
+= total_len
;
1488 #ifdef VFAT_IOCTL_READDIR_BOTH
1490 /***********************************************************************
1493 * Wrapper for the VFAT ioctl to work around various kernel bugs.
1494 * dir_section must be held by caller.
1496 static KERNEL_DIRENT
*start_vfat_ioctl( int fd
)
1498 static KERNEL_DIRENT
*de
;
1503 SIZE_T size
= 2 * sizeof(*de
) + page_size
;
1506 if (NtAllocateVirtualMemory( GetCurrentProcess(), &addr
, 1, &size
, MEM_RESERVE
, PAGE_READWRITE
))
1508 /* commit only the size needed for the dir entries */
1509 /* this leaves an extra unaccessible page, which should make the kernel */
1510 /* fail with -EFAULT before it stomps all over our memory */
1512 size
= 2 * sizeof(*de
);
1513 NtAllocateVirtualMemory( GetCurrentProcess(), &addr
, 1, &size
, MEM_COMMIT
, PAGE_READWRITE
);
1516 /* set d_reclen to 65535 to work around an AFS kernel bug */
1517 de
[0].d_reclen
= 65535;
1518 res
= ioctl( fd
, VFAT_IOCTL_READDIR_BOTH
, (long)de
);
1521 if (errno
!= ENOENT
) return NULL
; /* VFAT ioctl probably not supported */
1522 de
[0].d_reclen
= 0; /* eof */
1524 else if (!res
&& de
[0].d_reclen
== 65535) return NULL
; /* AFS bug */
1530 /***********************************************************************
1531 * read_directory_vfat
1533 * Read a directory using the VFAT ioctl; helper for NtQueryDirectoryFile.
1535 static int read_directory_vfat( int fd
, IO_STATUS_BLOCK
*io
, void *buffer
, ULONG length
,
1536 BOOLEAN single_entry
, const UNICODE_STRING
*mask
,
1537 BOOLEAN restart_scan
, FILE_INFORMATION_CLASS
class )
1542 union file_directory_info
*info
, *last_info
= NULL
;
1544 io
->u
.Status
= STATUS_SUCCESS
;
1546 if (restart_scan
) lseek( fd
, 0, SEEK_SET
);
1548 if (length
< max_dir_info_size(class)) /* we may have to return a partial entry here */
1550 off_t old_pos
= lseek( fd
, 0, SEEK_CUR
);
1552 if (!(de
= start_vfat_ioctl( fd
))) return -1; /* not supported */
1554 while (de
[0].d_reclen
)
1556 /* make sure names are null-terminated to work around an x86-64 kernel bug */
1557 len
= min(de
[0].d_reclen
, sizeof(de
[0].d_name
) - 1 );
1558 de
[0].d_name
[len
] = 0;
1559 len
= min(de
[1].d_reclen
, sizeof(de
[1].d_name
) - 1 );
1560 de
[1].d_name
[len
] = 0;
1562 if (de
[1].d_name
[0])
1563 info
= append_entry( buffer
, io
, length
, de
[1].d_name
, de
[0].d_name
, mask
, class );
1565 info
= append_entry( buffer
, io
, length
, de
[0].d_name
, NULL
, mask
, class );
1569 if (io
->u
.Status
== STATUS_BUFFER_OVERFLOW
)
1570 lseek( fd
, old_pos
, SEEK_SET
); /* restore pos to previous entry */
1573 old_pos
= lseek( fd
, 0, SEEK_CUR
);
1574 if (ioctl( fd
, VFAT_IOCTL_READDIR_BOTH
, (long)de
) == -1) break;
1577 else /* we'll only return full entries, no need to worry about overflow */
1579 if (!(de
= start_vfat_ioctl( fd
))) return -1; /* not supported */
1581 while (de
[0].d_reclen
)
1583 /* make sure names are null-terminated to work around an x86-64 kernel bug */
1584 len
= min(de
[0].d_reclen
, sizeof(de
[0].d_name
) - 1 );
1585 de
[0].d_name
[len
] = 0;
1586 len
= min(de
[1].d_reclen
, sizeof(de
[1].d_name
) - 1 );
1587 de
[1].d_name
[len
] = 0;
1589 if (de
[1].d_name
[0])
1590 info
= append_entry( buffer
, io
, length
, de
[1].d_name
, de
[0].d_name
, mask
, class );
1592 info
= append_entry( buffer
, io
, length
, de
[0].d_name
, NULL
, mask
, class );
1596 if (single_entry
) break;
1597 /* check if we still have enough space for the largest possible entry */
1598 if (io
->Information
+ max_dir_info_size(class) > length
) break;
1600 if (ioctl( fd
, VFAT_IOCTL_READDIR_BOTH
, (long)de
) == -1) break;
1604 if (last_info
) last_info
->next
= 0;
1605 else io
->u
.Status
= restart_scan
? STATUS_NO_SUCH_FILE
: STATUS_NO_MORE_FILES
;
1608 #endif /* VFAT_IOCTL_READDIR_BOTH */
1612 /***********************************************************************
1613 * read_first_dent_name
1615 * reads name of first or second dentry (if they have inodes).
1617 static char *read_first_dent_name( int which
, int fd
, off_t second_offs
, KERNEL_DIRENT64
*de_first_two
,
1618 char *buffer
, size_t size
, BOOL
*buffer_changed
)
1620 KERNEL_DIRENT64
*de
;
1627 de
= (KERNEL_DIRENT64
*)((char *)de
+ de
->d_reclen
);
1629 return de
->d_ino
? de
->d_name
: NULL
;
1632 *buffer_changed
= TRUE
;
1633 lseek( fd
, which
== 1 ? second_offs
: 0, SEEK_SET
);
1634 res
= getdents64( fd
, buffer
, size
);
1638 de
= (KERNEL_DIRENT64
*)buffer
;
1639 return de
->d_ino
? de
->d_name
: NULL
;
1642 /***********************************************************************
1643 * read_directory_getdents
1645 * Read a directory using the Linux getdents64 system call; helper for NtQueryDirectoryFile.
1647 static int read_directory_getdents( int fd
, IO_STATUS_BLOCK
*io
, void *buffer
, ULONG length
,
1648 BOOLEAN single_entry
, const UNICODE_STRING
*mask
,
1649 BOOLEAN restart_scan
, FILE_INFORMATION_CLASS
class )
1651 static off_t second_entry_pos
;
1652 static struct file_identity last_dir_id
;
1653 off_t old_pos
= 0, next_pos
;
1654 size_t size
= length
;
1655 char *data
, local_buffer
[8192];
1656 KERNEL_DIRENT64
*de
, *de_first_two
= NULL
;
1657 union file_directory_info
*info
, *last_info
= NULL
;
1658 const char *filename
;
1659 BOOL data_buffer_changed
;
1662 if (size
<= sizeof(local_buffer
) || !(data
= RtlAllocateHeap( GetProcessHeap(), 0, size
)))
1664 size
= sizeof(local_buffer
);
1665 data
= local_buffer
;
1668 if (restart_scan
) lseek( fd
, 0, SEEK_SET
);
1671 old_pos
= lseek( fd
, 0, SEEK_CUR
);
1674 io
->u
.Status
= (errno
== ENOENT
) ? STATUS_NO_MORE_FILES
: FILE_GetNtStatus();
1680 io
->u
.Status
= STATUS_SUCCESS
;
1681 de
= (KERNEL_DIRENT64
*)data
;
1683 /* if old_pos is not 0 we don't know how many entries have been returned already,
1684 * so maintain second_entry_pos to know when to return '..' */
1685 if (old_pos
!= 0 && (last_dir_id
.dev
!= curdir
.dev
|| last_dir_id
.ino
!= curdir
.ino
))
1687 lseek( fd
, 0, SEEK_SET
);
1688 res
= getdents64( fd
, data
, size
);
1691 second_entry_pos
= de
->d_off
;
1692 last_dir_id
= curdir
;
1694 lseek( fd
, old_pos
, SEEK_SET
);
1697 res
= getdents64( fd
, data
, size
);
1700 if (errno
!= ENOSYS
)
1702 io
->u
.Status
= FILE_GetNtStatus();
1708 if (old_pos
== 0 && res
> 0)
1710 second_entry_pos
= de
->d_off
;
1711 last_dir_id
= curdir
;
1712 if (res
> de
->d_reclen
)
1718 res
-= de
->d_reclen
;
1719 next_pos
= de
->d_off
;
1722 /* we must return first 2 entries as "." and "..", but getdents64()
1723 * can return them anywhere, so swap first entries with "." and ".." */
1726 else if (old_pos
== second_entry_pos
)
1728 else if (!strcmp( de
->d_name
, "." ) || !strcmp( de
->d_name
, ".." ))
1730 swap_to
= !strcmp( de
->d_name
, "." ) ? 0 : 1;
1731 data_buffer_changed
= FALSE
;
1733 filename
= read_first_dent_name( swap_to
, fd
, second_entry_pos
, de_first_two
,
1734 data
, size
, &data_buffer_changed
);
1735 if (filename
!= NULL
&& (!strcmp( filename
, "." ) || !strcmp( filename
, ".." )))
1736 filename
= read_first_dent_name( swap_to
^ 1, fd
, second_entry_pos
, NULL
,
1737 data
, size
, &data_buffer_changed
);
1738 if (data_buffer_changed
)
1740 lseek( fd
, next_pos
, SEEK_SET
);
1745 filename
= de
->d_name
;
1747 if (filename
&& (info
= append_entry( buffer
, io
, length
, filename
, NULL
, mask
, class )))
1750 if (io
->u
.Status
== STATUS_BUFFER_OVERFLOW
)
1752 lseek( fd
, old_pos
, SEEK_SET
); /* restore pos to previous entry */
1755 /* check if we still have enough space for the largest possible entry */
1756 if (single_entry
|| io
->Information
+ max_dir_info_size(class) > length
)
1758 if (res
> 0) lseek( fd
, next_pos
, SEEK_SET
); /* set pos to next entry */
1763 /* move on to the next entry */
1764 if (res
> 0) de
= (KERNEL_DIRENT64
*)((char *)de
+ de
->d_reclen
);
1767 res
= getdents64( fd
, data
, size
);
1768 de
= (KERNEL_DIRENT64
*)data
;
1769 de_first_two
= NULL
;
1773 if (last_info
) last_info
->next
= 0;
1774 else io
->u
.Status
= restart_scan
? STATUS_NO_SUCH_FILE
: STATUS_NO_MORE_FILES
;
1777 if (data
!= local_buffer
) RtlFreeHeap( GetProcessHeap(), 0, data
);
1781 #elif defined HAVE_GETDIRENTRIES
1783 #ifdef _DARWIN_FEATURE_64_BIT_INODE
1785 /* Darwin doesn't provide a version of getdirentries with support for 64-bit
1786 * inodes. When 64-bit inodes are enabled, the getdirentries symbol is mapped
1787 * to _getdirentries_is_not_available_when_64_bit_inodes_are_in_effect so that
1788 * we get link errors if we try to use it. We still need getdirentries, but we
1789 * don't need it to support 64-bit inodes. So, we use the legacy getdirentries
1790 * with 32-bit inodes. We have to be careful to use a corresponding dirent
1793 int darwin_legacy_getdirentries(int, char *, int, long *) __asm("_getdirentries");
1794 #define getdirentries darwin_legacy_getdirentries
1796 struct darwin_legacy_dirent
{
1798 __uint16_t d_reclen
;
1801 char d_name
[__DARWIN_MAXNAMLEN
+ 1];
1803 #define dirent darwin_legacy_dirent
1807 /***********************************************************************
1808 * wine_getdirentries
1810 * Wrapper for the BSD getdirentries system call to fix a bug in the
1811 * Mac OS X version. For some file systems (at least Apple Filing
1812 * Protocol a.k.a. AFP), getdirentries resets the file position to 0
1813 * when it's about to return 0 (no more entries). So, a subsequent
1814 * getdirentries call starts over at the beginning again, causing an
1817 static inline int wine_getdirentries(int fd
, char *buf
, int nbytes
, long *basep
)
1819 int res
= getdirentries(fd
, buf
, nbytes
, basep
);
1822 lseek(fd
, *basep
, SEEK_SET
);
1827 static inline int dir_reclen(struct dirent
*de
)
1829 #ifdef HAVE_STRUCT_DIRENT_D_RECLEN
1830 return de
->d_reclen
;
1832 return _DIRENT_RECLEN(de
->d_namlen
);
1836 /***********************************************************************
1837 * read_directory_getdirentries
1839 * Read a directory using the BSD getdirentries system call; helper for NtQueryDirectoryFile.
1841 static int read_directory_getdirentries( int fd
, IO_STATUS_BLOCK
*io
, void *buffer
, ULONG length
,
1842 BOOLEAN single_entry
, const UNICODE_STRING
*mask
,
1843 BOOLEAN restart_scan
, FILE_INFORMATION_CLASS
class )
1846 ULONG_PTR restart_info_pos
= 0;
1847 size_t size
, initial_size
= length
;
1848 int res
, fake_dot_dot
= 1;
1849 char *data
, local_buffer
[8192];
1851 union file_directory_info
*info
, *last_info
= NULL
, *restart_last_info
= NULL
;
1853 size
= initial_size
;
1854 data
= local_buffer
;
1855 if (size
> sizeof(local_buffer
) && !(data
= RtlAllocateHeap( GetProcessHeap(), 0, size
)))
1857 io
->u
.Status
= STATUS_NO_MEMORY
;
1858 return io
->u
.Status
;
1861 if (restart_scan
) lseek( fd
, 0, SEEK_SET
);
1863 io
->u
.Status
= STATUS_SUCCESS
;
1865 /* FIXME: should make sure size is larger than filesystem block size */
1866 res
= wine_getdirentries( fd
, data
, size
, &restart_pos
);
1869 io
->u
.Status
= FILE_GetNtStatus();
1874 de
= (struct dirent
*)data
;
1878 /* check if we got . and .. from getdirentries */
1881 if (!strcmp( de
->d_name
, "." ) && res
> dir_reclen(de
))
1883 struct dirent
*next_de
= (struct dirent
*)(data
+ dir_reclen(de
));
1884 if (!strcmp( next_de
->d_name
, ".." )) fake_dot_dot
= 0;
1887 /* make sure we have enough room for both entries */
1890 const ULONG min_info_size
= dir_info_size( class, 1 ) + dir_info_size( class, 2 );
1891 if (length
< min_info_size
|| single_entry
)
1893 FIXME( "not enough room %u/%u for fake . and .. entries\n", length
, single_entry
);
1900 if ((info
= append_entry( buffer
, io
, length
, ".", NULL
, mask
, class )))
1902 if ((info
= append_entry( buffer
, io
, length
, "..", NULL
, mask
, class )))
1905 restart_last_info
= last_info
;
1906 restart_info_pos
= io
->Information
;
1908 /* check if we still have enough space for the largest possible entry */
1909 if (last_info
&& io
->Information
+ max_dir_info_size(class) > length
)
1911 lseek( fd
, 0, SEEK_SET
); /* reset pos to first entry */
1919 res
-= dir_reclen(de
);
1921 !(fake_dot_dot
&& (!strcmp( de
->d_name
, "." ) || !strcmp( de
->d_name
, ".." ))) &&
1922 ((info
= append_entry( buffer
, io
, length
, de
->d_name
, NULL
, mask
, class ))))
1925 if (io
->u
.Status
== STATUS_BUFFER_OVERFLOW
)
1927 lseek( fd
, (unsigned long)restart_pos
, SEEK_SET
);
1928 if (restart_info_pos
) /* if we have a complete read already, return it */
1930 io
->u
.Status
= STATUS_SUCCESS
;
1931 io
->Information
= restart_info_pos
;
1932 last_info
= restart_last_info
;
1935 /* otherwise restart from the start with a smaller size */
1936 size
= (char *)de
- data
;
1938 io
->Information
= 0;
1942 if (!has_wildcard( mask
)) break;
1943 /* if we have to return but the buffer contains more data, restart with a smaller size */
1944 if (res
> 0 && (single_entry
|| io
->Information
+ max_dir_info_size(class) > length
))
1946 lseek( fd
, (unsigned long)restart_pos
, SEEK_SET
);
1947 size
= (char *)de
+ dir_reclen(de
) - data
;
1948 io
->Information
= restart_info_pos
;
1949 last_info
= restart_last_info
;
1953 /* move on to the next entry */
1956 de
= (struct dirent
*)((char *)de
+ dir_reclen(de
));
1959 if (size
< initial_size
) break; /* already restarted once, give up now */
1960 restart_last_info
= last_info
;
1961 restart_info_pos
= io
->Information
;
1963 res
= wine_getdirentries( fd
, data
, size
, &restart_pos
);
1964 de
= (struct dirent
*)data
;
1967 if (last_info
) last_info
->next
= 0;
1968 else io
->u
.Status
= restart_scan
? STATUS_NO_SUCH_FILE
: STATUS_NO_MORE_FILES
;
1971 if (data
!= local_buffer
) RtlFreeHeap( GetProcessHeap(), 0, data
);
1975 #ifdef _DARWIN_FEATURE_64_BIT_INODE
1976 #undef getdirentries
1980 #endif /* HAVE_GETDIRENTRIES */
1983 /***********************************************************************
1984 * read_directory_readdir
1986 * Read a directory using the POSIX readdir interface; helper for NtQueryDirectoryFile.
1988 static void read_directory_readdir( int fd
, IO_STATUS_BLOCK
*io
, void *buffer
, ULONG length
,
1989 BOOLEAN single_entry
, const UNICODE_STRING
*mask
,
1990 BOOLEAN restart_scan
, FILE_INFORMATION_CLASS
class )
1993 off_t i
, old_pos
= 0;
1995 union file_directory_info
*info
, *last_info
= NULL
;
1997 if (!(dir
= opendir( "." )))
1999 io
->u
.Status
= FILE_GetNtStatus();
2005 old_pos
= lseek( fd
, 0, SEEK_CUR
);
2006 /* skip the right number of entries */
2007 for (i
= 0; i
< old_pos
- 2; i
++)
2009 if (!readdir( dir
))
2012 io
->u
.Status
= STATUS_NO_MORE_FILES
;
2017 io
->u
.Status
= STATUS_SUCCESS
;
2022 info
= append_entry( buffer
, io
, length
, ".", NULL
, mask
, class );
2023 else if (old_pos
== 1)
2024 info
= append_entry( buffer
, io
, length
, "..", NULL
, mask
, class );
2025 else if ((de
= readdir( dir
)))
2027 if (strcmp( de
->d_name
, "." ) && strcmp( de
->d_name
, ".." ))
2028 info
= append_entry( buffer
, io
, length
, de
->d_name
, NULL
, mask
, class );
2038 if (io
->u
.Status
== STATUS_BUFFER_OVERFLOW
)
2040 old_pos
--; /* restore pos to previous entry */
2043 if (single_entry
) break;
2044 /* check if we still have enough space for the largest possible entry */
2045 if (io
->Information
+ max_dir_info_size(class) > length
) break;
2049 lseek( fd
, old_pos
, SEEK_SET
); /* store dir offset as filepos for fd */
2052 if (last_info
) last_info
->next
= 0;
2053 else io
->u
.Status
= restart_scan
? STATUS_NO_SUCH_FILE
: STATUS_NO_MORE_FILES
;
2056 /***********************************************************************
2057 * read_directory_stat
2059 * Read a single file from a directory by determining whether the file
2060 * identified by mask exists using stat.
2062 static int read_directory_stat( int fd
, IO_STATUS_BLOCK
*io
, void *buffer
, ULONG length
,
2063 BOOLEAN single_entry
, const UNICODE_STRING
*mask
,
2064 BOOLEAN restart_scan
, FILE_INFORMATION_CLASS
class )
2066 int unix_len
, ret
, used_default
;
2070 TRACE("trying optimisation for file %s\n", debugstr_us( mask
));
2072 unix_len
= ntdll_wcstoumbs( 0, mask
->Buffer
, mask
->Length
/ sizeof(WCHAR
), NULL
, 0, NULL
, NULL
);
2073 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, unix_len
+ 1)))
2075 io
->u
.Status
= STATUS_NO_MEMORY
;
2078 ret
= ntdll_wcstoumbs( 0, mask
->Buffer
, mask
->Length
/ sizeof(WCHAR
), unix_name
, unix_len
,
2079 NULL
, &used_default
);
2080 if (ret
> 0 && !used_default
)
2085 lseek( fd
, 0, SEEK_SET
);
2087 else if (lseek( fd
, 0, SEEK_CUR
) != 0)
2089 io
->u
.Status
= STATUS_NO_MORE_FILES
;
2094 ret
= stat( unix_name
, &st
);
2097 union file_directory_info
*info
= append_entry( buffer
, io
, length
, unix_name
, NULL
, NULL
, class );
2101 if (io
->u
.Status
!= STATUS_BUFFER_OVERFLOW
) lseek( fd
, 1, SEEK_CUR
);
2103 else io
->u
.Status
= STATUS_NO_MORE_FILES
;
2109 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2111 TRACE("returning %d\n", ret
);
2117 /******************************************************************************
2118 * NtQueryDirectoryFile [NTDLL.@]
2119 * ZwQueryDirectoryFile [NTDLL.@]
2121 NTSTATUS WINAPI
NtQueryDirectoryFile( HANDLE handle
, HANDLE event
,
2122 PIO_APC_ROUTINE apc_routine
, PVOID apc_context
,
2123 PIO_STATUS_BLOCK io
,
2124 PVOID buffer
, ULONG length
,
2125 FILE_INFORMATION_CLASS info_class
,
2126 BOOLEAN single_entry
,
2127 PUNICODE_STRING mask
,
2128 BOOLEAN restart_scan
)
2130 int cwd
, fd
, needs_close
;
2132 TRACE("(%p %p %p %p %p %p 0x%08x 0x%08x 0x%08x %s 0x%08x\n",
2133 handle
, event
, apc_routine
, apc_context
, io
, buffer
,
2134 length
, info_class
, single_entry
, debugstr_us(mask
),
2137 if (event
|| apc_routine
)
2139 FIXME( "Unsupported yet option\n" );
2140 return io
->u
.Status
= STATUS_NOT_IMPLEMENTED
;
2144 case FileDirectoryInformation
:
2145 case FileBothDirectoryInformation
:
2146 case FileFullDirectoryInformation
:
2147 case FileIdBothDirectoryInformation
:
2148 case FileIdFullDirectoryInformation
:
2149 if (length
< dir_info_size( info_class
, 1 )) return io
->u
.Status
= STATUS_INFO_LENGTH_MISMATCH
;
2150 if (!buffer
) return io
->u
.Status
= STATUS_ACCESS_VIOLATION
;
2153 FIXME( "Unsupported file info class %d\n", info_class
);
2154 return io
->u
.Status
= STATUS_NOT_IMPLEMENTED
;
2157 if ((io
->u
.Status
= server_get_unix_fd( handle
, FILE_LIST_DIRECTORY
, &fd
, &needs_close
, NULL
, NULL
)) != STATUS_SUCCESS
)
2158 return io
->u
.Status
;
2160 io
->Information
= 0;
2162 RtlRunOnceExecuteOnce( &init_once
, init_options
, NULL
, NULL
);
2164 RtlEnterCriticalSection( &dir_section
);
2166 cwd
= open( ".", O_RDONLY
);
2167 if (fchdir( fd
) != -1)
2171 curdir
.dev
= st
.st_dev
;
2172 curdir
.ino
= st
.st_ino
;
2173 #ifdef VFAT_IOCTL_READDIR_BOTH
2174 if ((read_directory_vfat( fd
, io
, buffer
, length
, single_entry
,
2175 mask
, restart_scan
, info_class
)) != -1) goto done
;
2177 if (!has_wildcard( mask
) &&
2178 read_directory_stat( fd
, io
, buffer
, length
, single_entry
,
2179 mask
, restart_scan
, info_class
) != -1) goto done
;
2181 if ((read_directory_getdents( fd
, io
, buffer
, length
, single_entry
,
2182 mask
, restart_scan
, info_class
)) != -1) goto done
;
2183 #elif defined HAVE_GETDIRENTRIES
2184 if ((read_directory_getdirentries( fd
, io
, buffer
, length
, single_entry
,
2185 mask
, restart_scan
, info_class
)) != -1) goto done
;
2187 read_directory_readdir( fd
, io
, buffer
, length
, single_entry
, mask
, restart_scan
, info_class
);
2190 if (cwd
== -1 || fchdir( cwd
) == -1) chdir( "/" );
2192 else io
->u
.Status
= FILE_GetNtStatus();
2194 RtlLeaveCriticalSection( &dir_section
);
2196 if (needs_close
) close( fd
);
2197 if (cwd
!= -1) close( cwd
);
2198 TRACE( "=> %x (%ld)\n", io
->u
.Status
, io
->Information
);
2199 return io
->u
.Status
;
2203 /***********************************************************************
2206 * Find a file in a directory the hard way, by doing a case-insensitive search.
2207 * The file found is appended to unix_name at pos.
2208 * There must be at least MAX_DIR_ENTRY_LEN+2 chars available at pos.
2210 static NTSTATUS
find_file_in_dir( char *unix_name
, int pos
, const WCHAR
*name
, int length
,
2211 BOOLEAN check_case
, BOOLEAN
*is_win_dir
)
2213 WCHAR buffer
[MAX_DIR_ENTRY_LEN
];
2215 BOOLEAN spaces
, is_name_8_dot_3
;
2219 int ret
, used_default
;
2221 /* try a shortcut for this directory */
2223 unix_name
[pos
++] = '/';
2224 ret
= ntdll_wcstoumbs( 0, name
, length
, unix_name
+ pos
, MAX_DIR_ENTRY_LEN
,
2225 NULL
, &used_default
);
2226 /* if we used the default char, the Unix name won't round trip properly back to Unicode */
2227 /* so it cannot match the file we are looking for */
2228 if (ret
>= 0 && !used_default
)
2230 unix_name
[pos
+ ret
] = 0;
2231 if (!stat( unix_name
, &st
))
2233 if (is_win_dir
) *is_win_dir
= is_same_file( &windir
, &st
);
2234 return STATUS_SUCCESS
;
2237 if (check_case
) goto not_found
; /* we want an exact match */
2239 if (pos
> 1) unix_name
[pos
- 1] = 0;
2240 else unix_name
[1] = 0; /* keep the initial slash */
2242 /* check if it fits in 8.3 so that we don't look for short names if we won't need them */
2244 str
.Buffer
= (WCHAR
*)name
;
2245 str
.Length
= length
* sizeof(WCHAR
);
2246 str
.MaximumLength
= str
.Length
;
2247 is_name_8_dot_3
= RtlIsNameLegalDOS8Dot3( &str
, NULL
, &spaces
) && !spaces
;
2248 #ifndef VFAT_IOCTL_READDIR_BOTH
2249 is_name_8_dot_3
= is_name_8_dot_3
&& length
>= 8 && name
[4] == '~';
2252 if (!is_name_8_dot_3
&& !get_dir_case_sensitivity( unix_name
)) goto not_found
;
2254 /* now look for it through the directory */
2256 #ifdef VFAT_IOCTL_READDIR_BOTH
2257 if (is_name_8_dot_3
)
2259 int fd
= open( unix_name
, O_RDONLY
| O_DIRECTORY
);
2264 RtlEnterCriticalSection( &dir_section
);
2265 if ((kde
= start_vfat_ioctl( fd
)))
2267 unix_name
[pos
- 1] = '/';
2268 while (kde
[0].d_reclen
)
2270 /* make sure names are null-terminated to work around an x86-64 kernel bug */
2271 size_t len
= min(kde
[0].d_reclen
, sizeof(kde
[0].d_name
) - 1 );
2272 kde
[0].d_name
[len
] = 0;
2273 len
= min(kde
[1].d_reclen
, sizeof(kde
[1].d_name
) - 1 );
2274 kde
[1].d_name
[len
] = 0;
2276 if (kde
[1].d_name
[0])
2278 ret
= ntdll_umbstowcs( 0, kde
[1].d_name
, strlen(kde
[1].d_name
),
2279 buffer
, MAX_DIR_ENTRY_LEN
);
2280 if (ret
== length
&& !memicmpW( buffer
, name
, length
))
2282 strcpy( unix_name
+ pos
, kde
[1].d_name
);
2283 RtlLeaveCriticalSection( &dir_section
);
2288 ret
= ntdll_umbstowcs( 0, kde
[0].d_name
, strlen(kde
[0].d_name
),
2289 buffer
, MAX_DIR_ENTRY_LEN
);
2290 if (ret
== length
&& !memicmpW( buffer
, name
, length
))
2292 strcpy( unix_name
+ pos
,
2293 kde
[1].d_name
[0] ? kde
[1].d_name
: kde
[0].d_name
);
2294 RtlLeaveCriticalSection( &dir_section
);
2298 if (ioctl( fd
, VFAT_IOCTL_READDIR_BOTH
, (long)kde
) == -1)
2300 RtlLeaveCriticalSection( &dir_section
);
2306 RtlLeaveCriticalSection( &dir_section
);
2309 /* fall through to normal handling */
2311 #endif /* VFAT_IOCTL_READDIR_BOTH */
2313 if (!(dir
= opendir( unix_name
)))
2315 if (errno
== ENOENT
) return STATUS_OBJECT_PATH_NOT_FOUND
;
2316 else return FILE_GetNtStatus();
2318 unix_name
[pos
- 1] = '/';
2319 str
.Buffer
= buffer
;
2320 str
.MaximumLength
= sizeof(buffer
);
2321 while ((de
= readdir( dir
)))
2323 ret
= ntdll_umbstowcs( 0, de
->d_name
, strlen(de
->d_name
), buffer
, MAX_DIR_ENTRY_LEN
);
2324 if (ret
== length
&& !memicmpW( buffer
, name
, length
))
2326 strcpy( unix_name
+ pos
, de
->d_name
);
2331 if (!is_name_8_dot_3
) continue;
2333 str
.Length
= ret
* sizeof(WCHAR
);
2334 if (!RtlIsNameLegalDOS8Dot3( &str
, NULL
, &spaces
) || spaces
)
2336 WCHAR short_nameW
[12];
2337 ret
= hash_short_file_name( &str
, short_nameW
);
2338 if (ret
== length
&& !memicmpW( short_nameW
, name
, length
))
2340 strcpy( unix_name
+ pos
, de
->d_name
);
2349 unix_name
[pos
- 1] = 0;
2350 return STATUS_OBJECT_PATH_NOT_FOUND
;
2353 if (is_win_dir
&& !stat( unix_name
, &st
)) *is_win_dir
= is_same_file( &windir
, &st
);
2354 return STATUS_SUCCESS
;
2360 static const WCHAR catrootW
[] = {'s','y','s','t','e','m','3','2','\\','c','a','t','r','o','o','t',0};
2361 static const WCHAR catroot2W
[] = {'s','y','s','t','e','m','3','2','\\','c','a','t','r','o','o','t','2',0};
2362 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};
2363 static const WCHAR driversetcW
[] = {'s','y','s','t','e','m','3','2','\\','d','r','i','v','e','r','s','\\','e','t','c',0};
2364 static const WCHAR logfilesW
[] = {'s','y','s','t','e','m','3','2','\\','l','o','g','f','i','l','e','s',0};
2365 static const WCHAR spoolW
[] = {'s','y','s','t','e','m','3','2','\\','s','p','o','o','l',0};
2366 static const WCHAR system32W
[] = {'s','y','s','t','e','m','3','2',0};
2367 static const WCHAR syswow64W
[] = {'s','y','s','w','o','w','6','4',0};
2368 static const WCHAR sysnativeW
[] = {'s','y','s','n','a','t','i','v','e',0};
2369 static const WCHAR regeditW
[] = {'r','e','g','e','d','i','t','.','e','x','e',0};
2370 static const WCHAR wow_regeditW
[] = {'s','y','s','w','o','w','6','4','\\','r','e','g','e','d','i','t','.','e','x','e',0};
2374 const WCHAR
*source
;
2375 const WCHAR
*dos_target
;
2376 const char *unix_target
;
2379 { catrootW
, NULL
, NULL
},
2380 { catroot2W
, NULL
, NULL
},
2381 { driversstoreW
, NULL
, NULL
},
2382 { driversetcW
, NULL
, NULL
},
2383 { logfilesW
, NULL
, NULL
},
2384 { spoolW
, NULL
, NULL
},
2385 { system32W
, syswow64W
, NULL
},
2386 { sysnativeW
, system32W
, NULL
},
2387 { regeditW
, wow_regeditW
, NULL
}
2390 static unsigned int nb_redirects
;
2393 /***********************************************************************
2394 * get_redirect_target
2396 * Find the target unix name for a redirected dir.
2398 static const char *get_redirect_target( const char *windows_dir
, const WCHAR
*name
)
2400 int used_default
, len
, pos
, win_len
= strlen( windows_dir
);
2401 char *unix_name
, *unix_target
= NULL
;
2404 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, win_len
+ MAX_DIR_ENTRY_LEN
+ 2 )))
2406 memcpy( unix_name
, windows_dir
, win_len
);
2411 const WCHAR
*end
, *next
;
2413 for (end
= name
; *end
; end
++) if (IS_SEPARATOR(*end
)) break;
2414 for (next
= end
; *next
; next
++) if (!IS_SEPARATOR(*next
)) break;
2416 status
= find_file_in_dir( unix_name
, pos
, name
, end
- name
, FALSE
, NULL
);
2417 if (status
== STATUS_OBJECT_PATH_NOT_FOUND
&& !*next
) /* not finding last element is ok */
2419 len
= ntdll_wcstoumbs( 0, name
, end
- name
, unix_name
+ pos
+ 1,
2420 MAX_DIR_ENTRY_LEN
- (pos
- win_len
), NULL
, &used_default
);
2421 if (len
> 0 && !used_default
)
2423 unix_name
[pos
] = '/';
2429 if (status
) goto done
;
2430 pos
+= strlen( unix_name
+ pos
);
2434 if ((unix_target
= RtlAllocateHeap( GetProcessHeap(), 0, pos
- win_len
)))
2435 memcpy( unix_target
, unix_name
+ win_len
+ 1, pos
- win_len
);
2438 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2443 /***********************************************************************
2446 static void init_redirects(void)
2448 UNICODE_STRING nt_name
;
2449 ANSI_STRING unix_name
;
2454 if (!RtlDosPathNameToNtPathName_U( user_shared_data
->NtSystemRoot
, &nt_name
, NULL
, NULL
))
2456 ERR( "can't convert %s\n", debugstr_w(user_shared_data
->NtSystemRoot
) );
2459 status
= wine_nt_to_unix_file_name( &nt_name
, &unix_name
, FILE_OPEN_IF
, FALSE
);
2460 RtlFreeUnicodeString( &nt_name
);
2463 ERR( "cannot open %s (%x)\n", debugstr_w(user_shared_data
->NtSystemRoot
), status
);
2466 if (!stat( unix_name
.Buffer
, &st
))
2468 windir
.dev
= st
.st_dev
;
2469 windir
.ino
= st
.st_ino
;
2470 nb_redirects
= sizeof(redirects
) / sizeof(redirects
[0]);
2471 for (i
= 0; i
< nb_redirects
; i
++)
2473 if (!redirects
[i
].dos_target
) continue;
2474 redirects
[i
].unix_target
= get_redirect_target( unix_name
.Buffer
, redirects
[i
].dos_target
);
2475 TRACE( "%s -> %s\n", debugstr_w(redirects
[i
].source
), redirects
[i
].unix_target
);
2478 RtlFreeAnsiString( &unix_name
);
2483 /***********************************************************************
2486 * Check if path matches a redirect name. If yes, return matched length.
2488 static int match_redirect( const WCHAR
*path
, int len
, const WCHAR
*redir
, BOOLEAN check_case
)
2492 while (i
< len
&& *redir
)
2494 if (IS_SEPARATOR(path
[i
]))
2496 if (*redir
++ != '\\') return 0;
2497 while (i
< len
&& IS_SEPARATOR(path
[i
])) i
++;
2498 continue; /* move on to next path component */
2500 else if (check_case
)
2502 if (path
[i
] != *redir
) return 0;
2506 if (tolowerW(path
[i
]) != tolowerW(*redir
)) return 0;
2511 if (*redir
) return 0;
2512 if (i
< len
&& !IS_SEPARATOR(path
[i
])) return 0;
2513 while (i
< len
&& IS_SEPARATOR(path
[i
])) i
++;
2518 /***********************************************************************
2521 * Retrieve the Unix path corresponding to a redirected path if any.
2523 static int get_redirect_path( char *unix_name
, int pos
, const WCHAR
*name
, int length
, BOOLEAN check_case
)
2528 for (i
= 0; i
< nb_redirects
; i
++)
2530 if ((len
= match_redirect( name
, length
, redirects
[i
].source
, check_case
)))
2532 if (!redirects
[i
].unix_target
) break;
2533 unix_name
[pos
++] = '/';
2534 strcpy( unix_name
+ pos
, redirects
[i
].unix_target
);
2543 /* there are no redirects on 64-bit */
2545 static const unsigned int nb_redirects
= 0;
2547 static int get_redirect_path( char *unix_name
, int pos
, const WCHAR
*name
, int length
, BOOLEAN check_case
)
2554 /***********************************************************************
2555 * DIR_init_windows_dir
2557 void DIR_init_windows_dir( const WCHAR
*win
, const WCHAR
*sys
)
2559 /* FIXME: should probably store paths as NT file names */
2561 RtlCreateUnicodeString( &system_dir
, sys
);
2564 if (is_wow64
) init_redirects();
2569 /******************************************************************************
2572 * Get the Unix path of a DOS device.
2574 static NTSTATUS
get_dos_device( const WCHAR
*name
, UINT name_len
, ANSI_STRING
*unix_name_ret
)
2576 const char *config_dir
= wine_get_config_dir();
2578 char *unix_name
, *new_name
, *dev
;
2582 /* make sure the device name is ASCII */
2583 for (i
= 0; i
< name_len
; i
++)
2584 if (name
[i
] <= 32 || name
[i
] >= 127) return STATUS_BAD_DEVICE_TYPE
;
2586 unix_len
= strlen(config_dir
) + sizeof("/dosdevices/") + name_len
+ 1;
2588 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, unix_len
)))
2589 return STATUS_NO_MEMORY
;
2591 strcpy( unix_name
, config_dir
);
2592 strcat( unix_name
, "/dosdevices/" );
2593 dev
= unix_name
+ strlen(unix_name
);
2595 for (i
= 0; i
< name_len
; i
++) dev
[i
] = (char)tolowerW(name
[i
]);
2598 /* special case for drive devices */
2599 if (name_len
== 2 && dev
[1] == ':')
2607 if (!stat( unix_name
, &st
))
2609 TRACE( "%s -> %s\n", debugstr_wn(name
,name_len
), debugstr_a(unix_name
) );
2610 unix_name_ret
->Buffer
= unix_name
;
2611 unix_name_ret
->Length
= strlen(unix_name
);
2612 unix_name_ret
->MaximumLength
= unix_len
;
2613 return STATUS_SUCCESS
;
2617 /* now try some defaults for it */
2618 if (!strcmp( dev
, "aux" ))
2620 strcpy( dev
, "com1" );
2623 if (!strcmp( dev
, "prn" ))
2625 strcpy( dev
, "lpt1" );
2628 if (!strcmp( dev
, "nul" ))
2630 strcpy( unix_name
, "/dev/null" );
2631 dev
= NULL
; /* last try */
2636 if (dev
[1] == ':' && dev
[2] == ':') /* drive device */
2638 dev
[2] = 0; /* remove last ':' to get the drive mount point symlink */
2639 new_name
= get_default_drive_device( unix_name
);
2641 else if (!strncmp( dev
, "com", 3 )) new_name
= get_default_com_device( atoi(dev
+ 3 ));
2642 else if (!strncmp( dev
, "lpt", 3 )) new_name
= get_default_lpt_device( atoi(dev
+ 3 ));
2644 if (!new_name
) break;
2646 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2647 unix_name
= new_name
;
2648 unix_len
= strlen(unix_name
) + 1;
2649 dev
= NULL
; /* last try */
2651 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2652 return STATUS_BAD_DEVICE_TYPE
;
2656 /* return the length of the DOS namespace prefix if any */
2657 static inline int get_dos_prefix_len( const UNICODE_STRING
*name
)
2659 static const WCHAR nt_prefixW
[] = {'\\','?','?','\\'};
2660 static const WCHAR dosdev_prefixW
[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\'};
2662 if (name
->Length
> sizeof(nt_prefixW
) &&
2663 !memcmp( name
->Buffer
, nt_prefixW
, sizeof(nt_prefixW
) ))
2664 return sizeof(nt_prefixW
) / sizeof(WCHAR
);
2666 if (name
->Length
> sizeof(dosdev_prefixW
) &&
2667 !memicmpW( name
->Buffer
, dosdev_prefixW
, sizeof(dosdev_prefixW
)/sizeof(WCHAR
) ))
2668 return sizeof(dosdev_prefixW
) / sizeof(WCHAR
);
2674 /******************************************************************************
2677 * Recursively search directories from the dir queue for a given inode.
2679 static NTSTATUS
find_file_id( ANSI_STRING
*unix_name
, ULONGLONG file_id
, dev_t dev
)
2687 while (!(status
= next_dir_in_queue( unix_name
->Buffer
)))
2689 if (!(dir
= opendir( unix_name
->Buffer
))) continue;
2690 TRACE( "searching %s for %s\n", unix_name
->Buffer
, wine_dbgstr_longlong(file_id
) );
2691 pos
= strlen( unix_name
->Buffer
);
2692 if (pos
+ MAX_DIR_ENTRY_LEN
>= unix_name
->MaximumLength
/sizeof(WCHAR
))
2694 char *new = RtlReAllocateHeap( GetProcessHeap(), 0, unix_name
->Buffer
,
2695 unix_name
->MaximumLength
* 2 );
2699 return STATUS_NO_MEMORY
;
2701 unix_name
->MaximumLength
*= 2;
2702 unix_name
->Buffer
= new;
2704 unix_name
->Buffer
[pos
++] = '/';
2705 while ((de
= readdir( dir
)))
2707 if (!strcmp( de
->d_name
, "." ) || !strcmp( de
->d_name
, ".." )) continue;
2708 strcpy( unix_name
->Buffer
+ pos
, de
->d_name
);
2709 if (lstat( unix_name
->Buffer
, &st
) == -1) continue;
2710 if (st
.st_dev
!= dev
) continue;
2711 if (st
.st_ino
== file_id
)
2714 return STATUS_SUCCESS
;
2716 if (!S_ISDIR( st
.st_mode
)) continue;
2717 if ((status
= add_dir_to_queue( unix_name
->Buffer
)) != STATUS_SUCCESS
)
2729 /******************************************************************************
2730 * file_id_to_unix_file_name
2732 * Lookup a file from its file id instead of its name.
2734 NTSTATUS
file_id_to_unix_file_name( const OBJECT_ATTRIBUTES
*attr
, ANSI_STRING
*unix_name
)
2736 enum server_fd_type type
;
2737 int old_cwd
, root_fd
, needs_close
;
2740 struct stat st
, root_st
;
2742 if (attr
->ObjectName
->Length
!= sizeof(ULONGLONG
)) return STATUS_OBJECT_PATH_SYNTAX_BAD
;
2743 if (!attr
->RootDirectory
) return STATUS_INVALID_PARAMETER
;
2744 memcpy( &file_id
, attr
->ObjectName
->Buffer
, sizeof(file_id
) );
2746 unix_name
->MaximumLength
= 2 * MAX_DIR_ENTRY_LEN
+ 4;
2747 if (!(unix_name
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, unix_name
->MaximumLength
)))
2748 return STATUS_NO_MEMORY
;
2749 strcpy( unix_name
->Buffer
, "." );
2751 if ((status
= server_get_unix_fd( attr
->RootDirectory
, 0, &root_fd
, &needs_close
, &type
, NULL
)))
2754 if (type
!= FD_TYPE_DIR
)
2756 status
= STATUS_OBJECT_TYPE_MISMATCH
;
2760 fstat( root_fd
, &root_st
);
2761 if (root_st
.st_ino
== file_id
) /* shortcut for "." */
2763 status
= STATUS_SUCCESS
;
2767 RtlEnterCriticalSection( &dir_section
);
2768 if ((old_cwd
= open( ".", O_RDONLY
)) != -1 && fchdir( root_fd
) != -1)
2770 /* shortcut for ".." */
2771 if (!stat( "..", &st
) && st
.st_dev
== root_st
.st_dev
&& st
.st_ino
== file_id
)
2773 strcpy( unix_name
->Buffer
, ".." );
2774 status
= STATUS_SUCCESS
;
2778 status
= add_dir_to_queue( "." );
2780 status
= find_file_id( unix_name
, file_id
, root_st
.st_dev
);
2781 if (!status
) /* get rid of "./" prefix */
2782 memmove( unix_name
->Buffer
, unix_name
->Buffer
+ 2, strlen(unix_name
->Buffer
) - 1 );
2785 if (fchdir( old_cwd
) == -1) chdir( "/" );
2787 else status
= FILE_GetNtStatus();
2788 RtlLeaveCriticalSection( &dir_section
);
2789 if (old_cwd
!= -1) close( old_cwd
);
2792 if (status
== STATUS_SUCCESS
)
2794 TRACE( "%s -> %s\n", wine_dbgstr_longlong(file_id
), debugstr_a(unix_name
->Buffer
) );
2795 unix_name
->Length
= strlen( unix_name
->Buffer
);
2799 TRACE( "%s not found in dir %p\n", wine_dbgstr_longlong(file_id
), attr
->RootDirectory
);
2800 RtlFreeHeap( GetProcessHeap(), 0, unix_name
->Buffer
);
2802 if (needs_close
) close( root_fd
);
2807 /******************************************************************************
2810 * Helper for nt_to_unix_file_name
2812 static NTSTATUS
lookup_unix_name( const WCHAR
*name
, int name_len
, char **buffer
, int unix_len
, int pos
,
2813 UINT disposition
, BOOLEAN check_case
)
2816 int ret
, used_default
, len
;
2818 char *unix_name
= *buffer
;
2819 const BOOL redirect
= nb_redirects
&& ntdll_get_thread_data()->wow64_redir
;
2821 /* try a shortcut first */
2823 ret
= ntdll_wcstoumbs( 0, name
, name_len
, unix_name
+ pos
, unix_len
- pos
- 1,
2824 NULL
, &used_default
);
2826 while (name_len
&& IS_SEPARATOR(*name
))
2832 if (ret
>= 0 && !used_default
) /* if we used the default char the name didn't convert properly */
2835 unix_name
[pos
+ ret
] = 0;
2836 for (p
= unix_name
+ pos
; *p
; p
++) if (*p
== '\\') *p
= '/';
2837 if (!redirect
|| (!strstr( unix_name
, "/windows/") && strncmp( unix_name
, "windows/", 8 )))
2839 if (!stat( unix_name
, &st
))
2841 /* creation fails with STATUS_ACCESS_DENIED for the root of the drive */
2842 if (disposition
== FILE_CREATE
)
2843 return name_len
? STATUS_OBJECT_NAME_COLLISION
: STATUS_ACCESS_DENIED
;
2844 return STATUS_SUCCESS
;
2849 if (!name_len
) /* empty name -> drive root doesn't exist */
2850 return STATUS_OBJECT_PATH_NOT_FOUND
;
2851 if (check_case
&& !redirect
&& (disposition
== FILE_OPEN
|| disposition
== FILE_OVERWRITE
))
2852 return STATUS_OBJECT_NAME_NOT_FOUND
;
2854 /* now do it component by component */
2858 const WCHAR
*end
, *next
;
2859 BOOLEAN is_win_dir
= FALSE
;
2862 while (end
< name
+ name_len
&& !IS_SEPARATOR(*end
)) end
++;
2864 while (next
< name
+ name_len
&& IS_SEPARATOR(*next
)) next
++;
2865 name_len
-= next
- name
;
2867 /* grow the buffer if needed */
2869 if (unix_len
- pos
< MAX_DIR_ENTRY_LEN
+ 2)
2872 unix_len
+= 2 * MAX_DIR_ENTRY_LEN
;
2873 if (!(new_name
= RtlReAllocateHeap( GetProcessHeap(), 0, unix_name
, unix_len
)))
2874 return STATUS_NO_MEMORY
;
2875 unix_name
= *buffer
= new_name
;
2878 status
= find_file_in_dir( unix_name
, pos
, name
, end
- name
,
2879 check_case
, redirect
? &is_win_dir
: NULL
);
2881 /* if this is the last element, not finding it is not necessarily fatal */
2884 if (status
== STATUS_OBJECT_PATH_NOT_FOUND
)
2886 status
= STATUS_OBJECT_NAME_NOT_FOUND
;
2887 if (disposition
!= FILE_OPEN
&& disposition
!= FILE_OVERWRITE
)
2889 ret
= ntdll_wcstoumbs( 0, name
, end
- name
, unix_name
+ pos
+ 1,
2890 MAX_DIR_ENTRY_LEN
, NULL
, &used_default
);
2891 if (ret
> 0 && !used_default
)
2893 unix_name
[pos
] = '/';
2894 unix_name
[pos
+ 1 + ret
] = 0;
2895 status
= STATUS_NO_SUCH_FILE
;
2900 else if (status
== STATUS_SUCCESS
&& disposition
== FILE_CREATE
)
2902 status
= STATUS_OBJECT_NAME_COLLISION
;
2906 if (status
!= STATUS_SUCCESS
) break;
2908 pos
+= strlen( unix_name
+ pos
);
2911 if (is_win_dir
&& (len
= get_redirect_path( unix_name
, pos
, name
, name_len
, check_case
)))
2915 pos
+= strlen( unix_name
+ pos
);
2916 TRACE( "redirecting -> %s + %s\n", debugstr_a(unix_name
), debugstr_w(name
) );
2924 /******************************************************************************
2925 * nt_to_unix_file_name_attr
2927 NTSTATUS
nt_to_unix_file_name_attr( const OBJECT_ATTRIBUTES
*attr
, ANSI_STRING
*unix_name_ret
,
2930 static const WCHAR invalid_charsW
[] = { INVALID_NT_CHARS
, 0 };
2931 enum server_fd_type type
;
2932 int old_cwd
, root_fd
, needs_close
;
2933 const WCHAR
*name
, *p
;
2935 int name_len
, unix_len
;
2937 BOOLEAN check_case
= !(attr
->Attributes
& OBJ_CASE_INSENSITIVE
);
2939 if (!attr
->RootDirectory
) /* without root dir fall back to normal lookup */
2940 return wine_nt_to_unix_file_name( attr
->ObjectName
, unix_name_ret
, disposition
, check_case
);
2942 name
= attr
->ObjectName
->Buffer
;
2943 name_len
= attr
->ObjectName
->Length
/ sizeof(WCHAR
);
2945 if (name_len
&& IS_SEPARATOR(name
[0])) return STATUS_INVALID_PARAMETER
;
2947 /* check for invalid characters */
2948 for (p
= name
; p
< name
+ name_len
; p
++)
2949 if (*p
< 32 || strchrW( invalid_charsW
, *p
)) return STATUS_OBJECT_NAME_INVALID
;
2951 unix_len
= ntdll_wcstoumbs( 0, name
, name_len
, NULL
, 0, NULL
, NULL
);
2952 unix_len
+= MAX_DIR_ENTRY_LEN
+ 3;
2953 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, unix_len
)))
2954 return STATUS_NO_MEMORY
;
2957 if (!(status
= server_get_unix_fd( attr
->RootDirectory
, 0, &root_fd
, &needs_close
, &type
, NULL
)))
2959 if (type
!= FD_TYPE_DIR
)
2961 if (needs_close
) close( root_fd
);
2962 status
= STATUS_BAD_DEVICE_TYPE
;
2966 RtlEnterCriticalSection( &dir_section
);
2967 if ((old_cwd
= open( ".", O_RDONLY
)) != -1 && fchdir( root_fd
) != -1)
2969 status
= lookup_unix_name( name
, name_len
, &unix_name
, unix_len
, 1,
2970 disposition
, check_case
);
2971 if (fchdir( old_cwd
) == -1) chdir( "/" );
2973 else status
= FILE_GetNtStatus();
2974 RtlLeaveCriticalSection( &dir_section
);
2975 if (old_cwd
!= -1) close( old_cwd
);
2976 if (needs_close
) close( root_fd
);
2979 else if (status
== STATUS_OBJECT_TYPE_MISMATCH
) status
= STATUS_BAD_DEVICE_TYPE
;
2981 if (status
== STATUS_SUCCESS
|| status
== STATUS_NO_SUCH_FILE
)
2983 TRACE( "%s -> %s\n", debugstr_us(attr
->ObjectName
), debugstr_a(unix_name
) );
2984 unix_name_ret
->Buffer
= unix_name
;
2985 unix_name_ret
->Length
= strlen(unix_name
);
2986 unix_name_ret
->MaximumLength
= unix_len
;
2990 TRACE( "%s not found in %s\n", debugstr_w(name
), unix_name
);
2991 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2997 /******************************************************************************
2998 * wine_nt_to_unix_file_name (NTDLL.@) Not a Windows API
3000 * Convert a file name from NT namespace to Unix namespace.
3002 * If disposition is not FILE_OPEN or FILE_OVERWRITE, the last path
3003 * element doesn't have to exist; in that case STATUS_NO_SUCH_FILE is
3004 * returned, but the unix name is still filled in properly.
3006 NTSTATUS CDECL
wine_nt_to_unix_file_name( const UNICODE_STRING
*nameW
, ANSI_STRING
*unix_name_ret
,
3007 UINT disposition
, BOOLEAN check_case
)
3009 static const WCHAR unixW
[] = {'u','n','i','x'};
3010 static const WCHAR invalid_charsW
[] = { INVALID_NT_CHARS
, 0 };
3012 NTSTATUS status
= STATUS_SUCCESS
;
3013 const char *config_dir
= wine_get_config_dir();
3014 const WCHAR
*name
, *p
;
3017 int pos
, ret
, name_len
, unix_len
, prefix_len
, used_default
;
3018 WCHAR prefix
[MAX_DIR_ENTRY_LEN
];
3019 BOOLEAN is_unix
= FALSE
;
3021 name
= nameW
->Buffer
;
3022 name_len
= nameW
->Length
/ sizeof(WCHAR
);
3024 if (!name_len
|| !IS_SEPARATOR(name
[0])) return STATUS_OBJECT_PATH_SYNTAX_BAD
;
3026 if (!(pos
= get_dos_prefix_len( nameW
)))
3027 return STATUS_BAD_DEVICE_TYPE
; /* no DOS prefix, assume NT native name */
3032 /* check for sub-directory */
3033 for (pos
= 0; pos
< name_len
; pos
++)
3035 if (IS_SEPARATOR(name
[pos
])) break;
3036 if (name
[pos
] < 32 || strchrW( invalid_charsW
, name
[pos
] ))
3037 return STATUS_OBJECT_NAME_INVALID
;
3039 if (pos
> MAX_DIR_ENTRY_LEN
)
3040 return STATUS_OBJECT_NAME_INVALID
;
3042 if (pos
== name_len
) /* no subdir, plain DOS device */
3043 return get_dos_device( name
, name_len
, unix_name_ret
);
3045 for (prefix_len
= 0; prefix_len
< pos
; prefix_len
++)
3046 prefix
[prefix_len
] = tolowerW(name
[prefix_len
]);
3049 name_len
-= prefix_len
;
3051 /* check for invalid characters (all chars except 0 are valid for unix) */
3052 is_unix
= (prefix_len
== 4 && !memcmp( prefix
, unixW
, sizeof(unixW
) ));
3055 for (p
= name
; p
< name
+ name_len
; p
++)
3056 if (!*p
) return STATUS_OBJECT_NAME_INVALID
;
3061 for (p
= name
; p
< name
+ name_len
; p
++)
3062 if (*p
< 32 || strchrW( invalid_charsW
, *p
)) return STATUS_OBJECT_NAME_INVALID
;
3065 unix_len
= ntdll_wcstoumbs( 0, prefix
, prefix_len
, NULL
, 0, NULL
, NULL
);
3066 unix_len
+= ntdll_wcstoumbs( 0, name
, name_len
, NULL
, 0, NULL
, NULL
);
3067 unix_len
+= MAX_DIR_ENTRY_LEN
+ 3;
3068 unix_len
+= strlen(config_dir
) + sizeof("/dosdevices/");
3069 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, unix_len
)))
3070 return STATUS_NO_MEMORY
;
3071 strcpy( unix_name
, config_dir
);
3072 strcat( unix_name
, "/dosdevices/" );
3073 pos
= strlen(unix_name
);
3075 ret
= ntdll_wcstoumbs( 0, prefix
, prefix_len
, unix_name
+ pos
, unix_len
- pos
- 1,
3076 NULL
, &used_default
);
3077 if (!ret
|| used_default
)
3079 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
3080 return STATUS_OBJECT_NAME_INVALID
;
3084 /* check if prefix exists (except for DOS drives to avoid extra stat calls) */
3086 if (prefix_len
!= 2 || prefix
[1] != ':')
3089 if (lstat( unix_name
, &st
) == -1 && errno
== ENOENT
)
3093 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
3094 return STATUS_BAD_DEVICE_TYPE
;
3096 pos
= 0; /* fall back to unix root */
3100 status
= lookup_unix_name( name
, name_len
, &unix_name
, unix_len
, pos
, disposition
, check_case
);
3101 if (status
== STATUS_SUCCESS
|| status
== STATUS_NO_SUCH_FILE
)
3103 TRACE( "%s -> %s\n", debugstr_us(nameW
), debugstr_a(unix_name
) );
3104 unix_name_ret
->Buffer
= unix_name
;
3105 unix_name_ret
->Length
= strlen(unix_name
);
3106 unix_name_ret
->MaximumLength
= unix_len
;
3110 TRACE( "%s not found in %s\n", debugstr_w(name
), unix_name
);
3111 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
3117 /******************************************************************
3118 * RtlWow64EnableFsRedirection (NTDLL.@)
3120 NTSTATUS WINAPI
RtlWow64EnableFsRedirection( BOOLEAN enable
)
3122 if (!is_wow64
) return STATUS_NOT_IMPLEMENTED
;
3123 ntdll_get_thread_data()->wow64_redir
= enable
;
3124 return STATUS_SUCCESS
;
3128 /******************************************************************
3129 * RtlWow64EnableFsRedirectionEx (NTDLL.@)
3131 NTSTATUS WINAPI
RtlWow64EnableFsRedirectionEx( ULONG disable
, ULONG
*old_value
)
3133 if (!is_wow64
) return STATUS_NOT_IMPLEMENTED
;
3134 if (((ULONG_PTR
)old_value
>> 16) == 0) return STATUS_ACCESS_VIOLATION
;
3136 *old_value
= !ntdll_get_thread_data()->wow64_redir
;
3137 ntdll_get_thread_data()->wow64_redir
= !disable
;
3138 return STATUS_SUCCESS
;
3142 /******************************************************************
3143 * RtlDoesFileExists_U (NTDLL.@)
3145 BOOLEAN WINAPI
RtlDoesFileExists_U(LPCWSTR file_name
)
3147 UNICODE_STRING nt_name
;
3148 FILE_BASIC_INFORMATION basic_info
;
3149 OBJECT_ATTRIBUTES attr
;
3152 if (!RtlDosPathNameToNtPathName_U( file_name
, &nt_name
, NULL
, NULL
)) return FALSE
;
3154 attr
.Length
= sizeof(attr
);
3155 attr
.RootDirectory
= 0;
3156 attr
.ObjectName
= &nt_name
;
3157 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
3158 attr
.SecurityDescriptor
= NULL
;
3159 attr
.SecurityQualityOfService
= NULL
;
3161 ret
= NtQueryAttributesFile(&attr
, &basic_info
) == STATUS_SUCCESS
;
3163 RtlFreeUnicodeString( &nt_name
);
3168 /***********************************************************************
3169 * DIR_unmount_device
3171 * Unmount the specified device.
3173 NTSTATUS
DIR_unmount_device( HANDLE handle
)
3176 int unix_fd
, needs_close
;
3178 if (!(status
= server_get_unix_fd( handle
, 0, &unix_fd
, &needs_close
, NULL
, NULL
)))
3181 char *mount_point
= NULL
;
3183 if (fstat( unix_fd
, &st
) == -1 || !is_valid_mounted_device( &st
))
3184 status
= STATUS_INVALID_PARAMETER
;
3187 if ((mount_point
= get_device_mount_point( st
.st_rdev
)))
3190 static const char umount
[] = "diskutil unmount >/dev/null 2>&1 ";
3192 static const char umount
[] = "umount >/dev/null 2>&1 ";
3194 char *cmd
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(mount_point
)+sizeof(umount
));
3197 strcpy( cmd
, umount
);
3198 strcat( cmd
, mount_point
);
3200 RtlFreeHeap( GetProcessHeap(), 0, cmd
);
3202 /* umount will fail to release the loop device since we still have
3203 a handle to it, so we release it here */
3204 if (major(st
.st_rdev
) == LOOP_MAJOR
) ioctl( unix_fd
, 0x4c01 /*LOOP_CLR_FD*/, 0 );
3207 RtlFreeHeap( GetProcessHeap(), 0, mount_point
);
3210 if (needs_close
) close( unix_fd
);
3216 /******************************************************************************
3219 * Retrieve the Unix name of the current directory; helper for wine_unix_to_nt_file_name.
3220 * Returned value must be freed by caller.
3222 NTSTATUS
DIR_get_unix_cwd( char **cwd
)
3224 int old_cwd
, unix_fd
, needs_close
;
3229 RtlAcquirePebLock();
3231 if (NtCurrentTeb()->Tib
.SubSystemTib
) /* FIXME: hack */
3232 curdir
= &((WIN16_SUBSYSTEM_TIB
*)NtCurrentTeb()->Tib
.SubSystemTib
)->curdir
;
3234 curdir
= &NtCurrentTeb()->Peb
->ProcessParameters
->CurrentDirectory
;
3236 if (!(handle
= curdir
->Handle
))
3238 UNICODE_STRING dirW
;
3239 OBJECT_ATTRIBUTES attr
;
3242 if (!RtlDosPathNameToNtPathName_U( curdir
->DosPath
.Buffer
, &dirW
, NULL
, NULL
))
3244 status
= STATUS_OBJECT_NAME_INVALID
;
3247 attr
.Length
= sizeof(attr
);
3248 attr
.RootDirectory
= 0;
3249 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
3250 attr
.ObjectName
= &dirW
;
3251 attr
.SecurityDescriptor
= NULL
;
3252 attr
.SecurityQualityOfService
= NULL
;
3254 status
= NtOpenFile( &handle
, 0, &attr
, &io
, 0,
3255 FILE_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
);
3256 RtlFreeUnicodeString( &dirW
);
3257 if (status
!= STATUS_SUCCESS
) goto done
;
3260 if ((status
= server_get_unix_fd( handle
, 0, &unix_fd
, &needs_close
, NULL
, NULL
)) == STATUS_SUCCESS
)
3262 RtlEnterCriticalSection( &dir_section
);
3264 if ((old_cwd
= open(".", O_RDONLY
)) != -1 && fchdir( unix_fd
) != -1)
3266 unsigned int size
= 512;
3270 if (!(*cwd
= RtlAllocateHeap( GetProcessHeap(), 0, size
)))
3272 status
= STATUS_NO_MEMORY
;
3275 if (getcwd( *cwd
, size
)) break;
3276 RtlFreeHeap( GetProcessHeap(), 0, *cwd
);
3277 if (errno
!= ERANGE
)
3279 status
= STATUS_OBJECT_PATH_INVALID
;
3284 if (fchdir( old_cwd
) == -1) chdir( "/" );
3286 else status
= FILE_GetNtStatus();
3288 RtlLeaveCriticalSection( &dir_section
);
3289 if (old_cwd
!= -1) close( old_cwd
);
3290 if (needs_close
) close( unix_fd
);
3292 if (!curdir
->Handle
) NtClose( handle
);
3295 RtlReleasePebLock();
3299 struct read_changes_info
3305 PIO_APC_ROUTINE apc
;
3310 /* callback for ioctl user APC */
3311 static void WINAPI
read_changes_user_apc( void *arg
, IO_STATUS_BLOCK
*io
, ULONG reserved
)
3313 struct read_changes_info
*info
= arg
;
3314 if (info
->apc
) info
->apc( info
->apc_arg
, io
, reserved
);
3315 RtlFreeHeap( GetProcessHeap(), 0, info
);
3318 static NTSTATUS
read_changes_apc( void *user
, PIO_STATUS_BLOCK iosb
, NTSTATUS status
, void **apc
)
3320 struct read_changes_info
*info
= user
;
3324 SERVER_START_REQ( read_change
)
3326 req
->handle
= wine_server_obj_handle( info
->FileHandle
);
3327 wine_server_set_reply( req
, info
->data
, info
->data_size
);
3328 ret
= wine_server_call( req
);
3329 size
= wine_server_reply_size( reply
);
3333 if (ret
== STATUS_SUCCESS
&& info
->Buffer
)
3335 PFILE_NOTIFY_INFORMATION pfni
= info
->Buffer
;
3336 int i
, left
= info
->BufferSize
;
3337 DWORD
*last_entry_offset
= NULL
;
3338 struct filesystem_event
*event
= (struct filesystem_event
*)info
->data
;
3340 while (size
&& left
>= sizeof(*pfni
))
3342 /* convert to an NT style path */
3343 for (i
=0; i
<event
->len
; i
++)
3344 if (event
->name
[i
] == '/')
3345 event
->name
[i
] = '\\';
3347 pfni
->Action
= event
->action
;
3348 pfni
->FileNameLength
= ntdll_umbstowcs( 0, event
->name
, event
->len
, pfni
->FileName
,
3349 (left
- offsetof(FILE_NOTIFY_INFORMATION
, FileName
)) / sizeof(WCHAR
));
3350 last_entry_offset
= &pfni
->NextEntryOffset
;
3352 if(pfni
->FileNameLength
== -1 || pfni
->FileNameLength
== -2)
3355 i
= offsetof(FILE_NOTIFY_INFORMATION
, FileName
[pfni
->FileNameLength
]);
3356 pfni
->FileNameLength
*= sizeof(WCHAR
);
3357 pfni
->NextEntryOffset
= i
;
3358 pfni
= (FILE_NOTIFY_INFORMATION
*)((char*)pfni
+ i
);
3361 i
= (offsetof(struct filesystem_event
, name
[event
->len
])
3362 + sizeof(int)-1) / sizeof(int) * sizeof(int);
3363 event
= (struct filesystem_event
*)((char*)event
+ i
);
3369 ret
= STATUS_NOTIFY_ENUM_DIR
;
3374 *last_entry_offset
= 0;
3375 size
= info
->BufferSize
- left
;
3380 ret
= STATUS_NOTIFY_ENUM_DIR
;
3384 iosb
->u
.Status
= ret
;
3385 iosb
->Information
= size
;
3386 *apc
= read_changes_user_apc
;
3390 #define FILE_NOTIFY_ALL ( \
3391 FILE_NOTIFY_CHANGE_FILE_NAME | \
3392 FILE_NOTIFY_CHANGE_DIR_NAME | \
3393 FILE_NOTIFY_CHANGE_ATTRIBUTES | \
3394 FILE_NOTIFY_CHANGE_SIZE | \
3395 FILE_NOTIFY_CHANGE_LAST_WRITE | \
3396 FILE_NOTIFY_CHANGE_LAST_ACCESS | \
3397 FILE_NOTIFY_CHANGE_CREATION | \
3398 FILE_NOTIFY_CHANGE_SECURITY )
3400 /******************************************************************************
3401 * NtNotifyChangeDirectoryFile [NTDLL.@]
3404 NtNotifyChangeDirectoryFile( HANDLE FileHandle
, HANDLE Event
,
3405 PIO_APC_ROUTINE ApcRoutine
, PVOID ApcContext
,
3406 PIO_STATUS_BLOCK IoStatusBlock
, PVOID Buffer
,
3407 ULONG BufferSize
, ULONG CompletionFilter
, BOOLEAN WatchTree
)
3409 struct read_changes_info
*info
;
3411 ULONG size
= max( 4096, BufferSize
);
3412 ULONG_PTR cvalue
= ApcRoutine
? 0 : (ULONG_PTR
)ApcContext
;
3414 TRACE("%p %p %p %p %p %p %u %u %d\n",
3415 FileHandle
, Event
, ApcRoutine
, ApcContext
, IoStatusBlock
,
3416 Buffer
, BufferSize
, CompletionFilter
, WatchTree
);
3419 return STATUS_ACCESS_VIOLATION
;
3421 if (CompletionFilter
== 0 || (CompletionFilter
& ~FILE_NOTIFY_ALL
))
3422 return STATUS_INVALID_PARAMETER
;
3424 info
= RtlAllocateHeap( GetProcessHeap(), 0, offsetof( struct read_changes_info
, data
[size
] ));
3426 return STATUS_NO_MEMORY
;
3428 info
->FileHandle
= FileHandle
;
3429 info
->Buffer
= Buffer
;
3430 info
->BufferSize
= BufferSize
;
3431 info
->apc
= ApcRoutine
;
3432 info
->apc_arg
= ApcContext
;
3433 info
->data_size
= size
;
3435 SERVER_START_REQ( read_directory_changes
)
3437 req
->filter
= CompletionFilter
;
3438 req
->want_data
= (Buffer
!= NULL
);
3439 req
->subtree
= WatchTree
;
3440 req
->async
.handle
= wine_server_obj_handle( FileHandle
);
3441 req
->async
.callback
= wine_server_client_ptr( read_changes_apc
);
3442 req
->async
.iosb
= wine_server_client_ptr( IoStatusBlock
);
3443 req
->async
.arg
= wine_server_client_ptr( info
);
3444 req
->async
.event
= wine_server_obj_handle( Event
);
3445 req
->async
.cvalue
= cvalue
;
3446 status
= wine_server_call( req
);
3450 if (status
!= STATUS_PENDING
)
3451 RtlFreeHeap( GetProcessHeap(), 0, info
);