ObjFW  Diff

Differences From Artifact [344a7c8de0]:

To Artifact [bc7b129bce]:


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
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







-
-
-


-
-
-
-
-
-
-
-
-
-
-










-
-
-
+
-



-
-
-

-
-

-
-


-


-

-
+














-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-







 * file.
 */

#include "config.h"

#include <errno.h>

#ifdef HAVE_DIRENT_H
# include <dirent.h>
#endif
#include "unistd_wrapper.h"

#ifdef HAVE_SYS_STAT_H
# include <sys/stat.h>
#endif

#ifdef HAVE_PWD_H
# include <pwd.h>
#endif
#ifdef HAVE_GRP_H
# include <grp.h>
#endif

#import "OFArray.h"
#import "OFDate.h"
#import "OFDictionary.h"
#import "OFFile.h"
#import "OFFileManager.h"
#import "OFLocalization.h"
#import "OFNumber.h"
#import "OFString.h"
#import "OFSystemInfo.h"
#import "OFURL.h"

#ifdef OF_HAVE_THREADS
# import "OFMutex.h"
#import "OFURLHandler.h"
#endif

#import "OFChangeCurrentDirectoryPathFailedException.h"
#import "OFCopyItemFailedException.h"
#import "OFCreateDirectoryFailedException.h"
#import "OFCreateSymbolicLinkFailedException.h"
#import "OFInitializationFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFLinkFailedException.h"
#import "OFLockFailedException.h"
#import "OFMoveItemFailedException.h"
#import "OFNotImplementedException.h"
#import "OFOpenItemFailedException.h"
#import "OFOutOfMemoryException.h"
#import "OFOutOfRangeException.h"
#import "OFReadFailedException.h"
#import "OFRemoveItemFailedException.h"
#import "OFRetrieveItemAttributesFailedException.h"
#import "OFSetItemAttributesFailedException.h"
#import "OFUndefinedKeyException.h"
#import "OFUnlockFailedException.h"
#import "OFUnsupportedProtocolException.h"

#ifdef OF_WINDOWS
# include <windows.h>
# include <direct.h>
# include <ntdef.h>
#endif

#ifdef OF_MORPHOS
# define BOOL EXEC_BOOL
# include <proto/dos.h>
# include <proto/locale.h>
# undef BOOL
#endif

#if defined(OF_WINDOWS)
typedef struct __stat64 of_stat_t;
#elif defined(OF_MORPHOS)
typedef struct {
	of_offset_t st_size;
	mode_t st_mode;
	of_time_interval_t st_atime, st_mtime, st_ctime;
} of_stat_t;
#elif defined(OF_HAVE_OFF64_T)
typedef struct stat64 of_stat_t;
#else
typedef struct stat of_stat_t;
#endif

#ifndef S_ISLNK
# define S_ISLNK(s) 0
#endif

@interface OFFileManager_default: OFFileManager
@end

static OFFileManager *defaultManager;

const of_file_attribute_key_t of_file_attribute_key_size =
    @"of_file_attribute_key_size";
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
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







-
-
-
-
-
-
-
-
-
-
-











-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
















-
-
-
-



-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-







const of_file_type_t of_file_type_symbolic_link = @"of_file_type_symbolic_link";
const of_file_type_t of_file_type_fifo = @"of_file_type_fifo";
const of_file_type_t of_file_type_character_special =
    @"of_file_type_character_special";
const of_file_type_t of_file_type_block_special = @"of_file_type_block_special";
const of_file_type_t of_file_type_socket = @"of_file_type_socket";

#if defined(OF_HAVE_CHOWN) && defined(OF_HAVE_THREADS) && !defined(OF_MORPHOS)
static OFMutex *passwdMutex;
#endif
#if !defined(HAVE_READDIR_R) && defined(OF_HAVE_THREADS) && !defined(OF_WINDOWS)
static OFMutex *readdirMutex;
#endif

#ifdef OF_WINDOWS
static WINAPI BOOLEAN (*func_CreateSymbolicLinkW)(LPCWSTR, LPCWSTR, DWORD);
#endif

#ifdef OF_MORPHOS
static bool dirChanged = false;
static BPTR originalDirLock = 0;

OF_DESTRUCTOR()
{
	if (dirChanged)
		UnLock(CurrentDir(originalDirLock));
}
#endif

static int
of_stat(OFString *path, of_stat_t *buffer)
{
#if defined(OF_WINDOWS)
	return _wstat64([path UTF16String], buffer);
#elif defined(OF_MORPHOS)
	BPTR lock;
	struct FileInfoBlock fib;
	of_time_interval_t timeInterval;
	struct Locale *locale;

	if ((lock = Lock([path cStringWithEncoding: [OFLocalization encoding]],
	    SHARED_LOCK)) == 0) {
		switch (IoErr()) {
		case ERROR_OBJECT_IN_USE:
		case ERROR_DISK_NOT_VALIDATED:
			errno = EBUSY;
			break;
		case ERROR_OBJECT_NOT_FOUND:
			errno = ENOENT;
			break;
		default:
			errno = 0;
			break;
		}

		return -1;
	}

	if (!Examine64(lock, &fib, TAG_DONE)) {
		UnLock(lock);

		errno = 0;
		return -1;
	}

	UnLock(lock);

	buffer->st_size = fib.fib_Size64;
	buffer->st_mode = (fib.fib_DirEntryType > 0 ? S_IFDIR : S_IFREG);

	timeInterval = 252460800;	/* 1978-01-01 */

	locale = OpenLocale(NULL);
	/*
	 * FIXME: This does not take DST into account. But unfortunately, there
	 * is no way to figure out if DST was in effect when the file was
	 * modified.
	 */
	timeInterval += locale->loc_GMTOffset * 60.0;
	CloseLocale(locale);

	timeInterval += fib.fib_Date.ds_Days * 86400.0;
	timeInterval += fib.fib_Date.ds_Minute * 60.0;
	timeInterval +=
	    fib.fib_Date.ds_Tick / (of_time_interval_t)TICKS_PER_SECOND;

	buffer->st_atime = buffer->st_mtime = buffer->st_ctime = timeInterval;

	return 0;
#elif defined(OF_HAVE_OFF64_T)
	return stat64([path cStringWithEncoding: [OFLocalization encoding]],
	    buffer);
#else
	return stat([path cStringWithEncoding: [OFLocalization encoding]],
	    buffer);
#endif
}

