︙ | | |
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
|
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
|
-
-
+
+
+
-
-
+
+
|
{
#ifndef _WIN32
if (mkdir([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
DIR_MODE))
#else
if (mkdir([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE]))
#endif
@throw [OFCreateDirectoryFailedException newWithClass: self
path: path];
@throw [OFCreateDirectoryFailedException
exceptionWithClass: self
path: path];
}
+ (OFArray*)filesInDirectoryAtPath: (OFString*)path
{
OFAutoreleasePool *pool;
OFMutableArray *files = [OFMutableArray array];
#ifndef _WIN32
DIR *dir;
struct dirent *dirent;
if ((dir = opendir([path cStringWithEncoding:
OF_STRING_ENCODING_NATIVE])) == NULL)
@throw [OFOpenFileFailedException newWithClass: self
path: path
@throw [OFOpenFileFailedException exceptionWithClass: self
path: path
mode: @"r"];
@try {
pool = [[OFAutoreleasePool alloc] init];
while ((dirent = readdir(dir)) != NULL) {
OFString *file;
|
︙ | | |
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
-
-
-
+
+
+
|
WIN32_FIND_DATA fd;
pool = [[OFAutoreleasePool alloc] init];
path = [path stringByAppendingString: @"\\*"];
if ((handle = FindFirstFile([path cString], &fd)) ==
INVALID_HANDLE_VALUE)
@throw [OFOpenFileFailedException newWithClass: self
path: path
mode: @"r"];
@throw [OFOpenFileFailedException exceptionWithClass: self
path: path
mode: @"r"];
@try {
OFAutoreleasePool *pool2 = [[OFAutoreleasePool alloc] init];
do {
OFString *file;
|
︙ | | |
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
|
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
|
-
-
+
+
+
-
-
-
+
+
+
+
-
-
-
+
+
+
+
-
-
-
+
+
+
+
-
-
-
+
+
+
-
-
+
+
-
-
-
-
+
+
+
+
-
-
-
-
+
+
+
+
-
-
-
-
+
+
+
+
+
|
return files;
}
+ (void)changeToDirectory: (OFString*)path
{
if (chdir([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE]))
@throw [OFChangeDirectoryFailedException newWithClass: self
path: path];
@throw [OFChangeDirectoryFailedException
exceptionWithClass: self
path: path];
}
#ifndef _PSP
+ (void)changeModeOfFile: (OFString*)path
toMode: (mode_t)mode
{
# ifndef _WIN32
if (chmod([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE], mode))
@throw [OFChangeFileModeFailedException newWithClass: self
path: path
mode: mode];
@throw [OFChangeFileModeFailedException
exceptionWithClass: self
path: path
mode: mode];
# else
DWORD attributes = GetFileAttributes([path cString]);
if (attributes == INVALID_FILE_ATTRIBUTES)
@throw [OFChangeFileModeFailedException newWithClass: self
path: path
mode: mode];
@throw [OFChangeFileModeFailedException
exceptionWithClass: self
path: path
mode: mode];
if ((mode / 100) & 2)
attributes &= ~FILE_ATTRIBUTE_READONLY;
else
attributes |= FILE_ATTRIBUTE_READONLY;
if (!SetFileAttributes([path cString], attributes))
@throw [OFChangeFileModeFailedException newWithClass: self
path: path
mode: mode];
@throw [OFChangeFileModeFailedException
exceptionWithClass: self
path: path
mode: mode];
# endif
}
#endif
+ (OFDate*)modificationDateOfFile: (OFString*)path
{
struct stat s;
if (stat([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
&s) == -1)
/* FIXME: Maybe use another exception? */
@throw [OFOpenFileFailedException newWithClass: self
path: path
mode: @"r"];
@throw [OFOpenFileFailedException exceptionWithClass: self
path: path
mode: @"r"];
/* FIXME: We could be more precise on some OSes */
return [OFDate dateWithTimeIntervalSince1970: s.st_mtime];
}
#if !defined(_WIN32) && !defined(_PSP)
+ (void)changeOwnerOfFile: (OFString*)path
toOwner: (OFString*)owner
group: (OFString*)group
{
uid_t uid = -1;
gid_t gid = -1;
if (owner == nil && group == nil)
@throw [OFInvalidArgumentException newWithClass: self
selector: _cmd];
@throw [OFInvalidArgumentException exceptionWithClass: self
selector: _cmd];
# ifdef OF_THREADS
[mutex lock];
@try {
# endif
if (owner != nil) {
struct passwd *passwd;
if ((passwd = getpwnam([owner cStringWithEncoding:
OF_STRING_ENCODING_NATIVE])) == NULL)
@throw [OFChangeFileOwnerFailedException
newWithClass: self
path: path
owner: owner
group: group];
exceptionWithClass: self
path: path
owner: owner
group: group];
uid = passwd->pw_uid;
}
if (group != nil) {
struct group *group_;
if ((group_ = getgrnam([group cStringWithEncoding:
OF_STRING_ENCODING_NATIVE])) == NULL)
@throw [OFChangeFileOwnerFailedException
newWithClass: self
path: path
owner: owner
group: group];
exceptionWithClass: self
path: path
owner: owner
group: group];
gid = group_->gr_gid;
}
# ifdef OF_THREADS
} @finally {
[mutex unlock];
}
# endif
if (chown([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
uid, gid))
@throw [OFChangeFileOwnerFailedException newWithClass: self
path: path
owner: owner
group: group];
@throw [OFChangeFileOwnerFailedException
exceptionWithClass: self
path: path
owner: owner
group: group];
}
#endif
+ (void)copyFileAtPath: (OFString*)source
toPath: (OFString*)destination
{
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
|
︙ | | |
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
|
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
|
-
-
+
+
|
destination = [OFString stringWithPath: destination, filename,
nil];
}
override = [self fileExistsAtPath: destination];
if ((buffer = malloc(of_pagesize)) == NULL)
@throw [OFOutOfMemoryException newWithClass: self
requestedSize: of_pagesize];
@throw [OFOutOfMemoryException exceptionWithClass: self
requestedSize: of_pagesize];
@try {
sourceFile = [OFFile fileWithPath: source
mode: @"rb"];
destinationFile = [OFFile fileWithPath: destination
mode: @"wb"];
|
︙ | | |
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
|
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
|
-
-
-
+
+
+
+
-
-
+
+
-
-
+
+
+
-
-
-
+
+
+
-
-
-
+
+
+
+
-
-
+
+
-
-
+
+
+
-
-
-
+
+
+
+
|
#ifndef _WIN32
if (rename([source cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
[destination cStringWithEncoding: OF_STRING_ENCODING_NATIVE]))
#else
if (!MoveFile([source cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
[destination cStringWithEncoding: OF_STRING_ENCODING_NATIVE]))
#endif
@throw [OFRenameFileFailedException newWithClass: self
sourcePath: source
destinationPath: destination];
@throw [OFRenameFileFailedException
exceptionWithClass: self
sourcePath: source
destinationPath: destination];
[pool release];
}
+ (void)deleteFileAtPath: (OFString*)path
{
#ifndef _WIN32
if (unlink([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE]))
#else
if (!DeleteFile([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE]))
#endif
@throw [OFDeleteFileFailedException newWithClass: self
path: path];
@throw [OFDeleteFileFailedException exceptionWithClass: self
path: path];
}
+ (void)deleteDirectoryAtPath: (OFString*)path
{
if (rmdir([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE]))
@throw [OFDeleteDirectoryFailedException newWithClass: self
path: path];
@throw [OFDeleteDirectoryFailedException
exceptionWithClass: self
path: path];
}
#ifndef _WIN32
+ (void)linkFileAtPath: (OFString*)source
toPath: (OFString*)destination
{
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
if ([self directoryExistsAtPath: destination]) {
OFString *filename = [source lastPathComponent];
destination = [OFString stringWithPath: destination, filename,
nil];
}
if (link([source cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
[destination cStringWithEncoding: OF_STRING_ENCODING_NATIVE]) != 0)
@throw [OFLinkFailedException newWithClass: self
sourcePath: source
destinationPath: destination];
@throw [OFLinkFailedException exceptionWithClass: self
sourcePath: source
destinationPath: destination];
[pool release];
}
#endif
#if !defined(_WIN32) && !defined(_PSP)
+ (void)symlinkFileAtPath: (OFString*)source
toPath: (OFString*)destination
{
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
if ([self directoryExistsAtPath: destination]) {
OFString *filename = [source lastPathComponent];
destination = [OFString stringWithPath: destination, filename,
nil];
}
if (symlink([source cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
[destination cStringWithEncoding: OF_STRING_ENCODING_NATIVE]) != 0)
@throw [OFSymlinkFailedException newWithClass: self
sourcePath: source
destinationPath: destination];
@throw [OFSymlinkFailedException
exceptionWithClass: self
sourcePath: source
destinationPath: destination];
[pool release];
}
#endif
- init
{
Class c = isa;
[self release];
@throw [OFNotImplementedException newWithClass: c
selector: _cmd];
@throw [OFNotImplementedException exceptionWithClass: c
selector: _cmd];
}
- initWithPath: (OFString*)path
mode: (OFString*)mode
{
self = [super init];
@try {
int flags;
if ((flags = parse_mode([mode cStringWithEncoding:
OF_STRING_ENCODING_NATIVE])) == -1)
@throw [OFInvalidArgumentException newWithClass: isa
selector: _cmd];
@throw [OFInvalidArgumentException
exceptionWithClass: isa
selector: _cmd];
if ((fileDescriptor = open([path cStringWithEncoding:
OF_STRING_ENCODING_NATIVE], flags, DEFAULT_MODE)) == -1)
@throw [OFOpenFileFailedException newWithClass: isa
path: path
mode: mode];
@throw [OFOpenFileFailedException
exceptionWithClass: isa
path: path
mode: mode];
closable = YES;
} @catch (id e) {
[self release];
@throw e;
}
|
︙ | | |
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
|
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
|
-
-
-
+
+
+
-
-
-
+
+
+
-
-
-
-
+
+
+
+
-
-
-
-
+
+
+
+
-
-
-
-
+
+
+
+
|
- (size_t)_readNBytes: (size_t)length
intoBuffer: (void*)buffer
{
size_t ret;
if (fileDescriptor == -1 || atEndOfStream)
@throw [OFReadFailedException newWithClass: isa
stream: self
requestedLength: length];
@throw [OFReadFailedException exceptionWithClass: isa
stream: self
requestedLength: length];
if ((ret = read(fileDescriptor, buffer, length)) == 0)
atEndOfStream = YES;
return ret;
}
- (void)_writeNBytes: (size_t)length
fromBuffer: (const void*)buffer
{
if (fileDescriptor == -1 || atEndOfStream ||
write(fileDescriptor, buffer, length) < length)
@throw [OFWriteFailedException newWithClass: isa
stream: self
requestedLength: length];
@throw [OFWriteFailedException exceptionWithClass: isa
stream: self
requestedLength: length];
}
- (void)_seekToOffset: (off_t)offset
{
if (lseek(fileDescriptor, offset, SEEK_SET) == -1)
@throw [OFSeekFailedException newWithClass: isa
stream: self
offset: offset
whence: SEEK_SET];
@throw [OFSeekFailedException exceptionWithClass: isa
stream: self
offset: offset
whence: SEEK_SET];
}
- (off_t)_seekForwardWithOffset: (off_t)offset
{
off_t ret;
if ((ret = lseek(fileDescriptor, offset, SEEK_CUR)) == -1)
@throw [OFSeekFailedException newWithClass: isa
stream: self
offset: offset
whence: SEEK_CUR];
@throw [OFSeekFailedException exceptionWithClass: isa
stream: self
offset: offset
whence: SEEK_CUR];
return ret;
}
- (off_t)_seekToOffsetRelativeToEnd: (off_t)offset
{
off_t ret;
if ((ret = lseek(fileDescriptor, offset, SEEK_END)) == -1)
@throw [OFSeekFailedException newWithClass: isa
stream: self
offset: offset
whence: SEEK_END];
@throw [OFSeekFailedException exceptionWithClass: isa
stream: self
offset: offset
whence: SEEK_END];
return ret;
}
- (int)fileDescriptor
{
return fileDescriptor;
|
︙ | | |
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
|
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
|
-
-
+
+
|
@implementation OFFileSingleton
- initWithPath: (OFString*)path
mode: (OFString*)mode
{
Class c = isa;
[self release];
@throw [OFNotImplementedException newWithClass: c
selector: _cmd];
@throw [OFNotImplementedException exceptionWithClass: c
selector: _cmd];
}
- autorelease
{
return self;
}
|
︙ | | |
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
|
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
|
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
|
- (unsigned int)retainCount
{
return OF_RETAIN_COUNT_MAX;
}
- (void)dealloc
{
@throw [OFNotImplementedException newWithClass: isa
selector: _cmd];
@throw [OFNotImplementedException exceptionWithClass: isa
selector: _cmd];
[super dealloc]; /* Get rid of stupid warning */
}
- (void)_seekToOffset: (off_t)offset
{
@throw [OFNotImplementedException newWithClass: isa
selector: _cmd];
@throw [OFNotImplementedException exceptionWithClass: isa
selector: _cmd];
}
- (off_t)_seekForwardWithOffset: (off_t)offset
{
@throw [OFNotImplementedException newWithClass: isa
selector: _cmd];
@throw [OFNotImplementedException exceptionWithClass: isa
selector: _cmd];
}
- (off_t)_seekToOffsetRelativeToEnd: (off_t)offset
{
@throw [OFNotImplementedException newWithClass: isa
selector: _cmd];
@throw [OFNotImplementedException exceptionWithClass: isa
selector: _cmd];
}
@end
|