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 UNICODE_STRING system_dir
= { 0, 0, NULL
}; /* system directory */
210 static struct file_identity windir
;
212 static RTL_CRITICAL_SECTION dir_section
;
213 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
216 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
217 0, 0, { (DWORD_PTR
)(__FILE__
": dir_section") }
219 static RTL_CRITICAL_SECTION dir_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
222 /* check if a given Unicode char is OK in a DOS short name */
223 static inline BOOL
is_invalid_dos_char( WCHAR ch
)
225 static const WCHAR invalid_chars
[] = { INVALID_DOS_CHARS
,'~','.',0 };
226 if (ch
> 0x7f) return TRUE
;
227 return strchrW( invalid_chars
, ch
) != NULL
;
230 /* check if the device can be a mounted volume */
231 static inline BOOL
is_valid_mounted_device( const struct stat
*st
)
233 #if defined(linux) || defined(__sun__)
234 return S_ISBLK( st
->st_mode
);
236 /* disks are char devices on *BSD */
237 return S_ISCHR( st
->st_mode
);
241 static inline void ignore_file( const char *name
)
244 assert( ignored_files_count
< MAX_IGNORED_FILES
);
245 if (!stat( name
, &st
))
247 ignored_files
[ignored_files_count
].dev
= st
.st_dev
;
248 ignored_files
[ignored_files_count
].ino
= st
.st_ino
;
249 ignored_files_count
++;
253 static inline BOOL
is_same_file( const struct file_identity
*file
, const struct stat
*st
)
255 return st
->st_dev
== file
->dev
&& st
->st_ino
== file
->ino
;
258 static inline BOOL
is_ignored_file( const struct stat
*st
)
262 for (i
= 0; i
< ignored_files_count
; i
++)
263 if (is_same_file( &ignored_files
[i
], st
)) return TRUE
;
267 static inline unsigned int dir_info_align( unsigned int len
)
269 return (len
+ 7) & ~7;
272 static inline unsigned int dir_info_size( FILE_INFORMATION_CLASS
class, unsigned int len
)
276 case FileDirectoryInformation
:
277 return offsetof( FILE_DIRECTORY_INFORMATION
, FileName
[len
] );
278 case FileBothDirectoryInformation
:
279 return offsetof( FILE_BOTH_DIRECTORY_INFORMATION
, FileName
[len
] );
280 case FileFullDirectoryInformation
:
281 return offsetof( FILE_FULL_DIRECTORY_INFORMATION
, FileName
[len
] );
282 case FileIdBothDirectoryInformation
:
283 return offsetof( FILE_ID_BOTH_DIRECTORY_INFORMATION
, FileName
[len
] );
284 case FileIdFullDirectoryInformation
:
285 return offsetof( FILE_ID_FULL_DIRECTORY_INFORMATION
, FileName
[len
] );
286 case FileIdGlobalTxDirectoryInformation
:
287 return offsetof( FILE_ID_GLOBAL_TX_DIR_INFORMATION
, FileName
[len
] );
288 case FileNamesInformation
:
289 return offsetof( FILE_NAMES_INFORMATION
, FileName
[len
] );
296 static inline BOOL
has_wildcard( const UNICODE_STRING
*mask
)
299 memchrW( mask
->Buffer
, '*', mask
->Length
/ sizeof(WCHAR
) ) ||
300 memchrW( mask
->Buffer
, '?', mask
->Length
/ sizeof(WCHAR
) ));
303 /* get space from the current directory data buffer, allocating a new one if necessary */
304 static void *get_dir_data_space( struct dir_data
*data
, unsigned int size
)
306 struct dir_data_buffer
*buffer
= data
->buffer
;
309 if (!buffer
|| size
> buffer
->size
- buffer
->pos
)
311 unsigned int new_size
= buffer
? buffer
->size
* 2 : dir_data_buffer_initial_size
;
312 if (new_size
< size
) new_size
= size
;
313 if (!(buffer
= RtlAllocateHeap( GetProcessHeap(), 0,
314 offsetof( struct dir_data_buffer
, data
[new_size
] ) ))) return NULL
;
316 buffer
->size
= new_size
;
317 buffer
->next
= data
->buffer
;
318 data
->buffer
= buffer
;
320 ret
= buffer
->data
+ buffer
->pos
;
325 /* add a string to the directory data buffer */
326 static const char *add_dir_data_nameA( struct dir_data
*data
, const char *name
)
328 /* keep buffer data WCHAR-aligned */
329 char *ptr
= get_dir_data_space( data
, (strlen( name
) + sizeof(WCHAR
)) & ~(sizeof(WCHAR
) - 1) );
330 if (ptr
) strcpy( ptr
, name
);
334 /* add a Unicode string to the directory data buffer */
335 static const WCHAR
*add_dir_data_nameW( struct dir_data
*data
, const WCHAR
*name
)
337 WCHAR
*ptr
= get_dir_data_space( data
, (strlenW( name
) + 1) * sizeof(WCHAR
) );
338 if (ptr
) strcpyW( ptr
, name
);
342 /* add an entry to the directory names array */
343 static BOOL
add_dir_data_names( struct dir_data
*data
, const WCHAR
*long_name
,
344 const WCHAR
*short_name
, const char *unix_name
)
346 static const WCHAR empty
[1];
347 struct dir_data_names
*names
= data
->names
;
349 if (data
->count
>= data
->size
)
351 unsigned int new_size
= max( data
->size
* 2, dir_data_names_initial_size
);
353 if (names
) names
= RtlReAllocateHeap( GetProcessHeap(), 0, names
, new_size
* sizeof(*names
) );
354 else names
= RtlAllocateHeap( GetProcessHeap(), 0, new_size
* sizeof(*names
) );
355 if (!names
) return FALSE
;
356 data
->size
= new_size
;
362 if (!(names
[data
->count
].short_name
= add_dir_data_nameW( data
, short_name
))) return FALSE
;
364 else names
[data
->count
].short_name
= empty
;
366 if (!(names
[data
->count
].long_name
= add_dir_data_nameW( data
, long_name
))) return FALSE
;
367 if (!(names
[data
->count
].unix_name
= add_dir_data_nameA( data
, unix_name
))) return FALSE
;
372 /* free the complete directory data structure */
373 static void free_dir_data( struct dir_data
*data
)
375 struct dir_data_buffer
*buffer
, *next
;
379 for (buffer
= data
->buffer
; buffer
; buffer
= next
)
382 RtlFreeHeap( GetProcessHeap(), 0, buffer
);
384 RtlFreeHeap( GetProcessHeap(), 0, data
->names
);
385 RtlFreeHeap( GetProcessHeap(), 0, data
);
389 /* support for a directory queue for filesystem searches */
397 static struct list dir_queue
= LIST_INIT( dir_queue
);
399 static NTSTATUS
add_dir_to_queue( const char *name
)
401 int len
= strlen( name
) + 1;
402 struct dir_name
*dir
= RtlAllocateHeap( GetProcessHeap(), 0,
403 FIELD_OFFSET( struct dir_name
, name
[len
] ));
404 if (!dir
) return STATUS_NO_MEMORY
;
405 strcpy( dir
->name
, name
);
406 list_add_tail( &dir_queue
, &dir
->entry
);
407 return STATUS_SUCCESS
;
410 static NTSTATUS
next_dir_in_queue( char *name
)
412 struct list
*head
= list_head( &dir_queue
);
415 struct dir_name
*dir
= LIST_ENTRY( head
, struct dir_name
, entry
);
416 strcpy( name
, dir
->name
);
417 list_remove( &dir
->entry
);
418 RtlFreeHeap( GetProcessHeap(), 0, dir
);
419 return STATUS_SUCCESS
;
421 return STATUS_OBJECT_NAME_NOT_FOUND
;
424 static void flush_dir_queue(void)
428 while ((head
= list_head( &dir_queue
)))
430 struct dir_name
*dir
= LIST_ENTRY( head
, struct dir_name
, entry
);
431 list_remove( &dir
->entry
);
432 RtlFreeHeap( GetProcessHeap(), 0, dir
);
437 /***********************************************************************
438 * get_default_com_device
440 * Return the default device to use for serial ports.
442 static char *get_default_com_device( int num
)
446 if (num
< 1 || num
> 256) return NULL
;
448 ret
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/ttyS256") );
449 if (!ret
) return NULL
;
450 sprintf( ret
, "/dev/ttyS%d", num
- 1 );
451 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
452 ret
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/cuau256") );
453 if (!ret
) return NULL
;
454 sprintf( ret
, "/dev/cuau%d", num
- 1 );
455 #elif defined(__DragonFly__)
456 ret
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/cuaa256") );
457 if (!ret
) return NULL
;
458 sprintf( ret
, "/dev/cuaa%d", num
- 1 );
460 FIXME( "no known default for device com%d\n", num
);
466 /***********************************************************************
467 * get_default_lpt_device
469 * Return the default device to use for parallel ports.
471 static char *get_default_lpt_device( int num
)
475 if (num
< 1 || num
> 256) return NULL
;
477 ret
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/lp256") );
478 if (!ret
) return NULL
;
479 sprintf( ret
, "/dev/lp%d", num
- 1 );
481 FIXME( "no known default for device lpt%d\n", num
);
488 static char *unescape_field( char *str
)
492 for (in
= out
= str
; *in
; in
++, out
++)
502 else if (in
[1] == '0' && in
[2] == '4' && in
[3] == '0')
507 else if (in
[1] == '0' && in
[2] == '1' && in
[3] == '1')
512 else if (in
[1] == '0' && in
[2] == '1' && in
[3] == '2')
517 else if (in
[1] == '1' && in
[2] == '3' && in
[3] == '4')
529 static inline char *get_field( char **str
)
533 ret
= strsep( str
, " \t" );
534 if (*str
) *str
+= strspn( *str
, " \t" );
538 /************************************************************************
539 * getmntent_replacement
541 * getmntent replacement for Android.
543 * NB returned static buffer is not thread safe; protect with dir_section.
545 static struct mntent
*getmntent_replacement( FILE *f
)
547 static struct mntent entry
;
548 static char buf
[4096];
553 if (!fgets( buf
, sizeof(buf
), f
)) return NULL
;
554 p
= strchr( buf
, '\n' );
556 else /* Partially unread line, move file ptr to end */
559 while (fgets( tmp
, sizeof(tmp
), f
))
560 if (strchr( tmp
, '\n' )) break;
562 start
= buf
+ strspn( buf
, " \t" );
563 } while (start
[0] == '\0' || start
[0] == '#');
565 p
= get_field( &start
);
566 entry
.mnt_fsname
= p
? unescape_field( p
) : (char *)"";
568 p
= get_field( &start
);
569 entry
.mnt_dir
= p
? unescape_field( p
) : (char *)"";
571 p
= get_field( &start
);
572 entry
.mnt_type
= p
? unescape_field( p
) : (char *)"";
574 p
= get_field( &start
);
575 entry
.mnt_opts
= p
? unescape_field( p
) : (char *)"";
577 p
= get_field( &start
);
578 entry
.mnt_freq
= p
? atoi(p
) : 0;
580 p
= get_field( &start
);
581 entry
.mnt_passno
= p
? atoi(p
) : 0;
585 #define getmntent getmntent_replacement
588 /***********************************************************************
589 * DIR_get_drives_info
591 * Retrieve device/inode number for all the drives. Helper for find_drive_root.
593 unsigned int DIR_get_drives_info( struct drive_info info
[MAX_DOS_DRIVES
] )
595 static struct drive_info cache
[MAX_DOS_DRIVES
];
596 static time_t last_update
;
597 static unsigned int nb_drives
;
599 time_t now
= time(NULL
);
601 RtlEnterCriticalSection( &dir_section
);
602 if (now
!= last_update
)
604 const char *config_dir
= wine_get_config_dir();
609 if ((buffer
= RtlAllocateHeap( GetProcessHeap(), 0,
610 strlen(config_dir
) + sizeof("/dosdevices/a:") )))
612 strcpy( buffer
, config_dir
);
613 strcat( buffer
, "/dosdevices/a:" );
614 p
= buffer
+ strlen(buffer
) - 2;
616 for (i
= nb_drives
= 0; i
< MAX_DOS_DRIVES
; i
++)
619 if (!stat( buffer
, &st
))
621 cache
[i
].dev
= st
.st_dev
;
622 cache
[i
].ino
= st
.st_ino
;
631 RtlFreeHeap( GetProcessHeap(), 0, buffer
);
635 memcpy( info
, cache
, sizeof(cache
) );
637 RtlLeaveCriticalSection( &dir_section
);
642 /***********************************************************************
643 * parse_mount_entries
645 * Parse mount entries looking for a given device. Helper for get_default_drive_device.
649 #include <sys/vfstab.h>
650 static char *parse_vfstab_entries( FILE *f
, dev_t dev
, ino_t ino
)
656 while (! getvfsent( f
, &entry
))
658 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
659 if (!strcmp( entry
.vfs_fstype
, "nfs" ) ||
660 !strcmp( entry
.vfs_fstype
, "smbfs" ) ||
661 !strcmp( entry
.vfs_fstype
, "ncpfs" )) continue;
663 if (stat( entry
.vfs_mountp
, &st
) == -1) continue;
664 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
665 if (!strcmp( entry
.vfs_fstype
, "fd" ))
667 if ((device
= strstr( entry
.vfs_mntopts
, "dev=" )))
669 char *p
= strchr( device
+ 4, ',' );
675 return entry
.vfs_special
;
682 static char *parse_mount_entries( FILE *f
, dev_t dev
, ino_t ino
)
684 struct mntent
*entry
;
688 while ((entry
= getmntent( f
)))
690 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
691 if (!strcmp( entry
->mnt_type
, "nfs" ) ||
692 !strcmp( entry
->mnt_type
, "smbfs" ) ||
693 !strcmp( entry
->mnt_type
, "ncpfs" )) continue;
695 if (stat( entry
->mnt_dir
, &st
) == -1) continue;
696 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
697 if (!strcmp( entry
->mnt_type
, "supermount" ))
699 if ((device
= strstr( entry
->mnt_opts
, "dev=" )))
701 char *p
= strchr( device
+ 4, ',' );
706 else if (!stat( entry
->mnt_fsname
, &st
) && S_ISREG(st
.st_mode
))
708 /* if device is a regular file check for a loop mount */
709 if ((device
= strstr( entry
->mnt_opts
, "loop=" )))
711 char *p
= strchr( device
+ 5, ',' );
717 return entry
->mnt_fsname
;
723 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
725 static char *parse_mount_entries( FILE *f
, dev_t dev
, ino_t ino
)
730 while ((entry
= getfsent()))
732 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
733 if (!strcmp( entry
->fs_vfstype
, "nfs" ) ||
734 !strcmp( entry
->fs_vfstype
, "smbfs" ) ||
735 !strcmp( entry
->fs_vfstype
, "ncpfs" )) continue;
737 if (stat( entry
->fs_file
, &st
) == -1) continue;
738 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
739 return entry
->fs_spec
;
746 #include <sys/mnttab.h>
747 static char *parse_mount_entries( FILE *f
, dev_t dev
, ino_t ino
)
754 while (( ! getmntent( f
, &entry
) ))
756 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
757 if (!strcmp( entry
.mnt_fstype
, "nfs" ) ||
758 !strcmp( entry
.mnt_fstype
, "smbfs" ) ||
759 !strcmp( entry
.mnt_fstype
, "ncpfs" )) continue;
761 if (stat( entry
.mnt_mountp
, &st
) == -1) continue;
762 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
763 if (!strcmp( entry
.mnt_fstype
, "fd" ))
765 if ((device
= strstr( entry
.mnt_mntopts
, "dev=" )))
767 char *p
= strchr( device
+ 4, ',' );
773 return entry
.mnt_special
;
779 /***********************************************************************
780 * get_default_drive_device
782 * Return the default device to use for a given drive mount point.
784 static char *get_default_drive_device( const char *root
)
794 /* try to open it first to force it to get mounted */
795 if ((fd
= open( root
, O_RDONLY
| O_DIRECTORY
)) != -1)
797 res
= fstat( fd
, &st
);
800 /* now try normal stat just in case */
801 if (res
== -1) res
= stat( root
, &st
);
802 if (res
== -1) return NULL
;
804 RtlEnterCriticalSection( &dir_section
);
807 if ((f
= fopen( "/proc/mounts", "r" )))
809 device
= parse_mount_entries( f
, st
.st_dev
, st
.st_ino
);
813 if ((f
= fopen( "/etc/mtab", "r" )))
815 device
= parse_mount_entries( f
, st
.st_dev
, st
.st_ino
);
818 /* look through fstab too in case it's not mounted (for instance if it's an audio CD) */
819 if (!device
&& (f
= fopen( "/etc/fstab", "r" )))
821 device
= parse_mount_entries( f
, st
.st_dev
, st
.st_ino
);
827 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(device
) + 1 );
828 if (ret
) strcpy( ret
, device
);
830 RtlLeaveCriticalSection( &dir_section
);
832 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__ ) || defined(__DragonFly__)
837 /* try to open it first to force it to get mounted */
838 if ((fd
= open( root
, O_RDONLY
)) != -1)
840 res
= fstat( fd
, &st
);
843 /* now try normal stat just in case */
844 if (res
== -1) res
= stat( root
, &st
);
845 if (res
== -1) return NULL
;
847 RtlEnterCriticalSection( &dir_section
);
849 /* The FreeBSD parse_mount_entries doesn't require a file argument, so just
850 * pass NULL. Leave the argument in for symmetry.
852 device
= parse_mount_entries( NULL
, st
.st_dev
, st
.st_ino
);
855 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(device
) + 1 );
856 if (ret
) strcpy( ret
, device
);
858 RtlLeaveCriticalSection( &dir_section
);
866 /* try to open it first to force it to get mounted */
867 if ((fd
= open( root
, O_RDONLY
)) != -1)
869 res
= fstat( fd
, &st
);
872 /* now try normal stat just in case */
873 if (res
== -1) res
= stat( root
, &st
);
874 if (res
== -1) return NULL
;
876 RtlEnterCriticalSection( &dir_section
);
878 if ((f
= fopen( "/etc/mnttab", "r" )))
880 device
= parse_mount_entries( f
, st
.st_dev
, st
.st_ino
);
883 /* look through fstab too in case it's not mounted (for instance if it's an audio CD) */
884 if (!device
&& (f
= fopen( "/etc/vfstab", "r" )))
886 device
= parse_vfstab_entries( f
, st
.st_dev
, st
.st_ino
);
891 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(device
) + 1 );
892 if (ret
) strcpy( ret
, device
);
894 RtlLeaveCriticalSection( &dir_section
);
896 #elif defined(__APPLE__)
897 struct statfs
*mntStat
;
903 static const char path_bsd_device
[] = "/dev/disk";
906 res
= stat( root
, &st
);
907 if (res
== -1) return NULL
;
912 RtlEnterCriticalSection( &dir_section
);
914 mntSize
= getmntinfo(&mntStat
, MNT_NOWAIT
);
916 for (i
= 0; i
< mntSize
&& !ret
; i
++)
918 if (stat(mntStat
[i
].f_mntonname
, &st
) == -1) continue;
919 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
921 /* FIXME add support for mounted network drive */
922 if ( strncmp(mntStat
[i
].f_mntfromname
, path_bsd_device
, strlen(path_bsd_device
)) == 0)
924 /* set return value to the corresponding raw BSD node */
925 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(mntStat
[i
].f_mntfromname
) + 2 /* 2 : r and \0 */ );
928 strcpy(ret
, "/dev/r");
929 strcat(ret
, mntStat
[i
].f_mntfromname
+sizeof("/dev/")-1);
933 RtlLeaveCriticalSection( &dir_section
);
936 if (!warned
++) FIXME( "auto detection of DOS devices not supported on this platform\n" );
942 /***********************************************************************
943 * get_device_mount_point
945 * Return the current mount point for a device.
947 static char *get_device_mount_point( dev_t dev
)
954 RtlEnterCriticalSection( &dir_section
);
957 if ((f
= fopen( "/proc/mounts", "r" )))
959 if ((f
= fopen( "/etc/mtab", "r" )))
962 struct mntent
*entry
;
966 while ((entry
= getmntent( f
)))
968 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
969 if (!strcmp( entry
->mnt_type
, "nfs" ) ||
970 !strcmp( entry
->mnt_type
, "smbfs" ) ||
971 !strcmp( entry
->mnt_type
, "ncpfs" )) continue;
973 if (!strcmp( entry
->mnt_type
, "supermount" ))
975 if ((device
= strstr( entry
->mnt_opts
, "dev=" )))
978 if ((p
= strchr( device
, ',' ))) *p
= 0;
981 else if (!stat( entry
->mnt_fsname
, &st
) && S_ISREG(st
.st_mode
))
983 /* if device is a regular file check for a loop mount */
984 if ((device
= strstr( entry
->mnt_opts
, "loop=" )))
987 if ((p
= strchr( device
, ',' ))) *p
= 0;
990 else device
= entry
->mnt_fsname
;
992 if (device
&& !stat( device
, &st
) && S_ISBLK(st
.st_mode
) && st
.st_rdev
== dev
)
994 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(entry
->mnt_dir
) + 1 );
995 if (ret
) strcpy( ret
, entry
->mnt_dir
);
1001 RtlLeaveCriticalSection( &dir_section
);
1002 #elif defined(__APPLE__)
1003 struct statfs
*entry
;
1007 RtlEnterCriticalSection( &dir_section
);
1009 size
= getmntinfo( &entry
, MNT_NOWAIT
);
1010 for (i
= 0; i
< size
; i
++)
1012 if (stat( entry
[i
].f_mntfromname
, &st
) == -1) continue;
1013 if (S_ISBLK(st
.st_mode
) && st
.st_rdev
== dev
)
1015 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(entry
[i
].f_mntonname
) + 1 );
1016 if (ret
) strcpy( ret
, entry
[i
].f_mntonname
);
1020 RtlLeaveCriticalSection( &dir_section
);
1023 if (!warned
++) FIXME( "unmounting devices not supported on this platform\n" );
1029 #if defined(HAVE_GETATTRLIST) && defined(ATTR_VOL_CAPABILITIES) && \
1030 defined(VOL_CAPABILITIES_FORMAT) && defined(VOL_CAP_FMT_CASE_SENSITIVE)
1043 BOOLEAN case_sensitive
;
1049 vol_capabilities_attr_t caps
;
1052 /***********************************************************************
1055 * Checks if the specified file system is in the cache.
1057 static struct fs_cache
*look_up_fs_cache( dev_t dev
)
1060 for (i
= 0; i
< sizeof(fs_cache
)/sizeof(fs_cache
[0]); i
++)
1061 if (fs_cache
[i
].dev
== dev
)
1066 /***********************************************************************
1069 * Adds the specified file system to the cache.
1071 static void add_fs_cache( dev_t dev
, fsid_t fsid
, BOOLEAN case_sensitive
)
1074 struct fs_cache
*entry
= look_up_fs_cache( dev
);
1075 static int once
= 0;
1078 /* Update the cache */
1080 entry
->case_sensitive
= case_sensitive
;
1084 /* Add a new entry */
1085 for (i
= 0; i
< sizeof(fs_cache
)/sizeof(fs_cache
[0]); i
++)
1086 if (fs_cache
[i
].dev
== 0)
1088 /* This entry is empty, use it */
1089 fs_cache
[i
].dev
= dev
;
1090 fs_cache
[i
].fsid
= fsid
;
1091 fs_cache
[i
].case_sensitive
= case_sensitive
;
1095 /* Cache is out of space, warn */
1097 WARN( "FS cache is out of space, expect performance problems\n" );
1100 /***********************************************************************
1101 * get_dir_case_sensitivity_attr
1103 * Checks if the volume containing the specified directory is case
1104 * sensitive or not. Uses getattrlist(2).
1106 static int get_dir_case_sensitivity_attr( const char *dir
)
1109 struct attrlist attr
;
1110 struct vol_caps caps
;
1111 struct get_fsid get_fsid
;
1112 struct fs_cache
*entry
;
1114 /* First get the FS ID of the volume */
1115 attr
.bitmapcount
= ATTR_BIT_MAP_COUNT
;
1117 attr
.commonattr
= ATTR_CMN_DEVID
|ATTR_CMN_FSID
;
1118 attr
.volattr
= attr
.dirattr
= attr
.fileattr
= attr
.forkattr
= 0;
1120 if (getattrlist( dir
, &attr
, &get_fsid
, sizeof(get_fsid
), 0 ) != 0 ||
1121 get_fsid
.size
!= sizeof(get_fsid
))
1123 /* Try to look it up in the cache */
1124 entry
= look_up_fs_cache( get_fsid
.dev
);
1125 if (entry
&& !memcmp( &entry
->fsid
, &get_fsid
.fsid
, sizeof(fsid_t
) ))
1126 /* Cache lookup succeeded */
1127 return entry
->case_sensitive
;
1128 /* Cache is stale at this point, we have to update it */
1130 mntpoint
= get_device_mount_point( get_fsid
.dev
);
1131 /* Now look up the case-sensitivity */
1132 attr
.commonattr
= 0;
1133 attr
.volattr
= ATTR_VOL_INFO
|ATTR_VOL_CAPABILITIES
;
1134 if (getattrlist( mntpoint
, &attr
, &caps
, sizeof(caps
), 0 ) < 0)
1136 RtlFreeHeap( GetProcessHeap(), 0, mntpoint
);
1137 add_fs_cache( get_fsid
.dev
, get_fsid
.fsid
, TRUE
);
1140 RtlFreeHeap( GetProcessHeap(), 0, mntpoint
);
1141 if (caps
.size
== sizeof(caps
) &&
1142 (caps
.caps
.valid
[VOL_CAPABILITIES_FORMAT
] &
1143 (VOL_CAP_FMT_CASE_SENSITIVE
| VOL_CAP_FMT_CASE_PRESERVING
)) ==
1144 (VOL_CAP_FMT_CASE_SENSITIVE
| VOL_CAP_FMT_CASE_PRESERVING
))
1148 if ((caps
.caps
.capabilities
[VOL_CAPABILITIES_FORMAT
] &
1149 VOL_CAP_FMT_CASE_SENSITIVE
) != VOL_CAP_FMT_CASE_SENSITIVE
)
1153 /* Update the cache */
1154 add_fs_cache( get_fsid
.dev
, get_fsid
.fsid
, ret
);
1161 /***********************************************************************
1162 * get_dir_case_sensitivity_stat
1164 * Checks if the volume containing the specified directory is case
1165 * sensitive or not. Uses statfs(2) or statvfs(2).
1167 static BOOLEAN
get_dir_case_sensitivity_stat( const char *dir
)
1169 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1172 if (statfs( dir
, &stfs
) == -1) return FALSE
;
1173 /* Assume these file systems are always case insensitive on Mac OS.
1174 * For FreeBSD, only assume CIOPFS is case insensitive (AFAIK, Mac OS
1175 * is the only UNIX that supports case-insensitive lookup).
1177 if (!strcmp( stfs
.f_fstypename
, "fusefs" ) &&
1178 !strncmp( stfs
.f_mntfromname
, "ciopfs", 5 ))
1181 if (!strcmp( stfs
.f_fstypename
, "msdos" ) ||
1182 !strcmp( stfs
.f_fstypename
, "cd9660" ) ||
1183 !strcmp( stfs
.f_fstypename
, "udf" ) ||
1184 !strcmp( stfs
.f_fstypename
, "ntfs" ) ||
1185 !strcmp( stfs
.f_fstypename
, "smbfs" ))
1187 #ifdef _DARWIN_FEATURE_64_BIT_INODE
1188 if (!strcmp( stfs
.f_fstypename
, "hfs" ) && (stfs
.f_fssubtype
== 0 ||
1189 stfs
.f_fssubtype
== 1 ||
1190 stfs
.f_fssubtype
== 128))
1193 /* The field says "reserved", but a quick look at the kernel source
1194 * tells us that this "reserved" field is really the same as the
1195 * "fssubtype" field from the inode64 structure (see munge_statfs()
1196 * in <xnu-source>/bsd/vfs/vfs_syscalls.c).
1198 if (!strcmp( stfs
.f_fstypename
, "hfs" ) && (stfs
.f_reserved1
== 0 ||
1199 stfs
.f_reserved1
== 1 ||
1200 stfs
.f_reserved1
== 128))
1206 #elif defined(__NetBSD__)
1207 struct statvfs stfs
;
1209 if (statvfs( dir
, &stfs
) == -1) return FALSE
;
1210 /* Only assume CIOPFS is case insensitive. */
1211 if (strcmp( stfs
.f_fstypename
, "fusefs" ) ||
1212 strncmp( stfs
.f_mntfromname
, "ciopfs", 5 ))
1216 #elif defined(__linux__)
1221 /* Only assume CIOPFS is case insensitive. */
1222 if (statfs( dir
, &stfs
) == -1) return FALSE
;
1223 if (stfs
.f_type
!= 0x65735546 /* FUSE_SUPER_MAGIC */)
1225 /* Normally, we'd have to parse the mtab to find out exactly what
1226 * kind of FUSE FS this is. But, someone on wine-devel suggested
1227 * a shortcut. We'll stat a special file in the directory. If it's
1228 * there, we'll assume it's a CIOPFS, else not.
1229 * This will break if somebody puts a file named ".ciopfs" in a non-
1232 cifile
= RtlAllocateHeap( GetProcessHeap(), 0, strlen( dir
)+sizeof("/.ciopfs") );
1233 if (!cifile
) return TRUE
;
1234 strcpy( cifile
, dir
);
1235 strcat( cifile
, "/.ciopfs" );
1236 if (stat( cifile
, &st
) == 0)
1238 RtlFreeHeap( GetProcessHeap(), 0, cifile
);
1241 RtlFreeHeap( GetProcessHeap(), 0, cifile
);
1249 /***********************************************************************
1250 * get_dir_case_sensitivity
1252 * Checks if the volume containing the specified directory is case
1253 * sensitive or not. Uses statfs(2) or statvfs(2).
1255 static BOOLEAN
get_dir_case_sensitivity( const char *dir
)
1257 #if defined(HAVE_GETATTRLIST) && defined(ATTR_VOL_CAPABILITIES) && \
1258 defined(VOL_CAPABILITIES_FORMAT) && defined(VOL_CAP_FMT_CASE_SENSITIVE)
1259 int case_sensitive
= get_dir_case_sensitivity_attr( dir
);
1260 if (case_sensitive
!= -1) return case_sensitive
;
1262 return get_dir_case_sensitivity_stat( dir
);
1266 /***********************************************************************
1269 * Initialize the show_dot_files options.
1271 static DWORD WINAPI
init_options( RTL_RUN_ONCE
*once
, void *param
, void **context
)
1273 static const WCHAR WineW
[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e',0};
1274 static const WCHAR ShowDotFilesW
[] = {'S','h','o','w','D','o','t','F','i','l','e','s',0};
1278 OBJECT_ATTRIBUTES attr
;
1279 UNICODE_STRING nameW
;
1281 RtlOpenCurrentUser( KEY_ALL_ACCESS
, &root
);
1282 attr
.Length
= sizeof(attr
);
1283 attr
.RootDirectory
= root
;
1284 attr
.ObjectName
= &nameW
;
1285 attr
.Attributes
= 0;
1286 attr
.SecurityDescriptor
= NULL
;
1287 attr
.SecurityQualityOfService
= NULL
;
1288 RtlInitUnicodeString( &nameW
, WineW
);
1290 /* @@ Wine registry key: HKCU\Software\Wine */
1291 if (!NtOpenKey( &hkey
, KEY_ALL_ACCESS
, &attr
))
1293 RtlInitUnicodeString( &nameW
, ShowDotFilesW
);
1294 if (!NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
, tmp
, sizeof(tmp
), &dummy
))
1296 WCHAR
*str
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)tmp
)->Data
;
1297 show_dot_files
= IS_OPTION_TRUE( str
[0] );
1303 /* a couple of directories that we don't want to return in directory searches */
1304 ignore_file( wine_get_config_dir() );
1305 ignore_file( "/dev" );
1306 ignore_file( "/proc" );
1308 ignore_file( "/sys" );
1314 /***********************************************************************
1315 * DIR_is_hidden_file
1317 * Check if the specified file should be hidden based on its name and the show dot files option.
1319 BOOL
DIR_is_hidden_file( const UNICODE_STRING
*name
)
1323 RtlRunOnceExecuteOnce( &init_once
, init_options
, NULL
, NULL
);
1325 if (show_dot_files
) return FALSE
;
1327 end
= p
= name
->Buffer
+ name
->Length
/sizeof(WCHAR
);
1328 while (p
> name
->Buffer
&& IS_SEPARATOR(p
[-1])) p
--;
1329 while (p
> name
->Buffer
&& !IS_SEPARATOR(p
[-1])) p
--;
1330 if (p
== end
|| *p
!= '.') return FALSE
;
1331 /* make sure it isn't '.' or '..' */
1332 if (p
+ 1 == end
) return FALSE
;
1333 if (p
[1] == '.' && p
+ 2 == end
) return FALSE
;
1338 /***********************************************************************
1339 * hash_short_file_name
1341 * Transform a Unix file name into a hashed DOS name. If the name is a valid
1342 * DOS name, it is converted to upper-case; otherwise it is replaced by a
1343 * hashed version that fits in 8.3 format.
1344 * 'buffer' must be at least 12 characters long.
1345 * Returns length of short name in bytes; short name is NOT null-terminated.
1347 static ULONG
hash_short_file_name( const UNICODE_STRING
*name
, LPWSTR buffer
)
1349 static const char hash_chars
[32] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345";
1351 LPCWSTR p
, ext
, end
= name
->Buffer
+ name
->Length
/ sizeof(WCHAR
);
1353 unsigned short hash
;
1356 /* Compute the hash code of the file name */
1357 /* If you know something about hash functions, feel free to */
1358 /* insert a better algorithm here... */
1359 if (!is_case_sensitive
)
1361 for (p
= name
->Buffer
, hash
= 0xbeef; p
< end
- 1; p
++)
1362 hash
= (hash
<<3) ^ (hash
>>5) ^ tolowerW(*p
) ^ (tolowerW(p
[1]) << 8);
1363 hash
= (hash
<<3) ^ (hash
>>5) ^ tolowerW(*p
); /* Last character */
1367 for (p
= name
->Buffer
, hash
= 0xbeef; p
< end
- 1; p
++)
1368 hash
= (hash
<< 3) ^ (hash
>> 5) ^ *p
^ (p
[1] << 8);
1369 hash
= (hash
<< 3) ^ (hash
>> 5) ^ *p
; /* Last character */
1372 /* Find last dot for start of the extension */
1373 for (p
= name
->Buffer
+ 1, ext
= NULL
; p
< end
- 1; p
++) if (*p
== '.') ext
= p
;
1375 /* Copy first 4 chars, replacing invalid chars with '_' */
1376 for (i
= 4, p
= name
->Buffer
, dst
= buffer
; i
> 0; i
--, p
++)
1378 if (p
== end
|| p
== ext
) break;
1379 *dst
++ = is_invalid_dos_char(*p
) ? '_' : toupperW(*p
);
1381 /* Pad to 5 chars with '~' */
1382 while (i
-- >= 0) *dst
++ = '~';
1384 /* Insert hash code converted to 3 ASCII chars */
1385 *dst
++ = hash_chars
[(hash
>> 10) & 0x1f];
1386 *dst
++ = hash_chars
[(hash
>> 5) & 0x1f];
1387 *dst
++ = hash_chars
[hash
& 0x1f];
1389 /* Copy the first 3 chars of the extension (if any) */
1393 for (i
= 3, ext
++; (i
> 0) && ext
< end
; i
--, ext
++)
1394 *dst
++ = is_invalid_dos_char(*ext
) ? '_' : toupperW(*ext
);
1396 return dst
- buffer
;
1400 /***********************************************************************
1403 * Check a long file name against a mask.
1405 * Tests (done in W95 DOS shell - case insensitive):
1406 * *.txt test1.test.txt *
1408 * *.t??????.t* test1.ta.tornado.txt *
1409 * *tornado* test1.ta.tornado.txt *
1410 * t*t test1.ta.tornado.txt *
1412 * ?est??? test1.txt -
1413 * *test1.txt* test1.txt *
1414 * h?l?o*t.dat hellothisisatest.dat *
1416 static BOOLEAN
match_filename( const UNICODE_STRING
*name_str
, const UNICODE_STRING
*mask_str
)
1419 const WCHAR
*name
= name_str
->Buffer
;
1420 const WCHAR
*mask
= mask_str
->Buffer
;
1421 const WCHAR
*name_end
= name
+ name_str
->Length
/ sizeof(WCHAR
);
1422 const WCHAR
*mask_end
= mask
+ mask_str
->Length
/ sizeof(WCHAR
);
1423 const WCHAR
*lastjoker
= NULL
;
1424 const WCHAR
*next_to_retry
= NULL
;
1426 while (name
< name_end
&& mask
< mask_end
)
1432 while (mask
< mask_end
&& *mask
== '*') mask
++; /* Skip consecutive '*' */
1433 if (mask
== mask_end
) return TRUE
; /* end of mask is all '*', so match */
1436 /* skip to the next match after the joker(s) */
1437 if (is_case_sensitive
)
1438 while (name
< name_end
&& (*name
!= *mask
)) name
++;
1440 while (name
< name_end
&& (toupperW(*name
) != toupperW(*mask
))) name
++;
1441 next_to_retry
= name
;
1448 if (is_case_sensitive
) mismatch
= (*mask
!= *name
);
1449 else mismatch
= (toupperW(*mask
) != toupperW(*name
));
1455 if (mask
== mask_end
)
1457 if (name
== name_end
) return TRUE
;
1458 if (lastjoker
) mask
= lastjoker
;
1461 else /* mismatch ! */
1463 if (lastjoker
) /* we had an '*', so we can try unlimitedly */
1467 /* this scan sequence was a mismatch, so restart
1468 * 1 char after the first char we checked last time */
1470 name
= next_to_retry
;
1472 else return FALSE
; /* bad luck */
1477 while (mask
< mask_end
&& ((*mask
== '.') || (*mask
== '*')))
1478 mask
++; /* Ignore trailing '.' or '*' in mask */
1479 return (name
== name_end
&& mask
== mask_end
);
1483 /***********************************************************************
1486 * Add a file to the directory data if it matches the mask.
1488 static BOOL
append_entry( struct dir_data
*data
, const char *long_name
,
1489 const char *short_name
, const UNICODE_STRING
*mask
)
1491 int i
, long_len
, short_len
;
1492 WCHAR long_nameW
[MAX_DIR_ENTRY_LEN
+ 1];
1493 WCHAR short_nameW
[13];
1496 long_len
= ntdll_umbstowcs( 0, long_name
, strlen(long_name
), long_nameW
, MAX_DIR_ENTRY_LEN
);
1497 if (long_len
== -1) return TRUE
;
1498 long_nameW
[long_len
] = 0;
1500 str
.Buffer
= long_nameW
;
1501 str
.Length
= long_len
* sizeof(WCHAR
);
1502 str
.MaximumLength
= sizeof(long_nameW
);
1506 short_len
= ntdll_umbstowcs( 0, short_name
, strlen(short_name
),
1507 short_nameW
, sizeof(short_nameW
) / sizeof(WCHAR
) - 1 );
1508 if (short_len
== -1) short_len
= sizeof(short_nameW
) / sizeof(WCHAR
) - 1;
1509 for (i
= 0; i
< short_len
; i
++) short_nameW
[i
] = toupperW( short_nameW
[i
] );
1511 else /* generate a short name if necessary */
1516 if (!RtlIsNameLegalDOS8Dot3( &str
, NULL
, &spaces
) || spaces
)
1517 short_len
= hash_short_file_name( &str
, short_nameW
);
1519 short_nameW
[short_len
] = 0;
1521 TRACE( "long %s short %s mask %s\n",
1522 debugstr_w( long_nameW
), debugstr_w( short_nameW
), debugstr_us( mask
));
1524 if (mask
&& !match_filename( &str
, mask
))
1526 if (!short_len
) return TRUE
; /* no short name to match */
1527 str
.Buffer
= short_nameW
;
1528 str
.Length
= short_len
* sizeof(WCHAR
);
1529 str
.MaximumLength
= sizeof(short_nameW
);
1530 if (!match_filename( &str
, mask
)) return TRUE
;
1533 return add_dir_data_names( data
, long_nameW
, short_nameW
, long_name
);
1537 /***********************************************************************
1538 * get_dir_data_entry
1540 * Return a directory entry from the cached data.
1542 static NTSTATUS
get_dir_data_entry( struct dir_data
*dir_data
, void *info_ptr
, IO_STATUS_BLOCK
*io
,
1543 ULONG max_length
, FILE_INFORMATION_CLASS
class,
1544 union file_directory_info
**last_info
)
1546 const struct dir_data_names
*names
= &dir_data
->names
[dir_data
->pos
];
1547 union file_directory_info
*info
;
1549 ULONG name_len
, start
, dir_size
, attributes
;
1551 if (get_file_info( names
->unix_name
, &st
, &attributes
) == -1)
1553 TRACE( "file no longer exists %s\n", names
->unix_name
);
1554 return STATUS_SUCCESS
;
1556 if (is_ignored_file( &st
))
1558 TRACE( "ignoring file %s\n", names
->unix_name
);
1559 return STATUS_SUCCESS
;
1561 start
= dir_info_align( io
->Information
);
1562 dir_size
= dir_info_size( class, 0 );
1563 if (start
+ dir_size
> max_length
) return STATUS_MORE_ENTRIES
;
1565 max_length
-= start
+ dir_size
;
1566 name_len
= strlenW( names
->long_name
) * sizeof(WCHAR
);
1567 /* if this is not the first entry, fail; the first entry is always returned (but truncated) */
1568 if (*last_info
&& name_len
> max_length
) return STATUS_MORE_ENTRIES
;
1570 info
= (union file_directory_info
*)((char *)info_ptr
+ start
);
1571 info
->dir
.NextEntryOffset
= 0;
1572 info
->dir
.FileIndex
= 0; /* NTFS always has 0 here, so let's not bother with it */
1574 /* all the structures except FileNamesInformation start with a FileDirectoryInformation layout */
1575 if (class != FileNamesInformation
)
1577 if (st
.st_dev
!= dir_data
->id
.dev
) st
.st_ino
= 0; /* ignore inode if on a different device */
1579 if (!show_dot_files
&& names
->long_name
[0] == '.' && names
->long_name
[1] &&
1580 (names
->long_name
[1] != '.' || names
->long_name
[2]))
1581 attributes
|= FILE_ATTRIBUTE_HIDDEN
;
1583 fill_file_info( &st
, attributes
, info
, class );
1588 case FileDirectoryInformation
:
1589 info
->dir
.FileNameLength
= name_len
;
1592 case FileFullDirectoryInformation
:
1593 info
->full
.EaSize
= 0; /* FIXME */
1594 info
->full
.FileNameLength
= name_len
;
1597 case FileIdFullDirectoryInformation
:
1598 info
->id_full
.EaSize
= 0; /* FIXME */
1599 info
->id_full
.FileNameLength
= name_len
;
1602 case FileBothDirectoryInformation
:
1603 info
->both
.EaSize
= 0; /* FIXME */
1604 info
->both
.ShortNameLength
= strlenW( names
->short_name
) * sizeof(WCHAR
);
1605 memcpy( info
->both
.ShortName
, names
->short_name
, info
->both
.ShortNameLength
);
1606 info
->both
.FileNameLength
= name_len
;
1609 case FileIdBothDirectoryInformation
:
1610 info
->id_both
.EaSize
= 0; /* FIXME */
1611 info
->id_both
.ShortNameLength
= strlenW( names
->short_name
) * sizeof(WCHAR
);
1612 memcpy( info
->id_both
.ShortName
, names
->short_name
, info
->id_both
.ShortNameLength
);
1613 info
->id_both
.FileNameLength
= name_len
;
1616 case FileIdGlobalTxDirectoryInformation
:
1617 info
->id_tx
.TxInfoFlags
= 0;
1618 info
->id_tx
.FileNameLength
= name_len
;
1621 case FileNamesInformation
:
1622 info
->names
.FileNameLength
= name_len
;
1630 memcpy( (char *)info
+ dir_size
, names
->long_name
, min( name_len
, max_length
) );
1631 io
->Information
= start
+ dir_size
+ min( name_len
, max_length
);
1632 if (*last_info
) (*last_info
)->next
= (char *)info
- (char *)*last_info
;
1634 return name_len
> max_length
? STATUS_BUFFER_OVERFLOW
: STATUS_SUCCESS
;
1637 #ifdef VFAT_IOCTL_READDIR_BOTH
1639 /***********************************************************************
1642 * Wrapper for the VFAT ioctl to work around various kernel bugs.
1643 * dir_section must be held by caller.
1645 static KERNEL_DIRENT
*start_vfat_ioctl( int fd
)
1647 static KERNEL_DIRENT
*de
;
1652 SIZE_T size
= 2 * sizeof(*de
) + page_size
;
1655 if (NtAllocateVirtualMemory( GetCurrentProcess(), &addr
, 1, &size
, MEM_RESERVE
, PAGE_READWRITE
))
1657 /* commit only the size needed for the dir entries */
1658 /* this leaves an extra unaccessible page, which should make the kernel */
1659 /* fail with -EFAULT before it stomps all over our memory */
1661 size
= 2 * sizeof(*de
);
1662 NtAllocateVirtualMemory( GetCurrentProcess(), &addr
, 1, &size
, MEM_COMMIT
, PAGE_READWRITE
);
1665 /* set d_reclen to 65535 to work around an AFS kernel bug */
1666 de
[0].d_reclen
= 65535;
1667 res
= ioctl( fd
, VFAT_IOCTL_READDIR_BOTH
, (long)de
);
1670 if (errno
!= ENOENT
) return NULL
; /* VFAT ioctl probably not supported */
1671 de
[0].d_reclen
= 0; /* eof */
1673 else if (!res
&& de
[0].d_reclen
== 65535) return NULL
; /* AFS bug */
1679 /***********************************************************************
1680 * read_directory_vfat
1682 * Read a directory using the VFAT ioctl; helper for NtQueryDirectoryFile.
1684 static NTSTATUS
read_directory_data_vfat( struct dir_data
*data
, int fd
, const UNICODE_STRING
*mask
)
1686 char *short_name
, *long_name
;
1689 NTSTATUS status
= STATUS_NO_MEMORY
;
1690 off_t old_pos
= lseek( fd
, 0, SEEK_CUR
);
1692 if (!(de
= start_vfat_ioctl( fd
))) return STATUS_NOT_SUPPORTED
;
1694 lseek( fd
, 0, SEEK_SET
);
1696 if (!append_entry( data
, ".", NULL
, mask
)) goto done
;
1697 if (!append_entry( data
, "..", NULL
, mask
)) goto done
;
1699 while (ioctl( fd
, VFAT_IOCTL_READDIR_BOTH
, (long)de
) != -1)
1701 if (!de
[0].d_reclen
) break; /* eof */
1703 /* make sure names are null-terminated to work around an x86-64 kernel bug */
1704 len
= min( de
[0].d_reclen
, sizeof(de
[0].d_name
) - 1 );
1705 de
[0].d_name
[len
] = 0;
1706 len
= min( de
[1].d_reclen
, sizeof(de
[1].d_name
) - 1 );
1707 de
[1].d_name
[len
] = 0;
1709 if (!strcmp( de
[0].d_name
, "." ) || !strcmp( de
[0].d_name
, ".." )) continue;
1710 if (de
[1].d_name
[0])
1712 short_name
= de
[0].d_name
;
1713 long_name
= de
[1].d_name
;
1717 long_name
= de
[0].d_name
;
1720 if (!append_entry( data
, long_name
, short_name
, mask
)) goto done
;
1722 status
= STATUS_SUCCESS
;
1724 lseek( fd
, old_pos
, SEEK_SET
);
1727 #endif /* VFAT_IOCTL_READDIR_BOTH */
1730 #ifdef HAVE_GETATTRLIST
1731 /***********************************************************************
1732 * read_directory_getattrlist
1734 * Read a single file from a directory by determining whether the file
1735 * identified by mask exists using getattrlist.
1737 static NTSTATUS
read_directory_data_getattrlist( struct dir_data
*data
, const char *unix_name
)
1739 struct attrlist attrlist
;
1740 #include "pshpack4.h"
1744 struct attrreference name_reference
;
1746 char name
[NAME_MAX
* 3 + 1];
1748 #include "poppack.h"
1750 memset( &attrlist
, 0, sizeof(attrlist
) );
1751 attrlist
.bitmapcount
= ATTR_BIT_MAP_COUNT
;
1752 attrlist
.commonattr
= ATTR_CMN_NAME
| ATTR_CMN_OBJTYPE
;
1753 if (getattrlist( unix_name
, &attrlist
, &buffer
, sizeof(buffer
), FSOPT_NOFOLLOW
) == -1)
1754 return STATUS_NO_SUCH_FILE
;
1755 /* If unix_name named a symlink, the above may have succeeded even if the symlink is broken.
1756 Check that with another call without FSOPT_NOFOLLOW. We don't ask for any attributes. */
1757 if (buffer
.type
== VLNK
)
1760 attrlist
.commonattr
= 0;
1761 if (getattrlist( unix_name
, &attrlist
, &dummy
, sizeof(dummy
), 0 ) == -1)
1762 return STATUS_NO_SUCH_FILE
;
1765 TRACE( "found %s\n", buffer
.name
);
1767 if (!append_entry( data
, buffer
.name
, NULL
, NULL
)) return STATUS_NO_MEMORY
;
1769 return STATUS_SUCCESS
;
1771 #endif /* HAVE_GETATTRLIST */
1774 /***********************************************************************
1775 * read_directory_stat
1777 * Read a single file from a directory by determining whether the file
1778 * identified by mask exists using stat.
1780 static NTSTATUS
read_directory_data_stat( struct dir_data
*data
, const char *unix_name
)
1784 /* if the file system is not case sensitive we can't find the actual name through stat() */
1785 if (!get_dir_case_sensitivity(".")) return STATUS_NO_SUCH_FILE
;
1786 if (stat( unix_name
, &st
) == -1) return STATUS_NO_SUCH_FILE
;
1788 TRACE( "found %s\n", unix_name
);
1790 if (!append_entry( data
, unix_name
, NULL
, NULL
)) return STATUS_NO_MEMORY
;
1792 return STATUS_SUCCESS
;
1796 /***********************************************************************
1797 * read_directory_readdir
1799 * Read a directory using the POSIX readdir interface; helper for NtQueryDirectoryFile.
1801 static NTSTATUS
read_directory_data_readdir( struct dir_data
*data
, const UNICODE_STRING
*mask
)
1804 NTSTATUS status
= STATUS_NO_MEMORY
;
1805 DIR *dir
= opendir( "." );
1807 if (!dir
) return STATUS_NO_SUCH_FILE
;
1809 if (!append_entry( data
, ".", NULL
, mask
)) goto done
;
1810 if (!append_entry( data
, "..", NULL
, mask
)) goto done
;
1811 while ((de
= readdir( dir
)))
1813 if (!strcmp( de
->d_name
, "." ) || !strcmp( de
->d_name
, ".." )) continue;
1814 if (!append_entry( data
, de
->d_name
, NULL
, mask
)) goto done
;
1816 status
= STATUS_SUCCESS
;
1824 /***********************************************************************
1825 * read_directory_data
1827 * Read the full contents of a directory, using one of the above helper functions.
1829 static NTSTATUS
read_directory_data( struct dir_data
*data
, int fd
, const UNICODE_STRING
*mask
)
1833 #ifdef VFAT_IOCTL_READDIR_BOTH
1834 if (!(status
= read_directory_data_vfat( data
, fd
, mask
))) return status
;
1837 if (!has_wildcard( mask
))
1839 /* convert the mask to a Unix name and check for it */
1840 int ret
, used_default
;
1841 char unix_name
[MAX_DIR_ENTRY_LEN
* 3 + 1];
1843 ret
= ntdll_wcstoumbs( 0, mask
->Buffer
, mask
->Length
/ sizeof(WCHAR
),
1844 unix_name
, sizeof(unix_name
) - 1, NULL
, &used_default
);
1845 if (ret
> 0 && !used_default
)
1848 #ifdef HAVE_GETATTRLIST
1849 if (!(status
= read_directory_data_getattrlist( data
, unix_name
))) return status
;
1851 if (!(status
= read_directory_data_stat( data
, unix_name
))) return status
;
1855 return read_directory_data_readdir( data
, mask
);
1859 /* compare file names for directory sorting */
1860 static int name_compare( const void *a
, const void *b
)
1862 const struct dir_data_names
*file_a
= (const struct dir_data_names
*)a
;
1863 const struct dir_data_names
*file_b
= (const struct dir_data_names
*)b
;
1864 int ret
= RtlCompareUnicodeStrings( file_a
->long_name
, strlenW(file_a
->long_name
),
1865 file_b
->long_name
, strlenW(file_b
->long_name
), TRUE
);
1866 if (!ret
) ret
= strcmpW( file_a
->long_name
, file_b
->long_name
);
1871 /***********************************************************************
1872 * init_cached_dir_data
1874 * Initialize the cached directory contents.
1876 static NTSTATUS
init_cached_dir_data( struct dir_data
**data_ret
, int fd
, const UNICODE_STRING
*mask
)
1878 struct dir_data
*data
;
1883 if (!(data
= RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*data
) )))
1884 return STATUS_NO_MEMORY
;
1886 if ((status
= read_directory_data( data
, fd
, mask
)))
1888 free_dir_data( data
);
1892 /* sort filenames, but not "." and ".." */
1894 if (i
< data
->count
&& !strcmp( data
->names
[i
].unix_name
, "." )) i
++;
1895 if (i
< data
->count
&& !strcmp( data
->names
[i
].unix_name
, ".." )) i
++;
1896 if (i
< data
->count
) qsort( data
->names
+ i
, data
->count
- i
, sizeof(*data
->names
), name_compare
);
1900 /* release unused space */
1902 RtlReAllocateHeap( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY
, data
->buffer
,
1903 offsetof( struct dir_data_buffer
, data
[data
->buffer
->pos
] ));
1904 if (data
->count
< data
->size
)
1905 RtlReAllocateHeap( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY
, data
->names
,
1906 data
->count
* sizeof(*data
->names
) );
1907 if (!fstat( fd
, &st
))
1909 data
->id
.dev
= st
.st_dev
;
1910 data
->id
.ino
= st
.st_ino
;
1914 TRACE( "mask %s found %u files\n", debugstr_us( mask
), data
->count
);
1915 for (i
= 0; i
< data
->count
; i
++)
1916 TRACE( "%s %s\n", debugstr_w(data
->names
[i
].long_name
), debugstr_w(data
->names
[i
].short_name
) );
1919 return data
->count
? STATUS_SUCCESS
: STATUS_NO_SUCH_FILE
;
1923 /***********************************************************************
1924 * get_cached_dir_data
1926 * Retrieve the cached directory data, or initialize it if necessary.
1928 static NTSTATUS
get_cached_dir_data( HANDLE handle
, struct dir_data
**data_ret
, int fd
,
1929 const UNICODE_STRING
*mask
)
1932 int entry
= -1, free_entries
[16];
1935 SERVER_START_REQ( get_directory_cache_entry
)
1937 req
->handle
= wine_server_obj_handle( handle
);
1938 wine_server_set_reply( req
, free_entries
, sizeof(free_entries
) );
1939 if (!(status
= wine_server_call( req
))) entry
= reply
->entry
;
1941 for (i
= 0; i
< wine_server_reply_size( reply
) / sizeof(*free_entries
); i
++)
1943 int free_idx
= free_entries
[i
];
1944 if (free_idx
< dir_data_cache_size
)
1946 free_dir_data( dir_data_cache
[free_idx
] );
1947 dir_data_cache
[free_idx
] = NULL
;
1955 if (status
== STATUS_SHARING_VIOLATION
) FIXME( "shared directory handle not supported yet\n" );
1959 if (entry
>= dir_data_cache_size
)
1961 unsigned int size
= max( dir_data_cache_initial_size
, max( dir_data_cache_size
* 2, entry
+ 1 ) );
1962 struct dir_data
**new_cache
;
1965 new_cache
= RtlReAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY
, dir_data_cache
,
1966 size
* sizeof(*new_cache
) );
1968 new_cache
= RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY
, size
* sizeof(*new_cache
) );
1969 if (!new_cache
) return STATUS_NO_MEMORY
;
1970 dir_data_cache
= new_cache
;
1971 dir_data_cache_size
= size
;
1974 if (!dir_data_cache
[entry
]) status
= init_cached_dir_data( &dir_data_cache
[entry
], fd
, mask
);
1976 *data_ret
= dir_data_cache
[entry
];
1981 /******************************************************************************
1982 * NtQueryDirectoryFile [NTDLL.@]
1983 * ZwQueryDirectoryFile [NTDLL.@]
1985 NTSTATUS WINAPI
NtQueryDirectoryFile( HANDLE handle
, HANDLE event
,
1986 PIO_APC_ROUTINE apc_routine
, PVOID apc_context
,
1987 PIO_STATUS_BLOCK io
,
1988 PVOID buffer
, ULONG length
,
1989 FILE_INFORMATION_CLASS info_class
,
1990 BOOLEAN single_entry
,
1991 PUNICODE_STRING mask
,
1992 BOOLEAN restart_scan
)
1994 int cwd
, fd
, needs_close
;
1995 struct dir_data
*data
;
1998 TRACE("(%p %p %p %p %p %p 0x%08x 0x%08x 0x%08x %s 0x%08x\n",
1999 handle
, event
, apc_routine
, apc_context
, io
, buffer
,
2000 length
, info_class
, single_entry
, debugstr_us(mask
),
2003 if (event
|| apc_routine
)
2005 FIXME( "Unsupported yet option\n" );
2006 return STATUS_NOT_IMPLEMENTED
;
2010 case FileDirectoryInformation
:
2011 case FileBothDirectoryInformation
:
2012 case FileFullDirectoryInformation
:
2013 case FileIdBothDirectoryInformation
:
2014 case FileIdFullDirectoryInformation
:
2015 case FileIdGlobalTxDirectoryInformation
:
2016 case FileNamesInformation
:
2017 if (length
< dir_info_align( dir_info_size( info_class
, 1 ))) return STATUS_INFO_LENGTH_MISMATCH
;
2019 case FileObjectIdInformation
:
2020 if (length
!= sizeof(FILE_OBJECTID_INFORMATION
)) return STATUS_INFO_LENGTH_MISMATCH
;
2021 return STATUS_INVALID_INFO_CLASS
;
2022 case FileQuotaInformation
:
2023 if (length
!= sizeof(FILE_QUOTA_INFORMATION
)) return STATUS_INFO_LENGTH_MISMATCH
;
2024 return STATUS_INVALID_INFO_CLASS
;
2025 case FileReparsePointInformation
:
2026 if (length
!= sizeof(FILE_REPARSE_POINT_INFORMATION
)) return STATUS_INFO_LENGTH_MISMATCH
;
2027 return STATUS_INVALID_INFO_CLASS
;
2029 return STATUS_INVALID_INFO_CLASS
;
2031 if (!buffer
) return STATUS_ACCESS_VIOLATION
;
2033 if ((status
= server_get_unix_fd( handle
, FILE_LIST_DIRECTORY
, &fd
, &needs_close
, NULL
, NULL
)) != STATUS_SUCCESS
)
2036 io
->Information
= 0;
2038 RtlRunOnceExecuteOnce( &init_once
, init_options
, NULL
, NULL
);
2040 RtlEnterCriticalSection( &dir_section
);
2042 cwd
= open( ".", O_RDONLY
);
2043 if (fchdir( fd
) != -1)
2045 if (!(status
= get_cached_dir_data( handle
, &data
, fd
, mask
)))
2047 union file_directory_info
*last_info
= NULL
;
2049 if (restart_scan
) data
->pos
= 0;
2051 while (!status
&& data
->pos
< data
->count
)
2053 status
= get_dir_data_entry( data
, buffer
, io
, length
, info_class
, &last_info
);
2054 if (!status
|| status
== STATUS_BUFFER_OVERFLOW
) data
->pos
++;
2055 if (single_entry
) break;
2058 if (!last_info
) status
= STATUS_NO_MORE_FILES
;
2059 else if (status
== STATUS_MORE_ENTRIES
) status
= STATUS_SUCCESS
;
2061 io
->u
.Status
= status
;
2063 if (cwd
== -1 || fchdir( cwd
) == -1) chdir( "/" );
2065 else status
= FILE_GetNtStatus();
2067 RtlLeaveCriticalSection( &dir_section
);
2069 if (needs_close
) close( fd
);
2070 if (cwd
!= -1) close( cwd
);
2071 TRACE( "=> %x (%ld)\n", status
, io
->Information
);
2076 /***********************************************************************
2079 * Find a file in a directory the hard way, by doing a case-insensitive search.
2080 * The file found is appended to unix_name at pos.
2081 * There must be at least MAX_DIR_ENTRY_LEN+2 chars available at pos.
2083 static NTSTATUS
find_file_in_dir( char *unix_name
, int pos
, const WCHAR
*name
, int length
,
2084 BOOLEAN check_case
, BOOLEAN
*is_win_dir
)
2086 WCHAR buffer
[MAX_DIR_ENTRY_LEN
];
2088 BOOLEAN spaces
, is_name_8_dot_3
;
2092 int ret
, used_default
;
2094 /* try a shortcut for this directory */
2096 unix_name
[pos
++] = '/';
2097 ret
= ntdll_wcstoumbs( 0, name
, length
, unix_name
+ pos
, MAX_DIR_ENTRY_LEN
,
2098 NULL
, &used_default
);
2099 /* if we used the default char, the Unix name won't round trip properly back to Unicode */
2100 /* so it cannot match the file we are looking for */
2101 if (ret
>= 0 && !used_default
)
2103 unix_name
[pos
+ ret
] = 0;
2104 if (!stat( unix_name
, &st
))
2106 if (is_win_dir
) *is_win_dir
= is_same_file( &windir
, &st
);
2107 return STATUS_SUCCESS
;
2110 if (check_case
) goto not_found
; /* we want an exact match */
2112 if (pos
> 1) unix_name
[pos
- 1] = 0;
2113 else unix_name
[1] = 0; /* keep the initial slash */
2115 /* check if it fits in 8.3 so that we don't look for short names if we won't need them */
2117 str
.Buffer
= (WCHAR
*)name
;
2118 str
.Length
= length
* sizeof(WCHAR
);
2119 str
.MaximumLength
= str
.Length
;
2120 is_name_8_dot_3
= RtlIsNameLegalDOS8Dot3( &str
, NULL
, &spaces
) && !spaces
;
2121 #ifndef VFAT_IOCTL_READDIR_BOTH
2122 is_name_8_dot_3
= is_name_8_dot_3
&& length
>= 8 && name
[4] == '~';
2125 if (!is_name_8_dot_3
&& !get_dir_case_sensitivity( unix_name
)) goto not_found
;
2127 /* now look for it through the directory */
2129 #ifdef VFAT_IOCTL_READDIR_BOTH
2130 if (is_name_8_dot_3
)
2132 int fd
= open( unix_name
, O_RDONLY
| O_DIRECTORY
);
2137 RtlEnterCriticalSection( &dir_section
);
2138 if ((kde
= start_vfat_ioctl( fd
)))
2140 unix_name
[pos
- 1] = '/';
2141 while (kde
[0].d_reclen
)
2143 /* make sure names are null-terminated to work around an x86-64 kernel bug */
2144 size_t len
= min(kde
[0].d_reclen
, sizeof(kde
[0].d_name
) - 1 );
2145 kde
[0].d_name
[len
] = 0;
2146 len
= min(kde
[1].d_reclen
, sizeof(kde
[1].d_name
) - 1 );
2147 kde
[1].d_name
[len
] = 0;
2149 if (kde
[1].d_name
[0])
2151 ret
= ntdll_umbstowcs( 0, kde
[1].d_name
, strlen(kde
[1].d_name
),
2152 buffer
, MAX_DIR_ENTRY_LEN
);
2153 if (ret
== length
&& !memicmpW( buffer
, name
, length
))
2155 strcpy( unix_name
+ pos
, kde
[1].d_name
);
2156 RtlLeaveCriticalSection( &dir_section
);
2161 ret
= ntdll_umbstowcs( 0, kde
[0].d_name
, strlen(kde
[0].d_name
),
2162 buffer
, MAX_DIR_ENTRY_LEN
);
2163 if (ret
== length
&& !memicmpW( buffer
, name
, length
))
2165 strcpy( unix_name
+ pos
,
2166 kde
[1].d_name
[0] ? kde
[1].d_name
: kde
[0].d_name
);
2167 RtlLeaveCriticalSection( &dir_section
);
2171 if (ioctl( fd
, VFAT_IOCTL_READDIR_BOTH
, (long)kde
) == -1)
2173 RtlLeaveCriticalSection( &dir_section
);
2179 RtlLeaveCriticalSection( &dir_section
);
2182 /* fall through to normal handling */
2184 #endif /* VFAT_IOCTL_READDIR_BOTH */
2186 if (!(dir
= opendir( unix_name
)))
2188 if (errno
== ENOENT
) return STATUS_OBJECT_PATH_NOT_FOUND
;
2189 else return FILE_GetNtStatus();
2191 unix_name
[pos
- 1] = '/';
2192 str
.Buffer
= buffer
;
2193 str
.MaximumLength
= sizeof(buffer
);
2194 while ((de
= readdir( dir
)))
2196 ret
= ntdll_umbstowcs( 0, de
->d_name
, strlen(de
->d_name
), buffer
, MAX_DIR_ENTRY_LEN
);
2197 if (ret
== length
&& !memicmpW( buffer
, name
, length
))
2199 strcpy( unix_name
+ pos
, de
->d_name
);
2204 if (!is_name_8_dot_3
) continue;
2206 str
.Length
= ret
* sizeof(WCHAR
);
2207 if (!RtlIsNameLegalDOS8Dot3( &str
, NULL
, &spaces
) || spaces
)
2209 WCHAR short_nameW
[12];
2210 ret
= hash_short_file_name( &str
, short_nameW
);
2211 if (ret
== length
&& !memicmpW( short_nameW
, name
, length
))
2213 strcpy( unix_name
+ pos
, de
->d_name
);
2222 unix_name
[pos
- 1] = 0;
2223 return STATUS_OBJECT_PATH_NOT_FOUND
;
2226 if (is_win_dir
&& !stat( unix_name
, &st
)) *is_win_dir
= is_same_file( &windir
, &st
);
2227 return STATUS_SUCCESS
;
2233 static const WCHAR catrootW
[] = {'s','y','s','t','e','m','3','2','\\','c','a','t','r','o','o','t',0};
2234 static const WCHAR catroot2W
[] = {'s','y','s','t','e','m','3','2','\\','c','a','t','r','o','o','t','2',0};
2235 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};
2236 static const WCHAR driversetcW
[] = {'s','y','s','t','e','m','3','2','\\','d','r','i','v','e','r','s','\\','e','t','c',0};
2237 static const WCHAR logfilesW
[] = {'s','y','s','t','e','m','3','2','\\','l','o','g','f','i','l','e','s',0};
2238 static const WCHAR spoolW
[] = {'s','y','s','t','e','m','3','2','\\','s','p','o','o','l',0};
2239 static const WCHAR system32W
[] = {'s','y','s','t','e','m','3','2',0};
2240 static const WCHAR syswow64W
[] = {'s','y','s','w','o','w','6','4',0};
2241 static const WCHAR sysnativeW
[] = {'s','y','s','n','a','t','i','v','e',0};
2242 static const WCHAR regeditW
[] = {'r','e','g','e','d','i','t','.','e','x','e',0};
2243 static const WCHAR wow_regeditW
[] = {'s','y','s','w','o','w','6','4','\\','r','e','g','e','d','i','t','.','e','x','e',0};
2247 const WCHAR
*source
;
2248 const WCHAR
*dos_target
;
2249 const char *unix_target
;
2252 { catrootW
, NULL
, NULL
},
2253 { catroot2W
, NULL
, NULL
},
2254 { driversstoreW
, NULL
, NULL
},
2255 { driversetcW
, NULL
, NULL
},
2256 { logfilesW
, NULL
, NULL
},
2257 { spoolW
, NULL
, NULL
},
2258 { system32W
, syswow64W
, NULL
},
2259 { sysnativeW
, system32W
, NULL
},
2260 { regeditW
, wow_regeditW
, NULL
}
2263 static unsigned int nb_redirects
;
2266 /***********************************************************************
2267 * get_redirect_target
2269 * Find the target unix name for a redirected dir.
2271 static const char *get_redirect_target( const char *windows_dir
, const WCHAR
*name
)
2273 int used_default
, len
, pos
, win_len
= strlen( windows_dir
);
2274 char *unix_name
, *unix_target
= NULL
;
2277 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, win_len
+ MAX_DIR_ENTRY_LEN
+ 2 )))
2279 memcpy( unix_name
, windows_dir
, win_len
);
2284 const WCHAR
*end
, *next
;
2286 for (end
= name
; *end
; end
++) if (IS_SEPARATOR(*end
)) break;
2287 for (next
= end
; *next
; next
++) if (!IS_SEPARATOR(*next
)) break;
2289 status
= find_file_in_dir( unix_name
, pos
, name
, end
- name
, FALSE
, NULL
);
2290 if (status
== STATUS_OBJECT_PATH_NOT_FOUND
&& !*next
) /* not finding last element is ok */
2292 len
= ntdll_wcstoumbs( 0, name
, end
- name
, unix_name
+ pos
+ 1,
2293 MAX_DIR_ENTRY_LEN
- (pos
- win_len
), NULL
, &used_default
);
2294 if (len
> 0 && !used_default
)
2296 unix_name
[pos
] = '/';
2302 if (status
) goto done
;
2303 pos
+= strlen( unix_name
+ pos
);
2307 if ((unix_target
= RtlAllocateHeap( GetProcessHeap(), 0, pos
- win_len
)))
2308 memcpy( unix_target
, unix_name
+ win_len
+ 1, pos
- win_len
);
2311 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2316 /***********************************************************************
2319 static void init_redirects(void)
2321 UNICODE_STRING nt_name
;
2322 ANSI_STRING unix_name
;
2327 if (!RtlDosPathNameToNtPathName_U( user_shared_data
->NtSystemRoot
, &nt_name
, NULL
, NULL
))
2329 ERR( "can't convert %s\n", debugstr_w(user_shared_data
->NtSystemRoot
) );
2332 status
= wine_nt_to_unix_file_name( &nt_name
, &unix_name
, FILE_OPEN_IF
, FALSE
);
2333 RtlFreeUnicodeString( &nt_name
);
2336 ERR( "cannot open %s (%x)\n", debugstr_w(user_shared_data
->NtSystemRoot
), status
);
2339 if (!stat( unix_name
.Buffer
, &st
))
2341 windir
.dev
= st
.st_dev
;
2342 windir
.ino
= st
.st_ino
;
2343 nb_redirects
= sizeof(redirects
) / sizeof(redirects
[0]);
2344 for (i
= 0; i
< nb_redirects
; i
++)
2346 if (!redirects
[i
].dos_target
) continue;
2347 redirects
[i
].unix_target
= get_redirect_target( unix_name
.Buffer
, redirects
[i
].dos_target
);
2348 TRACE( "%s -> %s\n", debugstr_w(redirects
[i
].source
), redirects
[i
].unix_target
);
2351 RtlFreeAnsiString( &unix_name
);
2356 /***********************************************************************
2359 * Check if path matches a redirect name. If yes, return matched length.
2361 static int match_redirect( const WCHAR
*path
, int len
, const WCHAR
*redir
, BOOLEAN check_case
)
2365 while (i
< len
&& *redir
)
2367 if (IS_SEPARATOR(path
[i
]))
2369 if (*redir
++ != '\\') return 0;
2370 while (i
< len
&& IS_SEPARATOR(path
[i
])) i
++;
2371 continue; /* move on to next path component */
2373 else if (check_case
)
2375 if (path
[i
] != *redir
) return 0;
2379 if (tolowerW(path
[i
]) != tolowerW(*redir
)) return 0;
2384 if (*redir
) return 0;
2385 if (i
< len
&& !IS_SEPARATOR(path
[i
])) return 0;
2386 while (i
< len
&& IS_SEPARATOR(path
[i
])) i
++;
2391 /***********************************************************************
2394 * Retrieve the Unix path corresponding to a redirected path if any.
2396 static int get_redirect_path( char *unix_name
, int pos
, const WCHAR
*name
, int length
, BOOLEAN check_case
)
2401 for (i
= 0; i
< nb_redirects
; i
++)
2403 if ((len
= match_redirect( name
, length
, redirects
[i
].source
, check_case
)))
2405 if (!redirects
[i
].unix_target
) break;
2406 unix_name
[pos
++] = '/';
2407 strcpy( unix_name
+ pos
, redirects
[i
].unix_target
);
2416 /* there are no redirects on 64-bit */
2418 static const unsigned int nb_redirects
= 0;
2420 static int get_redirect_path( char *unix_name
, int pos
, const WCHAR
*name
, int length
, BOOLEAN check_case
)
2427 /***********************************************************************
2428 * DIR_init_windows_dir
2430 void DIR_init_windows_dir( const WCHAR
*win
, const WCHAR
*sys
)
2432 /* FIXME: should probably store paths as NT file names */
2434 RtlCreateUnicodeString( &system_dir
, sys
);
2437 if (is_wow64
) init_redirects();
2442 /******************************************************************************
2445 * Get the Unix path of a DOS device.
2447 static NTSTATUS
get_dos_device( const WCHAR
*name
, UINT name_len
, ANSI_STRING
*unix_name_ret
)
2449 const char *config_dir
= wine_get_config_dir();
2451 char *unix_name
, *new_name
, *dev
;
2455 /* make sure the device name is ASCII */
2456 for (i
= 0; i
< name_len
; i
++)
2457 if (name
[i
] <= 32 || name
[i
] >= 127) return STATUS_BAD_DEVICE_TYPE
;
2459 unix_len
= strlen(config_dir
) + sizeof("/dosdevices/") + name_len
+ 1;
2461 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, unix_len
)))
2462 return STATUS_NO_MEMORY
;
2464 strcpy( unix_name
, config_dir
);
2465 strcat( unix_name
, "/dosdevices/" );
2466 dev
= unix_name
+ strlen(unix_name
);
2468 for (i
= 0; i
< name_len
; i
++) dev
[i
] = (char)tolowerW(name
[i
]);
2471 /* special case for drive devices */
2472 if (name_len
== 2 && dev
[1] == ':')
2480 if (!stat( unix_name
, &st
))
2482 TRACE( "%s -> %s\n", debugstr_wn(name
,name_len
), debugstr_a(unix_name
) );
2483 unix_name_ret
->Buffer
= unix_name
;
2484 unix_name_ret
->Length
= strlen(unix_name
);
2485 unix_name_ret
->MaximumLength
= unix_len
;
2486 return STATUS_SUCCESS
;
2490 /* now try some defaults for it */
2491 if (!strcmp( dev
, "aux" ))
2493 strcpy( dev
, "com1" );
2496 if (!strcmp( dev
, "prn" ))
2498 strcpy( dev
, "lpt1" );
2503 if (dev
[1] == ':' && dev
[2] == ':') /* drive device */
2505 dev
[2] = 0; /* remove last ':' to get the drive mount point symlink */
2506 new_name
= get_default_drive_device( unix_name
);
2508 else if (!strncmp( dev
, "com", 3 )) new_name
= get_default_com_device( atoi(dev
+ 3 ));
2509 else if (!strncmp( dev
, "lpt", 3 )) new_name
= get_default_lpt_device( atoi(dev
+ 3 ));
2511 if (!new_name
) break;
2513 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2514 unix_name
= new_name
;
2515 unix_len
= strlen(unix_name
) + 1;
2516 dev
= NULL
; /* last try */
2518 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2519 return STATUS_BAD_DEVICE_TYPE
;
2523 /* return the length of the DOS namespace prefix if any */
2524 static inline int get_dos_prefix_len( const UNICODE_STRING
*name
)
2526 static const WCHAR nt_prefixW
[] = {'\\','?','?','\\'};
2527 static const WCHAR dosdev_prefixW
[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\'};
2529 if (name
->Length
>= sizeof(nt_prefixW
) &&
2530 !memcmp( name
->Buffer
, nt_prefixW
, sizeof(nt_prefixW
) ))
2531 return sizeof(nt_prefixW
) / sizeof(WCHAR
);
2533 if (name
->Length
>= sizeof(dosdev_prefixW
) &&
2534 !memicmpW( name
->Buffer
, dosdev_prefixW
, sizeof(dosdev_prefixW
)/sizeof(WCHAR
) ))
2535 return sizeof(dosdev_prefixW
) / sizeof(WCHAR
);
2541 /******************************************************************************
2544 * Recursively search directories from the dir queue for a given inode.
2546 static NTSTATUS
find_file_id( ANSI_STRING
*unix_name
, ULONGLONG file_id
, dev_t dev
)
2554 while (!(status
= next_dir_in_queue( unix_name
->Buffer
)))
2556 if (!(dir
= opendir( unix_name
->Buffer
))) continue;
2557 TRACE( "searching %s for %s\n", unix_name
->Buffer
, wine_dbgstr_longlong(file_id
) );
2558 pos
= strlen( unix_name
->Buffer
);
2559 if (pos
+ MAX_DIR_ENTRY_LEN
>= unix_name
->MaximumLength
/sizeof(WCHAR
))
2561 char *new = RtlReAllocateHeap( GetProcessHeap(), 0, unix_name
->Buffer
,
2562 unix_name
->MaximumLength
* 2 );
2566 return STATUS_NO_MEMORY
;
2568 unix_name
->MaximumLength
*= 2;
2569 unix_name
->Buffer
= new;
2571 unix_name
->Buffer
[pos
++] = '/';
2572 while ((de
= readdir( dir
)))
2574 if (!strcmp( de
->d_name
, "." ) || !strcmp( de
->d_name
, ".." )) continue;
2575 strcpy( unix_name
->Buffer
+ pos
, de
->d_name
);
2576 if (lstat( unix_name
->Buffer
, &st
) == -1) continue;
2577 if (st
.st_dev
!= dev
) continue;
2578 if (st
.st_ino
== file_id
)
2581 return STATUS_SUCCESS
;
2583 if (!S_ISDIR( st
.st_mode
)) continue;
2584 if ((status
= add_dir_to_queue( unix_name
->Buffer
)) != STATUS_SUCCESS
)
2596 /******************************************************************************
2597 * file_id_to_unix_file_name
2599 * Lookup a file from its file id instead of its name.
2601 NTSTATUS
file_id_to_unix_file_name( const OBJECT_ATTRIBUTES
*attr
, ANSI_STRING
*unix_name
)
2603 enum server_fd_type type
;
2604 int old_cwd
, root_fd
, needs_close
;
2607 struct stat st
, root_st
;
2609 if (attr
->ObjectName
->Length
!= sizeof(ULONGLONG
)) return STATUS_OBJECT_PATH_SYNTAX_BAD
;
2610 if (!attr
->RootDirectory
) return STATUS_INVALID_PARAMETER
;
2611 memcpy( &file_id
, attr
->ObjectName
->Buffer
, sizeof(file_id
) );
2613 unix_name
->MaximumLength
= 2 * MAX_DIR_ENTRY_LEN
+ 4;
2614 if (!(unix_name
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, unix_name
->MaximumLength
)))
2615 return STATUS_NO_MEMORY
;
2616 strcpy( unix_name
->Buffer
, "." );
2618 if ((status
= server_get_unix_fd( attr
->RootDirectory
, 0, &root_fd
, &needs_close
, &type
, NULL
)))
2621 if (type
!= FD_TYPE_DIR
)
2623 status
= STATUS_OBJECT_TYPE_MISMATCH
;
2627 fstat( root_fd
, &root_st
);
2628 if (root_st
.st_ino
== file_id
) /* shortcut for "." */
2630 status
= STATUS_SUCCESS
;
2634 RtlEnterCriticalSection( &dir_section
);
2635 if ((old_cwd
= open( ".", O_RDONLY
)) != -1 && fchdir( root_fd
) != -1)
2637 /* shortcut for ".." */
2638 if (!stat( "..", &st
) && st
.st_dev
== root_st
.st_dev
&& st
.st_ino
== file_id
)
2640 strcpy( unix_name
->Buffer
, ".." );
2641 status
= STATUS_SUCCESS
;
2645 status
= add_dir_to_queue( "." );
2647 status
= find_file_id( unix_name
, file_id
, root_st
.st_dev
);
2648 if (!status
) /* get rid of "./" prefix */
2649 memmove( unix_name
->Buffer
, unix_name
->Buffer
+ 2, strlen(unix_name
->Buffer
) - 1 );
2652 if (fchdir( old_cwd
) == -1) chdir( "/" );
2654 else status
= FILE_GetNtStatus();
2655 RtlLeaveCriticalSection( &dir_section
);
2656 if (old_cwd
!= -1) close( old_cwd
);
2659 if (status
== STATUS_SUCCESS
)
2661 TRACE( "%s -> %s\n", wine_dbgstr_longlong(file_id
), debugstr_a(unix_name
->Buffer
) );
2662 unix_name
->Length
= strlen( unix_name
->Buffer
);
2666 TRACE( "%s not found in dir %p\n", wine_dbgstr_longlong(file_id
), attr
->RootDirectory
);
2667 RtlFreeHeap( GetProcessHeap(), 0, unix_name
->Buffer
);
2669 if (needs_close
) close( root_fd
);
2674 /******************************************************************************
2677 * Helper for nt_to_unix_file_name
2679 static NTSTATUS
lookup_unix_name( const WCHAR
*name
, int name_len
, char **buffer
, int unix_len
, int pos
,
2680 UINT disposition
, BOOLEAN check_case
)
2683 int ret
, used_default
, len
;
2685 char *unix_name
= *buffer
;
2686 const BOOL redirect
= nb_redirects
&& ntdll_get_thread_data()->wow64_redir
;
2688 /* try a shortcut first */
2690 ret
= ntdll_wcstoumbs( 0, name
, name_len
, unix_name
+ pos
, unix_len
- pos
- 1,
2691 NULL
, &used_default
);
2693 while (name_len
&& IS_SEPARATOR(*name
))
2699 if (ret
>= 0 && !used_default
) /* if we used the default char the name didn't convert properly */
2702 unix_name
[pos
+ ret
] = 0;
2703 for (p
= unix_name
+ pos
; *p
; p
++) if (*p
== '\\') *p
= '/';
2704 if (!redirect
|| (!strstr( unix_name
, "/windows/") && strncmp( unix_name
, "windows/", 8 )))
2706 if (!stat( unix_name
, &st
))
2708 /* creation fails with STATUS_ACCESS_DENIED for the root of the drive */
2709 if (disposition
== FILE_CREATE
)
2710 return name_len
? STATUS_OBJECT_NAME_COLLISION
: STATUS_ACCESS_DENIED
;
2711 return STATUS_SUCCESS
;
2716 if (!name_len
) /* empty name -> drive root doesn't exist */
2717 return STATUS_OBJECT_PATH_NOT_FOUND
;
2718 if (check_case
&& !redirect
&& (disposition
== FILE_OPEN
|| disposition
== FILE_OVERWRITE
))
2719 return STATUS_OBJECT_NAME_NOT_FOUND
;
2721 /* now do it component by component */
2725 const WCHAR
*end
, *next
;
2726 BOOLEAN is_win_dir
= FALSE
;
2729 while (end
< name
+ name_len
&& !IS_SEPARATOR(*end
)) end
++;
2731 while (next
< name
+ name_len
&& IS_SEPARATOR(*next
)) next
++;
2732 name_len
-= next
- name
;
2734 /* grow the buffer if needed */
2736 if (unix_len
- pos
< MAX_DIR_ENTRY_LEN
+ 2)
2739 unix_len
+= 2 * MAX_DIR_ENTRY_LEN
;
2740 if (!(new_name
= RtlReAllocateHeap( GetProcessHeap(), 0, unix_name
, unix_len
)))
2741 return STATUS_NO_MEMORY
;
2742 unix_name
= *buffer
= new_name
;
2745 status
= find_file_in_dir( unix_name
, pos
, name
, end
- name
,
2746 check_case
, redirect
? &is_win_dir
: NULL
);
2748 /* if this is the last element, not finding it is not necessarily fatal */
2751 if (status
== STATUS_OBJECT_PATH_NOT_FOUND
)
2753 status
= STATUS_OBJECT_NAME_NOT_FOUND
;
2754 if (disposition
!= FILE_OPEN
&& disposition
!= FILE_OVERWRITE
)
2756 ret
= ntdll_wcstoumbs( 0, name
, end
- name
, unix_name
+ pos
+ 1,
2757 MAX_DIR_ENTRY_LEN
, NULL
, &used_default
);
2758 if (ret
> 0 && !used_default
)
2760 unix_name
[pos
] = '/';
2761 unix_name
[pos
+ 1 + ret
] = 0;
2762 status
= STATUS_NO_SUCH_FILE
;
2767 else if (status
== STATUS_SUCCESS
&& disposition
== FILE_CREATE
)
2769 status
= STATUS_OBJECT_NAME_COLLISION
;
2773 if (status
!= STATUS_SUCCESS
) break;
2775 pos
+= strlen( unix_name
+ pos
);
2778 if (is_win_dir
&& (len
= get_redirect_path( unix_name
, pos
, name
, name_len
, check_case
)))
2782 pos
+= strlen( unix_name
+ pos
);
2783 TRACE( "redirecting -> %s + %s\n", debugstr_a(unix_name
), debugstr_w(name
) );
2791 /******************************************************************************
2792 * nt_to_unix_file_name_attr
2794 NTSTATUS
nt_to_unix_file_name_attr( const OBJECT_ATTRIBUTES
*attr
, ANSI_STRING
*unix_name_ret
,
2797 static const WCHAR invalid_charsW
[] = { INVALID_NT_CHARS
, 0 };
2798 enum server_fd_type type
;
2799 int old_cwd
, root_fd
, needs_close
;
2800 const WCHAR
*name
, *p
;
2802 int name_len
, unix_len
;
2804 BOOLEAN check_case
= !(attr
->Attributes
& OBJ_CASE_INSENSITIVE
);
2806 if (!attr
->RootDirectory
) /* without root dir fall back to normal lookup */
2807 return wine_nt_to_unix_file_name( attr
->ObjectName
, unix_name_ret
, disposition
, check_case
);
2809 name
= attr
->ObjectName
->Buffer
;
2810 name_len
= attr
->ObjectName
->Length
/ sizeof(WCHAR
);
2812 if (name_len
&& IS_SEPARATOR(name
[0])) return STATUS_INVALID_PARAMETER
;
2814 /* check for invalid characters */
2815 for (p
= name
; p
< name
+ name_len
; p
++)
2816 if (*p
< 32 || strchrW( invalid_charsW
, *p
)) return STATUS_OBJECT_NAME_INVALID
;
2818 unix_len
= ntdll_wcstoumbs( 0, name
, name_len
, NULL
, 0, NULL
, NULL
);
2819 unix_len
+= MAX_DIR_ENTRY_LEN
+ 3;
2820 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, unix_len
)))
2821 return STATUS_NO_MEMORY
;
2824 if (!(status
= server_get_unix_fd( attr
->RootDirectory
, 0, &root_fd
, &needs_close
, &type
, NULL
)))
2826 if (type
!= FD_TYPE_DIR
)
2828 if (needs_close
) close( root_fd
);
2829 status
= STATUS_BAD_DEVICE_TYPE
;
2833 RtlEnterCriticalSection( &dir_section
);
2834 if ((old_cwd
= open( ".", O_RDONLY
)) != -1 && fchdir( root_fd
) != -1)
2836 status
= lookup_unix_name( name
, name_len
, &unix_name
, unix_len
, 1,
2837 disposition
, check_case
);
2838 if (fchdir( old_cwd
) == -1) chdir( "/" );
2840 else status
= FILE_GetNtStatus();
2841 RtlLeaveCriticalSection( &dir_section
);
2842 if (old_cwd
!= -1) close( old_cwd
);
2843 if (needs_close
) close( root_fd
);
2846 else if (status
== STATUS_OBJECT_TYPE_MISMATCH
) status
= STATUS_BAD_DEVICE_TYPE
;
2848 if (status
== STATUS_SUCCESS
|| status
== STATUS_NO_SUCH_FILE
)
2850 TRACE( "%s -> %s\n", debugstr_us(attr
->ObjectName
), debugstr_a(unix_name
) );
2851 unix_name_ret
->Buffer
= unix_name
;
2852 unix_name_ret
->Length
= strlen(unix_name
);
2853 unix_name_ret
->MaximumLength
= unix_len
;
2857 TRACE( "%s not found in %s\n", debugstr_w(name
), unix_name
);
2858 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2864 /******************************************************************************
2865 * wine_nt_to_unix_file_name (NTDLL.@) Not a Windows API
2867 * Convert a file name from NT namespace to Unix namespace.
2869 * If disposition is not FILE_OPEN or FILE_OVERWRITE, the last path
2870 * element doesn't have to exist; in that case STATUS_NO_SUCH_FILE is
2871 * returned, but the unix name is still filled in properly.
2873 NTSTATUS CDECL
wine_nt_to_unix_file_name( const UNICODE_STRING
*nameW
, ANSI_STRING
*unix_name_ret
,
2874 UINT disposition
, BOOLEAN check_case
)
2876 static const WCHAR unixW
[] = {'u','n','i','x'};
2877 static const WCHAR invalid_charsW
[] = { INVALID_NT_CHARS
, 0 };
2879 NTSTATUS status
= STATUS_SUCCESS
;
2880 const char *config_dir
= wine_get_config_dir();
2881 const WCHAR
*name
, *p
;
2884 int pos
, ret
, name_len
, unix_len
, prefix_len
, used_default
;
2885 WCHAR prefix
[MAX_DIR_ENTRY_LEN
];
2886 BOOLEAN is_unix
= FALSE
;
2888 name
= nameW
->Buffer
;
2889 name_len
= nameW
->Length
/ sizeof(WCHAR
);
2891 if (!name_len
|| !IS_SEPARATOR(name
[0])) return STATUS_OBJECT_PATH_SYNTAX_BAD
;
2893 if (!(pos
= get_dos_prefix_len( nameW
)))
2894 return STATUS_BAD_DEVICE_TYPE
; /* no DOS prefix, assume NT native name */
2899 if (!name_len
) return STATUS_OBJECT_NAME_INVALID
;
2901 /* check for sub-directory */
2902 for (pos
= 0; pos
< name_len
; pos
++)
2904 if (IS_SEPARATOR(name
[pos
])) break;
2905 if (name
[pos
] < 32 || strchrW( invalid_charsW
, name
[pos
] ))
2906 return STATUS_OBJECT_NAME_INVALID
;
2908 if (pos
> MAX_DIR_ENTRY_LEN
)
2909 return STATUS_OBJECT_NAME_INVALID
;
2911 if (pos
== name_len
) /* no subdir, plain DOS device */
2912 return get_dos_device( name
, name_len
, unix_name_ret
);
2914 for (prefix_len
= 0; prefix_len
< pos
; prefix_len
++)
2915 prefix
[prefix_len
] = tolowerW(name
[prefix_len
]);
2918 name_len
-= prefix_len
;
2920 /* check for invalid characters (all chars except 0 are valid for unix) */
2921 is_unix
= (prefix_len
== 4 && !memcmp( prefix
, unixW
, sizeof(unixW
) ));
2924 for (p
= name
; p
< name
+ name_len
; p
++)
2925 if (!*p
) return STATUS_OBJECT_NAME_INVALID
;
2930 for (p
= name
; p
< name
+ name_len
; p
++)
2931 if (*p
< 32 || strchrW( invalid_charsW
, *p
)) return STATUS_OBJECT_NAME_INVALID
;
2934 unix_len
= ntdll_wcstoumbs( 0, prefix
, prefix_len
, NULL
, 0, NULL
, NULL
);
2935 unix_len
+= ntdll_wcstoumbs( 0, name
, name_len
, NULL
, 0, NULL
, NULL
);
2936 unix_len
+= MAX_DIR_ENTRY_LEN
+ 3;
2937 unix_len
+= strlen(config_dir
) + sizeof("/dosdevices/");
2938 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, unix_len
)))
2939 return STATUS_NO_MEMORY
;
2940 strcpy( unix_name
, config_dir
);
2941 strcat( unix_name
, "/dosdevices/" );
2942 pos
= strlen(unix_name
);
2944 ret
= ntdll_wcstoumbs( 0, prefix
, prefix_len
, unix_name
+ pos
, unix_len
- pos
- 1,
2945 NULL
, &used_default
);
2946 if (!ret
|| used_default
)
2948 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2949 return STATUS_OBJECT_NAME_INVALID
;
2953 /* check if prefix exists (except for DOS drives to avoid extra stat calls) */
2955 if (prefix_len
!= 2 || prefix
[1] != ':')
2958 if (lstat( unix_name
, &st
) == -1 && errno
== ENOENT
)
2962 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2963 return STATUS_BAD_DEVICE_TYPE
;
2965 pos
= 0; /* fall back to unix root */
2969 status
= lookup_unix_name( name
, name_len
, &unix_name
, unix_len
, pos
, disposition
, check_case
);
2970 if (status
== STATUS_SUCCESS
|| status
== STATUS_NO_SUCH_FILE
)
2972 TRACE( "%s -> %s\n", debugstr_us(nameW
), debugstr_a(unix_name
) );
2973 unix_name_ret
->Buffer
= unix_name
;
2974 unix_name_ret
->Length
= strlen(unix_name
);
2975 unix_name_ret
->MaximumLength
= unix_len
;
2979 TRACE( "%s not found in %s\n", debugstr_w(name
), unix_name
);
2980 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2986 /******************************************************************
2987 * RtlWow64EnableFsRedirection (NTDLL.@)
2989 NTSTATUS WINAPI
RtlWow64EnableFsRedirection( BOOLEAN enable
)
2991 if (!is_wow64
) return STATUS_NOT_IMPLEMENTED
;
2992 ntdll_get_thread_data()->wow64_redir
= enable
;
2993 return STATUS_SUCCESS
;
2997 /******************************************************************
2998 * RtlWow64EnableFsRedirectionEx (NTDLL.@)
3000 NTSTATUS WINAPI
RtlWow64EnableFsRedirectionEx( ULONG disable
, ULONG
*old_value
)
3002 if (!is_wow64
) return STATUS_NOT_IMPLEMENTED
;
3006 *old_value
= !ntdll_get_thread_data()->wow64_redir
;
3010 return STATUS_ACCESS_VIOLATION
;
3014 ntdll_get_thread_data()->wow64_redir
= !disable
;
3015 return STATUS_SUCCESS
;
3019 /******************************************************************
3020 * RtlDoesFileExists_U (NTDLL.@)
3022 BOOLEAN WINAPI
RtlDoesFileExists_U(LPCWSTR file_name
)
3024 UNICODE_STRING nt_name
;
3025 FILE_BASIC_INFORMATION basic_info
;
3026 OBJECT_ATTRIBUTES attr
;
3029 if (!RtlDosPathNameToNtPathName_U( file_name
, &nt_name
, NULL
, NULL
)) return FALSE
;
3031 attr
.Length
= sizeof(attr
);
3032 attr
.RootDirectory
= 0;
3033 attr
.ObjectName
= &nt_name
;
3034 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
3035 attr
.SecurityDescriptor
= NULL
;
3036 attr
.SecurityQualityOfService
= NULL
;
3038 ret
= NtQueryAttributesFile(&attr
, &basic_info
) == STATUS_SUCCESS
;
3040 RtlFreeUnicodeString( &nt_name
);
3045 /***********************************************************************
3046 * DIR_unmount_device
3048 * Unmount the specified device.
3050 NTSTATUS
DIR_unmount_device( HANDLE handle
)
3053 int unix_fd
, needs_close
;
3055 if (!(status
= server_get_unix_fd( handle
, 0, &unix_fd
, &needs_close
, NULL
, NULL
)))
3058 char *mount_point
= NULL
;
3060 if (fstat( unix_fd
, &st
) == -1 || !is_valid_mounted_device( &st
))
3061 status
= STATUS_INVALID_PARAMETER
;
3064 if ((mount_point
= get_device_mount_point( st
.st_rdev
)))
3067 static const char umount
[] = "diskutil unmount >/dev/null 2>&1 ";
3069 static const char umount
[] = "umount >/dev/null 2>&1 ";
3071 char *cmd
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(mount_point
)+sizeof(umount
));
3074 strcpy( cmd
, umount
);
3075 strcat( cmd
, mount_point
);
3077 RtlFreeHeap( GetProcessHeap(), 0, cmd
);
3079 /* umount will fail to release the loop device since we still have
3080 a handle to it, so we release it here */
3081 if (major(st
.st_rdev
) == LOOP_MAJOR
) ioctl( unix_fd
, 0x4c01 /*LOOP_CLR_FD*/, 0 );
3084 RtlFreeHeap( GetProcessHeap(), 0, mount_point
);
3087 if (needs_close
) close( unix_fd
);
3093 /******************************************************************************
3096 * Retrieve the Unix name of the current directory; helper for wine_unix_to_nt_file_name.
3097 * Returned value must be freed by caller.
3099 NTSTATUS
DIR_get_unix_cwd( char **cwd
)
3101 int old_cwd
, unix_fd
, needs_close
;
3106 RtlAcquirePebLock();
3108 if (NtCurrentTeb()->Tib
.SubSystemTib
) /* FIXME: hack */
3109 curdir
= &((WIN16_SUBSYSTEM_TIB
*)NtCurrentTeb()->Tib
.SubSystemTib
)->curdir
;
3111 curdir
= &NtCurrentTeb()->Peb
->ProcessParameters
->CurrentDirectory
;
3113 if (!(handle
= curdir
->Handle
))
3115 UNICODE_STRING dirW
;
3116 OBJECT_ATTRIBUTES attr
;
3119 if (!RtlDosPathNameToNtPathName_U( curdir
->DosPath
.Buffer
, &dirW
, NULL
, NULL
))
3121 status
= STATUS_OBJECT_NAME_INVALID
;
3124 attr
.Length
= sizeof(attr
);
3125 attr
.RootDirectory
= 0;
3126 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
3127 attr
.ObjectName
= &dirW
;
3128 attr
.SecurityDescriptor
= NULL
;
3129 attr
.SecurityQualityOfService
= NULL
;
3131 status
= NtOpenFile( &handle
, SYNCHRONIZE
, &attr
, &io
, 0,
3132 FILE_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
);
3133 RtlFreeUnicodeString( &dirW
);
3134 if (status
!= STATUS_SUCCESS
) goto done
;
3137 if ((status
= server_get_unix_fd( handle
, 0, &unix_fd
, &needs_close
, NULL
, NULL
)) == STATUS_SUCCESS
)
3139 RtlEnterCriticalSection( &dir_section
);
3141 if ((old_cwd
= open(".", O_RDONLY
)) != -1 && fchdir( unix_fd
) != -1)
3143 unsigned int size
= 512;
3147 if (!(*cwd
= RtlAllocateHeap( GetProcessHeap(), 0, size
)))
3149 status
= STATUS_NO_MEMORY
;
3152 if (getcwd( *cwd
, size
)) break;
3153 RtlFreeHeap( GetProcessHeap(), 0, *cwd
);
3154 if (errno
!= ERANGE
)
3156 status
= STATUS_OBJECT_PATH_INVALID
;
3161 if (fchdir( old_cwd
) == -1) chdir( "/" );
3163 else status
= FILE_GetNtStatus();
3165 RtlLeaveCriticalSection( &dir_section
);
3166 if (old_cwd
!= -1) close( old_cwd
);
3167 if (needs_close
) close( unix_fd
);
3169 if (!curdir
->Handle
) NtClose( handle
);
3172 RtlReleasePebLock();