static int
of_lstat(OFString *path, of_stat_t *buffer)
{
#if defined(HAVE_LSTAT) && !defined(OF_WINDOWS) && !defined(OF_MORPHOS)
# ifdef OF_HAVE_OFF64_T
	return lstat64([path cStringWithEncoding: [OFLocalization encoding]],
	    buffer);
# else
	return lstat([path cStringWithEncoding: [OFLocalization encoding]],
	    buffer);
# endif
#else
	return of_stat(path, buffer);
#endif
}

static void
setTypeAttribute(of_mutable_file_attributes_t attributes, of_stat_t *s)
{
	if (S_ISREG(s->st_mode))
		[attributes setObject: of_file_type_regular
			       forKey: of_file_attribute_key_type];
	else if (S_ISDIR(s->st_mode))
		[attributes setObject: of_file_type_directory
			       forKey: of_file_attribute_key_type];
#ifdef S_ISLNK
	else if (S_ISLNK(s->st_mode))
		[attributes setObject: of_file_type_symbolic_link
			       forKey: of_file_attribute_key_type];
#endif
#ifdef S_ISFIFO
	else if (S_ISFIFO(s->st_mode))
		[attributes setObject: of_file_type_fifo
			       forKey: of_file_attribute_key_type];
#endif
#ifdef S_ISCHR
	else if (S_ISCHR(s->st_mode))
		[attributes setObject: of_file_type_character_special
			       forKey: of_file_attribute_key_type];
#endif
#ifdef S_ISBLK
	else if (S_ISBLK(s->st_mode))
		[attributes setObject: of_file_type_block_special
			       forKey: of_file_attribute_key_type];
#endif
#ifdef S_ISSOCK
	else if (S_ISSOCK(s->st_mode))
		[attributes setObject: of_file_type_socket
			       forKey: of_file_attribute_key_type];
#endif
}

static void
setDateAttributes(of_mutable_file_attributes_t attributes, of_stat_t *s)
{
	/* FIXME: We could be more precise on some OSes */
	[attributes
	    setObject: [OFDate dateWithTimeIntervalSince1970: s->st_atime]
	       forKey: of_file_attribute_key_last_access_date];
	[attributes
	    setObject: [OFDate dateWithTimeIntervalSince1970: s->st_mtime]
	       forKey: of_file_attribute_key_modification_date];
	[attributes
	    setObject: [OFDate dateWithTimeIntervalSince1970: s->st_ctime]
	       forKey: of_file_attribute_key_status_change_date];
}

static void
setOwnerAndGroupAttributes(of_mutable_file_attributes_t attributes,
    of_stat_t *s)
{
#ifdef OF_FILE_MANAGER_SUPPORTS_OWNER
	[attributes setObject: [NSNumber numberWithUInt16: s->st_uid]
		       forKey: of_file_attribute_key_posix_uid];
	[attributes setObject: [NSNumber numberWithUInt16: s->st_gid]
		       forKey: of_file_attribute_key_posix_gid];

# ifdef OF_HAVE_THREADS
	[passwdMutex lock];
	@try {
# endif
		of_string_encoding_t encoding = [OFLocalization encoding];
		struct passwd *passwd = getpwuid(s->st_uid);
		struct group *group_ = getgrgid(s->st_gid);

		if (passwd != NULL) {
			OFString *owner = [OFString
			    stringWithCString: passwd->pw_name
				     encoding: encoding];

			[attributes setObject: owner
				       forKey: of_file_attribute_key_owner];
		}

		if (group_ != NULL) {
			OFString *group = [OFString
			    stringWithCString: group_->gr_name
				     encoding: encoding];

			[attributes setObject: group
				       forKey: of_file_attribute_key_group];
		}
# ifdef OF_HAVE_THREADS
	} @finally {
		[passwdMutex unlock];
	}
# endif
#endif
}

