ObjFW  Diff

Differences From Artifact [f5483886a4]:

To Artifact [c425786752]:


1
2

3
4
5
6
7
8
9
1

2
3
4
5
6
7
8
9

-
+







/*
 * Copyright (c) 2008-2022 Jonathan Schleifer <js@nil.im>
 * Copyright (c) 2008-2023 Jonathan Schleifer <js@nil.im>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
 * the packaging of this file.
 *
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
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







-
+

+
+
+





+






+
+





-
-








#define OF_FILE_MANAGER_M

#include <errno.h>
#include <limits.h>
#include "unistd_wrapper.h"

#import "platform.h"
#include "platform.h"

#ifdef OF_DJGPP
# include <syslimits.h>
#endif
#ifdef OF_PSP
# include <sys/syslimits.h>
#endif

#import "OFArray.h"
#import "OFData.h"
#import "OFDate.h"
#import "OFDictionary.h"
#ifdef OF_HAVE_FILES
# import "OFFile.h"
#endif
#import "OFFileManager.h"
#import "OFIRI.h"
#import "OFIRIHandler.h"
#import "OFLocale.h"
#import "OFNumber.h"
#import "OFStream.h"
#import "OFString.h"
#import "OFSystemInfo.h"
#import "OFURI.h"
#import "OFURIHandler.h"

#import "OFChangeCurrentDirectoryFailedException.h"
#import "OFCopyItemFailedException.h"
#import "OFCreateDirectoryFailedException.h"
#import "OFGetCurrentDirectoryFailedException.h"
#import "OFInitializationFailedException.h"
#import "OFInvalidArgumentException.h"
60
61
62
63
64
65
66

67
68

69
70
71
72
73
74
75
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81







+


+







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

#ifdef OF_AMIGAOS
# define Class IntuitionClass
# include <proto/exec.h>
# include <proto/dos.h>
# undef Class
#endif

#ifdef OF_MINT
# include <bits/local_lim.h>
#endif

@interface OFDefaultFileManager: OFFileManager
163
164
165
166
167
168
169
















170
171
172
173
174
175
176
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







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







			@throw [OFOutOfRangeException exception];

		return nil;
	}

	return [OFString stringWithCString: buffer
				  encoding: [OFLocale encoding]];
# elif defined(OF_GLIBC)
	char *buffer;
	OFString *path;

	if ((buffer = getcwd(NULL, 0)) == NULL)
		@throw [OFGetCurrentDirectoryFailedException
		    exceptionWithErrNo: errno];

	@try {
		path = [OFString stringWithCString: buffer
					  encoding: [OFLocale encoding]];
	} @finally {
		free(buffer);
	}

	return path;
# else
	char buffer[PATH_MAX];

	if ((getcwd(buffer, PATH_MAX)) == NULL)
		@throw [OFGetCurrentDirectoryFailedException
		    exceptionWithErrNo: errno];

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







-
+


-
+

-
+
+

-





-
+

-
+

-
+


-
-
+
+

-
+








-
+
-
-
+







-
+

-
+

-
+


-
-
+
+

-
+








-
+




-
+

-
+

-
+


-
-
+
+

-
+








-
+







-
+

-
+

-
+


-
-
+
+

-
+








-
+







-
+

-
+

-
+


-
-
+
+

-
+


-
+


-
+

-
-
+
+


-
+



-
+










-
+



-
+











-
-
+
+



-
-
-
-
+
+
+
+


-
+

-
-
-
+
+
+


-
-
-
-
+
+
+
+



-
+

-
+







-
-
+
+









-
+









-
+






-
+

-
+

-
+


-
-
+
+

-
+






-
+


-
-
+
+

-
-
+
+


-
+







#  endif

	return [OFString stringWithCString: buffer
				  encoding: [OFLocale encoding]];
# endif
}

- (OFURI *)currentDirectoryURI
- (OFIRI *)currentDirectoryIRI
{
	void *pool = objc_autoreleasePoolPush();
	OFURI *ret;
	OFIRI *ret;

	ret = [OFURI fileURIWithPath: self.currentDirectoryPath];
	ret = [OFIRI fileIRIWithPath: self.currentDirectoryPath];
	ret = [ret retain];

	[ret retain];
	objc_autoreleasePoolPop(pool);
	return [ret autorelease];
}
#endif

- (OFFileAttributes)attributesOfItemAtURI: (OFURI *)URI
- (OFFileAttributes)attributesOfItemAtIRI: (OFIRI *)IRI
{
	OFURIHandler *URIHandler;
	OFIRIHandler *IRIHandler;

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

	if ((URIHandler = [OFURIHandler handlerForURI: URI]) == nil)
		@throw [OFUnsupportedProtocolException exceptionWithURI: URI];
	if ((IRIHandler = [OFIRIHandler handlerForIRI: IRI]) == nil)
		@throw [OFUnsupportedProtocolException exceptionWithIRI: IRI];

	return [URIHandler attributesOfItemAtURI: URI];
	return [IRIHandler attributesOfItemAtIRI: IRI];
}

#ifdef OF_HAVE_FILES
- (OFFileAttributes)attributesOfItemAtPath: (OFString *)path
{
	void *pool = objc_autoreleasePoolPush();
	OFFileAttributes ret;

	ret = [self attributesOfItemAtURI: [OFURI fileURIWithPath: path]];
	ret = [self attributesOfItemAtIRI: [OFIRI fileIRIWithPath: path]];

	[ret retain];
	ret = [ret retain];

	objc_autoreleasePoolPop(pool);

	return [ret autorelease];
}
#endif

- (void)setAttributes: (OFFileAttributes)attributes ofItemAtURI: (OFURI *)URI
- (void)setAttributes: (OFFileAttributes)attributes ofItemAtIRI: (OFIRI *)IRI
{
	OFURIHandler *URIHandler;
	OFIRIHandler *IRIHandler;

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

	if ((URIHandler = [OFURIHandler handlerForURI: URI]) == nil)
		@throw [OFUnsupportedProtocolException exceptionWithURI: URI];
	if ((IRIHandler = [OFIRIHandler handlerForIRI: IRI]) == nil)
		@throw [OFUnsupportedProtocolException exceptionWithIRI: IRI];

	[URIHandler setAttributes: attributes ofItemAtURI: URI];
	[IRIHandler setAttributes: attributes ofItemAtIRI: IRI];
}

#ifdef OF_HAVE_FILES
- (void)setAttributes: (OFFileAttributes)attributes
	 ofItemAtPath: (OFString *)path
{
	void *pool = objc_autoreleasePoolPush();
	[self setAttributes: attributes
		ofItemAtURI: [OFURI fileURIWithPath: path]];
		ofItemAtIRI: [OFIRI fileIRIWithPath: path]];
	objc_autoreleasePoolPop(pool);
}
#endif

- (bool)fileExistsAtURI: (OFURI *)URI
- (bool)fileExistsAtIRI: (OFIRI *)IRI
{
	OFURIHandler *URIHandler;
	OFIRIHandler *IRIHandler;

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

	if ((URIHandler = [OFURIHandler handlerForURI: URI]) == nil)
		@throw [OFUnsupportedProtocolException exceptionWithURI: URI];
	if ((IRIHandler = [OFIRIHandler handlerForIRI: IRI]) == nil)
		@throw [OFUnsupportedProtocolException exceptionWithIRI: IRI];

	return [URIHandler fileExistsAtURI: URI];
	return [IRIHandler fileExistsAtIRI: IRI];
}

#ifdef OF_HAVE_FILES
- (bool)fileExistsAtPath: (OFString *)path
{
	void *pool = objc_autoreleasePoolPush();
	bool ret;

	ret = [self fileExistsAtURI: [OFURI fileURIWithPath: path]];
	ret = [self fileExistsAtIRI: [OFIRI fileIRIWithPath: path]];

	objc_autoreleasePoolPop(pool);

	return ret;
}
#endif

- (bool)directoryExistsAtURI: (OFURI *)URI
- (bool)directoryExistsAtIRI: (OFIRI *)IRI
{
	OFURIHandler *URIHandler;
	OFIRIHandler *IRIHandler;

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

	if ((URIHandler = [OFURIHandler handlerForURI: URI]) == nil)
		@throw [OFUnsupportedProtocolException exceptionWithURI: URI];
	if ((IRIHandler = [OFIRIHandler handlerForIRI: IRI]) == nil)
		@throw [OFUnsupportedProtocolException exceptionWithIRI: IRI];

	return [URIHandler directoryExistsAtURI: URI];
	return [IRIHandler directoryExistsAtIRI: IRI];
}

#ifdef OF_HAVE_FILES
- (bool)directoryExistsAtPath: (OFString *)path
{
	void *pool = objc_autoreleasePoolPush();
	bool ret;

	ret = [self directoryExistsAtURI: [OFURI fileURIWithPath: path]];
	ret = [self directoryExistsAtIRI: [OFIRI fileIRIWithPath: path]];

	objc_autoreleasePoolPop(pool);

	return ret;
}
#endif

- (void)createDirectoryAtURI: (OFURI *)URI
- (void)createDirectoryAtIRI: (OFIRI *)IRI
{
	OFURIHandler *URIHandler;
	OFIRIHandler *IRIHandler;

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

	if ((URIHandler = [OFURIHandler handlerForURI: URI]) == nil)
		@throw [OFUnsupportedProtocolException exceptionWithURI: URI];
	if ((IRIHandler = [OFIRIHandler handlerForIRI: IRI]) == nil)
		@throw [OFUnsupportedProtocolException exceptionWithIRI: IRI];

	[URIHandler createDirectoryAtURI: URI];
	[IRIHandler createDirectoryAtIRI: IRI];
}

- (void)createDirectoryAtURI: (OFURI *)URI createParents: (bool)createParents
- (void)createDirectoryAtIRI: (OFIRI *)IRI createParents: (bool)createParents
{
	void *pool = objc_autoreleasePoolPush();
	OFMutableURI *mutableURI;
	OFMutableIRI *mutableIRI;
	OFArray OF_GENERIC(OFString *) *components;
	OFMutableArray OF_GENERIC(OFURI *) *componentURIs;
	size_t componentURIsCount;
	OFMutableArray OF_GENERIC(OFIRI *) *componentIRIs;
	size_t componentIRIsCount;
	ssize_t i;

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

	if (!createParents) {
		[self createDirectoryAtURI: URI];
		[self createDirectoryAtIRI: IRI];
		return;
	}

	/*
	 * Try blindly creating the directory first.
	 *
	 * The reason for this is that we might be sandboxed, so attempting to
	 * create any of the parent directories will fail, while creating the
	 * directory itself will work.
	 */
	if ([self directoryExistsAtURI: URI])
	if ([self directoryExistsAtIRI: IRI])
		return;

	@try {
		[self createDirectoryAtURI: URI];
		[self createDirectoryAtIRI: IRI];
		return;
	} @catch (OFCreateDirectoryFailedException *e) {
		/*
		 * If we didn't fail because any of the parents is missing,
		 * there is no point in trying to create the parents.
		 */
		if (e.errNo != ENOENT)
			@throw e;
	}

	/*
	 * Because we might be sandboxed (and for remote URIs don't even know
	 * anything at all), we generate the URI for every component. We then
	 * Because we might be sandboxed (and for remote IRIs don't even know
	 * anything at all), we generate the IRI for every component. We then
	 * iterate them in reverse order until we find the first existing
	 * directory, and then create subdirectories from there.
	 */
	mutableURI = [[URI mutableCopy] autorelease];
	mutableURI.percentEncodedPath = @"/";
	components = URI.pathComponents;
	componentURIs = [OFMutableArray arrayWithCapacity: components.count];
	mutableIRI = [[IRI mutableCopy] autorelease];
	mutableIRI.percentEncodedPath = @"/";
	components = IRI.pathComponents;
	componentIRIs = [OFMutableArray arrayWithCapacity: components.count];

	for (OFString *component in components) {
		[mutableURI appendPathComponent: component];
		[mutableIRI appendPathComponent: component];

		if (![mutableURI.percentEncodedPath isEqual: @"/"])
			[componentURIs addObject:
			    [[mutableURI copy] autorelease]];
		if (![mutableIRI.percentEncodedPath isEqual: @"/"])
			[componentIRIs addObject:
			    [[mutableIRI copy] autorelease]];
	}

	componentURIsCount = componentURIs.count;
	for (i = componentURIsCount - 1; i > 0; i--) {
		if ([self directoryExistsAtURI:
		    [componentURIs objectAtIndex: i]])
	componentIRIsCount = componentIRIs.count;
	for (i = componentIRIsCount - 1; i > 0; i--) {
		if ([self directoryExistsAtIRI:
		    [componentIRIs objectAtIndex: i]])
			break;
	}

	if (++i == (ssize_t)componentURIsCount) {
	if (++i == (ssize_t)componentIRIsCount) {
		/*
		 * The URI exists, even though before we made sure it did not.
		 * The IRI exists, even though before we made sure it did not.
		 * That means it was created in the meantime by something else,
		 * so we're done here.
		 */
		objc_autoreleasePoolPop(pool);
		return;
	}

	for (; i < (ssize_t)componentURIsCount; i++)
		[self createDirectoryAtURI: [componentURIs objectAtIndex: i]];
	for (; i < (ssize_t)componentIRIsCount; i++)
		[self createDirectoryAtIRI: [componentIRIs objectAtIndex: i]];

	objc_autoreleasePoolPop(pool);
}

#ifdef OF_HAVE_FILES
- (void)createDirectoryAtPath: (OFString *)path
{
	void *pool = objc_autoreleasePoolPush();

	[self createDirectoryAtURI: [OFURI fileURIWithPath: path]];
	[self createDirectoryAtIRI: [OFIRI fileIRIWithPath: path]];

	objc_autoreleasePoolPop(pool);
}

- (void)createDirectoryAtPath: (OFString *)path
		createParents: (bool)createParents
{
	void *pool = objc_autoreleasePoolPush();

	[self createDirectoryAtURI: [OFURI fileURIWithPath: path]
	[self createDirectoryAtIRI: [OFIRI fileIRIWithPath: path]
		     createParents: createParents];

	objc_autoreleasePoolPop(pool);
}
#endif

- (OFArray OF_GENERIC(OFURI *) *)contentsOfDirectoryAtURI: (OFURI *)URI
- (OFArray OF_GENERIC(OFIRI *) *)contentsOfDirectoryAtIRI: (OFIRI *)IRI
{
	OFURIHandler *URIHandler;
	OFIRIHandler *IRIHandler;

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

	if ((URIHandler = [OFURIHandler handlerForURI: URI]) == nil)
		@throw [OFUnsupportedProtocolException exceptionWithURI: URI];
	if ((IRIHandler = [OFIRIHandler handlerForIRI: IRI]) == nil)
		@throw [OFUnsupportedProtocolException exceptionWithIRI: IRI];

	return [URIHandler contentsOfDirectoryAtURI: URI];
	return [IRIHandler contentsOfDirectoryAtIRI: IRI];
}

#ifdef OF_HAVE_FILES
- (OFArray OF_GENERIC(OFString *) *)contentsOfDirectoryAtPath: (OFString *)path
{
	void *pool = objc_autoreleasePoolPush();
	OFArray OF_GENERIC(OFURI *) *URIs;
	OFArray OF_GENERIC(OFIRI *) *IRIs;
	OFMutableArray OF_GENERIC(OFString *) *ret;

	URIs = [self contentsOfDirectoryAtURI: [OFURI fileURIWithPath: path]];
	ret = [OFMutableArray arrayWithCapacity: URIs.count];
	IRIs = [self contentsOfDirectoryAtIRI: [OFIRI fileIRIWithPath: path]];
	ret = [OFMutableArray arrayWithCapacity: IRIs.count];

	for (OFURI *URI in URIs)
		[ret addObject: URI.lastPathComponent];
	for (OFIRI *IRI in IRIs)
		[ret addObject: IRI.lastPathComponent];

	[ret makeImmutable];
	[ret retain];
	ret = [ret retain];

	objc_autoreleasePoolPop(pool);

	return [ret autorelease];
}

- (OFArray OF_GENERIC(OFString *) *)subpathsOfDirectoryAtPath: (OFString *)path
478
479
480
481
482
483
484
485

486
487
488
489
490
491
492
499
500
501
502
503
504
505

506
507
508
509
510
511
512
513







-
+







		else
			[ret addObject: fullSubpath];

		objc_autoreleasePoolPop(pool2);
	}

	[ret makeImmutable];
	[ret retain];
	ret = [ret retain];

	objc_autoreleasePoolPop(pool);

	return [ret autorelease];
}

