-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNSFileManager.m
More file actions
4159 lines (3712 loc) · 106 KB
/
NSFileManager.m
File metadata and controls
4159 lines (3712 loc) · 106 KB
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
/**
NSFileManager.m
Copyright (C) 1997-2020 Free Software Foundation, Inc.
Author: Mircea Oancea <mircea@jupiter.elcom.pub.ro>
Author: Ovidiu Predescu <ovidiu@net-community.com>
Date: Feb 1997
Updates and fixes: Richard Frith-Macdonald
Author: Nicola Pero <n.pero@mi.flashnet.it>
Date: Apr 2001
Rewritten NSDirectoryEnumerator
Author: Richard Frith-Macdonald <rfm@gnu.org>
Date: Sep 2002
Rewritten attribute handling code
This file is part of the GNUstep Base Library.
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 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 Street, Fifth Floor,
Boston, MA 02110 USA.
<title>NSFileManager class reference</title>
$Date$ $Revision$
*/
/* The following define is needed for Solaris get(pw/gr)(nam/uid)_r declartions
which default to pre POSIX declaration. */
#define _POSIX_PTHREAD_SEMANTICS
#import "common.h"
#define EXPOSE_NSFileManager_IVARS 1
#define EXPOSE_NSDirectoryEnumerator_IVARS 1
#import "Foundation/NSArray.h"
#import "Foundation/NSAutoreleasePool.h"
#import "Foundation/NSData.h"
#import "Foundation/NSDate.h"
#import "Foundation/NSDictionary.h"
#import "Foundation/NSEnumerator.h"
#import "Foundation/NSError.h"
#import "Foundation/NSException.h"
#import "Foundation/NSFileManager.h"
#import "Foundation/NSLock.h"
#import "Foundation/NSPathUtilities.h"
#import "Foundation/NSProcessInfo.h"
#import "Foundation/NSSet.h"
#import "Foundation/NSURL.h"
#import "Foundation/NSValue.h"
#import "GSPrivate.h"
#import "GNUstepBase/NSString+GNUstepBase.h"
#import "GNUstepBase/NSTask+GNUstepBase.h"
#include <stdio.h>
/* determine directory reading files */
#if defined(HAVE_DIRENT_H)
# include <dirent.h>
#elif defined(HAVE_SYS_DIR_H)
# include <sys/dir.h>
#elif defined(HAVE_SYS_NDIR_H)
# include <sys/ndir.h>
#elif defined(HAVE_NDIR_H)
# include <ndir.h>
#elif defined(_MSC_VER)
// we provide our own version of dirent.h on Windows MSVC
# include <win32/dirent.h>
#endif
#ifdef HAVE_WINDOWS_H
# include <windows.h>
#endif
#if defined(_WIN32)
#include <stdio.h>
#include <tchar.h>
#include <wchar.h>
#include <accctrl.h>
#include <aclapi.h>
#define WIN32ERR ((DWORD)0xFFFFFFFF)
#endif
/* determine filesystem max path length */
#if defined(_POSIX_VERSION) || defined(_WIN32)
# if defined(_WIN32)
# include <sys/utime.h>
# else
# include <utime.h>
# endif
#endif
#ifdef HAVE_SYS_CDEFS_H
# include <sys/cdefs.h>
#endif
#ifdef HAVE_SYS_SYSLIMITS_H
# include <sys/syslimits.h>
#endif
#ifdef HAVE_SYS_PARAM_H
# include <sys/param.h> /* for MAXPATHLEN */
#endif
#ifndef PATH_MAX
# ifdef _POSIX_VERSION
# define PATH_MAX _POSIX_PATH_MAX
# else
# ifdef MAXPATHLEN
# define PATH_MAX MAXPATHLEN
# else
# define PATH_MAX 1024
# endif
# endif
#endif
/* determine if we have statfs struct and function */
#ifdef HAVE_SYS_VFS_H
# include <sys/vfs.h>
#endif
#ifdef HAVE_SYS_STATVFS_H
# include <sys/statvfs.h>
#endif
#ifdef HAVE_SYS_STATFS_H
# include <sys/statfs.h>
#endif
#if defined(HAVE_SYS_FILE_H)
# include <sys/file.h>
#endif
#ifdef HAVE_SYS_MOUNT_H
#include <sys/mount.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#if defined(HAVE_SYS_FCNTL_H)
# include <sys/fcntl.h>
#elif defined(HAVE_FCNTL_H)
# include <fcntl.h>
#endif
#ifdef HAVE_PWD_H
#include <pwd.h> /* For struct passwd */
#endif
#ifdef HAVE_GRP_H
#include <grp.h> /* For struct group */
#endif
#ifdef HAVE_UTIME_H
# include <utime.h>
#endif
/*
* On systems that have the O_BINARY flag, use it for a binary copy.
*/
#if defined(O_BINARY)
#define GSBINIO O_BINARY
#else
#define GSBINIO 0
#endif
@interface NSDirectoryEnumerator (Local)
- (id) initWithDirectoryPath: (NSString*)path
recurseIntoSubdirectories: (BOOL)recurse
followSymlinks: (BOOL)follow
justContents: (BOOL)justContents
for: (NSFileManager*)mgr;
- (id) initWithDirectoryPath: (NSString*)path
recurseIntoSubdirectories: (BOOL)recurse
followSymlinks: (BOOL)follow
justContents: (BOOL)justContents
skipHidden: (BOOL)skipHidden
errorHandler: (GSDirEnumErrorHandler) handler
for: (NSFileManager*)mgr;
- (void) _setSkipHidden: (BOOL)flag;
- (void) _setErrorHandler: (GSDirEnumErrorHandler) handler;
@end
/*
* Macros to handle unichar filesystem support.
*/
#if defined(_WIN32)
#define _CHMOD(A,B) _wchmod(A,B)
#define _CLOSEDIR(A) _wclosedir(A)
#define _OPENDIR(A) _wopendir(A)
#define _READDIR(A) _wreaddir(A)
#define _RENAME(A,B) (MoveFileExW(A,B,MOVEFILE_COPY_ALLOWED|MOVEFILE_REPLACE_EXISTING|MOVEFILE_WRITE_THROUGH)==0)?-1:0
#define _RMDIR(A) _wrmdir(A)
#define _STAT(A,B) _wstat(A,B)
#define _UTIME(A,B) _wutime(A,B)
#define _DIR _WDIR
#define _DIRENT _wdirent
#define _STATB _stat
#define _UTIMB _utimbuf
#define _NUL L'\0'
#else
#define _CHMOD(A,B) chmod(A,B)
#define _CLOSEDIR(A) closedir(A)
#define _OPENDIR(A) opendir(A)
#define _READDIR(A) readdir(A)
#define _RENAME(A,B) rename(A,B)
#define _RMDIR(A) rmdir(A)
#define _STAT(A,B) stat(A,B)
#define _UTIME(A,B) utime(A,B)
#define _DIR DIR
#define _DIRENT dirent
#define _STATB stat
#define _UTIMB utimbuf
#define _NUL '\0'
#endif
#define _CHAR GSNativeChar
#define _CCP const _CHAR*
/*
* GSAttrDictionary is a private NSDictionary subclass used to
* handle file attributes efficiently ... using lazy evaluation
* to ensure that we only do the minimum work necessary at any time.
*/
@interface GSAttrDictionary : NSDictionary
{
@public
struct _STATB statbuf;
_CHAR _path[0];
}
+ (NSDictionary*) attributesAt: (NSString *)path
traverseLink: (BOOL)traverse;
@end
static Class GSAttrDictionaryClass = 0;
/*
* We also need a special enumerator class to enumerate the dictionary.
*/
@interface GSAttrDictionaryEnumerator : NSEnumerator
{
NSDictionary *dictionary;
NSEnumerator *enumerator;
}
+ (NSEnumerator*) enumeratorFor: (NSDictionary*)d;
@end
@interface NSFileManager (PrivateMethods)
/* Copies the contents of source file to destination file. Assumes source
and destination are regular files or symbolic links. */
- (BOOL) _copyFile: (NSString*)source
toFile: (NSString*)destination
handler: (id)handler;
/* Recursively copies the contents of source directory to destination. */
- (BOOL) _copyPath: (NSString*)source
toPath: (NSString*)destination
handler: (id)handler;
/* Recursively links the contents of source directory to destination. */
- (BOOL) _linkPath: (NSString*)source
toPath: (NSString*)destination
handler: handler;
/* encapsulates the will Process check for existence of selector. */
- (void) _sendToHandler: (id) handler
willProcessPath: (NSString*) path;
/* methods to encapsulates setting up and calling the handler
in case of an error */
- (BOOL) _proceedAccordingToHandler: (id) handler
forError: (NSString*) error
inPath: (NSString*) path;
- (BOOL) _proceedAccordingToHandler: (id) handler
forError: (NSString*) error
inPath: (NSString*) path
fromPath: (NSString*) fromPath
toPath: (NSString*) toPath;
/* A convenience method to return an NSError object.
* If the _lastError message is set, this creates an NSError using
* that message in the NSCocoaErrorDomain, otherwise it used the
* most recent system error and the Posix error domain.
* The userInfo is set to contain NSLocalizedDescriptionKey for the
* message text, 'Path' if only the fromPath argument is specified,
* and 'FromPath' and 'ToPath' if both path argument are specified.
*/
- (NSError*) _errorFrom: (NSString*)fromPath to: (NSString*)toPath;
@end /* NSFileManager (PrivateMethods) */
/**
* This is the main class for platform-independent management of the local
* filesystem, which allows you to read and save files, create/list
* directories, and move or delete files and directories. In addition to
* simply listing directories, you may obtain an [NSDirectoryEnumerator]
* instance for recursive directory contents enumeration.
*/
@implementation NSFileManager
// Getting the default manager
static NSFileManager* defaultManager = nil;
static NSStringEncoding defaultEncoding;
+ (NSFileManager*) defaultManager
{
if (defaultManager == nil)
{
NS_DURING
{
[gnustep_global_lock lock];
if (defaultManager == nil)
{
defaultManager = [[self alloc] init];
}
[gnustep_global_lock unlock];
}
NS_HANDLER
{
// unlock then re-raise the exception
[gnustep_global_lock unlock];
[localException raise];
}
NS_ENDHANDLER
}
return defaultManager;
}
+ (void) initialize
{
defaultEncoding = [NSString defaultCStringEncoding];
GSAttrDictionaryClass = [GSAttrDictionary class];
}
- (void) dealloc
{
TEST_RELEASE(_lastError);
[super dealloc];
}
- (id<NSFileManagerDelegate>) delegate
{
return _delegate;
}
- (void) setDelegate: (id<NSFileManagerDelegate>)delegate
{
_delegate = delegate;
}
- (BOOL) changeCurrentDirectoryPath: (NSString*)path
{
static Class bundleClass = 0;
const _CHAR *lpath = [self fileSystemRepresentationWithPath: path];
/*
* On some systems the only way NSBundle can determine the path to the
* executable is by searching for it ... so it needs to know what was
* the current directory at launch time ... so we must make sure it is
* initialised before we change the current directory.
*/
if (bundleClass == 0)
{
bundleClass = [NSBundle class];
}
#if defined(_WIN32)
return SetCurrentDirectoryW(lpath) == TRUE ? YES : NO;
#else
return (chdir(lpath) == 0) ? YES : NO;
#endif
}
/**
* Change the attributes of the file at path to those specified.<br />
* Returns YES if all requested changes were made (or if the dictionary
* was nil or empty, so no changes were requested), NO otherwise.<br />
* On failure, some of the requested changes may have taken place.<br />
*/
- (BOOL) changeFileAttributes: (NSDictionary*)attributes atPath: (NSString*)path
{
NSDictionary *old;
const _CHAR *lpath = 0;
NSUInteger num;
NSString *str;
NSDate *date;
BOOL allOk = YES;
if (0 == [attributes count])
{
return YES;
}
old = [self fileAttributesAtPath: path traverseLink: YES];
lpath = [defaultManager fileSystemRepresentationWithPath: path];
#ifndef _WIN32
if (object_getClass(attributes) == GSAttrDictionaryClass)
{
num = ((GSAttrDictionary*)attributes)->statbuf.st_uid;
}
else
{
NSNumber *tmpNum = [attributes fileOwnerAccountID];
num = tmpNum ? [tmpNum unsignedLongValue] : NSNotFound;
}
if (num != NSNotFound && num != [[old fileOwnerAccountID] unsignedLongValue])
{
if (chown(lpath, num, -1) != 0)
{
allOk = NO;
str = [NSString stringWithFormat:
@"Unable to change NSFileOwnerAccountID to '%"PRIuPTR"' - %@",
num, [NSError _last]];
ASSIGN(_lastError, str);
}
}
else
{
if ((str = [attributes fileOwnerAccountName]) != nil
&& NO == [str isEqual: [old fileOwnerAccountName]])
{
BOOL ok = NO;
#ifdef HAVE_PWD_H
#if defined(HAVE_GETPWNAM_R)
struct passwd pw;
struct passwd *p;
char buf[BUFSIZ*10];
if (getpwnam_r([str cStringUsingEncoding: defaultEncoding],
&pw, buf, sizeof(buf), &p) == 0)
{
ok = (chown(lpath, pw.pw_uid, -1) == 0);
(void)chown(lpath, -1, pw.pw_gid);
}
#else
#if defined(HAVE_GETPWNAM)
struct passwd *pw;
[gnustep_global_lock lock];
pw = getpwnam([str cStringUsingEncoding: defaultEncoding]);
if (pw != 0)
{
ok = (chown(lpath, pw->pw_uid, -1) == 0);
(void)chown(lpath, -1, pw->pw_gid);
}
[gnustep_global_lock unlock];
#endif
#endif
#endif
if (ok == NO)
{
allOk = NO;
str = [NSString stringWithFormat:
@"Unable to change NSFileOwnerAccountName to '%@' - %@",
str, [NSError _last]];
ASSIGN(_lastError, str);
}
}
}
if (object_getClass(attributes) == GSAttrDictionaryClass)
{
num = ((GSAttrDictionary*)attributes)->statbuf.st_gid;
}
else
{
NSNumber *tmpNum = [attributes fileGroupOwnerAccountID];
num = tmpNum ? [tmpNum unsignedLongValue] : NSNotFound;
}
if (num != NSNotFound
&& num != [[old fileGroupOwnerAccountID] unsignedLongValue])
{
if (chown(lpath, -1, num) != 0)
{
allOk = NO;
str = [NSString stringWithFormat:
@"Unable to change NSFileGroupOwnerAccountID to '%"PRIuPTR"' - %@",
num, [NSError _last]];
ASSIGN(_lastError, str);
}
}
else if ((str = [attributes fileGroupOwnerAccountName]) != nil
&& NO == [str isEqual: [old fileGroupOwnerAccountName]])
{
BOOL ok = NO;
#ifdef HAVE_GRP_H
#ifdef HAVE_GETGRNAM_R
struct group gp;
struct group *p;
char buf[BUFSIZ*10];
if (getgrnam_r([str cStringUsingEncoding: defaultEncoding], &gp,
buf, sizeof(buf), &p) == 0)
{
if (chown(lpath, -1, gp.gr_gid) == 0)
ok = YES;
}
#else
#ifdef HAVE_GETGRNAM
struct group *gp;
[gnustep_global_lock lock];
gp = getgrnam([str cStringUsingEncoding: defaultEncoding]);
if (gp)
{
if (chown(lpath, -1, gp->gr_gid) == 0)
ok = YES;
}
[gnustep_global_lock unlock];
#endif
#endif
#endif
if (ok == NO)
{
allOk = NO;
str = [NSString stringWithFormat:
@"Unable to change NSFileGroupOwnerAccountName to '%@' - %@",
str, [NSError _last]];
ASSIGN(_lastError, str);
}
}
#endif /* _WIN32 */
num = [attributes filePosixPermissions];
if (num != NSNotFound && num != [old filePosixPermissions])
{
if (_CHMOD(lpath, num) != 0)
{
allOk = NO;
str = [NSString stringWithFormat:
@"Unable to change NSFilePosixPermissions to '%o' - %@",
(unsigned)num, [NSError _last]];
ASSIGN(_lastError, str);
}
}
date = [attributes fileCreationDate];
if (date != nil && NO == [date isEqual: [old fileCreationDate]])
{
BOOL ok = NO;
struct _STATB sb;
const _CHAR *lpath;
lpath = [self fileSystemRepresentationWithPath: path];
if (_STAT(lpath, &sb) != 0)
{
ok = NO;
}
#if defined(_WIN32)
else if (sb.st_mode & _S_IFDIR)
{
ok = YES; // Directories don't have creation times.
}
#endif
else
{
#if defined(_WIN32)
FILETIME ctime;
HANDLE fh;
ULONGLONG nanosecs = ((ULONGLONG)([date timeIntervalSince1970]*10000000)+116444736000000000ULL);
fh = CreateFileW(lpath, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL );
if (fh == INVALID_HANDLE_VALUE)
{
ok = NO;
}
else
{
ctime.dwLowDateTime = (DWORD) (nanosecs & 0xFFFFFFFF );
ctime.dwHighDateTime = (DWORD) (nanosecs >> 32 );
ok = SetFileTime(fh, &ctime, NULL, NULL);
CloseHandle(fh);
}
#else
NSTimeInterval ti = [date timeIntervalSince1970];
/* on Unix we try setting the creation date by setting the modification date earlier than the current one */
#if defined (HAVE_UTIMENSAT)
struct timespec ub[2];
ub[0].tv_sec = 0;
ub[0].tv_nsec = UTIME_OMIT; // we don't touch access time
ub[1].tv_sec = (time_t)trunc(ti);
ub[1].tv_nsec = (long)trunc((ti - trunc(ti)) * 1.0e9);
ok = (utimensat(AT_FDCWD, lpath, ub, 0) == 0);
#elif defined(_POSIX_VERSION)
struct _UTIMB ub;
ub.actime = sb.st_atime;
ub.modtime = ti;
ok = (_UTIME(lpath, &ub) == 0);
#else
time_t ub[2];
ub[0] = sb.st_atime;
ub[1] = ti;
ok = (_UTIME(lpath, ub) == 0);
#endif
#endif
}
if (ok == NO)
{
allOk = NO;
str = [NSString stringWithFormat:
@"Unable to change NSFileCreationDate to '%@' - %@",
date, [NSError _last]];
ASSIGN(_lastError, str);
}
}
date = [attributes fileModificationDate];
if (date != nil && NO == [date isEqual: [old fileModificationDate]])
{
BOOL ok = NO;
struct _STATB sb;
if (_STAT(lpath, &sb) != 0)
{
ok = NO;
}
#if defined(_WIN32)
else if (sb.st_mode & _S_IFDIR)
{
ok = YES; // Directories don't have modification times.
}
#endif
else
{
NSTimeInterval ti = [date timeIntervalSince1970];
#if defined (HAVE_UTIMENSAT)
struct timespec ub[2];
ub[0].tv_sec = 0;
ub[0].tv_nsec = UTIME_OMIT; // we don't touch access time
ub[1].tv_sec = (time_t)trunc(ti);
ub[1].tv_nsec = (long)trunc((ti - trunc(ti)) * 1.0e9);
ok = (utimensat(AT_FDCWD, lpath, ub, 0) == 0);
#elif defined(_WIN32) || defined(_POSIX_VERSION)
struct _UTIMB ub;
ub.actime = sb.st_atime;
ub.modtime = ti;
ok = (_UTIME(lpath, &ub) == 0);
#else
time_t ub[2];
ub[0] = sb.st_atime;
ub[1] = ti;
ok = (_UTIME(lpath, ub) == 0);
#endif
}
if (ok == NO)
{
allOk = NO;
str = [NSString stringWithFormat:
@"Unable to change NSFileModificationDate to '%@' - %@",
date, [NSError _last]];
ASSIGN(_lastError, str);
}
}
return allOk;
}
/**
* Returns an array of path components suitably modified for display
* to the end user. This modification may render the returned strings
* unusable for path manipulation, so you should work with two arrays ...
* one returned by this method (for display to the user), and a
* parallel one returned by [NSString-pathComponents] (for path
* manipulation).
*/
- (NSArray*) componentsToDisplayForPath: (NSString*)path
{
return [path pathComponents];
}
/**
* Reads the file at path an returns its contents as an NSData object.<br />
* If an error occurs or if path specifies a directory etc then nil is
* returned.
*/
- (NSData*) contentsAtPath: (NSString*)path
{
return [NSData dataWithContentsOfFile: path];
}
/**
* Returns YES if the contents of the file or directory at path1 are the same
* as those at path2.<br />
* If path1 and path2 are files, this is a simple comparison. If they are
* directories, the contents of the files in those subdirectories are
* compared recursively.<br />
* Symbolic links are not followed.<br />
* A comparison checks first file identity, then size, then content.
*/
- (BOOL) contentsEqualAtPath: (NSString*)path1 andPath: (NSString*)path2
{
NSDictionary *d1;
NSDictionary *d2;
NSString *t;
if ([path1 isEqual: path2])
return YES;
d1 = [self fileAttributesAtPath: path1 traverseLink: NO];
d2 = [self fileAttributesAtPath: path2 traverseLink: NO];
t = [d1 fileType];
if ([t isEqual: [d2 fileType]] == NO)
{
return NO;
}
if ([t isEqual: NSFileTypeRegular])
{
if ([d1 fileSize] == [d2 fileSize])
{
NSData *c1 = [NSData dataWithContentsOfFile: path1];
NSData *c2 = [NSData dataWithContentsOfFile: path2];
if ([c1 isEqual: c2])
{
return YES;
}
}
return NO;
}
else if ([t isEqual: NSFileTypeDirectory])
{
NSArray *a1 = [self directoryContentsAtPath: path1];
NSArray *a2 = [self directoryContentsAtPath: path2];
unsigned index, count = [a1 count];
BOOL ok = YES;
if ([a1 isEqual: a2] == NO)
{
return NO;
}
for (index = 0; ok == YES && index < count; index++)
{
NSString *n = [a1 objectAtIndex: index];
NSString *p1;
NSString *p2;
ENTER_POOL
p1 = [path1 stringByAppendingPathComponent: n];
p2 = [path2 stringByAppendingPathComponent: n];
d1 = [self fileAttributesAtPath: p1 traverseLink: NO];
d2 = [self fileAttributesAtPath: p2 traverseLink: NO];
t = [d1 fileType];
if ([t isEqual: [d2 fileType]] == NO)
{
ok = NO;
}
else if ([t isEqual: NSFileTypeDirectory]
|| [t isEqual: NSFileTypeRegular])
{
ok = [self contentsEqualAtPath: p1 andPath: p2];
}
LEAVE_POOL
}
return ok;
}
else
{
return YES;
}
}
- (NSArray*) contentsOfDirectoryAtURL: (NSURL*)url
includingPropertiesForKeys: (NSArray*)keys
options: (NSDirectoryEnumerationOptions)mask
error: (NSError **)error
{
NSArray *result;
NSDirectoryEnumerator *direnum;
NSString *path;
DESTROY(_lastError);
if (![[url scheme] isEqualToString: @"file"])
{
return nil;
}
path = [url path];
direnum = [[NSDirectoryEnumerator alloc]
initWithDirectoryPath: path
recurseIntoSubdirectories: NO
followSymlinks: NO
justContents: NO
for: self];
/* we make an array of NSURLs */
result = nil;
if (nil != direnum)
{
IMP nxtImp;
NSMutableArray *urlArray;
NSString *tempPath;
nxtImp = [direnum methodForSelector: @selector(nextObject)];
urlArray = [NSMutableArray arrayWithCapacity: 128];
while ((tempPath = (*nxtImp)(direnum, @selector(nextObject))) != nil)
{
NSURL *tempURL;
NSString *lastComponent;
tempURL = [NSURL fileURLWithPath: tempPath];
lastComponent = [tempPath lastPathComponent];
/* we purge files beginning with . */
if (!((mask & NSDirectoryEnumerationSkipsHiddenFiles)
&& [lastComponent hasPrefix: @"."]))
{
[urlArray addObject: tempURL];
}
}
RELEASE(direnum);
if ([urlArray count] > 0)
{
result = [NSArray arrayWithArray: urlArray];
}
}
if (error != NULL)
{
if (nil == result)
{
*error = [self _errorFrom: path to: nil];
}
}
return result;
}
- (NSURL *)URLForDirectory: (NSSearchPathDirectory)directory
inDomain: (NSSearchPathDomainMask)domain
appropriateForURL: (NSURL *)url
create: (BOOL)shouldCreate
error: (NSError **)error
{
NSURL *result = nil;
NSArray *urlArray = NSSearchPathForDirectoriesInDomains(directory, domain, YES);
// Find out the URL exists...
if ([urlArray count] > 0)
{
result = [NSURL URLWithString: [urlArray objectAtIndex: 0]];
}
if (directory == NSItemReplacementDirectory)
{
result = [NSURL URLWithString: NSTemporaryDirectory()];
}
if (![self fileExistsAtPath: [result absoluteString]])
{
// If we should created it, create it...
if (shouldCreate)
{
[self createDirectoryAtPath: [result absoluteString]
withIntermediateDirectories: YES
attributes: nil
error: error];
}
}
return result;
}
- (NSDirectoryEnumerator *)enumeratorAtURL: (NSURL *)url
includingPropertiesForKeys: (NSArray *)keys
options: (NSDirectoryEnumerationOptions)mask
errorHandler: (GSDirEnumErrorHandler)handler
{
NSDirectoryEnumerator *direnum;
NSString *path;
DESTROY(_lastError);
if (![[url scheme] isEqualToString: @"file"])
{
return nil;
}
path = [url path];
direnum = [[NSDirectoryEnumerator alloc]
initWithDirectoryPath: path
recurseIntoSubdirectories: !(mask & NSDirectoryEnumerationSkipsSubdirectoryDescendants)
followSymlinks: NO
justContents: NO
skipHidden: (mask & NSDirectoryEnumerationSkipsHiddenFiles)
errorHandler: handler
for: self];
return direnum;
}
- (NSArray*) contentsOfDirectoryAtPath: (NSString*)path error: (NSError**)error
{
NSArray *result;
DESTROY(_lastError);
result = [self directoryContentsAtPath: path];
if (error != NULL)
{
if (nil == result)
{
*error = [self _errorFrom: path to: nil];
}
}
return result;
}
/**
* Creates a new directory (and all intermediate directories if flag is YES).
* Creates only the last directory in the path if flag is NO.<br />
* The directory is created with the attributes specified, and any problem
* is returned in error.<br />
* Returns YES if the directory is created (or flag is YES and the directory
* already exists), NO on failure.
*/
- (BOOL) createDirectoryAtPath: (NSString *)path
withIntermediateDirectories: (BOOL)flag
attributes: (NSDictionary *)attributes
error: (NSError **)error
{
BOOL result = NO;
DESTROY(_lastError);
if (YES == flag)
{
NSEnumerator *paths = [[path pathComponents] objectEnumerator];
NSString *path = nil;
NSString *dir = [NSString string];
result = YES;
while (YES == result && (path = (NSString *)[paths nextObject]) != nil)
{
dir = [dir stringByAppendingPathComponent: path];
// create directory only if it doesn't exist
if (NO == [self fileExistsAtPath: dir])
{
result = [self createDirectoryAtPath: dir
attributes: attributes];
}
}
}
else
{
BOOL isDir;
if ([self fileExistsAtPath: [path stringByDeletingLastPathComponent]
isDirectory: &isDir] && isDir)
{
result = [self createDirectoryAtPath: path
attributes: attributes];
}
else
{
result = NO;
ASSIGN(_lastError, @"Could not create directory - intermediate path did not exist or was not a directory");
}
}
if (error != NULL)
{
if (NO == result)
{
*error = [self _errorFrom: path to: nil];