static void
setSymbolicLinkDestinationAttribute(of_mutable_file_attributes_t attributes,
    of_stat_t *s, OFString *path)
{
#ifdef OF_FILE_MANAGER_SUPPORTS_SYMLINKS
# ifndef OF_WINDOWS
	if (S_ISLNK(s->st_mode)) {
		of_string_encoding_t encoding = [OFLocalization encoding];
		char destinationC[PATH_MAX];
		ssize_t length;
		OFString *destination;
		of_file_attribute_key_t key;

		length = readlink([path cStringWithEncoding: encoding],
		    destinationC, PATH_MAX);

		if (length < 0)
			@throw [OFRetrieveItemAttributesFailedException
			    exceptionWithPath: path
					errNo: errno];

		destination = [OFString stringWithCString: destinationC
						 encoding: encoding
						   length: length];

		key = of_file_attribute_key_symbolic_link_destination;
		[attributes setObject: destination
			       forKey: key];
	}
# else
	WIN32_FIND_DATAW data;

	if (func_CreateSymbolicLinkW != NULL &&
	    FindFirstFileW([path UTF16String], &data) &&
	    (data.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
	    data.dwReserved0 == IO_REPARSE_TAG_SYMLINK) {
		HANDLE handle;
		OFString *destination;

		if ((handle = CreateFileW([path UTF16String], 0,
		    (FILE_SHARE_READ | FILE_SHARE_WRITE), NULL, OPEN_EXISTING,
		    FILE_FLAG_OPEN_REPARSE_POINT, NULL)) ==
		    INVALID_HANDLE_VALUE)
			@throw [OFRetrieveItemAttributesFailedException
			    exceptionWithPath: path
					errNo: 0];

		@try {
			union {
				char bytes[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
				REPARSE_DATA_BUFFER data;
			} buffer;
			DWORD size;
			wchar_t *tmp;
			of_file_attribute_key_t key;

			if (!DeviceIoControl(handle, FSCTL_GET_REPARSE_POINT,
			    NULL, 0, buffer.bytes,
			    MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &size, NULL))
				@throw [OFRetrieveItemAttributesFailedException
				    exceptionWithPath: path
						errNo: 0];

			if (buffer.data.ReparseTag != IO_REPARSE_TAG_SYMLINK)
				@throw [OFRetrieveItemAttributesFailedException
				    exceptionWithPath: path
						errNo: 0];

#  define slrb buffer.data.SymbolicLinkReparseBuffer
			tmp = slrb.PathBuffer +
			    (slrb.SubstituteNameOffset / sizeof(wchar_t));

			destination = [OFString
			    stringWithUTF16String: tmp
					   length: slrb.SubstituteNameLength /
						   sizeof(wchar_t)];

			[attributes setObject: of_file_type_symbolic_link
				       forKey: of_file_attribute_key_type];
			key = of_file_attribute_key_symbolic_link_destination;
			[attributes setObject: destination
				       forKey: key];
#  undef slrb
		} @finally {
			CloseHandle(handle);
		}
	}
# endif
#endif
}

static id
attributeForKeyOrException(of_file_attributes_t attributes,
    of_file_attribute_key_t key)
{
	id object = [attributes objectForKey: key];

	if (object == nil)
		@throw [OFUndefinedKeyException exceptionWithObject: attributes
								key: key];

	return object;
}

@implementation OFFileManager
+ (void)initialize
{
#ifdef OF_WINDOWS
	HMODULE module;
#endif

	if (self != [OFFileManager class])
		return;

	/*
	 * Make sure OFFile is initialized.
	 * On some systems, this is needed to initialize the file system driver.
	 */
	[OFFile class];

#if defined(OF_HAVE_CHOWN) && defined(OF_HAVE_THREADS)
	passwdMutex = [[OFMutex alloc] init];
#endif
#if !defined(HAVE_READDIR_R) && !defined(OF_WINDOWS) && defined(OF_HAVE_THREADS)
	readdirMutex = [[OFMutex alloc] init];
#endif

#ifdef OF_WINDOWS
	if ((module = LoadLibrary("kernel32.dll")) != NULL)
		func_CreateSymbolicLinkW =
		    (WINAPI BOOLEAN (*)(LPCWSTR, LPCWSTR, DWORD))
		    GetProcAddress(module, "CreateSymbolicLinkW");
#endif

	defaultManager = [[OFFileManager_default alloc] init];
}

+ (OFFileManager *)defaultManager
{
	return defaultManager;
}
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
1001

1002
1003
1004
1005
1006
1007
1008
1009
1010

1011
1012
1013
1014
1015
1016
1017
1018
1019

1020
1021
1022
1023
1024
1025
1026
1027

1028
1029
1030
1031
1032
1033
1034

1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047

1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078

1079
1080
1081
1082
1083
1084
1085
1086
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








-
+

-
+
-
-

-
+


-
-
-
-
-
+
-
-
+

-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+


-
+


-
-
+
+
+








-
-
-
+
+

-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-


-
-
+
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-

-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-





-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+


-
-
-
-
-
+
+
+
+
+
+
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
+
+
-
-
-
-
+
-
-
+
+


-
-
+
+
+
-
-
-
+
-
-
+
-
-
-
+
-
-
-
+
-
-
+
-
-
-
+


-
+
-
+

-
+


-
+


+
-
+
+






-
+

-
+

-
+


-
-
+
+

-
+


-
+
+


-
-
+
-
-
+
-
-
-
+
-
-
-
+
+


-
-
-
+
+
-
-
+
-
-
-
+
-
-
-
+
+

+
-
+
-
-
-
-
+
+
+
-
-
-
-
+
+
-
-
-
-
+
+
-
-
+
-
-
-
+
-
-
-
-
+
+

-
-
-
-
+
+

-
-
-
-
+
+
+
+
-
-
-
+
+
-
+





-
-
-
-
-
-
-
-
-
-
-
-
+

-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+




-
+

-
+

-
+


-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
+
-
-
-
-
-
-
-
-
+
-
-
-

-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-








	[URL makeImmutable];

	objc_autoreleasePoolPop(pool);

	return URL;
}

- (of_file_attributes_t)attributesOfItemAtPath: (OFString *)path
- (of_file_attributes_t)attributesOfItemAtURL: (OFURL *)URL
{
	of_mutable_file_attributes_t ret = [OFMutableDictionary dictionary];
	OF_KINDOF(OFURLHandler *) URLHandler;
	void *pool = objc_autoreleasePoolPush();
	of_stat_t s;

	if (path == nil)
	if (URL == nil)
		@throw [OFInvalidArgumentException exception];

	if (of_lstat(path, &s) == -1)
		@throw [OFRetrieveItemAttributesFailedException
		    exceptionWithPath: path
				errNo: errno];

	if ((URLHandler = [OFURLHandler handlerForURL: URL]) == nil)
	if (s.st_size < 0)
		@throw [OFOutOfRangeException exception];
		@throw [OFUnsupportedProtocolException exceptionWithURL: URL];

	[ret setObject: [NSNumber numberWithUIntMax: s.st_size]
		forKey: of_file_attribute_key_size];

	setTypeAttribute(ret, &s);

	[ret setObject: [NSNumber numberWithUInt16: s.st_mode & 07777]
		forKey: of_file_attribute_key_posix_permissions];

	setOwnerAndGroupAttributes(ret, &s);
	setDateAttributes(ret, &s);
	setSymbolicLinkDestinationAttribute(ret, &s, path);

	objc_autoreleasePoolPop(pool);

	return ret;
	return [URLHandler attributesOfItemAtURL: URL];
}

- (of_file_attributes_t)attributesOfItemAtURL: (OFURL *)URL
- (of_file_attributes_t)attributesOfItemAtPath: (OFString *)path
{
	void *pool = objc_autoreleasePoolPush();
	of_file_attributes_t ret = [self attributesOfItemAtPath:
	    [URL fileSystemRepresentation]];
	of_file_attributes_t ret;

	ret = [self attributesOfItemAtURL: [OFURL fileURLWithPath: path]];

	[ret retain];

	objc_autoreleasePoolPop(pool);

	return [ret autorelease];
}

- (void)of_setPOSIXPermissions: (OFNumber *)permissions
		  ofItemAtPath: (OFString *)path
		    attributes: (of_file_attributes_t)attributes
- (void)setAttributes: (of_file_attributes_t)attributes
	  ofItemAtURL: (OFURL *)URL
{
#ifdef OF_FILE_MANAGER_SUPPORTS_PERMISSIONS
	uint16_t mode = [permissions uInt16Value] & 0777;

	OF_KINDOF(OFURLHandler *) URLHandler;
# ifndef OF_WINDOWS
	if (chmod([path cStringWithEncoding: [OFLocalization encoding]],
	    mode) != 0)
# else
	if (_wchmod([path UTF16String], mode) != 0)
# endif
		@throw [OFSetItemAttributesFailedException
		    exceptionWithPath: path
			   attributes: attributes
		      failedAttribute: of_file_attribute_key_posix_permissions
				errNo: errno];
#else
	OF_UNRECOGNIZED_SELECTOR
#endif
}


	if (URL == nil)
- (void)of_setOwner: (OFString *)owner
	   andGroup: (OFString *)group
       ofItemAtPath: (OFString *)path
       attributeKey: (of_file_attribute_key_t)attributeKey
	 attributes: (of_file_attributes_t)attributes
{
#ifdef OF_FILE_MANAGER_SUPPORTS_OWNER
	uid_t uid = -1;
	gid_t gid = -1;
	of_string_encoding_t encoding;

	if (owner == nil && group == nil)
		@throw [OFInvalidArgumentException exception];

	encoding = [OFLocalization encoding];

	if ((URLHandler = [OFURLHandler handlerForURL: URL]) == nil)
# ifdef OF_HAVE_THREADS
	[passwdMutex lock];
	@try {
# endif
		if (owner != nil) {
			struct passwd *passwd;

		@throw [OFUnsupportedProtocolException exceptionWithURL: URL];
			if ((passwd = getpwnam([owner
			    cStringWithEncoding: encoding])) == NULL)
				@throw [OFSetItemAttributesFailedException
				    exceptionWithPath: path
					   attributes: attributes
				      failedAttribute: attributeKey
						errNo: errno];

			uid = passwd->pw_uid;
		}

		if (group != nil) {
			struct group *group_;

			if ((group_ = getgrnam([group
			    cStringWithEncoding: encoding])) == NULL)
				@throw [OFSetItemAttributesFailedException
				    exceptionWithPath: path
					   attributes: attributes
	[URLHandler setAttributes: attributes
				      failedAttribute: attributeKey
						errNo: errno];

		      ofItemAtURL: URL];
			gid = group_->gr_gid;
		}
# ifdef OF_HAVE_THREADS
	} @finally {
		[passwdMutex unlock];
	}
# endif

	if (chown([path cStringWithEncoding: encoding], uid, gid) != 0)
		@throw [OFSetItemAttributesFailedException
		    exceptionWithPath: path
			   attributes: attributes
		      failedAttribute: attributeKey
				errNo: errno];
#else
	OF_UNRECOGNIZED_SELECTOR
#endif
}

- (void)setAttributes: (of_file_attributes_t)attributes
	 ofItemAtPath: (OFString *)path
{
	void *pool;
	OFEnumerator OF_GENERIC(of_file_attribute_key_t) *keyEnumerator;
	OFEnumerator *objectEnumerator;
	of_file_attribute_key_t key;
	id object;

	if (path == nil)
	void *pool = objc_autoreleasePoolPush();

	[self setAttributes: attributes
		ofItemAtURL: [OFURL fileURLWithPath: path]];

	objc_autoreleasePoolPop(pool);
}

- (bool)fileExistsAtURL: (OFURL *)URL
{
	OF_KINDOF(OFURLHandler *) URLHandler;

	if (URL == nil)
		@throw [OFInvalidArgumentException exception];

	pool = objc_autoreleasePoolPush();
	keyEnumerator = [attributes keyEnumerator];
	objectEnumerator = [attributes objectEnumerator];

	while ((key = [keyEnumerator nextObject]) != nil &&
	if ((URLHandler = [OFURLHandler handlerForURL: URL]) == nil)
		@throw [OFUnsupportedProtocolException exceptionWithURL: URL];

	return [URLHandler fileExistsAtURL: URL];
}

	    (object = [objectEnumerator nextObject]) != nil) {
		if ([key isEqual: of_file_attribute_key_posix_permissions])
			[self of_setPOSIXPermissions: object
					ofItemAtPath: path
- (bool)fileExistsAtPath: (OFString *)path
					  attributes: attributes];
		else if ([key isEqual: of_file_attribute_key_owner])
			[self of_setOwner: object
				 andGroup: nil
			     ofItemAtPath: path
			     attributeKey: key
			       attributes: attributes];
		else if ([key isEqual: of_file_attribute_key_group])
			[self of_setOwner: nil
				 andGroup: object
{
	void *pool = objc_autoreleasePoolPush();
			     ofItemAtPath: path
			     attributeKey: key
			       attributes: attributes];
		else
	bool ret;
			@throw [OFInvalidArgumentException exception];
	}

	ret = [self fileExistsAtURL: [OFURL fileURLWithPath: path]];

	objc_autoreleasePoolPop(pool);
}


	return ret;
}
- (void)setAttributes: (of_file_attributes_t)attributes
	  ofItemAtURL: (OFURL *)URL
{

	void *pool = objc_autoreleasePoolPush();

- (bool)directoryExistsAtURL: (OFURL *)URL
	[self setAttributes: attributes
	       ofItemAtPath: [URL fileSystemRepresentation]];

{
	objc_autoreleasePoolPop(pool);
}

	OF_KINDOF(OFURLHandler *) URLHandler;
- (bool)fileExistsAtPath: (OFString *)path
{

	of_stat_t s;

	if (path == nil)
	if (URL == nil)
		@throw [OFInvalidArgumentException exception];

	if (of_stat(path, &s) == -1)
	if ((URLHandler = [OFURLHandler handlerForURL: URL]) == nil)
		return false;
		@throw [OFUnsupportedProtocolException exceptionWithURL: URL];

	return S_ISREG(s.st_mode);
	return [URLHandler directoryExistsAtURL: URL];
}

- (bool)fileExistsAtURL: (OFURL *)URL
- (bool)directoryExistsAtPath: (OFString *)path
{
	void *pool = objc_autoreleasePoolPush();
	bool ret;
	bool ret = [self fileExistsAtPath: [URL fileSystemRepresentation]];

	ret = [self directoryExistsAtURL: [OFURL fileURLWithPath: path]];

	objc_autoreleasePoolPop(pool);

	return ret;
}

- (bool)directoryExistsAtPath: (OFString *)path
- (void)createDirectoryAtURL: (OFURL *)URL
{
	of_stat_t s;
	OF_KINDOF(OFURLHandler *) URLHandler;

	if (path == nil)
	if (URL == nil)
		@throw [OFInvalidArgumentException exception];

	if (of_stat(path, &s) == -1)
		return false;
	if ((URLHandler = [OFURLHandler handlerForURL: URL]) == nil)
		@throw [OFUnsupportedProtocolException exceptionWithURL: URL];

	return S_ISDIR(s.st_mode);
	[URLHandler createDirectoryAtURL: URL];
}

- (bool)directoryExistsAtURL: (OFURL *)URL
- (void)createDirectoryAtURL: (OFURL *)URL_
	       createParents: (bool)createParents
{
	void *pool = objc_autoreleasePoolPush();
	bool ret = [self directoryExistsAtPath: [URL fileSystemRepresentation]];

	OFMutableURL *URL = [[URL_ mutableCopy] autorelease];
	objc_autoreleasePoolPop(pool);

	OFArray OF_GENERIC(OFString *) *components;
	return ret;
}

	OFString *currentPath = nil;
- (void)createDirectoryAtPath: (OFString *)path
{
	if (path == nil)

	if (URL == nil)
		@throw [OFInvalidArgumentException exception];

#if defined(OF_WINDOWS)
	if (_wmkdir([path UTF16String]) != 0)
		@throw [OFCreateDirectoryFailedException
	if (!createParents) {
		[self createDirectoryAtURL: URL];
		    exceptionWithPath: path
				errNo: errno];
		return;
#elif defined(OF_MORPHOS)
	BPTR lock;

	}
	if ((lock = CreateDir(
	    [path cStringWithEncoding: [OFLocalization encoding]])) == 0) {
		int errNo;

	components = [[URL URLEncodedPath] componentsSeparatedByString: @"/"];

	for (OFString *component in components) {
		switch (IoErr()) {
		if (currentPath != nil)
		case ERROR_NO_FREE_STORE:
		case ERROR_DISK_FULL:
			errNo = ENOSPC;
			break;
			currentPath = [currentPath
			    stringByAppendingFormat: @"/%@", component];
		else
		case ERROR_OBJECT_IN_USE:
		case ERROR_DISK_NOT_VALIDATED:
			errNo = EBUSY;
			break;
			currentPath = component;

		case ERROR_OBJECT_EXISTS:
			errNo = EEXIST;
			break;
		case ERROR_OBJECT_NOT_FOUND:
		[URL setURLEncodedPath: currentPath];

			errNo = ENOENT;
			break;
		if ([currentPath length] > 0 &&
		case ERROR_DISK_WRITE_PROTECTED:
			errNo = EROFS;
			break;
		    ![self directoryExistsAtURL: URL])
		default:
			errNo = 0;
			break;
		}
			[self createDirectoryAtURL: URL];
	}

		@throw [OFCreateDirectoryFailedException
		    exceptionWithPath: path
				errNo: errNo];
	}
	objc_autoreleasePoolPop(pool);
}

	UnLock(lock);
#else
	if (mkdir([path cStringWithEncoding: [OFLocalization encoding]],
	    0777) != 0)
- (void)createDirectoryAtPath: (OFString *)path
{
	void *pool = objc_autoreleasePoolPush();

		@throw [OFCreateDirectoryFailedException
		    exceptionWithPath: path
				errNo: errno];
	[self createDirectoryAtURL: [OFURL fileURLWithPath: path]];

#endif
	objc_autoreleasePoolPop(pool);
}

- (void)createDirectoryAtPath: (OFString *)path
		createParents: (bool)createParents
{
	OFString *currentPath = nil;

	if (!createParents) {
		[self createDirectoryAtPath: path];
		return;
	}

	if (path == nil)
		@throw [OFInvalidArgumentException exception];

	for (OFString *component in [path pathComponents]) {
		void *pool = objc_autoreleasePoolPush();
	void *pool = objc_autoreleasePoolPush();

		if (currentPath != nil)
			currentPath = [currentPath
			    stringByAppendingPathComponent: component];
		else
			currentPath = component;

		if ([currentPath length] > 0 &&
		    ![self directoryExistsAtPath: currentPath])
			[self createDirectoryAtPath: currentPath];
	[self createDirectoryAtURL: [OFURL fileURLWithPath: path]

		[currentPath retain];

		objc_autoreleasePoolPop(pool);

		[currentPath autorelease];
	}
}

- (void)createDirectoryAtURL: (OFURL *)URL
{
	void *pool = objc_autoreleasePoolPush();

	[self createDirectoryAtPath: [URL fileSystemRepresentation]];

	objc_autoreleasePoolPop(pool);
}

- (void)createDirectoryAtURL: (OFURL *)URL
	       createParents: (bool)createParents
{
	void *pool = objc_autoreleasePoolPush();

	[self createDirectoryAtPath: [URL fileSystemRepresentation]
		      createParents: createParents];
		     createParents: createParents];

	objc_autoreleasePoolPop(pool);
}

- (OFArray *)contentsOfDirectoryAtPath: (OFString *)path
- (OFArray OF_GENERIC(OFString *) *)contentsOfDirectoryAtURL: (OFURL *)URL
{
	OFMutableArray *files;
	OF_KINDOF(OFURLHandler *) URLHandler;

	if (path == nil)
	if (URL == nil)
		@throw [OFInvalidArgumentException exception];

	files = [OFMutableArray array];

	if ((URLHandler = [OFURLHandler handlerForURL: URL]) == nil)
#if defined(OF_WINDOWS)
	void *pool = objc_autoreleasePoolPush();
	HANDLE handle;
	WIN32_FIND_DATAW fd;

	path = [path stringByAppendingString: @"\\*"];

	if ((handle = FindFirstFileW([path UTF16String],
	    &fd)) == INVALID_HANDLE_VALUE) {
		int errNo = 0;

		if (GetLastError() == ERROR_FILE_NOT_FOUND)
			errNo = ENOENT;

		@throw [OFOpenItemFailedException exceptionWithPath: path
		@throw [OFUnsupportedProtocolException exceptionWithURL: URL];
							       mode: nil
							      errNo: errNo];
	}

	@try {
		do {
			OFString *file;

			if (!wcscmp(fd.cFileName, L".") ||
			    !wcscmp(fd.cFileName, L".."))
				continue;

			file = [OFString stringWithUTF16String: fd.cFileName];
			@try {
				[files addObject: file];
			} @finally {
				[file release];
			}
		} while (FindNextFileW(handle, &fd));

		if (GetLastError() != ERROR_NO_MORE_FILES)
			@throw [OFReadFailedException exceptionWithObject: self
							  requestedLength: 0
								    errNo: EIO];
	} @finally {
		FindClose(handle);
	}

	objc_autoreleasePoolPop(pool);
#elif defined(OF_MORPHOS)
	of_string_encoding_t encoding = [OFLocalization encoding];
	BPTR lock;
	struct FileInfoBlock fib;

	if ((lock = Lock([path cStringWithEncoding: encoding],
	    SHARED_LOCK)) == 0) {
		int errNo;

		switch (IoErr()) {
		case ERROR_OBJECT_IN_USE:
		case ERROR_DISK_NOT_VALIDATED:
			errNo = EBUSY;
			break;
		case ERROR_OBJECT_NOT_FOUND:
			errNo = ENOENT;
			break;
		default:
			errNo = 0;
			break;
		}

		@throw [OFOpenItemFailedException exceptionWithPath: path
							       mode: nil
							      errNo: errNo];
	}

	@try {
		if (!Examine(lock, &fib))
			@throw [OFOpenItemFailedException
			    exceptionWithPath: path
					 mode: nil
					errNo: 0];

		while (ExNext(lock, &fib)) {
			OFString *file;

			file = [[OFString alloc]
			    initWithCString: fib.fib_FileName
				   encoding: encoding];
			@try {
				[files addObject: file];
			} @finally {
				[file release];
			}

		}

	return [URLHandler contentsOfDirectoryAtURL: URL];
		if (IoErr() != ERROR_NO_MORE_ENTRIES)
			@throw [OFReadFailedException
			    exceptionWithObject: self
				requestedLength: 0
					  errNo: EIO];
	} @finally {
		UnLock(lock);
	}
}
#else
	of_string_encoding_t encoding = [OFLocalization encoding];
	DIR *dir;

	if ((dir = opendir([path cStringWithEncoding: encoding])) == NULL)
		@throw [OFOpenItemFailedException exceptionWithPath: path
							       mode: nil
							      errNo: errno];

- (OFArray OF_GENERIC(OFString *) *)contentsOfDirectoryAtPath: (OFString *)path
# if !defined(HAVE_READDIR_R) && defined(OF_HAVE_THREADS)
	@try {
		[readdirMutex lock];
	} @catch (id e) {
		closedir(dir);
		@throw e;
	}
# endif

{
	@try {
		for (;;) {
			struct dirent *dirent;
# ifdef HAVE_READDIR_R
			struct dirent buffer;
# endif
			OFString *file;

	void *pool = objc_autoreleasePoolPush();
# ifdef HAVE_READDIR_R
			if (readdir_r(dir, &buffer, &dirent) != 0)
				@throw [OFReadFailedException
				    exceptionWithObject: self
					requestedLength: 0
						  errNo: errno];

	OFArray OF_GENERIC(OFString *) *ret;
			if (dirent == NULL)
				break;
# else
			errno = 0;
			if ((dirent = readdir(dir)) == NULL) {
				if (errno == 0)
					break;
				else
					@throw [OFReadFailedException
					    exceptionWithObject: self
						requestedLength: 0
							  errNo: errno];
			}

# endif

			if (strcmp(dirent->d_name, ".") == 0 ||
			    strcmp(dirent->d_name, "..") == 0)
				continue;

			file = [[OFString alloc] initWithCString: dirent->d_name
							encoding: encoding];
			@try {
				[files addObject: file];
			} @finally {
				[file release];
			}
		}
	} @finally {
		closedir(dir);
# if !defined(HAVE_READDIR_R) && defined(OF_HAVE_THREADS)
		[readdirMutex unlock];
# endif
	}
#endif

	[files makeImmutable];

	return files;
}

- (OFArray *)contentsOfDirectoryAtURL: (OFURL *)URL
{
	void *pool = objc_autoreleasePoolPush();
	OFArray *ret = [self contentsOfDirectoryAtPath:
	ret = [self contentsOfDirectoryAtURL: [OFURL fileURLWithPath: path]];
	    [URL fileSystemRepresentation]];

	[ret retain];

	objc_autoreleasePoolPop(pool);

	return [ret autorelease];
}
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332

1333
1334
1335
1336
1337
1338
1339
634
635
636
637
638
639
640


641
642
643

644
645
646
647
648
649
650
651







-
-



-
+








	objc_autoreleasePoolPop(pool);
}