- (void)changeCurrentDirectoryPath: (OFString *)path
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
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







-
+



-
+








-
-
+
+





-
+


-
+








-
+

-
+

-
+


-
+

-
-
+
+



-
+


-
-
+
+






-
+


-
+













-
+




-
+









-
-
+
+





-
+

-
-
+
+


-
+











-
+

-
-
+
+







	if (status != 0)
		@throw [OFChangeCurrentDirectoryFailedException
		    exceptionWithPath: path
				errNo: errno];
# endif
}

- (void)changeCurrentDirectoryURI: (OFURI *)URI
- (void)changeCurrentDirectoryIRI: (OFIRI *)IRI
{
	void *pool = objc_autoreleasePoolPush();

	[self changeCurrentDirectoryPath: URI.fileSystemRepresentation];
	[self changeCurrentDirectoryPath: IRI.fileSystemRepresentation];

	objc_autoreleasePoolPop(pool);
}

- (void)copyItemAtPath: (OFString *)source toPath: (OFString *)destination
{
	void *pool = objc_autoreleasePoolPush();

	[self copyItemAtURI: [OFURI fileURIWithPath: source]
		      toURI: [OFURI fileURIWithPath: destination]];
	[self copyItemAtIRI: [OFIRI fileIRIWithPath: source]
		      toIRI: [OFIRI fileIRIWithPath: destination]];

	objc_autoreleasePoolPop(pool);
}
#endif

