mirrored from git://source.winehq.org/git/wine.git
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
file.c
6325 lines (5522 loc) · 209 KB
/
file.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* NTDLL directory and file functions
*
* Copyright 1993 Erik Bos
* Copyright 2003 Eric Pouech
* Copyright 1996, 2004 Alexandre Julliard
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#if 0
#pragma makedep unix
#endif
#include "config.h"
#include "wine/port.h"
#include <assert.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#ifdef HAVE_MNTENT_H
#include <mntent.h>
#endif
#ifdef HAVE_POLL_H
#include <poll.h>
#endif
#ifdef HAVE_SYS_STAT_H
# include <sys/stat.h>
#endif
#ifdef HAVE_SYS_STATVFS_H
# include <sys/statvfs.h>
#endif
#ifdef HAVE_SYS_SYSCALL_H
# include <sys/syscall.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
#endif
#ifdef HAVE_SYS_ATTR_H
#include <sys/attr.h>
#endif
#ifdef MAJOR_IN_MKDEV
# include <sys/mkdev.h>
#elif defined(MAJOR_IN_SYSMACROS)
# include <sys/sysmacros.h>
#endif
#ifdef HAVE_SYS_VNODE_H
# ifdef HAVE_STDINT_H
# include <stdint.h> /* needed for kfreebsd */
# endif
/* Work around a conflict with Solaris' system list defined in sys/list.h. */
#define list SYSLIST
#define list_next SYSLIST_NEXT
#define list_prev SYSLIST_PREV
#define list_head SYSLIST_HEAD
#define list_tail SYSLIST_TAIL
#define list_move_tail SYSLIST_MOVE_TAIL
#define list_remove SYSLIST_REMOVE
#include <sys/vnode.h>
#undef list
#undef list_next
#undef list_prev
#undef list_head
#undef list_tail
#undef list_move_tail
#undef list_remove
#endif
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#ifdef HAVE_LINUX_IOCTL_H
#include <linux/ioctl.h>
#endif
#ifdef HAVE_LINUX_MAJOR_H
# include <linux/major.h>
#endif
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#ifdef HAVE_SYS_MOUNT_H
#include <sys/mount.h>
#endif
#ifdef HAVE_SYS_STATFS_H
#include <sys/statfs.h>
#endif
#include <time.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include "ntstatus.h"
#define WIN32_NO_STATUS
#define NONAMELESSUNION
#include "windef.h"
#include "winnt.h"
#include "winioctl.h"
#include "winternl.h"
#include "ddk/ntddk.h"
#include "ddk/ntddser.h"
#include "ddk/wdm.h"
#define WINE_MOUNTMGR_EXTENSIONS
#include "ddk/mountmgr.h"
#include "wine/server.h"
#include "wine/list.h"
#include "wine/debug.h"
#include "unix_private.h"
WINE_DEFAULT_DEBUG_CHANNEL(file);
WINE_DECLARE_DEBUG_CHANNEL(winediag);
#define MAX_DOS_DRIVES 26
#define FILE_WRITE_TO_END_OF_FILE ((LONGLONG)-1)
#define FILE_USE_FILE_POINTER_POSITION ((LONGLONG)-2)
/* just in case... */
#undef VFAT_IOCTL_READDIR_BOTH
#undef EXT2_IOC_GETFLAGS
#undef EXT4_CASEFOLD_FL
#ifdef linux
/* We want the real kernel dirent structure, not the libc one */
typedef struct
{
long d_ino;
long d_off;
unsigned short d_reclen;
char d_name[256];
} KERNEL_DIRENT;
/* Define the VFAT ioctl to get both short and long file names */
#define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, KERNEL_DIRENT [2] )
/* Define the ext2 ioctl for handling extra attributes */
#define EXT2_IOC_GETFLAGS _IOR('f', 1, long)
/* Case-insensitivity attribute */
#define EXT4_CASEFOLD_FL 0x40000000
#ifndef O_DIRECTORY
# define O_DIRECTORY 0200000 /* must be directory */
#endif
#ifndef AT_NO_AUTOMOUNT
#define AT_NO_AUTOMOUNT 0x800
#endif
#endif /* linux */
#define IS_SEPARATOR(ch) ((ch) == '\\' || (ch) == '/')
#define INVALID_NT_CHARS '*','?','<','>','|','"'
#define INVALID_DOS_CHARS INVALID_NT_CHARS,'+','=',',',';','[',']',' ','\345'
#define MAX_DIR_ENTRY_LEN 255 /* max length of a directory entry in chars */
#define MAX_IGNORED_FILES 4
struct file_identity
{
dev_t dev;
ino_t ino;
};
static struct file_identity ignored_files[MAX_IGNORED_FILES];
static unsigned int ignored_files_count;
union file_directory_info
{
ULONG next;
FILE_DIRECTORY_INFORMATION dir;
FILE_BOTH_DIRECTORY_INFORMATION both;
FILE_FULL_DIRECTORY_INFORMATION full;
FILE_ID_BOTH_DIRECTORY_INFORMATION id_both;
FILE_ID_FULL_DIRECTORY_INFORMATION id_full;
FILE_ID_GLOBAL_TX_DIR_INFORMATION id_tx;
FILE_NAMES_INFORMATION names;
};
struct dir_data_buffer
{
struct dir_data_buffer *next; /* next buffer in the list */
unsigned int size; /* total size of the buffer */
unsigned int pos; /* current position in the buffer */
char data[1];
};
struct dir_data_names
{
const WCHAR *long_name; /* long file name in Unicode */
const WCHAR *short_name; /* short file name in Unicode */
const char *unix_name; /* Unix file name in host encoding */
};
struct dir_data
{
unsigned int size; /* size of the names array */
unsigned int count; /* count of used entries in the names array */
unsigned int pos; /* current reading position in the names array */
struct file_identity id; /* directory file identity */
struct dir_data_names *names; /* directory file names */
struct dir_data_buffer *buffer; /* head of data buffers list */
};
static const unsigned int dir_data_buffer_initial_size = 4096;
static const unsigned int dir_data_cache_initial_size = 256;
static const unsigned int dir_data_names_initial_size = 64;
static struct dir_data **dir_data_cache;
static unsigned int dir_data_cache_size;
static BOOL show_dot_files;
static mode_t start_umask;
/* at some point we may want to allow Winelib apps to set this */
static const BOOL is_case_sensitive = FALSE;
static struct file_identity windir;
static RTL_CRITICAL_SECTION dir_section;
static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
{
0, 0, &dir_section,
{ &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
0, 0, { (DWORD_PTR)(__FILE__ ": dir_section") }
};
static RTL_CRITICAL_SECTION dir_section = { &critsect_debug, -1, 0, 0, 0, 0 };
/* check if a given Unicode char is OK in a DOS short name */
static inline BOOL is_invalid_dos_char( WCHAR ch )
{
static const WCHAR invalid_chars[] = { INVALID_DOS_CHARS,'~','.',0 };
if (ch > 0x7f) return TRUE;
return wcschr( invalid_chars, ch ) != NULL;
}
/* check if the device can be a mounted volume */
static inline BOOL is_valid_mounted_device( const struct stat *st )
{
#if defined(linux) || defined(__sun__)
return S_ISBLK( st->st_mode );
#else
/* disks are char devices on *BSD */
return S_ISCHR( st->st_mode );
#endif
}
static inline void ignore_file( const char *name )
{
struct stat st;
assert( ignored_files_count < MAX_IGNORED_FILES );
if (!stat( name, &st ))
{
ignored_files[ignored_files_count].dev = st.st_dev;
ignored_files[ignored_files_count].ino = st.st_ino;
ignored_files_count++;
}
}
static inline BOOL is_same_file( const struct file_identity *file, const struct stat *st )
{
return st->st_dev == file->dev && st->st_ino == file->ino;
}
static inline BOOL is_ignored_file( const struct stat *st )
{
unsigned int i;
for (i = 0; i < ignored_files_count; i++)
if (is_same_file( &ignored_files[i], st )) return TRUE;
return FALSE;
}
static inline unsigned int dir_info_align( unsigned int len )
{
return (len + 7) & ~7;
}
static inline unsigned int dir_info_size( FILE_INFORMATION_CLASS class, unsigned int len )
{
switch (class)
{
case FileDirectoryInformation:
return offsetof( FILE_DIRECTORY_INFORMATION, FileName[len] );
case FileBothDirectoryInformation:
return offsetof( FILE_BOTH_DIRECTORY_INFORMATION, FileName[len] );
case FileFullDirectoryInformation:
return offsetof( FILE_FULL_DIRECTORY_INFORMATION, FileName[len] );
case FileIdBothDirectoryInformation:
return offsetof( FILE_ID_BOTH_DIRECTORY_INFORMATION, FileName[len] );
case FileIdFullDirectoryInformation:
return offsetof( FILE_ID_FULL_DIRECTORY_INFORMATION, FileName[len] );
case FileIdGlobalTxDirectoryInformation:
return offsetof( FILE_ID_GLOBAL_TX_DIR_INFORMATION, FileName[len] );
case FileNamesInformation:
return offsetof( FILE_NAMES_INFORMATION, FileName[len] );
default:
assert(0);
return 0;
}
}
static inline BOOL has_wildcard( const UNICODE_STRING *mask )
{
int i;
if (!mask) return TRUE;
for (i = 0; i < mask->Length / sizeof(WCHAR); i++)
if (mask->Buffer[i] == '*' || mask->Buffer[i] == '?') return TRUE;
return FALSE;
}
NTSTATUS errno_to_status( int err )
{
TRACE( "errno = %d\n", err );
switch (err)
{
case EAGAIN: return STATUS_SHARING_VIOLATION;
case EBADF: return STATUS_INVALID_HANDLE;
case EBUSY: return STATUS_DEVICE_BUSY;
case ENOSPC: return STATUS_DISK_FULL;
case EPERM:
case EROFS:
case EACCES: return STATUS_ACCESS_DENIED;
case ENOTDIR: return STATUS_OBJECT_PATH_NOT_FOUND;
case ENOENT: return STATUS_OBJECT_NAME_NOT_FOUND;
case EISDIR: return STATUS_FILE_IS_A_DIRECTORY;
case EMFILE:
case ENFILE: return STATUS_TOO_MANY_OPENED_FILES;
case EINVAL: return STATUS_INVALID_PARAMETER;
case ENOTEMPTY: return STATUS_DIRECTORY_NOT_EMPTY;
case EPIPE: return STATUS_PIPE_DISCONNECTED;
case EIO: return STATUS_DEVICE_NOT_READY;
#ifdef ENOMEDIUM
case ENOMEDIUM: return STATUS_NO_MEDIA_IN_DEVICE;
#endif
case ENXIO: return STATUS_NO_SUCH_DEVICE;
case ENOTTY:
case EOPNOTSUPP:return STATUS_NOT_SUPPORTED;
case ECONNRESET:return STATUS_PIPE_DISCONNECTED;
case EFAULT: return STATUS_ACCESS_VIOLATION;
case ESPIPE: return STATUS_ILLEGAL_FUNCTION;
case ELOOP: return STATUS_REPARSE_POINT_NOT_RESOLVED;
#ifdef ETIME /* Missing on FreeBSD */
case ETIME: return STATUS_IO_TIMEOUT;
#endif
case ENOEXEC: /* ?? */
case EEXIST: /* ?? */
default:
FIXME( "Converting errno %d to STATUS_UNSUCCESSFUL\n", err );
return STATUS_UNSUCCESSFUL;
}
}
/* get space from the current directory data buffer, allocating a new one if necessary */
static void *get_dir_data_space( struct dir_data *data, unsigned int size )
{
struct dir_data_buffer *buffer = data->buffer;
void *ret;
if (!buffer || size > buffer->size - buffer->pos)
{
unsigned int new_size = buffer ? buffer->size * 2 : dir_data_buffer_initial_size;
if (new_size < size) new_size = size;
if (!(buffer = RtlAllocateHeap( GetProcessHeap(), 0,
offsetof( struct dir_data_buffer, data[new_size] ) ))) return NULL;
buffer->pos = 0;
buffer->size = new_size;
buffer->next = data->buffer;
data->buffer = buffer;
}
ret = buffer->data + buffer->pos;
buffer->pos += size;
return ret;
}
/* add a string to the directory data buffer */
static const char *add_dir_data_nameA( struct dir_data *data, const char *name )
{
/* keep buffer data WCHAR-aligned */
char *ptr = get_dir_data_space( data, (strlen( name ) + sizeof(WCHAR)) & ~(sizeof(WCHAR) - 1) );
if (ptr) strcpy( ptr, name );
return ptr;
}
/* add a Unicode string to the directory data buffer */
static const WCHAR *add_dir_data_nameW( struct dir_data *data, const WCHAR *name )
{
WCHAR *ptr = get_dir_data_space( data, (wcslen( name ) + 1) * sizeof(WCHAR) );
if (ptr) wcscpy( ptr, name );
return ptr;
}
/* add an entry to the directory names array */
static BOOL add_dir_data_names( struct dir_data *data, const WCHAR *long_name,
const WCHAR *short_name, const char *unix_name )
{
static const WCHAR empty[1];
struct dir_data_names *names = data->names;
if (data->count >= data->size)
{
unsigned int new_size = max( data->size * 2, dir_data_names_initial_size );
if (names) names = RtlReAllocateHeap( GetProcessHeap(), 0, names, new_size * sizeof(*names) );
else names = RtlAllocateHeap( GetProcessHeap(), 0, new_size * sizeof(*names) );
if (!names) return FALSE;
data->size = new_size;
data->names = names;
}
if (short_name[0])
{
if (!(names[data->count].short_name = add_dir_data_nameW( data, short_name ))) return FALSE;
}
else names[data->count].short_name = empty;
if (!(names[data->count].long_name = add_dir_data_nameW( data, long_name ))) return FALSE;
if (!(names[data->count].unix_name = add_dir_data_nameA( data, unix_name ))) return FALSE;
data->count++;
return TRUE;
}
/* free the complete directory data structure */
static void free_dir_data( struct dir_data *data )
{
struct dir_data_buffer *buffer, *next;
if (!data) return;
for (buffer = data->buffer; buffer; buffer = next)
{
next = buffer->next;
RtlFreeHeap( GetProcessHeap(), 0, buffer );
}
RtlFreeHeap( GetProcessHeap(), 0, data->names );
RtlFreeHeap( GetProcessHeap(), 0, data );
}
/* support for a directory queue for filesystem searches */
struct dir_name
{
struct list entry;
char name[1];
};
static struct list dir_queue = LIST_INIT( dir_queue );
static NTSTATUS add_dir_to_queue( const char *name )
{
int len = strlen( name ) + 1;
struct dir_name *dir = RtlAllocateHeap( GetProcessHeap(), 0,
FIELD_OFFSET( struct dir_name, name[len] ));
if (!dir) return STATUS_NO_MEMORY;
strcpy( dir->name, name );
list_add_tail( &dir_queue, &dir->entry );
return STATUS_SUCCESS;
}
static NTSTATUS next_dir_in_queue( char *name )
{
struct list *head = list_head( &dir_queue );
if (head)
{
struct dir_name *dir = LIST_ENTRY( head, struct dir_name, entry );
strcpy( name, dir->name );
list_remove( &dir->entry );
RtlFreeHeap( GetProcessHeap(), 0, dir );
return STATUS_SUCCESS;
}
return STATUS_OBJECT_NAME_NOT_FOUND;
}
static void flush_dir_queue(void)
{
struct list *head;
while ((head = list_head( &dir_queue )))
{
struct dir_name *dir = LIST_ENTRY( head, struct dir_name, entry );
list_remove( &dir->entry );
RtlFreeHeap( GetProcessHeap(), 0, dir );
}
}
#ifdef __ANDROID__
static char *unescape_field( char *str )
{
char *in, *out;
for (in = out = str; *in; in++, out++)
{
*out = *in;
if (in[0] == '\\')
{
if (in[1] == '\\')
{
out[0] = '\\';
in++;
}
else if (in[1] == '0' && in[2] == '4' && in[3] == '0')
{
out[0] = ' ';
in += 3;
}
else if (in[1] == '0' && in[2] == '1' && in[3] == '1')
{
out[0] = '\t';
in += 3;
}
else if (in[1] == '0' && in[2] == '1' && in[3] == '2')
{
out[0] = '\n';
in += 3;
}
else if (in[1] == '1' && in[2] == '3' && in[3] == '4')
{
out[0] = '\\';
in += 3;
}
}
}
*out = '\0';
return str;
}
static inline char *get_field( char **str )
{
char *ret;
ret = strsep( str, " \t" );
if (*str) *str += strspn( *str, " \t" );
return ret;
}
/************************************************************************
* getmntent_replacement
*
* getmntent replacement for Android.
*
* NB returned static buffer is not thread safe; protect with dir_section.
*/
static struct mntent *getmntent_replacement( FILE *f )
{
static struct mntent entry;
static char buf[4096];
char *p, *start;
do
{
if (!fgets( buf, sizeof(buf), f )) return NULL;
p = strchr( buf, '\n' );
if (p) *p = '\0';
else /* Partially unread line, move file ptr to end */
{
char tmp[1024];
while (fgets( tmp, sizeof(tmp), f ))
if (strchr( tmp, '\n' )) break;
}
start = buf + strspn( buf, " \t" );
} while (start[0] == '\0' || start[0] == '#');
p = get_field( &start );
entry.mnt_fsname = p ? unescape_field( p ) : (char *)"";
p = get_field( &start );
entry.mnt_dir = p ? unescape_field( p ) : (char *)"";
p = get_field( &start );
entry.mnt_type = p ? unescape_field( p ) : (char *)"";
p = get_field( &start );
entry.mnt_opts = p ? unescape_field( p ) : (char *)"";
p = get_field( &start );
entry.mnt_freq = p ? atoi(p) : 0;
p = get_field( &start );
entry.mnt_passno = p ? atoi(p) : 0;
return &entry;
}
#define getmntent getmntent_replacement
#endif
/***********************************************************************
* parse_mount_entries
*
* Parse mount entries looking for a given device. Helper for get_default_drive_device.
*/
#ifdef sun
#include <sys/vfstab.h>
static char *parse_vfstab_entries( FILE *f, dev_t dev, ino_t ino)
{
struct vfstab entry;
struct stat st;
char *device;
while (! getvfsent( f, &entry ))
{
/* don't even bother stat'ing network mounts, there's no meaningful device anyway */
if (!strcmp( entry.vfs_fstype, "nfs" ) ||
!strcmp( entry.vfs_fstype, "smbfs" ) ||
!strcmp( entry.vfs_fstype, "ncpfs" )) continue;
if (stat( entry.vfs_mountp, &st ) == -1) continue;
if (st.st_dev != dev || st.st_ino != ino) continue;
if (!strcmp( entry.vfs_fstype, "fd" ))
{
if ((device = strstr( entry.vfs_mntopts, "dev=" )))
{
char *p = strchr( device + 4, ',' );
if (p) *p = 0;
return device + 4;
}
}
else
return entry.vfs_special;
}
return NULL;
}
#endif
#ifdef linux
static char *parse_mount_entries( FILE *f, dev_t dev, ino_t ino )
{
struct mntent *entry;
struct stat st;
char *device;
while ((entry = getmntent( f )))
{
/* don't even bother stat'ing network mounts, there's no meaningful device anyway */
if (!strcmp( entry->mnt_type, "nfs" ) ||
!strcmp( entry->mnt_type, "cifs" ) ||
!strcmp( entry->mnt_type, "smbfs" ) ||
!strcmp( entry->mnt_type, "ncpfs" )) continue;
if (stat( entry->mnt_dir, &st ) == -1) continue;
if (st.st_dev != dev || st.st_ino != ino) continue;
if (!strcmp( entry->mnt_type, "supermount" ))
{
if ((device = strstr( entry->mnt_opts, "dev=" )))
{
char *p = strchr( device + 4, ',' );
if (p) *p = 0;
return device + 4;
}
}
else if (!stat( entry->mnt_fsname, &st ) && S_ISREG(st.st_mode))
{
/* if device is a regular file check for a loop mount */
if ((device = strstr( entry->mnt_opts, "loop=" )))
{
char *p = strchr( device + 5, ',' );
if (p) *p = 0;
return device + 5;
}
}
else
return entry->mnt_fsname;
}
return NULL;
}
#endif
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
#include <fstab.h>
static char *parse_mount_entries( FILE *f, dev_t dev, ino_t ino )
{
struct fstab *entry;
struct stat st;
while ((entry = getfsent()))
{
/* don't even bother stat'ing network mounts, there's no meaningful device anyway */
if (!strcmp( entry->fs_vfstype, "nfs" ) ||
!strcmp( entry->fs_vfstype, "smbfs" ) ||
!strcmp( entry->fs_vfstype, "ncpfs" )) continue;
if (stat( entry->fs_file, &st ) == -1) continue;
if (st.st_dev != dev || st.st_ino != ino) continue;
return entry->fs_spec;
}
return NULL;
}
#endif
#ifdef sun
#include <sys/mnttab.h>
static char *parse_mount_entries( FILE *f, dev_t dev, ino_t ino )
{
struct mnttab entry;
struct stat st;
char *device;
while (( ! getmntent( f, &entry) ))
{
/* don't even bother stat'ing network mounts, there's no meaningful device anyway */
if (!strcmp( entry.mnt_fstype, "nfs" ) ||
!strcmp( entry.mnt_fstype, "smbfs" ) ||
!strcmp( entry.mnt_fstype, "ncpfs" )) continue;
if (stat( entry.mnt_mountp, &st ) == -1) continue;
if (st.st_dev != dev || st.st_ino != ino) continue;
if (!strcmp( entry.mnt_fstype, "fd" ))
{
if ((device = strstr( entry.mnt_mntopts, "dev=" )))
{
char *p = strchr( device + 4, ',' );
if (p) *p = 0;
return device + 4;
}
}
else
return entry.mnt_special;
}
return NULL;
}
#endif
/***********************************************************************
* get_default_drive_device
*
* Return the default device to use for a given drive mount point.
*/
static char *get_default_drive_device( const char *root )
{
char *ret = NULL;
#ifdef linux
FILE *f;
char *device = NULL;
int fd, res = -1;
struct stat st;
/* try to open it first to force it to get mounted */
if ((fd = open( root, O_RDONLY | O_DIRECTORY )) != -1)
{
res = fstat( fd, &st );
close( fd );
}
/* now try normal stat just in case */
if (res == -1) res = stat( root, &st );
if (res == -1) return NULL;
RtlEnterCriticalSection( &dir_section );
#ifdef __ANDROID__
if ((f = fopen( "/proc/mounts", "r" )))
{
device = parse_mount_entries( f, st.st_dev, st.st_ino );
fclose( f );
}
#else
if ((f = fopen( "/etc/mtab", "r" )))
{
device = parse_mount_entries( f, st.st_dev, st.st_ino );
fclose( f );
}
/* look through fstab too in case it's not mounted (for instance if it's an audio CD) */
if (!device && (f = fopen( "/etc/fstab", "r" )))
{
device = parse_mount_entries( f, st.st_dev, st.st_ino );
fclose( f );
}
#endif
if (device)
{
ret = RtlAllocateHeap( GetProcessHeap(), 0, strlen(device) + 1 );
if (ret) strcpy( ret, device );
}
RtlLeaveCriticalSection( &dir_section );
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__ ) || defined(__DragonFly__)
char *device = NULL;
int fd, res = -1;
struct stat st;
/* try to open it first to force it to get mounted */
if ((fd = open( root, O_RDONLY )) != -1)
{
res = fstat( fd, &st );
close( fd );
}
/* now try normal stat just in case */
if (res == -1) res = stat( root, &st );
if (res == -1) return NULL;
RtlEnterCriticalSection( &dir_section );
/* The FreeBSD parse_mount_entries doesn't require a file argument, so just
* pass NULL. Leave the argument in for symmetry.
*/
device = parse_mount_entries( NULL, st.st_dev, st.st_ino );
if (device)
{
ret = RtlAllocateHeap( GetProcessHeap(), 0, strlen(device) + 1 );
if (ret) strcpy( ret, device );
}
RtlLeaveCriticalSection( &dir_section );
#elif defined( sun )
FILE *f;
char *device = NULL;
int fd, res = -1;
struct stat st;
/* try to open it first to force it to get mounted */
if ((fd = open( root, O_RDONLY )) != -1)
{
res = fstat( fd, &st );
close( fd );
}
/* now try normal stat just in case */
if (res == -1) res = stat( root, &st );
if (res == -1) return NULL;
RtlEnterCriticalSection( &dir_section );
if ((f = fopen( "/etc/mnttab", "r" )))
{
device = parse_mount_entries( f, st.st_dev, st.st_ino);
fclose( f );
}
/* look through fstab too in case it's not mounted (for instance if it's an audio CD) */
if (!device && (f = fopen( "/etc/vfstab", "r" )))
{
device = parse_vfstab_entries( f, st.st_dev, st.st_ino );
fclose( f );
}
if (device)
{
ret = RtlAllocateHeap( GetProcessHeap(), 0, strlen(device) + 1 );
if (ret) strcpy( ret, device );
}
RtlLeaveCriticalSection( &dir_section );
#elif defined(__APPLE__)
struct statfs *mntStat;
struct stat st;
int i;
int mntSize;
dev_t dev;
ino_t ino;
static const char path_bsd_device[] = "/dev/disk";
int res;
res = stat( root, &st );
if (res == -1) return NULL;
dev = st.st_dev;
ino = st.st_ino;
RtlEnterCriticalSection( &dir_section );
mntSize = getmntinfo(&mntStat, MNT_NOWAIT);
for (i = 0; i < mntSize && !ret; i++)
{
if (stat(mntStat[i].f_mntonname, &st ) == -1) continue;
if (st.st_dev != dev || st.st_ino != ino) continue;
/* FIXME add support for mounted network drive */
if ( strncmp(mntStat[i].f_mntfromname, path_bsd_device, strlen(path_bsd_device)) == 0)
{
/* set return value to the corresponding raw BSD node */
ret = RtlAllocateHeap( GetProcessHeap(), 0, strlen(mntStat[i].f_mntfromname) + 2 /* 2 : r and \0 */ );
if (ret)
{
strcpy(ret, "/dev/r");
strcat(ret, mntStat[i].f_mntfromname+sizeof("/dev/")-1);
}
}
}
RtlLeaveCriticalSection( &dir_section );
#else
static int warned;
if (!warned++) FIXME( "auto detection of DOS devices not supported on this platform\n" );
#endif
return ret;
}
/***********************************************************************
* get_device_mount_point
*
* Return the current mount point for a device.
*/
static char *get_device_mount_point( dev_t dev )
{
char *ret = NULL;
#ifdef linux
FILE *f;
RtlEnterCriticalSection( &dir_section );
#ifdef __ANDROID__
if ((f = fopen( "/proc/mounts", "r" )))
#else
if ((f = fopen( "/etc/mtab", "r" )))
#endif
{
struct mntent *entry;
struct stat st;
char *p, *device;
while ((entry = getmntent( f )))
{
/* don't even bother stat'ing network mounts, there's no meaningful device anyway */
if (!strcmp( entry->mnt_type, "nfs" ) ||
!strcmp( entry->mnt_type, "cifs" ) ||
!strcmp( entry->mnt_type, "smbfs" ) ||
!strcmp( entry->mnt_type, "ncpfs" )) continue;
if (!strcmp( entry->mnt_type, "supermount" ))
{
if ((device = strstr( entry->mnt_opts, "dev=" )))
{
device += 4;
if ((p = strchr( device, ',' ))) *p = 0;
}
}
else if (!stat( entry->mnt_fsname, &st ) && S_ISREG(st.st_mode))
{
/* if device is a regular file check for a loop mount */
if ((device = strstr( entry->mnt_opts, "loop=" )))
{
device += 5;
if ((p = strchr( device, ',' ))) *p = 0;
}
}
else device = entry->mnt_fsname;
if (device && !stat( device, &st ) && S_ISBLK(st.st_mode) && st.st_rdev == dev)
{
ret = RtlAllocateHeap( GetProcessHeap(), 0, strlen(entry->mnt_dir) + 1 );
if (ret) strcpy( ret, entry->mnt_dir );
break;
}
}
fclose( f );
}
RtlLeaveCriticalSection( &dir_section );
#elif defined(__APPLE__)
struct statfs *entry;
struct stat st;
int i, size;
RtlEnterCriticalSection( &dir_section );
size = getmntinfo( &entry, MNT_NOWAIT );
for (i = 0; i < size; i++)
{
if (stat( entry[i].f_mntfromname, &st ) == -1) continue;
if (S_ISBLK(st.st_mode) && st.st_rdev == dev)
{
ret = RtlAllocateHeap( GetProcessHeap(), 0, strlen(entry[i].f_mntonname) + 1 );
if (ret) strcpy( ret, entry[i].f_mntonname );
break;
}
}
RtlLeaveCriticalSection( &dir_section );
#else
static int warned;
if (!warned++) FIXME( "unmounting devices not supported on this platform\n" );
#endif
return ret;