- (void)moveItemAtPath: (OFString *)source
		toPath: (OFString *)destination
{
	of_stat_t s;

	if (source == nil || destination == nil)
		@throw [OFInvalidArgumentException exception];

	if (of_lstat(destination, &s) == 0)
	if ([self fileExistsAtPath: destination])
		@throw [OFMoveItemFailedException
		    exceptionWithSourcePath: source
			    destinationPath: destination
				      errNo: EEXIST];

#if defined(OF_WINDOWS)
	void *pool = objc_autoreleasePoolPush();
1424
1425
1426
1427
1428
1429
1430
1431

1432
1433

1434
1435
1436

1437
1438
1439
1440

1441
1442

1443
1444
1445
1446
1447

1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462

1463
1464

1465
1466
1467
1468
1469

1470
1471
1472
1473



1474
1475
1476
1477
1478
1479
1480
1481
1482
1483

1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530


1531
1532

1533


1534












1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548

1549
1550
1551
1552

1553
1554
1555
1556
1557

1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590


1591
1592


1593
1594

1595
1596
1597

1598
1599

1600

1601
1602
1603
1604

1605
1606

1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624

1625
1626


1627
1628
1629
1630
1631
1632


1633
1634
1635
1636
1637
1638

1639
1640
1641
1642
1643
1644
1645
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







-
+

-
+
-

-
+


-
-
+
-
-
+
-

-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
+
-
-
-
-
-
+
-
-
-
-
+
+
+
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-




-
+
+


+

+
+
-
+
+
+
+
+
+
+
+
+
+
+
+








-
-
-
-
-
-
+

-
-
-
+
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-





-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+

-
+
+

-
+


-
+

-
+
-
+

-
-
-
+
-
-
+
-
-
-
-
-

-
-
-
-
-
-
-
-


-

+
-
-
+
+

-


-
-
+
+


-
-
-

+








	[self moveItemAtPath: [source fileSystemRepresentation]
		      toPath: [destination fileSystemRepresentation]];

	objc_autoreleasePoolPop(pool);
}

