d2d1: Implement d2d_d3d_render_target_CreateBitmap().
[wine/multimedia.git] / dlls / ntdll / directory.c
blob01998b2c46220ab985b33b932dabcc347b051dc3
1 /*
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
23 #include "config.h"
24 #include "wine/port.h"
26 #include <assert.h>
27 #include <sys/types.h>
28 #ifdef HAVE_DIRENT_H
29 # include <dirent.h>
30 #endif
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <stdarg.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <limits.h>
38 #ifdef HAVE_MNTENT_H
39 #include <mntent.h>
40 #endif
41 #ifdef HAVE_SYS_STAT_H
42 # include <sys/stat.h>
43 #endif
44 #ifdef HAVE_SYS_SYSCALL_H
45 # include <sys/syscall.h>
46 #endif
47 #ifdef HAVE_SYS_ATTR_H
48 #include <sys/attr.h>
49 #endif
50 #ifdef HAVE_SYS_IOCTL_H
51 #include <sys/ioctl.h>
52 #endif
53 #ifdef HAVE_LINUX_IOCTL_H
54 #include <linux/ioctl.h>
55 #endif
56 #ifdef HAVE_LINUX_MAJOR_H
57 # include <linux/major.h>
58 #endif
59 #ifdef HAVE_SYS_PARAM_H
60 #include <sys/param.h>
61 #endif
62 #ifdef HAVE_SYS_MOUNT_H
63 #include <sys/mount.h>
64 #endif
65 #ifdef HAVE_SYS_STATFS_H
66 #include <sys/statfs.h>
67 #endif
68 #include <time.h>
69 #ifdef HAVE_UNISTD_H
70 # include <unistd.h>
71 #endif
73 #define NONAMELESSUNION
74 #define NONAMELESSSTRUCT
75 #include "ntstatus.h"
76 #define WIN32_NO_STATUS
77 #include "windef.h"
78 #include "winnt.h"
79 #include "winternl.h"
80 #include "ddk/wdm.h"
81 #include "ntdll_misc.h"
82 #include "wine/unicode.h"
83 #include "wine/server.h"
84 #include "wine/list.h"
85 #include "wine/library.h"
86 #include "wine/debug.h"
88 WINE_DEFAULT_DEBUG_CHANNEL(file);
90 /* just in case... */
91 #undef VFAT_IOCTL_READDIR_BOTH
92 #undef USE_GETDENTS
94 #ifdef linux
96 /* We want the real kernel dirent structure, not the libc one */
97 typedef struct
99 long d_ino;
100 long d_off;
101 unsigned short d_reclen;
102 char d_name[256];
103 } KERNEL_DIRENT;
105 /* Define the VFAT ioctl to get both short and long file names */
106 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, KERNEL_DIRENT [2] )
108 #ifndef O_DIRECTORY
109 # define O_DIRECTORY 0200000 /* must be directory */
110 #endif
112 #ifdef __NR_getdents64
113 typedef struct
115 ULONG64 d_ino;
116 LONG64 d_off;
117 unsigned short d_reclen;
118 unsigned char d_type;
119 char d_name[256];
120 } KERNEL_DIRENT64;
122 #undef getdents64
123 static inline int getdents64( int fd, char *de, unsigned int size )
125 return syscall( __NR_getdents64, fd, de, size );
127 #define USE_GETDENTS
128 #endif
130 #endif /* linux */
132 #define IS_OPTION_TRUE(ch) ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
133 #define IS_SEPARATOR(ch) ((ch) == '\\' || (ch) == '/')
135 #define INVALID_NT_CHARS '*','?','<','>','|','"'
136 #define INVALID_DOS_CHARS INVALID_NT_CHARS,'+','=',',',';','[',']',' ','\345'
138 #define MAX_DIR_ENTRY_LEN 255 /* max length of a directory entry in chars */
140 #define MAX_IGNORED_FILES 4
142 struct file_identity
144 dev_t dev;
145 ino_t ino;
148 static struct file_identity ignored_files[MAX_IGNORED_FILES];
149 static unsigned int ignored_files_count;
151 union file_directory_info
153 ULONG next;
154 FILE_DIRECTORY_INFORMATION dir;
155 FILE_BOTH_DIRECTORY_INFORMATION both;
156 FILE_FULL_DIRECTORY_INFORMATION full;
157 FILE_ID_BOTH_DIRECTORY_INFORMATION id_both;
158 FILE_ID_FULL_DIRECTORY_INFORMATION id_full;
161 static BOOL show_dot_files;
162 static RTL_RUN_ONCE init_once = RTL_RUN_ONCE_INIT;
164 /* at some point we may want to allow Winelib apps to set this */
165 static const BOOL is_case_sensitive = FALSE;
167 UNICODE_STRING system_dir = { 0, 0, NULL }; /* system directory */
169 static struct file_identity curdir;
170 static struct file_identity windir;
172 static RTL_CRITICAL_SECTION dir_section;
173 static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
175 0, 0, &dir_section,
176 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
177 0, 0, { (DWORD_PTR)(__FILE__ ": dir_section") }
179 static RTL_CRITICAL_SECTION dir_section = { &critsect_debug, -1, 0, 0, 0, 0 };
182 /* check if a given Unicode char is OK in a DOS short name */
183 static inline BOOL is_invalid_dos_char( WCHAR ch )
185 static const WCHAR invalid_chars[] = { INVALID_DOS_CHARS,'~','.',0 };
186 if (ch > 0x7f) return TRUE;
187 return strchrW( invalid_chars, ch ) != NULL;
190 /* check if the device can be a mounted volume */
191 static inline BOOL is_valid_mounted_device( const struct stat *st )
193 #if defined(linux) || defined(__sun__)
194 return S_ISBLK( st->st_mode );
195 #else
196 /* disks are char devices on *BSD */
197 return S_ISCHR( st->st_mode );
198 #endif
201 static inline void ignore_file( const char *name )
203 struct stat st;
204 assert( ignored_files_count < MAX_IGNORED_FILES );
205 if (!stat( name, &st ))
207 ignored_files[ignored_files_count].dev = st.st_dev;
208 ignored_files[ignored_files_count].ino = st.st_ino;
209 ignored_files_count++;
213 static inline BOOL is_same_file( const struct file_identity *file, const struct stat *st )
215 return st->st_dev == file->dev && st->st_ino == file->ino;
218 static inline BOOL is_ignored_file( const struct stat *st )
220 unsigned int i;
222 for (i = 0; i < ignored_files_count; i++)
223 if (is_same_file( &ignored_files[i], st )) return TRUE;
224 return FALSE;
227 static inline unsigned int dir_info_size( FILE_INFORMATION_CLASS class, unsigned int len )
229 switch (class)
231 case FileDirectoryInformation:
232 return (FIELD_OFFSET( FILE_DIRECTORY_INFORMATION, FileName[len] ) + 7) & ~7;
233 case FileBothDirectoryInformation:
234 return (FIELD_OFFSET( FILE_BOTH_DIRECTORY_INFORMATION, FileName[len] ) + 7) & ~7;
235 case FileFullDirectoryInformation:
236 return (FIELD_OFFSET( FILE_FULL_DIRECTORY_INFORMATION, FileName[len] ) + 7) & ~7;
237 case FileIdBothDirectoryInformation:
238 return (FIELD_OFFSET( FILE_ID_BOTH_DIRECTORY_INFORMATION, FileName[len] ) + 7) & ~7;
239 case FileIdFullDirectoryInformation:
240 return (FIELD_OFFSET( FILE_ID_FULL_DIRECTORY_INFORMATION, FileName[len] ) + 7) & ~7;
241 default:
242 assert(0);
243 return 0;
247 static inline unsigned int max_dir_info_size( FILE_INFORMATION_CLASS class )
249 return dir_info_size( class, MAX_DIR_ENTRY_LEN );
252 static inline BOOL has_wildcard( const UNICODE_STRING *mask )
254 return (!mask ||
255 memchrW( mask->Buffer, '*', mask->Length / sizeof(WCHAR) ) ||
256 memchrW( mask->Buffer, '?', mask->Length / sizeof(WCHAR) ));
260 /* support for a directory queue for filesystem searches */
262 struct dir_name
264 struct list entry;
265 char name[1];
268 static struct list dir_queue = LIST_INIT( dir_queue );
270 static NTSTATUS add_dir_to_queue( const char *name )
272 int len = strlen( name ) + 1;
273 struct dir_name *dir = RtlAllocateHeap( GetProcessHeap(), 0,
274 FIELD_OFFSET( struct dir_name, name[len] ));
275 if (!dir) return STATUS_NO_MEMORY;
276 strcpy( dir->name, name );
277 list_add_tail( &dir_queue, &dir->entry );
278 return STATUS_SUCCESS;
281 static NTSTATUS next_dir_in_queue( char *name )
283 struct list *head = list_head( &dir_queue );
284 if (head)
286 struct dir_name *dir = LIST_ENTRY( head, struct dir_name, entry );
287 strcpy( name, dir->name );
288 list_remove( &dir->entry );
289 RtlFreeHeap( GetProcessHeap(), 0, dir );
290 return STATUS_SUCCESS;
292 return STATUS_OBJECT_NAME_NOT_FOUND;
295 static void flush_dir_queue(void)
297 struct list *head;
299 while ((head = list_head( &dir_queue )))
301 struct dir_name *dir = LIST_ENTRY( head, struct dir_name, entry );
302 list_remove( &dir->entry );
303 RtlFreeHeap( GetProcessHeap(), 0, dir );
308 /***********************************************************************
309 * get_default_com_device
311 * Return the default device to use for serial ports.
313 static char *get_default_com_device( int num )
315 char *ret = NULL;
317 if (!num || num > 9) return ret;
318 #ifdef linux
319 ret = RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/ttyS0") );
320 if (ret)
322 strcpy( ret, "/dev/ttyS0" );
323 ret[strlen(ret) - 1] = '0' + num - 1;
325 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
326 ret = RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/cuau0") );
327 if (ret)
329 strcpy( ret, "/dev/cuau0" );
330 ret[strlen(ret) - 1] = '0' + num - 1;
332 #elif defined(__DragonFly__)
333 ret = RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/cuaa0") );
334 if (ret)
336 strcpy( ret, "/dev/cuaa0" );
337 ret[strlen(ret) - 1] = '0' + num - 1;
339 #else
340 FIXME( "no known default for device com%d\n", num );
341 #endif
342 return ret;
346 /***********************************************************************
347 * get_default_lpt_device
349 * Return the default device to use for parallel ports.
351 static char *get_default_lpt_device( int num )
353 char *ret = NULL;
355 if (!num || num > 9) return ret;
356 #ifdef linux
357 ret = RtlAllocateHeap( GetProcessHeap(), 0, sizeof("/dev/lp0") );
358 if (ret)
360 strcpy( ret, "/dev/lp0" );
361 ret[strlen(ret) - 1] = '0' + num - 1;
363 #else
364 FIXME( "no known default for device lpt%d\n", num );
365 #endif
366 return ret;
369 #ifdef __ANDROID__
371 static char *unescape_field( char *str )
373 char *in, *out;
375 for (in = out = str; *in; in++, out++)
377 *out = *in;
378 if (in[0] == '\\')
380 if (in[1] == '\\')
382 out[0] = '\\';
383 in++;
385 else if (in[1] == '0' && in[2] == '4' && in[3] == '0')
387 out[0] = ' ';
388 in += 3;
390 else if (in[1] == '0' && in[2] == '1' && in[3] == '1')
392 out[0] = '\t';
393 in += 3;
395 else if (in[1] == '0' && in[2] == '1' && in[3] == '2')
397 out[0] = '\n';
398 in += 3;
400 else if (in[1] == '1' && in[2] == '3' && in[3] == '4')
402 out[0] = '\\';
403 in += 3;
407 *out = '\0';
409 return str;
412 static inline char *get_field( char **str )
414 char *ret;
416 ret = strsep( str, " \t" );
417 if (*str) *str += strspn( *str, " \t" );
419 return ret;
421 /************************************************************************
422 * getmntent_replacement
424 * getmntent replacement for Android.
426 * NB returned static buffer is not thread safe; protect with dir_section.
428 static struct mntent *getmntent_replacement( FILE *f )
430 static struct mntent entry;
431 static char buf[4096];
432 char *p, *start;
436 if (!fgets( buf, sizeof(buf), f )) return NULL;
437 p = strchr( buf, '\n' );
438 if (p) *p = '\0';
439 else /* Partially unread line, move file ptr to end */
441 char tmp[1024];
442 while (fgets( tmp, sizeof(tmp), f ))
443 if (strchr( tmp, '\n' )) break;
445 start = buf + strspn( buf, " \t" );
446 } while (start[0] == '\0' || start[0] == '#');
448 p = get_field( &start );
449 entry.mnt_fsname = p ? unescape_field( p ) : (char *)"";
451 p = get_field( &start );
452 entry.mnt_dir = p ? unescape_field( p ) : (char *)"";
454 p = get_field( &start );
455 entry.mnt_type = p ? unescape_field( p ) : (char *)"";
457 p = get_field( &start );
458 entry.mnt_opts = p ? unescape_field( p ) : (char *)"";
460 p = get_field( &start );
461 entry.mnt_freq = p ? atoi(p) : 0;
463 p = get_field( &start );
464 entry.mnt_passno = p ? atoi(p) : 0;
466 return &entry;
468 #define getmntent getmntent_replacement
469 #endif
471 /***********************************************************************
472 * DIR_get_drives_info
474 * Retrieve device/inode number for all the drives. Helper for find_drive_root.
476 unsigned int DIR_get_drives_info( struct drive_info info[MAX_DOS_DRIVES] )
478 static struct drive_info cache[MAX_DOS_DRIVES];
479 static time_t last_update;
480 static unsigned int nb_drives;
481 unsigned int ret;
482 time_t now = time(NULL);
484 RtlEnterCriticalSection( &dir_section );
485 if (now != last_update)
487 const char *config_dir = wine_get_config_dir();
488 char *buffer, *p;
489 struct stat st;
490 unsigned int i;
492 if ((buffer = RtlAllocateHeap( GetProcessHeap(), 0,
493 strlen(config_dir) + sizeof("/dosdevices/a:") )))
495 strcpy( buffer, config_dir );
496 strcat( buffer, "/dosdevices/a:" );
497 p = buffer + strlen(buffer) - 2;
499 for (i = nb_drives = 0; i < MAX_DOS_DRIVES; i++)
501 *p = 'a' + i;
502 if (!stat( buffer, &st ))
504 cache[i].dev = st.st_dev;
505 cache[i].ino = st.st_ino;
506 nb_drives++;
508 else
510 cache[i].dev = 0;
511 cache[i].ino = 0;
514 RtlFreeHeap( GetProcessHeap(), 0, buffer );
516 last_update = now;
518 memcpy( info, cache, sizeof(cache) );
519 ret = nb_drives;
520 RtlLeaveCriticalSection( &dir_section );
521 return ret;
525 /***********************************************************************
526 * parse_mount_entries
528 * Parse mount entries looking for a given device. Helper for get_default_drive_device.
531 #ifdef sun
532 #include <sys/vfstab.h>
533 static char *parse_vfstab_entries( FILE *f, dev_t dev, ino_t ino)
535 struct vfstab entry;
536 struct stat st;
537 char *device;
539 while (! getvfsent( f, &entry ))
541 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
542 if (!strcmp( entry.vfs_fstype, "nfs" ) ||
543 !strcmp( entry.vfs_fstype, "smbfs" ) ||
544 !strcmp( entry.vfs_fstype, "ncpfs" )) continue;
546 if (stat( entry.vfs_mountp, &st ) == -1) continue;
547 if (st.st_dev != dev || st.st_ino != ino) continue;
548 if (!strcmp( entry.vfs_fstype, "fd" ))
550 if ((device = strstr( entry.vfs_mntopts, "dev=" )))
552 char *p = strchr( device + 4, ',' );
553 if (p) *p = 0;
554 return device + 4;
557 else
558 return entry.vfs_special;
560 return NULL;
562 #endif
564 #ifdef linux
565 static char *parse_mount_entries( FILE *f, dev_t dev, ino_t ino )
567 struct mntent *entry;
568 struct stat st;
569 char *device;
571 while ((entry = getmntent( f )))
573 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
574 if (!strcmp( entry->mnt_type, "nfs" ) ||
575 !strcmp( entry->mnt_type, "smbfs" ) ||
576 !strcmp( entry->mnt_type, "ncpfs" )) continue;
578 if (stat( entry->mnt_dir, &st ) == -1) continue;
579 if (st.st_dev != dev || st.st_ino != ino) continue;
580 if (!strcmp( entry->mnt_type, "supermount" ))
582 if ((device = strstr( entry->mnt_opts, "dev=" )))
584 char *p = strchr( device + 4, ',' );
585 if (p) *p = 0;
586 return device + 4;
589 else if (!stat( entry->mnt_fsname, &st ) && S_ISREG(st.st_mode))
591 /* if device is a regular file check for a loop mount */
592 if ((device = strstr( entry->mnt_opts, "loop=" )))
594 char *p = strchr( device + 5, ',' );
595 if (p) *p = 0;
596 return device + 5;
599 else
600 return entry->mnt_fsname;
602 return NULL;
604 #endif
606 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
607 #include <fstab.h>
608 static char *parse_mount_entries( FILE *f, dev_t dev, ino_t ino )
610 struct fstab *entry;
611 struct stat st;
613 while ((entry = getfsent()))
615 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
616 if (!strcmp( entry->fs_vfstype, "nfs" ) ||
617 !strcmp( entry->fs_vfstype, "smbfs" ) ||
618 !strcmp( entry->fs_vfstype, "ncpfs" )) continue;
620 if (stat( entry->fs_file, &st ) == -1) continue;
621 if (st.st_dev != dev || st.st_ino != ino) continue;
622 return entry->fs_spec;
624 return NULL;
626 #endif
628 #ifdef sun
629 #include <sys/mnttab.h>
630 static char *parse_mount_entries( FILE *f, dev_t dev, ino_t ino )
632 struct mnttab entry;
633 struct stat st;
634 char *device;
637 while (( ! getmntent( f, &entry) ))
639 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
640 if (!strcmp( entry.mnt_fstype, "nfs" ) ||
641 !strcmp( entry.mnt_fstype, "smbfs" ) ||
642 !strcmp( entry.mnt_fstype, "ncpfs" )) continue;
644 if (stat( entry.mnt_mountp, &st ) == -1) continue;
645 if (st.st_dev != dev || st.st_ino != ino) continue;
646 if (!strcmp( entry.mnt_fstype, "fd" ))
648 if ((device = strstr( entry.mnt_mntopts, "dev=" )))
650 char *p = strchr( device + 4, ',' );
651 if (p) *p = 0;
652 return device + 4;
655 else
656 return entry.mnt_special;
658 return NULL;
660 #endif
662 /***********************************************************************
663 * get_default_drive_device
665 * Return the default device to use for a given drive mount point.
667 static char *get_default_drive_device( const char *root )
669 char *ret = NULL;
671 #ifdef linux
672 FILE *f;
673 char *device = NULL;
674 int fd, res = -1;
675 struct stat st;
677 /* try to open it first to force it to get mounted */
678 if ((fd = open( root, O_RDONLY | O_DIRECTORY )) != -1)
680 res = fstat( fd, &st );
681 close( fd );
683 /* now try normal stat just in case */
684 if (res == -1) res = stat( root, &st );
685 if (res == -1) return NULL;
687 RtlEnterCriticalSection( &dir_section );
689 #ifdef __ANDROID__
690 if ((f = fopen( "/proc/mounts", "r" )))
692 device = parse_mount_entries( f, st.st_dev, st.st_ino );
693 fclose( f );
695 #else
696 if ((f = fopen( "/etc/mtab", "r" )))
698 device = parse_mount_entries( f, st.st_dev, st.st_ino );
699 fclose( f );
701 /* look through fstab too in case it's not mounted (for instance if it's an audio CD) */
702 if (!device && (f = fopen( "/etc/fstab", "r" )))
704 device = parse_mount_entries( f, st.st_dev, st.st_ino );
705 fclose( f );
707 #endif
708 if (device)
710 ret = RtlAllocateHeap( GetProcessHeap(), 0, strlen(device) + 1 );
711 if (ret) strcpy( ret, device );
713 RtlLeaveCriticalSection( &dir_section );
715 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__ ) || defined(__DragonFly__)
716 char *device = NULL;
717 int fd, res = -1;
718 struct stat st;
720 /* try to open it first to force it to get mounted */
721 if ((fd = open( root, O_RDONLY )) != -1)
723 res = fstat( fd, &st );
724 close( fd );
726 /* now try normal stat just in case */
727 if (res == -1) res = stat( root, &st );
728 if (res == -1) return NULL;
730 RtlEnterCriticalSection( &dir_section );
732 /* The FreeBSD parse_mount_entries doesn't require a file argument, so just
733 * pass NULL. Leave the argument in for symmetry.
735 device = parse_mount_entries( NULL, st.st_dev, st.st_ino );
736 if (device)
738 ret = RtlAllocateHeap( GetProcessHeap(), 0, strlen(device) + 1 );
739 if (ret) strcpy( ret, device );
741 RtlLeaveCriticalSection( &dir_section );
743 #elif defined( sun )
744 FILE *f;
745 char *device = NULL;
746 int fd, res = -1;
747 struct stat st;
749 /* try to open it first to force it to get mounted */
750 if ((fd = open( root, O_RDONLY )) != -1)
752 res = fstat( fd, &st );
753 close( fd );
755 /* now try normal stat just in case */
756 if (res == -1) res = stat( root, &st );
757 if (res == -1) return NULL;
759 RtlEnterCriticalSection( &dir_section );
761 if ((f = fopen( "/etc/mnttab", "r" )))
763 device = parse_mount_entries( f, st.st_dev, st.st_ino);
764 fclose( f );
766 /* look through fstab too in case it's not mounted (for instance if it's an audio CD) */
767 if (!device && (f = fopen( "/etc/vfstab", "r" )))
769 device = parse_vfstab_entries( f, st.st_dev, st.st_ino );
770 fclose( f );
772 if (device)
774 ret = RtlAllocateHeap( GetProcessHeap(), 0, strlen(device) + 1 );
775 if (ret) strcpy( ret, device );
777 RtlLeaveCriticalSection( &dir_section );
779 #elif defined(__APPLE__)
780 struct statfs *mntStat;
781 struct stat st;
782 int i;
783 int mntSize;
784 dev_t dev;
785 ino_t ino;
786 static const char path_bsd_device[] = "/dev/disk";
787 int res;
789 res = stat( root, &st );
790 if (res == -1) return NULL;
792 dev = st.st_dev;
793 ino = st.st_ino;
795 RtlEnterCriticalSection( &dir_section );
797 mntSize = getmntinfo(&mntStat, MNT_NOWAIT);
799 for (i = 0; i < mntSize && !ret; i++)
801 if (stat(mntStat[i].f_mntonname, &st ) == -1) continue;
802 if (st.st_dev != dev || st.st_ino != ino) continue;
804 /* FIXME add support for mounted network drive */
805 if ( strncmp(mntStat[i].f_mntfromname, path_bsd_device, strlen(path_bsd_device)) == 0)
807 /* set return value to the corresponding raw BSD node */
808 ret = RtlAllocateHeap( GetProcessHeap(), 0, strlen(mntStat[i].f_mntfromname) + 2 /* 2 : r and \0 */ );
809 if (ret)
811 strcpy(ret, "/dev/r");
812 strcat(ret, mntStat[i].f_mntfromname+sizeof("/dev/")-1);
816 RtlLeaveCriticalSection( &dir_section );
817 #else
818 static int warned;
819 if (!warned++) FIXME( "auto detection of DOS devices not supported on this platform\n" );
820 #endif
821 return ret;
825 /***********************************************************************
826 * get_device_mount_point
828 * Return the current mount point for a device.
830 static char *get_device_mount_point( dev_t dev )
832 char *ret = NULL;
834 #ifdef linux
835 FILE *f;
837 RtlEnterCriticalSection( &dir_section );
839 #ifdef __ANDROID__
840 if ((f = fopen( "/proc/mounts", "r" )))
841 #else
842 if ((f = fopen( "/etc/mtab", "r" )))
843 #endif
845 struct mntent *entry;
846 struct stat st;
847 char *p, *device;
849 while ((entry = getmntent( f )))
851 /* don't even bother stat'ing network mounts, there's no meaningful device anyway */
852 if (!strcmp( entry->mnt_type, "nfs" ) ||
853 !strcmp( entry->mnt_type, "smbfs" ) ||
854 !strcmp( entry->mnt_type, "ncpfs" )) continue;
856 if (!strcmp( entry->mnt_type, "supermount" ))
858 if ((device = strstr( entry->mnt_opts, "dev=" )))
860 device += 4;
861 if ((p = strchr( device, ',' ))) *p = 0;
864 else if (!stat( entry->mnt_fsname, &st ) && S_ISREG(st.st_mode))
866 /* if device is a regular file check for a loop mount */
867 if ((device = strstr( entry->mnt_opts, "loop=" )))
869 device += 5;
870 if ((p = strchr( device, ',' ))) *p = 0;
873 else device = entry->mnt_fsname;
875 if (device && !stat( device, &st ) && S_ISBLK(st.st_mode) && st.st_rdev == dev)
877 ret = RtlAllocateHeap( GetProcessHeap(), 0, strlen(entry->mnt_dir) + 1 );
878 if (ret) strcpy( ret, entry->mnt_dir );
879 break;
882 fclose( f );
884 RtlLeaveCriticalSection( &dir_section );
885 #elif defined(__APPLE__)
886 struct statfs *entry;
887 struct stat st;
888 int i, size;
890 RtlEnterCriticalSection( &dir_section );
892 size = getmntinfo( &entry, MNT_NOWAIT );
893 for (i = 0; i < size; i++)
895 if (stat( entry[i].f_mntfromname, &st ) == -1) continue;
896 if (S_ISBLK(st.st_mode) && st.st_rdev == dev)
898 ret = RtlAllocateHeap( GetProcessHeap(), 0, strlen(entry[i].f_mntonname) + 1 );
899 if (ret) strcpy( ret, entry[i].f_mntonname );
900 break;
903 RtlLeaveCriticalSection( &dir_section );
904 #else
905 static int warned;
906 if (!warned++) FIXME( "unmounting devices not supported on this platform\n" );
907 #endif
908 return ret;
912 #if defined(HAVE_GETATTRLIST) && defined(ATTR_VOL_CAPABILITIES) && \
913 defined(VOL_CAPABILITIES_FORMAT) && defined(VOL_CAP_FMT_CASE_SENSITIVE)
915 struct get_fsid
917 ULONG size;
918 dev_t dev;
919 fsid_t fsid;
922 struct fs_cache
924 dev_t dev;
925 fsid_t fsid;
926 BOOLEAN case_sensitive;
927 } fs_cache[64];
929 struct vol_caps
931 ULONG size;
932 vol_capabilities_attr_t caps;
935 /***********************************************************************
936 * look_up_fs_cache
938 * Checks if the specified file system is in the cache.
940 static struct fs_cache *look_up_fs_cache( dev_t dev )
942 int i;
943 for (i = 0; i < sizeof(fs_cache)/sizeof(fs_cache[0]); i++)
944 if (fs_cache[i].dev == dev)
945 return fs_cache+i;
946 return NULL;
949 /***********************************************************************
950 * add_fs_cache
952 * Adds the specified file system to the cache.
954 static void add_fs_cache( dev_t dev, fsid_t fsid, BOOLEAN case_sensitive )
956 int i;
957 struct fs_cache *entry = look_up_fs_cache( dev );
958 static int once = 0;
959 if (entry)
961 /* Update the cache */
962 entry->fsid = fsid;
963 entry->case_sensitive = case_sensitive;
964 return;
967 /* Add a new entry */
968 for (i = 0; i < sizeof(fs_cache)/sizeof(fs_cache[0]); i++)
969 if (fs_cache[i].dev == 0)
971 /* This entry is empty, use it */
972 fs_cache[i].dev = dev;
973 fs_cache[i].fsid = fsid;
974 fs_cache[i].case_sensitive = case_sensitive;
975 return;
978 /* Cache is out of space, warn */
979 if (!once++)
980 WARN( "FS cache is out of space, expect performance problems\n" );
983 /***********************************************************************
984 * get_dir_case_sensitivity_attr
986 * Checks if the volume containing the specified directory is case
987 * sensitive or not. Uses getattrlist(2).
989 static int get_dir_case_sensitivity_attr( const char *dir )
991 char *mntpoint = NULL;
992 struct attrlist attr;
993 struct vol_caps caps;
994 struct get_fsid get_fsid;
995 struct fs_cache *entry;
997 /* First get the FS ID of the volume */
998 attr.bitmapcount = ATTR_BIT_MAP_COUNT;
999 attr.reserved = 0;
1000 attr.commonattr = ATTR_CMN_DEVID|ATTR_CMN_FSID;
1001 attr.volattr = attr.dirattr = attr.fileattr = attr.forkattr = 0;
1002 get_fsid.size = 0;
1003 if (getattrlist( dir, &attr, &get_fsid, sizeof(get_fsid), 0 ) != 0 ||
1004 get_fsid.size != sizeof(get_fsid))
1005 return -1;
1006 /* Try to look it up in the cache */
1007 entry = look_up_fs_cache( get_fsid.dev );
1008 if (entry && !memcmp( &entry->fsid, &get_fsid.fsid, sizeof(fsid_t) ))
1009 /* Cache lookup succeeded */
1010 return entry->case_sensitive;
1011 /* Cache is stale at this point, we have to update it */
1013 mntpoint = get_device_mount_point( get_fsid.dev );
1014 /* Now look up the case-sensitivity */
1015 attr.commonattr = 0;
1016 attr.volattr = ATTR_VOL_INFO|ATTR_VOL_CAPABILITIES;
1017 if (getattrlist( mntpoint, &attr, &caps, sizeof(caps), 0 ) < 0)
1019 RtlFreeHeap( GetProcessHeap(), 0, mntpoint );
1020 add_fs_cache( get_fsid.dev, get_fsid.fsid, TRUE );
1021 return TRUE;
1023 RtlFreeHeap( GetProcessHeap(), 0, mntpoint );
1024 if (caps.size == sizeof(caps) &&
1025 (caps.caps.valid[VOL_CAPABILITIES_FORMAT] &
1026 (VOL_CAP_FMT_CASE_SENSITIVE | VOL_CAP_FMT_CASE_PRESERVING)) ==
1027 (VOL_CAP_FMT_CASE_SENSITIVE | VOL_CAP_FMT_CASE_PRESERVING))
1029 BOOLEAN ret;
1031 if ((caps.caps.capabilities[VOL_CAPABILITIES_FORMAT] &
1032 VOL_CAP_FMT_CASE_SENSITIVE) != VOL_CAP_FMT_CASE_SENSITIVE)
1033 ret = FALSE;
1034 else
1035 ret = TRUE;
1036 /* Update the cache */
1037 add_fs_cache( get_fsid.dev, get_fsid.fsid, ret );
1038 return ret;
1040 return FALSE;
1042 #endif
1044 /***********************************************************************
1045 * get_dir_case_sensitivity_stat
1047 * Checks if the volume containing the specified directory is case
1048 * sensitive or not. Uses statfs(2) or statvfs(2).
1050 static BOOLEAN get_dir_case_sensitivity_stat( const char *dir )
1052 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1053 struct statfs stfs;
1055 if (statfs( dir, &stfs ) == -1) return FALSE;
1056 /* Assume these file systems are always case insensitive on Mac OS.
1057 * For FreeBSD, only assume CIOPFS is case insensitive (AFAIK, Mac OS
1058 * is the only UNIX that supports case-insensitive lookup).
1060 if (!strcmp( stfs.f_fstypename, "fusefs" ) &&
1061 !strncmp( stfs.f_mntfromname, "ciopfs", 5 ))
1062 return FALSE;
1063 #ifdef __APPLE__
1064 if (!strcmp( stfs.f_fstypename, "msdos" ) ||
1065 !strcmp( stfs.f_fstypename, "cd9660" ) ||
1066 !strcmp( stfs.f_fstypename, "udf" ) ||
1067 !strcmp( stfs.f_fstypename, "ntfs" ) ||
1068 !strcmp( stfs.f_fstypename, "smbfs" ))
1069 return FALSE;
1070 #ifdef _DARWIN_FEATURE_64_BIT_INODE
1071 if (!strcmp( stfs.f_fstypename, "hfs" ) && (stfs.f_fssubtype == 0 ||
1072 stfs.f_fssubtype == 1 ||
1073 stfs.f_fssubtype == 128))
1074 return FALSE;
1075 #else
1076 /* The field says "reserved", but a quick look at the kernel source
1077 * tells us that this "reserved" field is really the same as the
1078 * "fssubtype" field from the inode64 structure (see munge_statfs()
1079 * in <xnu-source>/bsd/vfs/vfs_syscalls.c).
1081 if (!strcmp( stfs.f_fstypename, "hfs" ) && (stfs.f_reserved1 == 0 ||
1082 stfs.f_reserved1 == 1 ||
1083 stfs.f_reserved1 == 128))
1084 return FALSE;
1085 #endif
1086 #endif
1087 return TRUE;
1089 #elif defined(__NetBSD__)
1090 struct statvfs stfs;
1092 if (statvfs( dir, &stfs ) == -1) return FALSE;
1093 /* Only assume CIOPFS is case insensitive. */
1094 if (strcmp( stfs.f_fstypename, "fusefs" ) ||
1095 strncmp( stfs.f_mntfromname, "ciopfs", 5 ))
1096 return TRUE;
1097 return FALSE;
1099 #elif defined(__linux__)
1100 struct statfs stfs;
1101 struct stat st;
1102 char *cifile;
1104 /* Only assume CIOPFS is case insensitive. */
1105 if (statfs( dir, &stfs ) == -1) return FALSE;
1106 if (stfs.f_type != 0x65735546 /* FUSE_SUPER_MAGIC */)
1107 return TRUE;
1108 /* Normally, we'd have to parse the mtab to find out exactly what
1109 * kind of FUSE FS this is. But, someone on wine-devel suggested
1110 * a shortcut. We'll stat a special file in the directory. If it's
1111 * there, we'll assume it's a CIOPFS, else not.
1112 * This will break if somebody puts a file named ".ciopfs" in a non-
1113 * CIOPFS directory.
1115 cifile = RtlAllocateHeap( GetProcessHeap(), 0, strlen( dir )+sizeof("/.ciopfs") );
1116 if (!cifile) return TRUE;
1117 strcpy( cifile, dir );
1118 strcat( cifile, "/.ciopfs" );
1119 if (stat( cifile, &st ) == 0)
1121 RtlFreeHeap( GetProcessHeap(), 0, cifile );
1122 return FALSE;
1124 RtlFreeHeap( GetProcessHeap(), 0, cifile );
1125 return TRUE;
1126 #else
1127 return TRUE;
1128 #endif
1132 /***********************************************************************
1133 * get_dir_case_sensitivity
1135 * Checks if the volume containing the specified directory is case
1136 * sensitive or not. Uses statfs(2) or statvfs(2).
1138 static BOOLEAN get_dir_case_sensitivity( const char *dir )
1140 #if defined(HAVE_GETATTRLIST) && defined(ATTR_VOL_CAPABILITIES) && \
1141 defined(VOL_CAPABILITIES_FORMAT) && defined(VOL_CAP_FMT_CASE_SENSITIVE)
1142 int case_sensitive = get_dir_case_sensitivity_attr( dir );
1143 if (case_sensitive != -1) return case_sensitive;
1144 #endif
1145 return get_dir_case_sensitivity_stat( dir );
1149 /***********************************************************************
1150 * init_options
1152 * Initialize the show_dot_files options.
1154 static DWORD WINAPI init_options( RTL_RUN_ONCE *once, void *param, void **context )
1156 static const WCHAR WineW[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e',0};
1157 static const WCHAR ShowDotFilesW[] = {'S','h','o','w','D','o','t','F','i','l','e','s',0};
1158 char tmp[80];
1159 HANDLE root, hkey;
1160 DWORD dummy;
1161 OBJECT_ATTRIBUTES attr;
1162 UNICODE_STRING nameW;
1164 RtlOpenCurrentUser( KEY_ALL_ACCESS, &root );
1165 attr.Length = sizeof(attr);
1166 attr.RootDirectory = root;
1167 attr.ObjectName = &nameW;
1168 attr.Attributes = 0;
1169 attr.SecurityDescriptor = NULL;
1170 attr.SecurityQualityOfService = NULL;
1171 RtlInitUnicodeString( &nameW, WineW );
1173 /* @@ Wine registry key: HKCU\Software\Wine */
1174 if (!NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ))
1176 RtlInitUnicodeString( &nameW, ShowDotFilesW );
1177 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, tmp, sizeof(tmp), &dummy ))
1179 WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
1180 show_dot_files = IS_OPTION_TRUE( str[0] );
1182 NtClose( hkey );
1184 NtClose( root );
1186 /* a couple of directories that we don't want to return in directory searches */
1187 ignore_file( wine_get_config_dir() );
1188 ignore_file( "/dev" );
1189 ignore_file( "/proc" );
1190 #ifdef linux
1191 ignore_file( "/sys" );
1192 #endif
1193 return TRUE;
1197 /***********************************************************************
1198 * DIR_is_hidden_file
1200 * Check if the specified file should be hidden based on its name and the show dot files option.
1202 BOOL DIR_is_hidden_file( const UNICODE_STRING *name )
1204 WCHAR *p, *end;
1206 RtlRunOnceExecuteOnce( &init_once, init_options, NULL, NULL );
1208 if (show_dot_files) return FALSE;
1210 end = p = name->Buffer + name->Length/sizeof(WCHAR);
1211 while (p > name->Buffer && IS_SEPARATOR(p[-1])) p--;
1212 while (p > name->Buffer && !IS_SEPARATOR(p[-1])) p--;
1213 if (p == end || *p != '.') return FALSE;
1214 /* make sure it isn't '.' or '..' */
1215 if (p + 1 == end) return FALSE;
1216 if (p[1] == '.' && p + 2 == end) return FALSE;
1217 return TRUE;
1221 /***********************************************************************
1222 * hash_short_file_name
1224 * Transform a Unix file name into a hashed DOS name. If the name is a valid
1225 * DOS name, it is converted to upper-case; otherwise it is replaced by a
1226 * hashed version that fits in 8.3 format.
1227 * 'buffer' must be at least 12 characters long.
1228 * Returns length of short name in bytes; short name is NOT null-terminated.
1230 static ULONG hash_short_file_name( const UNICODE_STRING *name, LPWSTR buffer )
1232 static const char hash_chars[32] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345";
1234 LPCWSTR p, ext, end = name->Buffer + name->Length / sizeof(WCHAR);
1235 LPWSTR dst;
1236 unsigned short hash;
1237 int i;
1239 /* Compute the hash code of the file name */
1240 /* If you know something about hash functions, feel free to */
1241 /* insert a better algorithm here... */
1242 if (!is_case_sensitive)
1244 for (p = name->Buffer, hash = 0xbeef; p < end - 1; p++)
1245 hash = (hash<<3) ^ (hash>>5) ^ tolowerW(*p) ^ (tolowerW(p[1]) << 8);
1246 hash = (hash<<3) ^ (hash>>5) ^ tolowerW(*p); /* Last character */
1248 else
1250 for (p = name->Buffer, hash = 0xbeef; p < end - 1; p++)
1251 hash = (hash << 3) ^ (hash >> 5) ^ *p ^ (p[1] << 8);
1252 hash = (hash << 3) ^ (hash >> 5) ^ *p; /* Last character */
1255 /* Find last dot for start of the extension */
1256 for (p = name->Buffer + 1, ext = NULL; p < end - 1; p++) if (*p == '.') ext = p;
1258 /* Copy first 4 chars, replacing invalid chars with '_' */
1259 for (i = 4, p = name->Buffer, dst = buffer; i > 0; i--, p++)
1261 if (p == end || p == ext) break;
1262 *dst++ = is_invalid_dos_char(*p) ? '_' : toupperW(*p);
1264 /* Pad to 5 chars with '~' */
1265 while (i-- >= 0) *dst++ = '~';
1267 /* Insert hash code converted to 3 ASCII chars */
1268 *dst++ = hash_chars[(hash >> 10) & 0x1f];
1269 *dst++ = hash_chars[(hash >> 5) & 0x1f];
1270 *dst++ = hash_chars[hash & 0x1f];
1272 /* Copy the first 3 chars of the extension (if any) */
1273 if (ext)
1275 *dst++ = '.';
1276 for (i = 3, ext++; (i > 0) && ext < end; i--, ext++)
1277 *dst++ = is_invalid_dos_char(*ext) ? '_' : toupperW(*ext);
1279 return dst - buffer;
1283 /***********************************************************************
1284 * match_filename
1286 * Check a long file name against a mask.
1288 * Tests (done in W95 DOS shell - case insensitive):
1289 * *.txt test1.test.txt *
1290 * *st1* test1.txt *
1291 * *.t??????.t* test1.ta.tornado.txt *
1292 * *tornado* test1.ta.tornado.txt *
1293 * t*t test1.ta.tornado.txt *
1294 * ?est* test1.txt *
1295 * ?est??? test1.txt -
1296 * *test1.txt* test1.txt *
1297 * h?l?o*t.dat hellothisisatest.dat *
1299 static BOOLEAN match_filename( const UNICODE_STRING *name_str, const UNICODE_STRING *mask_str )
1301 BOOL mismatch;
1302 const WCHAR *name = name_str->Buffer;
1303 const WCHAR *mask = mask_str->Buffer;
1304 const WCHAR *name_end = name + name_str->Length / sizeof(WCHAR);
1305 const WCHAR *mask_end = mask + mask_str->Length / sizeof(WCHAR);
1306 const WCHAR *lastjoker = NULL;
1307 const WCHAR *next_to_retry = NULL;
1309 TRACE("(%s, %s)\n", debugstr_us(name_str), debugstr_us(mask_str));
1311 while (name < name_end && mask < mask_end)
1313 switch(*mask)
1315 case '*':
1316 mask++;
1317 while (mask < mask_end && *mask == '*') mask++; /* Skip consecutive '*' */
1318 if (mask == mask_end) return TRUE; /* end of mask is all '*', so match */
1319 lastjoker = mask;
1321 /* skip to the next match after the joker(s) */
1322 if (is_case_sensitive)
1323 while (name < name_end && (*name != *mask)) name++;
1324 else
1325 while (name < name_end && (toupperW(*name) != toupperW(*mask))) name++;
1326 next_to_retry = name;
1327 break;
1328 case '?':
1329 mask++;
1330 name++;
1331 break;
1332 default:
1333 if (is_case_sensitive) mismatch = (*mask != *name);
1334 else mismatch = (toupperW(*mask) != toupperW(*name));
1336 if (!mismatch)
1338 mask++;
1339 name++;
1340 if (mask == mask_end)
1342 if (name == name_end) return TRUE;
1343 if (lastjoker) mask = lastjoker;
1346 else /* mismatch ! */
1348 if (lastjoker) /* we had an '*', so we can try unlimitedly */
1350 mask = lastjoker;
1352 /* this scan sequence was a mismatch, so restart
1353 * 1 char after the first char we checked last time */
1354 next_to_retry++;
1355 name = next_to_retry;
1357 else return FALSE; /* bad luck */
1359 break;
1362 while (mask < mask_end && ((*mask == '.') || (*mask == '*')))
1363 mask++; /* Ignore trailing '.' or '*' in mask */
1364 return (name == name_end && mask == mask_end);
1368 /***********************************************************************
1369 * append_entry
1371 * helper for NtQueryDirectoryFile
1373 static union file_directory_info *append_entry( void *info_ptr, IO_STATUS_BLOCK *io, ULONG max_length,
1374 const char *long_name, const char *short_name,
1375 const UNICODE_STRING *mask, FILE_INFORMATION_CLASS class )
1377 union file_directory_info *info;
1378 int i, long_len, short_len, total_len;
1379 struct stat st;
1380 WCHAR long_nameW[MAX_DIR_ENTRY_LEN];
1381 WCHAR short_nameW[12];
1382 WCHAR *filename;
1383 UNICODE_STRING str;
1384 ULONG attributes = 0;
1386 io->u.Status = STATUS_SUCCESS;
1387 long_len = ntdll_umbstowcs( 0, long_name, strlen(long_name), long_nameW, MAX_DIR_ENTRY_LEN );
1388 if (long_len == -1) return NULL;
1390 str.Buffer = long_nameW;
1391 str.Length = long_len * sizeof(WCHAR);
1392 str.MaximumLength = sizeof(long_nameW);
1394 if (short_name)
1396 short_len = ntdll_umbstowcs( 0, short_name, strlen(short_name),
1397 short_nameW, sizeof(short_nameW) / sizeof(WCHAR) );
1398 if (short_len == -1) short_len = sizeof(short_nameW) / sizeof(WCHAR);
1400 else /* generate a short name if necessary */
1402 BOOLEAN spaces;
1404 short_len = 0;
1405 if (!RtlIsNameLegalDOS8Dot3( &str, NULL, &spaces ) || spaces)
1406 short_len = hash_short_file_name( &str, short_nameW );
1409 TRACE( "long %s short %s mask %s\n",
1410 debugstr_us(&str), debugstr_wn(short_nameW, short_len), debugstr_us(mask) );
1412 if (mask && !match_filename( &str, mask ))
1414 if (!short_len) return NULL; /* no short name to match */
1415 str.Buffer = short_nameW;
1416 str.Length = short_len * sizeof(WCHAR);
1417 str.MaximumLength = sizeof(short_nameW);
1418 if (!match_filename( &str, mask )) return NULL;
1421 if (lstat( long_name, &st ) == -1) return NULL;
1422 if (S_ISLNK( st.st_mode ))
1424 if (stat( long_name, &st ) == -1) return NULL;
1425 if (S_ISDIR( st.st_mode )) attributes |= FILE_ATTRIBUTE_REPARSE_POINT;
1427 if (is_ignored_file( &st ))
1429 TRACE( "ignoring file %s\n", long_name );
1430 return NULL;
1432 if (!show_dot_files && long_name[0] == '.' && long_name[1] && (long_name[1] != '.' || long_name[2]))
1433 attributes |= FILE_ATTRIBUTE_HIDDEN;
1435 total_len = dir_info_size( class, long_len );
1436 if (io->Information + total_len > max_length)
1438 total_len = max_length - io->Information;
1439 io->u.Status = STATUS_BUFFER_OVERFLOW;
1441 info = (union file_directory_info *)((char *)info_ptr + io->Information);
1442 if (st.st_dev != curdir.dev) st.st_ino = 0; /* ignore inode if on a different device */
1443 /* all the structures start with a FileDirectoryInformation layout */
1444 fill_stat_info( &st, info, class );
1445 info->dir.NextEntryOffset = total_len;
1446 info->dir.FileIndex = 0; /* NTFS always has 0 here, so let's not bother with it */
1447 info->dir.FileAttributes |= attributes;
1449 switch (class)
1451 case FileDirectoryInformation:
1452 info->dir.FileNameLength = long_len * sizeof(WCHAR);
1453 filename = info->dir.FileName;
1454 break;
1456 case FileFullDirectoryInformation:
1457 info->full.EaSize = 0; /* FIXME */
1458 info->full.FileNameLength = long_len * sizeof(WCHAR);
1459 filename = info->full.FileName;
1460 break;
1462 case FileIdFullDirectoryInformation:
1463 info->id_full.EaSize = 0; /* FIXME */
1464 info->id_full.FileNameLength = long_len * sizeof(WCHAR);
1465 filename = info->id_full.FileName;
1466 break;
1468 case FileBothDirectoryInformation:
1469 info->both.EaSize = 0; /* FIXME */
1470 info->both.ShortNameLength = short_len * sizeof(WCHAR);
1471 for (i = 0; i < short_len; i++) info->both.ShortName[i] = toupperW(short_nameW[i]);
1472 info->both.FileNameLength = long_len * sizeof(WCHAR);
1473 filename = info->both.FileName;
1474 break;
1476 case FileIdBothDirectoryInformation:
1477 info->id_both.EaSize = 0; /* FIXME */
1478 info->id_both.ShortNameLength = short_len * sizeof(WCHAR);
1479 for (i = 0; i < short_len; i++) info->id_both.ShortName[i] = toupperW(short_nameW[i]);
1480 info->id_both.FileNameLength = long_len * sizeof(WCHAR);
1481 filename = info->id_both.FileName;
1482 break;
1484 default:
1485 assert(0);
1486 return NULL;
1488 memcpy( filename, long_nameW, long_len * sizeof(WCHAR) );
1489 io->Information += total_len;
1490 return info;
1494 #ifdef VFAT_IOCTL_READDIR_BOTH
1496 /***********************************************************************
1497 * start_vfat_ioctl
1499 * Wrapper for the VFAT ioctl to work around various kernel bugs.
1500 * dir_section must be held by caller.
1502 static KERNEL_DIRENT *start_vfat_ioctl( int fd )
1504 static KERNEL_DIRENT *de;
1505 int res;
1507 if (!de)
1509 SIZE_T size = 2 * sizeof(*de) + page_size;
1510 void *addr = NULL;
1512 if (NtAllocateVirtualMemory( GetCurrentProcess(), &addr, 1, &size, MEM_RESERVE, PAGE_READWRITE ))
1513 return NULL;
1514 /* commit only the size needed for the dir entries */
1515 /* this leaves an extra unaccessible page, which should make the kernel */
1516 /* fail with -EFAULT before it stomps all over our memory */
1517 de = addr;
1518 size = 2 * sizeof(*de);
1519 NtAllocateVirtualMemory( GetCurrentProcess(), &addr, 1, &size, MEM_COMMIT, PAGE_READWRITE );
1522 /* set d_reclen to 65535 to work around an AFS kernel bug */
1523 de[0].d_reclen = 65535;
1524 res = ioctl( fd, VFAT_IOCTL_READDIR_BOTH, (long)de );
1525 if (res == -1)
1527 if (errno != ENOENT) return NULL; /* VFAT ioctl probably not supported */
1528 de[0].d_reclen = 0; /* eof */
1530 else if (!res && de[0].d_reclen == 65535) return NULL; /* AFS bug */
1532 return de;
1536 /***********************************************************************
1537 * read_directory_vfat
1539 * Read a directory using the VFAT ioctl; helper for NtQueryDirectoryFile.
1541 static int read_directory_vfat( int fd, IO_STATUS_BLOCK *io, void *buffer, ULONG length,
1542 BOOLEAN single_entry, const UNICODE_STRING *mask,
1543 BOOLEAN restart_scan, FILE_INFORMATION_CLASS class )
1546 size_t len;
1547 KERNEL_DIRENT *de;
1548 union file_directory_info *info, *last_info = NULL;
1550 io->u.Status = STATUS_SUCCESS;
1552 if (restart_scan) lseek( fd, 0, SEEK_SET );
1554 if (length < max_dir_info_size(class)) /* we may have to return a partial entry here */
1556 off_t old_pos = lseek( fd, 0, SEEK_CUR );
1558 if (!(de = start_vfat_ioctl( fd ))) return -1; /* not supported */
1560 while (de[0].d_reclen)
1562 /* make sure names are null-terminated to work around an x86-64 kernel bug */
1563 len = min(de[0].d_reclen, sizeof(de[0].d_name) - 1 );
1564 de[0].d_name[len] = 0;
1565 len = min(de[1].d_reclen, sizeof(de[1].d_name) - 1 );
1566 de[1].d_name[len] = 0;
1568 if (de[1].d_name[0])
1569 info = append_entry( buffer, io, length, de[1].d_name, de[0].d_name, mask, class );
1570 else
1571 info = append_entry( buffer, io, length, de[0].d_name, NULL, mask, class );
1572 if (info)
1574 last_info = info;
1575 if (io->u.Status == STATUS_BUFFER_OVERFLOW)
1576 lseek( fd, old_pos, SEEK_SET ); /* restore pos to previous entry */
1577 break;
1579 old_pos = lseek( fd, 0, SEEK_CUR );
1580 if (ioctl( fd, VFAT_IOCTL_READDIR_BOTH, (long)de ) == -1) break;
1583 else /* we'll only return full entries, no need to worry about overflow */
1585 if (!(de = start_vfat_ioctl( fd ))) return -1; /* not supported */
1587 while (de[0].d_reclen)
1589 /* make sure names are null-terminated to work around an x86-64 kernel bug */
1590 len = min(de[0].d_reclen, sizeof(de[0].d_name) - 1 );
1591 de[0].d_name[len] = 0;
1592 len = min(de[1].d_reclen, sizeof(de[1].d_name) - 1 );
1593 de[1].d_name[len] = 0;
1595 if (de[1].d_name[0])
1596 info = append_entry( buffer, io, length, de[1].d_name, de[0].d_name, mask, class );
1597 else
1598 info = append_entry( buffer, io, length, de[0].d_name, NULL, mask, class );
1599 if (info)
1601 last_info = info;
1602 if (single_entry) break;
1603 /* check if we still have enough space for the largest possible entry */
1604 if (io->Information + max_dir_info_size(class) > length) break;
1606 if (ioctl( fd, VFAT_IOCTL_READDIR_BOTH, (long)de ) == -1) break;
1610 if (last_info) last_info->next = 0;
1611 else io->u.Status = restart_scan ? STATUS_NO_SUCH_FILE : STATUS_NO_MORE_FILES;
1612 return 0;
1614 #endif /* VFAT_IOCTL_READDIR_BOTH */
1617 #ifdef USE_GETDENTS
1618 /***********************************************************************
1619 * read_first_dent_name
1621 * reads name of first or second dentry (if they have inodes).
1623 static char *read_first_dent_name( int which, int fd, off_t second_offs, KERNEL_DIRENT64 *de_first_two,
1624 char *buffer, size_t size, BOOL *buffer_changed )
1626 KERNEL_DIRENT64 *de;
1627 int res;
1629 de = de_first_two;
1630 if (de != NULL)
1632 if (which == 1)
1633 de = (KERNEL_DIRENT64 *)((char *)de + de->d_reclen);
1635 return de->d_ino ? de->d_name : NULL;
1638 *buffer_changed = TRUE;
1639 lseek( fd, which == 1 ? second_offs : 0, SEEK_SET );
1640 res = getdents64( fd, buffer, size );
1641 if (res <= 0)
1642 return NULL;
1644 de = (KERNEL_DIRENT64 *)buffer;
1645 return de->d_ino ? de->d_name : NULL;
1648 /***********************************************************************
1649 * read_directory_getdents
1651 * Read a directory using the Linux getdents64 system call; helper for NtQueryDirectoryFile.
1653 static int read_directory_getdents( int fd, IO_STATUS_BLOCK *io, void *buffer, ULONG length,
1654 BOOLEAN single_entry, const UNICODE_STRING *mask,
1655 BOOLEAN restart_scan, FILE_INFORMATION_CLASS class )
1657 static off_t second_entry_pos;
1658 static struct file_identity last_dir_id;
1659 off_t old_pos = 0, next_pos;
1660 size_t size = length;
1661 char *data, local_buffer[8192];
1662 KERNEL_DIRENT64 *de, *de_first_two = NULL;
1663 union file_directory_info *info, *last_info = NULL;
1664 const char *filename;
1665 BOOL data_buffer_changed;
1666 int res, swap_to;
1668 if (size <= sizeof(local_buffer) || !(data = RtlAllocateHeap( GetProcessHeap(), 0, size )))
1670 size = sizeof(local_buffer);
1671 data = local_buffer;
1674 if (restart_scan) lseek( fd, 0, SEEK_SET );
1675 else
1677 old_pos = lseek( fd, 0, SEEK_CUR );
1678 if (old_pos == -1)
1680 io->u.Status = (errno == ENOENT) ? STATUS_NO_MORE_FILES : FILE_GetNtStatus();
1681 res = 0;
1682 goto done;
1686 io->u.Status = STATUS_SUCCESS;
1687 de = (KERNEL_DIRENT64 *)data;
1689 /* if old_pos is not 0 we don't know how many entries have been returned already,
1690 * so maintain second_entry_pos to know when to return '..' */
1691 if (old_pos != 0 && (last_dir_id.dev != curdir.dev || last_dir_id.ino != curdir.ino))
1693 lseek( fd, 0, SEEK_SET );
1694 res = getdents64( fd, data, size );
1695 if (res > 0)
1697 second_entry_pos = de->d_off;
1698 last_dir_id = curdir;
1700 lseek( fd, old_pos, SEEK_SET );
1703 res = getdents64( fd, data, size );
1704 if (res == -1)
1706 if (errno != ENOSYS)
1708 io->u.Status = FILE_GetNtStatus();
1709 res = 0;
1711 goto done;
1714 if (old_pos == 0 && res > 0)
1716 second_entry_pos = de->d_off;
1717 last_dir_id = curdir;
1718 if (res > de->d_reclen)
1719 de_first_two = de;
1722 while (res > 0)
1724 res -= de->d_reclen;
1725 next_pos = de->d_off;
1726 filename = NULL;
1728 /* we must return first 2 entries as "." and "..", but getdents64()
1729 * can return them anywhere, so swap first entries with "." and ".." */
1730 if (old_pos == 0)
1731 filename = ".";
1732 else if (old_pos == second_entry_pos)
1733 filename = "..";
1734 else if (!strcmp( de->d_name, "." ) || !strcmp( de->d_name, ".." ))
1736 swap_to = !strcmp( de->d_name, "." ) ? 0 : 1;
1737 data_buffer_changed = FALSE;
1739 filename = read_first_dent_name( swap_to, fd, second_entry_pos, de_first_two,
1740 data, size, &data_buffer_changed );
1741 if (filename != NULL && (!strcmp( filename, "." ) || !strcmp( filename, ".." )))
1742 filename = read_first_dent_name( swap_to ^ 1, fd, second_entry_pos, NULL,
1743 data, size, &data_buffer_changed );
1744 if (data_buffer_changed)
1746 lseek( fd, next_pos, SEEK_SET );
1747 res = 0;
1750 else if (de->d_ino)
1751 filename = de->d_name;
1753 if (filename && (info = append_entry( buffer, io, length, filename, NULL, mask, class )))
1755 last_info = info;
1756 if (io->u.Status == STATUS_BUFFER_OVERFLOW)
1758 lseek( fd, old_pos, SEEK_SET ); /* restore pos to previous entry */
1759 break;
1761 /* check if we still have enough space for the largest possible entry */
1762 if (single_entry || io->Information + max_dir_info_size(class) > length)
1764 if (res > 0) lseek( fd, next_pos, SEEK_SET ); /* set pos to next entry */
1765 break;
1768 old_pos = next_pos;
1769 /* move on to the next entry */
1770 if (res > 0) de = (KERNEL_DIRENT64 *)((char *)de + de->d_reclen);
1771 else
1773 res = getdents64( fd, data, size );
1774 de = (KERNEL_DIRENT64 *)data;
1775 de_first_two = NULL;
1779 if (last_info) last_info->next = 0;
1780 else io->u.Status = restart_scan ? STATUS_NO_SUCH_FILE : STATUS_NO_MORE_FILES;
1781 res = 0;
1782 done:
1783 if (data != local_buffer) RtlFreeHeap( GetProcessHeap(), 0, data );
1784 return res;
1787 #elif defined HAVE_GETDIRENTRIES
1789 #ifdef _DARWIN_FEATURE_64_BIT_INODE
1791 /* Darwin doesn't provide a version of getdirentries with support for 64-bit
1792 * inodes. When 64-bit inodes are enabled, the getdirentries symbol is mapped
1793 * to _getdirentries_is_not_available_when_64_bit_inodes_are_in_effect so that
1794 * we get link errors if we try to use it. We still need getdirentries, but we
1795 * don't need it to support 64-bit inodes. So, we use the legacy getdirentries
1796 * with 32-bit inodes. We have to be careful to use a corresponding dirent
1797 * structure, too.
1799 int darwin_legacy_getdirentries(int, char *, int, long *) __asm("_getdirentries");
1800 #define getdirentries darwin_legacy_getdirentries
1802 struct darwin_legacy_dirent {
1803 __uint32_t d_ino;
1804 __uint16_t d_reclen;
1805 __uint8_t d_type;
1806 __uint8_t d_namlen;
1807 char d_name[__DARWIN_MAXNAMLEN + 1];
1809 #define dirent darwin_legacy_dirent
1811 #endif
1813 /***********************************************************************
1814 * wine_getdirentries
1816 * Wrapper for the BSD getdirentries system call to fix a bug in the
1817 * Mac OS X version. For some file systems (at least Apple Filing
1818 * Protocol a.k.a. AFP), getdirentries resets the file position to 0
1819 * when it's about to return 0 (no more entries). So, a subsequent
1820 * getdirentries call starts over at the beginning again, causing an
1821 * infinite loop.
1823 static inline int wine_getdirentries(int fd, char *buf, int nbytes, long *basep)
1825 int res = getdirentries(fd, buf, nbytes, basep);
1826 #ifdef __APPLE__
1827 if (res == 0)
1828 lseek(fd, *basep, SEEK_SET);
1829 #endif
1830 return res;
1833 static inline int dir_reclen(struct dirent *de)
1835 #ifdef HAVE_STRUCT_DIRENT_D_RECLEN
1836 return de->d_reclen;
1837 #else
1838 return _DIRENT_RECLEN(de->d_namlen);
1839 #endif
1842 /***********************************************************************
1843 * read_directory_getdirentries
1845 * Read a directory using the BSD getdirentries system call; helper for NtQueryDirectoryFile.
1847 static int read_directory_getdirentries( int fd, IO_STATUS_BLOCK *io, void *buffer, ULONG length,
1848 BOOLEAN single_entry, const UNICODE_STRING *mask,
1849 BOOLEAN restart_scan, FILE_INFORMATION_CLASS class )
1851 long restart_pos;
1852 ULONG_PTR restart_info_pos = 0;
1853 size_t size, initial_size = length;
1854 int res, fake_dot_dot = 1;
1855 char *data, local_buffer[8192];
1856 struct dirent *de;
1857 union file_directory_info *info, *last_info = NULL, *restart_last_info = NULL;
1859 size = initial_size;
1860 data = local_buffer;
1861 if (size > sizeof(local_buffer) && !(data = RtlAllocateHeap( GetProcessHeap(), 0, size )))
1863 io->u.Status = STATUS_NO_MEMORY;
1864 return io->u.Status;
1867 if (restart_scan) lseek( fd, 0, SEEK_SET );
1869 io->u.Status = STATUS_SUCCESS;
1871 /* FIXME: should make sure size is larger than filesystem block size */
1872 res = wine_getdirentries( fd, data, size, &restart_pos );
1873 if (res == -1)
1875 io->u.Status = FILE_GetNtStatus();
1876 res = 0;
1877 goto done;
1880 de = (struct dirent *)data;
1882 if (restart_scan)
1884 /* check if we got . and .. from getdirentries */
1885 if (res > 0)
1887 if (!strcmp( de->d_name, "." ) && res > dir_reclen(de))
1889 struct dirent *next_de = (struct dirent *)(data + dir_reclen(de));
1890 if (!strcmp( next_de->d_name, ".." )) fake_dot_dot = 0;
1893 /* make sure we have enough room for both entries */
1894 if (fake_dot_dot)
1896 const ULONG min_info_size = dir_info_size( class, 1 ) + dir_info_size( class, 2 );
1897 if (length < min_info_size || single_entry)
1899 FIXME( "not enough room %u/%u for fake . and .. entries\n", length, single_entry );
1900 fake_dot_dot = 0;
1904 if (fake_dot_dot)
1906 if ((info = append_entry( buffer, io, length, ".", NULL, mask, class )))
1907 last_info = info;
1908 if ((info = append_entry( buffer, io, length, "..", NULL, mask, class )))
1909 last_info = info;
1911 restart_last_info = last_info;
1912 restart_info_pos = io->Information;
1914 /* check if we still have enough space for the largest possible entry */
1915 if (last_info && io->Information + max_dir_info_size(class) > length)
1917 lseek( fd, 0, SEEK_SET ); /* reset pos to first entry */
1918 res = 0;
1923 while (res > 0)
1925 res -= dir_reclen(de);
1926 if (de->d_fileno &&
1927 !(fake_dot_dot && (!strcmp( de->d_name, "." ) || !strcmp( de->d_name, ".." ))) &&
1928 ((info = append_entry( buffer, io, length, de->d_name, NULL, mask, class ))))
1930 last_info = info;
1931 if (io->u.Status == STATUS_BUFFER_OVERFLOW)
1933 lseek( fd, (unsigned long)restart_pos, SEEK_SET );
1934 if (restart_info_pos) /* if we have a complete read already, return it */
1936 io->u.Status = STATUS_SUCCESS;
1937 io->Information = restart_info_pos;
1938 last_info = restart_last_info;
1939 break;
1941 /* otherwise restart from the start with a smaller size */
1942 size = (char *)de - data;
1943 if (!size) break;
1944 io->Information = 0;
1945 last_info = NULL;
1946 goto restart;
1948 if (!has_wildcard( mask )) break;
1949 /* if we have to return but the buffer contains more data, restart with a smaller size */
1950 if (res > 0 && (single_entry || io->Information + max_dir_info_size(class) > length))
1952 lseek( fd, (unsigned long)restart_pos, SEEK_SET );
1953 size = (char *)de + dir_reclen(de) - data;
1954 io->Information = restart_info_pos;
1955 last_info = restart_last_info;
1956 goto restart;
1959 /* move on to the next entry */
1960 if (res > 0)
1962 de = (struct dirent *)((char *)de + dir_reclen(de));
1963 continue;
1965 if (size < initial_size) break; /* already restarted once, give up now */
1966 restart_last_info = last_info;
1967 restart_info_pos = io->Information;
1968 restart:
1969 res = wine_getdirentries( fd, data, size, &restart_pos );
1970 de = (struct dirent *)data;
1973 if (last_info) last_info->next = 0;
1974 else io->u.Status = restart_scan ? STATUS_NO_SUCH_FILE : STATUS_NO_MORE_FILES;
1975 res = 0;
1976 done:
1977 if (data != local_buffer) RtlFreeHeap( GetProcessHeap(), 0, data );
1978 return res;
1981 #ifdef _DARWIN_FEATURE_64_BIT_INODE
1982 #undef getdirentries
1983 #undef dirent
1984 #endif
1986 #endif /* HAVE_GETDIRENTRIES */
1989 /***********************************************************************
1990 * read_directory_readdir
1992 * Read a directory using the POSIX readdir interface; helper for NtQueryDirectoryFile.
1994 static void read_directory_readdir( int fd, IO_STATUS_BLOCK *io, void *buffer, ULONG length,
1995 BOOLEAN single_entry, const UNICODE_STRING *mask,
1996 BOOLEAN restart_scan, FILE_INFORMATION_CLASS class )
1998 DIR *dir;
1999 off_t i, old_pos = 0;
2000 struct dirent *de;
2001 union file_directory_info *info, *last_info = NULL;
2003 if (!(dir = opendir( "." )))
2005 io->u.Status = FILE_GetNtStatus();
2006 return;
2009 if (!restart_scan)
2011 old_pos = lseek( fd, 0, SEEK_CUR );
2012 /* skip the right number of entries */
2013 for (i = 0; i < old_pos - 2; i++)
2015 if (!readdir( dir ))
2017 closedir( dir );
2018 io->u.Status = STATUS_NO_MORE_FILES;
2019 return;
2023 io->u.Status = STATUS_SUCCESS;
2025 for (;;)
2027 if (old_pos == 0)
2028 info = append_entry( buffer, io, length, ".", NULL, mask, class );
2029 else if (old_pos == 1)
2030 info = append_entry( buffer, io, length, "..", NULL, mask, class );
2031 else if ((de = readdir( dir )))
2033 if (strcmp( de->d_name, "." ) && strcmp( de->d_name, ".." ))
2034 info = append_entry( buffer, io, length, de->d_name, NULL, mask, class );
2035 else
2036 info = NULL;
2038 else
2039 break;
2040 old_pos++;
2041 if (info)
2043 last_info = info;
2044 if (io->u.Status == STATUS_BUFFER_OVERFLOW)
2046 old_pos--; /* restore pos to previous entry */
2047 break;
2049 if (single_entry) break;
2050 /* check if we still have enough space for the largest possible entry */
2051 if (io->Information + max_dir_info_size(class) > length) break;
2055 lseek( fd, old_pos, SEEK_SET ); /* store dir offset as filepos for fd */
2056 closedir( dir );
2058 if (last_info) last_info->next = 0;
2059 else io->u.Status = restart_scan ? STATUS_NO_SUCH_FILE : STATUS_NO_MORE_FILES;
2062 /***********************************************************************
2063 * read_directory_stat
2065 * Read a single file from a directory by determining whether the file
2066 * identified by mask exists using stat.
2068 static int read_directory_stat( int fd, IO_STATUS_BLOCK *io, void *buffer, ULONG length,
2069 BOOLEAN single_entry, const UNICODE_STRING *mask,
2070 BOOLEAN restart_scan, FILE_INFORMATION_CLASS class )
2072 int unix_len, ret, used_default;
2073 char *unix_name;
2074 struct stat st;
2076 TRACE("trying optimisation for file %s\n", debugstr_us( mask ));
2078 unix_len = ntdll_wcstoumbs( 0, mask->Buffer, mask->Length / sizeof(WCHAR), NULL, 0, NULL, NULL );
2079 if (!(unix_name = RtlAllocateHeap( GetProcessHeap(), 0, unix_len + 1)))
2081 io->u.Status = STATUS_NO_MEMORY;
2082 return 0;
2084 ret = ntdll_wcstoumbs( 0, mask->Buffer, mask->Length / sizeof(WCHAR), unix_name, unix_len,
2085 NULL, &used_default );
2086 if (ret > 0 && !used_default)
2088 unix_name[ret] = 0;
2089 if (restart_scan)
2091 lseek( fd, 0, SEEK_SET );
2093 else if (lseek( fd, 0, SEEK_CUR ) != 0)
2095 io->u.Status = STATUS_NO_MORE_FILES;
2096 ret = 0;
2097 goto done;
2100 ret = stat( unix_name, &st );
2101 if (!ret)
2103 union file_directory_info *info = append_entry( buffer, io, length, unix_name, NULL, NULL, class );
2104 if (info)
2106 info->next = 0;
2107 if (io->u.Status != STATUS_BUFFER_OVERFLOW) lseek( fd, 1, SEEK_CUR );
2109 else io->u.Status = STATUS_NO_MORE_FILES;
2112 else ret = -1;
2114 done:
2115 RtlFreeHeap( GetProcessHeap(), 0, unix_name );
2117 TRACE("returning %d\n", ret);
2119 return ret;
2123 /******************************************************************************
2124 * NtQueryDirectoryFile [NTDLL.@]
2125 * ZwQueryDirectoryFile [NTDLL.@]
2127 NTSTATUS WINAPI NtQueryDirectoryFile( HANDLE handle, HANDLE event,
2128 PIO_APC_ROUTINE apc_routine, PVOID apc_context,
2129 PIO_STATUS_BLOCK io,
2130 PVOID buffer, ULONG length,
2131 FILE_INFORMATION_CLASS info_class,
2132 BOOLEAN single_entry,
2133 PUNICODE_STRING mask,
2134 BOOLEAN restart_scan )
2136 int cwd, fd, needs_close;
2138 TRACE("(%p %p %p %p %p %p 0x%08x 0x%08x 0x%08x %s 0x%08x\n",
2139 handle, event, apc_routine, apc_context, io, buffer,
2140 length, info_class, single_entry, debugstr_us(mask),
2141 restart_scan);
2143 if (event || apc_routine)
2145 FIXME( "Unsupported yet option\n" );
2146 return io->u.Status = STATUS_NOT_IMPLEMENTED;
2148 switch (info_class)
2150 case FileDirectoryInformation:
2151 case FileBothDirectoryInformation:
2152 case FileFullDirectoryInformation:
2153 case FileIdBothDirectoryInformation:
2154 case FileIdFullDirectoryInformation:
2155 if (length < dir_info_size( info_class, 1 )) return io->u.Status = STATUS_INFO_LENGTH_MISMATCH;
2156 if (!buffer) return io->u.Status = STATUS_ACCESS_VIOLATION;
2157 break;
2158 default:
2159 FIXME( "Unsupported file info class %d\n", info_class );
2160 return io->u.Status = STATUS_NOT_IMPLEMENTED;
2163 if ((io->u.Status = server_get_unix_fd( handle, FILE_LIST_DIRECTORY, &fd, &needs_close, NULL, NULL )) != STATUS_SUCCESS)
2164 return io->u.Status;
2166 io->Information = 0;
2168 RtlRunOnceExecuteOnce( &init_once, init_options, NULL, NULL );
2170 RtlEnterCriticalSection( &dir_section );
2172 cwd = open( ".", O_RDONLY );
2173 if (fchdir( fd ) != -1)
2175 struct stat st;
2176 fstat( fd, &st );
2177 curdir.dev = st.st_dev;
2178 curdir.ino = st.st_ino;
2179 #ifdef VFAT_IOCTL_READDIR_BOTH
2180 if ((read_directory_vfat( fd, io, buffer, length, single_entry,
2181 mask, restart_scan, info_class )) != -1) goto done;
2182 #endif
2183 if (!has_wildcard( mask ) &&
2184 read_directory_stat( fd, io, buffer, length, single_entry,
2185 mask, restart_scan, info_class ) != -1) goto done;
2186 #ifdef USE_GETDENTS
2187 if ((read_directory_getdents( fd, io, buffer, length, single_entry,
2188 mask, restart_scan, info_class )) != -1) goto done;
2189 #elif defined HAVE_GETDIRENTRIES
2190 if ((read_directory_getdirentries( fd, io, buffer, length, single_entry,
2191 mask, restart_scan, info_class )) != -1) goto done;
2192 #endif
2193 read_directory_readdir( fd, io, buffer, length, single_entry, mask, restart_scan, info_class );
2195 done:
2196 if (cwd == -1 || fchdir( cwd ) == -1) chdir( "/" );
2198 else io->u.Status = FILE_GetNtStatus();
2200 RtlLeaveCriticalSection( &dir_section );
2202 if (needs_close) close( fd );
2203 if (cwd != -1) close( cwd );
2204 TRACE( "=> %x (%ld)\n", io->u.Status, io->Information );
2205 return io->u.Status;
2209 /***********************************************************************
2210 * find_file_in_dir
2212 * Find a file in a directory the hard way, by doing a case-insensitive search.
2213 * The file found is appended to unix_name at pos.
2214 * There must be at least MAX_DIR_ENTRY_LEN+2 chars available at pos.
2216 static NTSTATUS find_file_in_dir( char *unix_name, int pos, const WCHAR *name, int length,
2217 BOOLEAN check_case, BOOLEAN *is_win_dir )
2219 WCHAR buffer[MAX_DIR_ENTRY_LEN];
2220 UNICODE_STRING str;
2221 BOOLEAN spaces, is_name_8_dot_3;
2222 DIR *dir;
2223 struct dirent *de;
2224 struct stat st;
2225 int ret, used_default;
2227 /* try a shortcut for this directory */
2229 unix_name[pos++] = '/';
2230 ret = ntdll_wcstoumbs( 0, name, length, unix_name + pos, MAX_DIR_ENTRY_LEN,
2231 NULL, &used_default );
2232 /* if we used the default char, the Unix name won't round trip properly back to Unicode */
2233 /* so it cannot match the file we are looking for */
2234 if (ret >= 0 && !used_default)
2236 unix_name[pos + ret] = 0;
2237 if (!stat( unix_name, &st ))
2239 if (is_win_dir) *is_win_dir = is_same_file( &windir, &st );
2240 return STATUS_SUCCESS;
2243 if (check_case) goto not_found; /* we want an exact match */
2245 if (pos > 1) unix_name[pos - 1] = 0;
2246 else unix_name[1] = 0; /* keep the initial slash */
2248 /* check if it fits in 8.3 so that we don't look for short names if we won't need them */
2250 str.Buffer = (WCHAR *)name;
2251 str.Length = length * sizeof(WCHAR);
2252 str.MaximumLength = str.Length;
2253 is_name_8_dot_3 = RtlIsNameLegalDOS8Dot3( &str, NULL, &spaces ) && !spaces;
2254 #ifndef VFAT_IOCTL_READDIR_BOTH
2255 is_name_8_dot_3 = is_name_8_dot_3 && length >= 8 && name[4] == '~';
2256 #endif
2258 if (!is_name_8_dot_3 && !get_dir_case_sensitivity( unix_name )) goto not_found;
2260 /* now look for it through the directory */
2262 #ifdef VFAT_IOCTL_READDIR_BOTH
2263 if (is_name_8_dot_3)
2265 int fd = open( unix_name, O_RDONLY | O_DIRECTORY );
2266 if (fd != -1)
2268 KERNEL_DIRENT *kde;
2270 RtlEnterCriticalSection( &dir_section );
2271 if ((kde = start_vfat_ioctl( fd )))
2273 unix_name[pos - 1] = '/';
2274 while (kde[0].d_reclen)
2276 /* make sure names are null-terminated to work around an x86-64 kernel bug */
2277 size_t len = min(kde[0].d_reclen, sizeof(kde[0].d_name) - 1 );
2278 kde[0].d_name[len] = 0;
2279 len = min(kde[1].d_reclen, sizeof(kde[1].d_name) - 1 );
2280 kde[1].d_name[len] = 0;
2282 if (kde[1].d_name[0])
2284 ret = ntdll_umbstowcs( 0, kde[1].d_name, strlen(kde[1].d_name),
2285 buffer, MAX_DIR_ENTRY_LEN );
2286 if (ret == length && !memicmpW( buffer, name, length))
2288 strcpy( unix_name + pos, kde[1].d_name );
2289 RtlLeaveCriticalSection( &dir_section );
2290 close( fd );
2291 goto success;
2294 ret = ntdll_umbstowcs( 0, kde[0].d_name, strlen(kde[0].d_name),
2295 buffer, MAX_DIR_ENTRY_LEN );
2296 if (ret == length && !memicmpW( buffer, name, length))
2298 strcpy( unix_name + pos,
2299 kde[1].d_name[0] ? kde[1].d_name : kde[0].d_name );
2300 RtlLeaveCriticalSection( &dir_section );
2301 close( fd );
2302 goto success;
2304 if (ioctl( fd, VFAT_IOCTL_READDIR_BOTH, (long)kde ) == -1)
2306 RtlLeaveCriticalSection( &dir_section );
2307 close( fd );
2308 goto not_found;
2312 RtlLeaveCriticalSection( &dir_section );
2313 close( fd );
2315 /* fall through to normal handling */
2317 #endif /* VFAT_IOCTL_READDIR_BOTH */
2319 if (!(dir = opendir( unix_name )))
2321 if (errno == ENOENT) return STATUS_OBJECT_PATH_NOT_FOUND;
2322 else return FILE_GetNtStatus();
2324 unix_name[pos - 1] = '/';
2325 str.Buffer = buffer;
2326 str.MaximumLength = sizeof(buffer);
2327 while ((de = readdir( dir )))
2329 ret = ntdll_umbstowcs( 0, de->d_name, strlen(de->d_name), buffer, MAX_DIR_ENTRY_LEN );
2330 if (ret == length && !memicmpW( buffer, name, length ))
2332 strcpy( unix_name + pos, de->d_name );
2333 closedir( dir );
2334 goto success;
2337 if (!is_name_8_dot_3) continue;
2339 str.Length = ret * sizeof(WCHAR);
2340 if (!RtlIsNameLegalDOS8Dot3( &str, NULL, &spaces ) || spaces)
2342 WCHAR short_nameW[12];
2343 ret = hash_short_file_name( &str, short_nameW );
2344 if (ret == length && !memicmpW( short_nameW, name, length ))
2346 strcpy( unix_name + pos, de->d_name );
2347 closedir( dir );
2348 goto success;
2352 closedir( dir );
2354 not_found:
2355 unix_name[pos - 1] = 0;
2356 return STATUS_OBJECT_PATH_NOT_FOUND;
2358 success:
2359 if (is_win_dir && !stat( unix_name, &st )) *is_win_dir = is_same_file( &windir, &st );
2360 return STATUS_SUCCESS;
2364 #ifndef _WIN64
2366 static const WCHAR catrootW[] = {'s','y','s','t','e','m','3','2','\\','c','a','t','r','o','o','t',0};
2367 static const WCHAR catroot2W[] = {'s','y','s','t','e','m','3','2','\\','c','a','t','r','o','o','t','2',0};
2368 static const WCHAR driversstoreW[] = {'s','y','s','t','e','m','3','2','\\','d','r','i','v','e','r','s','s','t','o','r','e',0};
2369 static const WCHAR driversetcW[] = {'s','y','s','t','e','m','3','2','\\','d','r','i','v','e','r','s','\\','e','t','c',0};
2370 static const WCHAR logfilesW[] = {'s','y','s','t','e','m','3','2','\\','l','o','g','f','i','l','e','s',0};
2371 static const WCHAR spoolW[] = {'s','y','s','t','e','m','3','2','\\','s','p','o','o','l',0};
2372 static const WCHAR system32W[] = {'s','y','s','t','e','m','3','2',0};
2373 static const WCHAR syswow64W[] = {'s','y','s','w','o','w','6','4',0};
2374 static const WCHAR sysnativeW[] = {'s','y','s','n','a','t','i','v','e',0};
2375 static const WCHAR regeditW[] = {'r','e','g','e','d','i','t','.','e','x','e',0};
2376 static const WCHAR wow_regeditW[] = {'s','y','s','w','o','w','6','4','\\','r','e','g','e','d','i','t','.','e','x','e',0};
2378 static struct
2380 const WCHAR *source;
2381 const WCHAR *dos_target;
2382 const char *unix_target;
2383 } redirects[] =
2385 { catrootW, NULL, NULL },
2386 { catroot2W, NULL, NULL },
2387 { driversstoreW, NULL, NULL },
2388 { driversetcW, NULL, NULL },
2389 { logfilesW, NULL, NULL },
2390 { spoolW, NULL, NULL },
2391 { system32W, syswow64W, NULL },
2392 { sysnativeW, system32W, NULL },
2393 { regeditW, wow_regeditW, NULL }
2396 static unsigned int nb_redirects;
2399 /***********************************************************************
2400 * get_redirect_target
2402 * Find the target unix name for a redirected dir.
2404 static const char *get_redirect_target( const char *windows_dir, const WCHAR *name )
2406 int used_default, len, pos, win_len = strlen( windows_dir );
2407 char *unix_name, *unix_target = NULL;
2408 NTSTATUS status;
2410 if (!(unix_name = RtlAllocateHeap( GetProcessHeap(), 0, win_len + MAX_DIR_ENTRY_LEN + 2 )))
2411 return NULL;
2412 memcpy( unix_name, windows_dir, win_len );
2413 pos = win_len;
2415 while (*name)
2417 const WCHAR *end, *next;
2419 for (end = name; *end; end++) if (IS_SEPARATOR(*end)) break;
2420 for (next = end; *next; next++) if (!IS_SEPARATOR(*next)) break;
2422 status = find_file_in_dir( unix_name, pos, name, end - name, FALSE, NULL );
2423 if (status == STATUS_OBJECT_PATH_NOT_FOUND && !*next) /* not finding last element is ok */
2425 len = ntdll_wcstoumbs( 0, name, end - name, unix_name + pos + 1,
2426 MAX_DIR_ENTRY_LEN - (pos - win_len), NULL, &used_default );
2427 if (len > 0 && !used_default)
2429 unix_name[pos] = '/';
2430 pos += len + 1;
2431 unix_name[pos] = 0;
2432 break;
2435 if (status) goto done;
2436 pos += strlen( unix_name + pos );
2437 name = next;
2440 if ((unix_target = RtlAllocateHeap( GetProcessHeap(), 0, pos - win_len )))
2441 memcpy( unix_target, unix_name + win_len + 1, pos - win_len );
2443 done:
2444 RtlFreeHeap( GetProcessHeap(), 0, unix_name );
2445 return unix_target;
2449 /***********************************************************************
2450 * init_redirects
2452 static void init_redirects(void)
2454 UNICODE_STRING nt_name;
2455 ANSI_STRING unix_name;
2456 NTSTATUS status;
2457 struct stat st;
2458 unsigned int i;
2460 if (!RtlDosPathNameToNtPathName_U( user_shared_data->NtSystemRoot, &nt_name, NULL, NULL ))
2462 ERR( "can't convert %s\n", debugstr_w(user_shared_data->NtSystemRoot) );
2463 return;
2465 status = wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN_IF, FALSE );
2466 RtlFreeUnicodeString( &nt_name );
2467 if (status)
2469 ERR( "cannot open %s (%x)\n", debugstr_w(user_shared_data->NtSystemRoot), status );
2470 return;
2472 if (!stat( unix_name.Buffer, &st ))
2474 windir.dev = st.st_dev;
2475 windir.ino = st.st_ino;
2476 nb_redirects = sizeof(redirects) / sizeof(redirects[0]);
2477 for (i = 0; i < nb_redirects; i++)
2479 if (!redirects[i].dos_target) continue;
2480 redirects[i].unix_target = get_redirect_target( unix_name.Buffer, redirects[i].dos_target );
2481 TRACE( "%s -> %s\n", debugstr_w(redirects[i].source), redirects[i].unix_target );
2484 RtlFreeAnsiString( &unix_name );
2489 /***********************************************************************
2490 * match_redirect
2492 * Check if path matches a redirect name. If yes, return matched length.
2494 static int match_redirect( const WCHAR *path, int len, const WCHAR *redir, BOOLEAN check_case )
2496 int i = 0;
2498 while (i < len && *redir)
2500 if (IS_SEPARATOR(path[i]))
2502 if (*redir++ != '\\') return 0;
2503 while (i < len && IS_SEPARATOR(path[i])) i++;
2504 continue; /* move on to next path component */
2506 else if (check_case)
2508 if (path[i] != *redir) return 0;
2510 else
2512 if (tolowerW(path[i]) != tolowerW(*redir)) return 0;
2514 i++;
2515 redir++;
2517 if (*redir) return 0;
2518 if (i < len && !IS_SEPARATOR(path[i])) return 0;
2519 while (i < len && IS_SEPARATOR(path[i])) i++;
2520 return i;
2524 /***********************************************************************
2525 * get_redirect_path
2527 * Retrieve the Unix path corresponding to a redirected path if any.
2529 static int get_redirect_path( char *unix_name, int pos, const WCHAR *name, int length, BOOLEAN check_case )
2531 unsigned int i;
2532 int len;
2534 for (i = 0; i < nb_redirects; i++)
2536 if ((len = match_redirect( name, length, redirects[i].source, check_case )))
2538 if (!redirects[i].unix_target) break;
2539 unix_name[pos++] = '/';
2540 strcpy( unix_name + pos, redirects[i].unix_target );
2541 return len;
2544 return 0;
2547 #else /* _WIN64 */
2549 /* there are no redirects on 64-bit */
2551 static const unsigned int nb_redirects = 0;
2553 static int get_redirect_path( char *unix_name, int pos, const WCHAR *name, int length, BOOLEAN check_case )
2555 return 0;
2558 #endif
2560 /***********************************************************************
2561 * DIR_init_windows_dir
2563 void DIR_init_windows_dir( const WCHAR *win, const WCHAR *sys )
2565 /* FIXME: should probably store paths as NT file names */
2567 RtlCreateUnicodeString( &system_dir, sys );
2569 #ifndef _WIN64
2570 if (is_wow64) init_redirects();
2571 #endif
2575 /******************************************************************************
2576 * get_dos_device
2578 * Get the Unix path of a DOS device.
2580 static NTSTATUS get_dos_device( const WCHAR *name, UINT name_len, ANSI_STRING *unix_name_ret )
2582 const char *config_dir = wine_get_config_dir();
2583 struct stat st;
2584 char *unix_name, *new_name, *dev;
2585 unsigned int i;
2586 int unix_len;
2588 /* make sure the device name is ASCII */
2589 for (i = 0; i < name_len; i++)
2590 if (name[i] <= 32 || name[i] >= 127) return STATUS_BAD_DEVICE_TYPE;
2592 unix_len = strlen(config_dir) + sizeof("/dosdevices/") + name_len + 1;
2594 if (!(unix_name = RtlAllocateHeap( GetProcessHeap(), 0, unix_len )))
2595 return STATUS_NO_MEMORY;
2597 strcpy( unix_name, config_dir );
2598 strcat( unix_name, "/dosdevices/" );
2599 dev = unix_name + strlen(unix_name);
2601 for (i = 0; i < name_len; i++) dev[i] = (char)tolowerW(name[i]);
2602 dev[i] = 0;
2604 /* special case for drive devices */
2605 if (name_len == 2 && dev[1] == ':')
2607 dev[i++] = ':';
2608 dev[i] = 0;
2611 for (;;)
2613 if (!stat( unix_name, &st ))
2615 TRACE( "%s -> %s\n", debugstr_wn(name,name_len), debugstr_a(unix_name) );
2616 unix_name_ret->Buffer = unix_name;
2617 unix_name_ret->Length = strlen(unix_name);
2618 unix_name_ret->MaximumLength = unix_len;
2619 return STATUS_SUCCESS;
2621 if (!dev) break;
2623 /* now try some defaults for it */
2624 if (!strcmp( dev, "aux" ))
2626 strcpy( dev, "com1" );
2627 continue;
2629 if (!strcmp( dev, "prn" ))
2631 strcpy( dev, "lpt1" );
2632 continue;
2634 if (!strcmp( dev, "nul" ))
2636 strcpy( unix_name, "/dev/null" );
2637 dev = NULL; /* last try */
2638 continue;
2641 new_name = NULL;
2642 if (dev[1] == ':' && dev[2] == ':') /* drive device */
2644 dev[2] = 0; /* remove last ':' to get the drive mount point symlink */
2645 new_name = get_default_drive_device( unix_name );
2647 else if (!strncmp( dev, "com", 3 )) new_name = get_default_com_device( atoi(dev + 3 ));
2648 else if (!strncmp( dev, "lpt", 3 )) new_name = get_default_lpt_device( atoi(dev + 3 ));
2650 if (!new_name) break;
2652 RtlFreeHeap( GetProcessHeap(), 0, unix_name );
2653 unix_name = new_name;
2654 unix_len = strlen(unix_name) + 1;
2655 dev = NULL; /* last try */
2657 RtlFreeHeap( GetProcessHeap(), 0, unix_name );
2658 return STATUS_BAD_DEVICE_TYPE;
2662 /* return the length of the DOS namespace prefix if any */
2663 static inline int get_dos_prefix_len( const UNICODE_STRING *name )
2665 static const WCHAR nt_prefixW[] = {'\\','?','?','\\'};
2666 static const WCHAR dosdev_prefixW[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\'};
2668 if (name->Length > sizeof(nt_prefixW) &&
2669 !memcmp( name->Buffer, nt_prefixW, sizeof(nt_prefixW) ))
2670 return sizeof(nt_prefixW) / sizeof(WCHAR);
2672 if (name->Length > sizeof(dosdev_prefixW) &&
2673 !memicmpW( name->Buffer, dosdev_prefixW, sizeof(dosdev_prefixW)/sizeof(WCHAR) ))
2674 return sizeof(dosdev_prefixW) / sizeof(WCHAR);
2676 return 0;
2680 /******************************************************************************
2681 * find_file_id
2683 * Recursively search directories from the dir queue for a given inode.
2685 static NTSTATUS find_file_id( ANSI_STRING *unix_name, ULONGLONG file_id, dev_t dev )
2687 unsigned int pos;
2688 DIR *dir;
2689 struct dirent *de;
2690 NTSTATUS status;
2691 struct stat st;
2693 while (!(status = next_dir_in_queue( unix_name->Buffer )))
2695 if (!(dir = opendir( unix_name->Buffer ))) continue;
2696 TRACE( "searching %s for %s\n", unix_name->Buffer, wine_dbgstr_longlong(file_id) );
2697 pos = strlen( unix_name->Buffer );
2698 if (pos + MAX_DIR_ENTRY_LEN >= unix_name->MaximumLength/sizeof(WCHAR))
2700 char *new = RtlReAllocateHeap( GetProcessHeap(), 0, unix_name->Buffer,
2701 unix_name->MaximumLength * 2 );
2702 if (!new)
2704 closedir( dir );
2705 return STATUS_NO_MEMORY;
2707 unix_name->MaximumLength *= 2;
2708 unix_name->Buffer = new;
2710 unix_name->Buffer[pos++] = '/';
2711 while ((de = readdir( dir )))
2713 if (!strcmp( de->d_name, "." ) || !strcmp( de->d_name, ".." )) continue;
2714 strcpy( unix_name->Buffer + pos, de->d_name );
2715 if (lstat( unix_name->Buffer, &st ) == -1) continue;
2716 if (st.st_dev != dev) continue;
2717 if (st.st_ino == file_id)
2719 closedir( dir );
2720 return STATUS_SUCCESS;
2722 if (!S_ISDIR( st.st_mode )) continue;
2723 if ((status = add_dir_to_queue( unix_name->Buffer )) != STATUS_SUCCESS)
2725 closedir( dir );
2726 return status;
2729 closedir( dir );
2731 return status;
2735 /******************************************************************************
2736 * file_id_to_unix_file_name
2738 * Lookup a file from its file id instead of its name.
2740 NTSTATUS file_id_to_unix_file_name( const OBJECT_ATTRIBUTES *attr, ANSI_STRING *unix_name )
2742 enum server_fd_type type;
2743 int old_cwd, root_fd, needs_close;
2744 NTSTATUS status;
2745 ULONGLONG file_id;
2746 struct stat st, root_st;
2748 if (attr->ObjectName->Length != sizeof(ULONGLONG)) return STATUS_OBJECT_PATH_SYNTAX_BAD;
2749 if (!attr->RootDirectory) return STATUS_INVALID_PARAMETER;
2750 memcpy( &file_id, attr->ObjectName->Buffer, sizeof(file_id) );
2752 unix_name->MaximumLength = 2 * MAX_DIR_ENTRY_LEN + 4;
2753 if (!(unix_name->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, unix_name->MaximumLength )))
2754 return STATUS_NO_MEMORY;
2755 strcpy( unix_name->Buffer, "." );
2757 if ((status = server_get_unix_fd( attr->RootDirectory, 0, &root_fd, &needs_close, &type, NULL )))
2758 goto done;
2760 if (type != FD_TYPE_DIR)
2762 status = STATUS_OBJECT_TYPE_MISMATCH;
2763 goto done;
2766 fstat( root_fd, &root_st );
2767 if (root_st.st_ino == file_id) /* shortcut for "." */
2769 status = STATUS_SUCCESS;
2770 goto done;
2773 RtlEnterCriticalSection( &dir_section );
2774 if ((old_cwd = open( ".", O_RDONLY )) != -1 && fchdir( root_fd ) != -1)
2776 /* shortcut for ".." */
2777 if (!stat( "..", &st ) && st.st_dev == root_st.st_dev && st.st_ino == file_id)
2779 strcpy( unix_name->Buffer, ".." );
2780 status = STATUS_SUCCESS;
2782 else
2784 status = add_dir_to_queue( "." );
2785 if (!status)
2786 status = find_file_id( unix_name, file_id, root_st.st_dev );
2787 if (!status) /* get rid of "./" prefix */
2788 memmove( unix_name->Buffer, unix_name->Buffer + 2, strlen(unix_name->Buffer) - 1 );
2789 flush_dir_queue();
2791 if (fchdir( old_cwd ) == -1) chdir( "/" );
2793 else status = FILE_GetNtStatus();
2794 RtlLeaveCriticalSection( &dir_section );
2795 if (old_cwd != -1) close( old_cwd );
2797 done:
2798 if (status == STATUS_SUCCESS)
2800 TRACE( "%s -> %s\n", wine_dbgstr_longlong(file_id), debugstr_a(unix_name->Buffer) );
2801 unix_name->Length = strlen( unix_name->Buffer );
2803 else
2805 TRACE( "%s not found in dir %p\n", wine_dbgstr_longlong(file_id), attr->RootDirectory );
2806 RtlFreeHeap( GetProcessHeap(), 0, unix_name->Buffer );
2808 if (needs_close) close( root_fd );
2809 return status;
2813 /******************************************************************************
2814 * lookup_unix_name
2816 * Helper for nt_to_unix_file_name
2818 static NTSTATUS lookup_unix_name( const WCHAR *name, int name_len, char **buffer, int unix_len, int pos,
2819 UINT disposition, BOOLEAN check_case )
2821 NTSTATUS status;
2822 int ret, used_default, len;
2823 struct stat st;
2824 char *unix_name = *buffer;
2825 const BOOL redirect = nb_redirects && ntdll_get_thread_data()->wow64_redir;
2827 /* try a shortcut first */
2829 ret = ntdll_wcstoumbs( 0, name, name_len, unix_name + pos, unix_len - pos - 1,
2830 NULL, &used_default );
2832 while (name_len && IS_SEPARATOR(*name))
2834 name++;
2835 name_len--;
2838 if (ret >= 0 && !used_default) /* if we used the default char the name didn't convert properly */
2840 char *p;
2841 unix_name[pos + ret] = 0;
2842 for (p = unix_name + pos ; *p; p++) if (*p == '\\') *p = '/';
2843 if (!redirect || (!strstr( unix_name, "/windows/") && strncmp( unix_name, "windows/", 8 )))
2845 if (!stat( unix_name, &st ))
2847 /* creation fails with STATUS_ACCESS_DENIED for the root of the drive */
2848 if (disposition == FILE_CREATE)
2849 return name_len ? STATUS_OBJECT_NAME_COLLISION : STATUS_ACCESS_DENIED;
2850 return STATUS_SUCCESS;
2855 if (!name_len) /* empty name -> drive root doesn't exist */
2856 return STATUS_OBJECT_PATH_NOT_FOUND;
2857 if (check_case && !redirect && (disposition == FILE_OPEN || disposition == FILE_OVERWRITE))
2858 return STATUS_OBJECT_NAME_NOT_FOUND;
2860 /* now do it component by component */
2862 while (name_len)
2864 const WCHAR *end, *next;
2865 BOOLEAN is_win_dir = FALSE;
2867 end = name;
2868 while (end < name + name_len && !IS_SEPARATOR(*end)) end++;
2869 next = end;
2870 while (next < name + name_len && IS_SEPARATOR(*next)) next++;
2871 name_len -= next - name;
2873 /* grow the buffer if needed */
2875 if (unix_len - pos < MAX_DIR_ENTRY_LEN + 2)
2877 char *new_name;
2878 unix_len += 2 * MAX_DIR_ENTRY_LEN;
2879 if (!(new_name = RtlReAllocateHeap( GetProcessHeap(), 0, unix_name, unix_len )))
2880 return STATUS_NO_MEMORY;
2881 unix_name = *buffer = new_name;
2884 status = find_file_in_dir( unix_name, pos, name, end - name,
2885 check_case, redirect ? &is_win_dir : NULL );
2887 /* if this is the last element, not finding it is not necessarily fatal */
2888 if (!name_len)
2890 if (status == STATUS_OBJECT_PATH_NOT_FOUND)
2892 status = STATUS_OBJECT_NAME_NOT_FOUND;
2893 if (disposition != FILE_OPEN && disposition != FILE_OVERWRITE)
2895 ret = ntdll_wcstoumbs( 0, name, end - name, unix_name + pos + 1,
2896 MAX_DIR_ENTRY_LEN, NULL, &used_default );
2897 if (ret > 0 && !used_default)
2899 unix_name[pos] = '/';
2900 unix_name[pos + 1 + ret] = 0;
2901 status = STATUS_NO_SUCH_FILE;
2902 break;
2906 else if (status == STATUS_SUCCESS && disposition == FILE_CREATE)
2908 status = STATUS_OBJECT_NAME_COLLISION;
2912 if (status != STATUS_SUCCESS) break;
2914 pos += strlen( unix_name + pos );
2915 name = next;
2917 if (is_win_dir && (len = get_redirect_path( unix_name, pos, name, name_len, check_case )))
2919 name += len;
2920 name_len -= len;
2921 pos += strlen( unix_name + pos );
2922 TRACE( "redirecting -> %s + %s\n", debugstr_a(unix_name), debugstr_w(name) );
2926 return status;
2930 /******************************************************************************
2931 * nt_to_unix_file_name_attr
2933 NTSTATUS nt_to_unix_file_name_attr( const OBJECT_ATTRIBUTES *attr, ANSI_STRING *unix_name_ret,
2934 UINT disposition )
2936 static const WCHAR invalid_charsW[] = { INVALID_NT_CHARS, 0 };
2937 enum server_fd_type type;
2938 int old_cwd, root_fd, needs_close;
2939 const WCHAR *name, *p;
2940 char *unix_name;
2941 int name_len, unix_len;
2942 NTSTATUS status;
2943 BOOLEAN check_case = !(attr->Attributes & OBJ_CASE_INSENSITIVE);
2945 if (!attr->RootDirectory) /* without root dir fall back to normal lookup */
2946 return wine_nt_to_unix_file_name( attr->ObjectName, unix_name_ret, disposition, check_case );
2948 name = attr->ObjectName->Buffer;
2949 name_len = attr->ObjectName->Length / sizeof(WCHAR);
2951 if (name_len && IS_SEPARATOR(name[0])) return STATUS_INVALID_PARAMETER;
2953 /* check for invalid characters */
2954 for (p = name; p < name + name_len; p++)
2955 if (*p < 32 || strchrW( invalid_charsW, *p )) return STATUS_OBJECT_NAME_INVALID;
2957 unix_len = ntdll_wcstoumbs( 0, name, name_len, NULL, 0, NULL, NULL );
2958 unix_len += MAX_DIR_ENTRY_LEN + 3;
2959 if (!(unix_name = RtlAllocateHeap( GetProcessHeap(), 0, unix_len )))
2960 return STATUS_NO_MEMORY;
2961 unix_name[0] = '.';
2963 if (!(status = server_get_unix_fd( attr->RootDirectory, 0, &root_fd, &needs_close, &type, NULL )))
2965 if (type != FD_TYPE_DIR)
2967 if (needs_close) close( root_fd );
2968 status = STATUS_BAD_DEVICE_TYPE;
2970 else
2972 RtlEnterCriticalSection( &dir_section );
2973 if ((old_cwd = open( ".", O_RDONLY )) != -1 && fchdir( root_fd ) != -1)
2975 status = lookup_unix_name( name, name_len, &unix_name, unix_len, 1,
2976 disposition, check_case );
2977 if (fchdir( old_cwd ) == -1) chdir( "/" );
2979 else status = FILE_GetNtStatus();
2980 RtlLeaveCriticalSection( &dir_section );
2981 if (old_cwd != -1) close( old_cwd );
2982 if (needs_close) close( root_fd );
2985 else if (status == STATUS_OBJECT_TYPE_MISMATCH) status = STATUS_BAD_DEVICE_TYPE;
2987 if (status == STATUS_SUCCESS || status == STATUS_NO_SUCH_FILE)
2989 TRACE( "%s -> %s\n", debugstr_us(attr->ObjectName), debugstr_a(unix_name) );
2990 unix_name_ret->Buffer = unix_name;
2991 unix_name_ret->Length = strlen(unix_name);
2992 unix_name_ret->MaximumLength = unix_len;
2994 else
2996 TRACE( "%s not found in %s\n", debugstr_w(name), unix_name );
2997 RtlFreeHeap( GetProcessHeap(), 0, unix_name );
2999 return status;
3003 /******************************************************************************
3004 * wine_nt_to_unix_file_name (NTDLL.@) Not a Windows API
3006 * Convert a file name from NT namespace to Unix namespace.
3008 * If disposition is not FILE_OPEN or FILE_OVERWRITE, the last path
3009 * element doesn't have to exist; in that case STATUS_NO_SUCH_FILE is
3010 * returned, but the unix name is still filled in properly.
3012 NTSTATUS CDECL wine_nt_to_unix_file_name( const UNICODE_STRING *nameW, ANSI_STRING *unix_name_ret,
3013 UINT disposition, BOOLEAN check_case )
3015 static const WCHAR unixW[] = {'u','n','i','x'};
3016 static const WCHAR invalid_charsW[] = { INVALID_NT_CHARS, 0 };
3018 NTSTATUS status = STATUS_SUCCESS;
3019 const char *config_dir = wine_get_config_dir();
3020 const WCHAR *name, *p;
3021 struct stat st;
3022 char *unix_name;
3023 int pos, ret, name_len, unix_len, prefix_len, used_default;
3024 WCHAR prefix[MAX_DIR_ENTRY_LEN];
3025 BOOLEAN is_unix = FALSE;
3027 name = nameW->Buffer;
3028 name_len = nameW->Length / sizeof(WCHAR);
3030 if (!name_len || !IS_SEPARATOR(name[0])) return STATUS_OBJECT_PATH_SYNTAX_BAD;
3032 if (!(pos = get_dos_prefix_len( nameW )))
3033 return STATUS_BAD_DEVICE_TYPE; /* no DOS prefix, assume NT native name */
3035 name += pos;
3036 name_len -= pos;
3038 /* check for sub-directory */
3039 for (pos = 0; pos < name_len; pos++)
3041 if (IS_SEPARATOR(name[pos])) break;
3042 if (name[pos] < 32 || strchrW( invalid_charsW, name[pos] ))
3043 return STATUS_OBJECT_NAME_INVALID;
3045 if (pos > MAX_DIR_ENTRY_LEN)
3046 return STATUS_OBJECT_NAME_INVALID;
3048 if (pos == name_len) /* no subdir, plain DOS device */
3049 return get_dos_device( name, name_len, unix_name_ret );
3051 for (prefix_len = 0; prefix_len < pos; prefix_len++)
3052 prefix[prefix_len] = tolowerW(name[prefix_len]);
3054 name += prefix_len;
3055 name_len -= prefix_len;
3057 /* check for invalid characters (all chars except 0 are valid for unix) */
3058 is_unix = (prefix_len == 4 && !memcmp( prefix, unixW, sizeof(unixW) ));
3059 if (is_unix)
3061 for (p = name; p < name + name_len; p++)
3062 if (!*p) return STATUS_OBJECT_NAME_INVALID;
3063 check_case = TRUE;
3065 else
3067 for (p = name; p < name + name_len; p++)
3068 if (*p < 32 || strchrW( invalid_charsW, *p )) return STATUS_OBJECT_NAME_INVALID;
3071 unix_len = ntdll_wcstoumbs( 0, prefix, prefix_len, NULL, 0, NULL, NULL );
3072 unix_len += ntdll_wcstoumbs( 0, name, name_len, NULL, 0, NULL, NULL );
3073 unix_len += MAX_DIR_ENTRY_LEN + 3;
3074 unix_len += strlen(config_dir) + sizeof("/dosdevices/");
3075 if (!(unix_name = RtlAllocateHeap( GetProcessHeap(), 0, unix_len )))
3076 return STATUS_NO_MEMORY;
3077 strcpy( unix_name, config_dir );
3078 strcat( unix_name, "/dosdevices/" );
3079 pos = strlen(unix_name);
3081 ret = ntdll_wcstoumbs( 0, prefix, prefix_len, unix_name + pos, unix_len - pos - 1,
3082 NULL, &used_default );
3083 if (!ret || used_default)
3085 RtlFreeHeap( GetProcessHeap(), 0, unix_name );
3086 return STATUS_OBJECT_NAME_INVALID;
3088 pos += ret;
3090 /* check if prefix exists (except for DOS drives to avoid extra stat calls) */
3092 if (prefix_len != 2 || prefix[1] != ':')
3094 unix_name[pos] = 0;
3095 if (lstat( unix_name, &st ) == -1 && errno == ENOENT)
3097 if (!is_unix)
3099 RtlFreeHeap( GetProcessHeap(), 0, unix_name );
3100 return STATUS_BAD_DEVICE_TYPE;
3102 pos = 0; /* fall back to unix root */
3106 status = lookup_unix_name( name, name_len, &unix_name, unix_len, pos, disposition, check_case );
3107 if (status == STATUS_SUCCESS || status == STATUS_NO_SUCH_FILE)
3109 TRACE( "%s -> %s\n", debugstr_us(nameW), debugstr_a(unix_name) );
3110 unix_name_ret->Buffer = unix_name;
3111 unix_name_ret->Length = strlen(unix_name);
3112 unix_name_ret->MaximumLength = unix_len;
3114 else
3116 TRACE( "%s not found in %s\n", debugstr_w(name), unix_name );
3117 RtlFreeHeap( GetProcessHeap(), 0, unix_name );
3119 return status;
3123 /******************************************************************
3124 * RtlWow64EnableFsRedirection (NTDLL.@)
3126 NTSTATUS WINAPI RtlWow64EnableFsRedirection( BOOLEAN enable )
3128 if (!is_wow64) return STATUS_NOT_IMPLEMENTED;
3129 ntdll_get_thread_data()->wow64_redir = enable;
3130 return STATUS_SUCCESS;
3134 /******************************************************************
3135 * RtlWow64EnableFsRedirectionEx (NTDLL.@)
3137 NTSTATUS WINAPI RtlWow64EnableFsRedirectionEx( ULONG disable, ULONG *old_value )
3139 if (!is_wow64) return STATUS_NOT_IMPLEMENTED;
3140 if (((ULONG_PTR)old_value >> 16) == 0) return STATUS_ACCESS_VIOLATION;
3142 *old_value = !ntdll_get_thread_data()->wow64_redir;
3143 ntdll_get_thread_data()->wow64_redir = !disable;
3144 return STATUS_SUCCESS;
3148 /******************************************************************
3149 * RtlDoesFileExists_U (NTDLL.@)
3151 BOOLEAN WINAPI RtlDoesFileExists_U(LPCWSTR file_name)
3153 UNICODE_STRING nt_name;
3154 FILE_BASIC_INFORMATION basic_info;
3155 OBJECT_ATTRIBUTES attr;
3156 BOOLEAN ret;
3158 if (!RtlDosPathNameToNtPathName_U( file_name, &nt_name, NULL, NULL )) return FALSE;
3160 attr.Length = sizeof(attr);
3161 attr.RootDirectory = 0;
3162 attr.ObjectName = &nt_name;
3163 attr.Attributes = OBJ_CASE_INSENSITIVE;
3164 attr.SecurityDescriptor = NULL;
3165 attr.SecurityQualityOfService = NULL;
3167 ret = NtQueryAttributesFile(&attr, &basic_info) == STATUS_SUCCESS;
3169 RtlFreeUnicodeString( &nt_name );
3170 return ret;
3174 /***********************************************************************
3175 * DIR_unmount_device
3177 * Unmount the specified device.
3179 NTSTATUS DIR_unmount_device( HANDLE handle )
3181 NTSTATUS status;
3182 int unix_fd, needs_close;
3184 if (!(status = server_get_unix_fd( handle, 0, &unix_fd, &needs_close, NULL, NULL )))
3186 struct stat st;
3187 char *mount_point = NULL;
3189 if (fstat( unix_fd, &st ) == -1 || !is_valid_mounted_device( &st ))
3190 status = STATUS_INVALID_PARAMETER;
3191 else
3193 if ((mount_point = get_device_mount_point( st.st_rdev )))
3195 #ifdef __APPLE__
3196 static const char umount[] = "diskutil unmount >/dev/null 2>&1 ";
3197 #else
3198 static const char umount[] = "umount >/dev/null 2>&1 ";
3199 #endif
3200 char *cmd = RtlAllocateHeap( GetProcessHeap(), 0, strlen(mount_point)+sizeof(umount));
3201 if (cmd)
3203 strcpy( cmd, umount );
3204 strcat( cmd, mount_point );
3205 system( cmd );
3206 RtlFreeHeap( GetProcessHeap(), 0, cmd );
3207 #ifdef linux
3208 /* umount will fail to release the loop device since we still have
3209 a handle to it, so we release it here */
3210 if (major(st.st_rdev) == LOOP_MAJOR) ioctl( unix_fd, 0x4c01 /*LOOP_CLR_FD*/, 0 );
3211 #endif
3213 RtlFreeHeap( GetProcessHeap(), 0, mount_point );
3216 if (needs_close) close( unix_fd );
3218 return status;
3222 /******************************************************************************
3223 * DIR_get_unix_cwd
3225 * Retrieve the Unix name of the current directory; helper for wine_unix_to_nt_file_name.
3226 * Returned value must be freed by caller.
3228 NTSTATUS DIR_get_unix_cwd( char **cwd )
3230 int old_cwd, unix_fd, needs_close;
3231 CURDIR *curdir;
3232 HANDLE handle;
3233 NTSTATUS status;
3235 RtlAcquirePebLock();
3237 if (NtCurrentTeb()->Tib.SubSystemTib) /* FIXME: hack */
3238 curdir = &((WIN16_SUBSYSTEM_TIB *)NtCurrentTeb()->Tib.SubSystemTib)->curdir;
3239 else
3240 curdir = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectory;
3242 if (!(handle = curdir->Handle))
3244 UNICODE_STRING dirW;
3245 OBJECT_ATTRIBUTES attr;
3246 IO_STATUS_BLOCK io;
3248 if (!RtlDosPathNameToNtPathName_U( curdir->DosPath.Buffer, &dirW, NULL, NULL ))
3250 status = STATUS_OBJECT_NAME_INVALID;
3251 goto done;
3253 attr.Length = sizeof(attr);
3254 attr.RootDirectory = 0;
3255 attr.Attributes = OBJ_CASE_INSENSITIVE;
3256 attr.ObjectName = &dirW;
3257 attr.SecurityDescriptor = NULL;
3258 attr.SecurityQualityOfService = NULL;
3260 status = NtOpenFile( &handle, 0, &attr, &io, 0,
3261 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
3262 RtlFreeUnicodeString( &dirW );
3263 if (status != STATUS_SUCCESS) goto done;
3266 if ((status = server_get_unix_fd( handle, 0, &unix_fd, &needs_close, NULL, NULL )) == STATUS_SUCCESS)
3268 RtlEnterCriticalSection( &dir_section );
3270 if ((old_cwd = open(".", O_RDONLY)) != -1 && fchdir( unix_fd ) != -1)
3272 unsigned int size = 512;
3274 for (;;)
3276 if (!(*cwd = RtlAllocateHeap( GetProcessHeap(), 0, size )))
3278 status = STATUS_NO_MEMORY;
3279 break;
3281 if (getcwd( *cwd, size )) break;
3282 RtlFreeHeap( GetProcessHeap(), 0, *cwd );
3283 if (errno != ERANGE)
3285 status = STATUS_OBJECT_PATH_INVALID;
3286 break;
3288 size *= 2;
3290 if (fchdir( old_cwd ) == -1) chdir( "/" );
3292 else status = FILE_GetNtStatus();
3294 RtlLeaveCriticalSection( &dir_section );
3295 if (old_cwd != -1) close( old_cwd );
3296 if (needs_close) close( unix_fd );
3298 if (!curdir->Handle) NtClose( handle );
3300 done:
3301 RtlReleasePebLock();
3302 return status;
3305 struct read_changes_info
3307 HANDLE FileHandle;
3308 PVOID Buffer;
3309 ULONG BufferSize;
3310 PIO_APC_ROUTINE apc;
3311 void *apc_arg;
3314 /* callback for ioctl user APC */
3315 static void WINAPI read_changes_user_apc( void *arg, IO_STATUS_BLOCK *io, ULONG reserved )
3317 struct read_changes_info *info = arg;
3318 if (info->apc) info->apc( info->apc_arg, io, reserved );
3319 RtlFreeHeap( GetProcessHeap(), 0, info );
3322 static NTSTATUS read_changes_apc( void *user, PIO_STATUS_BLOCK iosb, NTSTATUS status, void **apc )
3324 struct read_changes_info *info = user;
3325 char data[PATH_MAX];
3326 NTSTATUS ret;
3327 int size;
3329 SERVER_START_REQ( read_change )
3331 req->handle = wine_server_obj_handle( info->FileHandle );
3332 wine_server_set_reply( req, data, PATH_MAX );
3333 ret = wine_server_call( req );
3334 size = wine_server_reply_size( reply );
3336 SERVER_END_REQ;
3338 if (ret == STATUS_SUCCESS && info->Buffer)
3340 PFILE_NOTIFY_INFORMATION pfni = info->Buffer;
3341 int i, left = info->BufferSize;
3342 DWORD *last_entry_offset = NULL;
3343 struct filesystem_event *event = (struct filesystem_event*)data;
3345 while (size && left >= sizeof(*pfni))
3347 /* convert to an NT style path */
3348 for (i=0; i<event->len; i++)
3349 if (event->name[i] == '/')
3350 event->name[i] = '\\';
3352 pfni->Action = event->action;
3353 pfni->FileNameLength = ntdll_umbstowcs( 0, event->name, event->len, pfni->FileName,
3354 (left - offsetof(FILE_NOTIFY_INFORMATION, FileName)) / sizeof(WCHAR));
3355 last_entry_offset = &pfni->NextEntryOffset;
3357 if(pfni->FileNameLength == -1 || pfni->FileNameLength == -2)
3358 break;
3360 i = offsetof(FILE_NOTIFY_INFORMATION, FileName[pfni->FileNameLength]);
3361 pfni->FileNameLength *= sizeof(WCHAR);
3362 pfni->NextEntryOffset = i;
3363 pfni = (FILE_NOTIFY_INFORMATION*)((char*)pfni + i);
3364 left -= i;
3366 i = (offsetof(struct filesystem_event, name[event->len])
3367 + sizeof(int)-1) / sizeof(int) * sizeof(int);
3368 event = (struct filesystem_event*)((char*)event + i);
3369 size -= i;
3372 if (size)
3374 ret = STATUS_NOTIFY_ENUM_DIR;
3375 size = 0;
3377 else
3379 *last_entry_offset = 0;
3380 size = info->BufferSize - left;
3383 else
3385 ret = STATUS_NOTIFY_ENUM_DIR;
3386 size = 0;
3389 iosb->u.Status = ret;
3390 iosb->Information = size;
3391 *apc = read_changes_user_apc;
3392 return ret;
3395 #define FILE_NOTIFY_ALL ( \
3396 FILE_NOTIFY_CHANGE_FILE_NAME | \
3397 FILE_NOTIFY_CHANGE_DIR_NAME | \
3398 FILE_NOTIFY_CHANGE_ATTRIBUTES | \
3399 FILE_NOTIFY_CHANGE_SIZE | \
3400 FILE_NOTIFY_CHANGE_LAST_WRITE | \
3401 FILE_NOTIFY_CHANGE_LAST_ACCESS | \
3402 FILE_NOTIFY_CHANGE_CREATION | \
3403 FILE_NOTIFY_CHANGE_SECURITY )
3405 /******************************************************************************
3406 * NtNotifyChangeDirectoryFile [NTDLL.@]
3408 NTSTATUS WINAPI
3409 NtNotifyChangeDirectoryFile( HANDLE FileHandle, HANDLE Event,
3410 PIO_APC_ROUTINE ApcRoutine, PVOID ApcContext,
3411 PIO_STATUS_BLOCK IoStatusBlock, PVOID Buffer,
3412 ULONG BufferSize, ULONG CompletionFilter, BOOLEAN WatchTree )
3414 struct read_changes_info *info;
3415 NTSTATUS status;
3416 ULONG_PTR cvalue = ApcRoutine ? 0 : (ULONG_PTR)ApcContext;
3418 TRACE("%p %p %p %p %p %p %u %u %d\n",
3419 FileHandle, Event, ApcRoutine, ApcContext, IoStatusBlock,
3420 Buffer, BufferSize, CompletionFilter, WatchTree );
3422 if (!IoStatusBlock)
3423 return STATUS_ACCESS_VIOLATION;
3425 if (CompletionFilter == 0 || (CompletionFilter & ~FILE_NOTIFY_ALL))
3426 return STATUS_INVALID_PARAMETER;
3428 info = RtlAllocateHeap( GetProcessHeap(), 0, sizeof *info );
3429 if (!info)
3430 return STATUS_NO_MEMORY;
3432 info->FileHandle = FileHandle;
3433 info->Buffer = Buffer;
3434 info->BufferSize = BufferSize;
3435 info->apc = ApcRoutine;
3436 info->apc_arg = ApcContext;
3438 SERVER_START_REQ( read_directory_changes )
3440 req->filter = CompletionFilter;
3441 req->want_data = (Buffer != NULL);
3442 req->subtree = WatchTree;
3443 req->async.handle = wine_server_obj_handle( FileHandle );
3444 req->async.callback = wine_server_client_ptr( read_changes_apc );
3445 req->async.iosb = wine_server_client_ptr( IoStatusBlock );
3446 req->async.arg = wine_server_client_ptr( info );
3447 req->async.event = wine_server_obj_handle( Event );
3448 req->async.cvalue = cvalue;
3449 status = wine_server_call( req );
3451 SERVER_END_REQ;
3453 if (status != STATUS_PENDING)
3454 RtlFreeHeap( GetProcessHeap(), 0, info );
3456 return status;