- (void)copyItemAtURI: (OFURI *)source toURI: (OFURI *)destination
- (void)copyItemAtIRI: (OFIRI *)source toIRI: (OFIRI *)destination
{
	void *pool;
	OFURIHandler *URIHandler;
	OFIRIHandler *IRIHandler;
	OFFileAttributes attributes;
	OFFileAttributeType type;

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

	pool = objc_autoreleasePoolPush();

	if ((URIHandler = [OFURIHandler handlerForURI: source]) == nil)
	if ((IRIHandler = [OFIRIHandler handlerForIRI: source]) == nil)
		@throw [OFUnsupportedProtocolException
		    exceptionWithURI: source];
		    exceptionWithIRI: source];

	if ([URIHandler copyItemAtURI: source toURI: destination])
	if ([IRIHandler copyItemAtIRI: source toIRI: destination])
		return;

	if ([self fileExistsAtURI: destination])
	if ([self fileExistsAtIRI: destination])
		@throw [OFCopyItemFailedException
		    exceptionWithSourceURI: source
			    destinationURI: destination
		    exceptionWithSourceIRI: source
			    destinationIRI: destination
				     errNo: EEXIST];

	@try {
		attributes = [self attributesOfItemAtURI: source];
		attributes = [self attributesOfItemAtIRI: source];
	} @catch (OFGetItemAttributesFailedException *e) {
		@throw [OFCopyItemFailedException
		    exceptionWithSourceURI: source
			    destinationURI: destination
		    exceptionWithSourceIRI: source
			    destinationIRI: destination
				     errNo: e.errNo];
	}

	type = attributes.fileType;

	if ([type isEqual: OFFileTypeDirectory]) {
		OFArray OF_GENERIC(OFURI *) *contents;
		OFArray OF_GENERIC(OFIRI *) *contents;

		@try {
			[self createDirectoryAtURI: destination];
			[self createDirectoryAtIRI: destination];

			@try {
				OFFileAttributeKey key = OFFilePOSIXPermissions;
				OFNumber *permissions =
				    [attributes objectForKey: key];
				OFFileAttributes destinationAttributes;

				if (permissions != nil) {
					destinationAttributes = [OFDictionary
					    dictionaryWithObject: permissions
							  forKey: key];
					[self
					    setAttributes: destinationAttributes
					      ofItemAtURI: destination];
					      ofItemAtIRI: destination];
				}
			} @catch (OFNotImplementedException *e) {
			}

			contents = [self contentsOfDirectoryAtURI: source];
			contents = [self contentsOfDirectoryAtIRI: source];
		} @catch (id e) {
			/*
			 * Only convert exceptions to OFCopyItemFailedException
			 * that have an errNo property. This covers all I/O
			 * related exceptions from the operations used to copy
			 * an item, all others should be left as is.
			 */
			if ([e respondsToSelector: @selector(errNo)])
				@throw [OFCopyItemFailedException
				    exceptionWithSourceURI: source
					    destinationURI: destination
				    exceptionWithSourceIRI: source
					    destinationIRI: destination
						     errNo: [e errNo]];

			@throw e;
		}

		for (OFURI *item in contents) {
		for (OFIRI *item in contents) {
			void *pool2 = objc_autoreleasePoolPush();
			OFURI *destinationURI = [destination
			    URIByAppendingPathComponent:
			OFIRI *destinationIRI = [destination
			    IRIByAppendingPathComponent:
			    item.lastPathComponent];

			[self copyItemAtURI: item toURI: destinationURI];
			[self copyItemAtIRI: item toIRI: destinationIRI];

			objc_autoreleasePoolPop(pool2);
		}
	} else if ([type isEqual: OFFileTypeRegular]) {
		size_t pageSize = [OFSystemInfo pageSize];
		OFStream *sourceStream = nil;
		OFStream *destinationStream = nil;
		char *buffer;

		buffer = OFAllocMemory(1, pageSize);
		@try {
			sourceStream = [OFURIHandler openItemAtURI: source
			sourceStream = [OFIRIHandler openItemAtIRI: source
							      mode: @"r"];
			destinationStream = [OFURIHandler
			    openItemAtURI: destination
			destinationStream = [OFIRIHandler
			    openItemAtIRI: destination
				     mode: @"w"];

			while (!sourceStream.atEndOfStream) {
				size_t length;

				length = [sourceStream
				    readIntoBuffer: buffer
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
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







-
+












-
-
+
+













-
+










-
-
+
+






-
-
+
+









-
-
+
+




-
+


-
+






-
+

-
+


-
+






-
+

-
-
+
+



-
+

-
+


-
-
+
+




-
+


-
-
+
+






-
+

-
+

-
+


-
-
+
+

-
+






-
+




-
+


-
+







-
+

-
+

-
+

-
+








-
-
+
+




-
+



-
+

-
+


-
+

-
-
+
+

-
+









-
+




+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+








				if (permissions != nil) {
					destinationAttributes = [OFDictionary
					    dictionaryWithObject: permissions
							  forKey: key];
					[self
					    setAttributes: destinationAttributes
					      ofItemAtURI: destination];
					      ofItemAtIRI: destination];
				}
			} @catch (OFNotImplementedException *e) {
			}
		} @catch (id e) {
			/*
			 * Only convert exceptions to OFCopyItemFailedException
			 * that have an errNo property. This covers all I/O
			 * related exceptions from the operations used to copy
			 * an item, all others should be left as is.
			 */
			if ([e respondsToSelector: @selector(errNo)])
				@throw [OFCopyItemFailedException
				    exceptionWithSourceURI: source
					    destinationURI: destination
				    exceptionWithSourceIRI: source
					    destinationIRI: destination
						     errNo: [e errNo]];

			@throw e;
		} @finally {
			[sourceStream close];
			[destinationStream close];
			OFFreeMemory(buffer);
		}
	} else if ([type isEqual: OFFileTypeSymbolicLink]) {
		@try {
			OFString *linkDestination =
			    attributes.fileSymbolicLinkDestination;

			[self createSymbolicLinkAtURI: destination
			[self createSymbolicLinkAtIRI: destination
				  withDestinationPath: linkDestination];
		} @catch (id e) {
			/*
			 * Only convert exceptions to OFCopyItemFailedException
			 * that have an errNo property. This covers all I/O
			 * related exceptions from the operations used to copy
			 * an item, all others should be left as is.
			 */
			if ([e respondsToSelector: @selector(errNo)])
				@throw [OFCopyItemFailedException
				    exceptionWithSourceURI: source
					    destinationURI: destination
				    exceptionWithSourceIRI: source
					    destinationIRI: destination
						     errNo: [e errNo]];

			@throw e;
		}
	} else
		@throw [OFCopyItemFailedException
		    exceptionWithSourceURI: source
			    destinationURI: destination
		    exceptionWithSourceIRI: source
			    destinationIRI: destination
				     errNo: EINVAL];

	objc_autoreleasePoolPop(pool);
}

#ifdef OF_HAVE_FILES
- (void)moveItemAtPath: (OFString *)source toPath: (OFString *)destination
{
	void *pool = objc_autoreleasePoolPush();
	[self moveItemAtURI: [OFURI fileURIWithPath: source]
		      toURI: [OFURI fileURIWithPath: destination]];
	[self moveItemAtIRI: [OFIRI fileIRIWithPath: source]
		      toIRI: [OFIRI fileIRIWithPath: destination]];
	objc_autoreleasePoolPop(pool);
}
#endif

- (void)moveItemAtURI: (OFURI *)source toURI: (OFURI *)destination
- (void)moveItemAtIRI: (OFIRI *)source toIRI: (OFIRI *)destination
{
	void *pool;
	OFURIHandler *URIHandler;
	OFIRIHandler *IRIHandler;

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

	pool = objc_autoreleasePoolPush();

	if ((URIHandler = [OFURIHandler handlerForURI: source]) == nil)
	if ((IRIHandler = [OFIRIHandler handlerForIRI: source]) == nil)
		@throw [OFUnsupportedProtocolException
		    exceptionWithURI: source];
		    exceptionWithIRI: source];

	@try {
		if ([URIHandler moveItemAtURI: source toURI: destination])
		if ([IRIHandler moveItemAtIRI: source toIRI: destination])
			return;
	} @catch (OFMoveItemFailedException *e) {
		if (e.errNo != EXDEV)
			@throw e;
	}

	if ([self fileExistsAtURI: destination])
	if ([self fileExistsAtIRI: destination])
		@throw [OFMoveItemFailedException
		    exceptionWithSourceURI: source
			    destinationURI: destination
		    exceptionWithSourceIRI: source
			    destinationIRI: destination
				     errNo: EEXIST];

	@try {
		[self copyItemAtURI: source toURI: destination];
		[self copyItemAtIRI: source toIRI: destination];
	} @catch (OFCopyItemFailedException *e) {
		[self removeItemAtURI: destination];
		[self removeItemAtIRI: destination];

		@throw [OFMoveItemFailedException
		    exceptionWithSourceURI: source
			    destinationURI: destination
		    exceptionWithSourceIRI: source
			    destinationIRI: destination
				     errNo: e.errNo];
	}

	@try {
		[self removeItemAtURI: source];
		[self removeItemAtIRI: source];
	} @catch (OFRemoveItemFailedException *e) {
		@throw [OFMoveItemFailedException
		    exceptionWithSourceURI: source
			    destinationURI: destination
		    exceptionWithSourceIRI: source
			    destinationIRI: destination
				     errNo: e.errNo];
	}

	objc_autoreleasePoolPop(pool);
}

- (void)removeItemAtURI: (OFURI *)URI
- (void)removeItemAtIRI: (OFIRI *)IRI
{
	OFURIHandler *URIHandler;
	OFIRIHandler *IRIHandler;

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

	if ((URIHandler = [OFURIHandler handlerForURI: URI]) == nil)
		@throw [OFUnsupportedProtocolException exceptionWithURI: URI];
	if ((IRIHandler = [OFIRIHandler handlerForIRI: IRI]) == nil)
		@throw [OFUnsupportedProtocolException exceptionWithIRI: IRI];

	[URIHandler removeItemAtURI: URI];
	[IRIHandler removeItemAtIRI: IRI];
}

#ifdef OF_HAVE_FILES
- (void)removeItemAtPath: (OFString *)path
{
	void *pool = objc_autoreleasePoolPush();
	[self removeItemAtURI: [OFURI fileURIWithPath: path]];
	[self removeItemAtIRI: [OFIRI fileIRIWithPath: path]];
	objc_autoreleasePoolPop(pool);
}
#endif

- (void)linkItemAtURI: (OFURI *)source toURI: (OFURI *)destination
- (void)linkItemAtIRI: (OFIRI *)source toIRI: (OFIRI *)destination
{
	void *pool = objc_autoreleasePoolPush();
	OFURIHandler *URIHandler;
	OFIRIHandler *IRIHandler;

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

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

	URIHandler = [OFURIHandler handlerForURI: source];
	IRIHandler = [OFIRIHandler handlerForIRI: source];

	if (URIHandler == nil)
	if (IRIHandler == nil)
		@throw [OFUnsupportedProtocolException
		    exceptionWithURI: source];
		    exceptionWithIRI: source];

	[URIHandler linkItemAtURI: source toURI: destination];
	[IRIHandler linkItemAtIRI: source toIRI: destination];

	objc_autoreleasePoolPop(pool);
}

#ifdef OF_FILE_MANAGER_SUPPORTS_LINKS
- (void)linkItemAtPath: (OFString *)source toPath: (OFString *)destination
{
	void *pool = objc_autoreleasePoolPush();
	[self linkItemAtURI: [OFURI fileURIWithPath: source]
		      toURI: [OFURI fileURIWithPath: destination]];
	[self linkItemAtIRI: [OFIRI fileIRIWithPath: source]
		      toIRI: [OFIRI fileIRIWithPath: destination]];
	objc_autoreleasePoolPop(pool);
}
#endif

- (void)createSymbolicLinkAtURI: (OFURI *)URI
- (void)createSymbolicLinkAtIRI: (OFIRI *)IRI
	    withDestinationPath: (OFString *)target
{
	void *pool = objc_autoreleasePoolPush();
	OFURIHandler *URIHandler;
	OFIRIHandler *IRIHandler;

	if (URI == nil || target == nil)
	if (IRI == nil || target == nil)
		@throw [OFInvalidArgumentException exception];

	URIHandler = [OFURIHandler handlerForURI: URI];
	IRIHandler = [OFIRIHandler handlerForIRI: IRI];

	if (URIHandler == nil)
		@throw [OFUnsupportedProtocolException exceptionWithURI: URI];
	if (IRIHandler == nil)
		@throw [OFUnsupportedProtocolException exceptionWithIRI: IRI];

	[URIHandler createSymbolicLinkAtURI: URI withDestinationPath: target];
	[IRIHandler createSymbolicLinkAtIRI: IRI withDestinationPath: target];

	objc_autoreleasePoolPop(pool);
}

#ifdef OF_FILE_MANAGER_SUPPORTS_SYMLINKS
- (void)createSymbolicLinkAtPath: (OFString *)path
	     withDestinationPath: (OFString *)target
{
	void *pool = objc_autoreleasePoolPush();
	[self createSymbolicLinkAtURI: [OFURI fileURIWithPath: path]
	[self createSymbolicLinkAtIRI: [OFIRI fileIRIWithPath: path]
		  withDestinationPath: target];
	objc_autoreleasePoolPop(pool);
}
#endif

- (OFData *)extendedAttributeDataForName: (OFString *)name
			     ofItemAtIRI: (OFIRI *)IRI
{
	OFIRIHandler *IRIHandler;

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

	if ((IRIHandler = [OFIRIHandler handlerForIRI: IRI]) == nil)
		@throw [OFUnsupportedProtocolException exceptionWithIRI: IRI];

	return [IRIHandler extendedAttributeDataForName: name ofItemAtIRI: IRI];
}

#ifdef OF_FILE_MANAGER_SUPPORTS_EXTENDED_ATTRIBUTES
- (OFData *)extendedAttributeDataForName: (OFString *)name
			    ofItemAtPath: (OFString *)path
{
	void *pool = objc_autoreleasePoolPush();
	OFData *ret;

	ret = [self
	    extendedAttributeDataForName: name
			     ofItemAtIRI: [OFIRI fileIRIWithPath: path]];
	ret = [ret retain];

	objc_autoreleasePoolPop(pool);

	return [ret autorelease];
}
#endif

- (void)setExtendedAttributeData: (OFData *)data
			 forName: (OFString *)name
		     ofItemAtIRI: (OFIRI *)IRI
{
	OFIRIHandler *IRIHandler;

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

	if ((IRIHandler = [OFIRIHandler handlerForIRI: IRI]) == nil)
		@throw [OFUnsupportedProtocolException exceptionWithIRI: IRI];

	[IRIHandler setExtendedAttributeData: data
				     forName: name
				 ofItemAtIRI: IRI];
}

#ifdef OF_FILE_MANAGER_SUPPORTS_EXTENDED_ATTRIBUTES
- (void)setExtendedAttributeData: (OFData *)data
			 forName: (OFString *)name
		    ofItemAtPath: (OFString *)path
{
	void *pool = objc_autoreleasePoolPush();
	[self setExtendedAttributeData: data
			       forName: name
			   ofItemAtIRI: [OFIRI fileIRIWithPath: path]];
	objc_autoreleasePoolPop(pool);
}
#endif

- (void)removeExtendedAttributeForName: (OFString *)name
			   ofItemAtIRI: (OFIRI *)IRI
{
	OFIRIHandler *IRIHandler;

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

	if ((IRIHandler = [OFIRIHandler handlerForIRI: IRI]) == nil)
		@throw [OFUnsupportedProtocolException exceptionWithIRI: IRI];

	[IRIHandler removeExtendedAttributeForName: name ofItemAtIRI: IRI];
}

#ifdef OF_FILE_MANAGER_SUPPORTS_EXTENDED_ATTRIBUTES
- (void)removeExtendedAttributeForName: (OFString *)name
			  ofItemAtPath: (OFString *)path
{
	void *pool = objc_autoreleasePoolPush();
	[self removeExtendedAttributeForName: name
				 ofItemAtIRI: [OFIRI fileIRIWithPath: path]];
	objc_autoreleasePoolPop(pool);
}
#endif
@end

@implementation OFDefaultFileManager
- (instancetype)autorelease
{
	return self;
}
968
969
970
971
972
973
974





975
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088







+
+
+
+
+

	return attributeForKeyOrException(self, OFFileCreationDate);
}

- (OFString *)fileSymbolicLinkDestination
{
	return attributeForKeyOrException(self, OFFileSymbolicLinkDestination);
}

- (OFArray OF_GENERIC(OFString *) *)fileExtendedAttributesNames
{
	return attributeForKeyOrException(self, OFFileExtendedAttributesNames);
}
@end