- (void)removeItemAtPath: (OFString *)path
- (void)removeItemAtURL: (OFURL *)URL
{
	void *pool;
	OF_KINDOF(OFURLHandler *) URLHandler;
	of_stat_t s;

	if (path == nil)
	if (URL == nil)
		@throw [OFInvalidArgumentException exception];

	pool = objc_autoreleasePoolPush();

	if ((URLHandler = [OFURLHandler handlerForURL: URL]) == nil)
	if (of_lstat(path, &s) != 0)
		@throw [OFRemoveItemFailedException exceptionWithPath: path
		@throw [OFUnsupportedProtocolException exceptionWithURL: URL];
								errNo: errno];

	if (S_ISDIR(s.st_mode)) {
		OFArray *contents;

	[URLHandler removeItemAtURL: URL];
		@try {
			contents = [self contentsOfDirectoryAtPath: path];
		} @catch (id e) {
			/*
			 * Only convert exceptions to
			 * OFRemoveItemFailedException that have an errNo
			 * property. This covers all I/O related exceptions
			 * from the operations used to remove an item, all
			 * others should be left as is.
			 */
			if ([e respondsToSelector: @selector(errNo)])
				@throw [OFRemoveItemFailedException
				    exceptionWithPath: path
						errNo: [e errNo]];

}
			@throw e;
		}


		for (OFString *item in contents) {
			void *pool2 = objc_autoreleasePoolPush();

			[self removeItemAtPath:
- (void)removeItemAtPath: (OFString *)path
			    [path stringByAppendingPathComponent: item]];

			objc_autoreleasePoolPop(pool2);
		}
{
	void *pool = objc_autoreleasePoolPush();


#ifndef OF_MORPHOS
# ifndef OF_WINDOWS
		if (rmdir([path cStringWithEncoding:
		    [OFLocalization encoding]]) != 0)
# else
		if (_wrmdir([path UTF16String]) != 0)
# endif
			@throw [OFRemoveItemFailedException
				exceptionWithPath: path
	[self removeItemAtURL: [OFURL fileURLWithPath: path]];
					    errNo: errno];
	} else {
# ifndef OF_WINDOWS
		if (unlink([path cStringWithEncoding:
		    [OFLocalization encoding]]) != 0)
# else
		if (_wunlink([path UTF16String]) != 0)
# endif
			@throw [OFRemoveItemFailedException
			    exceptionWithPath: path
					errNo: errno];
#endif
	}

#ifdef OF_MORPHOS
	if (!DeleteFile(
	    [path cStringWithEncoding: [OFLocalization encoding]])) {
		int errNo;

		switch (IoErr()) {
		case ERROR_OBJECT_IN_USE:
		case ERROR_DISK_NOT_VALIDATED:
			errNo = EBUSY;
			break;
		case ERROR_OBJECT_NOT_FOUND:
			errNo = ENOENT;
			break;
		case ERROR_DISK_WRITE_PROTECTED:
			errNo = EROFS;
			break;
		case ERROR_DELETE_PROTECTED:
			errNo = EACCES;
			break;
		default:
			errNo = 0;
			break;
		}

		@throw [OFRemoveItemFailedException exceptionWithPath: path
								errNo: errNo];
	}
#endif

	objc_autoreleasePoolPop(pool);
}

