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
51 # include <sys/mkdev.h>
52 #elif defined(MAJOR_IN_SYSMACROS)
53 # include <sys/sysmacros.h>
55 #ifdef HAVE_SYS_VNODE_H
57 # include <stdint.h> /* needed for kfreebsd */
59 /* Work around a conflict with Solaris' system list defined in sys/list.h. */
61 #define list_next SYSLIST_NEXT
62 #define list_prev SYSLIST_PREV
63 #define list_head SYSLIST_HEAD
64 #define list_tail SYSLIST_TAIL
65 #define list_move_tail SYSLIST_MOVE_TAIL
66 #define list_remove SYSLIST_REMOVE
67 #include <sys/vnode.h>
76 #ifdef HAVE_SYS_IOCTL_H
77 #include <sys/ioctl.h>
79 #ifdef HAVE_LINUX_IOCTL_H
80 #include <linux/ioctl.h>
82 #ifdef HAVE_LINUX_MAJOR_H
83 # include <linux/major.h>
85 #ifdef HAVE_SYS_PARAM_H
86 #include <sys/param.h>
88 #ifdef HAVE_SYS_MOUNT_H
89 #include <sys/mount.h>
91 #ifdef HAVE_SYS_STATFS_H
92 #include <sys/statfs.h>
100 #define WIN32_NO_STATUS
101 #define NONAMELESSUNION
104 #include "winternl.h"
106 #include "ntdll_misc.h"
107 #include "wine/unicode.h"
108 #include "wine/server.h"
109 #include "wine/list.h"
110 #include "wine/library.h"
111 #include "wine/debug.h"
112 #include "wine/exception.h"
114 WINE_DEFAULT_DEBUG_CHANNEL(file
);
116 /* just in case... */
117 #undef VFAT_IOCTL_READDIR_BOTH
121 /* We want the real kernel dirent structure, not the libc one */
126 unsigned short d_reclen
;
130 /* Define the VFAT ioctl to get both short and long file names */
131 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, KERNEL_DIRENT [2] )
134 # define O_DIRECTORY 0200000 /* must be directory */
139 #define IS_OPTION_TRUE(ch) ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
140 #define IS_SEPARATOR(ch) ((ch) == '\\' || (ch) == '/')
142 #define INVALID_NT_CHARS '*','?','<','>','|','"'
143 #define INVALID_DOS_CHARS INVALID_NT_CHARS,'+','=',',',';','[',']',' ','\345'
145 #define MAX_DIR_ENTRY_LEN 255 /* max length of a directory entry in chars */
147 #define MAX_IGNORED_FILES 4
155 static struct file_identity ignored_files
[MAX_IGNORED_FILES
];
156 static unsigned int ignored_files_count
;
158 union file_directory_info
161 FILE_DIRECTORY_INFORMATION dir
;
162 FILE_BOTH_DIRECTORY_INFORMATION both
;
163 FILE_FULL_DIRECTORY_INFORMATION full
;
164 FILE_ID_BOTH_DIRECTORY_INFORMATION id_both
;
165 FILE_ID_FULL_DIRECTORY_INFORMATION id_full
;
166 FILE_ID_GLOBAL_TX_DIR_INFORMATION id_tx
;
167 FILE_NAMES_INFORMATION names
;
170 struct dir_data_buffer
172 struct dir_data_buffer
*next
; /* next buffer in the list */
173 unsigned int size
; /* total size of the buffer */
174 unsigned int pos
; /* current position in the buffer */
178 struct dir_data_names
180 const WCHAR
*long_name
; /* long file name in Unicode */
181 const WCHAR
*short_name
; /* short file name in Unicode */
182 const char *unix_name
; /* Unix file name in host encoding */
187 unsigned int size
; /* size of the names array */
188 unsigned int count
; /* count of used entries in the names array */
189 unsigned int pos
; /* current reading position in the names array */
190 struct file_identity id
; /* directory file identity */
191 struct dir_data_names
*names
; /* directory file names */
192 struct dir_data_buffer
*buffer
; /* head of data buffers list */
195 static const unsigned int dir_data_buffer_initial_size
= 4096;
196 static const unsigned int dir_data_cache_initial_size
= 256;
197 static const unsigned int dir_data_names_initial_size
= 64;
199 static struct dir_data
**dir_data_cache
;
200 static unsigned int dir_data_cache_size
;
202 static BOOL show_dot_files
;
203 static RTL_RUN_ONCE init_once
= RTL_RUN_ONCE_INIT
;
205 /* at some point we may want to allow Winelib apps to set this */
206 static const BOOL is_case_sensitive
= FALSE
;
208 static struct file_identity windir
;
210 static RTL_CRITICAL_SECTION dir_section
;
211 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
214 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
215 0, 0, { (DWORD_PTR
)(__FILE__
": dir_section") }
217 static RTL_CRITICAL_SECTION dir_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
220 /* check if a given Unicode char is OK in a DOS short name */
221 static inline BOOL
is_invalid_dos_char( WCHAR ch
)
223 static const WCHAR invalid_chars
[] = { INVALID_DOS_CHARS
,'~','.',0 };
224 if (ch
> 0x7f) return TRUE
;
225 return strchrW( invalid_chars
, ch
) != NULL
;
228 /* check if the device can be a mounted volume */
229 static inline BOOL
is_valid_mounted_device( const struct stat
*st
)
231 #if defined(linux) || defined(__sun__)
232 return S_ISBLK( st
->st_mode
);
234 /* disks are char devices on *BSD */
235 return S_ISCHR( st
->st_mode
);
239 static inline void ignore_file( const char *name
)
242 assert( ignored_files_count
< MAX_IGNORED_FILES
);
243 if (!stat( name
, &st
))
245 ignored_files
[ignored_files_count
].dev
= st
.st_dev
;
246 ignored_files
[ignored_files_count
].ino
= st
.st_ino
;
247 ignored_files_count
++;
251 static inline BOOL
is_same_file( const struct file_identity
*file
, const struct stat
*st
)
253 return st
->st_dev
== file
->dev
&& st
->st_ino
== file
->ino
;
256 static inline BOOL
is_ignored_file( const struct stat
*st
)
260 for (i
= 0; i
< ignored_files_count
; i
++)
261 if (is_same_file( &ignored_files
[i
], st
)) return TRUE
;
265 static inline unsigned int dir_info_align( unsigned int len
)
267 return (len
+ 7) & ~7;
270 static inline unsigned int dir_info_size( FILE_INFORMATION_CLASS
class, unsigned int len
)
274 case FileDirectoryInformation
:
275 return offsetof( FILE_DIRECTORY_INFORMATION
, FileName
[len
] );
276 case FileBothDirectoryInformation
:
277 return offsetof( FILE_BOTH_DIRECTORY_INFORMATION
, FileName
[len
] );
278 case FileFullDirectoryInformation
:
279 return offsetof( FILE_FULL_DIRECTORY_INFORMATION
, FileName
[len
] );
280 case FileIdBothDirectoryInformation
:
281 return offsetof( FILE_ID_BOTH_DIRECTORY_INFORMATION
, FileName
[len
] );
282 case FileIdFullDirectoryInformation
:
283 return offsetof( FILE_ID_FULL_DIRECTORY_INFORMATION
, FileName
[len
] );
284 case FileIdGlobalTxDirectoryInformation
:
285 return offsetof( FILE_ID_GLOBAL_TX_DIR_INFORMATION
, FileName
[len
] );
286 case FileNamesInformation
:
287 return offsetof( FILE_NAMES_INFORMATION
, FileName
[len
] );
294 static inline BOOL
has_wildcard( const UNICODE_STRING
*mask
)
297 memchrW( mask
->Buffer
, '*', mask
->Length
/ sizeof(WCHAR
) ) ||
298 memchrW( mask
->Buffer
, '?', mask
->Length
/ sizeof(WCHAR
) ));
301 /* get space from the current directory data buffer, allocating a new one if necessary */
302 static void *get_dir_data_space( struct dir_data
*data
, unsigned int size
)
304 struct dir_data_buffer
*buffer
= data
->buffer
;
307 if (!buffer
|| size
> buffer
->size
- buffer
->pos
)
309 unsigned int new_size
= buffer
? buffer
->size
* 2 : dir_data_buffer_initial_size
;
310 if (new_size
< size
) new_size
= size
;
311 if (!(buffer
= RtlAllocateHeap( GetProcessHeap(), 0,
312 offsetof( struct dir_data_buffer
, data
[new_size
] ) ))) return NULL
;
314 buffer
->size
= new_size
;
315 buffer
->next
= data
->buffer
;
316 data
->buffer
= buffer
;
318 ret
= buffer
->data
+ buffer
->pos
;
323 /* add a string to the directory data buffer */
324 static const char *add_dir_data_nameA( struct dir_data
*data
, const char *name
)
326 /* keep buffer data WCHAR-aligned */
327 char *ptr
= get_dir_data_space( data
, (strlen( name
) + sizeof(WCHAR
)) & ~(sizeof(WCHAR
) - 1) );
328 if (ptr
) strcpy( ptr
, name
);
332 /* add a Unicode string to the directory data buffer */
333 static const WCHAR
*add_dir_data_nameW( struct dir_data
*data
, const WCHAR
*name
)
335 WCHAR
*ptr
= get_dir_data_space( data
, (strlenW( name
) + 1) * sizeof(WCHAR
) );
336 if (ptr
) strcpyW( ptr
, name
);
340 /* add an entry to the directory names array */
341 static BOOL
add_dir_data_names( struct dir_data
*data
, const WCHAR
*long_name
,
342 const WCHAR
*short_name
, const char *unix_name
)
344 static const WCHAR empty
[1];
345 struct dir_data_names
*names
= data
->names
;
347 if (data
->count
>= data
->size
)
349 unsigned int new_size
= max( data
->size
* 2, dir_data_names_initial_size
);
351 if (names
) names
= RtlReAllocateHeap( GetProcessHeap(), 0, names
, new_size
* sizeof(*names
) );
352 else names
= RtlAllocateHeap( GetProcessHeap(), 0, new_size
* sizeof(*names
) );
353 if (!names
) return FALSE
;
354 data
->size
= new_size
;
360 if (!(names
[data
->count
].short_name
= add_dir_data_nameW( data
, short_name
))) return FALSE
;
362 else names
[data
->count
].short_name
= empty
;
364 if (!(names
[data
->count
].long_name
= add_dir_data_nameW( data
, long_name
))) return FALSE
;
365 if (!(names
[data
->count
].unix_name
= add_dir_data_nameA( data
, unix_name
))) return FALSE
;
370 /* free the complete directory data structure */
371 static void free_dir_data( struct dir_data
*data
)
373 struct dir_data_buffer
*buffer
, *next
;
377 for (buffer
= data
->buffer
; buffer
; buffer
= next
)
380 RtlFreeHeap( GetProcessHeap(), 0, buffer
);
382 RtlFreeHeap( GetProcessHeap(), 0, data
->names
);
383 RtlFreeHeap( GetProcessHeap(), 0, data
);
387 /* support for a directory queue for filesystem searches */
395 static struct list dir_queue
= LIST_INIT( dir_queue
);
397 static NTSTATUS
add_dir_to_queue( const char *name
)
399 int len
= strlen( name
) + 1;
400 struct dir_name
*dir
= RtlAllocateHeap( GetProcessHeap(), 0,
401 FIELD_OFFSET( struct dir_name
, name
[len
] ));
402 if (!dir
) return STATUS_NO_MEMORY
;
403 strcpy( dir
->name
, name
);
404 list_add_tail( &dir_queue
, &dir
->entry
);
405 return STATUS_SUCCESS
;
408 static NTSTATUS
next_dir_in_queue( char *name
)
410 struct list
*head
= list_head( &dir_queue
);
413 struct dir_name
*dir
= LIST_ENTRY( head
, struct dir_name
, entry
);
414 strcpy( name
, dir
->name
);
415 list_remove( &dir
->entry
);
416 RtlFreeHeap( GetProcessHeap(), 0, dir
);
417 return STATUS_SUCCESS
;
419 return STATUS_OBJECT_NAME_NOT_FOUND
;
422 static void flush_dir_queue(void)
426 while ((head
= list_head( &dir_queue
)))
428 struct dir_name
*dir
= LIST_ENTRY( head
, struct dir_name
, entry
);
429 list_remove( &dir
->entry
);
430 RtlFreeHeap( GetProcessHeap(), 0, dir
);
437 static char *unescape_field( char *str
)
441 for (in
= out
= str
; *in
; in
++, out
++)
451 else if (in
[1] == '0' && in
[2] == '4' && in
[3] == '0')
456 else if (in
[1] == '0' && in
[2] == '1' && in
[3] == '1')
461 else if (in
[1] == '0' && in
[2] == '1' && in
[3] == '2')
466 else if (in
[1] == '1' && in
[2] == '3' && in
[3] == '4')
478 static inline char *get_field( char **str
)
482 ret
= strsep( str
, " \t" );
483 if (*str
) *str
+= strspn( *str
, " \t" );
487 /************************************************************************
488 * getmntent_replacement
490 * getmntent replacement for Android.
492 * NB returned static buffer is not thread safe; protect with dir_section.
494 static struct mntent
*getmntent_replacement( FILE *f
)
496 static struct mntent entry
;
497 static char buf
[4096];
502 if (!fgets( buf
, sizeof(buf
), f
)) return NULL
;
503 p
= strchr( buf
, '\n' );
505 else /* Partially unread line, move file ptr to end */
508 while (fgets( tmp
, sizeof(tmp
), f
))
509 if (strchr( tmp
, '\n' )) break;
511 start
= buf
+ strspn( buf
, " \t" );
512 } while (start
[0] == '\0' || start
[0] == '#');
514 p
= get_field( &start
);
515 entry
.mnt_fsname
= p
? unescape_field( p
) : (char *)"";
517 p
= get_field( &start
);
518 entry
.mnt_dir
= p
? unescape_field( p
) : (char *)"";
520 p
= get_field( &start
);
521 entry
.mnt_type
= p
? unescape_field( p
) : (char *)"";
523 p
= get_field( &start
);
524 entry
.mnt_opts
= p
? unescape_field( p
) : (char *)"";
526 p
= get_field( &start
);
527 entry
.mnt_freq
= p
? atoi(p
) : 0;
529 p
= get_field( &start
);
530 entry
.mnt_passno
= p
? atoi(p
) : 0;
534 #define getmntent getmntent_replacement
537 /***********************************************************************
538 * DIR_get_drives_info
540 * Retrieve device/inode number for all the drives. Helper for find_drive_root.
542 unsigned int DIR_get_drives_info( struct drive_info info
[MAX_DOS_DRIVES
] )
544 static struct drive_info cache
[MAX_DOS_DRIVES
];
545 static time_t last_update
;
546 static unsigned int nb_drives
;
548 time_t now
= time(NULL
);
550 RtlEnterCriticalSection( &dir_section
);
551 if (now
!= last_update
)
553 const char *config_dir
= wine_get_config_dir();
558 if ((buffer
= RtlAllocateHeap( GetProcessHeap(), 0,
559 strlen(config_dir
) + sizeof("/dosdevices/a:") )))
561 strcpy( buffer
, config_dir
);
562 strcat( buffer
, "/dosdevices/a:" );
563 p
= buffer
+ strlen(buffer
) - 2;
565 for (i
= nb_drives
= 0; i
< MAX_DOS_DRIVES
; i
++)
568 if (!stat( buffer
, &st
))
570 cache
[i
].dev
= st
.st_dev
;
571 cache
[i
].ino
= st
.st_ino
;
580 RtlFreeHeap( GetProcessHeap(), 0, buffer
);
584 memcpy( info
, cache
, sizeof(cache
) );
586 RtlLeaveCriticalSection( &dir_section
);
591 /***********************************************************************
592 * parse_mount_entries
594 * Parse mount entries looking for a given device. Helper for get_default_drive_device.
598 #include <sys/vfstab.h>
599 static char *parse_vfstab_entries( FILE *f
, dev_t dev
, ino_t ino
)
605 while (! getvfsent( f
, &entry
))
607 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
608 if (!strcmp( entry
.vfs_fstype
, "nfs" ) ||
609 !strcmp( entry
.vfs_fstype
, "smbfs" ) ||
610 !strcmp( entry
.vfs_fstype
, "ncpfs" )) continue;
612 if (stat( entry
.vfs_mountp
, &st
) == -1) continue;
613 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
614 if (!strcmp( entry
.vfs_fstype
, "fd" ))
616 if ((device
= strstr( entry
.vfs_mntopts
, "dev=" )))
618 char *p
= strchr( device
+ 4, ',' );
624 return entry
.vfs_special
;
631 static char *parse_mount_entries( FILE *f
, dev_t dev
, ino_t ino
)
633 struct mntent
*entry
;
637 while ((entry
= getmntent( f
)))
639 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
640 if (!strcmp( entry
->mnt_type
, "nfs" ) ||
641 !strcmp( entry
->mnt_type
, "cifs" ) ||
642 !strcmp( entry
->mnt_type
, "smbfs" ) ||
643 !strcmp( entry
->mnt_type
, "ncpfs" )) continue;
645 if (stat( entry
->mnt_dir
, &st
) == -1) continue;
646 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
647 if (!strcmp( entry
->mnt_type
, "supermount" ))
649 if ((device
= strstr( entry
->mnt_opts
, "dev=" )))
651 char *p
= strchr( device
+ 4, ',' );
656 else if (!stat( entry
->mnt_fsname
, &st
) && S_ISREG(st
.st_mode
))
658 /* if device is a regular file check for a loop mount */
659 if ((device
= strstr( entry
->mnt_opts
, "loop=" )))
661 char *p
= strchr( device
+ 5, ',' );
667 return entry
->mnt_fsname
;
673 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
675 static char *parse_mount_entries( FILE *f
, dev_t dev
, ino_t ino
)
680 while ((entry
= getfsent()))
682 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
683 if (!strcmp( entry
->fs_vfstype
, "nfs" ) ||
684 !strcmp( entry
->fs_vfstype
, "smbfs" ) ||
685 !strcmp( entry
->fs_vfstype
, "ncpfs" )) continue;
687 if (stat( entry
->fs_file
, &st
) == -1) continue;
688 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
689 return entry
->fs_spec
;
696 #include <sys/mnttab.h>
697 static char *parse_mount_entries( FILE *f
, dev_t dev
, ino_t ino
)
704 while (( ! getmntent( f
, &entry
) ))
706 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
707 if (!strcmp( entry
.mnt_fstype
, "nfs" ) ||
708 !strcmp( entry
.mnt_fstype
, "smbfs" ) ||
709 !strcmp( entry
.mnt_fstype
, "ncpfs" )) continue;
711 if (stat( entry
.mnt_mountp
, &st
) == -1) continue;
712 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
713 if (!strcmp( entry
.mnt_fstype
, "fd" ))
715 if ((device
= strstr( entry
.mnt_mntopts
, "dev=" )))
717 char *p
= strchr( device
+ 4, ',' );
723 return entry
.mnt_special
;
729 /***********************************************************************
730 * get_default_drive_device
732 * Return the default device to use for a given drive mount point.
734 static char *get_default_drive_device( const char *root
)
744 /* try to open it first to force it to get mounted */
745 if ((fd
= open( root
, O_RDONLY
| O_DIRECTORY
)) != -1)
747 res
= fstat( fd
, &st
);
750 /* now try normal stat just in case */
751 if (res
== -1) res
= stat( root
, &st
);
752 if (res
== -1) return NULL
;
754 RtlEnterCriticalSection( &dir_section
);
757 if ((f
= fopen( "/proc/mounts", "r" )))
759 device
= parse_mount_entries( f
, st
.st_dev
, st
.st_ino
);
763 if ((f
= fopen( "/etc/mtab", "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/fstab", "r" )))
771 device
= parse_mount_entries( f
, st
.st_dev
, st
.st_ino
);
777 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(device
) + 1 );
778 if (ret
) strcpy( ret
, device
);
780 RtlLeaveCriticalSection( &dir_section
);
782 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__ ) || defined(__DragonFly__)
787 /* try to open it first to force it to get mounted */
788 if ((fd
= open( root
, O_RDONLY
)) != -1)
790 res
= fstat( fd
, &st
);
793 /* now try normal stat just in case */
794 if (res
== -1) res
= stat( root
, &st
);
795 if (res
== -1) return NULL
;
797 RtlEnterCriticalSection( &dir_section
);
799 /* The FreeBSD parse_mount_entries doesn't require a file argument, so just
800 * pass NULL. Leave the argument in for symmetry.
802 device
= parse_mount_entries( NULL
, st
.st_dev
, st
.st_ino
);
805 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(device
) + 1 );
806 if (ret
) strcpy( ret
, device
);
808 RtlLeaveCriticalSection( &dir_section
);
816 /* try to open it first to force it to get mounted */
817 if ((fd
= open( root
, O_RDONLY
)) != -1)
819 res
= fstat( fd
, &st
);
822 /* now try normal stat just in case */
823 if (res
== -1) res
= stat( root
, &st
);
824 if (res
== -1) return NULL
;
826 RtlEnterCriticalSection( &dir_section
);
828 if ((f
= fopen( "/etc/mnttab", "r" )))
830 device
= parse_mount_entries( f
, st
.st_dev
, st
.st_ino
);
833 /* look through fstab too in case it's not mounted (for instance if it's an audio CD) */
834 if (!device
&& (f
= fopen( "/etc/vfstab", "r" )))
836 device
= parse_vfstab_entries( f
, st
.st_dev
, st
.st_ino
);
841 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(device
) + 1 );
842 if (ret
) strcpy( ret
, device
);
844 RtlLeaveCriticalSection( &dir_section
);
846 #elif defined(__APPLE__)
847 struct statfs
*mntStat
;
853 static const char path_bsd_device
[] = "/dev/disk";
856 res
= stat( root
, &st
);
857 if (res
== -1) return NULL
;
862 RtlEnterCriticalSection( &dir_section
);
864 mntSize
= getmntinfo(&mntStat
, MNT_NOWAIT
);
866 for (i
= 0; i
< mntSize
&& !ret
; i
++)
868 if (stat(mntStat
[i
].f_mntonname
, &st
) == -1) continue;
869 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
871 /* FIXME add support for mounted network drive */
872 if ( strncmp(mntStat
[i
].f_mntfromname
, path_bsd_device
, strlen(path_bsd_device
)) == 0)
874 /* set return value to the corresponding raw BSD node */
875 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(mntStat
[i
].f_mntfromname
) + 2 /* 2 : r and \0 */ );
878 strcpy(ret
, "/dev/r");
879 strcat(ret
, mntStat
[i
].f_mntfromname
+sizeof("/dev/")-1);
883 RtlLeaveCriticalSection( &dir_section
);
886 if (!warned
++) FIXME( "auto detection of DOS devices not supported on this platform\n" );
892 /***********************************************************************
893 * get_device_mount_point
895 * Return the current mount point for a device.
897 static char *get_device_mount_point( dev_t dev
)
904 RtlEnterCriticalSection( &dir_section
);
907 if ((f
= fopen( "/proc/mounts", "r" )))
909 if ((f
= fopen( "/etc/mtab", "r" )))
912 struct mntent
*entry
;
916 while ((entry
= getmntent( f
)))
918 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
919 if (!strcmp( entry
->mnt_type
, "nfs" ) ||
920 !strcmp( entry
->mnt_type
, "cifs" ) ||
921 !strcmp( entry
->mnt_type
, "smbfs" ) ||
922 !strcmp( entry
->mnt_type
, "ncpfs" )) continue;
924 if (!strcmp( entry
->mnt_type
, "supermount" ))
926 if ((device
= strstr( entry
->mnt_opts
, "dev=" )))
929 if ((p
= strchr( device
, ',' ))) *p
= 0;
932 else if (!stat( entry
->mnt_fsname
, &st
) && S_ISREG(st
.st_mode
))
934 /* if device is a regular file check for a loop mount */
935 if ((device
= strstr( entry
->mnt_opts
, "loop=" )))
938 if ((p
= strchr( device
, ',' ))) *p
= 0;
941 else device
= entry
->mnt_fsname
;
943 if (device
&& !stat( device
, &st
) && S_ISBLK(st
.st_mode
) && st
.st_rdev
== dev
)
945 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(entry
->mnt_dir
) + 1 );
946 if (ret
) strcpy( ret
, entry
->mnt_dir
);
952 RtlLeaveCriticalSection( &dir_section
);
953 #elif defined(__APPLE__)
954 struct statfs
*entry
;
958 RtlEnterCriticalSection( &dir_section
);
960 size
= getmntinfo( &entry
, MNT_NOWAIT
);
961 for (i
= 0; i
< size
; i
++)
963 if (stat( entry
[i
].f_mntfromname
, &st
) == -1) continue;
964 if (S_ISBLK(st
.st_mode
) && st
.st_rdev
== dev
)
966 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(entry
[i
].f_mntonname
) + 1 );
967 if (ret
) strcpy( ret
, entry
[i
].f_mntonname
);
971 RtlLeaveCriticalSection( &dir_section
);
974 if (!warned
++) FIXME( "unmounting devices not supported on this platform\n" );
980 #if defined(HAVE_GETATTRLIST) && defined(ATTR_VOL_CAPABILITIES) && \
981 defined(VOL_CAPABILITIES_FORMAT) && defined(VOL_CAP_FMT_CASE_SENSITIVE)
994 BOOLEAN case_sensitive
;
1000 vol_capabilities_attr_t caps
;
1003 /***********************************************************************
1006 * Checks if the specified file system is in the cache.
1008 static struct fs_cache
*look_up_fs_cache( dev_t dev
)
1011 for (i
= 0; i
< ARRAY_SIZE( fs_cache
); i
++)
1012 if (fs_cache
[i
].dev
== dev
)
1017 /***********************************************************************
1020 * Adds the specified file system to the cache.
1022 static void add_fs_cache( dev_t dev
, fsid_t fsid
, BOOLEAN case_sensitive
)
1025 struct fs_cache
*entry
= look_up_fs_cache( dev
);
1026 static int once
= 0;
1029 /* Update the cache */
1031 entry
->case_sensitive
= case_sensitive
;
1035 /* Add a new entry */
1036 for (i
= 0; i
< ARRAY_SIZE( fs_cache
); i
++)
1037 if (fs_cache
[i
].dev
== 0)
1039 /* This entry is empty, use it */
1040 fs_cache
[i
].dev
= dev
;
1041 fs_cache
[i
].fsid
= fsid
;
1042 fs_cache
[i
].case_sensitive
= case_sensitive
;
1046 /* Cache is out of space, warn */
1048 WARN( "FS cache is out of space, expect performance problems\n" );
1051 /***********************************************************************
1052 * get_dir_case_sensitivity_attr
1054 * Checks if the volume containing the specified directory is case
1055 * sensitive or not. Uses getattrlist(2).
1057 static int get_dir_case_sensitivity_attr( const char *dir
)
1060 struct attrlist attr
;
1061 struct vol_caps caps
;
1062 struct get_fsid get_fsid
;
1063 struct fs_cache
*entry
;
1065 /* First get the FS ID of the volume */
1066 attr
.bitmapcount
= ATTR_BIT_MAP_COUNT
;
1068 attr
.commonattr
= ATTR_CMN_DEVID
|ATTR_CMN_FSID
;
1069 attr
.volattr
= attr
.dirattr
= attr
.fileattr
= attr
.forkattr
= 0;
1071 if (getattrlist( dir
, &attr
, &get_fsid
, sizeof(get_fsid
), 0 ) != 0 ||
1072 get_fsid
.size
!= sizeof(get_fsid
))
1074 /* Try to look it up in the cache */
1075 entry
= look_up_fs_cache( get_fsid
.dev
);
1076 if (entry
&& !memcmp( &entry
->fsid
, &get_fsid
.fsid
, sizeof(fsid_t
) ))
1077 /* Cache lookup succeeded */
1078 return entry
->case_sensitive
;
1079 /* Cache is stale at this point, we have to update it */
1081 mntpoint
= get_device_mount_point( get_fsid
.dev
);
1082 /* Now look up the case-sensitivity */
1083 attr
.commonattr
= 0;
1084 attr
.volattr
= ATTR_VOL_INFO
|ATTR_VOL_CAPABILITIES
;
1085 if (getattrlist( mntpoint
, &attr
, &caps
, sizeof(caps
), 0 ) < 0)
1087 RtlFreeHeap( GetProcessHeap(), 0, mntpoint
);
1088 add_fs_cache( get_fsid
.dev
, get_fsid
.fsid
, TRUE
);
1091 RtlFreeHeap( GetProcessHeap(), 0, mntpoint
);
1092 if (caps
.size
== sizeof(caps
) &&
1093 (caps
.caps
.valid
[VOL_CAPABILITIES_FORMAT
] &
1094 (VOL_CAP_FMT_CASE_SENSITIVE
| VOL_CAP_FMT_CASE_PRESERVING
)) ==
1095 (VOL_CAP_FMT_CASE_SENSITIVE
| VOL_CAP_FMT_CASE_PRESERVING
))
1099 if ((caps
.caps
.capabilities
[VOL_CAPABILITIES_FORMAT
] &
1100 VOL_CAP_FMT_CASE_SENSITIVE
) != VOL_CAP_FMT_CASE_SENSITIVE
)
1104 /* Update the cache */
1105 add_fs_cache( get_fsid
.dev
, get_fsid
.fsid
, ret
);
1112 /***********************************************************************
1113 * get_dir_case_sensitivity_stat
1115 * Checks if the volume containing the specified directory is case
1116 * sensitive or not. Uses statfs(2) or statvfs(2).
1118 static BOOLEAN
get_dir_case_sensitivity_stat( const char *dir
)
1120 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1123 if (statfs( dir
, &stfs
) == -1) return FALSE
;
1124 /* Assume these file systems are always case insensitive on Mac OS.
1125 * For FreeBSD, only assume CIOPFS is case insensitive (AFAIK, Mac OS
1126 * is the only UNIX that supports case-insensitive lookup).
1128 if (!strcmp( stfs
.f_fstypename
, "fusefs" ) &&
1129 !strncmp( stfs
.f_mntfromname
, "ciopfs", 5 ))
1132 if (!strcmp( stfs
.f_fstypename
, "msdos" ) ||
1133 !strcmp( stfs
.f_fstypename
, "cd9660" ) ||
1134 !strcmp( stfs
.f_fstypename
, "udf" ) ||
1135 !strcmp( stfs
.f_fstypename
, "ntfs" ) ||
1136 !strcmp( stfs
.f_fstypename
, "smbfs" ))
1138 #ifdef _DARWIN_FEATURE_64_BIT_INODE
1139 if (!strcmp( stfs
.f_fstypename
, "hfs" ) && (stfs
.f_fssubtype
== 0 ||
1140 stfs
.f_fssubtype
== 1 ||
1141 stfs
.f_fssubtype
== 128))
1144 /* The field says "reserved", but a quick look at the kernel source
1145 * tells us that this "reserved" field is really the same as the
1146 * "fssubtype" field from the inode64 structure (see munge_statfs()
1147 * in <xnu-source>/bsd/vfs/vfs_syscalls.c).
1149 if (!strcmp( stfs
.f_fstypename
, "hfs" ) && (stfs
.f_reserved1
== 0 ||
1150 stfs
.f_reserved1
== 1 ||
1151 stfs
.f_reserved1
== 128))
1157 #elif defined(__NetBSD__)
1158 struct statvfs stfs
;
1160 if (statvfs( dir
, &stfs
) == -1) return FALSE
;
1161 /* Only assume CIOPFS is case insensitive. */
1162 if (strcmp( stfs
.f_fstypename
, "fusefs" ) ||
1163 strncmp( stfs
.f_mntfromname
, "ciopfs", 5 ))
1167 #elif defined(__linux__)
1172 /* Only assume CIOPFS is case insensitive. */
1173 if (statfs( dir
, &stfs
) == -1) return FALSE
;
1174 if (stfs
.f_type
!= 0x65735546 /* FUSE_SUPER_MAGIC */)
1176 /* Normally, we'd have to parse the mtab to find out exactly what
1177 * kind of FUSE FS this is. But, someone on wine-devel suggested
1178 * a shortcut. We'll stat a special file in the directory. If it's
1179 * there, we'll assume it's a CIOPFS, else not.
1180 * This will break if somebody puts a file named ".ciopfs" in a non-
1183 cifile
= RtlAllocateHeap( GetProcessHeap(), 0, strlen( dir
)+sizeof("/.ciopfs") );
1184 if (!cifile
) return TRUE
;
1185 strcpy( cifile
, dir
);
1186 strcat( cifile
, "/.ciopfs" );
1187 if (stat( cifile
, &st
) == 0)
1189 RtlFreeHeap( GetProcessHeap(), 0, cifile
);
1192 RtlFreeHeap( GetProcessHeap(), 0, cifile
);
1200 /***********************************************************************
1201 * get_dir_case_sensitivity
1203 * Checks if the volume containing the specified directory is case
1204 * sensitive or not. Uses statfs(2) or statvfs(2).
1206 static BOOLEAN
get_dir_case_sensitivity( const char *dir
)
1208 #if defined(HAVE_GETATTRLIST) && defined(ATTR_VOL_CAPABILITIES) && \
1209 defined(VOL_CAPABILITIES_FORMAT) && defined(VOL_CAP_FMT_CASE_SENSITIVE)
1210 int case_sensitive
= get_dir_case_sensitivity_attr( dir
);
1211 if (case_sensitive
!= -1) return case_sensitive
;
1213 return get_dir_case_sensitivity_stat( dir
);
1217 /***********************************************************************
1220 * Initialize the show_dot_files options.
1222 static DWORD WINAPI
init_options( RTL_RUN_ONCE
*once
, void *param
, void **context
)
1224 static const WCHAR WineW
[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e',0};
1225 static const WCHAR ShowDotFilesW
[] = {'S','h','o','w','D','o','t','F','i','l','e','s',0};
1229 OBJECT_ATTRIBUTES attr
;
1230 UNICODE_STRING nameW
;
1232 RtlOpenCurrentUser( KEY_ALL_ACCESS
, &root
);
1233 attr
.Length
= sizeof(attr
);
1234 attr
.RootDirectory
= root
;
1235 attr
.ObjectName
= &nameW
;
1236 attr
.Attributes
= 0;
1237 attr
.SecurityDescriptor
= NULL
;
1238 attr
.SecurityQualityOfService
= NULL
;
1239 RtlInitUnicodeString( &nameW
, WineW
);
1241 /* @@ Wine registry key: HKCU\Software\Wine */
1242 if (!NtOpenKey( &hkey
, KEY_ALL_ACCESS
, &attr
))
1244 RtlInitUnicodeString( &nameW
, ShowDotFilesW
);
1245 if (!NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
, tmp
, sizeof(tmp
), &dummy
))
1247 WCHAR
*str
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)tmp
)->Data
;
1248 show_dot_files
= IS_OPTION_TRUE( str
[0] );
1254 /* a couple of directories that we don't want to return in directory searches */
1255 ignore_file( wine_get_config_dir() );
1256 ignore_file( "/dev" );
1257 ignore_file( "/proc" );
1259 ignore_file( "/sys" );
1265 /***********************************************************************
1266 * DIR_is_hidden_file
1268 * Check if the specified file should be hidden based on its name and the show dot files option.
1270 BOOL
DIR_is_hidden_file( const UNICODE_STRING
*name
)
1274 RtlRunOnceExecuteOnce( &init_once
, init_options
, NULL
, NULL
);
1276 if (show_dot_files
) return FALSE
;
1278 end
= p
= name
->Buffer
+ name
->Length
/sizeof(WCHAR
);
1279 while (p
> name
->Buffer
&& IS_SEPARATOR(p
[-1])) p
--;
1280 while (p
> name
->Buffer
&& !IS_SEPARATOR(p
[-1])) p
--;
1281 if (p
== end
|| *p
!= '.') return FALSE
;
1282 /* make sure it isn't '.' or '..' */
1283 if (p
+ 1 == end
) return FALSE
;
1284 if (p
[1] == '.' && p
+ 2 == end
) return FALSE
;
1289 /***********************************************************************
1290 * hash_short_file_name
1292 * Transform a Unix file name into a hashed DOS name. If the name is a valid
1293 * DOS name, it is converted to upper-case; otherwise it is replaced by a
1294 * hashed version that fits in 8.3 format.
1295 * 'buffer' must be at least 12 characters long.
1296 * Returns length of short name in bytes; short name is NOT null-terminated.
1298 static ULONG
hash_short_file_name( const UNICODE_STRING
*name
, LPWSTR buffer
)
1300 static const char hash_chars
[32] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345";
1302 LPCWSTR p
, ext
, end
= name
->Buffer
+ name
->Length
/ sizeof(WCHAR
);
1304 unsigned short hash
;
1307 /* Compute the hash code of the file name */
1308 /* If you know something about hash functions, feel free to */
1309 /* insert a better algorithm here... */
1310 if (!is_case_sensitive
)
1312 for (p
= name
->Buffer
, hash
= 0xbeef; p
< end
- 1; p
++)
1313 hash
= (hash
<<3) ^ (hash
>>5) ^ tolowerW(*p
) ^ (tolowerW(p
[1]) << 8);
1314 hash
= (hash
<<3) ^ (hash
>>5) ^ tolowerW(*p
); /* Last character */
1318 for (p
= name
->Buffer
, hash
= 0xbeef; p
< end
- 1; p
++)
1319 hash
= (hash
<< 3) ^ (hash
>> 5) ^ *p
^ (p
[1] << 8);
1320 hash
= (hash
<< 3) ^ (hash
>> 5) ^ *p
; /* Last character */
1323 /* Find last dot for start of the extension */
1324 for (p
= name
->Buffer
+ 1, ext
= NULL
; p
< end
- 1; p
++) if (*p
== '.') ext
= p
;
1326 /* Copy first 4 chars, replacing invalid chars with '_' */
1327 for (i
= 4, p
= name
->Buffer
, dst
= buffer
; i
> 0; i
--, p
++)
1329 if (p
== end
|| p
== ext
) break;
1330 *dst
++ = is_invalid_dos_char(*p
) ? '_' : toupperW(*p
);
1332 /* Pad to 5 chars with '~' */
1333 while (i
-- >= 0) *dst
++ = '~';
1335 /* Insert hash code converted to 3 ASCII chars */
1336 *dst
++ = hash_chars
[(hash
>> 10) & 0x1f];
1337 *dst
++ = hash_chars
[(hash
>> 5) & 0x1f];
1338 *dst
++ = hash_chars
[hash
& 0x1f];
1340 /* Copy the first 3 chars of the extension (if any) */
1344 for (i
= 3, ext
++; (i
> 0) && ext
< end
; i
--, ext
++)
1345 *dst
++ = is_invalid_dos_char(*ext
) ? '_' : toupperW(*ext
);
1347 return dst
- buffer
;
1351 /***********************************************************************
1354 * Check a long file name against a mask.
1356 * Tests (done in W95 DOS shell - case insensitive):
1357 * *.txt test1.test.txt *
1359 * *.t??????.t* test1.ta.tornado.txt *
1360 * *tornado* test1.ta.tornado.txt *
1361 * t*t test1.ta.tornado.txt *
1363 * ?est??? test1.txt -
1364 * *test1.txt* test1.txt *
1365 * h?l?o*t.dat hellothisisatest.dat *
1367 static BOOLEAN
match_filename( const UNICODE_STRING
*name_str
, const UNICODE_STRING
*mask_str
)
1370 const WCHAR
*name
= name_str
->Buffer
;
1371 const WCHAR
*mask
= mask_str
->Buffer
;
1372 const WCHAR
*name_end
= name
+ name_str
->Length
/ sizeof(WCHAR
);
1373 const WCHAR
*mask_end
= mask
+ mask_str
->Length
/ sizeof(WCHAR
);
1374 const WCHAR
*lastjoker
= NULL
;
1375 const WCHAR
*next_to_retry
= NULL
;
1377 while (name
< name_end
&& mask
< mask_end
)
1383 while (mask
< mask_end
&& *mask
== '*') mask
++; /* Skip consecutive '*' */
1384 if (mask
== mask_end
) return TRUE
; /* end of mask is all '*', so match */
1387 /* skip to the next match after the joker(s) */
1388 if (is_case_sensitive
)
1389 while (name
< name_end
&& (*name
!= *mask
)) name
++;
1391 while (name
< name_end
&& (toupperW(*name
) != toupperW(*mask
))) name
++;
1392 next_to_retry
= name
;
1399 if (is_case_sensitive
) mismatch
= (*mask
!= *name
);
1400 else mismatch
= (toupperW(*mask
) != toupperW(*name
));
1406 if (mask
== mask_end
)
1408 if (name
== name_end
) return TRUE
;
1409 if (lastjoker
) mask
= lastjoker
;
1412 else /* mismatch ! */
1414 if (lastjoker
) /* we had an '*', so we can try unlimitedly */
1418 /* this scan sequence was a mismatch, so restart
1419 * 1 char after the first char we checked last time */
1421 name
= next_to_retry
;
1423 else return FALSE
; /* bad luck */
1428 while (mask
< mask_end
&& ((*mask
== '.') || (*mask
== '*')))
1429 mask
++; /* Ignore trailing '.' or '*' in mask */
1430 return (name
== name_end
&& mask
== mask_end
);
1434 /***********************************************************************
1437 * Add a file to the directory data if it matches the mask.
1439 static BOOL
append_entry( struct dir_data
*data
, const char *long_name
,
1440 const char *short_name
, const UNICODE_STRING
*mask
)
1442 int i
, long_len
, short_len
;
1443 WCHAR long_nameW
[MAX_DIR_ENTRY_LEN
+ 1];
1444 WCHAR short_nameW
[13];
1447 long_len
= ntdll_umbstowcs( 0, long_name
, strlen(long_name
), long_nameW
, MAX_DIR_ENTRY_LEN
);
1448 if (long_len
== -1) return TRUE
;
1449 long_nameW
[long_len
] = 0;
1451 str
.Buffer
= long_nameW
;
1452 str
.Length
= long_len
* sizeof(WCHAR
);
1453 str
.MaximumLength
= sizeof(long_nameW
);
1457 short_len
= ntdll_umbstowcs( 0, short_name
, strlen(short_name
),
1458 short_nameW
, ARRAY_SIZE( short_nameW
) - 1 );
1459 if (short_len
== -1) short_len
= ARRAY_SIZE( short_nameW
) - 1;
1460 for (i
= 0; i
< short_len
; i
++) short_nameW
[i
] = toupperW( short_nameW
[i
] );
1462 else /* generate a short name if necessary */
1467 if (!RtlIsNameLegalDOS8Dot3( &str
, NULL
, &spaces
) || spaces
)
1468 short_len
= hash_short_file_name( &str
, short_nameW
);
1470 short_nameW
[short_len
] = 0;
1472 TRACE( "long %s short %s mask %s\n",
1473 debugstr_w( long_nameW
), debugstr_w( short_nameW
), debugstr_us( mask
));
1475 if (mask
&& !match_filename( &str
, mask
))
1477 if (!short_len
) return TRUE
; /* no short name to match */
1478 str
.Buffer
= short_nameW
;
1479 str
.Length
= short_len
* sizeof(WCHAR
);
1480 str
.MaximumLength
= sizeof(short_nameW
);
1481 if (!match_filename( &str
, mask
)) return TRUE
;
1484 return add_dir_data_names( data
, long_nameW
, short_nameW
, long_name
);
1488 /***********************************************************************
1489 * get_dir_data_entry
1491 * Return a directory entry from the cached data.
1493 static NTSTATUS
get_dir_data_entry( struct dir_data
*dir_data
, void *info_ptr
, IO_STATUS_BLOCK
*io
,
1494 ULONG max_length
, FILE_INFORMATION_CLASS
class,
1495 union file_directory_info
**last_info
)
1497 const struct dir_data_names
*names
= &dir_data
->names
[dir_data
->pos
];
1498 union file_directory_info
*info
;
1500 ULONG name_len
, start
, dir_size
, attributes
;
1502 if (get_file_info( names
->unix_name
, &st
, &attributes
) == -1)
1504 TRACE( "file no longer exists %s\n", names
->unix_name
);
1505 return STATUS_SUCCESS
;
1507 if (is_ignored_file( &st
))
1509 TRACE( "ignoring file %s\n", names
->unix_name
);
1510 return STATUS_SUCCESS
;
1512 start
= dir_info_align( io
->Information
);
1513 dir_size
= dir_info_size( class, 0 );
1514 if (start
+ dir_size
> max_length
) return STATUS_MORE_ENTRIES
;
1516 max_length
-= start
+ dir_size
;
1517 name_len
= strlenW( names
->long_name
) * sizeof(WCHAR
);
1518 /* if this is not the first entry, fail; the first entry is always returned (but truncated) */
1519 if (*last_info
&& name_len
> max_length
) return STATUS_MORE_ENTRIES
;
1521 info
= (union file_directory_info
*)((char *)info_ptr
+ start
);
1522 info
->dir
.NextEntryOffset
= 0;
1523 info
->dir
.FileIndex
= 0; /* NTFS always has 0 here, so let's not bother with it */
1525 /* all the structures except FileNamesInformation start with a FileDirectoryInformation layout */
1526 if (class != FileNamesInformation
)
1528 if (st
.st_dev
!= dir_data
->id
.dev
) st
.st_ino
= 0; /* ignore inode if on a different device */
1530 if (!show_dot_files
&& names
->long_name
[0] == '.' && names
->long_name
[1] &&
1531 (names
->long_name
[1] != '.' || names
->long_name
[2]))
1532 attributes
|= FILE_ATTRIBUTE_HIDDEN
;
1534 fill_file_info( &st
, attributes
, info
, class );
1539 case FileDirectoryInformation
:
1540 info
->dir
.FileNameLength
= name_len
;
1543 case FileFullDirectoryInformation
:
1544 info
->full
.EaSize
= 0; /* FIXME */
1545 info
->full
.FileNameLength
= name_len
;
1548 case FileIdFullDirectoryInformation
:
1549 info
->id_full
.EaSize
= 0; /* FIXME */
1550 info
->id_full
.FileNameLength
= name_len
;
1553 case FileBothDirectoryInformation
:
1554 info
->both
.EaSize
= 0; /* FIXME */
1555 info
->both
.ShortNameLength
= strlenW( names
->short_name
) * sizeof(WCHAR
);
1556 memcpy( info
->both
.ShortName
, names
->short_name
, info
->both
.ShortNameLength
);
1557 info
->both
.FileNameLength
= name_len
;
1560 case FileIdBothDirectoryInformation
:
1561 info
->id_both
.EaSize
= 0; /* FIXME */
1562 info
->id_both
.ShortNameLength
= strlenW( names
->short_name
) * sizeof(WCHAR
);
1563 memcpy( info
->id_both
.ShortName
, names
->short_name
, info
->id_both
.ShortNameLength
);
1564 info
->id_both
.FileNameLength
= name_len
;
1567 case FileIdGlobalTxDirectoryInformation
:
1568 info
->id_tx
.TxInfoFlags
= 0;
1569 info
->id_tx
.FileNameLength
= name_len
;
1572 case FileNamesInformation
:
1573 info
->names
.FileNameLength
= name_len
;
1581 memcpy( (char *)info
+ dir_size
, names
->long_name
, min( name_len
, max_length
) );
1582 io
->Information
= start
+ dir_size
+ min( name_len
, max_length
);
1583 if (*last_info
) (*last_info
)->next
= (char *)info
- (char *)*last_info
;
1585 return name_len
> max_length
? STATUS_BUFFER_OVERFLOW
: STATUS_SUCCESS
;
1588 #ifdef VFAT_IOCTL_READDIR_BOTH
1590 /***********************************************************************
1593 * Wrapper for the VFAT ioctl to work around various kernel bugs.
1594 * dir_section must be held by caller.
1596 static KERNEL_DIRENT
*start_vfat_ioctl( int fd
)
1598 static KERNEL_DIRENT
*de
;
1603 SIZE_T size
= 2 * sizeof(*de
) + page_size
;
1606 if (NtAllocateVirtualMemory( GetCurrentProcess(), &addr
, 1, &size
, MEM_RESERVE
, PAGE_READWRITE
))
1608 /* commit only the size needed for the dir entries */
1609 /* this leaves an extra unaccessible page, which should make the kernel */
1610 /* fail with -EFAULT before it stomps all over our memory */
1612 size
= 2 * sizeof(*de
);
1613 NtAllocateVirtualMemory( GetCurrentProcess(), &addr
, 1, &size
, MEM_COMMIT
, PAGE_READWRITE
);
1616 /* set d_reclen to 65535 to work around an AFS kernel bug */
1617 de
[0].d_reclen
= 65535;
1618 res
= ioctl( fd
, VFAT_IOCTL_READDIR_BOTH
, (long)de
);
1621 if (errno
!= ENOENT
) return NULL
; /* VFAT ioctl probably not supported */
1622 de
[0].d_reclen
= 0; /* eof */
1624 else if (!res
&& de
[0].d_reclen
== 65535) return NULL
; /* AFS bug */
1630 /***********************************************************************
1631 * read_directory_vfat
1633 * Read a directory using the VFAT ioctl; helper for NtQueryDirectoryFile.
1635 static NTSTATUS
read_directory_data_vfat( struct dir_data
*data
, int fd
, const UNICODE_STRING
*mask
)
1637 char *short_name
, *long_name
;
1640 NTSTATUS status
= STATUS_NO_MEMORY
;
1641 off_t old_pos
= lseek( fd
, 0, SEEK_CUR
);
1643 if (!(de
= start_vfat_ioctl( fd
))) return STATUS_NOT_SUPPORTED
;
1645 lseek( fd
, 0, SEEK_SET
);
1647 if (!append_entry( data
, ".", NULL
, mask
)) goto done
;
1648 if (!append_entry( data
, "..", NULL
, mask
)) goto done
;
1650 while (ioctl( fd
, VFAT_IOCTL_READDIR_BOTH
, (long)de
) != -1)
1652 if (!de
[0].d_reclen
) break; /* eof */
1654 /* make sure names are null-terminated to work around an x86-64 kernel bug */
1655 len
= min( de
[0].d_reclen
, sizeof(de
[0].d_name
) - 1 );
1656 de
[0].d_name
[len
] = 0;
1657 len
= min( de
[1].d_reclen
, sizeof(de
[1].d_name
) - 1 );
1658 de
[1].d_name
[len
] = 0;
1660 if (!strcmp( de
[0].d_name
, "." ) || !strcmp( de
[0].d_name
, ".." )) continue;
1661 if (de
[1].d_name
[0])
1663 short_name
= de
[0].d_name
;
1664 long_name
= de
[1].d_name
;
1668 long_name
= de
[0].d_name
;
1671 if (!append_entry( data
, long_name
, short_name
, mask
)) goto done
;
1673 status
= STATUS_SUCCESS
;
1675 lseek( fd
, old_pos
, SEEK_SET
);
1678 #endif /* VFAT_IOCTL_READDIR_BOTH */
1681 #ifdef HAVE_GETATTRLIST
1682 /***********************************************************************
1683 * read_directory_getattrlist
1685 * Read a single file from a directory by determining whether the file
1686 * identified by mask exists using getattrlist.
1688 static NTSTATUS
read_directory_data_getattrlist( struct dir_data
*data
, const char *unix_name
)
1690 struct attrlist attrlist
;
1691 #include "pshpack4.h"
1695 struct attrreference name_reference
;
1697 char name
[NAME_MAX
* 3 + 1];
1699 #include "poppack.h"
1701 memset( &attrlist
, 0, sizeof(attrlist
) );
1702 attrlist
.bitmapcount
= ATTR_BIT_MAP_COUNT
;
1703 attrlist
.commonattr
= ATTR_CMN_NAME
| ATTR_CMN_OBJTYPE
;
1704 if (getattrlist( unix_name
, &attrlist
, &buffer
, sizeof(buffer
), FSOPT_NOFOLLOW
) == -1)
1705 return STATUS_NO_SUCH_FILE
;
1706 /* If unix_name named a symlink, the above may have succeeded even if the symlink is broken.
1707 Check that with another call without FSOPT_NOFOLLOW. We don't ask for any attributes. */
1708 if (buffer
.type
== VLNK
)
1711 attrlist
.commonattr
= 0;
1712 if (getattrlist( unix_name
, &attrlist
, &dummy
, sizeof(dummy
), 0 ) == -1)
1713 return STATUS_NO_SUCH_FILE
;
1716 TRACE( "found %s\n", buffer
.name
);
1718 if (!append_entry( data
, buffer
.name
, NULL
, NULL
)) return STATUS_NO_MEMORY
;
1720 return STATUS_SUCCESS
;
1722 #endif /* HAVE_GETATTRLIST */
1725 /***********************************************************************
1726 * read_directory_stat
1728 * Read a single file from a directory by determining whether the file
1729 * identified by mask exists using stat.
1731 static NTSTATUS
read_directory_data_stat( struct dir_data
*data
, const char *unix_name
)
1735 /* if the file system is not case sensitive we can't find the actual name through stat() */
1736 if (!get_dir_case_sensitivity(".")) return STATUS_NO_SUCH_FILE
;
1737 if (stat( unix_name
, &st
) == -1) return STATUS_NO_SUCH_FILE
;
1739 TRACE( "found %s\n", unix_name
);
1741 if (!append_entry( data
, unix_name
, NULL
, NULL
)) return STATUS_NO_MEMORY
;
1743 return STATUS_SUCCESS
;
1747 /***********************************************************************
1748 * read_directory_readdir
1750 * Read a directory using the POSIX readdir interface; helper for NtQueryDirectoryFile.
1752 static NTSTATUS
read_directory_data_readdir( struct dir_data
*data
, const UNICODE_STRING
*mask
)
1755 NTSTATUS status
= STATUS_NO_MEMORY
;
1756 DIR *dir
= opendir( "." );
1758 if (!dir
) return STATUS_NO_SUCH_FILE
;
1760 if (!append_entry( data
, ".", NULL
, mask
)) goto done
;
1761 if (!append_entry( data
, "..", NULL
, mask
)) goto done
;
1762 while ((de
= readdir( dir
)))
1764 if (!strcmp( de
->d_name
, "." ) || !strcmp( de
->d_name
, ".." )) continue;
1765 if (!append_entry( data
, de
->d_name
, NULL
, mask
)) goto done
;
1767 status
= STATUS_SUCCESS
;
1775 /***********************************************************************
1776 * read_directory_data
1778 * Read the full contents of a directory, using one of the above helper functions.
1780 static NTSTATUS
read_directory_data( struct dir_data
*data
, int fd
, const UNICODE_STRING
*mask
)
1784 #ifdef VFAT_IOCTL_READDIR_BOTH
1785 if (!(status
= read_directory_data_vfat( data
, fd
, mask
))) return status
;
1788 if (!has_wildcard( mask
))
1790 /* convert the mask to a Unix name and check for it */
1791 int ret
, used_default
;
1792 char unix_name
[MAX_DIR_ENTRY_LEN
* 3 + 1];
1794 ret
= ntdll_wcstoumbs( 0, mask
->Buffer
, mask
->Length
/ sizeof(WCHAR
),
1795 unix_name
, sizeof(unix_name
) - 1, NULL
, &used_default
);
1796 if (ret
> 0 && !used_default
)
1799 #ifdef HAVE_GETATTRLIST
1800 if (!(status
= read_directory_data_getattrlist( data
, unix_name
))) return status
;
1802 if (!(status
= read_directory_data_stat( data
, unix_name
))) return status
;
1806 return read_directory_data_readdir( data
, mask
);
1810 /* compare file names for directory sorting */
1811 static int name_compare( const void *a
, const void *b
)
1813 const struct dir_data_names
*file_a
= (const struct dir_data_names
*)a
;
1814 const struct dir_data_names
*file_b
= (const struct dir_data_names
*)b
;
1815 int ret
= RtlCompareUnicodeStrings( file_a
->long_name
, strlenW(file_a
->long_name
),
1816 file_b
->long_name
, strlenW(file_b
->long_name
), TRUE
);
1817 if (!ret
) ret
= strcmpW( file_a
->long_name
, file_b
->long_name
);
1822 /***********************************************************************
1823 * init_cached_dir_data
1825 * Initialize the cached directory contents.
1827 static NTSTATUS
init_cached_dir_data( struct dir_data
**data_ret
, int fd
, const UNICODE_STRING
*mask
)
1829 struct dir_data
*data
;
1834 if (!(data
= RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*data
) )))
1835 return STATUS_NO_MEMORY
;
1837 if ((status
= read_directory_data( data
, fd
, mask
)))
1839 free_dir_data( data
);
1843 /* sort filenames, but not "." and ".." */
1845 if (i
< data
->count
&& !strcmp( data
->names
[i
].unix_name
, "." )) i
++;
1846 if (i
< data
->count
&& !strcmp( data
->names
[i
].unix_name
, ".." )) i
++;
1847 if (i
< data
->count
) qsort( data
->names
+ i
, data
->count
- i
, sizeof(*data
->names
), name_compare
);
1851 /* release unused space */
1853 RtlReAllocateHeap( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY
, data
->buffer
,
1854 offsetof( struct dir_data_buffer
, data
[data
->buffer
->pos
] ));
1855 if (data
->count
< data
->size
)
1856 RtlReAllocateHeap( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY
, data
->names
,
1857 data
->count
* sizeof(*data
->names
) );
1858 if (!fstat( fd
, &st
))
1860 data
->id
.dev
= st
.st_dev
;
1861 data
->id
.ino
= st
.st_ino
;
1865 TRACE( "mask %s found %u files\n", debugstr_us( mask
), data
->count
);
1866 for (i
= 0; i
< data
->count
; i
++)
1867 TRACE( "%s %s\n", debugstr_w(data
->names
[i
].long_name
), debugstr_w(data
->names
[i
].short_name
) );
1870 return data
->count
? STATUS_SUCCESS
: STATUS_NO_SUCH_FILE
;
1874 /***********************************************************************
1875 * get_cached_dir_data
1877 * Retrieve the cached directory data, or initialize it if necessary.
1879 static NTSTATUS
get_cached_dir_data( HANDLE handle
, struct dir_data
**data_ret
, int fd
,
1880 const UNICODE_STRING
*mask
)
1883 int entry
= -1, free_entries
[16];
1886 SERVER_START_REQ( get_directory_cache_entry
)
1888 req
->handle
= wine_server_obj_handle( handle
);
1889 wine_server_set_reply( req
, free_entries
, sizeof(free_entries
) );
1890 if (!(status
= wine_server_call( req
))) entry
= reply
->entry
;
1892 for (i
= 0; i
< wine_server_reply_size( reply
) / sizeof(*free_entries
); i
++)
1894 int free_idx
= free_entries
[i
];
1895 if (free_idx
< dir_data_cache_size
)
1897 free_dir_data( dir_data_cache
[free_idx
] );
1898 dir_data_cache
[free_idx
] = NULL
;
1906 if (status
== STATUS_SHARING_VIOLATION
) FIXME( "shared directory handle not supported yet\n" );
1910 if (entry
>= dir_data_cache_size
)
1912 unsigned int size
= max( dir_data_cache_initial_size
, max( dir_data_cache_size
* 2, entry
+ 1 ) );
1913 struct dir_data
**new_cache
;
1916 new_cache
= RtlReAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY
, dir_data_cache
,
1917 size
* sizeof(*new_cache
) );
1919 new_cache
= RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY
, size
* sizeof(*new_cache
) );
1920 if (!new_cache
) return STATUS_NO_MEMORY
;
1921 dir_data_cache
= new_cache
;
1922 dir_data_cache_size
= size
;
1925 if (!dir_data_cache
[entry
]) status
= init_cached_dir_data( &dir_data_cache
[entry
], fd
, mask
);
1927 *data_ret
= dir_data_cache
[entry
];
1932 /******************************************************************************
1933 * NtQueryDirectoryFile [NTDLL.@]
1934 * ZwQueryDirectoryFile [NTDLL.@]
1936 NTSTATUS WINAPI
NtQueryDirectoryFile( HANDLE handle
, HANDLE event
,
1937 PIO_APC_ROUTINE apc_routine
, PVOID apc_context
,
1938 PIO_STATUS_BLOCK io
,
1939 PVOID buffer
, ULONG length
,
1940 FILE_INFORMATION_CLASS info_class
,
1941 BOOLEAN single_entry
,
1942 PUNICODE_STRING mask
,
1943 BOOLEAN restart_scan
)
1945 int cwd
, fd
, needs_close
;
1946 struct dir_data
*data
;
1949 TRACE("(%p %p %p %p %p %p 0x%08x 0x%08x 0x%08x %s 0x%08x\n",
1950 handle
, event
, apc_routine
, apc_context
, io
, buffer
,
1951 length
, info_class
, single_entry
, debugstr_us(mask
),
1954 if (event
|| apc_routine
)
1956 FIXME( "Unsupported yet option\n" );
1957 return STATUS_NOT_IMPLEMENTED
;
1961 case FileDirectoryInformation
:
1962 case FileBothDirectoryInformation
:
1963 case FileFullDirectoryInformation
:
1964 case FileIdBothDirectoryInformation
:
1965 case FileIdFullDirectoryInformation
:
1966 case FileIdGlobalTxDirectoryInformation
:
1967 case FileNamesInformation
:
1968 if (length
< dir_info_align( dir_info_size( info_class
, 1 ))) return STATUS_INFO_LENGTH_MISMATCH
;
1970 case FileObjectIdInformation
:
1971 if (length
!= sizeof(FILE_OBJECTID_INFORMATION
)) return STATUS_INFO_LENGTH_MISMATCH
;
1972 return STATUS_INVALID_INFO_CLASS
;
1973 case FileQuotaInformation
:
1974 if (length
!= sizeof(FILE_QUOTA_INFORMATION
)) return STATUS_INFO_LENGTH_MISMATCH
;
1975 return STATUS_INVALID_INFO_CLASS
;
1976 case FileReparsePointInformation
:
1977 if (length
!= sizeof(FILE_REPARSE_POINT_INFORMATION
)) return STATUS_INFO_LENGTH_MISMATCH
;
1978 return STATUS_INVALID_INFO_CLASS
;
1980 return STATUS_INVALID_INFO_CLASS
;
1982 if (!buffer
) return STATUS_ACCESS_VIOLATION
;
1984 if ((status
= server_get_unix_fd( handle
, FILE_LIST_DIRECTORY
, &fd
, &needs_close
, NULL
, NULL
)) != STATUS_SUCCESS
)
1987 io
->Information
= 0;
1989 RtlRunOnceExecuteOnce( &init_once
, init_options
, NULL
, NULL
);
1991 RtlEnterCriticalSection( &dir_section
);
1993 cwd
= open( ".", O_RDONLY
);
1994 if (fchdir( fd
) != -1)
1996 if (!(status
= get_cached_dir_data( handle
, &data
, fd
, mask
)))
1998 union file_directory_info
*last_info
= NULL
;
2000 if (restart_scan
) data
->pos
= 0;
2002 while (!status
&& data
->pos
< data
->count
)
2004 status
= get_dir_data_entry( data
, buffer
, io
, length
, info_class
, &last_info
);
2005 if (!status
|| status
== STATUS_BUFFER_OVERFLOW
) data
->pos
++;
2006 if (single_entry
) break;
2009 if (!last_info
) status
= STATUS_NO_MORE_FILES
;
2010 else if (status
== STATUS_MORE_ENTRIES
) status
= STATUS_SUCCESS
;
2012 io
->u
.Status
= status
;
2014 if (cwd
== -1 || fchdir( cwd
) == -1) chdir( "/" );
2016 else status
= FILE_GetNtStatus();
2018 RtlLeaveCriticalSection( &dir_section
);
2020 if (needs_close
) close( fd
);
2021 if (cwd
!= -1) close( cwd
);
2022 TRACE( "=> %x (%ld)\n", status
, io
->Information
);
2027 /***********************************************************************
2030 * Find a file in a directory the hard way, by doing a case-insensitive search.
2031 * The file found is appended to unix_name at pos.
2032 * There must be at least MAX_DIR_ENTRY_LEN+2 chars available at pos.
2034 static NTSTATUS
find_file_in_dir( char *unix_name
, int pos
, const WCHAR
*name
, int length
,
2035 BOOLEAN check_case
, BOOLEAN
*is_win_dir
)
2037 WCHAR buffer
[MAX_DIR_ENTRY_LEN
];
2039 BOOLEAN spaces
, is_name_8_dot_3
;
2043 int ret
, used_default
;
2045 /* try a shortcut for this directory */
2047 unix_name
[pos
++] = '/';
2048 ret
= ntdll_wcstoumbs( 0, name
, length
, unix_name
+ pos
, MAX_DIR_ENTRY_LEN
,
2049 NULL
, &used_default
);
2050 /* if we used the default char, the Unix name won't round trip properly back to Unicode */
2051 /* so it cannot match the file we are looking for */
2052 if (ret
>= 0 && !used_default
)
2054 unix_name
[pos
+ ret
] = 0;
2055 if (!stat( unix_name
, &st
))
2057 if (is_win_dir
) *is_win_dir
= is_same_file( &windir
, &st
);
2058 return STATUS_SUCCESS
;
2061 if (check_case
) goto not_found
; /* we want an exact match */
2063 if (pos
> 1) unix_name
[pos
- 1] = 0;
2064 else unix_name
[1] = 0; /* keep the initial slash */
2066 /* check if it fits in 8.3 so that we don't look for short names if we won't need them */
2068 str
.Buffer
= (WCHAR
*)name
;
2069 str
.Length
= length
* sizeof(WCHAR
);
2070 str
.MaximumLength
= str
.Length
;
2071 is_name_8_dot_3
= RtlIsNameLegalDOS8Dot3( &str
, NULL
, &spaces
) && !spaces
;
2072 #ifndef VFAT_IOCTL_READDIR_BOTH
2073 is_name_8_dot_3
= is_name_8_dot_3
&& length
>= 8 && name
[4] == '~';
2076 if (!is_name_8_dot_3
&& !get_dir_case_sensitivity( unix_name
)) goto not_found
;
2078 /* now look for it through the directory */
2080 #ifdef VFAT_IOCTL_READDIR_BOTH
2081 if (is_name_8_dot_3
)
2083 int fd
= open( unix_name
, O_RDONLY
| O_DIRECTORY
);
2088 RtlEnterCriticalSection( &dir_section
);
2089 if ((kde
= start_vfat_ioctl( fd
)))
2091 unix_name
[pos
- 1] = '/';
2092 while (kde
[0].d_reclen
)
2094 /* make sure names are null-terminated to work around an x86-64 kernel bug */
2095 size_t len
= min(kde
[0].d_reclen
, sizeof(kde
[0].d_name
) - 1 );
2096 kde
[0].d_name
[len
] = 0;
2097 len
= min(kde
[1].d_reclen
, sizeof(kde
[1].d_name
) - 1 );
2098 kde
[1].d_name
[len
] = 0;
2100 if (kde
[1].d_name
[0])
2102 ret
= ntdll_umbstowcs( 0, kde
[1].d_name
, strlen(kde
[1].d_name
),
2103 buffer
, MAX_DIR_ENTRY_LEN
);
2104 if (ret
== length
&& !memicmpW( buffer
, name
, length
))
2106 strcpy( unix_name
+ pos
, kde
[1].d_name
);
2107 RtlLeaveCriticalSection( &dir_section
);
2112 ret
= ntdll_umbstowcs( 0, kde
[0].d_name
, strlen(kde
[0].d_name
),
2113 buffer
, MAX_DIR_ENTRY_LEN
);
2114 if (ret
== length
&& !memicmpW( buffer
, name
, length
))
2116 strcpy( unix_name
+ pos
,
2117 kde
[1].d_name
[0] ? kde
[1].d_name
: kde
[0].d_name
);
2118 RtlLeaveCriticalSection( &dir_section
);
2122 if (ioctl( fd
, VFAT_IOCTL_READDIR_BOTH
, (long)kde
) == -1)
2124 RtlLeaveCriticalSection( &dir_section
);
2130 RtlLeaveCriticalSection( &dir_section
);
2133 /* fall through to normal handling */
2135 #endif /* VFAT_IOCTL_READDIR_BOTH */
2137 if (!(dir
= opendir( unix_name
)))
2139 if (errno
== ENOENT
) return STATUS_OBJECT_PATH_NOT_FOUND
;
2140 else return FILE_GetNtStatus();
2142 unix_name
[pos
- 1] = '/';
2143 str
.Buffer
= buffer
;
2144 str
.MaximumLength
= sizeof(buffer
);
2145 while ((de
= readdir( dir
)))
2147 ret
= ntdll_umbstowcs( 0, de
->d_name
, strlen(de
->d_name
), buffer
, MAX_DIR_ENTRY_LEN
);
2148 if (ret
== length
&& !memicmpW( buffer
, name
, length
))
2150 strcpy( unix_name
+ pos
, de
->d_name
);
2155 if (!is_name_8_dot_3
) continue;
2157 str
.Length
= ret
* sizeof(WCHAR
);
2158 if (!RtlIsNameLegalDOS8Dot3( &str
, NULL
, &spaces
) || spaces
)
2160 WCHAR short_nameW
[12];
2161 ret
= hash_short_file_name( &str
, short_nameW
);
2162 if (ret
== length
&& !memicmpW( short_nameW
, name
, length
))
2164 strcpy( unix_name
+ pos
, de
->d_name
);
2173 unix_name
[pos
- 1] = 0;
2174 return STATUS_OBJECT_PATH_NOT_FOUND
;
2177 if (is_win_dir
&& !stat( unix_name
, &st
)) *is_win_dir
= is_same_file( &windir
, &st
);
2178 return STATUS_SUCCESS
;
2184 static const WCHAR catrootW
[] = {'s','y','s','t','e','m','3','2','\\','c','a','t','r','o','o','t',0};
2185 static const WCHAR catroot2W
[] = {'s','y','s','t','e','m','3','2','\\','c','a','t','r','o','o','t','2',0};
2186 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};
2187 static const WCHAR driversetcW
[] = {'s','y','s','t','e','m','3','2','\\','d','r','i','v','e','r','s','\\','e','t','c',0};
2188 static const WCHAR logfilesW
[] = {'s','y','s','t','e','m','3','2','\\','l','o','g','f','i','l','e','s',0};
2189 static const WCHAR spoolW
[] = {'s','y','s','t','e','m','3','2','\\','s','p','o','o','l',0};
2190 static const WCHAR system32W
[] = {'s','y','s','t','e','m','3','2',0};
2191 static const WCHAR syswow64W
[] = {'s','y','s','w','o','w','6','4',0};
2192 static const WCHAR sysnativeW
[] = {'s','y','s','n','a','t','i','v','e',0};
2193 static const WCHAR regeditW
[] = {'r','e','g','e','d','i','t','.','e','x','e',0};
2194 static const WCHAR wow_regeditW
[] = {'s','y','s','w','o','w','6','4','\\','r','e','g','e','d','i','t','.','e','x','e',0};
2198 const WCHAR
*source
;
2199 const WCHAR
*dos_target
;
2200 const char *unix_target
;
2203 { catrootW
, NULL
, NULL
},
2204 { catroot2W
, NULL
, NULL
},
2205 { driversstoreW
, NULL
, NULL
},
2206 { driversetcW
, NULL
, NULL
},
2207 { logfilesW
, NULL
, NULL
},
2208 { spoolW
, NULL
, NULL
},
2209 { system32W
, syswow64W
, NULL
},
2210 { sysnativeW
, system32W
, NULL
},
2211 { regeditW
, wow_regeditW
, NULL
}
2214 static unsigned int nb_redirects
;
2217 /***********************************************************************
2218 * get_redirect_target
2220 * Find the target unix name for a redirected dir.
2222 static const char *get_redirect_target( const char *windows_dir
, const WCHAR
*name
)
2224 int used_default
, len
, pos
, win_len
= strlen( windows_dir
);
2225 char *unix_name
, *unix_target
= NULL
;
2228 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, win_len
+ MAX_DIR_ENTRY_LEN
+ 2 )))
2230 memcpy( unix_name
, windows_dir
, win_len
);
2235 const WCHAR
*end
, *next
;
2237 for (end
= name
; *end
; end
++) if (IS_SEPARATOR(*end
)) break;
2238 for (next
= end
; *next
; next
++) if (!IS_SEPARATOR(*next
)) break;
2240 status
= find_file_in_dir( unix_name
, pos
, name
, end
- name
, FALSE
, NULL
);
2241 if (status
== STATUS_OBJECT_PATH_NOT_FOUND
&& !*next
) /* not finding last element is ok */
2243 len
= ntdll_wcstoumbs( 0, name
, end
- name
, unix_name
+ pos
+ 1,
2244 MAX_DIR_ENTRY_LEN
- (pos
- win_len
), NULL
, &used_default
);
2245 if (len
> 0 && !used_default
)
2247 unix_name
[pos
] = '/';
2253 if (status
) goto done
;
2254 pos
+= strlen( unix_name
+ pos
);
2258 if ((unix_target
= RtlAllocateHeap( GetProcessHeap(), 0, pos
- win_len
)))
2259 memcpy( unix_target
, unix_name
+ win_len
+ 1, pos
- win_len
);
2262 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2267 /***********************************************************************
2270 static void init_redirects(void)
2272 UNICODE_STRING nt_name
;
2273 ANSI_STRING unix_name
;
2278 if (!RtlDosPathNameToNtPathName_U( user_shared_data
->NtSystemRoot
, &nt_name
, NULL
, NULL
))
2280 ERR( "can't convert %s\n", debugstr_w(user_shared_data
->NtSystemRoot
) );
2283 status
= wine_nt_to_unix_file_name( &nt_name
, &unix_name
, FILE_OPEN_IF
, FALSE
);
2284 RtlFreeUnicodeString( &nt_name
);
2287 ERR( "cannot open %s (%x)\n", debugstr_w(user_shared_data
->NtSystemRoot
), status
);
2290 if (!stat( unix_name
.Buffer
, &st
))
2292 windir
.dev
= st
.st_dev
;
2293 windir
.ino
= st
.st_ino
;
2294 nb_redirects
= ARRAY_SIZE( redirects
);
2295 for (i
= 0; i
< nb_redirects
; i
++)
2297 if (!redirects
[i
].dos_target
) continue;
2298 redirects
[i
].unix_target
= get_redirect_target( unix_name
.Buffer
, redirects
[i
].dos_target
);
2299 TRACE( "%s -> %s\n", debugstr_w(redirects
[i
].source
), redirects
[i
].unix_target
);
2302 RtlFreeAnsiString( &unix_name
);
2307 /***********************************************************************
2310 * Check if path matches a redirect name. If yes, return matched length.
2312 static int match_redirect( const WCHAR
*path
, int len
, const WCHAR
*redir
, BOOLEAN check_case
)
2316 while (i
< len
&& *redir
)
2318 if (IS_SEPARATOR(path
[i
]))
2320 if (*redir
++ != '\\') return 0;
2321 while (i
< len
&& IS_SEPARATOR(path
[i
])) i
++;
2322 continue; /* move on to next path component */
2324 else if (check_case
)
2326 if (path
[i
] != *redir
) return 0;
2330 if (tolowerW(path
[i
]) != tolowerW(*redir
)) return 0;
2335 if (*redir
) return 0;
2336 if (i
< len
&& !IS_SEPARATOR(path
[i
])) return 0;
2337 while (i
< len
&& IS_SEPARATOR(path
[i
])) i
++;
2342 /***********************************************************************
2345 * Retrieve the Unix path corresponding to a redirected path if any.
2347 static int get_redirect_path( char *unix_name
, int pos
, const WCHAR
*name
, int length
, BOOLEAN check_case
)
2352 for (i
= 0; i
< nb_redirects
; i
++)
2354 if ((len
= match_redirect( name
, length
, redirects
[i
].source
, check_case
)))
2356 if (!redirects
[i
].unix_target
) break;
2357 unix_name
[pos
++] = '/';
2358 strcpy( unix_name
+ pos
, redirects
[i
].unix_target
);
2367 /* there are no redirects on 64-bit */
2369 static const unsigned int nb_redirects
= 0;
2371 static int get_redirect_path( char *unix_name
, int pos
, const WCHAR
*name
, int length
, BOOLEAN check_case
)
2378 /***********************************************************************
2381 void init_directories(void)
2384 if (is_wow64
) init_redirects();
2389 /******************************************************************************
2392 * Get the Unix path of a DOS device.
2394 static NTSTATUS
get_dos_device( const WCHAR
*name
, UINT name_len
, ANSI_STRING
*unix_name_ret
)
2396 const char *config_dir
= wine_get_config_dir();
2398 char *unix_name
, *new_name
, *dev
;
2402 /* make sure the device name is ASCII */
2403 for (i
= 0; i
< name_len
; i
++)
2404 if (name
[i
] <= 32 || name
[i
] >= 127) return STATUS_BAD_DEVICE_TYPE
;
2406 unix_len
= strlen(config_dir
) + sizeof("/dosdevices/") + name_len
+ 1;
2408 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, unix_len
)))
2409 return STATUS_NO_MEMORY
;
2411 strcpy( unix_name
, config_dir
);
2412 strcat( unix_name
, "/dosdevices/" );
2413 dev
= unix_name
+ strlen(unix_name
);
2415 for (i
= 0; i
< name_len
; i
++) dev
[i
] = (char)tolowerW(name
[i
]);
2418 /* special case for drive devices */
2419 if (name_len
== 2 && dev
[1] == ':')
2427 if (!stat( unix_name
, &st
))
2429 TRACE( "%s -> %s\n", debugstr_wn(name
,name_len
), debugstr_a(unix_name
) );
2430 unix_name_ret
->Buffer
= unix_name
;
2431 unix_name_ret
->Length
= strlen(unix_name
);
2432 unix_name_ret
->MaximumLength
= unix_len
;
2433 return STATUS_SUCCESS
;
2437 /* now try some defaults for it */
2438 if (!strcmp( dev
, "aux" ))
2440 strcpy( dev
, "com1" );
2443 if (!strcmp( dev
, "prn" ))
2445 strcpy( dev
, "lpt1" );
2450 if (dev
[1] == ':' && dev
[2] == ':') /* drive device */
2452 dev
[2] = 0; /* remove last ':' to get the drive mount point symlink */
2453 new_name
= get_default_drive_device( unix_name
);
2456 if (!new_name
) break;
2458 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2459 unix_name
= new_name
;
2460 unix_len
= strlen(unix_name
) + 1;
2461 dev
= NULL
; /* last try */
2463 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2464 return STATUS_BAD_DEVICE_TYPE
;
2468 /* return the length of the DOS namespace prefix if any */
2469 static inline int get_dos_prefix_len( const UNICODE_STRING
*name
)
2471 static const WCHAR nt_prefixW
[] = {'\\','?','?','\\'};
2472 static const WCHAR dosdev_prefixW
[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\'};
2474 if (name
->Length
>= sizeof(nt_prefixW
) &&
2475 !memcmp( name
->Buffer
, nt_prefixW
, sizeof(nt_prefixW
) ))
2476 return ARRAY_SIZE( nt_prefixW
);
2478 if (name
->Length
>= sizeof(dosdev_prefixW
) &&
2479 !memicmpW( name
->Buffer
, dosdev_prefixW
, ARRAY_SIZE( dosdev_prefixW
)))
2480 return ARRAY_SIZE( dosdev_prefixW
);
2486 /******************************************************************************
2489 * Recursively search directories from the dir queue for a given inode.
2491 static NTSTATUS
find_file_id( ANSI_STRING
*unix_name
, ULONGLONG file_id
, dev_t dev
)
2499 while (!(status
= next_dir_in_queue( unix_name
->Buffer
)))
2501 if (!(dir
= opendir( unix_name
->Buffer
))) continue;
2502 TRACE( "searching %s for %s\n", unix_name
->Buffer
, wine_dbgstr_longlong(file_id
) );
2503 pos
= strlen( unix_name
->Buffer
);
2504 if (pos
+ MAX_DIR_ENTRY_LEN
>= unix_name
->MaximumLength
/sizeof(WCHAR
))
2506 char *new = RtlReAllocateHeap( GetProcessHeap(), 0, unix_name
->Buffer
,
2507 unix_name
->MaximumLength
* 2 );
2511 return STATUS_NO_MEMORY
;
2513 unix_name
->MaximumLength
*= 2;
2514 unix_name
->Buffer
= new;
2516 unix_name
->Buffer
[pos
++] = '/';
2517 while ((de
= readdir( dir
)))
2519 if (!strcmp( de
->d_name
, "." ) || !strcmp( de
->d_name
, ".." )) continue;
2520 strcpy( unix_name
->Buffer
+ pos
, de
->d_name
);
2521 if (lstat( unix_name
->Buffer
, &st
) == -1) continue;
2522 if (st
.st_dev
!= dev
) continue;
2523 if (st
.st_ino
== file_id
)
2526 return STATUS_SUCCESS
;
2528 if (!S_ISDIR( st
.st_mode
)) continue;
2529 if ((status
= add_dir_to_queue( unix_name
->Buffer
)) != STATUS_SUCCESS
)
2541 /******************************************************************************
2542 * file_id_to_unix_file_name
2544 * Lookup a file from its file id instead of its name.
2546 NTSTATUS
file_id_to_unix_file_name( const OBJECT_ATTRIBUTES
*attr
, ANSI_STRING
*unix_name
)
2548 enum server_fd_type type
;
2549 int old_cwd
, root_fd
, needs_close
;
2552 struct stat st
, root_st
;
2554 if (attr
->ObjectName
->Length
!= sizeof(ULONGLONG
)) return STATUS_OBJECT_PATH_SYNTAX_BAD
;
2555 if (!attr
->RootDirectory
) return STATUS_INVALID_PARAMETER
;
2556 memcpy( &file_id
, attr
->ObjectName
->Buffer
, sizeof(file_id
) );
2558 unix_name
->MaximumLength
= 2 * MAX_DIR_ENTRY_LEN
+ 4;
2559 if (!(unix_name
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, unix_name
->MaximumLength
)))
2560 return STATUS_NO_MEMORY
;
2561 strcpy( unix_name
->Buffer
, "." );
2563 if ((status
= server_get_unix_fd( attr
->RootDirectory
, 0, &root_fd
, &needs_close
, &type
, NULL
)))
2566 if (type
!= FD_TYPE_DIR
)
2568 status
= STATUS_OBJECT_TYPE_MISMATCH
;
2572 fstat( root_fd
, &root_st
);
2573 if (root_st
.st_ino
== file_id
) /* shortcut for "." */
2575 status
= STATUS_SUCCESS
;
2579 RtlEnterCriticalSection( &dir_section
);
2580 if ((old_cwd
= open( ".", O_RDONLY
)) != -1 && fchdir( root_fd
) != -1)
2582 /* shortcut for ".." */
2583 if (!stat( "..", &st
) && st
.st_dev
== root_st
.st_dev
&& st
.st_ino
== file_id
)
2585 strcpy( unix_name
->Buffer
, ".." );
2586 status
= STATUS_SUCCESS
;
2590 status
= add_dir_to_queue( "." );
2592 status
= find_file_id( unix_name
, file_id
, root_st
.st_dev
);
2593 if (!status
) /* get rid of "./" prefix */
2594 memmove( unix_name
->Buffer
, unix_name
->Buffer
+ 2, strlen(unix_name
->Buffer
) - 1 );
2597 if (fchdir( old_cwd
) == -1) chdir( "/" );
2599 else status
= FILE_GetNtStatus();
2600 RtlLeaveCriticalSection( &dir_section
);
2601 if (old_cwd
!= -1) close( old_cwd
);
2604 if (status
== STATUS_SUCCESS
)
2606 TRACE( "%s -> %s\n", wine_dbgstr_longlong(file_id
), debugstr_a(unix_name
->Buffer
) );
2607 unix_name
->Length
= strlen( unix_name
->Buffer
);
2611 TRACE( "%s not found in dir %p\n", wine_dbgstr_longlong(file_id
), attr
->RootDirectory
);
2612 RtlFreeHeap( GetProcessHeap(), 0, unix_name
->Buffer
);
2614 if (needs_close
) close( root_fd
);
2619 /******************************************************************************
2622 * Helper for nt_to_unix_file_name
2624 static NTSTATUS
lookup_unix_name( const WCHAR
*name
, int name_len
, char **buffer
, int unix_len
, int pos
,
2625 UINT disposition
, BOOLEAN check_case
)
2628 int ret
, used_default
, len
;
2630 char *unix_name
= *buffer
;
2631 const BOOL redirect
= nb_redirects
&& ntdll_get_thread_data()->wow64_redir
;
2633 /* try a shortcut first */
2635 ret
= ntdll_wcstoumbs( 0, name
, name_len
, unix_name
+ pos
, unix_len
- pos
- 1,
2636 NULL
, &used_default
);
2638 while (name_len
&& IS_SEPARATOR(*name
))
2644 if (ret
>= 0 && !used_default
) /* if we used the default char the name didn't convert properly */
2647 unix_name
[pos
+ ret
] = 0;
2648 for (p
= unix_name
+ pos
; *p
; p
++) if (*p
== '\\') *p
= '/';
2649 if (!redirect
|| (!strstr( unix_name
, "/windows/") && strncmp( unix_name
, "windows/", 8 )))
2651 if (!stat( unix_name
, &st
))
2653 /* creation fails with STATUS_ACCESS_DENIED for the root of the drive */
2654 if (disposition
== FILE_CREATE
)
2655 return name_len
? STATUS_OBJECT_NAME_COLLISION
: STATUS_ACCESS_DENIED
;
2656 return STATUS_SUCCESS
;
2661 if (!name_len
) /* empty name -> drive root doesn't exist */
2662 return STATUS_OBJECT_PATH_NOT_FOUND
;
2663 if (check_case
&& !redirect
&& (disposition
== FILE_OPEN
|| disposition
== FILE_OVERWRITE
))
2664 return STATUS_OBJECT_NAME_NOT_FOUND
;
2666 /* now do it component by component */
2670 const WCHAR
*end
, *next
;
2671 BOOLEAN is_win_dir
= FALSE
;
2674 while (end
< name
+ name_len
&& !IS_SEPARATOR(*end
)) end
++;
2676 while (next
< name
+ name_len
&& IS_SEPARATOR(*next
)) next
++;
2677 name_len
-= next
- name
;
2679 /* grow the buffer if needed */
2681 if (unix_len
- pos
< MAX_DIR_ENTRY_LEN
+ 2)
2684 unix_len
+= 2 * MAX_DIR_ENTRY_LEN
;
2685 if (!(new_name
= RtlReAllocateHeap( GetProcessHeap(), 0, unix_name
, unix_len
)))
2686 return STATUS_NO_MEMORY
;
2687 unix_name
= *buffer
= new_name
;
2690 status
= find_file_in_dir( unix_name
, pos
, name
, end
- name
,
2691 check_case
, redirect
? &is_win_dir
: NULL
);
2693 /* if this is the last element, not finding it is not necessarily fatal */
2696 if (status
== STATUS_OBJECT_PATH_NOT_FOUND
)
2698 status
= STATUS_OBJECT_NAME_NOT_FOUND
;
2699 if (disposition
!= FILE_OPEN
&& disposition
!= FILE_OVERWRITE
)
2701 ret
= ntdll_wcstoumbs( 0, name
, end
- name
, unix_name
+ pos
+ 1,
2702 MAX_DIR_ENTRY_LEN
, NULL
, &used_default
);
2703 if (ret
> 0 && !used_default
)
2705 unix_name
[pos
] = '/';
2706 unix_name
[pos
+ 1 + ret
] = 0;
2707 status
= STATUS_NO_SUCH_FILE
;
2712 else if (status
== STATUS_SUCCESS
&& disposition
== FILE_CREATE
)
2714 status
= STATUS_OBJECT_NAME_COLLISION
;
2718 if (status
!= STATUS_SUCCESS
) break;
2720 pos
+= strlen( unix_name
+ pos
);
2723 if (is_win_dir
&& (len
= get_redirect_path( unix_name
, pos
, name
, name_len
, check_case
)))
2727 pos
+= strlen( unix_name
+ pos
);
2728 TRACE( "redirecting -> %s + %s\n", debugstr_a(unix_name
), debugstr_w(name
) );
2736 /******************************************************************************
2737 * nt_to_unix_file_name_attr
2739 NTSTATUS
nt_to_unix_file_name_attr( const OBJECT_ATTRIBUTES
*attr
, ANSI_STRING
*unix_name_ret
,
2742 static const WCHAR invalid_charsW
[] = { INVALID_NT_CHARS
, 0 };
2743 enum server_fd_type type
;
2744 int old_cwd
, root_fd
, needs_close
;
2745 const WCHAR
*name
, *p
;
2747 int name_len
, unix_len
;
2749 BOOLEAN check_case
= !(attr
->Attributes
& OBJ_CASE_INSENSITIVE
);
2751 if (!attr
->RootDirectory
) /* without root dir fall back to normal lookup */
2752 return wine_nt_to_unix_file_name( attr
->ObjectName
, unix_name_ret
, disposition
, check_case
);
2754 name
= attr
->ObjectName
->Buffer
;
2755 name_len
= attr
->ObjectName
->Length
/ sizeof(WCHAR
);
2757 if (name_len
&& IS_SEPARATOR(name
[0])) return STATUS_INVALID_PARAMETER
;
2759 /* check for invalid characters */
2760 for (p
= name
; p
< name
+ name_len
; p
++)
2761 if (*p
< 32 || strchrW( invalid_charsW
, *p
)) return STATUS_OBJECT_NAME_INVALID
;
2763 unix_len
= ntdll_wcstoumbs( 0, name
, name_len
, NULL
, 0, NULL
, NULL
);
2764 unix_len
+= MAX_DIR_ENTRY_LEN
+ 3;
2765 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, unix_len
)))
2766 return STATUS_NO_MEMORY
;
2769 if (!(status
= server_get_unix_fd( attr
->RootDirectory
, 0, &root_fd
, &needs_close
, &type
, NULL
)))
2771 if (type
!= FD_TYPE_DIR
)
2773 if (needs_close
) close( root_fd
);
2774 status
= STATUS_BAD_DEVICE_TYPE
;
2778 RtlEnterCriticalSection( &dir_section
);
2779 if ((old_cwd
= open( ".", O_RDONLY
)) != -1 && fchdir( root_fd
) != -1)
2781 status
= lookup_unix_name( name
, name_len
, &unix_name
, unix_len
, 1,
2782 disposition
, check_case
);
2783 if (fchdir( old_cwd
) == -1) chdir( "/" );
2785 else status
= FILE_GetNtStatus();
2786 RtlLeaveCriticalSection( &dir_section
);
2787 if (old_cwd
!= -1) close( old_cwd
);
2788 if (needs_close
) close( root_fd
);
2791 else if (status
== STATUS_OBJECT_TYPE_MISMATCH
) status
= STATUS_BAD_DEVICE_TYPE
;
2793 if (status
== STATUS_SUCCESS
|| status
== STATUS_NO_SUCH_FILE
)
2795 TRACE( "%s -> %s\n", debugstr_us(attr
->ObjectName
), debugstr_a(unix_name
) );
2796 unix_name_ret
->Buffer
= unix_name
;
2797 unix_name_ret
->Length
= strlen(unix_name
);
2798 unix_name_ret
->MaximumLength
= unix_len
;
2802 TRACE( "%s not found in %s\n", debugstr_w(name
), unix_name
);
2803 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2809 /******************************************************************************
2810 * wine_nt_to_unix_file_name (NTDLL.@) Not a Windows API
2812 * Convert a file name from NT namespace to Unix namespace.
2814 * If disposition is not FILE_OPEN or FILE_OVERWRITE, the last path
2815 * element doesn't have to exist; in that case STATUS_NO_SUCH_FILE is
2816 * returned, but the unix name is still filled in properly.
2818 NTSTATUS CDECL
wine_nt_to_unix_file_name( const UNICODE_STRING
*nameW
, ANSI_STRING
*unix_name_ret
,
2819 UINT disposition
, BOOLEAN check_case
)
2821 static const WCHAR unixW
[] = {'u','n','i','x'};
2822 static const WCHAR invalid_charsW
[] = { INVALID_NT_CHARS
, 0 };
2824 NTSTATUS status
= STATUS_SUCCESS
;
2825 const char *config_dir
= wine_get_config_dir();
2826 const WCHAR
*name
, *p
;
2829 int pos
, ret
, name_len
, unix_len
, prefix_len
, used_default
;
2830 WCHAR prefix
[MAX_DIR_ENTRY_LEN
];
2831 BOOLEAN is_unix
= FALSE
;
2833 name
= nameW
->Buffer
;
2834 name_len
= nameW
->Length
/ sizeof(WCHAR
);
2836 if (!name_len
|| !IS_SEPARATOR(name
[0])) return STATUS_OBJECT_PATH_SYNTAX_BAD
;
2838 if (!(pos
= get_dos_prefix_len( nameW
)))
2839 return STATUS_BAD_DEVICE_TYPE
; /* no DOS prefix, assume NT native name */
2844 if (!name_len
) return STATUS_OBJECT_NAME_INVALID
;
2846 /* check for sub-directory */
2847 for (pos
= 0; pos
< name_len
; pos
++)
2849 if (IS_SEPARATOR(name
[pos
])) break;
2850 if (name
[pos
] < 32 || strchrW( invalid_charsW
, name
[pos
] ))
2851 return STATUS_OBJECT_NAME_INVALID
;
2853 if (pos
> MAX_DIR_ENTRY_LEN
)
2854 return STATUS_OBJECT_NAME_INVALID
;
2856 if (pos
== name_len
) /* no subdir, plain DOS device */
2857 return get_dos_device( name
, name_len
, unix_name_ret
);
2859 for (prefix_len
= 0; prefix_len
< pos
; prefix_len
++)
2860 prefix
[prefix_len
] = tolowerW(name
[prefix_len
]);
2863 name_len
-= prefix_len
;
2865 /* check for invalid characters (all chars except 0 are valid for unix) */
2866 is_unix
= (prefix_len
== 4 && !memcmp( prefix
, unixW
, sizeof(unixW
) ));
2869 for (p
= name
; p
< name
+ name_len
; p
++)
2870 if (!*p
) return STATUS_OBJECT_NAME_INVALID
;
2875 for (p
= name
; p
< name
+ name_len
; p
++)
2876 if (*p
< 32 || strchrW( invalid_charsW
, *p
)) return STATUS_OBJECT_NAME_INVALID
;
2879 unix_len
= ntdll_wcstoumbs( 0, prefix
, prefix_len
, NULL
, 0, NULL
, NULL
);
2880 unix_len
+= ntdll_wcstoumbs( 0, name
, name_len
, NULL
, 0, NULL
, NULL
);
2881 unix_len
+= MAX_DIR_ENTRY_LEN
+ 3;
2882 unix_len
+= strlen(config_dir
) + sizeof("/dosdevices/");
2883 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, unix_len
)))
2884 return STATUS_NO_MEMORY
;
2885 strcpy( unix_name
, config_dir
);
2886 strcat( unix_name
, "/dosdevices/" );
2887 pos
= strlen(unix_name
);
2889 ret
= ntdll_wcstoumbs( 0, prefix
, prefix_len
, unix_name
+ pos
, unix_len
- pos
- 1,
2890 NULL
, &used_default
);
2891 if (!ret
|| used_default
)
2893 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2894 return STATUS_OBJECT_NAME_INVALID
;
2898 /* check if prefix exists (except for DOS drives to avoid extra stat calls) */
2900 if (prefix_len
!= 2 || prefix
[1] != ':')
2903 if (lstat( unix_name
, &st
) == -1 && errno
== ENOENT
)
2907 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2908 return STATUS_BAD_DEVICE_TYPE
;
2910 pos
= 0; /* fall back to unix root */
2914 status
= lookup_unix_name( name
, name_len
, &unix_name
, unix_len
, pos
, disposition
, check_case
);
2915 if (status
== STATUS_SUCCESS
|| status
== STATUS_NO_SUCH_FILE
)
2917 TRACE( "%s -> %s\n", debugstr_us(nameW
), debugstr_a(unix_name
) );
2918 unix_name_ret
->Buffer
= unix_name
;
2919 unix_name_ret
->Length
= strlen(unix_name
);
2920 unix_name_ret
->MaximumLength
= unix_len
;
2924 TRACE( "%s not found in %s\n", debugstr_w(name
), unix_name
);
2925 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2931 /******************************************************************
2932 * RtlWow64EnableFsRedirection (NTDLL.@)
2934 NTSTATUS WINAPI
RtlWow64EnableFsRedirection( BOOLEAN enable
)
2936 if (!is_wow64
) return STATUS_NOT_IMPLEMENTED
;
2937 ntdll_get_thread_data()->wow64_redir
= enable
;
2938 return STATUS_SUCCESS
;
2942 /******************************************************************
2943 * RtlWow64EnableFsRedirectionEx (NTDLL.@)
2945 NTSTATUS WINAPI
RtlWow64EnableFsRedirectionEx( ULONG disable
, ULONG
*old_value
)
2947 if (!is_wow64
) return STATUS_NOT_IMPLEMENTED
;
2951 *old_value
= !ntdll_get_thread_data()->wow64_redir
;
2955 return STATUS_ACCESS_VIOLATION
;
2959 ntdll_get_thread_data()->wow64_redir
= !disable
;
2960 return STATUS_SUCCESS
;
2964 /******************************************************************
2965 * RtlDoesFileExists_U (NTDLL.@)
2967 BOOLEAN WINAPI
RtlDoesFileExists_U(LPCWSTR file_name
)
2969 UNICODE_STRING nt_name
;
2970 FILE_BASIC_INFORMATION basic_info
;
2971 OBJECT_ATTRIBUTES attr
;
2974 if (!RtlDosPathNameToNtPathName_U( file_name
, &nt_name
, NULL
, NULL
)) return FALSE
;
2976 attr
.Length
= sizeof(attr
);
2977 attr
.RootDirectory
= 0;
2978 attr
.ObjectName
= &nt_name
;
2979 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2980 attr
.SecurityDescriptor
= NULL
;
2981 attr
.SecurityQualityOfService
= NULL
;
2983 ret
= NtQueryAttributesFile(&attr
, &basic_info
) == STATUS_SUCCESS
;
2985 RtlFreeUnicodeString( &nt_name
);
2990 /***********************************************************************
2991 * DIR_unmount_device
2993 * Unmount the specified device.
2995 NTSTATUS
DIR_unmount_device( HANDLE handle
)
2998 int unix_fd
, needs_close
;
3000 if (!(status
= server_get_unix_fd( handle
, 0, &unix_fd
, &needs_close
, NULL
, NULL
)))
3003 char *mount_point
= NULL
;
3005 if (fstat( unix_fd
, &st
) == -1 || !is_valid_mounted_device( &st
))
3006 status
= STATUS_INVALID_PARAMETER
;
3009 if ((mount_point
= get_device_mount_point( st
.st_rdev
)))
3012 static const char umount
[] = "diskutil unmount >/dev/null 2>&1 ";
3014 static const char umount
[] = "umount >/dev/null 2>&1 ";
3016 char *cmd
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(mount_point
)+sizeof(umount
));
3019 strcpy( cmd
, umount
);
3020 strcat( cmd
, mount_point
);
3022 RtlFreeHeap( GetProcessHeap(), 0, cmd
);
3024 /* umount will fail to release the loop device since we still have
3025 a handle to it, so we release it here */
3026 if (major(st
.st_rdev
) == LOOP_MAJOR
) ioctl( unix_fd
, 0x4c01 /*LOOP_CLR_FD*/, 0 );
3029 RtlFreeHeap( GetProcessHeap(), 0, mount_point
);
3032 if (needs_close
) close( unix_fd
);
3038 /******************************************************************************
3041 * Retrieve the Unix name of the current directory; helper for wine_unix_to_nt_file_name.
3042 * Returned value must be freed by caller.
3044 NTSTATUS
DIR_get_unix_cwd( char **cwd
)
3046 int old_cwd
, unix_fd
, needs_close
;
3051 RtlAcquirePebLock();
3053 if (NtCurrentTeb()->Tib
.SubSystemTib
) /* FIXME: hack */
3054 curdir
= &((WIN16_SUBSYSTEM_TIB
*)NtCurrentTeb()->Tib
.SubSystemTib
)->curdir
;
3056 curdir
= &NtCurrentTeb()->Peb
->ProcessParameters
->CurrentDirectory
;
3058 if (!(handle
= curdir
->Handle
))
3060 UNICODE_STRING dirW
;
3061 OBJECT_ATTRIBUTES attr
;
3064 if (!RtlDosPathNameToNtPathName_U( curdir
->DosPath
.Buffer
, &dirW
, NULL
, NULL
))
3066 status
= STATUS_OBJECT_NAME_INVALID
;
3069 attr
.Length
= sizeof(attr
);
3070 attr
.RootDirectory
= 0;
3071 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
3072 attr
.ObjectName
= &dirW
;
3073 attr
.SecurityDescriptor
= NULL
;
3074 attr
.SecurityQualityOfService
= NULL
;
3076 status
= NtOpenFile( &handle
, SYNCHRONIZE
, &attr
, &io
, 0,
3077 FILE_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
);
3078 RtlFreeUnicodeString( &dirW
);
3079 if (status
!= STATUS_SUCCESS
) goto done
;
3082 if ((status
= server_get_unix_fd( handle
, 0, &unix_fd
, &needs_close
, NULL
, NULL
)) == STATUS_SUCCESS
)
3084 RtlEnterCriticalSection( &dir_section
);
3086 if ((old_cwd
= open(".", O_RDONLY
)) != -1 && fchdir( unix_fd
) != -1)
3088 unsigned int size
= 512;
3092 if (!(*cwd
= RtlAllocateHeap( GetProcessHeap(), 0, size
)))
3094 status
= STATUS_NO_MEMORY
;
3097 if (getcwd( *cwd
, size
)) break;
3098 RtlFreeHeap( GetProcessHeap(), 0, *cwd
);
3099 if (errno
!= ERANGE
)
3101 status
= STATUS_OBJECT_PATH_INVALID
;
3106 if (fchdir( old_cwd
) == -1) chdir( "/" );
3108 else status
= FILE_GetNtStatus();
3110 RtlLeaveCriticalSection( &dir_section
);
3111 if (old_cwd
!= -1) close( old_cwd
);
3112 if (needs_close
) close( unix_fd
);
3114 if (!curdir
->Handle
) NtClose( handle
);
3117 RtlReleasePebLock();