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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "wine/port.h"
26 #include <sys/types.h>
37 #ifdef HAVE_SYS_STAT_H
38 # include <sys/stat.h>
40 #ifdef HAVE_SYS_IOCTL_H
41 #include <sys/ioctl.h>
43 #ifdef HAVE_LINUX_IOCTL_H
44 #include <linux/ioctl.h>
46 #ifdef HAVE_LINUX_MAJOR_H
47 # include <linux/major.h>
49 #ifdef HAVE_SYS_PARAM_H
50 #include <sys/param.h>
52 #ifdef HAVE_SYS_MOUNT_H
53 #include <sys/mount.h>
60 #define NONAMELESSUNION
61 #define NONAMELESSSTRUCT
63 #define WIN32_NO_STATUS
68 #include "ntdll_misc.h"
69 #include "wine/unicode.h"
70 #include "wine/server.h"
71 #include "wine/library.h"
72 #include "wine/debug.h"
74 WINE_DEFAULT_DEBUG_CHANNEL(file
);
77 #undef VFAT_IOCTL_READDIR_BOTH
82 /* We want the real kernel dirent structure, not the libc one */
87 unsigned short d_reclen
;
91 /* Define the VFAT ioctl to get both short and long file names */
92 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, KERNEL_DIRENT [2] )
95 # define O_DIRECTORY 0200000 /* must be directory */
104 unsigned short d_reclen
;
105 unsigned char d_type
;
109 static inline int getdents64( int fd
, KERNEL_DIRENT64
*de
, unsigned int size
)
112 __asm__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
114 : "0" (220 /*NR_getdents64*/), "r" (fd
), "c" (de
), "d" (size
)
129 #define IS_OPTION_TRUE(ch) ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
130 #define IS_SEPARATOR(ch) ((ch) == '\\' || (ch) == '/')
132 #define INVALID_NT_CHARS '*','?','<','>','|','"'
133 #define INVALID_DOS_CHARS INVALID_NT_CHARS,'+','=',',',';','[',']',' ','\345'
135 #define MAX_DIR_ENTRY_LEN 255 /* max length of a directory entry in chars */
137 static int show_dot_files
= -1;
139 /* at some point we may want to allow Winelib apps to set this */
140 static const int is_case_sensitive
= FALSE
;
142 static RTL_CRITICAL_SECTION dir_section
;
143 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
146 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
147 0, 0, { (DWORD_PTR
)(__FILE__
": dir_section") }
149 static RTL_CRITICAL_SECTION dir_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
152 /* check if a given Unicode char is OK in a DOS short name */
153 static inline BOOL
is_invalid_dos_char( WCHAR ch
)
155 static const WCHAR invalid_chars
[] = { INVALID_DOS_CHARS
,'~','.',0 };
156 if (ch
> 0x7f) return TRUE
;
157 return strchrW( invalid_chars
, ch
) != NULL
;
160 /***********************************************************************
161 * get_default_com_device
163 * Return the default device to use for serial ports.
165 static char *get_default_com_device( int num
)
169 if (!num
|| num
> 9) return ret
;
171 ret
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/ttyS0") );
174 strcpy( ret
, "/dev/ttyS0" );
175 ret
[strlen(ret
) - 1] = '0' + num
- 1;
178 FIXME( "no known default for device com%d\n", num
);
184 /***********************************************************************
185 * get_default_lpt_device
187 * Return the default device to use for parallel ports.
189 static char *get_default_lpt_device( int num
)
193 if (!num
|| num
> 9) return ret
;
195 ret
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/lp0") );
198 strcpy( ret
, "/dev/lp0" );
199 ret
[strlen(ret
) - 1] = '0' + num
- 1;
202 FIXME( "no known default for device lpt%d\n", num
);
208 /***********************************************************************
209 * parse_mount_entries
211 * Parse mount entries looking for a given device. Helper for get_default_drive_device.
215 #include <sys/vfstab.h>
216 static char *parse_vfstab_entries( FILE *f
, dev_t dev
, ino_t ino
)
219 struct vfstab vfs_entry
;
220 struct vfstab
*entry
=&vfs_entry
;
224 while (! getvfsent( f
, entry
))
226 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
227 if (!strcmp( entry
->vfs_fstype
, "nfs" ) ||
228 !strcmp( entry
->vfs_fstype
, "smbfs" ) ||
229 !strcmp( entry
->vfs_fstype
, "ncpfs" )) continue;
231 if (stat( entry
->vfs_mountp
, &st
) == -1) continue;
232 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
233 if (!strcmp( entry
->vfs_fstype
, "fd" ))
235 if ((device
= strstr( entry
->vfs_mntopts
, "dev=" )))
237 char *p
= strchr( device
+ 4, ',' );
243 return entry
->vfs_special
;
250 static char *parse_mount_entries( FILE *f
, dev_t dev
, ino_t ino
)
252 struct mntent
*entry
;
256 while ((entry
= getmntent( f
)))
258 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
259 if (!strcmp( entry
->mnt_type
, "nfs" ) ||
260 !strcmp( entry
->mnt_type
, "smbfs" ) ||
261 !strcmp( entry
->mnt_type
, "ncpfs" )) continue;
263 if (stat( entry
->mnt_dir
, &st
) == -1) continue;
264 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
265 if (!strcmp( entry
->mnt_type
, "supermount" ))
267 if ((device
= strstr( entry
->mnt_opts
, "dev=" )))
269 char *p
= strchr( device
+ 4, ',' );
274 else if (!stat( entry
->mnt_fsname
, &st
) && S_ISREG(st
.st_mode
))
276 /* if device is a regular file check for a loop mount */
277 if ((device
= strstr( entry
->mnt_opts
, "loop=" )))
279 char *p
= strchr( device
+ 5, ',' );
285 return entry
->mnt_fsname
;
291 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
293 static char *parse_mount_entries( FILE *f
, dev_t dev
, ino_t ino
)
298 while ((entry
= getfsent()))
300 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
301 if (!strcmp( entry
->fs_vfstype
, "nfs" ) ||
302 !strcmp( entry
->fs_vfstype
, "smbfs" ) ||
303 !strcmp( entry
->fs_vfstype
, "ncpfs" )) continue;
305 if (stat( entry
->fs_file
, &st
) == -1) continue;
306 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
307 return entry
->fs_spec
;
314 #include <sys/mnttab.h>
315 static char *parse_mount_entries( FILE *f
, dev_t dev
, ino_t ino
)
318 volatile struct mnttab mntentry
;
319 struct mnttab
*entry
=&mntentry
;
324 while (( ! getmntent( f
, entry
) ))
326 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
327 if (!strcmp( entry
->mnt_fstype
, "nfs" ) ||
328 !strcmp( entry
->mnt_fstype
, "smbfs" ) ||
329 !strcmp( entry
->mnt_fstype
, "ncpfs" )) continue;
331 if (stat( entry
->mnt_mountp
, &st
) == -1) continue;
332 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
333 if (!strcmp( entry
->mnt_fstype
, "fd" ))
335 if ((device
= strstr( entry
->mnt_mntopts
, "dev=" )))
337 char *p
= strchr( device
+ 4, ',' );
343 return entry
->mnt_special
;
349 /***********************************************************************
350 * get_default_drive_device
352 * Return the default device to use for a given drive mount point.
354 static char *get_default_drive_device( const char *root
)
364 /* try to open it first to force it to get mounted */
365 if ((fd
= open( root
, O_RDONLY
| O_DIRECTORY
)) != -1)
367 res
= fstat( fd
, &st
);
370 /* now try normal stat just in case */
371 if (res
== -1) res
= stat( root
, &st
);
372 if (res
== -1) return NULL
;
374 RtlEnterCriticalSection( &dir_section
);
376 if ((f
= fopen( "/etc/mtab", "r" )))
378 device
= parse_mount_entries( f
, st
.st_dev
, st
.st_ino
);
381 /* look through fstab too in case it's not mounted (for instance if it's an audio CD) */
382 if (!device
&& (f
= fopen( "/etc/fstab", "r" )))
384 device
= parse_mount_entries( f
, st
.st_dev
, st
.st_ino
);
389 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(device
) + 1 );
390 if (ret
) strcpy( ret
, device
);
392 RtlLeaveCriticalSection( &dir_section
);
394 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__ )
399 /* try to open it first to force it to get mounted */
400 if ((fd
= open( root
, O_RDONLY
)) != -1)
402 res
= fstat( fd
, &st
);
405 /* now try normal stat just in case */
406 if (res
== -1) res
= stat( root
, &st
);
407 if (res
== -1) return NULL
;
409 RtlEnterCriticalSection( &dir_section
);
411 /* The FreeBSD parse_mount_entries doesn't require a file argument, so just
412 * pass NULL. Leave the argument in for symmetry.
414 device
= parse_mount_entries( NULL
, st
.st_dev
, st
.st_ino
);
417 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(device
) + 1 );
418 if (ret
) strcpy( ret
, device
);
420 RtlLeaveCriticalSection( &dir_section
);
428 /* try to open it first to force it to get mounted */
429 if ((fd
= open( root
, O_RDONLY
)) != -1)
431 res
= fstat( fd
, &st
);
434 /* now try normal stat just in case */
435 if (res
== -1) res
= stat( root
, &st
);
436 if (res
== -1) return NULL
;
438 RtlEnterCriticalSection( &dir_section
);
440 if ((f
= fopen( "/etc/mnttab", "r" )))
442 device
= parse_mount_entries( f
, st
.st_dev
, st
.st_ino
);
445 /* look through fstab too in case it's not mounted (for instance if it's an audio CD) */
446 if (!device
&& (f
= fopen( "/etc/vfstab", "r" )))
448 device
= parse_vfstab_entries( f
, st
.st_dev
, st
.st_ino
);
453 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(device
) + 1 );
454 if (ret
) strcpy( ret
, device
);
456 RtlLeaveCriticalSection( &dir_section
);
458 #elif defined(__APPLE__)
459 struct statfs
*mntStat
;
465 static const char path_bsd_device
[] = "/dev/disk";
468 res
= stat( root
, &st
);
469 if (res
== -1) return NULL
;
474 RtlEnterCriticalSection( &dir_section
);
476 mntSize
= getmntinfo(&mntStat
, MNT_NOWAIT
);
478 for (i
= 0; i
< mntSize
&& !ret
; i
++)
480 if (stat(mntStat
[i
].f_mntonname
, &st
) == -1) continue;
481 if (st
.st_dev
!= dev
|| st
.st_ino
!= ino
) continue;
483 /* FIXME add support for mounted network drive */
484 if ( strncmp(mntStat
[i
].f_mntfromname
, path_bsd_device
, strlen(path_bsd_device
)) == 0)
486 /* set return value to the corresponding raw BSD node */
487 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(mntStat
[i
].f_mntfromname
) + 2 /* 2 : r and \0 */ );
490 strcpy(ret
, "/dev/r");
491 strcat(ret
, mntStat
[i
].f_mntfromname
+sizeof("/dev/")-1);
495 RtlLeaveCriticalSection( &dir_section
);
498 if (!warned
++) FIXME( "auto detection of DOS devices not supported on this platform\n" );
504 /***********************************************************************
505 * get_device_mount_point
507 * Return the current mount point for a device.
509 static char *get_device_mount_point( dev_t dev
)
516 RtlEnterCriticalSection( &dir_section
);
518 if ((f
= fopen( "/etc/mtab", "r" )))
520 struct mntent
*entry
;
524 while ((entry
= getmntent( f
)))
526 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
527 if (!strcmp( entry
->mnt_type
, "nfs" ) ||
528 !strcmp( entry
->mnt_type
, "smbfs" ) ||
529 !strcmp( entry
->mnt_type
, "ncpfs" )) continue;
531 if (!strcmp( entry
->mnt_type
, "supermount" ))
533 if ((device
= strstr( entry
->mnt_opts
, "dev=" )))
536 if ((p
= strchr( device
, ',' ))) *p
= 0;
539 else if (!stat( entry
->mnt_fsname
, &st
) && S_ISREG(st
.st_mode
))
541 /* if device is a regular file check for a loop mount */
542 if ((device
= strstr( entry
->mnt_opts
, "loop=" )))
545 if ((p
= strchr( device
, ',' ))) *p
= 0;
548 else device
= entry
->mnt_fsname
;
550 if (device
&& !stat( device
, &st
) && S_ISBLK(st
.st_mode
) && st
.st_rdev
== dev
)
552 ret
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(entry
->mnt_dir
) + 1 );
553 if (ret
) strcpy( ret
, entry
->mnt_dir
);
559 RtlLeaveCriticalSection( &dir_section
);
562 if (!warned
++) FIXME( "unmounting devices not supported on this platform\n" );
568 /***********************************************************************
571 * Initialize the show_dot_files options.
573 static void init_options(void)
575 static const WCHAR WineW
[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e',0};
576 static const WCHAR ShowDotFilesW
[] = {'S','h','o','w','D','o','t','F','i','l','e','s',0};
580 OBJECT_ATTRIBUTES attr
;
581 UNICODE_STRING nameW
;
585 RtlOpenCurrentUser( KEY_ALL_ACCESS
, &root
);
586 attr
.Length
= sizeof(attr
);
587 attr
.RootDirectory
= root
;
588 attr
.ObjectName
= &nameW
;
590 attr
.SecurityDescriptor
= NULL
;
591 attr
.SecurityQualityOfService
= NULL
;
592 RtlInitUnicodeString( &nameW
, WineW
);
594 /* @@ Wine registry key: HKCU\Software\Wine */
595 if (!NtOpenKey( &hkey
, KEY_ALL_ACCESS
, &attr
))
597 RtlInitUnicodeString( &nameW
, ShowDotFilesW
);
598 if (!NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
, tmp
, sizeof(tmp
), &dummy
))
600 WCHAR
*str
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)tmp
)->Data
;
601 show_dot_files
= IS_OPTION_TRUE( str
[0] );
609 /***********************************************************************
612 * Check if the specified file should be hidden based on its name and the show dot files option.
614 BOOL
DIR_is_hidden_file( const UNICODE_STRING
*name
)
618 if (show_dot_files
== -1) init_options();
619 if (show_dot_files
) return FALSE
;
621 end
= p
= name
->Buffer
+ name
->Length
/sizeof(WCHAR
);
622 while (p
> name
->Buffer
&& IS_SEPARATOR(p
[-1])) p
--;
623 while (p
> name
->Buffer
&& !IS_SEPARATOR(p
[-1])) p
--;
624 if (p
== end
|| *p
!= '.') return FALSE
;
625 /* make sure it isn't '.' or '..' */
626 if (p
+ 1 == end
) return FALSE
;
627 if (p
[1] == '.' && p
+ 2 == end
) return FALSE
;
632 /***********************************************************************
633 * hash_short_file_name
635 * Transform a Unix file name into a hashed DOS name. If the name is a valid
636 * DOS name, it is converted to upper-case; otherwise it is replaced by a
637 * hashed version that fits in 8.3 format.
638 * 'buffer' must be at least 12 characters long.
639 * Returns length of short name in bytes; short name is NOT null-terminated.
641 static ULONG
hash_short_file_name( const UNICODE_STRING
*name
, LPWSTR buffer
)
643 static const char hash_chars
[32] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345";
645 LPCWSTR p
, ext
, end
= name
->Buffer
+ name
->Length
/ sizeof(WCHAR
);
650 /* Compute the hash code of the file name */
651 /* If you know something about hash functions, feel free to */
652 /* insert a better algorithm here... */
653 if (!is_case_sensitive
)
655 for (p
= name
->Buffer
, hash
= 0xbeef; p
< end
- 1; p
++)
656 hash
= (hash
<<3) ^ (hash
>>5) ^ tolowerW(*p
) ^ (tolowerW(p
[1]) << 8);
657 hash
= (hash
<<3) ^ (hash
>>5) ^ tolowerW(*p
); /* Last character */
661 for (p
= name
->Buffer
, hash
= 0xbeef; p
< end
- 1; p
++)
662 hash
= (hash
<< 3) ^ (hash
>> 5) ^ *p
^ (p
[1] << 8);
663 hash
= (hash
<< 3) ^ (hash
>> 5) ^ *p
; /* Last character */
666 /* Find last dot for start of the extension */
667 for (p
= name
->Buffer
+ 1, ext
= NULL
; p
< end
- 1; p
++) if (*p
== '.') ext
= p
;
669 /* Copy first 4 chars, replacing invalid chars with '_' */
670 for (i
= 4, p
= name
->Buffer
, dst
= buffer
; i
> 0; i
--, p
++)
672 if (p
== end
|| p
== ext
) break;
673 *dst
++ = is_invalid_dos_char(*p
) ? '_' : toupperW(*p
);
675 /* Pad to 5 chars with '~' */
676 while (i
-- >= 0) *dst
++ = '~';
678 /* Insert hash code converted to 3 ASCII chars */
679 *dst
++ = hash_chars
[(hash
>> 10) & 0x1f];
680 *dst
++ = hash_chars
[(hash
>> 5) & 0x1f];
681 *dst
++ = hash_chars
[hash
& 0x1f];
683 /* Copy the first 3 chars of the extension (if any) */
687 for (i
= 3, ext
++; (i
> 0) && ext
< end
; i
--, ext
++)
688 *dst
++ = is_invalid_dos_char(*ext
) ? '_' : toupperW(*ext
);
694 /***********************************************************************
697 * Check a long file name against a mask.
699 * Tests (done in W95 DOS shell - case insensitive):
700 * *.txt test1.test.txt *
702 * *.t??????.t* test1.ta.tornado.txt *
703 * *tornado* test1.ta.tornado.txt *
704 * t*t test1.ta.tornado.txt *
706 * ?est??? test1.txt -
707 * *test1.txt* test1.txt *
708 * h?l?o*t.dat hellothisisatest.dat *
710 static BOOLEAN
match_filename( const UNICODE_STRING
*name_str
, const UNICODE_STRING
*mask_str
)
713 const WCHAR
*name
= name_str
->Buffer
;
714 const WCHAR
*mask
= mask_str
->Buffer
;
715 const WCHAR
*name_end
= name
+ name_str
->Length
/ sizeof(WCHAR
);
716 const WCHAR
*mask_end
= mask
+ mask_str
->Length
/ sizeof(WCHAR
);
717 const WCHAR
*lastjoker
= NULL
;
718 const WCHAR
*next_to_retry
= NULL
;
720 TRACE("(%s, %s)\n", debugstr_us(name_str
), debugstr_us(mask_str
));
722 while (name
< name_end
&& mask
< mask_end
)
728 while (mask
< mask_end
&& *mask
== '*') mask
++; /* Skip consecutive '*' */
729 if (mask
== mask_end
) return TRUE
; /* end of mask is all '*', so match */
732 /* skip to the next match after the joker(s) */
733 if (is_case_sensitive
)
734 while (name
< name_end
&& (*name
!= *mask
)) name
++;
736 while (name
< name_end
&& (toupperW(*name
) != toupperW(*mask
))) name
++;
737 next_to_retry
= name
;
744 if (is_case_sensitive
) mismatch
= (*mask
!= *name
);
745 else mismatch
= (toupperW(*mask
) != toupperW(*name
));
751 if (mask
== mask_end
)
753 if (name
== name_end
) return TRUE
;
754 if (lastjoker
) mask
= lastjoker
;
757 else /* mismatch ! */
759 if (lastjoker
) /* we had an '*', so we can try unlimitedly */
763 /* this scan sequence was a mismatch, so restart
764 * 1 char after the first char we checked last time */
766 name
= next_to_retry
;
768 else return FALSE
; /* bad luck */
773 while (mask
< mask_end
&& ((*mask
== '.') || (*mask
== '*')))
774 mask
++; /* Ignore trailing '.' or '*' in mask */
775 return (name
== name_end
&& mask
== mask_end
);
779 /***********************************************************************
782 * helper for NtQueryDirectoryFile
784 static FILE_BOTH_DIR_INFORMATION
*append_entry( void *info_ptr
, ULONG_PTR
*pos
, ULONG max_length
,
785 const char *long_name
, const char *short_name
,
786 const UNICODE_STRING
*mask
)
788 FILE_BOTH_DIR_INFORMATION
*info
;
789 int i
, long_len
, short_len
, total_len
;
791 WCHAR long_nameW
[MAX_DIR_ENTRY_LEN
];
792 WCHAR short_nameW
[12];
795 long_len
= ntdll_umbstowcs( 0, long_name
, strlen(long_name
), long_nameW
, MAX_DIR_ENTRY_LEN
);
796 if (long_len
== -1) return NULL
;
798 str
.Buffer
= long_nameW
;
799 str
.Length
= long_len
* sizeof(WCHAR
);
800 str
.MaximumLength
= sizeof(long_nameW
);
804 short_len
= ntdll_umbstowcs( 0, short_name
, strlen(short_name
),
805 short_nameW
, sizeof(short_nameW
) / sizeof(WCHAR
) );
806 if (short_len
== -1) short_len
= sizeof(short_nameW
) / sizeof(WCHAR
);
808 else /* generate a short name if necessary */
813 if (!RtlIsNameLegalDOS8Dot3( &str
, NULL
, &spaces
) || spaces
)
814 short_len
= hash_short_file_name( &str
, short_nameW
);
817 TRACE( "long %s short %s mask %s\n",
818 debugstr_us(&str
), debugstr_wn(short_nameW
, short_len
), debugstr_us(mask
) );
820 if (mask
&& !match_filename( &str
, mask
))
822 if (!short_len
) return NULL
; /* no short name to match */
823 str
.Buffer
= short_nameW
;
824 str
.Length
= short_len
* sizeof(WCHAR
);
825 str
.MaximumLength
= sizeof(short_nameW
);
826 if (!match_filename( &str
, mask
)) return NULL
;
829 total_len
= (sizeof(*info
) - sizeof(info
->FileName
) + long_len
*sizeof(WCHAR
) + 3) & ~3;
830 info
= (FILE_BOTH_DIR_INFORMATION
*)((char *)info_ptr
+ *pos
);
832 if (*pos
+ total_len
> max_length
) total_len
= max_length
- *pos
;
834 info
->FileAttributes
= 0;
835 if (lstat( long_name
, &st
) == -1) return NULL
;
836 if (S_ISLNK( st
.st_mode
))
838 if (stat( long_name
, &st
) == -1) return NULL
;
839 if (S_ISDIR( st
.st_mode
)) info
->FileAttributes
|= FILE_ATTRIBUTE_REPARSE_POINT
;
842 info
->NextEntryOffset
= total_len
;
843 info
->FileIndex
= 0; /* NTFS always has 0 here, so let's not bother with it */
845 RtlSecondsSince1970ToTime( st
.st_mtime
, &info
->CreationTime
);
846 RtlSecondsSince1970ToTime( st
.st_mtime
, &info
->LastWriteTime
);
847 RtlSecondsSince1970ToTime( st
.st_atime
, &info
->LastAccessTime
);
848 RtlSecondsSince1970ToTime( st
.st_ctime
, &info
->ChangeTime
);
850 if (S_ISDIR(st
.st_mode
))
852 info
->EndOfFile
.QuadPart
= info
->AllocationSize
.QuadPart
= 0;
853 info
->FileAttributes
|= FILE_ATTRIBUTE_DIRECTORY
;
857 info
->EndOfFile
.QuadPart
= st
.st_size
;
858 info
->AllocationSize
.QuadPart
= (ULONGLONG
)st
.st_blocks
* 512;
859 info
->FileAttributes
|= FILE_ATTRIBUTE_ARCHIVE
;
862 if (!(st
.st_mode
& (S_IWUSR
| S_IWGRP
| S_IWOTH
)))
863 info
->FileAttributes
|= FILE_ATTRIBUTE_READONLY
;
865 if (!show_dot_files
&& long_name
[0] == '.' && long_name
[1] && (long_name
[1] != '.' || long_name
[2]))
866 info
->FileAttributes
|= FILE_ATTRIBUTE_HIDDEN
;
868 info
->EaSize
= 0; /* FIXME */
869 info
->ShortNameLength
= short_len
* sizeof(WCHAR
);
870 for (i
= 0; i
< short_len
; i
++) info
->ShortName
[i
] = toupperW(short_nameW
[i
]);
871 info
->FileNameLength
= long_len
* sizeof(WCHAR
);
872 memcpy( info
->FileName
, long_nameW
,
873 min( info
->FileNameLength
, total_len
-sizeof(*info
)+sizeof(info
->FileName
) ));
880 /***********************************************************************
881 * read_directory_vfat
883 * Read a directory using the VFAT ioctl; helper for NtQueryDirectoryFile.
885 #ifdef VFAT_IOCTL_READDIR_BOTH
886 static int read_directory_vfat( int fd
, IO_STATUS_BLOCK
*io
, void *buffer
, ULONG length
,
887 BOOLEAN single_entry
, const UNICODE_STRING
*mask
,
888 BOOLEAN restart_scan
)
893 FILE_BOTH_DIR_INFORMATION
*info
, *last_info
= NULL
;
894 static const unsigned int max_dir_info_size
= sizeof(*info
) + (MAX_DIR_ENTRY_LEN
-1) * sizeof(WCHAR
);
896 io
->u
.Status
= STATUS_SUCCESS
;
898 if (restart_scan
) lseek( fd
, 0, SEEK_SET
);
900 if (length
< max_dir_info_size
) /* we may have to return a partial entry here */
902 off_t old_pos
= lseek( fd
, 0, SEEK_CUR
);
904 /* Set d_reclen to 65535 to work around an AFS kernel bug */
905 de
[0].d_reclen
= 65535;
906 res
= ioctl( fd
, VFAT_IOCTL_READDIR_BOTH
, (long)de
);
907 if (res
== -1 && errno
!= ENOENT
) return -1; /* VFAT ioctl probably not supported */
908 if (!res
&& de
[0].d_reclen
== 65535) return -1; /* AFS bug */
912 if (!de
[0].d_reclen
) break;
914 info
= append_entry( buffer
, &io
->Information
, length
,
915 de
[1].d_name
, de
[0].d_name
, mask
);
917 info
= append_entry( buffer
, &io
->Information
, length
,
918 de
[0].d_name
, NULL
, mask
);
922 if ((char *)info
->FileName
+ info
->FileNameLength
> (char *)buffer
+ length
)
924 io
->u
.Status
= STATUS_BUFFER_OVERFLOW
;
925 lseek( fd
, old_pos
, SEEK_SET
); /* restore pos to previous entry */
929 old_pos
= lseek( fd
, 0, SEEK_CUR
);
930 res
= ioctl( fd
, VFAT_IOCTL_READDIR_BOTH
, (long)de
);
933 else /* we'll only return full entries, no need to worry about overflow */
935 /* Set d_reclen to 65535 to work around an AFS kernel bug */
936 de
[0].d_reclen
= 65535;
937 res
= ioctl( fd
, VFAT_IOCTL_READDIR_BOTH
, (long)de
);
938 if (res
== -1 && errno
!= ENOENT
) return -1; /* VFAT ioctl probably not supported */
939 if (!res
&& de
[0].d_reclen
== 65535) return -1; /* AFS bug */
943 if (!de
[0].d_reclen
) break;
945 info
= append_entry( buffer
, &io
->Information
, length
,
946 de
[1].d_name
, de
[0].d_name
, mask
);
948 info
= append_entry( buffer
, &io
->Information
, length
,
949 de
[0].d_name
, NULL
, mask
);
953 if (single_entry
) break;
954 /* check if we still have enough space for the largest possible entry */
955 if (io
->Information
+ max_dir_info_size
> length
) break;
957 res
= ioctl( fd
, VFAT_IOCTL_READDIR_BOTH
, (long)de
);
961 if (last_info
) last_info
->NextEntryOffset
= 0;
962 else io
->u
.Status
= restart_scan
? STATUS_NO_SUCH_FILE
: STATUS_NO_MORE_FILES
;
965 #endif /* VFAT_IOCTL_READDIR_BOTH */
968 /***********************************************************************
969 * read_directory_getdents
971 * Read a directory using the Linux getdents64 system call; helper for NtQueryDirectoryFile.
974 static int read_directory_getdents( int fd
, IO_STATUS_BLOCK
*io
, void *buffer
, ULONG length
,
975 BOOLEAN single_entry
, const UNICODE_STRING
*mask
,
976 BOOLEAN restart_scan
)
979 size_t size
= length
;
981 char local_buffer
[8192];
982 KERNEL_DIRENT64
*data
, *de
;
983 FILE_BOTH_DIR_INFORMATION
*info
, *last_info
= NULL
;
984 static const unsigned int max_dir_info_size
= sizeof(*info
) + (MAX_DIR_ENTRY_LEN
-1) * sizeof(WCHAR
);
986 if (size
<= sizeof(local_buffer
) || !(data
= RtlAllocateHeap( GetProcessHeap(), 0, size
)))
988 size
= sizeof(local_buffer
);
989 data
= (KERNEL_DIRENT64
*)local_buffer
;
992 if (restart_scan
) lseek( fd
, 0, SEEK_SET
);
993 else if (length
< max_dir_info_size
) /* we may have to return a partial entry here */
995 old_pos
= lseek( fd
, 0, SEEK_CUR
);
996 if (old_pos
== -1 && errno
== ENOENT
)
998 io
->u
.Status
= STATUS_NO_MORE_FILES
;
1004 io
->u
.Status
= STATUS_SUCCESS
;
1006 res
= getdents64( fd
, data
, size
);
1009 if (errno
!= ENOSYS
)
1011 io
->u
.Status
= FILE_GetNtStatus();
1021 res
-= de
->d_reclen
;
1022 info
= append_entry( buffer
, &io
->Information
, length
, de
->d_name
, NULL
, mask
);
1026 if ((char *)info
->FileName
+ info
->FileNameLength
> (char *)buffer
+ length
)
1028 io
->u
.Status
= STATUS_BUFFER_OVERFLOW
;
1029 lseek( fd
, old_pos
, SEEK_SET
); /* restore pos to previous entry */
1032 /* check if we still have enough space for the largest possible entry */
1033 if (single_entry
|| io
->Information
+ max_dir_info_size
> length
)
1035 if (res
> 0) lseek( fd
, de
->d_off
, SEEK_SET
); /* set pos to next entry */
1039 old_pos
= de
->d_off
;
1040 /* move on to the next entry */
1041 if (res
> 0) de
= (KERNEL_DIRENT64
*)((char *)de
+ de
->d_reclen
);
1044 res
= getdents64( fd
, data
, size
);
1049 if (last_info
) last_info
->NextEntryOffset
= 0;
1050 else io
->u
.Status
= restart_scan
? STATUS_NO_SUCH_FILE
: STATUS_NO_MORE_FILES
;
1053 if ((char *)data
!= local_buffer
) RtlFreeHeap( GetProcessHeap(), 0, data
);
1056 #endif /* USE_GETDENTS */
1059 /***********************************************************************
1060 * read_directory_readdir
1062 * Read a directory using the POSIX readdir interface; helper for NtQueryDirectoryFile.
1064 static void read_directory_readdir( int fd
, IO_STATUS_BLOCK
*io
, void *buffer
, ULONG length
,
1065 BOOLEAN single_entry
, const UNICODE_STRING
*mask
,
1066 BOOLEAN restart_scan
)
1069 off_t i
, old_pos
= 0;
1071 FILE_BOTH_DIR_INFORMATION
*info
, *last_info
= NULL
;
1072 static const unsigned int max_dir_info_size
= sizeof(*info
) + (MAX_DIR_ENTRY_LEN
-1) * sizeof(WCHAR
);
1074 if (!(dir
= opendir( "." )))
1076 io
->u
.Status
= FILE_GetNtStatus();
1082 old_pos
= lseek( fd
, 0, SEEK_CUR
);
1083 /* skip the right number of entries */
1084 for (i
= 0; i
< old_pos
; i
++)
1086 if (!readdir( dir
))
1089 io
->u
.Status
= STATUS_NO_MORE_FILES
;
1094 io
->u
.Status
= STATUS_SUCCESS
;
1096 while ((de
= readdir( dir
)))
1099 info
= append_entry( buffer
, &io
->Information
, length
, de
->d_name
, NULL
, mask
);
1103 if ((char *)info
->FileName
+ info
->FileNameLength
> (char *)buffer
+ length
)
1105 io
->u
.Status
= STATUS_BUFFER_OVERFLOW
;
1106 old_pos
--; /* restore pos to previous entry */
1109 if (single_entry
) break;
1110 /* check if we still have enough space for the largest possible entry */
1111 if (io
->Information
+ max_dir_info_size
> length
) break;
1115 lseek( fd
, old_pos
, SEEK_SET
); /* store dir offset as filepos for fd */
1118 if (last_info
) last_info
->NextEntryOffset
= 0;
1119 else io
->u
.Status
= restart_scan
? STATUS_NO_SUCH_FILE
: STATUS_NO_MORE_FILES
;
1123 /******************************************************************************
1124 * NtQueryDirectoryFile [NTDLL.@]
1125 * ZwQueryDirectoryFile [NTDLL.@]
1127 NTSTATUS WINAPI
NtQueryDirectoryFile( HANDLE handle
, HANDLE event
,
1128 PIO_APC_ROUTINE apc_routine
, PVOID apc_context
,
1129 PIO_STATUS_BLOCK io
,
1130 PVOID buffer
, ULONG length
,
1131 FILE_INFORMATION_CLASS info_class
,
1132 BOOLEAN single_entry
,
1133 PUNICODE_STRING mask
,
1134 BOOLEAN restart_scan
)
1138 TRACE("(%p %p %p %p %p %p 0x%08lx 0x%08x 0x%08x %s 0x%08x\n",
1139 handle
, event
, apc_routine
, apc_context
, io
, buffer
,
1140 length
, info_class
, single_entry
, debugstr_us(mask
),
1143 if (length
< sizeof(FILE_BOTH_DIR_INFORMATION
)) return STATUS_INFO_LENGTH_MISMATCH
;
1145 if (event
|| apc_routine
)
1147 FIXME( "Unsupported yet option\n" );
1148 return io
->u
.Status
= STATUS_NOT_IMPLEMENTED
;
1150 if (info_class
!= FileBothDirectoryInformation
)
1152 FIXME( "Unsupported file info class %d\n", info_class
);
1153 return io
->u
.Status
= STATUS_NOT_IMPLEMENTED
;
1156 if ((io
->u
.Status
= wine_server_handle_to_fd( handle
, FILE_LIST_DIRECTORY
, &fd
, NULL
)) != STATUS_SUCCESS
)
1157 return io
->u
.Status
;
1159 io
->Information
= 0;
1161 RtlEnterCriticalSection( &dir_section
);
1163 if (show_dot_files
== -1) init_options();
1165 if ((cwd
= open(".", O_RDONLY
)) != -1 && fchdir( fd
) != -1)
1167 #ifdef VFAT_IOCTL_READDIR_BOTH
1168 if ((read_directory_vfat( fd
, io
, buffer
, length
, single_entry
, mask
, restart_scan
)) == -1)
1171 if ((read_directory_getdents( fd
, io
, buffer
, length
, single_entry
, mask
, restart_scan
)) == -1)
1173 read_directory_readdir( fd
, io
, buffer
, length
, single_entry
, mask
, restart_scan
);
1175 if (fchdir( cwd
) == -1) chdir( "/" );
1177 else io
->u
.Status
= FILE_GetNtStatus();
1179 RtlLeaveCriticalSection( &dir_section
);
1181 wine_server_release_fd( handle
, fd
);
1182 if (cwd
!= -1) close( cwd
);
1183 TRACE( "=> %lx (%ld)\n", io
->u
.Status
, io
->Information
);
1184 return io
->u
.Status
;
1188 /***********************************************************************
1191 * Find a file in a directory the hard way, by doing a case-insensitive search.
1192 * The file found is appended to unix_name at pos.
1193 * There must be at least MAX_DIR_ENTRY_LEN+2 chars available at pos.
1195 static NTSTATUS
find_file_in_dir( char *unix_name
, int pos
, const WCHAR
*name
, int length
,
1198 WCHAR buffer
[MAX_DIR_ENTRY_LEN
];
1204 int ret
, used_default
, is_name_8_dot_3
;
1206 /* try a shortcut for this directory */
1208 unix_name
[pos
++] = '/';
1209 ret
= ntdll_wcstoumbs( 0, name
, length
, unix_name
+ pos
, MAX_DIR_ENTRY_LEN
,
1210 NULL
, &used_default
);
1211 /* if we used the default char, the Unix name won't round trip properly back to Unicode */
1212 /* so it cannot match the file we are looking for */
1213 if (ret
>= 0 && !used_default
)
1215 unix_name
[pos
+ ret
] = 0;
1216 if (!stat( unix_name
, &st
)) return STATUS_SUCCESS
;
1218 if (check_case
) goto not_found
; /* we want an exact match */
1220 if (pos
> 1) unix_name
[pos
- 1] = 0;
1221 else unix_name
[1] = 0; /* keep the initial slash */
1223 /* check if it fits in 8.3 so that we don't look for short names if we won't need them */
1225 str
.Buffer
= (WCHAR
*)name
;
1226 str
.Length
= length
* sizeof(WCHAR
);
1227 str
.MaximumLength
= str
.Length
;
1228 is_name_8_dot_3
= RtlIsNameLegalDOS8Dot3( &str
, NULL
, &spaces
) && !spaces
;
1230 /* now look for it through the directory */
1232 #ifdef VFAT_IOCTL_READDIR_BOTH
1233 if (is_name_8_dot_3
)
1235 int fd
= open( unix_name
, O_RDONLY
| O_DIRECTORY
);
1238 KERNEL_DIRENT de
[2];
1240 /* Set d_reclen to 65535 to work around an AFS kernel bug */
1241 de
[0].d_reclen
= 65535;
1242 if (ioctl( fd
, VFAT_IOCTL_READDIR_BOTH
, (long)de
) != -1 &&
1243 de
[0].d_reclen
!= 65535)
1245 unix_name
[pos
- 1] = '/';
1248 if (!de
[0].d_reclen
) break;
1250 if (de
[1].d_name
[0])
1252 ret
= ntdll_umbstowcs( 0, de
[1].d_name
, strlen(de
[1].d_name
),
1253 buffer
, MAX_DIR_ENTRY_LEN
);
1254 if (ret
== length
&& !memicmpW( buffer
, name
, length
))
1256 strcpy( unix_name
+ pos
, de
[1].d_name
);
1258 return STATUS_SUCCESS
;
1261 ret
= ntdll_umbstowcs( 0, de
[0].d_name
, strlen(de
[0].d_name
),
1262 buffer
, MAX_DIR_ENTRY_LEN
);
1263 if (ret
== length
&& !memicmpW( buffer
, name
, length
))
1265 strcpy( unix_name
+ pos
,
1266 de
[1].d_name
[0] ? de
[1].d_name
: de
[0].d_name
);
1268 return STATUS_SUCCESS
;
1270 if (ioctl( fd
, VFAT_IOCTL_READDIR_BOTH
, (long)de
) == -1)
1279 /* fall through to normal handling */
1281 #endif /* VFAT_IOCTL_READDIR_BOTH */
1283 if (!(dir
= opendir( unix_name
)))
1285 if (errno
== ENOENT
) return STATUS_OBJECT_PATH_NOT_FOUND
;
1286 else return FILE_GetNtStatus();
1288 unix_name
[pos
- 1] = '/';
1289 str
.Buffer
= buffer
;
1290 str
.MaximumLength
= sizeof(buffer
);
1291 while ((de
= readdir( dir
)))
1293 ret
= ntdll_umbstowcs( 0, de
->d_name
, strlen(de
->d_name
), buffer
, MAX_DIR_ENTRY_LEN
);
1294 if (ret
== length
&& !memicmpW( buffer
, name
, length
))
1296 strcpy( unix_name
+ pos
, de
->d_name
);
1298 return STATUS_SUCCESS
;
1301 if (!is_name_8_dot_3
) continue;
1303 str
.Length
= ret
* sizeof(WCHAR
);
1304 if (!RtlIsNameLegalDOS8Dot3( &str
, NULL
, &spaces
) || spaces
)
1306 WCHAR short_nameW
[12];
1307 ret
= hash_short_file_name( &str
, short_nameW
);
1308 if (ret
== length
&& !memicmpW( short_nameW
, name
, length
))
1310 strcpy( unix_name
+ pos
, de
->d_name
);
1312 return STATUS_SUCCESS
;
1317 goto not_found
; /* avoid warning */
1320 unix_name
[pos
- 1] = 0;
1321 return STATUS_OBJECT_PATH_NOT_FOUND
;
1325 /******************************************************************************
1328 * Get the Unix path of a DOS device.
1330 static NTSTATUS
get_dos_device( const WCHAR
*name
, UINT name_len
, ANSI_STRING
*unix_name_ret
)
1332 const char *config_dir
= wine_get_config_dir();
1334 char *unix_name
, *new_name
, *dev
;
1338 /* make sure the device name is ASCII */
1339 for (i
= 0; i
< name_len
; i
++)
1340 if (name
[i
] <= 32 || name
[i
] >= 127) return STATUS_BAD_DEVICE_TYPE
;
1342 unix_len
= strlen(config_dir
) + sizeof("/dosdevices/") + name_len
+ 1;
1344 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, unix_len
)))
1345 return STATUS_NO_MEMORY
;
1347 strcpy( unix_name
, config_dir
);
1348 strcat( unix_name
, "/dosdevices/" );
1349 dev
= unix_name
+ strlen(unix_name
);
1351 for (i
= 0; i
< name_len
; i
++) dev
[i
] = (char)tolowerW(name
[i
]);
1354 /* special case for drive devices */
1355 if (name_len
== 2 && dev
[1] == ':')
1363 if (!stat( unix_name
, &st
))
1365 TRACE( "%s -> %s\n", debugstr_wn(name
,name_len
), debugstr_a(unix_name
) );
1366 unix_name_ret
->Buffer
= unix_name
;
1367 unix_name_ret
->Length
= strlen(unix_name
);
1368 unix_name_ret
->MaximumLength
= unix_len
;
1369 return STATUS_SUCCESS
;
1373 /* now try some defaults for it */
1374 if (!strcmp( dev
, "aux" ))
1376 strcpy( dev
, "com1" );
1379 if (!strcmp( dev
, "prn" ))
1381 strcpy( dev
, "lpt1" );
1384 if (!strcmp( dev
, "nul" ))
1386 strcpy( unix_name
, "/dev/null" );
1387 dev
= NULL
; /* last try */
1392 if (dev
[1] == ':' && dev
[2] == ':') /* drive device */
1394 dev
[2] = 0; /* remove last ':' to get the drive mount point symlink */
1395 new_name
= get_default_drive_device( unix_name
);
1397 else if (!strncmp( dev
, "com", 3 )) new_name
= get_default_com_device( dev
[3] - '0' );
1398 else if (!strncmp( dev
, "lpt", 3 )) new_name
= get_default_lpt_device( dev
[3] - '0' );
1400 if (!new_name
) break;
1402 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
1403 unix_name
= new_name
;
1404 unix_len
= strlen(unix_name
) + 1;
1405 dev
= NULL
; /* last try */
1407 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
1408 return STATUS_BAD_DEVICE_TYPE
;
1412 /* return the length of the DOS namespace prefix if any */
1413 static inline int get_dos_prefix_len( const UNICODE_STRING
*name
)
1415 static const WCHAR nt_prefixW
[] = {'\\','?','?','\\'};
1416 static const WCHAR dosdev_prefixW
[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\'};
1418 if (name
->Length
> sizeof(nt_prefixW
) &&
1419 !memcmp( name
->Buffer
, nt_prefixW
, sizeof(nt_prefixW
) ))
1420 return sizeof(nt_prefixW
) / sizeof(WCHAR
);
1422 if (name
->Length
> sizeof(dosdev_prefixW
) &&
1423 !memicmpW( name
->Buffer
, dosdev_prefixW
, sizeof(dosdev_prefixW
)/sizeof(WCHAR
) ))
1424 return sizeof(dosdev_prefixW
) / sizeof(WCHAR
);
1430 /******************************************************************************
1431 * wine_nt_to_unix_file_name (NTDLL.@) Not a Windows API
1433 * Convert a file name from NT namespace to Unix namespace.
1435 * If disposition is not FILE_OPEN or FILE_OVERWRITTE, the last path
1436 * element doesn't have to exist; in that case STATUS_NO_SUCH_FILE is
1437 * returned, but the unix name is still filled in properly.
1439 NTSTATUS
wine_nt_to_unix_file_name( const UNICODE_STRING
*nameW
, ANSI_STRING
*unix_name_ret
,
1440 UINT disposition
, BOOLEAN check_case
)
1442 static const WCHAR uncW
[] = {'U','N','C','\\'};
1443 static const WCHAR invalid_charsW
[] = { INVALID_NT_CHARS
, 0 };
1445 NTSTATUS status
= STATUS_SUCCESS
;
1446 const char *config_dir
= wine_get_config_dir();
1447 const WCHAR
*name
, *p
;
1450 int pos
, ret
, name_len
, unix_len
, used_default
;
1452 name
= nameW
->Buffer
;
1453 name_len
= nameW
->Length
/ sizeof(WCHAR
);
1455 if (!name_len
|| !IS_SEPARATOR(name
[0])) return STATUS_OBJECT_PATH_SYNTAX_BAD
;
1457 if ((pos
= get_dos_prefix_len( nameW
)))
1459 BOOLEAN is_unc
= FALSE
;
1464 /* check for UNC prefix */
1465 if (name_len
> 4 && !memicmpW( name
, uncW
, 4 ))
1473 /* check for a drive letter with path */
1474 if (name_len
< 3 || !isalphaW(name
[0]) || name
[1] != ':' || !IS_SEPARATOR(name
[2]))
1476 /* not a drive with path, try other DOS devices */
1477 return get_dos_device( name
, name_len
, unix_name_ret
);
1479 name
+= 2; /* skip drive letter */
1483 /* check for invalid characters */
1484 for (p
= name
; p
< name
+ name_len
; p
++)
1485 if (*p
< 32 || strchrW( invalid_charsW
, *p
)) return STATUS_OBJECT_NAME_INVALID
;
1487 unix_len
= ntdll_wcstoumbs( 0, name
, name_len
, NULL
, 0, NULL
, NULL
);
1488 unix_len
+= MAX_DIR_ENTRY_LEN
+ 3;
1489 unix_len
+= strlen(config_dir
) + sizeof("/dosdevices/") + 3;
1490 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, unix_len
)))
1491 return STATUS_NO_MEMORY
;
1492 strcpy( unix_name
, config_dir
);
1493 strcat( unix_name
, "/dosdevices/" );
1494 pos
= strlen(unix_name
);
1497 strcpy( unix_name
+ pos
, "unc" );
1502 unix_name
[pos
++] = tolowerW( name
[-2] );
1503 unix_name
[pos
++] = ':';
1507 else /* no DOS prefix, assume NT native name, map directly to Unix */
1509 if (!name_len
|| !IS_SEPARATOR(name
[0])) return STATUS_OBJECT_NAME_INVALID
;
1510 unix_len
= ntdll_wcstoumbs( 0, name
, name_len
, NULL
, 0, NULL
, NULL
);
1511 unix_len
+= MAX_DIR_ENTRY_LEN
+ 3;
1512 if (!(unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, unix_len
)))
1513 return STATUS_NO_MEMORY
;
1517 /* try a shortcut first */
1519 ret
= ntdll_wcstoumbs( 0, name
, name_len
, unix_name
+ pos
, unix_len
- pos
- 1,
1520 NULL
, &used_default
);
1522 while (name_len
&& IS_SEPARATOR(*name
))
1528 if (ret
> 0 && !used_default
) /* if we used the default char the name didn't convert properly */
1531 unix_name
[pos
+ ret
] = 0;
1532 for (p
= unix_name
+ pos
; *p
; p
++) if (*p
== '\\') *p
= '/';
1533 if (!stat( unix_name
, &st
))
1535 /* creation fails with STATUS_ACCESS_DENIED for the root of the drive */
1536 if (disposition
== FILE_CREATE
)
1537 return name_len
? STATUS_OBJECT_NAME_COLLISION
: STATUS_ACCESS_DENIED
;
1542 if (!name_len
) /* empty name -> drive root doesn't exist */
1544 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
1545 return STATUS_OBJECT_PATH_NOT_FOUND
;
1547 if (check_case
&& (disposition
== FILE_OPEN
|| disposition
== FILE_OVERWRITE
))
1549 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
1550 return STATUS_OBJECT_NAME_NOT_FOUND
;
1553 /* now do it component by component */
1557 const WCHAR
*end
, *next
;
1560 while (end
< name
+ name_len
&& !IS_SEPARATOR(*end
)) end
++;
1562 while (next
< name
+ name_len
&& IS_SEPARATOR(*next
)) next
++;
1563 name_len
-= next
- name
;
1565 /* grow the buffer if needed */
1567 if (unix_len
- pos
< MAX_DIR_ENTRY_LEN
+ 2)
1570 unix_len
+= 2 * MAX_DIR_ENTRY_LEN
;
1571 if (!(new_name
= RtlReAllocateHeap( GetProcessHeap(), 0, unix_name
, unix_len
)))
1573 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
1574 return STATUS_NO_MEMORY
;
1576 unix_name
= new_name
;
1579 status
= find_file_in_dir( unix_name
, pos
, name
, end
- name
, check_case
);
1581 /* if this is the last element, not finding it is not necessarily fatal */
1584 if (status
== STATUS_OBJECT_PATH_NOT_FOUND
)
1586 status
= STATUS_OBJECT_NAME_NOT_FOUND
;
1587 if (disposition
!= FILE_OPEN
&& disposition
!= FILE_OVERWRITE
)
1589 ret
= ntdll_wcstoumbs( 0, name
, end
- name
, unix_name
+ pos
+ 1,
1590 MAX_DIR_ENTRY_LEN
, NULL
, &used_default
);
1591 if (ret
> 0 && !used_default
)
1593 unix_name
[pos
] = '/';
1594 unix_name
[pos
+ 1 + ret
] = 0;
1595 status
= STATUS_NO_SUCH_FILE
;
1600 else if (status
== STATUS_SUCCESS
&& disposition
== FILE_CREATE
)
1602 status
= STATUS_OBJECT_NAME_COLLISION
;
1606 if (status
!= STATUS_SUCCESS
)
1608 /* couldn't find it at all, fail */
1609 WARN( "%s not found in %s\n", debugstr_w(name
), unix_name
);
1610 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
1614 pos
+= strlen( unix_name
+ pos
);
1618 WARN( "%s -> %s required a case-insensitive search\n",
1619 debugstr_us(nameW
), debugstr_a(unix_name
) );
1622 TRACE( "%s -> %s\n", debugstr_us(nameW
), debugstr_a(unix_name
) );
1623 unix_name_ret
->Buffer
= unix_name
;
1624 unix_name_ret
->Length
= strlen(unix_name
);
1625 unix_name_ret
->MaximumLength
= unix_len
;
1630 /******************************************************************
1631 * RtlDoesFileExists_U (NTDLL.@)
1633 BOOLEAN WINAPI
RtlDoesFileExists_U(LPCWSTR file_name
)
1635 UNICODE_STRING nt_name
;
1636 ANSI_STRING unix_name
;
1639 if (!RtlDosPathNameToNtPathName_U( file_name
, &nt_name
, NULL
, NULL
)) return FALSE
;
1640 ret
= (wine_nt_to_unix_file_name( &nt_name
, &unix_name
, FILE_OPEN
, FALSE
) == STATUS_SUCCESS
);
1641 if (ret
) RtlFreeAnsiString( &unix_name
);
1642 RtlFreeUnicodeString( &nt_name
);
1647 /***********************************************************************
1648 * DIR_unmount_device
1650 * Unmount the specified device.
1652 NTSTATUS
DIR_unmount_device( HANDLE handle
)
1657 SERVER_START_REQ( unmount_device
)
1659 req
->handle
= handle
;
1660 status
= wine_server_call( req
);
1663 if (status
) return status
;
1665 if (!(status
= wine_server_handle_to_fd( handle
, 0, &unix_fd
, NULL
)))
1668 char *mount_point
= NULL
;
1670 if (fstat( unix_fd
, &st
) == -1 || !S_ISBLK(st
.st_mode
))
1671 status
= STATUS_INVALID_PARAMETER
;
1674 if ((mount_point
= get_device_mount_point( st
.st_rdev
)))
1676 static const char umount
[] = "umount >/dev/null 2>&1 ";
1677 char *cmd
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(mount_point
)+sizeof(umount
));
1680 strcpy( cmd
, umount
);
1681 strcat( cmd
, mount_point
);
1683 RtlFreeHeap( GetProcessHeap(), 0, cmd
);
1685 /* umount will fail to release the loop device since we still have
1686 a handle to it, so we release it here */
1687 if (major(st
.st_rdev
) == LOOP_MAJOR
) ioctl( unix_fd
, 0x4c01 /*LOOP_CLR_FD*/, 0 );
1690 RtlFreeHeap( GetProcessHeap(), 0, mount_point
);
1693 wine_server_release_fd( handle
, unix_fd
);
1699 /******************************************************************************
1702 * Retrieve the Unix name of the current directory; helper for wine_unix_to_nt_file_name.
1703 * Returned value must be freed by caller.
1705 NTSTATUS
DIR_get_unix_cwd( char **cwd
)
1707 int old_cwd
, unix_fd
;
1712 RtlAcquirePebLock();
1714 if (NtCurrentTeb()->Tib
.SubSystemTib
) /* FIXME: hack */
1715 curdir
= &((WIN16_SUBSYSTEM_TIB
*)NtCurrentTeb()->Tib
.SubSystemTib
)->curdir
;
1717 curdir
= &NtCurrentTeb()->Peb
->ProcessParameters
->CurrentDirectory
;
1719 if (!(handle
= curdir
->Handle
))
1721 UNICODE_STRING dirW
;
1722 OBJECT_ATTRIBUTES attr
;
1725 if (!RtlDosPathNameToNtPathName_U( curdir
->DosPath
.Buffer
, &dirW
, NULL
, NULL
))
1727 status
= STATUS_OBJECT_NAME_INVALID
;
1730 attr
.Length
= sizeof(attr
);
1731 attr
.RootDirectory
= 0;
1732 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1733 attr
.ObjectName
= &dirW
;
1734 attr
.SecurityDescriptor
= NULL
;
1735 attr
.SecurityQualityOfService
= NULL
;
1737 status
= NtOpenFile( &handle
, 0, &attr
, &io
, 0,
1738 FILE_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
);
1739 RtlFreeUnicodeString( &dirW
);
1740 if (status
!= STATUS_SUCCESS
) goto done
;
1743 if ((status
= wine_server_handle_to_fd( handle
, 0, &unix_fd
, NULL
)) == STATUS_SUCCESS
)
1745 RtlEnterCriticalSection( &dir_section
);
1747 if ((old_cwd
= open(".", O_RDONLY
)) != -1 && fchdir( unix_fd
) != -1)
1749 unsigned int size
= 512;
1753 if (!(*cwd
= RtlAllocateHeap( GetProcessHeap(), 0, size
)))
1755 status
= STATUS_NO_MEMORY
;
1758 if (getcwd( *cwd
, size
)) break;
1759 RtlFreeHeap( GetProcessHeap(), 0, *cwd
);
1760 if (errno
!= ERANGE
)
1762 status
= STATUS_OBJECT_PATH_INVALID
;
1767 if (fchdir( old_cwd
) == -1) chdir( "/" );
1769 else status
= FILE_GetNtStatus();
1771 RtlLeaveCriticalSection( &dir_section
);
1772 wine_server_release_fd( handle
, unix_fd
);
1774 if (!curdir
->Handle
) NtClose( handle
);
1777 RtlReleasePebLock();
1781 #define FILE_NOTIFY_ALL ( \
1782 FILE_NOTIFY_CHANGE_FILE_NAME | \
1783 FILE_NOTIFY_CHANGE_DIR_NAME | \
1784 FILE_NOTIFY_CHANGE_ATTRIBUTES | \
1785 FILE_NOTIFY_CHANGE_SIZE | \
1786 FILE_NOTIFY_CHANGE_LAST_WRITE | \
1787 FILE_NOTIFY_CHANGE_LAST_ACCESS | \
1788 FILE_NOTIFY_CHANGE_CREATION | \
1789 FILE_NOTIFY_CHANGE_SECURITY )
1791 /******************************************************************************
1792 * NtNotifyChangeDirectoryFile [NTDLL.@]
1795 NtNotifyChangeDirectoryFile( HANDLE FileHandle
, HANDLE Event
,
1796 PIO_APC_ROUTINE ApcRoutine
, PVOID ApcContext
,
1797 PIO_STATUS_BLOCK IoStatusBlock
, PVOID Buffer
,
1798 ULONG BufferSize
, ULONG CompletionFilter
, BOOLEAN WatchTree
)
1802 TRACE("%p %p %p %p %p %p %lu %lu %d\n",
1803 FileHandle
, Event
, ApcRoutine
, ApcContext
, IoStatusBlock
,
1804 Buffer
, BufferSize
, CompletionFilter
, WatchTree
);
1807 return STATUS_ACCESS_VIOLATION
;
1809 if (CompletionFilter
== 0 || (CompletionFilter
& ~FILE_NOTIFY_ALL
))
1810 return STATUS_INVALID_PARAMETER
;
1812 if (ApcRoutine
|| ApcContext
|| Buffer
|| BufferSize
|| WatchTree
)
1813 FIXME("parameters ignored %p %p %p %lu %d\n",
1814 ApcRoutine
, ApcContext
, Buffer
, BufferSize
, WatchTree
);
1816 SERVER_START_REQ( read_directory_changes
)
1818 req
->handle
= FileHandle
;
1820 req
->filter
= CompletionFilter
;
1821 status
= wine_server_call( req
);