- (void)removeItemAtURL: (OFURL *)URL
- (void)linkItemAtURL: (OFURL *)source
		toURL: (OFURL *)destination
{
	void *pool = objc_autoreleasePoolPush();
	OF_KINDOF(OFURLHandler *) URLHandler;

	if (source == nil || destination == nil)
		@throw [OFInvalidArgumentException exception];
	[self removeItemAtPath: [URL fileSystemRepresentation]];

	if (![[destination scheme] isEqual: [source scheme]])
		@throw [OFInvalidArgumentException exception];

	URLHandler = [OFURLHandler handlerForURL: source];

	if (URLHandler == nil)
		@throw [OFUnsupportedProtocolException
		    exceptionWithURL: source];

	[URLHandler linkItemAtURL: source
			    toURL: destination];

	objc_autoreleasePoolPop(pool);
}

#ifdef OF_FILE_MANAGER_SUPPORTS_LINKS
- (void)linkItemAtPath: (OFString *)source
		toPath: (OFString *)destination
{
	void *pool;

	if (source == nil || destination == nil)
		@throw [OFInvalidArgumentException exception];

	pool = objc_autoreleasePoolPush();
	void *pool = objc_autoreleasePoolPush();

# ifndef OF_WINDOWS
	of_string_encoding_t encoding = [OFLocalization encoding];

	[self linkItemAtURL: [OFURL fileURLWithPath: source]
	if (link([source cStringWithEncoding: encoding],
	    [destination cStringWithEncoding: encoding]) != 0)
		@throw [OFLinkFailedException
		    exceptionWithSourcePath: source
			    destinationPath: destination
		      toURL: [OFURL fileURLWithPath: destination]];
				      errNo: errno];
# else
	if (!CreateHardLinkW([destination UTF16String],
	    [source UTF16String], NULL))
		@throw [OFLinkFailedException
		    exceptionWithSourcePath: source
			    destinationPath: destination
				      errNo: 0];

# endif

	objc_autoreleasePoolPop(pool);
}
#endif

