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"
26 #include <sys/types.h>
38 #ifdef HAVE_SYS_STAT_H
39 # include <sys/stat.h>
41 #ifdef HAVE_SYS_IOCTL_H
42 #include <sys/ioctl.h>
44 #ifdef HAVE_LINUX_IOCTL_H
45 #include <linux/ioctl.h>
47 #ifdef HAVE_LINUX_MAJOR_H
48 # include <linux/major.h>
50 #ifdef HAVE_SYS_PARAM_H
51 #include <sys/param.h>
53 #ifdef HAVE_SYS_MOUNT_H
54 #include <sys/mount.h>
61 #define NONAMELESSUNION
62 #define NONAMELESSSTRUCT
64 #define WIN32_NO_STATUS
69 #include "ntdll_misc.h"
70 #include "wine/unicode.h"
71 #include "wine/server.h"
72 #include "wine/library.h"
73 #include "wine/debug.h"
75 WINE_DEFAULT_DEBUG_CHANNEL(file);
78 #undef VFAT_IOCTL_READDIR_BOTH
83 /* We want the real kernel dirent structure, not the libc one */
88 unsigned short d_reclen;
92 /* Define the VFAT ioctl to get both short and long file names */
93 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, KERNEL_DIRENT [2] )
96 # define O_DIRECTORY 0200000 /* must be directory */
105 unsigned short d_reclen;
106 unsigned char d_type;
110 static inline int getdents64( int fd, KERNEL_DIRENT64 *de, unsigned int size )
113 __asm__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
115 : "0" (220 /*NR_getdents64*/), "r" (fd), "c" (de), "d" (size)
130 #define IS_OPTION_TRUE(ch) ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
131 #define IS_SEPARATOR(ch) ((ch) == '\\' || (ch) == '/')
133 #define INVALID_NT_CHARS '*','?','<','>','|','"'
134 #define INVALID_DOS_CHARS INVALID_NT_CHARS,'+','=',',',';','[',']',' ','\345'
136 #define MAX_DIR_ENTRY_LEN 255 /* max length of a directory entry in chars */
138 static int show_dot_files = -1;
140 /* at some point we may want to allow Winelib apps to set this */
141 static const int is_case_sensitive = FALSE;
143 static RTL_CRITICAL_SECTION dir_section;
144 static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
147 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
148 0, 0, { (DWORD_PTR)(__FILE__ ": dir_section") }
150 static RTL_CRITICAL_SECTION dir_section = { &critsect_debug, -1, 0, 0, 0, 0 };
153 /* check if a given Unicode char is OK in a DOS short name */
154 static inline BOOL is_invalid_dos_char( WCHAR ch )
156 static const WCHAR invalid_chars[] = { INVALID_DOS_CHARS,'~','.',0 };
157 if (ch > 0x7f) return TRUE;
158 return strchrW( invalid_chars, ch ) != NULL;
161 /***********************************************************************
162 * get_default_com_device
164 * Return the default device to use for serial ports.
166 static char *get_default_com_device( int num )
170 if (!num || num > 9) return ret;
172 ret = RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/ttyS0") );
175 strcpy( ret, "/dev/ttyS0" );
176 ret[strlen(ret) - 1] = '0' + num - 1;
178 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
179 ret = RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/cuad0") );
182 strcpy( ret, "/dev/cuad0" );
183 ret[strlen(ret) - 1] = '0' + num - 1;
186 FIXME( "no known default for device com%d\n", num );
192 /***********************************************************************
193 * get_default_lpt_device
195 * Return the default device to use for parallel ports.
197 static char *get_default_lpt_device( int num )
201 if (!num || num > 9) return ret;
203 ret = RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/lp0") );
206 strcpy( ret, "/dev/lp0" );
207 ret[strlen(ret) - 1] = '0' + num - 1;
210 FIXME( "no known default for device lpt%d\n", num );
216 /***********************************************************************
217 * parse_mount_entries
219 * Parse mount entries looking for a given device. Helper for get_default_drive_device.
223 #include <sys/vfstab.h>
224 static char *parse_vfstab_entries( FILE *f, dev_t dev, ino_t ino)
227 struct vfstab vfs_entry;
228 struct vfstab *entry=&vfs_entry;
232 while (! getvfsent( f, entry ))
234 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
235 if (!strcmp( entry->vfs_fstype, "nfs" ) ||
236 !strcmp( entry->vfs_fstype, "smbfs" ) ||
237 !strcmp( entry->vfs_fstype, "ncpfs" )) continue;
239 if (stat( entry->vfs_mountp, &st ) == -1) continue;
240 if (st.st_dev != dev || st.st_ino != ino) continue;
241 if (!strcmp( entry->vfs_fstype, "fd" ))
243 if ((device = strstr( entry->vfs_mntopts, "dev=" )))
245 char *p = strchr( device + 4, ',' );
251 return entry->vfs_special;
258 static char *parse_mount_entries( FILE *f, dev_t dev, ino_t ino )
260 struct mntent *entry;
264 while ((entry = getmntent( f )))
266 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
267 if (!strcmp( entry->mnt_type, "nfs" ) ||
268 !strcmp( entry->mnt_type, "smbfs" ) ||
269 !strcmp( entry->mnt_type, "ncpfs" )) continue;
271 if (stat( entry->mnt_dir, &st ) == -1) continue;
272 if (st.st_dev != dev || st.st_ino != ino) continue;
273 if (!strcmp( entry->mnt_type, "supermount" ))
275 if ((device = strstr( entry->mnt_opts, "dev=" )))
277 char *p = strchr( device + 4, ',' );
282 else if (!stat( entry->mnt_fsname, &st ) && S_ISREG(st.st_mode))
284 /* if device is a regular file check for a loop mount */
285 if ((device = strstr( entry->mnt_opts, "loop=" )))
287 char *p = strchr( device + 5, ',' );
293 return entry->mnt_fsname;
299 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
301 static char *parse_mount_entries( FILE *f, dev_t dev, ino_t ino )
306 while ((entry = getfsent()))
308 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
309 if (!strcmp( entry->fs_vfstype, "nfs" ) ||
310 !strcmp( entry->fs_vfstype, "smbfs" ) ||
311 !strcmp( entry->fs_vfstype, "ncpfs" )) continue;
313 if (stat( entry->fs_file, &st ) == -1) continue;
314 if (st.st_dev != dev || st.st_ino != ino) continue;
315 return entry->fs_spec;
322 #include <sys/mnttab.h>
323 static char *parse_mount_entries( FILE *f, dev_t dev, ino_t ino )
326 volatile struct mnttab mntentry;
327 struct mnttab *entry=&mntentry;
332 while (( ! getmntent( f , entry) ))
334 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
335 if (!strcmp( entry->mnt_fstype, "nfs" ) ||
336 !strcmp( entry->mnt_fstype, "smbfs" ) ||
337 !strcmp( entry->mnt_fstype, "ncpfs" )) continue;
339 if (stat( entry->mnt_mountp, &st ) == -1) continue;
340 if (st.st_dev != dev || st.st_ino != ino) continue;
341 if (!strcmp( entry->mnt_fstype, "fd" ))
343 if ((device = strstr( entry->mnt_mntopts, "dev=" )))
345 char *p = strchr( device + 4, ',' );
351 return entry->mnt_special;
357 /***********************************************************************
358 * get_default_drive_device
360 * Return the default device to use for a given drive mount point.
362 static char *get_default_drive_device( const char *root )
372 /* try to open it first to force it to get mounted */
373 if ((fd = open( root, O_RDONLY | O_DIRECTORY )) != -1)
375 res = fstat( fd, &st );
378 /* now try normal stat just in case */
379 if (res == -1) res = stat( root, &st );
380 if (res == -1) return NULL;
382 RtlEnterCriticalSection( &dir_section );
384 if ((f = fopen( "/etc/mtab", "r" )))
386 device = parse_mount_entries( f, st.st_dev, st.st_ino );
389 /* look through fstab too in case it's not mounted (for instance if it's an audio CD) */
390 if (!device && (f = fopen( "/etc/fstab", "r" )))
392 device = parse_mount_entries( f, st.st_dev, st.st_ino );
397 ret = RtlAllocateHeap( GetProcessHeap(), 0, strlen(device) + 1 );
398 if (ret) strcpy( ret, device );
400 RtlLeaveCriticalSection( &dir_section );
402 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__ )
407 /* try to open it first to force it to get mounted */
408 if ((fd = open( root, O_RDONLY )) != -1)
410 res = fstat( fd, &st );
413 /* now try normal stat just in case */
414 if (res == -1) res = stat( root, &st );
415 if (res == -1) return NULL;
417 RtlEnterCriticalSection( &dir_section );
419 /* The FreeBSD parse_mount_entries doesn't require a file argument, so just
420 * pass NULL. Leave the argument in for symmetry.
422 device = parse_mount_entries( NULL, st.st_dev, st.st_ino );
425 ret = RtlAllocateHeap( GetProcessHeap(), 0, strlen(device) + 1 );
426 if (ret) strcpy( ret, device );
428 RtlLeaveCriticalSection( &dir_section );
436 /* try to open it first to force it to get mounted */
437 if ((fd = open( root, O_RDONLY )) != -1)
439 res = fstat( fd, &st );
442 /* now try normal stat just in case */
443 if (res == -1) res = stat( root, &st );
444 if (res == -1) return NULL;
446 RtlEnterCriticalSection( &dir_section );
448 if ((f = fopen( "/etc/mnttab", "r" )))
450 device = parse_mount_entries( f, st.st_dev, st.st_ino);
453 /* look through fstab too in case it's not mounted (for instance if it's an audio CD) */
454 if (!device && (f = fopen( "/etc/vfstab", "r" )))
456 device = parse_vfstab_entries( f, st.st_dev, st.st_ino );
461 ret = RtlAllocateHeap( GetProcessHeap(), 0, strlen(device) + 1 );
462 if (ret) strcpy( ret, device );
464 RtlLeaveCriticalSection( &dir_section );
466 #elif defined(__APPLE__)
467 struct statfs *mntStat;
473 static const char path_bsd_device[] = "/dev/disk";
476 res = stat( root, &st );
477 if (res == -1) return NULL;
482 RtlEnterCriticalSection( &dir_section );
484 mntSize = getmntinfo(&mntStat, MNT_NOWAIT);
486 for (i = 0; i < mntSize && !ret; i++)
488 if (stat(mntStat[i].f_mntonname, &st ) == -1) continue;
489 if (st.st_dev != dev || st.st_ino != ino) continue;
491 /* FIXME add support for mounted network drive */
492 if ( strncmp(mntStat[i].f_mntfromname, path_bsd_device, strlen(path_bsd_device)) == 0)
494 /* set return value to the corresponding raw BSD node */
495 ret = RtlAllocateHeap( GetProcessHeap(), 0, strlen(mntStat[i].f_mntfromname) + 2 /* 2 : r and \0 */ );
498 strcpy(ret, "/dev/r");
499 strcat(ret, mntStat[i].f_mntfromname+sizeof("/dev/")-1);
503 RtlLeaveCriticalSection( &dir_section );
506 if (!warned++) FIXME( "auto detection of DOS devices not supported on this platform\n" );
512 /***********************************************************************
513 * get_device_mount_point
515 * Return the current mount point for a device.
517 static char *get_device_mount_point( dev_t dev )
524 RtlEnterCriticalSection( &dir_section );
526 if ((f = fopen( "/etc/mtab", "r" )))
528 struct mntent *entry;
532 while ((entry = getmntent( f )))
534 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
535 if (!strcmp( entry->mnt_type, "nfs" ) ||
536 !strcmp( entry->mnt_type, "smbfs" ) ||
537 !strcmp( entry->mnt_type, "ncpfs" )) continue;
539 if (!strcmp( entry->mnt_type, "supermount" ))
541 if ((device = strstr( entry->mnt_opts, "dev=" )))
544 if ((p = strchr( device, ',' ))) *p = 0;
547 else if (!stat( entry->mnt_fsname, &st ) && S_ISREG(st.st_mode))
549 /* if device is a regular file check for a loop mount */
550 if ((device = strstr( entry->mnt_opts, "loop=" )))
553 if ((p = strchr( device, ',' ))) *p = 0;
556 else device = entry->mnt_fsname;
558 if (device && !stat( device, &st ) && S_ISBLK(st.st_mode) && st.st_rdev == dev)
560 ret = RtlAllocateHeap( GetProcessHeap(), 0, strlen(entry->mnt_dir) + 1 );
561 if (ret) strcpy( ret, entry->mnt_dir );
567 RtlLeaveCriticalSection( &dir_section );
570 if (!warned++) FIXME( "unmounting devices not supported on this platform\n" );
576 /***********************************************************************
579 * Initialize the show_dot_files options.
581 static void init_options(void)
583 static const WCHAR WineW[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e',0};
584 static const WCHAR ShowDotFilesW[] = {'S','h','o','w','D','o','t','F','i','l','e','s',0};
588 OBJECT_ATTRIBUTES attr;
589 UNICODE_STRING nameW;
593 RtlOpenCurrentUser( KEY_ALL_ACCESS, &root );
594 attr.Length = sizeof(attr);
595 attr.RootDirectory = root;
596 attr.ObjectName = &nameW;
598 attr.SecurityDescriptor = NULL;
599 attr.SecurityQualityOfService = NULL;
600 RtlInitUnicodeString( &nameW, WineW );
602 /* @@ Wine registry key: HKCU\Software\Wine */
603 if (!NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ))
605 RtlInitUnicodeString( &nameW, ShowDotFilesW );
606 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, tmp, sizeof(tmp), &dummy ))
608 WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
609 show_dot_files = IS_OPTION_TRUE( str[0] );
617 /***********************************************************************
620 * Check if the specified file should be hidden based on its name and the show dot files option.
622 BOOL DIR_is_hidden_file( const UNICODE_STRING *name )
626 if (show_dot_files == -1) init_options();
627 if (show_dot_files) return FALSE;
629 end = p = name->Buffer + name->Length/sizeof(WCHAR);
630 while (p > name->Buffer && IS_SEPARATOR(p[-1])) p--;
631 while (p > name->Buffer && !IS_SEPARATOR(p[-1])) p--;
632 if (p == end || *p != '.') return FALSE;
633 /* make sure it isn't '.' or '..' */
634 if (p + 1 == end) return FALSE;
635 if (p[1] == '.' && p + 2 == end) return FALSE;
640 /***********************************************************************
641 * hash_short_file_name
643 * Transform a Unix file name into a hashed DOS name. If the name is a valid
644 * DOS name, it is converted to upper-case; otherwise it is replaced by a
645 * hashed version that fits in 8.3 format.
646 * 'buffer' must be at least 12 characters long.
647 * Returns length of short name in bytes; short name is NOT null-terminated.
649 static ULONG hash_short_file_name( const UNICODE_STRING *name, LPWSTR buffer )
651 static const char hash_chars[32] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345";
653 LPCWSTR p, ext, end = name->Buffer + name->Length / sizeof(WCHAR);
658 /* Compute the hash code of the file name */
659 /* If you know something about hash functions, feel free to */
660 /* insert a better algorithm here... */
661 if (!is_case_sensitive)
663 for (p = name->Buffer, hash = 0xbeef; p < end - 1; p++)
664 hash = (hash<<3) ^ (hash>>5) ^ tolowerW(*p) ^ (tolowerW(p[1]) << 8);
665 hash = (hash<<3) ^ (hash>>5) ^ tolowerW(*p); /* Last character */
669 for (p = name->Buffer, hash = 0xbeef; p < end - 1; p++)
670 hash = (hash << 3) ^ (hash >> 5) ^ *p ^ (p[1] << 8);
671 hash = (hash << 3) ^ (hash >> 5) ^ *p; /* Last character */
674 /* Find last dot for start of the extension */
675 for (p = name->Buffer + 1, ext = NULL; p < end - 1; p++) if (*p == '.') ext = p;
677 /* Copy first 4 chars, replacing invalid chars with '_' */
678 for (i = 4, p = name->Buffer, dst = buffer; i > 0; i--, p++)
680 if (p == end || p == ext) break;
681 *dst++ = is_invalid_dos_char(*p) ? '_' : toupperW(*p);
683 /* Pad to 5 chars with '~' */
684 while (i-- >= 0) *dst++ = '~';
686 /* Insert hash code converted to 3 ASCII chars */
687 *dst++ = hash_chars[(hash >> 10) & 0x1f];
688 *dst++ = hash_chars[(hash >> 5) & 0x1f];
689 *dst++ = hash_chars[hash & 0x1f];
691 /* Copy the first 3 chars of the extension (if any) */
695 for (i = 3, ext++; (i > 0) && ext < end; i--, ext++)
696 *dst++ = is_invalid_dos_char(*ext) ? '_' : toupperW(*ext);
702 /***********************************************************************
705 * Check a long file name against a mask.
707 * Tests (done in W95 DOS shell - case insensitive):
708 * *.txt test1.test.txt *
710 * *.t??????.t* test1.ta.tornado.txt *
711 * *tornado* test1.ta.tornado.txt *
712 * t*t test1.ta.tornado.txt *
714 * ?est??? test1.txt -
715 * *test1.txt* test1.txt *
716 * h?l?o*t.dat hellothisisatest.dat *
718 static BOOLEAN match_filename( const UNICODE_STRING *name_str, const UNICODE_STRING *mask_str )
721 const WCHAR *name = name_str->Buffer;
722 const WCHAR *mask = mask_str->Buffer;
723 const WCHAR *name_end = name + name_str->Length / sizeof(WCHAR);
724 const WCHAR *mask_end = mask + mask_str->Length / sizeof(WCHAR);
725 const WCHAR *lastjoker = NULL;
726 const WCHAR *next_to_retry = NULL;
728 TRACE("(%s, %s)\n", debugstr_us(name_str), debugstr_us(mask_str));
730 while (name < name_end && mask < mask_end)
736 while (mask < mask_end && *mask == '*') mask++; /* Skip consecutive '*' */
737 if (mask == mask_end) return TRUE; /* end of mask is all '*', so match */
740 /* skip to the next match after the joker(s) */
741 if (is_case_sensitive)
742 while (name < name_end && (*name != *mask)) name++;
744 while (name < name_end && (toupperW(*name) != toupperW(*mask))) name++;
745 next_to_retry = name;
752 if (is_case_sensitive) mismatch = (*mask != *name);
753 else mismatch = (toupperW(*mask) != toupperW(*name));
759 if (mask == mask_end)
761 if (name == name_end) return TRUE;
762 if (lastjoker) mask = lastjoker;
765 else /* mismatch ! */
767 if (lastjoker) /* we had an '*', so we can try unlimitedly */
771 /* this scan sequence was a mismatch, so restart
772 * 1 char after the first char we checked last time */
774 name = next_to_retry;
776 else return FALSE; /* bad luck */
781 while (mask < mask_end && ((*mask == '.') || (*mask == '*')))
782 mask++; /* Ignore trailing '.' or '*' in mask */
783 return (name == name_end && mask == mask_end);
787 /***********************************************************************
790 * helper for NtQueryDirectoryFile
792 static FILE_BOTH_DIR_INFORMATION *append_entry( void *info_ptr, ULONG_PTR *pos, ULONG max_length,
793 const char *long_name, const char *short_name,
794 const UNICODE_STRING *mask )
796 FILE_BOTH_DIR_INFORMATION *info;
797 int i, long_len, short_len, total_len;
799 WCHAR long_nameW[MAX_DIR_ENTRY_LEN];
800 WCHAR short_nameW[12];
803 long_len = ntdll_umbstowcs( 0, long_name, strlen(long_name), long_nameW, MAX_DIR_ENTRY_LEN );
804 if (long_len == -1) return NULL;
806 str.Buffer = long_nameW;
807 str.Length = long_len * sizeof(WCHAR);
808 str.MaximumLength = sizeof(long_nameW);
812 short_len = ntdll_umbstowcs( 0, short_name, strlen(short_name),
813 short_nameW, sizeof(short_nameW) / sizeof(WCHAR) );
814 if (short_len == -1) short_len = sizeof(short_nameW) / sizeof(WCHAR);
816 else /* generate a short name if necessary */
821 if (!RtlIsNameLegalDOS8Dot3( &str, NULL, &spaces ) || spaces)
822 short_len = hash_short_file_name( &str, short_nameW );
825 TRACE( "long %s short %s mask %s\n",
826 debugstr_us(&str), debugstr_wn(short_nameW, short_len), debugstr_us(mask) );
828 if (mask && !match_filename( &str, mask ))
830 if (!short_len) return NULL; /* no short name to match */
831 str.Buffer = short_nameW;
832 str.Length = short_len * sizeof(WCHAR);
833 str.MaximumLength = sizeof(short_nameW);
834 if (!match_filename( &str, mask )) return NULL;
837 total_len = (sizeof(*info) - sizeof(info->FileName) + long_len*sizeof(WCHAR) + 3) & ~3;
838 info = (FILE_BOTH_DIR_INFORMATION *)((char *)info_ptr + *pos);
840 if (*pos + total_len > max_length) total_len = max_length - *pos;
842 info->FileAttributes = 0;
843 if (lstat( long_name, &st ) == -1) return NULL;
844 if (S_ISLNK( st.st_mode ))
846 if (stat( long_name, &st ) == -1) return NULL;
847 if (S_ISDIR( st.st_mode )) info->FileAttributes |= FILE_ATTRIBUTE_REPARSE_POINT;
850 info->NextEntryOffset = total_len;
851 info->FileIndex = 0; /* NTFS always has 0 here, so let's not bother with it */
853 RtlSecondsSince1970ToTime( st.st_mtime, &info->CreationTime );
854 RtlSecondsSince1970ToTime( st.st_mtime, &info->LastWriteTime );
855 RtlSecondsSince1970ToTime( st.st_atime, &info->LastAccessTime );
856 RtlSecondsSince1970ToTime( st.st_ctime, &info->ChangeTime );
858 if (S_ISDIR(st.st_mode))
860 info->EndOfFile.QuadPart = info->AllocationSize.QuadPart = 0;
861 info->FileAttributes |= FILE_ATTRIBUTE_DIRECTORY;
865 info->EndOfFile.QuadPart = st.st_size;
866 info->AllocationSize.QuadPart = (ULONGLONG)st.st_blocks * 512;
867 info->FileAttributes |= FILE_ATTRIBUTE_ARCHIVE;
870 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
871 info->FileAttributes |= FILE_ATTRIBUTE_READONLY;
873 if (!show_dot_files && long_name[0] == '.' && long_name[1] && (long_name[1] != '.' || long_name[2]))
874 info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN;
876 info->EaSize = 0; /* FIXME */
877 info->ShortNameLength = short_len * sizeof(WCHAR);
878 for (i = 0; i < short_len; i++) info->ShortName[i] = toupperW(short_nameW[i]);
879 info->FileNameLength = long_len * sizeof(WCHAR);
880 memcpy( info->FileName, long_nameW,
881 min( info->FileNameLength, total_len-sizeof(*info)+sizeof(info->FileName) ));
888 /***********************************************************************
889 * read_directory_vfat
891 * Read a directory using the VFAT ioctl; helper for NtQueryDirectoryFile.
893 #ifdef VFAT_IOCTL_READDIR_BOTH
894 static int read_directory_vfat( int fd, IO_STATUS_BLOCK *io, void *buffer, ULONG length,
895 BOOLEAN single_entry, const UNICODE_STRING *mask,
896 BOOLEAN restart_scan )
901 FILE_BOTH_DIR_INFORMATION *info, *last_info = NULL;
902 static const unsigned int max_dir_info_size = sizeof(*info) + (MAX_DIR_ENTRY_LEN-1) * sizeof(WCHAR);
904 io->u.Status = STATUS_SUCCESS;
906 if (restart_scan) lseek( fd, 0, SEEK_SET );
908 if (length < max_dir_info_size) /* we may have to return a partial entry here */
910 off_t old_pos = lseek( fd, 0, SEEK_CUR );
912 /* Set d_reclen to 65535 to work around an AFS kernel bug */
913 de[0].d_reclen = 65535;
914 res = ioctl( fd, VFAT_IOCTL_READDIR_BOTH, (long)de );
915 if (res == -1 && errno != ENOENT) return -1; /* VFAT ioctl probably not supported */
916 if (!res && de[0].d_reclen == 65535) return -1; /* AFS bug */
920 if (!de[0].d_reclen) break;
921 /* make sure names are null-terminated to work around an x86-64 kernel bug */
922 if (de[0].d_reclen < sizeof(de[0].d_name)) de[0].d_name[de[0].d_reclen] = 0;
923 if (de[1].d_reclen < sizeof(de[1].d_name)) de[1].d_name[de[1].d_reclen] = 0;
925 info = append_entry( buffer, &io->Information, length,
926 de[1].d_name, de[0].d_name, mask );
928 info = append_entry( buffer, &io->Information, length,
929 de[0].d_name, NULL, mask );
933 if ((char *)info->FileName + info->FileNameLength > (char *)buffer + length)
935 io->u.Status = STATUS_BUFFER_OVERFLOW;
936 lseek( fd, old_pos, SEEK_SET ); /* restore pos to previous entry */
940 old_pos = lseek( fd, 0, SEEK_CUR );
941 res = ioctl( fd, VFAT_IOCTL_READDIR_BOTH, (long)de );
944 else /* we'll only return full entries, no need to worry about overflow */
946 /* Set d_reclen to 65535 to work around an AFS kernel bug */
947 de[0].d_reclen = 65535;
948 res = ioctl( fd, VFAT_IOCTL_READDIR_BOTH, (long)de );
949 if (res == -1 && errno != ENOENT) return -1; /* VFAT ioctl probably not supported */
950 if (!res && de[0].d_reclen == 65535) return -1; /* AFS bug */
954 if (!de[0].d_reclen) break;
955 /* make sure names are null-terminated to work around an x86-64 kernel bug */
956 if (de[0].d_reclen < sizeof(de[0].d_name)) de[0].d_name[de[0].d_reclen] = 0;
957 if (de[1].d_reclen < sizeof(de[1].d_name)) de[1].d_name[de[1].d_reclen] = 0;
959 info = append_entry( buffer, &io->Information, length,
960 de[1].d_name, de[0].d_name, mask );
962 info = append_entry( buffer, &io->Information, length,
963 de[0].d_name, NULL, mask );
967 if (single_entry) break;
968 /* check if we still have enough space for the largest possible entry */
969 if (io->Information + max_dir_info_size > length) break;
971 res = ioctl( fd, VFAT_IOCTL_READDIR_BOTH, (long)de );
975 if (last_info) last_info->NextEntryOffset = 0;
976 else io->u.Status = restart_scan ? STATUS_NO_SUCH_FILE : STATUS_NO_MORE_FILES;
979 #endif /* VFAT_IOCTL_READDIR_BOTH */
982 /***********************************************************************
983 * read_directory_getdents
985 * Read a directory using the Linux getdents64 system call; helper for NtQueryDirectoryFile.
988 static int read_directory_getdents( int fd, IO_STATUS_BLOCK *io, void *buffer, ULONG length,
989 BOOLEAN single_entry, const UNICODE_STRING *mask,
990 BOOLEAN restart_scan )
993 size_t size = length;
995 char local_buffer[8192];
996 KERNEL_DIRENT64 *data, *de;
997 FILE_BOTH_DIR_INFORMATION *info, *last_info = NULL;
998 static const unsigned int max_dir_info_size = sizeof(*info) + (MAX_DIR_ENTRY_LEN-1) * sizeof(WCHAR);
1000 if (size <= sizeof(local_buffer) || !(data = RtlAllocateHeap( GetProcessHeap(), 0, size )))
1002 size = sizeof(local_buffer);
1003 data = (KERNEL_DIRENT64 *)local_buffer;
1006 if (restart_scan) lseek( fd, 0, SEEK_SET );
1007 else if (length < max_dir_info_size) /* we may have to return a partial entry here */
1009 old_pos = lseek( fd, 0, SEEK_CUR );
1010 if (old_pos == -1 && errno == ENOENT)
1012 io->u.Status = STATUS_NO_MORE_FILES;
1018 io->u.Status = STATUS_SUCCESS;
1020 res = getdents64( fd, data, size );
1023 if (errno != ENOSYS)
1025 io->u.Status = FILE_GetNtStatus();
1035 res -= de->d_reclen;
1036 info = append_entry( buffer, &io->Information, length, de->d_name, NULL, mask );
1040 if ((char *)info->FileName + info->FileNameLength > (char *)buffer + length)
1042 io->u.Status = STATUS_BUFFER_OVERFLOW;
1043 lseek( fd, old_pos, SEEK_SET ); /* restore pos to previous entry */
1046 /* check if we still have enough space for the largest possible entry */
1047 if (single_entry || io->Information + max_dir_info_size > length)
1049 if (res > 0) lseek( fd, de->d_off, SEEK_SET ); /* set pos to next entry */
1053 old_pos = de->d_off;
1054 /* move on to the next entry */
1055 if (res > 0) de = (KERNEL_DIRENT64 *)((char *)de + de->d_reclen);
1058 res = getdents64( fd, data, size );
1063 if (last_info) last_info->NextEntryOffset = 0;
1064 else io->u.Status = restart_scan ? STATUS_NO_SUCH_FILE : STATUS_NO_MORE_FILES;
1067 if ((char *)data != local_buffer) RtlFreeHeap( GetProcessHeap(), 0, data );
1070 #endif /* USE_GETDENTS */
1073 /***********************************************************************
1074 * read_directory_readdir
1076 * Read a directory using the POSIX readdir interface; helper for NtQueryDirectoryFile.
1078 static void read_directory_readdir( int fd, IO_STATUS_BLOCK *io, void *buffer, ULONG length,
1079 BOOLEAN single_entry, const UNICODE_STRING *mask,
1080 BOOLEAN restart_scan )
1083 off_t i, old_pos = 0;
1085 FILE_BOTH_DIR_INFORMATION *info, *last_info = NULL;
1086 static const unsigned int max_dir_info_size = sizeof(*info) + (MAX_DIR_ENTRY_LEN-1) * sizeof(WCHAR);
1088 if (!(dir = opendir( "." )))
1090 io->u.Status = FILE_GetNtStatus();
1096 old_pos = lseek( fd, 0, SEEK_CUR );
1097 /* skip the right number of entries */
1098 for (i = 0; i < old_pos; i++)
1100 if (!readdir( dir ))
1103 io->u.Status = STATUS_NO_MORE_FILES;
1108 io->u.Status = STATUS_SUCCESS;
1110 while ((de = readdir( dir )))
1113 info = append_entry( buffer, &io->Information, length, de->d_name, NULL, mask );
1117 if ((char *)info->FileName + info->FileNameLength > (char *)buffer + length)
1119 io->u.Status = STATUS_BUFFER_OVERFLOW;
1120 old_pos--; /* restore pos to previous entry */
1123 if (single_entry) break;
1124 /* check if we still have enough space for the largest possible entry */
1125 if (io->Information + max_dir_info_size > length) break;
1129 lseek( fd, old_pos, SEEK_SET ); /* store dir offset as filepos for fd */
1132 if (last_info) last_info->NextEntryOffset = 0;
1133 else io->u.Status = restart_scan ? STATUS_NO_SUCH_FILE : STATUS_NO_MORE_FILES;
1136 /***********************************************************************
1137 * read_directory_stat
1139 * Read a single file from a directory by determining whether the file
1140 * identified by mask exists using stat.
1142 static int read_directory_stat( int fd, IO_STATUS_BLOCK *io, void *buffer, ULONG length,
1143 BOOLEAN single_entry, const UNICODE_STRING *mask,
1144 BOOLEAN restart_scan )
1146 int unix_len, ret, used_default;
1150 TRACE("trying optimisation for file %s\n", debugstr_us( mask ));
1152 unix_len = ntdll_wcstoumbs( 0, mask->Buffer, mask->Length / sizeof(WCHAR), NULL, 0, NULL, NULL );
1153 if (!(unix_name = RtlAllocateHeap( GetProcessHeap(), 0, unix_len + 1)))
1155 io->u.Status = STATUS_NO_MEMORY;
1158 ret = ntdll_wcstoumbs( 0, mask->Buffer, mask->Length / sizeof(WCHAR), unix_name, unix_len,
1159 NULL, &used_default );
1160 if (ret > 0 && !used_default)
1165 lseek( fd, 0, SEEK_SET );
1167 else if (lseek( fd, 0, SEEK_CUR ) != 0)
1169 io->u.Status = STATUS_NO_MORE_FILES;
1174 ret = stat( unix_name, &st );
1177 FILE_BOTH_DIR_INFORMATION *info = append_entry( buffer, &io->Information, length, unix_name, NULL, mask );
1180 info->NextEntryOffset = 0;
1181 if ((char *)info->FileName + info->FileNameLength > (char *)buffer + length)
1182 io->u.Status = STATUS_BUFFER_OVERFLOW;
1184 lseek( fd, 1, SEEK_CUR );
1191 RtlFreeHeap( GetProcessHeap(), 0, unix_name );
1193 TRACE("returning %d\n", ret);
1199 static inline WCHAR *mempbrkW( const WCHAR *ptr, const WCHAR *accept, size_t n )
1202 for (end = ptr + n; ptr < end; ptr++) if (strchrW( accept, *ptr )) return (WCHAR *)ptr;
1206 /******************************************************************************
1207 * NtQueryDirectoryFile [NTDLL.@]
1208 * ZwQueryDirectoryFile [NTDLL.@]
1210 NTSTATUS WINAPI NtQueryDirectoryFile( HANDLE handle, HANDLE event,
1211 PIO_APC_ROUTINE apc_routine, PVOID apc_context,
1212 PIO_STATUS_BLOCK io,
1213 PVOID buffer, ULONG length,
1214 FILE_INFORMATION_CLASS info_class,
1215 BOOLEAN single_entry,
1216 PUNICODE_STRING mask,
1217 BOOLEAN restart_scan )
1220 static const WCHAR wszWildcards[] = { '*','?',0 };
1222 TRACE("(%p %p %p %p %p %p 0x%08lx 0x%08x 0x%08x %s 0x%08x\n",
1223 handle, event, apc_routine, apc_context, io, buffer,
1224 length, info_class, single_entry, debugstr_us(mask),
1227 if (length < sizeof(FILE_BOTH_DIR_INFORMATION)) return STATUS_INFO_LENGTH_MISMATCH;
1229 if (event || apc_routine)
1231 FIXME( "Unsupported yet option\n" );
1232 return io->u.Status = STATUS_NOT_IMPLEMENTED;
1234 if (info_class != FileBothDirectoryInformation)
1236 FIXME( "Unsupported file info class %d\n", info_class );
1237 return io->u.Status = STATUS_NOT_IMPLEMENTED;
1240 if ((io->u.Status = wine_server_handle_to_fd( handle, FILE_LIST_DIRECTORY, &fd, NULL )) != STATUS_SUCCESS)
1241 return io->u.Status;
1243 io->Information = 0;
1245 RtlEnterCriticalSection( &dir_section );
1247 if (show_dot_files == -1) init_options();
1249 if ((cwd = open(".", O_RDONLY)) != -1 && fchdir( fd ) != -1)
1251 if (mask && !mempbrkW( mask->Buffer, wszWildcards, mask->Length / sizeof(WCHAR) ) &&
1252 read_directory_stat( fd, io, buffer, length, single_entry, mask, restart_scan ) != -1)
1254 #ifdef VFAT_IOCTL_READDIR_BOTH
1255 if ((read_directory_vfat( fd, io, buffer, length, single_entry, mask, restart_scan )) != -1)
1259 if ((read_directory_getdents( fd, io, buffer, length, single_entry, mask, restart_scan )) != -1)
1262 read_directory_readdir( fd, io, buffer, length, single_entry, mask, restart_scan );
1265 if (fchdir( cwd ) == -1) chdir( "/" );
1267 else io->u.Status = FILE_GetNtStatus();
1269 RtlLeaveCriticalSection( &dir_section );
1271 wine_server_release_fd( handle, fd );
1272 if (cwd != -1) close( cwd );
1273 TRACE( "=> %lx (%ld)\n", io->u.Status, io->Information );
1274 return io->u.Status;
1278 /***********************************************************************
1281 * Find a file in a directory the hard way, by doing a case-insensitive search.
1282 * The file found is appended to unix_name at pos.
1283 * There must be at least MAX_DIR_ENTRY_LEN+2 chars available at pos.
1285 static NTSTATUS find_file_in_dir( char *unix_name, int pos, const WCHAR *name, int length,
1288 WCHAR buffer[MAX_DIR_ENTRY_LEN];
1294 int ret, used_default, is_name_8_dot_3;
1296 /* try a shortcut for this directory */
1298 unix_name[pos++] = '/';
1299 ret = ntdll_wcstoumbs( 0, name, length, unix_name + pos, MAX_DIR_ENTRY_LEN,
1300 NULL, &used_default );
1301 /* if we used the default char, the Unix name won't round trip properly back to Unicode */
1302 /* so it cannot match the file we are looking for */
1303 if (ret >= 0 && !used_default)
1305 unix_name[pos + ret] = 0;
1306 if (!stat( unix_name, &st )) return STATUS_SUCCESS;
1308 if (check_case) goto not_found; /* we want an exact match */
1310 if (pos > 1) unix_name[pos - 1] = 0;
1311 else unix_name[1] = 0; /* keep the initial slash */
1313 /* check if it fits in 8.3 so that we don't look for short names if we won't need them */
1315 str.Buffer = (WCHAR *)name;
1316 str.Length = length * sizeof(WCHAR);
1317 str.MaximumLength = str.Length;
1318 is_name_8_dot_3 = RtlIsNameLegalDOS8Dot3( &str, NULL, &spaces ) && !spaces;
1320 /* now look for it through the directory */
1322 #ifdef VFAT_IOCTL_READDIR_BOTH
1323 if (is_name_8_dot_3)
1325 int fd = open( unix_name, O_RDONLY | O_DIRECTORY );
1328 KERNEL_DIRENT de[2];
1330 /* Set d_reclen to 65535 to work around an AFS kernel bug */
1331 de[0].d_reclen = 65535;
1332 if (ioctl( fd, VFAT_IOCTL_READDIR_BOTH, (long)de ) != -1 &&
1333 de[0].d_reclen != 65535)
1335 unix_name[pos - 1] = '/';
1338 if (!de[0].d_reclen) break;
1339 /* make sure names are null-terminated to work around an x86-64 kernel bug */
1340 if (de[0].d_reclen < sizeof(de[0].d_name)) de[0].d_name[de[0].d_reclen] = 0;
1341 if (de[1].d_reclen < sizeof(de[1].d_name)) de[1].d_name[de[1].d_reclen] = 0;
1343 if (de[1].d_name[0])
1345 ret = ntdll_umbstowcs( 0, de[1].d_name, strlen(de[1].d_name),
1346 buffer, MAX_DIR_ENTRY_LEN );
1347 if (ret == length && !memicmpW( buffer, name, length))
1349 strcpy( unix_name + pos, de[1].d_name );
1351 return STATUS_SUCCESS;
1354 ret = ntdll_umbstowcs( 0, de[0].d_name, strlen(de[0].d_name),
1355 buffer, MAX_DIR_ENTRY_LEN );
1356 if (ret == length && !memicmpW( buffer, name, length))
1358 strcpy( unix_name + pos,
1359 de[1].d_name[0] ? de[1].d_name : de[0].d_name );
1361 return STATUS_SUCCESS;
1363 if (ioctl( fd, VFAT_IOCTL_READDIR_BOTH, (long)de ) == -1)
1372 /* fall through to normal handling */
1374 #endif /* VFAT_IOCTL_READDIR_BOTH */
1376 if (!(dir = opendir( unix_name )))
1378 if (errno == ENOENT) return STATUS_OBJECT_PATH_NOT_FOUND;
1379 else return FILE_GetNtStatus();
1381 unix_name[pos - 1] = '/';
1382 str.Buffer = buffer;
1383 str.MaximumLength = sizeof(buffer);
1384 while ((de = readdir( dir )))
1386 ret = ntdll_umbstowcs( 0, de->d_name, strlen(de->d_name), buffer, MAX_DIR_ENTRY_LEN );
1387 if (ret == length && !memicmpW( buffer, name, length ))
1389 strcpy( unix_name + pos, de->d_name );
1391 return STATUS_SUCCESS;
1394 if (!is_name_8_dot_3) continue;
1396 str.Length = ret * sizeof(WCHAR);
1397 if (!RtlIsNameLegalDOS8Dot3( &str, NULL, &spaces ) || spaces)
1399 WCHAR short_nameW[12];
1400 ret = hash_short_file_name( &str, short_nameW );
1401 if (ret == length && !memicmpW( short_nameW, name, length ))
1403 strcpy( unix_name + pos, de->d_name );
1405 return STATUS_SUCCESS;
1410 goto not_found; /* avoid warning */
1413 unix_name[pos - 1] = 0;
1414 return STATUS_OBJECT_PATH_NOT_FOUND;
1418 /******************************************************************************
1421 * Get the Unix path of a DOS device.
1423 static NTSTATUS get_dos_device( const WCHAR *name, UINT name_len, ANSI_STRING *unix_name_ret )
1425 const char *config_dir = wine_get_config_dir();
1427 char *unix_name, *new_name, *dev;
1431 /* make sure the device name is ASCII */
1432 for (i = 0; i < name_len; i++)
1433 if (name[i] <= 32 || name[i] >= 127) return STATUS_BAD_DEVICE_TYPE;
1435 unix_len = strlen(config_dir) + sizeof("/dosdevices/") + name_len + 1;
1437 if (!(unix_name = RtlAllocateHeap( GetProcessHeap(), 0, unix_len )))
1438 return STATUS_NO_MEMORY;
1440 strcpy( unix_name, config_dir );
1441 strcat( unix_name, "/dosdevices/" );
1442 dev = unix_name + strlen(unix_name);
1444 for (i = 0; i < name_len; i++) dev[i] = (char)tolowerW(name[i]);
1447 /* special case for drive devices */
1448 if (name_len == 2 && dev[1] == ':')
1456 if (!stat( unix_name, &st ))
1458 TRACE( "%s -> %s\n", debugstr_wn(name,name_len), debugstr_a(unix_name) );
1459 unix_name_ret->Buffer = unix_name;
1460 unix_name_ret->Length = strlen(unix_name);
1461 unix_name_ret->MaximumLength = unix_len;
1462 return STATUS_SUCCESS;
1466 /* now try some defaults for it */
1467 if (!strcmp( dev, "aux" ))
1469 strcpy( dev, "com1" );
1472 if (!strcmp( dev, "prn" ))
1474 strcpy( dev, "lpt1" );
1477 if (!strcmp( dev, "nul" ))
1479 strcpy( unix_name, "/dev/null" );
1480 dev = NULL; /* last try */
1485 if (dev[1] == ':' && dev[2] == ':') /* drive device */
1487 dev[2] = 0; /* remove last ':' to get the drive mount point symlink */
1488 new_name = get_default_drive_device( unix_name );
1490 else if (!strncmp( dev, "com", 3 )) new_name = get_default_com_device( dev[3] - '0' );
1491 else if (!strncmp( dev, "lpt", 3 )) new_name = get_default_lpt_device( dev[3] - '0' );
1493 if (!new_name) break;
1495 RtlFreeHeap( GetProcessHeap(), 0, unix_name );
1496 unix_name = new_name;
1497 unix_len = strlen(unix_name) + 1;
1498 dev = NULL; /* last try */
1500 RtlFreeHeap( GetProcessHeap(), 0, unix_name );
1501 return STATUS_BAD_DEVICE_TYPE;
1505 /* return the length of the DOS namespace prefix if any */
1506 static inline int get_dos_prefix_len( const UNICODE_STRING *name )
1508 static const WCHAR nt_prefixW[] = {'\\','?','?','\\'};
1509 static const WCHAR dosdev_prefixW[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\'};
1511 if (name->Length > sizeof(nt_prefixW) &&
1512 !memcmp( name->Buffer, nt_prefixW, sizeof(nt_prefixW) ))
1513 return sizeof(nt_prefixW) / sizeof(WCHAR);
1515 if (name->Length > sizeof(dosdev_prefixW) &&
1516 !memicmpW( name->Buffer, dosdev_prefixW, sizeof(dosdev_prefixW)/sizeof(WCHAR) ))
1517 return sizeof(dosdev_prefixW) / sizeof(WCHAR);
1523 /******************************************************************************
1524 * wine_nt_to_unix_file_name (NTDLL.@) Not a Windows API
1526 * Convert a file name from NT namespace to Unix namespace.
1528 * If disposition is not FILE_OPEN or FILE_OVERWRITTE, the last path
1529 * element doesn't have to exist; in that case STATUS_NO_SUCH_FILE is
1530 * returned, but the unix name is still filled in properly.
1532 NTSTATUS wine_nt_to_unix_file_name( const UNICODE_STRING *nameW, ANSI_STRING *unix_name_ret,
1533 UINT disposition, BOOLEAN check_case )
1535 static const WCHAR uncW[] = {'U','N','C','\\'};
1536 static const WCHAR invalid_charsW[] = { INVALID_NT_CHARS, 0 };
1538 NTSTATUS status = STATUS_SUCCESS;
1539 const char *config_dir = wine_get_config_dir();
1540 const WCHAR *name, *p;
1543 int pos, ret, name_len, unix_len, used_default;
1545 name = nameW->Buffer;
1546 name_len = nameW->Length / sizeof(WCHAR);
1548 if (!name_len || !IS_SEPARATOR(name[0])) return STATUS_OBJECT_PATH_SYNTAX_BAD;
1550 if ((pos = get_dos_prefix_len( nameW )))
1552 BOOLEAN is_unc = FALSE;
1557 /* check for UNC prefix */
1558 if (name_len > 4 && !memicmpW( name, uncW, 4 ))
1566 /* check for a drive letter with path */
1567 if (name_len < 3 || !isalphaW(name[0]) || name[1] != ':' || !IS_SEPARATOR(name[2]))
1569 /* not a drive with path, try other DOS devices */
1570 return get_dos_device( name, name_len, unix_name_ret );
1572 name += 2; /* skip drive letter */
1576 /* check for invalid characters */
1577 for (p = name; p < name + name_len; p++)
1578 if (*p < 32 || strchrW( invalid_charsW, *p )) return STATUS_OBJECT_NAME_INVALID;
1580 unix_len = ntdll_wcstoumbs( 0, name, name_len, NULL, 0, NULL, NULL );
1581 unix_len += MAX_DIR_ENTRY_LEN + 3;
1582 unix_len += strlen(config_dir) + sizeof("/dosdevices/") + 3;
1583 if (!(unix_name = RtlAllocateHeap( GetProcessHeap(), 0, unix_len )))
1584 return STATUS_NO_MEMORY;
1585 strcpy( unix_name, config_dir );
1586 strcat( unix_name, "/dosdevices/" );
1587 pos = strlen(unix_name);
1590 strcpy( unix_name + pos, "unc" );
1595 unix_name[pos++] = tolowerW( name[-2] );
1596 unix_name[pos++] = ':';
1600 else /* no DOS prefix, assume NT native name, map directly to Unix */
1602 if (!name_len || !IS_SEPARATOR(name[0])) return STATUS_OBJECT_NAME_INVALID;
1603 unix_len = ntdll_wcstoumbs( 0, name, name_len, NULL, 0, NULL, NULL );
1604 unix_len += MAX_DIR_ENTRY_LEN + 3;
1605 if (!(unix_name = RtlAllocateHeap( GetProcessHeap(), 0, unix_len )))
1606 return STATUS_NO_MEMORY;
1610 /* try a shortcut first */
1612 ret = ntdll_wcstoumbs( 0, name, name_len, unix_name + pos, unix_len - pos - 1,
1613 NULL, &used_default );
1615 while (name_len && IS_SEPARATOR(*name))
1621 if (ret > 0 && !used_default) /* if we used the default char the name didn't convert properly */
1624 unix_name[pos + ret] = 0;
1625 for (p = unix_name + pos ; *p; p++) if (*p == '\\') *p = '/';
1626 if (!stat( unix_name, &st ))
1628 /* creation fails with STATUS_ACCESS_DENIED for the root of the drive */
1629 if (disposition == FILE_CREATE)
1630 return name_len ? STATUS_OBJECT_NAME_COLLISION : STATUS_ACCESS_DENIED;
1635 if (!name_len) /* empty name -> drive root doesn't exist */
1637 RtlFreeHeap( GetProcessHeap(), 0, unix_name );
1638 return STATUS_OBJECT_PATH_NOT_FOUND;
1640 if (check_case && (disposition == FILE_OPEN || disposition == FILE_OVERWRITE))
1642 RtlFreeHeap( GetProcessHeap(), 0, unix_name );
1643 return STATUS_OBJECT_NAME_NOT_FOUND;
1646 /* now do it component by component */
1650 const WCHAR *end, *next;
1653 while (end < name + name_len && !IS_SEPARATOR(*end)) end++;
1655 while (next < name + name_len && IS_SEPARATOR(*next)) next++;
1656 name_len -= next - name;
1658 /* grow the buffer if needed */
1660 if (unix_len - pos < MAX_DIR_ENTRY_LEN + 2)
1663 unix_len += 2 * MAX_DIR_ENTRY_LEN;
1664 if (!(new_name = RtlReAllocateHeap( GetProcessHeap(), 0, unix_name, unix_len )))
1666 RtlFreeHeap( GetProcessHeap(), 0, unix_name );
1667 return STATUS_NO_MEMORY;
1669 unix_name = new_name;
1672 status = find_file_in_dir( unix_name, pos, name, end - name, check_case );
1674 /* if this is the last element, not finding it is not necessarily fatal */
1677 if (status == STATUS_OBJECT_PATH_NOT_FOUND)
1679 status = STATUS_OBJECT_NAME_NOT_FOUND;
1680 if (disposition != FILE_OPEN && disposition != FILE_OVERWRITE)
1682 ret = ntdll_wcstoumbs( 0, name, end - name, unix_name + pos + 1,
1683 MAX_DIR_ENTRY_LEN, NULL, &used_default );
1684 if (ret > 0 && !used_default)
1686 unix_name[pos] = '/';
1687 unix_name[pos + 1 + ret] = 0;
1688 status = STATUS_NO_SUCH_FILE;
1693 else if (status == STATUS_SUCCESS && disposition == FILE_CREATE)
1695 status = STATUS_OBJECT_NAME_COLLISION;
1699 if (status != STATUS_SUCCESS)
1701 /* couldn't find it at all, fail */
1702 WARN( "%s not found in %s\n", debugstr_w(name), unix_name );
1703 RtlFreeHeap( GetProcessHeap(), 0, unix_name );
1707 pos += strlen( unix_name + pos );
1711 WARN( "%s -> %s required a case-insensitive search\n",
1712 debugstr_us(nameW), debugstr_a(unix_name) );
1715 TRACE( "%s -> %s\n", debugstr_us(nameW), debugstr_a(unix_name) );
1716 unix_name_ret->Buffer = unix_name;
1717 unix_name_ret->Length = strlen(unix_name);
1718 unix_name_ret->MaximumLength = unix_len;
1723 /******************************************************************
1724 * RtlDoesFileExists_U (NTDLL.@)
1726 BOOLEAN WINAPI RtlDoesFileExists_U(LPCWSTR file_name)
1728 UNICODE_STRING nt_name;
1729 ANSI_STRING unix_name;
1732 if (!RtlDosPathNameToNtPathName_U( file_name, &nt_name, NULL, NULL )) return FALSE;
1733 ret = (wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, FALSE ) == STATUS_SUCCESS);
1734 if (ret) RtlFreeAnsiString( &unix_name );
1735 RtlFreeUnicodeString( &nt_name );
1740 /***********************************************************************
1741 * DIR_unmount_device
1743 * Unmount the specified device.
1745 NTSTATUS DIR_unmount_device( HANDLE handle )
1750 SERVER_START_REQ( unmount_device )
1752 req->handle = handle;
1753 status = wine_server_call( req );
1756 if (status) return status;
1758 if (!(status = wine_server_handle_to_fd( handle, 0, &unix_fd, NULL )))
1761 char *mount_point = NULL;
1763 if (fstat( unix_fd, &st ) == -1 || !S_ISBLK(st.st_mode))
1764 status = STATUS_INVALID_PARAMETER;
1767 if ((mount_point = get_device_mount_point( st.st_rdev )))
1769 static const char umount[] = "umount >/dev/null 2>&1 ";
1770 char *cmd = RtlAllocateHeap( GetProcessHeap(), 0, strlen(mount_point)+sizeof(umount));
1773 strcpy( cmd, umount );
1774 strcat( cmd, mount_point );
1776 RtlFreeHeap( GetProcessHeap(), 0, cmd );
1778 /* umount will fail to release the loop device since we still have
1779 a handle to it, so we release it here */
1780 if (major(st.st_rdev) == LOOP_MAJOR) ioctl( unix_fd, 0x4c01 /*LOOP_CLR_FD*/, 0 );
1783 RtlFreeHeap( GetProcessHeap(), 0, mount_point );
1786 wine_server_release_fd( handle, unix_fd );
1792 /******************************************************************************
1795 * Retrieve the Unix name of the current directory; helper for wine_unix_to_nt_file_name.
1796 * Returned value must be freed by caller.
1798 NTSTATUS DIR_get_unix_cwd( char **cwd )
1800 int old_cwd, unix_fd;
1805 RtlAcquirePebLock();
1807 if (NtCurrentTeb()->Tib.SubSystemTib) /* FIXME: hack */
1808 curdir = &((WIN16_SUBSYSTEM_TIB *)NtCurrentTeb()->Tib.SubSystemTib)->curdir;
1810 curdir = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectory;
1812 if (!(handle = curdir->Handle))
1814 UNICODE_STRING dirW;
1815 OBJECT_ATTRIBUTES attr;
1818 if (!RtlDosPathNameToNtPathName_U( curdir->DosPath.Buffer, &dirW, NULL, NULL ))
1820 status = STATUS_OBJECT_NAME_INVALID;
1823 attr.Length = sizeof(attr);
1824 attr.RootDirectory = 0;
1825 attr.Attributes = OBJ_CASE_INSENSITIVE;
1826 attr.ObjectName = &dirW;
1827 attr.SecurityDescriptor = NULL;
1828 attr.SecurityQualityOfService = NULL;
1830 status = NtOpenFile( &handle, 0, &attr, &io, 0,
1831 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
1832 RtlFreeUnicodeString( &dirW );
1833 if (status != STATUS_SUCCESS) goto done;
1836 if ((status = wine_server_handle_to_fd( handle, 0, &unix_fd, NULL )) == STATUS_SUCCESS)
1838 RtlEnterCriticalSection( &dir_section );
1840 if ((old_cwd = open(".", O_RDONLY)) != -1 && fchdir( unix_fd ) != -1)
1842 unsigned int size = 512;
1846 if (!(*cwd = RtlAllocateHeap( GetProcessHeap(), 0, size )))
1848 status = STATUS_NO_MEMORY;
1851 if (getcwd( *cwd, size )) break;
1852 RtlFreeHeap( GetProcessHeap(), 0, *cwd );
1853 if (errno != ERANGE)
1855 status = STATUS_OBJECT_PATH_INVALID;
1860 if (fchdir( old_cwd ) == -1) chdir( "/" );
1862 else status = FILE_GetNtStatus();
1864 RtlLeaveCriticalSection( &dir_section );
1865 wine_server_release_fd( handle, unix_fd );
1867 if (!curdir->Handle) NtClose( handle );
1870 RtlReleasePebLock();
1874 struct read_changes_info
1878 PIO_APC_ROUTINE ApcRoutine;
1884 static void WINAPI read_changes_apc( void *user, PIO_STATUS_BLOCK iosb, ULONG status )
1886 struct read_changes_info *info = user;
1887 char path[PATH_MAX];
1888 NTSTATUS ret = STATUS_SUCCESS;
1891 TRACE("%p %p %p %08lx\n", info, info->ApcContext, iosb, status);
1896 * hEvent/hDir is set before the output buffer and iosb is updated.
1897 * Since the thread that called NtNotifyChangeDirectoryFile is usually
1898 * waiting, we'll be safe since we're called in that thread's context.
1899 * If a different thread is waiting on our hEvent/hDir we're going to be
1902 SERVER_START_REQ( read_change )
1904 req->handle = info->FileHandle;
1905 wine_server_set_reply( req, path, PATH_MAX );
1906 ret = wine_server_call( req );
1907 action = reply->action;
1908 len = wine_server_reply_size( reply );
1912 if (ret == STATUS_SUCCESS && info->Buffer &&
1913 (info->BufferSize > (sizeof (FILE_NOTIFY_INFORMATION) + len*sizeof(WCHAR))))
1915 PFILE_NOTIFY_INFORMATION pfni;
1917 pfni = (PFILE_NOTIFY_INFORMATION) info->Buffer;
1919 /* convert to an NT style path */
1920 for (i=0; i<len; i++)
1924 len = ntdll_umbstowcs( 0, path, len, pfni->FileName,
1925 info->BufferSize - sizeof (*pfni) );
1927 pfni->NextEntryOffset = 0;
1928 pfni->Action = action;
1929 pfni->FileNameLength = len * sizeof (WCHAR);
1930 pfni->FileName[len] = 0;
1932 TRACE("action = %ld name = %s\n", pfni->Action,
1933 debugstr_w(pfni->FileName) );
1934 len = sizeof (*pfni) - sizeof (DWORD) + pfni->FileNameLength;
1938 ret = STATUS_NOTIFY_ENUM_DIR;
1942 iosb->u.Status = ret;
1943 iosb->Information = len;
1945 RtlFreeHeap( GetProcessHeap(), 0, info );
1948 #define FILE_NOTIFY_ALL ( \
1949 FILE_NOTIFY_CHANGE_FILE_NAME | \
1950 FILE_NOTIFY_CHANGE_DIR_NAME | \
1951 FILE_NOTIFY_CHANGE_ATTRIBUTES | \
1952 FILE_NOTIFY_CHANGE_SIZE | \
1953 FILE_NOTIFY_CHANGE_LAST_WRITE | \
1954 FILE_NOTIFY_CHANGE_LAST_ACCESS | \
1955 FILE_NOTIFY_CHANGE_CREATION | \
1956 FILE_NOTIFY_CHANGE_SECURITY )
1958 /******************************************************************************
1959 * NtNotifyChangeDirectoryFile [NTDLL.@]
1962 NtNotifyChangeDirectoryFile( HANDLE FileHandle, HANDLE Event,
1963 PIO_APC_ROUTINE ApcRoutine, PVOID ApcContext,
1964 PIO_STATUS_BLOCK IoStatusBlock, PVOID Buffer,
1965 ULONG BufferSize, ULONG CompletionFilter, BOOLEAN WatchTree )
1967 struct read_changes_info *info;
1970 TRACE("%p %p %p %p %p %p %lu %lu %d\n",
1971 FileHandle, Event, ApcRoutine, ApcContext, IoStatusBlock,
1972 Buffer, BufferSize, CompletionFilter, WatchTree );
1975 return STATUS_ACCESS_VIOLATION;
1977 if (CompletionFilter == 0 || (CompletionFilter & ~FILE_NOTIFY_ALL))
1978 return STATUS_INVALID_PARAMETER;
1981 FIXME("parameters ignored %p %p\n", ApcRoutine, ApcContext );
1983 info = RtlAllocateHeap( GetProcessHeap(), 0, sizeof *info );
1985 return STATUS_NO_MEMORY;
1987 info->FileHandle = FileHandle;
1988 info->Event = Event;
1989 info->Buffer = Buffer;
1990 info->BufferSize = BufferSize;
1991 info->ApcRoutine = ApcRoutine;
1992 info->ApcContext = ApcContext;
1994 SERVER_START_REQ( read_directory_changes )
1996 req->handle = FileHandle;
1998 req->filter = CompletionFilter;
1999 req->want_data = (Buffer != NULL);
2000 req->subtree = WatchTree;
2001 req->io_apc = read_changes_apc;
2002 req->io_sb = IoStatusBlock;
2003 req->io_user = info;
2004 status = wine_server_call( req );
2008 if (status != STATUS_PENDING)
2009 RtlFreeHeap( GetProcessHeap(), 0, info );