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
56 /* Work around a conflict with Solaris' system list defined in sys/list.h. */
58 #define list_next SYSLIST_NEXT
59 #define list_prev SYSLIST_PREV
60 #define list_head SYSLIST_HEAD
61 #define list_tail SYSLIST_TAIL
62 #define list_move_tail SYSLIST_MOVE_TAIL
63 #define list_remove SYSLIST_REMOVE
64 #include <sys/vnode.h>
73 #ifdef HAVE_SYS_IOCTL_H
74 #include <sys/ioctl.h>
76 #ifdef HAVE_LINUX_IOCTL_H
77 #include <linux/ioctl.h>
79 #ifdef HAVE_LINUX_MAJOR_H
80 # include <linux/major.h>
82 #ifdef HAVE_SYS_PARAM_H
83 #include <sys/param.h>
85 #ifdef HAVE_SYS_MOUNT_H
86 #include <sys/mount.h>
88 #ifdef HAVE_SYS_STATFS_H
89 #include <sys/statfs.h>
97 #define WIN32_NO_STATUS
98 #define NONAMELESSUNION
101 #include "winternl.h"
103 #include "ntdll_misc.h"
104 #include "wine/unicode.h"
105 #include "wine/server.h"
106 #include "wine/list.h"
107 #include "wine/library.h"
108 #include "wine/debug.h"
110 WINE_DEFAULT_DEBUG_CHANNEL(file
);
112 /* just in case... */
113 #undef VFAT_IOCTL_READDIR_BOTH
117 /* We want the real kernel dirent structure, not the libc one */
122 unsigned short d_reclen
;
126 /* Define the VFAT ioctl to get both short and long file names */
127 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, KERNEL_DIRENT [2] )
130 # define O_DIRECTORY 0200000 /* must be directory */
135 #define IS_OPTION_TRUE(ch) ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
136 #define IS_SEPARATOR(ch) ((ch) == '\\' || (ch) == '/')
138 #define INVALID_NT_CHARS '*','?','<','>','|','"'
139 #define INVALID_DOS_CHARS INVALID_NT_CHARS,'+','=',',',';','[',']',' ','\345'
141 #define MAX_DIR_ENTRY_LEN 255 /* max length of a directory entry in chars */
143 #define MAX_IGNORED_FILES 4
151 static struct file_identity ignored_files
[MAX_IGNORED_FILES
];
152 static unsigned int ignored_files_count
;
154 union file_directory_info
157 FILE_DIRECTORY_INFORMATION dir
;
158 FILE_BOTH_DIRECTORY_INFORMATION both
;
159 FILE_FULL_DIRECTORY_INFORMATION full
;
160 FILE_ID_BOTH_DIRECTORY_INFORMATION id_both
;
161 FILE_ID_FULL_DIRECTORY_INFORMATION id_full
;
162 FILE_ID_GLOBAL_TX_DIR_INFORMATION id_tx
;
163 FILE_NAMES_INFORMATION names
;
166 struct dir_data_buffer
168 struct dir_data_buffer
*next
; /* next buffer in the list */
169 unsigned int size
; /* total size of the buffer */
170 unsigned int pos
; /* current position in the buffer */
174 struct dir_data_names
176 const WCHAR
*long_name
; /* long file name in Unicode */
177 const WCHAR
*short_name
; /* short file name in Unicode */
178 const char *unix_name
; /* Unix file name in host encoding */
183 unsigned int size
; /* size of the names array */
184 unsigned int count
; /* count of used entries in the names array */
185 unsigned int pos
; /* current reading position in the names array */
186 struct file_identity id
; /* directory file identity */
187 struct dir_data_names
*names
; /* directory file names */
188 struct dir_data_buffer
*buffer
; /* head of data buffers list */
191 static const unsigned int dir_data_buffer_initial_size
= 4096;
192 static const unsigned int dir_data_cache_initial_size
= 256;
193 static const unsigned int dir_data_names_initial_size
= 64;
195 static struct dir_data
**dir_data_cache
;
196 static unsigned int dir_data_cache_size
;
198 static BOOL show_dot_files
;
199 static RTL_RUN_ONCE init_once
= RTL_RUN_ONCE_INIT
;
201 /* at some point we may want to allow Winelib apps to set this */
202 static const BOOL is_case_sensitive
= FALSE
;
204 UNICODE_STRING system_dir
= { 0, 0, NULL
}; /* system directory */
206 static struct file_identity windir
;
208 static RTL_CRITICAL_SECTION dir_section
;
209 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
212 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
213 0, 0, { (DWORD_PTR
)(__FILE__
": dir_section") }
215 static RTL_CRITICAL_SECTION dir_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
218 /* check if a given Unicode char is OK in a DOS short name */
219 static inline BOOL
is_invalid_dos_char( WCHAR ch
)
221 static const WCHAR invalid_chars
[] = { INVALID_DOS_CHARS
,'~','.',0 };
222 if (ch
> 0x7f) return TRUE
;
223 return strchrW( invalid_chars
, ch
) != NULL
;
226 /* check if the device can be a mounted volume */
227 static inline BOOL
is_valid_mounted_device( const struct stat
*st
)
229 #if defined(linux) || defined(__sun__)
230 return S_ISBLK( st
->st_mode
);
232 /* disks are char devices on *BSD */
233 return S_ISCHR( st
->st_mode
);
237 static inline void ignore_file( const char *name
)
240 assert( ignored_files_count
< MAX_IGNORED_FILES
);
241 if (!stat( name
, &st
))
243 ignored_files
[ignored_files_count
].dev
= st
.st_dev
;
244 ignored_files
[ignored_files_count
].ino
= st
.st_ino
;
245 ignored_files_count
++;
249 static inline BOOL
is_same_file( const struct file_identity
*file
, const struct stat
*st
)
251 return st
->st_dev
== file
->dev
&& st
->st_ino
== file
->ino
;
254 static inline BOOL
is_ignored_file( const struct stat
*st
)
258 for (i
= 0; i
< ignored_files_count
; i
++)
259 if (is_same_file( &ignored_files
[i
], st
)) return TRUE
;
263 static inline unsigned int dir_info_align( unsigned int len
)
265 return (len
+ 7) & ~7;
268 static inline unsigned int dir_info_size( FILE_INFORMATION_CLASS
class, unsigned int len
)
272 case FileDirectoryInformation
:
273 return offsetof( FILE_DIRECTORY_INFORMATION
, FileName
[len
] );
274 case FileBothDirectoryInformation
:
275 return offsetof( FILE_BOTH_DIRECTORY_INFORMATION
, FileName
[len
] );
276 case FileFullDirectoryInformation
:
277 return offsetof( FILE_FULL_DIRECTORY_INFORMATION
, FileName
[len
] );
278 case FileIdBothDirectoryInformation
:
279 return offsetof( FILE_ID_BOTH_DIRECTORY_INFORMATION
, FileName
[len
] );
280 case FileIdFullDirectoryInformation
:
281 return offsetof( FILE_ID_FULL_DIRECTORY_INFORMATION
, FileName
[len
] );
282 case FileIdGlobalTxDirectoryInformation
:
283 return offsetof( FILE_ID_GLOBAL_TX_DIR_INFORMATION
, FileName
[len
] );
284 case FileNamesInformation
:
285 return offsetof( FILE_NAMES_INFORMATION
, FileName
[len
] );
292 static inline BOOL
has_wildcard( const UNICODE_STRING
*mask
)
295 memchrW( mask
->Buffer
, '*', mask
->Length
/ sizeof(WCHAR
) ) ||
296 memchrW( mask
->Buffer
, '?', mask
->Length
/ sizeof(WCHAR
) ));
299 /* get space from the current directory data buffer, allocating a new one if necessary */
300 static void *get_dir_data_space( struct dir_data
*data
, unsigned int size
)
302 struct dir_data_buffer
*buffer
= data
->buffer
;
305 if (!buffer
|| size
> buffer
->size
- buffer
->pos
)
307 unsigned int new_size
= buffer
? buffer
->size
* 2 : dir_data_buffer_initial_size
;
308 if (new_size
< size
) new_size
= size
;
309 if (!(buffer
= RtlAllocateHeap( GetProcessHeap(), 0,
310 offsetof( struct dir_data_buffer
, data
[new_size
] ) ))) return NULL
;
312 buffer
->size
= new_size
;
313 buffer
->next
= data
->buffer
;
314 data
->buffer
= buffer
;
316 ret
= buffer
->data
+ buffer
->pos
;
321 /* add a string to the directory data buffer */
322 static const char *add_dir_data_nameA( struct dir_data
*data
, const char *name
)
324 /* keep buffer data WCHAR-aligned */
325 char *ptr
= get_dir_data_space( data
, (strlen( name
) + sizeof(WCHAR
)) & ~(sizeof(WCHAR
) - 1) );
326 if (ptr
) strcpy( ptr
, name
);
330 /* add a Unicode string to the directory data buffer */
331 static const WCHAR
*add_dir_data_nameW( struct dir_data
*data
, const WCHAR
*name
)
333 WCHAR
*ptr
= get_dir_data_space( data
, (strlenW( name
) + 1) * sizeof(WCHAR
) );
334 if (ptr
) strcpyW( ptr
, name
);
338 /* add an entry to the directory names array */
339 static BOOL
add_dir_data_names( struct dir_data
*data
, const WCHAR
*long_name
,
340 const WCHAR
*short_name
, const char *unix_name
)
342 static const WCHAR empty
[1];
343 struct dir_data_names
*names
= data
->names
;
345 if (data
->count
>= data
->size
)
347 unsigned int new_size
= max( data
->size
* 2, dir_data_names_initial_size
);
349 if (names
) names
= RtlReAllocateHeap( GetProcessHeap(), 0, names
, new_size
* sizeof(*names
) );
350 else names
= RtlAllocateHeap( GetProcessHeap(), 0, new_size
* sizeof(*names
) );
351 if (!names
) return FALSE
;
352 data
->size
= new_size
;
358 if (!(names
[data
->count
].short_name
= add_dir_data_nameW( data
, short_name
))) return FALSE
;
360 else names
[data
->count
].short_name
= empty
;
362 if (!(names
[data
->count
].long_name
= add_dir_data_nameW( data
, long_name
))) return FALSE
;
363 if (!(names
[data
->count
].unix_name
= add_dir_data_nameA( data
, unix_name
))) return FALSE
;
368 /* free the complete directory data structure */
369 static void free_dir_data( struct dir_data
*data
)
371 struct dir_data_buffer
*buffer
, *next
;
375 for (buffer
= data
->buffer
; buffer
; buffer
= next
)
378 RtlFreeHeap( GetProcessHeap(), 0, buffer
);
380 RtlFreeHeap( GetProcessHeap(), 0, data
->names
);
381 RtlFreeHeap( GetProcessHeap(), 0, data
);
385 /* support for a directory queue for filesystem searches */
393 static struct list dir_queue
= LIST_INIT( dir_queue
);
395 static NTSTATUS
add_dir_to_queue( const char *name
)
397 int len
= strlen( name
) + 1;
398 struct dir_name
*dir
= RtlAllocateHeap( GetProcessHeap(), 0,
399 FIELD_OFFSET( struct dir_name
, name
[len
] ));
400 if (!dir
) return STATUS_NO_MEMORY
;
401 strcpy( dir
->name
, name
);
402 list_add_tail( &dir_queue
, &dir
->entry
);
403 return STATUS_SUCCESS
;
406 static NTSTATUS
next_dir_in_queue( char *name
)
408 struct list
*head
= list_head( &dir_queue
);
411 struct dir_name
*dir
= LIST_ENTRY( head
, struct dir_name
, entry
);
412 strcpy( name
, dir
->name
);
413 list_remove( &dir
->entry
);
414 RtlFreeHeap( GetProcessHeap(), 0, dir
);
415 return STATUS_SUCCESS
;
417 return STATUS_OBJECT_NAME_NOT_FOUND
;
420 static void flush_dir_queue(void)
424 while ((head
= list_head( &dir_queue
)))
426 struct dir_name
*dir
= LIST_ENTRY( head
, struct dir_name
, entry
);
427 list_remove( &dir
->entry
);
428 RtlFreeHeap( GetProcessHeap(), 0, dir
);
433 /***********************************************************************
434 * get_default_com_device
436 * Return the default device to use for serial ports.
438 static char *get_default_com_device( int num
)
442 if (num
< 1 || num
> 256) return NULL
;
444 ret
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/ttyS256") );
445 if (!ret
) return NULL
;
446 sprintf( ret
, "/dev/ttyS%d", num
- 1 );
447 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
448 ret
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/cuau256") );
449 if (!ret
) return NULL
;
450 sprintf( ret
, "/dev/cuau%d", num
- 1 );
451 #elif defined(__DragonFly__)
452 ret
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/cuaa256") );
453 if (!ret
) return NULL
;
454 sprintf( ret
, "/dev/cuaa%d", num
- 1 );
456 FIXME( "no known default for device com%d\n", num
);
462 /***********************************************************************
463 * get_default_lpt_device
465 * Return the default device to use for parallel ports.
467 static char *get_default_lpt_device( int num
)
471 if (num
< 1 || num
> 256) return NULL
;
473 ret
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/lp256") );
474 if (!ret
) return NULL
;
475 sprintf( ret
, "/dev/lp%d", num
- 1 );
477 FIXME( "no known default for device lpt%d\n", num
);
484 static char *unescape_field( char *str
)
488 for (in
= out
= str
; *in
; in
++, out
++)
498 else if (in
[1] == '0' && in
[2] == '4' && in
[3] == '0')
503 else if (in
[1] == '0' && in
[2] == '1' && in
[3] == '1')
508 else if (in
[1] == '0' && in
[2] == '1' && in
[3] == '2')
513 else if (in
[1] == '1' && in
[2] == '3' && in
[3] == '4')
525 static inline char *get_field( char **str
)
529 ret
= strsep( str
, " \t" );
530 if (*str
) *str
+= strspn( *str
, " \t" );
534 /************************************************************************
535 * getmntent_replacement
537 * getmntent replacement for Android.
539 * NB returned static buffer is not thread safe; protect with dir_section.
541 static struct mntent
*getmntent_replacement( FILE *f
)
543 static struct mntent entry
;
544 static char buf
[4096];
549 if (!fgets( buf
, sizeof(buf
), f
)) return NULL
;
550 p
= strchr( buf
, '\n' );
552 else /* Partially unread line, move file ptr to end */
555 while (fgets( tmp
, sizeof(tmp
), f
))
556 if (strchr( tmp
, '\n' )) break;
558 start
= buf
+ strspn( buf
, " \t" );
559 } while (start
[0] == '\0' || start
[0] == '#');
561 p
= get_field( &start
);
562 entry
.mnt_fsname
= p
? unescape_field( p
) : (char *)"";
564 p
= get_field( &start
);
565 entry
.mnt_dir
= p
? unescape_field( p
) : (char *)"";
567 p
= get_field( &start
);
568 entry
.mnt_type
= p
? unescape_field( p
) : (char *)"";
570 p
= get_field( &start
);
571 entry
.mnt_opts
= p
? unescape_field( p
) : (char *)"";
573 p
= get_field( &start
);
574 entry
.mnt_freq
= p
? atoi(p
) : 0;
576 p
= get_field( &start
);
577 entry
.mnt_passno
= p
? atoi(p
) : 0;
581 #define getmntent getmntent_replacement
584 /***********************************************************************
585 * DIR_get_drives_info
587 * Retrieve device/inode number for all the drives. Helper for find_drive_root.
589 unsigned int DIR_get_drives_info( struct drive_info info
[MAX_DOS_DRIVES
] )
591 static struct drive_info cache
[MAX_DOS_DRIVES
];
592 static time_t last_update
;
593 static unsigned int nb_drives
;
595 time_t now
= time(NULL
);
597 RtlEnterCriticalSection( &dir_section
);
598 if (now
!= last_update
)
600 const char *config_dir
= wine_get_config_dir();
605 if ((buffer
= RtlAllocateHeap( GetProcessHeap(), 0,
606 strlen(config_dir
) + sizeof("/dosdevices/a:") )))
608 strcpy( buffer
, config_dir
);
609 strcat( buffer
, "/dosdevices/a:" );
610 p
= buffer
+ strlen(buffer
) - 2;
612 for (i
= nb_drives
= 0; i
< MAX_DOS_DRIVES
; i
++)
615 if (!stat( buffer
, &st
))
617 cache
[i
].dev
= st
.st_dev
;
618 cache
[i
].ino
= st
.st_ino
;
627 RtlFreeHeap( GetProcessHeap(), 0, buffer
);
631 memcpy( info
, cache
, sizeof(cache
) );
633 RtlLeaveCriticalSection( &dir_section
);
638 /***********************************************************************
639 * parse_mount_entries
641 * Parse mount entries looking for a given device. Helper for get_default_drive_device.
645 #include <sys/vfstab.h>
646 static char *parse_vfstab_entries( FILE *f
, dev_t dev
, ino_t ino
)
652 while (! getvfsent( f
, &entry
))
654 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
655 if (!strcmp( entry
.vfs_fstype
, "nfs" ) ||
656 !strcmp( entry
.vfs_fstype
, "smbfs" ) ||
657 !strcmp( entry
.vfs_fstype
, "ncpfs" )) continue;
659 if (stat( entry
.vfs_mountp
, &st
) == -1) continue;
660 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
661 if (!strcmp( entry
.vfs_fstype
, "fd" ))
663 if ((device
= strstr( entry
.vfs_mntopts
, "dev=" )))
665 char *p
= strchr( device
+ 4, ',' );
671 return entry
.vfs_special
;
678 static char *parse_mount_entries( FILE *f
, dev_t dev
, ino_t ino
)
680 struct mntent
*entry
;
684 while ((entry
= getmntent( f
)))
686 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
687 if (!strcmp( entry
->mnt_type
, "nfs" ) ||
688 !strcmp( entry
->mnt_type
, "smbfs" ) ||
689 !strcmp( entry
->mnt_type
, "ncpfs" )) continue;
691 if (stat( entry
->mnt_dir
, &st
) == -1) continue;
692 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
693 if (!strcmp( entry
->mnt_type
, "supermount" ))
695 if ((device
= strstr( entry
->mnt_opts
, "dev=" )))
697 char *p
= strchr( device
+ 4, ',' );
702 else if (!stat( entry
->mnt_fsname
, &st
) && S_ISREG(st
.st_mode
))
704 /* if device is a regular file check for a loop mount */
705 if ((device
= strstr( entry
->mnt_opts
, "loop=" )))
707 char *p
= strchr( device
+ 5, ',' );
713 return entry
->mnt_fsname
;
719 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
721 static char *parse_mount_entries( FILE *f
, dev_t dev
, ino_t ino
)
726 while ((entry
= getfsent()))
728 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
729 if (!strcmp( entry
->fs_vfstype
, "nfs" ) ||
730 !strcmp( entry
->fs_vfstype
, "smbfs" ) ||
731 !strcmp( entry
->fs_vfstype
, "ncpfs" )) continue;
733 if (stat( entry
->fs_file
, &st
) == -1) continue;
734 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
735 return entry
->fs_spec
;
742 #include <sys/mnttab.h>
743 static char *parse_mount_entries( FILE *f
, dev_t dev
, ino_t ino
)
750 while (( ! getmntent( f
, &entry
) ))
752 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
753 if (!strcmp( entry
.mnt_fstype
, "nfs" ) ||
754 !strcmp( entry
.mnt_fstype
, "smbfs" ) ||
755 !strcmp( entry
.mnt_fstype
, "ncpfs" )) continue;
757 if (stat( entry
.mnt_mountp
, &st
) == -1) continue;
758 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
759 if (!strcmp( entry
.mnt_fstype
, "fd" ))
761 if ((device
= strstr( entry
.mnt_mntopts
, "dev=" )))
763 char *p
= strchr( device
+ 4, ',' );
769 return entry
.mnt_special
;
775 /***********************************************************************
776 * get_default_drive_device
778 * Return the default device to use for a given drive mount point.
780 static char *get_default_drive_device( const char *root
)
790 /* try to open it first to force it to get mounted */
791 if ((fd
= open( root
, O_RDONLY
| O_DIRECTORY
)) != -1)
793 res
= fstat( fd
, &st
);
796 /* now try normal stat just in case */
797 if (res
== -1) res
= stat( root
, &st
);
798 if (res
== -1) return NULL
;
800 RtlEnterCriticalSection( &dir_section
);
803 if ((f
= fopen( "/proc/mounts", "r" )))
805 device
= parse_mount_entries( f
, st
.st_dev
, st
.st_ino
);
809 if ((f
= fopen( "/etc/mtab", "r" )))
811 device
= parse_mount_entries( f
, st
.st_dev
, st
.st_ino
);
814 /* look through fstab too in case it's not mounted (for instance if it's an audio CD) */
815 if (!device
&& (f
= fopen( "/etc/fstab", "r" )))
817 device
= parse_mount_entries( f
, st
.st_dev
, st
.st_ino
);
823 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(device
) + 1 );
824 if (ret
) strcpy( ret
, device
);
826 RtlLeaveCriticalSection( &dir_section
);
828 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__ ) || defined(__DragonFly__)
833 /* try to open it first to force it to get mounted */
834 if ((fd
= open( root
, O_RDONLY
)) != -1)
836 res
= fstat( fd
, &st
);
839 /* now try normal stat just in case */
840 if (res
== -1) res
= stat( root
, &st
);
841 if (res
== -1) return NULL
;
843 RtlEnterCriticalSection( &dir_section
);
845 /* The FreeBSD parse_mount_entries doesn't require a file argument, so just
846 * pass NULL. Leave the argument in for symmetry.
848 device
= parse_mount_entries( NULL
, st
.st_dev
, st
.st_ino
);
851 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(device
) + 1 );
852 if (ret
) strcpy( ret
, device
);
854 RtlLeaveCriticalSection( &dir_section
);
862 /* try to open it first to force it to get mounted */
863 if ((fd
= open( root
, O_RDONLY
)) != -1)
865 res
= fstat( fd
, &st
);
868 /* now try normal stat just in case */
869 if (res
== -1) res
= stat( root
, &st
);
870 if (res
== -1) return NULL
;
872 RtlEnterCriticalSection( &dir_section
);
874 if ((f
= fopen( "/etc/mnttab", "r" )))
876 device
= parse_mount_entries( f
, st
.st_dev
, st
.st_ino
);
879 /* look through fstab too in case it's not mounted (for instance if it's an audio CD) */
880 if (!device
&& (f
= fopen( "/etc/vfstab", "r" )))
882 device
= parse_vfstab_entries( f
, st
.st_dev
, st
.st_ino
);
887 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(device
) + 1 );
888 if (ret
) strcpy( ret
, device
);
890 RtlLeaveCriticalSection( &dir_section
);
892 #elif defined(__APPLE__)
893 struct statfs
*mntStat
;
899 static const char path_bsd_device
[] = "/dev/disk";
902 res
= stat( root
, &st
);
903 if (res
== -1) return NULL
;
908 RtlEnterCriticalSection( &dir_section
);
910 mntSize
= getmntinfo(&mntStat
, MNT_NOWAIT
);
912 for (i
= 0; i
< mntSize
&& !ret
; i
++)
914 if (stat(mntStat
[i
].f_mntonname
, &st
) == -1) continue;
915 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
917 /* FIXME add support for mounted network drive */
918 if ( strncmp(mntStat
[i
].f_mntfromname
, path_bsd_device
, strlen(path_bsd_device
)) == 0)
920 /* set return value to the corresponding raw BSD node */
921 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(mntStat
[i
].f_mntfromname
) + 2 /* 2 : r and \0 */ );
924 strcpy(ret
, "/dev/r");
925 strcat(ret
, mntStat
[i
].f_mntfromname
+sizeof("/dev/")-1);
929 RtlLeaveCriticalSection( &dir_section
);
932 if (!warned
++) FIXME( "auto detection of DOS devices not supported on this platform\n" );
938 /***********************************************************************
939 * get_device_mount_point
941 * Return the current mount point for a device.
943 static char *get_device_mount_point( dev_t dev
)
950 RtlEnterCriticalSection( &dir_section
);
953 if ((f
= fopen( "/proc/mounts", "r" )))
955 if ((f
= fopen( "/etc/mtab", "r" )))
958 struct mntent
*entry
;
962 while ((entry
= getmntent( f
)))
964 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
965 if (!strcmp( entry
->mnt_type
, "nfs" ) ||
966 !strcmp( entry
->mnt_type
, "smbfs" ) ||
967 !strcmp( entry
->mnt_type
, "ncpfs" )) continue;
969 if (!strcmp( entry
->mnt_type
, "supermount" ))
971 if ((device
= strstr( entry
->mnt_opts
, "dev=" )))
974 if ((p
= strchr( device
, ',' ))) *p
= 0;
977 else if (!stat( entry
->mnt_fsname
, &st
) && S_ISREG(st
.st_mode
))
979 /* if device is a regular file check for a loop mount */
980 if ((device
= strstr( entry
->mnt_opts
, "loop=" )))
983 if ((p
= strchr( device
, ',' ))) *p
= 0;
986 else device
= entry
->mnt_fsname
;
988 if (device
&& !stat( device
, &st
) && S_ISBLK(st
.st_mode
) && st
.st_rdev
== dev
)
990 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(entry
->mnt_dir
) + 1 );
991 if (ret
) strcpy( ret
, entry
->mnt_dir
);
997 RtlLeaveCriticalSection( &dir_section
);
998 #elif defined(__APPLE__)
999 struct statfs
*entry
;
1003 RtlEnterCriticalSection( &dir_section
);
1005 size
= getmntinfo( &entry
, MNT_NOWAIT
);
1006 for (i
= 0; i
< size
; i
++)
1008 if (stat( entry
[i
].f_mntfromname
, &st
) == -1) continue;
1009 if (S_ISBLK(st
.st_mode
) && st
.st_rdev
== dev
)
1011 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(entry
[i
].f_mntonname
) + 1 );
1012 if (ret
) strcpy( ret
, entry
[i
].f_mntonname
);
1016 RtlLeaveCriticalSection( &dir_section
);
1019 if (!warned
++) FIXME( "unmounting devices not supported on this platform\n" );
1025 #if defined(HAVE_GETATTRLIST) && defined(ATTR_VOL_CAPABILITIES) && \
1026 defined(VOL_CAPABILITIES_FORMAT) && defined(VOL_CAP_FMT_CASE_SENSITIVE)
1039 BOOLEAN case_sensitive
;
1045 vol_capabilities_attr_t caps
;
1048 /***********************************************************************
1051 * Checks if the specified file system is in the cache.
1053 static struct fs_cache
*look_up_fs_cache( dev_t dev
)
1056 for (i
= 0; i
< sizeof(fs_cache
)/sizeof(fs_cache
[0]); i
++)
1057 if (fs_cache
[i
].dev
== dev
)
1062 /***********************************************************************
1065 * Adds the specified file system to the cache.
1067 static void add_fs_cache( dev_t dev
, fsid_t fsid
, BOOLEAN case_sensitive
)
1070 struct fs_cache
*entry
= look_up_fs_cache( dev
);
1071 static int once
= 0;
1074 /* Update the cache */
1076 entry
->case_sensitive
= case_sensitive
;
1080 /* Add a new entry */
1081 for (i
= 0; i
< sizeof(fs_cache
)/sizeof(fs_cache
[0]); i
++)
1082 if (fs_cache
[i
].dev
== 0)
1084 /* This entry is empty, use it */
1085 fs_cache
[i
].dev
= dev
;
1086 fs_cache
[i
].fsid
= fsid
;
1087 fs_cache
[i
].case_sensitive
= case_sensitive
;
1091 /* Cache is out of space, warn */
1093 WARN( "FS cache is out of space, expect performance problems\n" );
1096 /***********************************************************************
1097 * get_dir_case_sensitivity_attr
1099 * Checks if the volume containing the specified directory is case
1100 * sensitive or not. Uses getattrlist(2).
1102 static int get_dir_case_sensitivity_attr( const char *dir
)
1105 struct attrlist attr
;
1106 struct vol_caps caps
;
1107 struct get_fsid get_fsid
;
1108 struct fs_cache
*entry
;
1110 /* First get the FS ID of the volume */
1111 attr
.bitmapcount
= ATTR_BIT_MAP_COUNT
;
1113 attr
.commonattr
= ATTR_CMN_DEVID
|ATTR_CMN_FSID
;
1114 attr
.volattr
= attr
.dirattr
= attr
.fileattr
= attr
.forkattr
= 0;
1116 if (getattrlist( dir
, &attr
, &get_fsid
, sizeof(get_fsid
), 0 ) != 0 ||
1117 get_fsid
.size
!= sizeof(get_fsid
))
1119 /* Try to look it up in the cache */
1120 entry
= look_up_fs_cache( get_fsid
.dev
);
1121 if (entry
&& !memcmp( &entry
->fsid
, &get_fsid
.fsid
, sizeof(fsid_t
) ))
1122 /* Cache lookup succeeded */
1123 return entry
->case_sensitive
;
1124 /* Cache is stale at this point, we have to update it */
1126 mntpoint
= get_device_mount_point( get_fsid
.dev
);
1127 /* Now look up the case-sensitivity */
1128 attr
.commonattr
= 0;
1129 attr
.volattr
= ATTR_VOL_INFO
|ATTR_VOL_CAPABILITIES
;
1130 if (getattrlist( mntpoint
, &attr
, &caps
, sizeof(caps
), 0 ) < 0)
1132 RtlFreeHeap( GetProcessHeap(), 0, mntpoint
);
1133 add_fs_cache( get_fsid
.dev
, get_fsid
.fsid
, TRUE
);
1136 RtlFreeHeap( GetProcessHeap(), 0, mntpoint
);
1137 if (caps
.size
== sizeof(caps
) &&
1138 (caps
.caps
.valid
[VOL_CAPABILITIES_FORMAT
] &
1139 (VOL_CAP_FMT_CASE_SENSITIVE
| VOL_CAP_FMT_CASE_PRESERVING
)) ==
1140 (VOL_CAP_FMT_CASE_SENSITIVE
| VOL_CAP_FMT_CASE_PRESERVING
))
1144 if ((caps
.caps
.capabilities
[VOL_CAPABILITIES_FORMAT
] &
1145 VOL_CAP_FMT_CASE_SENSITIVE
) != VOL_CAP_FMT_CASE_SENSITIVE
)
1149 /* Update the cache */
1150 add_fs_cache( get_fsid
.dev
, get_fsid
.fsid
, ret
);
1157 /***********************************************************************
1158 * get_dir_case_sensitivity_stat
1160 * Checks if the volume containing the specified directory is case
1161 * sensitive or not. Uses statfs(2) or statvfs(2).
1163 static BOOLEAN
get_dir_case_sensitivity_stat( const char *dir
)
1165 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1168 if (statfs( dir
, &stfs
) == -1) return FALSE
;
1169 /* Assume these file systems are always case insensitive on Mac OS.
1170 * For FreeBSD, only assume CIOPFS is case insensitive (AFAIK, Mac OS
1171 * is the only UNIX that supports case-insensitive lookup).
1173 if (!strcmp( stfs
.f_fstypename
, "fusefs" ) &&
1174 !strncmp( stfs
.f_mntfromname
, "ciopfs", 5 ))
1177 if (!strcmp( stfs
.f_fstypename
, "msdos" ) ||
1178 !strcmp( stfs
.f_fstypename
, "cd9660" ) ||
1179 !strcmp( stfs
.f_fstypename
, "udf" ) ||
1180 !strcmp( stfs
.f_fstypename
, "ntfs" ) ||
1181 !strcmp( stfs
.f_fstypename
, "smbfs" ))
1183 #ifdef _DARWIN_FEATURE_64_BIT_INODE
1184 if (!strcmp( stfs
.f_fstypename
, "hfs" ) && (stfs
.f_fssubtype
== 0 ||
1185 stfs
.f_fssubtype
== 1 ||
1186 stfs
.f_fssubtype
== 128))
1189 /* The field says "reserved", but a quick look at the kernel source
1190 * tells us that this "reserved" field is really the same as the
1191 * "fssubtype" field from the inode64 structure (see munge_statfs()
1192 * in <xnu-source>/bsd/vfs/vfs_syscalls.c).
1194 if (!strcmp( stfs
.f_fstypename
, "hfs" ) && (stfs
.f_reserved1
== 0 ||
1195 stfs
.f_reserved1
== 1 ||
1196 stfs
.f_reserved1
== 128))
1202 #elif defined(__NetBSD__)
1203 struct statvfs stfs
;
1205 if (statvfs( dir
, &stfs
) == -1) return FALSE
;
1206 /* Only assume CIOPFS is case insensitive. */
1207 if (strcmp( stfs
.f_fstypename
, "fusefs" ) ||
1208 strncmp( stfs
.f_mntfromname
, "ciopfs", 5 ))
1212 #elif defined(__linux__)
1217 /* Only assume CIOPFS is case insensitive. */
1218 if (statfs( dir
, &stfs
) == -1) return FALSE
;
1219 if (stfs
.f_type
!= 0x65735546 /* FUSE_SUPER_MAGIC */)
1221 /* Normally, we'd have to parse the mtab to find out exactly what
1222 * kind of FUSE FS this is. But, someone on wine-devel suggested
1223 * a shortcut. We'll stat a special file in the directory. If it's
1224 * there, we'll assume it's a CIOPFS, else not.
1225 * This will break if somebody puts a file named ".ciopfs" in a non-
1228 cifile
= RtlAllocateHeap( GetProcessHeap(), 0, strlen( dir
)+sizeof("/.ciopfs") );
1229 if (!cifile
) return TRUE
;
1230 strcpy( cifile
, dir
);
1231 strcat( cifile
, "/.ciopfs" );
1232 if (stat( cifile
, &st
) == 0)
1234 RtlFreeHeap( GetProcessHeap(), 0, cifile
);
1237 RtlFreeHeap( GetProcessHeap(), 0, cifile
);
1245 /***********************************************************************
1246 * get_dir_case_sensitivity
1248 * Checks if the volume containing the specified directory is case
1249 * sensitive or not. Uses statfs(2) or statvfs(2).
1251 static BOOLEAN
get_dir_case_sensitivity( const char *dir
)
1253 #if defined(HAVE_GETATTRLIST) && defined(ATTR_VOL_CAPABILITIES) && \
1254 defined(VOL_CAPABILITIES_FORMAT) && defined(VOL_CAP_FMT_CASE_SENSITIVE)
1255 int case_sensitive
= get_dir_case_sensitivity_attr( dir
);
1256 if (case_sensitive
!= -1) return case_sensitive
;
1258 return get_dir_case_sensitivity_stat( dir
);
1262 /***********************************************************************
1265 * Initialize the show_dot_files options.
1267 static DWORD WINAPI
init_options( RTL_RUN_ONCE
*once
, void *param
, void **context
)
1269 static const WCHAR WineW
[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e',0};
1270 static const WCHAR ShowDotFilesW
[] = {'S','h','o','w','D','o','t','F','i','l','e','s',0};
1274 OBJECT_ATTRIBUTES attr
;
1275 UNICODE_STRING nameW
;
1277 RtlOpenCurrentUser( KEY_ALL_ACCESS
, &root
);
1278 attr
.Length
= sizeof(attr
);
1279 attr
.RootDirectory
= root
;
1280 attr
.ObjectName
= &nameW
;
1281 attr
.Attributes
= 0;
1282 attr
.SecurityDescriptor
= NULL
;
1283 attr
.SecurityQualityOfService
= NULL
;
1284 RtlInitUnicodeString( &nameW
, WineW
);
1286 /* @@ Wine registry key: HKCU\Software\Wine */
1287 if (!NtOpenKey( &hkey
, KEY_ALL_ACCESS
, &attr
))
1289 RtlInitUnicodeString( &nameW
, ShowDotFilesW
);
1290 if (!NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
, tmp
, sizeof(tmp
), &dummy
))
1292 WCHAR
*str
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)tmp
)->Data
;
1293 show_dot_files
= IS_OPTION_TRUE( str
[0] );
1299 /* a couple of directories that we don't want to return in directory searches */
1300 ignore_file( wine_get_config_dir() );
1301 ignore_file( "/dev" );
1302 ignore_file( "/proc" );
1304 ignore_file( "/sys" );
1310 /***********************************************************************
1311 * DIR_is_hidden_file
1313 * Check if the specified file should be hidden based on its name and the show dot files option.
1315 BOOL
DIR_is_hidden_file( const UNICODE_STRING
*name
)
1319 RtlRunOnceExecuteOnce( &init_once
, init_options
, NULL
, NULL
);
1321 if (show_dot_files
) return FALSE
;
1323 end
= p
= name
->Buffer
+ name
->Length
/sizeof(WCHAR
);
1324 while (p
> name
->Buffer
&& IS_SEPARATOR(p
[-1])) p
--;
1325 while (p
> name
->Buffer
&& !IS_SEPARATOR(p
[-1])) p
--;
1326 if (p
== end
|| *p
!= '.') return FALSE
;
1327 /* make sure it isn't '.' or '..' */
1328 if (p
+ 1 == end
) return FALSE
;
1329 if (p
[1] == '.' && p
+ 2 == end
) return FALSE
;
1334 /***********************************************************************
1335 * hash_short_file_name
1337 * Transform a Unix file name into a hashed DOS name. If the name is a valid
1338 * DOS name, it is converted to upper-case; otherwise it is replaced by a
1339 * hashed version that fits in 8.3 format.
1340 * 'buffer' must be at least 12 characters long.
1341 * Returns length of short name in bytes; short name is NOT null-terminated.
1343 static ULONG
hash_short_file_name( const UNICODE_STRING
*name
, LPWSTR buffer
)
1345 static const char hash_chars
[32] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345";
1347 LPCWSTR p
, ext
, end
= name
->Buffer
+ name
->Length
/ sizeof(WCHAR
);
1349 unsigned short hash
;
1352 /* Compute the hash code of the file name */
1353 /* If you know something about hash functions, feel free to */
1354 /* insert a better algorithm here... */
1355 if (!is_case_sensitive
)
1357 for (p
= name
->Buffer
, hash
= 0xbeef; p
< end
- 1; p
++)
1358 hash
= (hash
<<3) ^ (hash
>>5) ^ tolowerW(*p
) ^ (tolowerW(p
[1]) << 8);
1359 hash
= (hash
<<3) ^ (hash
>>5) ^ tolowerW(*p
); /* Last character */
1363 for (p
= name
->Buffer
, hash
= 0xbeef; p
< end
- 1; p
++)
1364 hash
= (hash
<< 3) ^ (hash
>> 5) ^ *p
^ (p
[1] << 8);
1365 hash
= (hash
<< 3) ^ (hash
>> 5) ^ *p
; /* Last character */
1368 /* Find last dot for start of the extension */
1369 for (p
= name
->Buffer
+ 1, ext
= NULL
; p
< end
- 1; p
++) if (*p
== '.') ext
= p
;
1371 /* Copy first 4 chars, replacing invalid chars with '_' */
1372 for (i
= 4, p
= name
->Buffer
, dst
= buffer
; i
> 0; i
--, p
++)
1374 if (p
== end
|| p
== ext
) break;
1375 *dst
++ = is_invalid_dos_char(*p
) ? '_' : toupperW(*p
);
1377 /* Pad to 5 chars with '~' */
1378 while (i
-- >= 0) *dst
++ = '~';
1380 /* Insert hash code converted to 3 ASCII chars */
1381 *dst
++ = hash_chars
[(hash
>> 10) & 0x1f];
1382 *dst
++ = hash_chars
[(hash
>> 5) & 0x1f];
1383 *dst
++ = hash_chars
[hash
& 0x1f];
1385 /* Copy the first 3 chars of the extension (if any) */
1389 for (i
= 3, ext
++; (i
> 0) && ext
< end
; i
--, ext
++)
1390 *dst
++ = is_invalid_dos_char(*ext
) ? '_' : toupperW(*ext
);
1392 return dst
- buffer
;
1396 /***********************************************************************
1399 * Check a long file name against a mask.
1401 * Tests (done in W95 DOS shell - case insensitive):
1402 * *.txt test1.test.txt *
1404 * *.t??????.t* test1.ta.tornado.txt *
1405 * *tornado* test1.ta.tornado.txt *
1406 * t*t test1.ta.tornado.txt *
1408 * ?est??? test1.txt -
1409 * *test1.txt* test1.txt *
1410 * h?l?o*t.dat hellothisisatest.dat *
1412 static BOOLEAN
match_filename( const UNICODE_STRING
*name_str
, const UNICODE_STRING
*mask_str
)
1415 const WCHAR
*name
= name_str
->Buffer
;
1416 const WCHAR
*mask
= mask_str
->Buffer
;
1417 const WCHAR
*name_end
= name
+ name_str
->Length
/ sizeof(WCHAR
);
1418 const WCHAR
*mask_end
= mask
+ mask_str
->Length
/ sizeof(WCHAR
);
1419 const WCHAR
*lastjoker
= NULL
;
1420 const WCHAR
*next_to_retry
= NULL
;
1422 while (name
< name_end
&& mask
< mask_end
)
1428 while (mask
< mask_end
&& *mask
== '*') mask
++; /* Skip consecutive '*' */
1429 if (mask
== mask_end
) return TRUE
; /* end of mask is all '*', so match */
1432 /* skip to the next match after the joker(s) */
1433 if (is_case_sensitive
)
1434 while (name
< name_end
&& (*name
!= *mask
)) name
++;
1436 while (name
< name_end
&& (toupperW(*name
) != toupperW(*mask
))) name
++;
1437 next_to_retry
= name
;
1444 if (is_case_sensitive
) mismatch
= (*mask
!= *name
);
1445 else mismatch
= (toupperW(*mask
) != toupperW(*name
));
1451 if (mask
== mask_end
)
1453 if (name
== name_end
) return TRUE
;
1454 if (lastjoker
) mask
= lastjoker
;
1457 else /* mismatch ! */
1459 if (lastjoker
) /* we had an '*', so we can try unlimitedly */
1463 /* this scan sequence was a mismatch, so restart
1464 * 1 char after the first char we checked last time */
1466 name
= next_to_retry
;
1468 else return FALSE
; /* bad luck */
1473 while (mask
< mask_end
&& ((*mask
== '.') || (*mask
== '*')))
1474 mask
++; /* Ignore trailing '.' or '*' in mask */
1475 return (name
== name_end
&& mask
== mask_end
);
1479 /***********************************************************************
1482 * Add a file to the directory data if it matches the mask.
1484 static BOOL
append_entry( struct dir_data
*data
, const char *long_name
,
1485 const char *short_name
, const UNICODE_STRING
*mask
)
1487 int i
, long_len
, short_len
;
1488 WCHAR long_nameW
[MAX_DIR_ENTRY_LEN
+ 1];
1489 WCHAR short_nameW
[13];
1492 long_len
= ntdll_umbstowcs( 0, long_name
, strlen(long_name
), long_nameW
, MAX_DIR_ENTRY_LEN
);
1493 if (long_len
== -1) return TRUE
;
1494 long_nameW
[long_len
] = 0;
1496 str
.Buffer
= long_nameW
;
1497 str
.Length
= long_len
* sizeof(WCHAR
);
1498 str
.MaximumLength
= sizeof(long_nameW
);
1502 short_len
= ntdll_umbstowcs( 0, short_name
, strlen(short_name
),
1503 short_nameW
, sizeof(short_nameW
) / sizeof(WCHAR
) - 1 );
1504 if (short_len
== -1) short_len
= sizeof(short_nameW
) / sizeof(WCHAR
) - 1;
1505 for (i
= 0; i
< short_len
; i
++) short_nameW
[i
] = toupperW( short_nameW
[i
] );
1507 else /* generate a short name if necessary */
1512 if (!RtlIsNameLegalDOS8Dot3( &str
, NULL
, &spaces
) || spaces
)
1513 short_len
= hash_short_file_name( &str
, short_nameW
);
1515 short_nameW
[short_len
] = 0;
1517 TRACE( "long %s short %s mask %s\n",
1518 debugstr_w( long_nameW
), debugstr_w( short_nameW
), debugstr_us( mask
));
1520 if (mask
&& !match_filename( &str
, mask
))
1522 if (!short_len
) return TRUE
; /* no short name to match */
1523 str
.Buffer
= short_nameW
;
1524 str
.Length
= short_len
* sizeof(WCHAR
);
1525 str
.MaximumLength
= sizeof(short_nameW
);
1526 if (!match_filename( &str
, mask
)) return TRUE
;
1529 return add_dir_data_names( data
, long_nameW
, short_nameW
, long_name
);
1533 /***********************************************************************
1534 * get_dir_data_entry
1536 * Return a directory entry from the cached data.
1538 static NTSTATUS
get_dir_data_entry( struct dir_data
*dir_data
, void *info_ptr
, IO_STATUS_BLOCK
*io
,
1539 ULONG max_length
, FILE_INFORMATION_CLASS
class,
1540 union file_directory_info
**last_info
)
1542 const struct dir_data_names
*names
= &dir_data
->names
[dir_data
->pos
];
1543 union file_directory_info
*info
;
1545 ULONG name_len
, start
, dir_size
, attributes
;
1547 if (get_file_info( names
->unix_name
, &st
, &attributes
) == -1)
1549 TRACE( "file no longer exists %s\n", names
->unix_name
);
1550 return STATUS_SUCCESS
;
1552 if (is_ignored_file( &st
))
1554 TRACE( "ignoring file %s\n", names
->unix_name
);
1555 return STATUS_SUCCESS
;
1557 start
= dir_info_align( io
->Information
);
1558 dir_size
= dir_info_size( class, 0 );
1559 if (start
+ dir_size
> max_length
) return STATUS_MORE_ENTRIES
;
1561 max_length
-= start
+ dir_size
;
1562 name_len
= strlenW( names
->long_name
) * sizeof(WCHAR
);
1563 /* if this is not the first entry, fail; the first entry is always returned (but truncated) */
1564 if (*last_info
&& name_len
> max_length
) return STATUS_MORE_ENTRIES
;
1566 info
= (union file_directory_info
*)((char *)info_ptr
+ start
);
1567 info
->dir
.NextEntryOffset
= 0;
1568 info
->dir
.FileIndex
= 0; /* NTFS always has 0 here, so let's not bother with it */
1570 /* all the structures except FileNamesInformation start with a FileDirectoryInformation layout */
1571 if (class != FileNamesInformation
)
1573 if (st
.st_dev
!= dir_data
->id
.dev
) st
.st_ino
= 0; /* ignore inode if on a different device */
1575 if (!show_dot_files
&& names
->long_name
[0] == '.' && names
->long_name
[1] &&
1576 (names
->long_name
[1] != '.' || names
->long_name
[2]))
1577 attributes
|= FILE_ATTRIBUTE_HIDDEN
;
1579 fill_file_info( &st
, attributes
, info
, class );
1584 case FileDirectoryInformation
:
1585 info
->dir
.FileNameLength
= name_len
;
1588 case FileFullDirectoryInformation
:
1589 info
->full
.EaSize
= 0; /* FIXME */
1590 info
->full
.FileNameLength
= name_len
;
1593 case FileIdFullDirectoryInformation
:
1594 info
->id_full
.EaSize
= 0; /* FIXME */
1595 info
->id_full
.FileNameLength
= name_len
;
1598 case FileBothDirectoryInformation
:
1599 info
->both
.EaSize
= 0; /* FIXME */
1600 info
->both
.ShortNameLength
= strlenW( names
->short_name
) * sizeof(WCHAR
);
1601 memcpy( info
->both
.ShortName
, names
->short_name
, info
->both
.ShortNameLength
);
1602 info
->both
.FileNameLength
= name_len
;
1605 case FileIdBothDirectoryInformation
:
1606 info
->id_both
.EaSize
= 0; /* FIXME */
1607 info
->id_both
.ShortNameLength
= strlenW( names
->short_name
) * sizeof(WCHAR
);
1608 memcpy( info
->id_both
.ShortName
, names
->short_name
, info
->id_both
.ShortNameLength
);
1609 info
->id_both
.FileNameLength
= name_len
;
1612 case FileIdGlobalTxDirectoryInformation
:
1613 info
->id_tx
.TxInfoFlags
= 0;
1614 info
->id_tx
.FileNameLength
= name_len
;
1617 case FileNamesInformation
:
1618 info
->names
.FileNameLength
= name_len
;
1626 memcpy( (char *)info
+ dir_size
, names
->long_name
, min( name_len
, max_length
) );
1627 io
->Information
= start
+ dir_size
+ min( name_len
, max_length
);
1628 if (*last_info
) (*last_info
)->next
= (char *)info
- (char *)*last_info
;
1630 return name_len
> max_length
? STATUS_BUFFER_OVERFLOW
: STATUS_SUCCESS
;
1633 #ifdef VFAT_IOCTL_READDIR_BOTH
1635 /***********************************************************************
1638 * Wrapper for the VFAT ioctl to work around various kernel bugs.
1639 * dir_section must be held by caller.
1641 static KERNEL_DIRENT
*start_vfat_ioctl( int fd
)
1643 static KERNEL_DIRENT
*de
;
1648 SIZE_T size
= 2 * sizeof(*de
) + page_size
;
1651 if (NtAllocateVirtualMemory( GetCurrentProcess(), &addr
, 1, &size
, MEM_RESERVE
, PAGE_READWRITE
))
1653 /* commit only the size needed for the dir entries */
1654 /* this leaves an extra unaccessible page, which should make the kernel */
1655 /* fail with -EFAULT before it stomps all over our memory */
1657 size
= 2 * sizeof(*de
);
1658 NtAllocateVirtualMemory( GetCurrentProcess(), &addr
, 1, &size
, MEM_COMMIT
, PAGE_READWRITE
);
1661 /* set d_reclen to 65535 to work around an AFS kernel bug */
1662 de
[0].d_reclen
= 65535;
1663 res
= ioctl( fd
, VFAT_IOCTL_READDIR_BOTH
, (long)de
);
1666 if (errno
!= ENOENT
) return NULL
; /* VFAT ioctl probably not supported */
1667 de
[0].d_reclen
= 0; /* eof */
1669 else if (!res
&& de
[0].d_reclen
== 65535) return NULL
; /* AFS bug */
1675 /***********************************************************************
1676 * read_directory_vfat
1678 * Read a directory using the VFAT ioctl; helper for NtQueryDirectoryFile.
1680 static NTSTATUS
read_directory_data_vfat( struct dir_data
*data
, int fd
, const UNICODE_STRING
*mask
)
1682 char *short_name
, *long_name
;
1685 NTSTATUS status
= STATUS_NO_MEMORY
;
1686 off_t old_pos
= lseek( fd
, 0, SEEK_CUR
);
1688 if (!(de
= start_vfat_ioctl( fd
))) return STATUS_NOT_SUPPORTED
;
1690 lseek( fd
, 0, SEEK_SET
);
1692 if (!append_entry( data
, ".", NULL
, mask
)) goto done
;
1693 if (!append_entry( data
, "..", NULL
, mask
)) goto done
;
1695 while (ioctl( fd
, VFAT_IOCTL_READDIR_BOTH
, (long)de
) != -1)
1697 if (!de
[0].d_reclen
) break; /* eof */
1699 /* make sure names are null-terminated to work around an x86-64 kernel bug */
1700 len
= min( de
[0].d_reclen
, sizeof(de
[0].d_name
) - 1 );
1701 de
[0].d_name
[len
] = 0;
1702 len
= min( de
[1].d_reclen
, sizeof(de
[1].d_name
) - 1 );
1703 de
[1].d_name
[len
] = 0;
1705 if (!strcmp( de
[0].d_name
, "." ) || !strcmp( de
[0].d_name
, ".." )) continue;
1706 if (de
[1].d_name
[0])
1708 short_name
= de
[0].d_name
;
1709 long_name
= de
[1].d_name
;
1713 long_name
= de
[0].d_name
;
1716 if (!append_entry( data
, long_name
, short_name
, mask
)) goto done
;
1718 status
= STATUS_SUCCESS
;
1720 lseek( fd
, old_pos
, SEEK_SET
);
1723 #endif /* VFAT_IOCTL_READDIR_BOTH */
1726 #ifdef HAVE_GETATTRLIST
1727 /***********************************************************************
1728 * read_directory_getattrlist
1730 * Read a single file from a directory by determining whether the file
1731 * identified by mask exists using getattrlist.
1733 static NTSTATUS
read_directory_data_getattrlist( struct dir_data
*data
, const char *unix_name
)
1735 struct attrlist attrlist
;
1736 #include "pshpack4.h"
1740 struct attrreference name_reference
;
1742 char name
[NAME_MAX
* 3 + 1];
1744 #include "poppack.h"
1746 memset( &attrlist
, 0, sizeof(attrlist
) );
1747 attrlist
.bitmapcount
= ATTR_BIT_MAP_COUNT
;
1748 attrlist
.commonattr
= ATTR_CMN_NAME
| ATTR_CMN_OBJTYPE
;
1749 if (getattrlist( unix_name
, &attrlist
, &buffer
, sizeof(buffer
), FSOPT_NOFOLLOW
) == -1)
1750 return STATUS_NO_SUCH_FILE
;
1751 /* If unix_name named a symlink, the above may have succeeded even if the symlink is broken.
1752 Check that with another call without FSOPT_NOFOLLOW. We don't ask for any attributes. */
1753 if (buffer
.type
== VLNK
)
1756 attrlist
.commonattr
= 0;
1757 if (getattrlist( unix_name
, &attrlist
, &dummy
, sizeof(dummy
), 0 ) == -1)
1758 return STATUS_NO_SUCH_FILE
;
1761 TRACE( "found %s\n", buffer
.name
);
1763 if (!append_entry( data
, buffer
.name
, NULL
, NULL
)) return STATUS_NO_MEMORY
;
1765 return STATUS_SUCCESS
;
1767 #endif /* HAVE_GETATTRLIST */
1770 /***********************************************************************
1771 * read_directory_stat
1773 * Read a single file from a directory by determining whether the file
1774 * identified by mask exists using stat.
1776 static NTSTATUS
read_directory_data_stat( struct dir_data
*data
, const char *unix_name
)
1780 /* if the file system is not case sensitive we can't find the actual name through stat() */
1781 if (!get_dir_case_sensitivity(".")) return STATUS_NO_SUCH_FILE
;
1782 if (stat( unix_name
, &st
) == -1) return STATUS_NO_SUCH_FILE
;
1784 TRACE( "found %s\n", unix_name
);
1786 if (!append_entry( data
, unix_name
, NULL
, NULL
)) return STATUS_NO_MEMORY
;
1788 return STATUS_SUCCESS
;
1792 /***********************************************************************
1793 * read_directory_readdir
1795 * Read a directory using the POSIX readdir interface; helper for NtQueryDirectoryFile.
1797 static NTSTATUS
read_directory_data_readdir( struct dir_data
*data
, const UNICODE_STRING
*mask
)
1800 NTSTATUS status
= STATUS_NO_MEMORY
;
1801 DIR *dir
= opendir( "." );
1803 if (!dir
) return STATUS_NO_SUCH_FILE
;
1805 if (!append_entry( data
, ".", NULL
, mask
)) goto done
;
1806 if (!append_entry( data
, "..", NULL
, mask
)) goto done
;
1807 while ((de
= readdir( dir
)))
1809 if (!strcmp( de
->d_name
, "." ) || !strcmp( de
->d_name
, ".." )) continue;
1810 if (!append_entry( data
, de
->d_name
, NULL
, mask
)) goto done
;
1812 status
= STATUS_SUCCESS
;
1820 /***********************************************************************
1821 * read_directory_data
1823 * Read the full contents of a directory, using one of the above helper functions.
1825 static NTSTATUS
read_directory_data( struct dir_data
*data
, int fd
, const UNICODE_STRING
*mask
)
1829 #ifdef VFAT_IOCTL_READDIR_BOTH
1830 if (!(status
= read_directory_data_vfat( data
, fd
, mask
))) return status
;
1833 if (!has_wildcard( mask
))
1835 /* convert the mask to a Unix name and check for it */
1836 int ret
, used_default
;
1837 char unix_name
[MAX_DIR_ENTRY_LEN
* 3 + 1];
1839 ret
= ntdll_wcstoumbs( 0, mask
->Buffer
, mask
->Length
/ sizeof(WCHAR
),
1840 unix_name
, sizeof(unix_name
) - 1, NULL
, &used_default
);
1841 if (ret
> 0 && !used_default
)
1844 #ifdef HAVE_GETATTRLIST
1845 if (!(status
= read_directory_data_getattrlist( data
, unix_name
))) return status
;
1847 if (!(status
= read_directory_data_stat( data
, unix_name
))) return status
;
1851 return read_directory_data_readdir( data
, mask
);
1855 /* compare file names for directory sorting */
1856 static int name_compare( const void *a
, const void *b
)
1858 const struct dir_data_names
*file_a
= (const struct dir_data_names
*)a
;
1859 const struct dir_data_names
*file_b
= (const struct dir_data_names
*)b
;
1860 int ret
= RtlCompareUnicodeStrings( file_a
->long_name
, strlenW(file_a
->long_name
),
1861 file_b
->long_name
, strlenW(file_b
->long_name
), TRUE
);
1862 if (!ret
) ret
= strcmpW( file_a
->long_name
, file_b
->long_name
);
1867 /***********************************************************************
1868 * init_cached_dir_data
1870 * Initialize the cached directory contents.
1872 static NTSTATUS
init_cached_dir_data( struct dir_data
**data_ret
, int fd
, const UNICODE_STRING
*mask
)
1874 struct dir_data
*data
;
1879 if (!(data
= RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*data
) )))
1880 return STATUS_NO_MEMORY
;
1882 if ((status
= read_directory_data( data
, fd
, mask
)))
1884 free_dir_data( data
);
1888 /* sort filenames, but not "." and ".." */
1890 if (i
< data
->count
&& !strcmp( data
->names
[i
].unix_name
, "." )) i
++;
1891 if (i
< data
->count
&& !strcmp( data
->names
[i
].unix_name
, ".." )) i
++;
1892 if (i
< data
->count
) qsort( data
->names
+ i
, data
->count
- i
, sizeof(*data
->names
), name_compare
);
1896 /* release unused space */
1898 RtlReAllocateHeap( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY
, data
->buffer
,
1899 offsetof( struct dir_data_buffer
, data
[data
->buffer
->pos
] ));
1900 if (data
->count
< data
->size
)
1901 RtlReAllocateHeap( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY
, data
->names
,
1902 data
->count
* sizeof(*data
->names
) );
1903 if (!fstat( fd
, &st
))
1905 data
->id
.dev
= st
.st_dev
;
1906 data
->id
.ino
= st
.st_ino
;
1910 TRACE( "mask %s found %u files\n", debugstr_us( mask
), data
->count
);
1911 for (i
= 0; i
< data
->count
; i
++)
1912 TRACE( "%s %s\n", debugstr_w(data
->names
[i
].long_name
), debugstr_w(data
->names
[i
].short_name
) );
1915 return data
->count
? STATUS_SUCCESS
: STATUS_NO_SUCH_FILE
;
1919 /***********************************************************************
1920 * get_cached_dir_data
1922 * Retrieve the cached directory data, or initialize it if necessary.
1924 static NTSTATUS
get_cached_dir_data( HANDLE handle
, struct dir_data
**data_ret
, int fd
,
1925 const UNICODE_STRING
*mask
)
1928 int entry
= -1, free_entries
[16];
1931 SERVER_START_REQ( get_directory_cache_entry
)
1933 req
->handle
= wine_server_obj_handle( handle
);
1934 wine_server_set_reply( req
, free_entries
, sizeof(free_entries
) );
1935 if (!(status
= wine_server_call( req
))) entry
= reply
->entry
;
1937 for (i
= 0; i
< wine_server_reply_size( reply
) / sizeof(*free_entries
); i
++)
1939 int free_idx
= free_entries
[i
];
1940 if (free_idx
< dir_data_cache_size
)
1942 free_dir_data( dir_data_cache
[free_idx
] );
1943 dir_data_cache
[free_idx
] = NULL
;
1951 if (status
== STATUS_SHARING_VIOLATION
) FIXME( "shared directory handle not supported yet\n" );
1955 if (entry
>= dir_data_cache_size
)
1957 unsigned int size
= max( dir_data_cache_initial_size
, max( dir_data_cache_size
* 2, entry
+ 1 ) );
1958 struct dir_data
**new_cache
;
1961 new_cache
= RtlReAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY
, dir_data_cache
,
1962 size
* sizeof(*new_cache
) );
1964 new_cache
= RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY
, size
* sizeof(*new_cache
) );
1965 if (!new_cache
) return STATUS_NO_MEMORY
;
1966 dir_data_cache
= new_cache
;
1967 dir_data_cache_size
= size
;
1970 if (!dir_data_cache
[entry
]) status
= init_cached_dir_data( &dir_data_cache
[entry
], fd
, mask
);
1972 *data_ret
= dir_data_cache
[entry
];
1977 /******************************************************************************
1978 * NtQueryDirectoryFile [NTDLL.@]
1979 * ZwQueryDirectoryFile [NTDLL.@]
1981 NTSTATUS WINAPI
NtQueryDirectoryFile( HANDLE handle
, HANDLE event
,
1982 PIO_APC_ROUTINE apc_routine
, PVOID apc_context
,
1983 PIO_STATUS_BLOCK io
,
1984 PVOID buffer
, ULONG length
,
1985 FILE_INFORMATION_CLASS info_class
,
1986 BOOLEAN single_entry
,
1987 PUNICODE_STRING mask
,
1988 BOOLEAN restart_scan
)
1990 int cwd
, fd
, needs_close
;
1991 struct dir_data
*data
;
1994 TRACE("(%p %p %p %p %p %p 0x%08x 0x%08x 0x%08x %s 0x%08x\n",
1995 handle
, event
, apc_routine
, apc_context
, io
, buffer
,
1996 length
, info_class
, single_entry
, debugstr_us(mask
),
1999 if (event
|| apc_routine
)
2001 FIXME( "Unsupported yet option\n" );
2002 return STATUS_NOT_IMPLEMENTED
;
2006 case FileDirectoryInformation
:
2007 case FileBothDirectoryInformation
:
2008 case FileFullDirectoryInformation
:
2009 case FileIdBothDirectoryInformation
:
2010 case FileIdFullDirectoryInformation
:
2011 case FileIdGlobalTxDirectoryInformation
:
2012 case FileNamesInformation
:
2013 if (length
< dir_info_align( dir_info_size( info_class
, 1 ))) return STATUS_INFO_LENGTH_MISMATCH
;
2015 case FileObjectIdInformation
:
2016 if (length
!= sizeof(FILE_OBJECTID_INFORMATION
)) return STATUS_INFO_LENGTH_MISMATCH
;
2017 return STATUS_INVALID_INFO_CLASS
;
2018 case FileQuotaInformation
:
2019 if (length
!= sizeof(FILE_QUOTA_INFORMATION
)) return STATUS_INFO_LENGTH_MISMATCH
;
2020 return STATUS_INVALID_INFO_CLASS
;
2021 case FileReparsePointInformation
:
2022 if (length
!= sizeof(FILE_REPARSE_POINT_INFORMATION
)) return STATUS_INFO_LENGTH_MISMATCH
;
2023 return STATUS_INVALID_INFO_CLASS
;
2025 return STATUS_INVALID_INFO_CLASS
;
2027 if (!buffer
) return STATUS_ACCESS_VIOLATION
;
2029 if ((status
= server_get_unix_fd( handle
, FILE_LIST_DIRECTORY
, &fd
, &needs_close
, NULL
, NULL
)) != STATUS_SUCCESS
)
2032 io
->Information
= 0;
2034 RtlRunOnceExecuteOnce( &init_once
, init_options
, NULL
, NULL
);
2036 RtlEnterCriticalSection( &dir_section
);
2038 cwd
= open( ".", O_RDONLY
);
2039 if (fchdir( fd
) != -1)
2041 if (!(status
= get_cached_dir_data( handle
, &data
, fd
, mask
)))
2043 union file_directory_info
*last_info
= NULL
;
2045 if (restart_scan
) data
->pos
= 0;
2047 while (!status
&& data
->pos
< data
->count
)
2049 status
= get_dir_data_entry( data
, buffer
, io
, length
, info_class
, &last_info
);
2050 if (!status
|| status
== STATUS_BUFFER_OVERFLOW
) data
->pos
++;
2051 if (single_entry
) break;
2054 if (!last_info
) status
= STATUS_NO_MORE_FILES
;
2055 else if (status
== STATUS_MORE_ENTRIES
) status
= STATUS_SUCCESS
;
2057 io
->u
.Status
= status
;
2059 if (cwd
== -1 || fchdir( cwd
) == -1) chdir( "/" );
2061 else status
= FILE_GetNtStatus();
2063 RtlLeaveCriticalSection( &dir_section
);
2065 if (needs_close
) close( fd
);
2066 if (cwd
!= -1) close( cwd
);
2067 TRACE( "=> %x (%ld)\n", status
, io
->Information
);
2072 /***********************************************************************
2075 * Find a file in a directory the hard way, by doing a case-insensitive search.
2076 * The file found is appended to unix_name at pos.
2077 * There must be at least MAX_DIR_ENTRY_LEN+2 chars available at pos.
2079 static NTSTATUS
find_file_in_dir( char *unix_name
, int pos
, const WCHAR
*name
, int length
,
2080 BOOLEAN check_case
, BOOLEAN
*is_win_dir
)
2082 WCHAR buffer
[MAX_DIR_ENTRY_LEN
];
2084 BOOLEAN spaces
, is_name_8_dot_3
;
2088 int ret
, used_default
;
2090 /* try a shortcut for this directory */
2092 unix_name
[pos
++] = '/';
2093 ret
= ntdll_wcstoumbs( 0, name
, length
, unix_name
+ pos
, MAX_DIR_ENTRY_LEN
,
2094 NULL
, &used_default
);
2095 /* if we used the default char, the Unix name won't round trip properly back to Unicode */
2096 /* so it cannot match the file we are looking for */
2097 if (ret
>= 0 && !used_default
)
2099 unix_name
[pos
+ ret
] = 0;
2100 if (!stat( unix_name
, &st
))
2102 if (is_win_dir
) *is_win_dir
= is_same_file( &windir
, &st
);
2103 return STATUS_SUCCESS
;
2106 if (check_case
) goto not_found
; /* we want an exact match */
2108 if (pos
> 1) unix_name
[pos
- 1] = 0;
2109 else unix_name
[1] = 0; /* keep the initial slash */
2111 /* check if it fits in 8.3 so that we don't look for short names if we won't need them */
2113 str
.Buffer
= (WCHAR
*)name
;
2114 str
.Length
= length
* sizeof(WCHAR
);
2115 str
.MaximumLength
= str
.Length
;
2116 is_name_8_dot_3
= RtlIsNameLegalDOS8Dot3( &str
, NULL
, &spaces
) && !spaces
;
2117 #ifndef VFAT_IOCTL_READDIR_BOTH
2118 is_name_8_dot_3
= is_name_8_dot_3
&& length
>= 8 && name
[4] == '~';
2121 if (!is_name_8_dot_3
&& !get_dir_case_sensitivity( unix_name
)) goto not_found
;
2123 /* now look for it through the directory */
2125 #ifdef VFAT_IOCTL_READDIR_BOTH
2126 if (is_name_8_dot_3
)
2128 int fd
= open( unix_name
, O_RDONLY
| O_DIRECTORY
);
2133 RtlEnterCriticalSection( &dir_section
);
2134 if ((kde
= start_vfat_ioctl( fd
)))
2136 unix_name
[pos
- 1] = '/';
2137 while (kde
[0].d_reclen
)
2139 /* make sure names are null-terminated to work around an x86-64 kernel bug */
2140 size_t len
= min(kde
[0].d_reclen
, sizeof(kde
[0].d_name
) - 1 );
2141 kde
[0].d_name
[len
] = 0;
2142 len
= min(kde
[1].d_reclen
, sizeof(kde
[1].d_name
) - 1 );
2143 kde
[1].d_name
[len
] = 0;
2145 if (kde
[1].d_name
[0])
2147 ret
= ntdll_umbstowcs( 0, kde
[1].d_name
, strlen(kde
[1].d_name
),
2148 buffer
, MAX_DIR_ENTRY_LEN
);
2149 if (ret
== length
&& !memicmpW( buffer
, name
, length
))
2151 strcpy( unix_name
+ pos
, kde
[1].d_name
);
2152 RtlLeaveCriticalSection( &dir_section
);
2157 ret
= ntdll_umbstowcs( 0, kde
[0].d_name
, strlen(kde
[0].d_name
),
2158 buffer
, MAX_DIR_ENTRY_LEN
);
2159 if (ret
== length
&& !memicmpW( buffer
, name
, length
))
2161 strcpy( unix_name
+ pos
,
2162 kde
[1].d_name
[0] ? kde
[1].d_name
: kde
[0].d_name
);
2163 RtlLeaveCriticalSection( &dir_section
);
2167 if (ioctl( fd
, VFAT_IOCTL_READDIR_BOTH
, (long)kde
) == -1)
2169 RtlLeaveCriticalSection( &dir_section
);
2175 RtlLeaveCriticalSection( &dir_section
);
2178 /* fall through to normal handling */
2180 #endif /* VFAT_IOCTL_READDIR_BOTH */
2182 if (!(dir
= opendir( unix_name
)))
2184 if (errno
== ENOENT
) return STATUS_OBJECT_PATH_NOT_FOUND
;
2185 else return FILE_GetNtStatus();
2187 unix_name
[pos
- 1] = '/';
2188 str
.Buffer
= buffer
;
2189 str
.MaximumLength
= sizeof(buffer
);
2190 while ((de
= readdir( dir
)))
2192 ret
= ntdll_umbstowcs( 0, de
->d_name
, strlen(de
->d_name
), buffer
, MAX_DIR_ENTRY_LEN
);
2193 if (ret
== length
&& !memicmpW( buffer
, name
, length
))
2195 strcpy( unix_name
+ pos
, de
->d_name
);
2200 if (!is_name_8_dot_3
) continue;
2202 str
.Length
= ret
* sizeof(WCHAR
);
2203 if (!RtlIsNameLegalDOS8Dot3( &str
, NULL
, &spaces
) || spaces
)
2205 WCHAR short_nameW
[12];
2206 ret
= hash_short_file_name( &str
, short_nameW
);
2207 if (ret
== length
&& !memicmpW( short_nameW
, name
, length
))
2209 strcpy( unix_name
+ pos
, de
->d_name
);
2218 unix_name
[pos
- 1] = 0;
2219 return STATUS_OBJECT_PATH_NOT_FOUND
;
2222 if (is_win_dir
&& !stat( unix_name
, &st
)) *is_win_dir
= is_same_file( &windir
, &st
);
2223 return STATUS_SUCCESS
;
2229 static const WCHAR catrootW
[] = {'s','y','s','t','e','m','3','2','\\','c','a','t','r','o','o','t',0};
2230 static const WCHAR catroot2W
[] = {'s','y','s','t','e','m','3','2','\\','c','a','t','r','o','o','t','2',0};
2231 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};
2232 static const WCHAR driversetcW
[] = {'s','y','s','t','e','m','3','2','\\','d','r','i','v','e','r','s','\\','e','t','c',0};
2233 static const WCHAR logfilesW
[] = {'s','y','s','t','e','m','3','2','\\','l','o','g','f','i','l','e','s',0};
2234 static const WCHAR spoolW
[] = {'s','y','s','t','e','m','3','2','\\','s','p','o','o','l',0};
2235 static const WCHAR system32W
[] = {'s','y','s','t','e','m','3','2',0};
2236 static const WCHAR syswow64W
[] = {'s','y','s','w','o','w','6','4',0};
2237 static const WCHAR sysnativeW
[] = {'s','y','s','n','a','t','i','v','e',0};
2238 static const WCHAR regeditW
[] = {'r','e','g','e','d','i','t','.','e','x','e',0};
2239 static const WCHAR wow_regeditW
[] = {'s','y','s','w','o','w','6','4','\\','r','e','g','e','d','i','t','.','e','x','e',0};
2243 const WCHAR
*source
;
2244 const WCHAR
*dos_target
;
2245 const char *unix_target
;
2248 { catrootW
, NULL
, NULL
},
2249 { catroot2W
, NULL
, NULL
},
2250 { driversstoreW
, NULL
, NULL
},
2251 { driversetcW
, NULL
, NULL
},
2252 { logfilesW
, NULL
, NULL
},
2253 { spoolW
, NULL
, NULL
},
2254 { system32W
, syswow64W
, NULL
},
2255 { sysnativeW
, system32W
, NULL
},
2256 { regeditW
, wow_regeditW
, NULL
}
2259 static unsigned int nb_redirects
;
2262 /***********************************************************************
2263 * get_redirect_target
2265 * Find the target unix name for a redirected dir.
2267 static const char *get_redirect_target( const char *windows_dir
, const WCHAR
*name
)
2269 int used_default
, len
, pos
, win_len
= strlen( windows_dir
);
2270 char *unix_name
, *unix_target
= NULL
;
2273 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, win_len
+ MAX_DIR_ENTRY_LEN
+ 2 )))
2275 memcpy( unix_name
, windows_dir
, win_len
);
2280 const WCHAR
*end
, *next
;
2282 for (end
= name
; *end
; end
++) if (IS_SEPARATOR(*end
)) break;
2283 for (next
= end
; *next
; next
++) if (!IS_SEPARATOR(*next
)) break;
2285 status
= find_file_in_dir( unix_name
, pos
, name
, end
- name
, FALSE
, NULL
);
2286 if (status
== STATUS_OBJECT_PATH_NOT_FOUND
&& !*next
) /* not finding last element is ok */
2288 len
= ntdll_wcstoumbs( 0, name
, end
- name
, unix_name
+ pos
+ 1,
2289 MAX_DIR_ENTRY_LEN
- (pos
- win_len
), NULL
, &used_default
);
2290 if (len
> 0 && !used_default
)
2292 unix_name
[pos
] = '/';
2298 if (status
) goto done
;
2299 pos
+= strlen( unix_name
+ pos
);
2303 if ((unix_target
= RtlAllocateHeap( GetProcessHeap(), 0, pos
- win_len
)))
2304 memcpy( unix_target
, unix_name
+ win_len
+ 1, pos
- win_len
);
2307 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2312 /***********************************************************************
2315 static void init_redirects(void)
2317 UNICODE_STRING nt_name
;
2318 ANSI_STRING unix_name
;
2323 if (!RtlDosPathNameToNtPathName_U( user_shared_data
->NtSystemRoot
, &nt_name
, NULL
, NULL
))
2325 ERR( "can't convert %s\n", debugstr_w(user_shared_data
->NtSystemRoot
) );
2328 status
= wine_nt_to_unix_file_name( &nt_name
, &unix_name
, FILE_OPEN_IF
, FALSE
);
2329 RtlFreeUnicodeString( &nt_name
);
2332 ERR( "cannot open %s (%x)\n", debugstr_w(user_shared_data
->NtSystemRoot
), status
);
2335 if (!stat( unix_name
.Buffer
, &st
))
2337 windir
.dev
= st
.st_dev
;
2338 windir
.ino
= st
.st_ino
;
2339 nb_redirects
= sizeof(redirects
) / sizeof(redirects
[0]);
2340 for (i
= 0; i
< nb_redirects
; i
++)
2342 if (!redirects
[i
].dos_target
) continue;
2343 redirects
[i
].unix_target
= get_redirect_target( unix_name
.Buffer
, redirects
[i
].dos_target
);
2344 TRACE( "%s -> %s\n", debugstr_w(redirects
[i
].source
), redirects
[i
].unix_target
);
2347 RtlFreeAnsiString( &unix_name
);
2352 /***********************************************************************
2355 * Check if path matches a redirect name. If yes, return matched length.
2357 static int match_redirect( const WCHAR
*path
, int len
, const WCHAR
*redir
, BOOLEAN check_case
)
2361 while (i
< len
&& *redir
)
2363 if (IS_SEPARATOR(path
[i
]))
2365 if (*redir
++ != '\\') return 0;
2366 while (i
< len
&& IS_SEPARATOR(path
[i
])) i
++;
2367 continue; /* move on to next path component */
2369 else if (check_case
)
2371 if (path
[i
] != *redir
) return 0;
2375 if (tolowerW(path
[i
]) != tolowerW(*redir
)) return 0;
2380 if (*redir
) return 0;
2381 if (i
< len
&& !IS_SEPARATOR(path
[i
])) return 0;
2382 while (i
< len
&& IS_SEPARATOR(path
[i
])) i
++;
2387 /***********************************************************************
2390 * Retrieve the Unix path corresponding to a redirected path if any.
2392 static int get_redirect_path( char *unix_name
, int pos
, const WCHAR
*name
, int length
, BOOLEAN check_case
)
2397 for (i
= 0; i
< nb_redirects
; i
++)
2399 if ((len
= match_redirect( name
, length
, redirects
[i
].source
, check_case
)))
2401 if (!redirects
[i
].unix_target
) break;
2402 unix_name
[pos
++] = '/';
2403 strcpy( unix_name
+ pos
, redirects
[i
].unix_target
);
2412 /* there are no redirects on 64-bit */
2414 static const unsigned int nb_redirects
= 0;
2416 static int get_redirect_path( char *unix_name
, int pos
, const WCHAR
*name
, int length
, BOOLEAN check_case
)
2423 /***********************************************************************
2424 * DIR_init_windows_dir
2426 void DIR_init_windows_dir( const WCHAR
*win
, const WCHAR
*sys
)
2428 /* FIXME: should probably store paths as NT file names */
2430 RtlCreateUnicodeString( &system_dir
, sys
);
2433 if (is_wow64
) init_redirects();
2438 /******************************************************************************
2441 * Get the Unix path of a DOS device.
2443 static NTSTATUS
get_dos_device( const WCHAR
*name
, UINT name_len
, ANSI_STRING
*unix_name_ret
)
2445 const char *config_dir
= wine_get_config_dir();
2447 char *unix_name
, *new_name
, *dev
;
2451 /* make sure the device name is ASCII */
2452 for (i
= 0; i
< name_len
; i
++)
2453 if (name
[i
] <= 32 || name
[i
] >= 127) return STATUS_BAD_DEVICE_TYPE
;
2455 unix_len
= strlen(config_dir
) + sizeof("/dosdevices/") + name_len
+ 1;
2457 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, unix_len
)))
2458 return STATUS_NO_MEMORY
;
2460 strcpy( unix_name
, config_dir
);
2461 strcat( unix_name
, "/dosdevices/" );
2462 dev
= unix_name
+ strlen(unix_name
);
2464 for (i
= 0; i
< name_len
; i
++) dev
[i
] = (char)tolowerW(name
[i
]);
2467 /* special case for drive devices */
2468 if (name_len
== 2 && dev
[1] == ':')
2476 if (!stat( unix_name
, &st
))
2478 TRACE( "%s -> %s\n", debugstr_wn(name
,name_len
), debugstr_a(unix_name
) );
2479 unix_name_ret
->Buffer
= unix_name
;
2480 unix_name_ret
->Length
= strlen(unix_name
);
2481 unix_name_ret
->MaximumLength
= unix_len
;
2482 return STATUS_SUCCESS
;
2486 /* now try some defaults for it */
2487 if (!strcmp( dev
, "aux" ))
2489 strcpy( dev
, "com1" );
2492 if (!strcmp( dev
, "prn" ))
2494 strcpy( dev
, "lpt1" );
2499 if (dev
[1] == ':' && dev
[2] == ':') /* drive device */
2501 dev
[2] = 0; /* remove last ':' to get the drive mount point symlink */
2502 new_name
= get_default_drive_device( unix_name
);
2504 else if (!strncmp( dev
, "com", 3 )) new_name
= get_default_com_device( atoi(dev
+ 3 ));
2505 else if (!strncmp( dev
, "lpt", 3 )) new_name
= get_default_lpt_device( atoi(dev
+ 3 ));
2507 if (!new_name
) break;
2509 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2510 unix_name
= new_name
;
2511 unix_len
= strlen(unix_name
) + 1;
2512 dev
= NULL
; /* last try */
2514 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2515 return STATUS_BAD_DEVICE_TYPE
;
2519 /* return the length of the DOS namespace prefix if any */
2520 static inline int get_dos_prefix_len( const UNICODE_STRING
*name
)
2522 static const WCHAR nt_prefixW
[] = {'\\','?','?','\\'};
2523 static const WCHAR dosdev_prefixW
[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\'};
2525 if (name
->Length
>= sizeof(nt_prefixW
) &&
2526 !memcmp( name
->Buffer
, nt_prefixW
, sizeof(nt_prefixW
) ))
2527 return sizeof(nt_prefixW
) / sizeof(WCHAR
);
2529 if (name
->Length
>= sizeof(dosdev_prefixW
) &&
2530 !memicmpW( name
->Buffer
, dosdev_prefixW
, sizeof(dosdev_prefixW
)/sizeof(WCHAR
) ))
2531 return sizeof(dosdev_prefixW
) / sizeof(WCHAR
);
2537 /******************************************************************************
2540 * Recursively search directories from the dir queue for a given inode.
2542 static NTSTATUS
find_file_id( ANSI_STRING
*unix_name
, ULONGLONG file_id
, dev_t dev
)
2550 while (!(status
= next_dir_in_queue( unix_name
->Buffer
)))
2552 if (!(dir
= opendir( unix_name
->Buffer
))) continue;
2553 TRACE( "searching %s for %s\n", unix_name
->Buffer
, wine_dbgstr_longlong(file_id
) );
2554 pos
= strlen( unix_name
->Buffer
);
2555 if (pos
+ MAX_DIR_ENTRY_LEN
>= unix_name
->MaximumLength
/sizeof(WCHAR
))
2557 char *new = RtlReAllocateHeap( GetProcessHeap(), 0, unix_name
->Buffer
,
2558 unix_name
->MaximumLength
* 2 );
2562 return STATUS_NO_MEMORY
;
2564 unix_name
->MaximumLength
*= 2;
2565 unix_name
->Buffer
= new;
2567 unix_name
->Buffer
[pos
++] = '/';
2568 while ((de
= readdir( dir
)))
2570 if (!strcmp( de
->d_name
, "." ) || !strcmp( de
->d_name
, ".." )) continue;
2571 strcpy( unix_name
->Buffer
+ pos
, de
->d_name
);
2572 if (lstat( unix_name
->Buffer
, &st
) == -1) continue;
2573 if (st
.st_dev
!= dev
) continue;
2574 if (st
.st_ino
== file_id
)
2577 return STATUS_SUCCESS
;
2579 if (!S_ISDIR( st
.st_mode
)) continue;
2580 if ((status
= add_dir_to_queue( unix_name
->Buffer
)) != STATUS_SUCCESS
)
2592 /******************************************************************************
2593 * file_id_to_unix_file_name
2595 * Lookup a file from its file id instead of its name.
2597 NTSTATUS
file_id_to_unix_file_name( const OBJECT_ATTRIBUTES
*attr
, ANSI_STRING
*unix_name
)
2599 enum server_fd_type type
;
2600 int old_cwd
, root_fd
, needs_close
;
2603 struct stat st
, root_st
;
2605 if (attr
->ObjectName
->Length
!= sizeof(ULONGLONG
)) return STATUS_OBJECT_PATH_SYNTAX_BAD
;
2606 if (!attr
->RootDirectory
) return STATUS_INVALID_PARAMETER
;
2607 memcpy( &file_id
, attr
->ObjectName
->Buffer
, sizeof(file_id
) );
2609 unix_name
->MaximumLength
= 2 * MAX_DIR_ENTRY_LEN
+ 4;
2610 if (!(unix_name
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, unix_name
->MaximumLength
)))
2611 return STATUS_NO_MEMORY
;
2612 strcpy( unix_name
->Buffer
, "." );
2614 if ((status
= server_get_unix_fd( attr
->RootDirectory
, 0, &root_fd
, &needs_close
, &type
, NULL
)))
2617 if (type
!= FD_TYPE_DIR
)
2619 status
= STATUS_OBJECT_TYPE_MISMATCH
;
2623 fstat( root_fd
, &root_st
);
2624 if (root_st
.st_ino
== file_id
) /* shortcut for "." */
2626 status
= STATUS_SUCCESS
;
2630 RtlEnterCriticalSection( &dir_section
);
2631 if ((old_cwd
= open( ".", O_RDONLY
)) != -1 && fchdir( root_fd
) != -1)
2633 /* shortcut for ".." */
2634 if (!stat( "..", &st
) && st
.st_dev
== root_st
.st_dev
&& st
.st_ino
== file_id
)
2636 strcpy( unix_name
->Buffer
, ".." );
2637 status
= STATUS_SUCCESS
;
2641 status
= add_dir_to_queue( "." );
2643 status
= find_file_id( unix_name
, file_id
, root_st
.st_dev
);
2644 if (!status
) /* get rid of "./" prefix */
2645 memmove( unix_name
->Buffer
, unix_name
->Buffer
+ 2, strlen(unix_name
->Buffer
) - 1 );
2648 if (fchdir( old_cwd
) == -1) chdir( "/" );
2650 else status
= FILE_GetNtStatus();
2651 RtlLeaveCriticalSection( &dir_section
);
2652 if (old_cwd
!= -1) close( old_cwd
);
2655 if (status
== STATUS_SUCCESS
)
2657 TRACE( "%s -> %s\n", wine_dbgstr_longlong(file_id
), debugstr_a(unix_name
->Buffer
) );
2658 unix_name
->Length
= strlen( unix_name
->Buffer
);
2662 TRACE( "%s not found in dir %p\n", wine_dbgstr_longlong(file_id
), attr
->RootDirectory
);
2663 RtlFreeHeap( GetProcessHeap(), 0, unix_name
->Buffer
);
2665 if (needs_close
) close( root_fd
);
2670 /******************************************************************************
2673 * Helper for nt_to_unix_file_name
2675 static NTSTATUS
lookup_unix_name( const WCHAR
*name
, int name_len
, char **buffer
, int unix_len
, int pos
,
2676 UINT disposition
, BOOLEAN check_case
)
2679 int ret
, used_default
, len
;
2681 char *unix_name
= *buffer
;
2682 const BOOL redirect
= nb_redirects
&& ntdll_get_thread_data()->wow64_redir
;
2684 /* try a shortcut first */
2686 ret
= ntdll_wcstoumbs( 0, name
, name_len
, unix_name
+ pos
, unix_len
- pos
- 1,
2687 NULL
, &used_default
);
2689 while (name_len
&& IS_SEPARATOR(*name
))
2695 if (ret
>= 0 && !used_default
) /* if we used the default char the name didn't convert properly */
2698 unix_name
[pos
+ ret
] = 0;
2699 for (p
= unix_name
+ pos
; *p
; p
++) if (*p
== '\\') *p
= '/';
2700 if (!redirect
|| (!strstr( unix_name
, "/windows/") && strncmp( unix_name
, "windows/", 8 )))
2702 if (!stat( unix_name
, &st
))
2704 /* creation fails with STATUS_ACCESS_DENIED for the root of the drive */
2705 if (disposition
== FILE_CREATE
)
2706 return name_len
? STATUS_OBJECT_NAME_COLLISION
: STATUS_ACCESS_DENIED
;
2707 return STATUS_SUCCESS
;
2712 if (!name_len
) /* empty name -> drive root doesn't exist */
2713 return STATUS_OBJECT_PATH_NOT_FOUND
;
2714 if (check_case
&& !redirect
&& (disposition
== FILE_OPEN
|| disposition
== FILE_OVERWRITE
))
2715 return STATUS_OBJECT_NAME_NOT_FOUND
;
2717 /* now do it component by component */
2721 const WCHAR
*end
, *next
;
2722 BOOLEAN is_win_dir
= FALSE
;
2725 while (end
< name
+ name_len
&& !IS_SEPARATOR(*end
)) end
++;
2727 while (next
< name
+ name_len
&& IS_SEPARATOR(*next
)) next
++;
2728 name_len
-= next
- name
;
2730 /* grow the buffer if needed */
2732 if (unix_len
- pos
< MAX_DIR_ENTRY_LEN
+ 2)
2735 unix_len
+= 2 * MAX_DIR_ENTRY_LEN
;
2736 if (!(new_name
= RtlReAllocateHeap( GetProcessHeap(), 0, unix_name
, unix_len
)))
2737 return STATUS_NO_MEMORY
;
2738 unix_name
= *buffer
= new_name
;
2741 status
= find_file_in_dir( unix_name
, pos
, name
, end
- name
,
2742 check_case
, redirect
? &is_win_dir
: NULL
);
2744 /* if this is the last element, not finding it is not necessarily fatal */
2747 if (status
== STATUS_OBJECT_PATH_NOT_FOUND
)
2749 status
= STATUS_OBJECT_NAME_NOT_FOUND
;
2750 if (disposition
!= FILE_OPEN
&& disposition
!= FILE_OVERWRITE
)
2752 ret
= ntdll_wcstoumbs( 0, name
, end
- name
, unix_name
+ pos
+ 1,
2753 MAX_DIR_ENTRY_LEN
, NULL
, &used_default
);
2754 if (ret
> 0 && !used_default
)
2756 unix_name
[pos
] = '/';
2757 unix_name
[pos
+ 1 + ret
] = 0;
2758 status
= STATUS_NO_SUCH_FILE
;
2763 else if (status
== STATUS_SUCCESS
&& disposition
== FILE_CREATE
)
2765 status
= STATUS_OBJECT_NAME_COLLISION
;
2769 if (status
!= STATUS_SUCCESS
) break;
2771 pos
+= strlen( unix_name
+ pos
);
2774 if (is_win_dir
&& (len
= get_redirect_path( unix_name
, pos
, name
, name_len
, check_case
)))
2778 pos
+= strlen( unix_name
+ pos
);
2779 TRACE( "redirecting -> %s + %s\n", debugstr_a(unix_name
), debugstr_w(name
) );
2787 /******************************************************************************
2788 * nt_to_unix_file_name_attr
2790 NTSTATUS
nt_to_unix_file_name_attr( const OBJECT_ATTRIBUTES
*attr
, ANSI_STRING
*unix_name_ret
,
2793 static const WCHAR invalid_charsW
[] = { INVALID_NT_CHARS
, 0 };
2794 enum server_fd_type type
;
2795 int old_cwd
, root_fd
, needs_close
;
2796 const WCHAR
*name
, *p
;
2798 int name_len
, unix_len
;
2800 BOOLEAN check_case
= !(attr
->Attributes
& OBJ_CASE_INSENSITIVE
);
2802 if (!attr
->RootDirectory
) /* without root dir fall back to normal lookup */
2803 return wine_nt_to_unix_file_name( attr
->ObjectName
, unix_name_ret
, disposition
, check_case
);
2805 name
= attr
->ObjectName
->Buffer
;
2806 name_len
= attr
->ObjectName
->Length
/ sizeof(WCHAR
);
2808 if (name_len
&& IS_SEPARATOR(name
[0])) return STATUS_INVALID_PARAMETER
;
2810 /* check for invalid characters */
2811 for (p
= name
; p
< name
+ name_len
; p
++)
2812 if (*p
< 32 || strchrW( invalid_charsW
, *p
)) return STATUS_OBJECT_NAME_INVALID
;
2814 unix_len
= ntdll_wcstoumbs( 0, name
, name_len
, NULL
, 0, NULL
, NULL
);
2815 unix_len
+= MAX_DIR_ENTRY_LEN
+ 3;
2816 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, unix_len
)))
2817 return STATUS_NO_MEMORY
;
2820 if (!(status
= server_get_unix_fd( attr
->RootDirectory
, 0, &root_fd
, &needs_close
, &type
, NULL
)))
2822 if (type
!= FD_TYPE_DIR
)
2824 if (needs_close
) close( root_fd
);
2825 status
= STATUS_BAD_DEVICE_TYPE
;
2829 RtlEnterCriticalSection( &dir_section
);
2830 if ((old_cwd
= open( ".", O_RDONLY
)) != -1 && fchdir( root_fd
) != -1)
2832 status
= lookup_unix_name( name
, name_len
, &unix_name
, unix_len
, 1,
2833 disposition
, check_case
);
2834 if (fchdir( old_cwd
) == -1) chdir( "/" );
2836 else status
= FILE_GetNtStatus();
2837 RtlLeaveCriticalSection( &dir_section
);
2838 if (old_cwd
!= -1) close( old_cwd
);
2839 if (needs_close
) close( root_fd
);
2842 else if (status
== STATUS_OBJECT_TYPE_MISMATCH
) status
= STATUS_BAD_DEVICE_TYPE
;
2844 if (status
== STATUS_SUCCESS
|| status
== STATUS_NO_SUCH_FILE
)
2846 TRACE( "%s -> %s\n", debugstr_us(attr
->ObjectName
), debugstr_a(unix_name
) );
2847 unix_name_ret
->Buffer
= unix_name
;
2848 unix_name_ret
->Length
= strlen(unix_name
);
2849 unix_name_ret
->MaximumLength
= unix_len
;
2853 TRACE( "%s not found in %s\n", debugstr_w(name
), unix_name
);
2854 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2860 /******************************************************************************
2861 * wine_nt_to_unix_file_name (NTDLL.@) Not a Windows API
2863 * Convert a file name from NT namespace to Unix namespace.
2865 * If disposition is not FILE_OPEN or FILE_OVERWRITE, the last path
2866 * element doesn't have to exist; in that case STATUS_NO_SUCH_FILE is
2867 * returned, but the unix name is still filled in properly.
2869 NTSTATUS CDECL
wine_nt_to_unix_file_name( const UNICODE_STRING
*nameW
, ANSI_STRING
*unix_name_ret
,
2870 UINT disposition
, BOOLEAN check_case
)
2872 static const WCHAR unixW
[] = {'u','n','i','x'};
2873 static const WCHAR invalid_charsW
[] = { INVALID_NT_CHARS
, 0 };
2875 NTSTATUS status
= STATUS_SUCCESS
;
2876 const char *config_dir
= wine_get_config_dir();
2877 const WCHAR
*name
, *p
;
2880 int pos
, ret
, name_len
, unix_len
, prefix_len
, used_default
;
2881 WCHAR prefix
[MAX_DIR_ENTRY_LEN
];
2882 BOOLEAN is_unix
= FALSE
;
2884 name
= nameW
->Buffer
;
2885 name_len
= nameW
->Length
/ sizeof(WCHAR
);
2887 if (!name_len
|| !IS_SEPARATOR(name
[0])) return STATUS_OBJECT_PATH_SYNTAX_BAD
;
2889 if (!(pos
= get_dos_prefix_len( nameW
)))
2890 return STATUS_BAD_DEVICE_TYPE
; /* no DOS prefix, assume NT native name */
2895 if (!name_len
) return STATUS_OBJECT_NAME_INVALID
;
2897 /* check for sub-directory */
2898 for (pos
= 0; pos
< name_len
; pos
++)
2900 if (IS_SEPARATOR(name
[pos
])) break;
2901 if (name
[pos
] < 32 || strchrW( invalid_charsW
, name
[pos
] ))
2902 return STATUS_OBJECT_NAME_INVALID
;
2904 if (pos
> MAX_DIR_ENTRY_LEN
)
2905 return STATUS_OBJECT_NAME_INVALID
;
2907 if (pos
== name_len
) /* no subdir, plain DOS device */
2908 return get_dos_device( name
, name_len
, unix_name_ret
);
2910 for (prefix_len
= 0; prefix_len
< pos
; prefix_len
++)
2911 prefix
[prefix_len
] = tolowerW(name
[prefix_len
]);
2914 name_len
-= prefix_len
;
2916 /* check for invalid characters (all chars except 0 are valid for unix) */
2917 is_unix
= (prefix_len
== 4 && !memcmp( prefix
, unixW
, sizeof(unixW
) ));
2920 for (p
= name
; p
< name
+ name_len
; p
++)
2921 if (!*p
) return STATUS_OBJECT_NAME_INVALID
;
2926 for (p
= name
; p
< name
+ name_len
; p
++)
2927 if (*p
< 32 || strchrW( invalid_charsW
, *p
)) return STATUS_OBJECT_NAME_INVALID
;
2930 unix_len
= ntdll_wcstoumbs( 0, prefix
, prefix_len
, NULL
, 0, NULL
, NULL
);
2931 unix_len
+= ntdll_wcstoumbs( 0, name
, name_len
, NULL
, 0, NULL
, NULL
);
2932 unix_len
+= MAX_DIR_ENTRY_LEN
+ 3;
2933 unix_len
+= strlen(config_dir
) + sizeof("/dosdevices/");
2934 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, unix_len
)))
2935 return STATUS_NO_MEMORY
;
2936 strcpy( unix_name
, config_dir
);
2937 strcat( unix_name
, "/dosdevices/" );
2938 pos
= strlen(unix_name
);
2940 ret
= ntdll_wcstoumbs( 0, prefix
, prefix_len
, unix_name
+ pos
, unix_len
- pos
- 1,
2941 NULL
, &used_default
);
2942 if (!ret
|| used_default
)
2944 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2945 return STATUS_OBJECT_NAME_INVALID
;
2949 /* check if prefix exists (except for DOS drives to avoid extra stat calls) */
2951 if (prefix_len
!= 2 || prefix
[1] != ':')
2954 if (lstat( unix_name
, &st
) == -1 && errno
== ENOENT
)
2958 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2959 return STATUS_BAD_DEVICE_TYPE
;
2961 pos
= 0; /* fall back to unix root */
2965 status
= lookup_unix_name( name
, name_len
, &unix_name
, unix_len
, pos
, disposition
, check_case
);
2966 if (status
== STATUS_SUCCESS
|| status
== STATUS_NO_SUCH_FILE
)
2968 TRACE( "%s -> %s\n", debugstr_us(nameW
), debugstr_a(unix_name
) );
2969 unix_name_ret
->Buffer
= unix_name
;
2970 unix_name_ret
->Length
= strlen(unix_name
);
2971 unix_name_ret
->MaximumLength
= unix_len
;
2975 TRACE( "%s not found in %s\n", debugstr_w(name
), unix_name
);
2976 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
2982 /******************************************************************
2983 * RtlWow64EnableFsRedirection (NTDLL.@)
2985 NTSTATUS WINAPI
RtlWow64EnableFsRedirection( BOOLEAN enable
)
2987 if (!is_wow64
) return STATUS_NOT_IMPLEMENTED
;
2988 ntdll_get_thread_data()->wow64_redir
= enable
;
2989 return STATUS_SUCCESS
;
2993 /******************************************************************
2994 * RtlWow64EnableFsRedirectionEx (NTDLL.@)
2996 NTSTATUS WINAPI
RtlWow64EnableFsRedirectionEx( ULONG disable
, ULONG
*old_value
)
2998 if (!is_wow64
) return STATUS_NOT_IMPLEMENTED
;
2999 if (((ULONG_PTR
)old_value
>> 16) == 0) return STATUS_ACCESS_VIOLATION
;
3001 *old_value
= !ntdll_get_thread_data()->wow64_redir
;
3002 ntdll_get_thread_data()->wow64_redir
= !disable
;
3003 return STATUS_SUCCESS
;
3007 /******************************************************************
3008 * RtlDoesFileExists_U (NTDLL.@)
3010 BOOLEAN WINAPI
RtlDoesFileExists_U(LPCWSTR file_name
)
3012 UNICODE_STRING nt_name
;
3013 FILE_BASIC_INFORMATION basic_info
;
3014 OBJECT_ATTRIBUTES attr
;
3017 if (!RtlDosPathNameToNtPathName_U( file_name
, &nt_name
, NULL
, NULL
)) return FALSE
;
3019 attr
.Length
= sizeof(attr
);
3020 attr
.RootDirectory
= 0;
3021 attr
.ObjectName
= &nt_name
;
3022 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
3023 attr
.SecurityDescriptor
= NULL
;
3024 attr
.SecurityQualityOfService
= NULL
;
3026 ret
= NtQueryAttributesFile(&attr
, &basic_info
) == STATUS_SUCCESS
;
3028 RtlFreeUnicodeString( &nt_name
);
3033 /***********************************************************************
3034 * DIR_unmount_device
3036 * Unmount the specified device.
3038 NTSTATUS
DIR_unmount_device( HANDLE handle
)
3041 int unix_fd
, needs_close
;
3043 if (!(status
= server_get_unix_fd( handle
, 0, &unix_fd
, &needs_close
, NULL
, NULL
)))
3046 char *mount_point
= NULL
;
3048 if (fstat( unix_fd
, &st
) == -1 || !is_valid_mounted_device( &st
))
3049 status
= STATUS_INVALID_PARAMETER
;
3052 if ((mount_point
= get_device_mount_point( st
.st_rdev
)))
3055 static const char umount
[] = "diskutil unmount >/dev/null 2>&1 ";
3057 static const char umount
[] = "umount >/dev/null 2>&1 ";
3059 char *cmd
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(mount_point
)+sizeof(umount
));
3062 strcpy( cmd
, umount
);
3063 strcat( cmd
, mount_point
);
3065 RtlFreeHeap( GetProcessHeap(), 0, cmd
);
3067 /* umount will fail to release the loop device since we still have
3068 a handle to it, so we release it here */
3069 if (major(st
.st_rdev
) == LOOP_MAJOR
) ioctl( unix_fd
, 0x4c01 /*LOOP_CLR_FD*/, 0 );
3072 RtlFreeHeap( GetProcessHeap(), 0, mount_point
);
3075 if (needs_close
) close( unix_fd
);
3081 /******************************************************************************
3084 * Retrieve the Unix name of the current directory; helper for wine_unix_to_nt_file_name.
3085 * Returned value must be freed by caller.
3087 NTSTATUS
DIR_get_unix_cwd( char **cwd
)
3089 int old_cwd
, unix_fd
, needs_close
;
3094 RtlAcquirePebLock();
3096 if (NtCurrentTeb()->Tib
.SubSystemTib
) /* FIXME: hack */
3097 curdir
= &((WIN16_SUBSYSTEM_TIB
*)NtCurrentTeb()->Tib
.SubSystemTib
)->curdir
;
3099 curdir
= &NtCurrentTeb()->Peb
->ProcessParameters
->CurrentDirectory
;
3101 if (!(handle
= curdir
->Handle
))
3103 UNICODE_STRING dirW
;
3104 OBJECT_ATTRIBUTES attr
;
3107 if (!RtlDosPathNameToNtPathName_U( curdir
->DosPath
.Buffer
, &dirW
, NULL
, NULL
))
3109 status
= STATUS_OBJECT_NAME_INVALID
;
3112 attr
.Length
= sizeof(attr
);
3113 attr
.RootDirectory
= 0;
3114 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
3115 attr
.ObjectName
= &dirW
;
3116 attr
.SecurityDescriptor
= NULL
;
3117 attr
.SecurityQualityOfService
= NULL
;
3119 status
= NtOpenFile( &handle
, SYNCHRONIZE
, &attr
, &io
, 0,
3120 FILE_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
);
3121 RtlFreeUnicodeString( &dirW
);
3122 if (status
!= STATUS_SUCCESS
) goto done
;
3125 if ((status
= server_get_unix_fd( handle
, 0, &unix_fd
, &needs_close
, NULL
, NULL
)) == STATUS_SUCCESS
)
3127 RtlEnterCriticalSection( &dir_section
);
3129 if ((old_cwd
= open(".", O_RDONLY
)) != -1 && fchdir( unix_fd
) != -1)
3131 unsigned int size
= 512;
3135 if (!(*cwd
= RtlAllocateHeap( GetProcessHeap(), 0, size
)))
3137 status
= STATUS_NO_MEMORY
;
3140 if (getcwd( *cwd
, size
)) break;
3141 RtlFreeHeap( GetProcessHeap(), 0, *cwd
);
3142 if (errno
!= ERANGE
)
3144 status
= STATUS_OBJECT_PATH_INVALID
;
3149 if (fchdir( old_cwd
) == -1) chdir( "/" );
3151 else status
= FILE_GetNtStatus();
3153 RtlLeaveCriticalSection( &dir_section
);
3154 if (old_cwd
!= -1) close( old_cwd
);
3155 if (needs_close
) close( unix_fd
);
3157 if (!curdir
->Handle
) NtClose( handle
);
3160 RtlReleasePebLock();