- (void)linkItemAtURL: (OFURL *)source
		toURL: (OFURL *)destination
{
#ifdef OF_FILE_MANAGER_SUPPORTS_LINKS
	void *pool = objc_autoreleasePoolPush();

	[self linkItemAtPath: [source fileSystemRepresentation]
		      toPath: [destination fileSystemRepresentation]];

	objc_autoreleasePoolPop(pool);
#else
	OF_UNRECOGNIZED_SELECTOR
#endif
}

#ifdef OF_FILE_MANAGER_SUPPORTS_SYMLINKS
- (void)createSymbolicLinkAtPath: (OFString *)destination
	     withDestinationPath: (OFString *)source
- (void)createSymbolicLinkAtURL: (OFURL *)URL
	    withDestinationPath: (OFString *)target
{
	void *pool;
	void *pool = objc_autoreleasePoolPush();
	OF_KINDOF(OFURLHandler *) URLHandler;

	if (source == nil || destination == nil)
	if (URL == nil || target == nil)
		@throw [OFInvalidArgumentException exception];

	pool = objc_autoreleasePoolPush();
	URLHandler = [OFURLHandler handlerForURL: URL];

# ifndef OF_WINDOWS
	if (URLHandler == nil)
	of_string_encoding_t encoding = [OFLocalization encoding];
		@throw [OFUnsupportedProtocolException exceptionWithURL: URL];

	if (symlink([source cStringWithEncoding: encoding],
	    [destination cStringWithEncoding: encoding]) != 0)
		@throw [OFCreateSymbolicLinkFailedException
	[URLHandler createSymbolicLinkAtURL: URL
		    exceptionWithSourcePath: source
			    destinationPath: destination
			withDestinationPath: target];
				      errNo: errno];
# else
	if (func_CreateSymbolicLinkW == NULL)
		@throw [OFNotImplementedException exceptionWithSelector: _cmd
								 object: self];

	if (!func_CreateSymbolicLinkW([destination UTF16String],
	    [source UTF16String], 0))
		@throw [OFCreateSymbolicLinkFailedException
		    exceptionWithSourcePath: source
			    destinationPath: destination
				      errNo: 0];
# endif

	objc_autoreleasePoolPop(pool);
}
#endif

#ifdef OF_FILE_MANAGER_SUPPORTS_SYMLINKS
- (void)createSymbolicLinkAtURL: (OFURL *)destination
	    withDestinationPath: (OFString *)source
- (void)createSymbolicLinkAtPath: (OFString *)path
	     withDestinationPath: (OFString *)target
{
#ifdef OF_FILE_MANAGER_SUPPORTS_SYMLINKS
	void *pool = objc_autoreleasePoolPush();

	[self createSymbolicLinkAtPath: [destination fileSystemRepresentation]
		   withDestinationPath: source];
	[self createSymbolicLinkAtURL: [OFURL fileURLWithPath: path]
		  withDestinationPath: target];

	objc_autoreleasePoolPop(pool);
#else
	OF_UNRECOGNIZED_SELECTOR
#endif
}
#endif
@end

@implementation OFDictionary (FileAttributes)
- (uintmax_t)fileSize
{
	return [attributeForKeyOrException(self, of_file_attribute_key_size)
	    uIntMaxValue];