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
|
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
|
+
+
+
+
+
+
+
+
+
+
+
-
+
-
-
-
+
+
+
-
+
-
-
-
+
+
+
-
+
-
+
-
+
-
-
-
+
+
+
-
+
-
-
-
-
+
+
+
+
-
-
+
+
-
-
+
+
+
-
-
-
-
+
+
+
+
+
-
+
-
-
-
-
+
+
+
+
+
-
+
-
-
-
+
+
+
-
-
+
+
+
-
+
-
-
+
+
-
-
-
+
+
+
-
-
-
+
+
+
-
-
-
-
-
-
-
-
-
-
-
|
objc_autoreleasePoolPop(pool);
}
- (void)copyItemAtPath: (OFString *)source
toPath: (OFString *)destination
{
void *pool = objc_autoreleasePoolPush();
[self copyItemAtURL: [OFURL fileURLWithPath: source]
toURL: [OFURL fileURLWithPath: destination]];
objc_autoreleasePoolPop(pool);
}
- (void)copyItemAtURL: (OFURL *)source
toURL: (OFURL *)destination
{
void *pool;
of_file_attributes_t attributes;
of_file_type_t type;
if (source == nil || destination == nil)
@throw [OFInvalidArgumentException exception];
pool = objc_autoreleasePoolPush();
if ([self fileExistsAtPath: destination])
if ([self fileExistsAtURL: destination])
@throw [OFCopyItemFailedException
exceptionWithSourcePath: source
destinationPath: destination
errNo: EEXIST];
exceptionWithSourceURL: source
destinationURL: destination
errNo: EEXIST];
@try {
attributes = [self attributesOfItemAtPath: source];
attributes = [self attributesOfItemAtURL: source];
} @catch (OFRetrieveItemAttributesFailedException *e) {
@throw [OFCopyItemFailedException
exceptionWithSourcePath: source
destinationPath: destination
errNo: [e errNo]];
exceptionWithSourceURL: source
destinationURL: destination
errNo: [e errNo]];
}
type = [attributes fileType];
if ([type isEqual: of_file_type_directory]) {
OFArray *contents;
@try {
[self createDirectoryAtPath: destination];
[self createDirectoryAtURL: destination];
#ifdef OF_FILE_MANAGER_SUPPORTS_PERMISSIONS
of_file_attribute_key_t key =
of_file_attribute_key_posix_permissions;
OFNumber *permissions = [attributes objectForKey: key];
of_file_attributes_t destinationAttributes =
[OFDictionary dictionaryWithObject: permissions
forKey: key];
[self setAttributes: destinationAttributes
ofItemAtPath: destination];
ofItemAtURL: destination];
#endif
contents = [self contentsOfDirectoryAtPath: source];
contents = [self contentsOfDirectoryAtURL: 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
exceptionWithSourcePath: source
destinationPath: destination
errNo: [e errNo]];
exceptionWithSourceURL: source
destinationURL: destination
errNo: [e errNo]];
@throw e;
}
for (OFString *item in contents) {
void *pool2 = objc_autoreleasePoolPush();
OFString *sourcePath, *destinationPath;
OFURL *sourceURL, *destinationURL;
sourcePath =
[source stringByAppendingPathComponent: item];
destinationPath =
[destination stringByAppendingPathComponent: item];
sourceURL =
[source URLByAppendingPathComponent: item];
destinationURL =
[destination URLByAppendingPathComponent: item];
[self copyItemAtPath: sourcePath
toPath: destinationPath];
[self copyItemAtURL: sourceURL
toURL: destinationURL];
objc_autoreleasePoolPop(pool2);
}
} else if ([type isEqual: of_file_type_regular]) {
size_t pageSize = [OFSystemInfo pageSize];
OFFile *sourceFile = nil;
OFFile *destinationFile = nil;
OFStream *sourceStream = nil;
OFStream *destinationStream = nil;
char *buffer;
if ((buffer = malloc(pageSize)) == NULL)
@throw [OFOutOfMemoryException
exceptionWithRequestedSize: pageSize];
@try {
sourceStream = [[OFURLHandler handlerForURL: source]
sourceFile = [OFFile fileWithPath: source
mode: @"r"];
destinationFile = [OFFile fileWithPath: destination
mode: @"w"];
openItemAtURL: source
mode: @"r"];
destinationStream = [[OFURLHandler handlerForURL:
destination] openItemAtURL: destination
mode: @"w"];
while (![sourceFile isAtEndOfStream]) {
while (![sourceStream isAtEndOfStream]) {
size_t length;
length = [sourceFile readIntoBuffer: buffer
length: pageSize];
[destinationFile writeBuffer: buffer
length: length];
length = [sourceStream
readIntoBuffer: buffer
length: pageSize];
[destinationStream writeBuffer: buffer
length: length];
}
#ifdef OF_FILE_MANAGER_SUPPORTS_PERMISSIONS
of_file_attribute_key_t key =
of_file_attribute_key_posix_permissions;
OFNumber *permissions = [attributes objectForKey: key];
of_file_attributes_t destinationAttributes =
[OFDictionary dictionaryWithObject: permissions
forKey: key];
[self setAttributes: destinationAttributes
ofItemAtPath: destination];
ofItemAtURL: destination];
#endif
} @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
exceptionWithSourcePath: source
destinationPath: destination
errNo: [e errNo]];
exceptionWithSourceURL: source
destinationURL: destination
errNo: [e errNo]];
@throw e;
} @finally {
[sourceFile close];
[destinationFile close];
[sourceStream close];
[destinationStream close];
free(buffer);
}
#ifdef OF_FILE_MANAGER_SUPPORTS_SYMLINKS
} else if ([type isEqual: of_file_type_symbolic_link]) {
@try {
OFString *linkDestination =
source = [attributes fileSymbolicLinkDestination];
[attributes fileSymbolicLinkDestination];
[self createSymbolicLinkAtPath: destination
withDestinationPath: source];
[self createSymbolicLinkAtURL: 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
exceptionWithSourcePath: source
destinationPath: destination
errNo: [e errNo]];
exceptionWithSourceURL: source
destinationURL: destination
errNo: [e errNo]];
@throw e;
}
#endif
} else
@throw [OFCopyItemFailedException
exceptionWithSourcePath: source
destinationPath: destination
errNo: EINVAL];
exceptionWithSourceURL: source
destinationURL: destination
errNo: EINVAL];
objc_autoreleasePoolPop(pool);
}
- (void)copyItemAtURL: (OFURL *)source
toURL: (OFURL *)destination
{
void *pool = objc_autoreleasePoolPush();
[self copyItemAtPath: [source fileSystemRepresentation]
toPath: [destination fileSystemRepresentation]];
objc_autoreleasePoolPop(pool);
}
- (void)moveItemAtPath: (OFString *)source
toPath: (OFString *)destination
{
|