ObjFW  Check-in [5cb7f93a1f]

Overview
Comment:Improve OFFile and add a few new exceptions.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 5cb7f93a1f6fd69e15ab82bc0704c6325244aa530a118396611d65e3c03c76df
User & Date: js on 2009-11-15 01:39:19
Other Links: manifest | tags
Context
2009-11-15
02:10
Improve handling of failed init in OFTLSKey. check-in: 344d7506df user: js tags: trunk
01:39
Improve OFFile and add a few new exceptions. check-in: 5cb7f93a1f user: js tags: trunk
01:17
Add +[load] to OFObject. check-in: 1bae432025 user: js tags: trunk
Changes

Modified src/OFExceptions.h from [26a733beee] to [bb672c63ef].

1
2
3
4
5
6
7
8
9
10
11
12
13

14
15
16
17
18
19
20
/*
 * Copyright (c) 2008 - 2009
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * 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 included in
 * the packaging of this file.
 */

#import "OFObject.h"
#import "OFString.h"


/**
 * An exception indicating an object could not be allocated.
 *
 * This exception is preallocated, as if there's no memory, no exception can
 * be allocated of course. That's why you shouldn't and even can't deallocate
 * it.













>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
 * Copyright (c) 2008 - 2009
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * 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 included in
 * the packaging of this file.
 */

#import "OFObject.h"
#import "OFString.h"
#import "OFFile.h"

/**
 * An exception indicating an object could not be allocated.
 *
 * This exception is preallocated, as if there's no memory, no exception can
 * be allocated of course. That's why you shouldn't and even can't deallocate
 * it.
351
352
353
354
355
356
357
































































































































































































358
359
360
361
362
363
364

/**
 * An OFException indicating a write to the file failed.
 */
@interface OFWriteFailedException: OFReadOrWriteFailedException {}
@end

































































































































































































/**
 * An OFException indicating that creating a link failed.
 */
@interface OFLinkFailedException: OFException
{
	OFString *src;
	OFString *dest;







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557

/**
 * An OFException indicating a write to the file failed.
 */
@interface OFWriteFailedException: OFReadOrWriteFailedException {}
@end

/**
 * An OFException indicating that changing the mode of the file failed.
 */
@interface OFChangeFileModeFailedException: OFException
{
	OFString *path;
	mode_t mode;
	int err;
}

/**
 * \param class_ The class of the object which caused the exception
 * \param path The path of the file
 * \param mode The new mode for the file
 * \return An initialized change file mode failed exception
 */
+ newWithClass: (Class)class_
	  path: (OFString*)path
	  mode: (mode_t)mode;

/**
 * Initializes an already allocated change file mode failed exception.
 *
 * \param class_ The class of the object which caused the exception
 * \param path The path of the file
 * \param mode The new mode for the file
 * \return An initialized change file mode failed exception
 */
- initWithClass: (Class)class_
	   path: (OFString*)path
	   mode: (mode_t)mode;

/**
 * \return The errno from when the exception was created
 */
- (int)errNo;

/**
 * \return The path of the file
 */
- (OFString*)path;

/**
 * \return The new mode for the file
 */
- (mode_t)mode;
@end

/**
 * An OFException indicating that changing the owner of the file failed.
 */
@interface OFChangeFileOwnerFailedException: OFException
{
	OFString *path;
	uid_t owner;
	gid_t group;
	int err;
}

/**
 * \param class_ The class of the object which caused the exception
 * \param path The path of the file
 * \param owner The new owner for the file
 * \param group The new group for the file
 * \return An initialized change file owner failed exception
 */
+ newWithClass: (Class)class_
	  path: (OFString*)path
	 owner: (uid_t)owner
	 group: (gid_t)group;

/**
 * Initializes an already allocated change file owner failed exception.
 *
 * \param class_ The class of the object which caused the exception
 * \param path The path of the file
 * \param owner The new owner for the file
 * \param group The new group for the file
 * \return An initialized change file owner failed exception
 */
- initWithClass: (Class)class_
	   path: (OFString*)path
	  owner: (uid_t)owner
	  group: (gid_t)group;

/**
 * \return The errno from when the exception was created
 */
- (int)errNo;

/**
 * \return The path of the file
 */
- (OFString*)path;

/**
 * \return The new owner for the file
 */
- (uid_t)owner;

/**
 * \return The new group for the file
 */
- (gid_t)group;
@end

/**
 * An OFException indicating that renaming a file failed.
 */
@interface OFRenameFileFailedException: OFException
{
	OFString *from;
	OFString *to;
	int err;
}

/**
 * \param class_ The class of the object which caused the exception
 * \param from The original path
 * \param to The new path
 * \return A new rename file failed exception
 */
+ newWithClass: (Class)class_
	  from: (OFString*)from
	    to: (OFString*)to;

/**
 * Initializes an already allocated rename failed exception.
 *
 * \param class_ The class of the object which caused the exception
 * \param from The original path
 * \param to The new path
 * \return An initialized rename file failed exception
 */
- initWithClass: (Class)class_
	   from: (OFString*)from
	     to: (OFString*)to;

/**
 * \return The errno from when the exception was created
 */
- (int)errNo;

/**
 * \return The original path
 */
- (OFString*)from;

/**
 * \return The new path
 */
- (OFString*)to;
@end

/**
 * An OFException indicating that deleting a file failed.
 */
@interface OFDeleteFileFailedException: OFException
{
	OFString *path;
	int err;
}

/**
 * \param class_ The class of the object which caused the exception
 * \param path The path of the file
 * \return A new delete file failed exception
 */
+ newWithClass: (Class)class_
	  path: (OFString*)path;

/**
 * Initializes an already allocated delete file failed exception.
 *
 * \param class_ The class of the object which caused the exception
 * \param path The path of the file
 * \return An initialized delete file failed exception
 */
- initWithClass: (Class)class_
	   path: (OFString*)path;

/**
 * \return The errno from when the exception was created
 */
- (int)errNo;

/**
 * \return The path of the file
 */
- (OFString*)path;
@end

/**
 * An OFException indicating that creating a link failed.
 */
@interface OFLinkFailedException: OFException
{
	OFString *src;
	OFString *dest;
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
 * \return A new link failed exception
 */
+ newWithClass: (Class)class_
	source: (OFString*)src
   destination: (OFString*)dest;

/**
 * Initializes an already allocated open file failed exception.
 *
 * \param class_ The class of the object which caused the exception
 * \param source The source for the link
 * \param destination The destination for the link
 * \return An initialized link failed exception
 */
- initWithClass: (Class)class_







|







565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
 * \return A new link failed exception
 */
+ newWithClass: (Class)class_
	source: (OFString*)src
   destination: (OFString*)dest;

/**
 * Initializes an already allocated link failed exception.
 *
 * \param class_ The class of the object which caused the exception
 * \param source The source for the link
 * \param destination The destination for the link
 * \return An initialized link failed exception
 */
- initWithClass: (Class)class_
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
 * \return A new symlink failed exception
 */
+ newWithClass: (Class)class_
	source: (OFString*)src
   destination: (OFString*)dest;

/**
 * Initializes an already allocated open file failed exception.
 *
 * \param class_ The class of the object which caused the exception
 * \param source The source for the symlink
 * \param destination The destination for the symlink
 * \return An initialized symlink failed exception
 */
- initWithClass: (Class)class_







|







613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
 * \return A new symlink failed exception
 */
+ newWithClass: (Class)class_
	source: (OFString*)src
   destination: (OFString*)dest;

/**
 * Initializes an already allocated symlink failed exception.
 *
 * \param class_ The class of the object which caused the exception
 * \param source The source for the symlink
 * \param destination The destination for the symlink
 * \return An initialized symlink failed exception
 */
- initWithClass: (Class)class_

Modified src/OFExceptions.m from [f159950386] to [db844d6204].

333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
@end

@implementation OFOpenFileFailedException
+ newWithClass: (Class)class__
	  path: (OFString*)path_
	  mode: (OFString*)mode_
{
	return [[self alloc] initWithClass: class__
				      path: path_
				      mode: mode_];
}

- initWithClass: (Class)class__
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}







|
|
|







333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
@end

@implementation OFOpenFileFailedException
+ newWithClass: (Class)class__
	  path: (OFString*)path_
	  mode: (OFString*)mode_
{
	return [(OFOpenFileFailedException*)[self alloc] initWithClass: class__
								  path: path_
								  mode: mode_];
}

- initWithClass: (Class)class__
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}
509
510
511
512
513
514
515


































































































































































































































































516
517
518
519
520
521
522
		string = [[OFString alloc] initWithFormat:
		    @"Failed to write %zu bytes in class %s! " ERRFMT, req_size,
		    [class_ className], ERRPARAM];

	return string;
}
@end



































































































































































































































































@implementation OFLinkFailedException
+ newWithClass: (Class)class__
	source: (OFString*)src_
   destination: (OFString*)dest_
{
	return [[self alloc] initWithClass: class__







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
		string = [[OFString alloc] initWithFormat:
		    @"Failed to write %zu bytes in class %s! " ERRFMT, req_size,
		    [class_ className], ERRPARAM];

	return string;
}
@end

@implementation OFChangeFileModeFailedException
+ newWithClass: (Class)class__
	  path: (OFString*)path_
	  mode: (mode_t)mode_
{
	return [(OFChangeFileModeFailedException*)[self alloc]
	    initWithClass: class__
		     path: path_
		     mode: mode_];
}

- initWithClass: (Class)class__
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- initWithClass: (Class)class__
	   path: (OFString*)path_
	   mode: (mode_t)mode_
{
	self = [super initWithClass: class__];

	path = [path_ copy];
	mode = mode_;
	err  = GET_ERR;

	return self;
}

- (void)dealloc
{
	[path release];

	[super dealloc];
}

- (OFString*)string
{
	if (string != nil)
		return string;

	string = [[OFString alloc] initWithFormat:
	    @"Failed to change mode for file %s to %d in class %s! " ERRFMT,
	    [path cString], mode, [class_ className], ERRPARAM];

	return string;
}

- (int)errNo
{
	return err;
}

- (OFString*)path
{
	return path;
}

- (mode_t)mode
{
	return mode;
}
@end

@implementation OFChangeFileOwnerFailedException
+ newWithClass: (Class)class__
	  path: (OFString*)path_
	 owner: (uid_t)owner_
	 group: (gid_t)group_
{
	return [[self alloc] initWithClass: class__
				      path: path_
				     owner: owner_
				     group: group_];
}

- initWithClass: (Class)class__
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- initWithClass: (Class)class__
	   path: (OFString*)path_
	  owner: (uid_t)owner_
	  group: (gid_t)group_
{
	self = [super initWithClass: class__];

	path  = [path_ copy];
	owner = owner_;
	group = group_;
	err   = GET_ERR;

	return self;
}

- (void)dealloc
{
	[path release];

	[super dealloc];
}

- (OFString*)string
{
	if (string != nil)
		return string;

	string = [[OFString alloc] initWithFormat:
	    @"Failed to change owner for file %s to %d:%d in class %s! " ERRFMT,
	    [path cString], owner, group, [class_ className], ERRPARAM];

	return string;
}

- (int)errNo
{
	return err;
}

- (OFString*)path
{
	return path;
}

- (uid_t)owner
{
	return owner;
}

- (gid_t)group
{
	return group;
}
@end

@implementation OFRenameFileFailedException
+ newWithClass: (Class)class__
	  from: (OFString*)from_
	    to: (OFString*)to_
{
	return [[self alloc] initWithClass: class__
				      from: from_
					to: to_];
}

- initWithClass: (Class)class__
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- initWithClass: (Class)class__
	   from: (OFString*)from_
	     to: (OFString*)to_
{
	self = [super initWithClass: class__];

	from = [from_ copy];
	to   = [to_ copy];
	err  = GET_ERR;

	return self;
}

- (void)dealloc
{
	[from release];
	[to release];

	[super dealloc];
}

- (OFString*)string
{
	if (string != nil)
		return string;

	string = [[OFString alloc] initWithFormat:
	    @"Failed to rename file %s to %s in class %s! " ERRFMT,
	    [from cString], [to cString], [class_ className], ERRPARAM];

	return string;
}

- (int)errNo
{
	return err;
}

- (OFString*)from
{
	return from;
}

- (OFString*)to
{
	return to;
}
@end

@implementation OFDeleteFileFailedException
+ newWithClass: (Class)class__
	  path: (OFString*)path_
{
	return [[self alloc] initWithClass: class__
				      path: path_];
}

- initWithClass: (Class)class__
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- initWithClass: (Class)class__
	   path: (OFString*)path_
{
	self = [super initWithClass: class__];

	path = [path_ copy];
	err  = GET_ERR;

	return self;
}

- (void)dealloc
{
	[path release];

	[super dealloc];
}

- (OFString*)string
{
	if (string != nil)
		return string;

	string = [[OFString alloc] initWithFormat:
	    @"Failed to delete file %s in class %s! " ERRFMT, [path cString],
	    [class_ className], ERRPARAM];

	return string;
}

- (int)errNo
{
	return err;
}

- (OFString*)path
{
	return path;
}
@end

@implementation OFLinkFailedException
+ newWithClass: (Class)class__
	source: (OFString*)src_
   destination: (OFString*)dest_
{
	return [[self alloc] initWithClass: class__

Modified src/OFFile.h from [bbac3bc2d4] to [b5cc441e5f].

41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90









91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
 * \param fp A file pointer, returned from for example fopen().
 *	     It is not closed when the OFFile object is deallocated!
 * \return A new autoreleased OFFile
 */
+ fileWithFilePointer: (FILE*)fp;

/**
 * \return An OFFile singleton for stdin
 */
+ standardInput;

/**
 * \return An OFFile singleton for stdout
 */
+ standardOutput;

/**
 * \return An OFFile singleton for stderr
 */
+ standardError;

/**
 * Changes the mode of a file.
 *
 * Not available on Windows.
 *
 * \param path The path to the file of which the mode should be changed as a
 *	       string
 * \param mode The new mode for the file
 * \return A boolean whether the operation succeeded
 */
+ (BOOL)changeModeOfFile: (OFString*)path
		  toMode: (mode_t)mode;

/**
 * Changes the owner of a file.
 *
 * Not available on Windows.
 *
 * \param path The path to the file of which the owner should be changed as a
 *	       string
 * \param owner The new owner for the file
 * \param group The new group for the file
 * \return A boolean whether the operation succeeded
 */
+ (BOOL)changeOwnerOfFile: (OFString*)path
		    owner: (uid_t)owner
		    group: (gid_t)group;










/**
 * Deletes a file.
 *
 * \param path The path to the file of which should be deleted as a string
 * \return A boolean whether the operation succeeded
 */
+ (void)delete: (OFString*)path;

/**
 * Hardlinks a file.
 *
 * Not available on Windows.
 *
 * \param src The path to the file of which should be linked as a string
 * \param dest The path to where the file should be linked as a string
 * \return A boolean whether the operation succeeded
 */
+ (void)link: (OFString*)src
	  to: (OFString*)dest;

/**
 * Symlinks a file.
 *
 * Not available on Windows.
 *
 * \param src The path to the file of which should be symlinked as a string
 * \param dest The path to where the file should be symlinked as a string
 * \return A boolean whether the operation succeeded
 */
+ (void)symlink: (OFString*)src
	     to: (OFString*)dest;

/**
 * Initializes an already allocated OFFile.
 *







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<








<

|











<

|
|


>
>
>
>
>
>
>
>
>




<










<











<







41
42
43
44
45
46
47















48
49
50
51
52
53
54
55

56
57
58
59
60
61
62
63
64
65
66
67
68

69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

87
88
89
90
91
92
93
94
95
96

97
98
99
100
101
102
103
104
105
106
107

108
109
110
111
112
113
114
/**
 * \param fp A file pointer, returned from for example fopen().
 *	     It is not closed when the OFFile object is deallocated!
 * \return A new autoreleased OFFile
 */
+ fileWithFilePointer: (FILE*)fp;
















/**
 * Changes the mode of a file.
 *
 * Not available on Windows.
 *
 * \param path The path to the file of which the mode should be changed as a
 *	       string
 * \param mode The new mode for the file

 */
+ (void)changeModeOfFile: (OFString*)path
		  toMode: (mode_t)mode;

/**
 * Changes the owner of a file.
 *
 * Not available on Windows.
 *
 * \param path The path to the file of which the owner should be changed as a
 *	       string
 * \param owner The new owner for the file
 * \param group The new group for the file

 */
+ (void)changeOwnerOfFile: (OFString*)path
		  toOwner: (uid_t)owner
		    group: (gid_t)group;

/**
 * Renames a file.
 *
 * \param from The file to rename
 * \param to The new name
 */
+ (void)rename: (OFString*)from
	    to: (OFString*)to;

/**
 * Deletes a file.
 *
 * \param path The path to the file of which should be deleted as a string

 */
+ (void)delete: (OFString*)path;

/**
 * Hardlinks a file.
 *
 * Not available on Windows.
 *
 * \param src The path to the file of which should be linked as a string
 * \param dest The path to where the file should be linked as a string

 */
+ (void)link: (OFString*)src
	  to: (OFString*)dest;

/**
 * Symlinks a file.
 *
 * Not available on Windows.
 *
 * \param src The path to the file of which should be symlinked as a string
 * \param dest The path to where the file should be symlinked as a string

 */
+ (void)symlink: (OFString*)src
	     to: (OFString*)dest;

/**
 * Initializes an already allocated OFFile.
 *
163
164
165
166
167
168
169







- (size_t)writeNItems: (size_t)nitems
	       ofSize: (size_t)size
	   fromBuffer: (const char*)buf;
@end

@interface OFFileSingleton: OFFile
@end














>
>
>
>
>
>
>
152
153
154
155
156
157
158
159
160
161
162
163
164
165
- (size_t)writeNItems: (size_t)nitems
	       ofSize: (size_t)size
	   fromBuffer: (const char*)buf;
@end

@interface OFFileSingleton: OFFile
@end

/// An OFFile object for stdin.
extern OFFile *of_stdin;
/// An OFFile object for stdout.
extern OFFile *of_stdout;
/// An OFFile object for stderr.
extern OFFile *of_stderr;

Modified src/OFFile.m from [9f7f9d7344] to [9c9c1d09c4].

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
#include <sys/types.h>
#include <sys/stat.h>
#endif

#import "OFFile.h"
#import "OFExceptions.h"





static OFFileSingleton *of_file_stdin = nil;
static OFFileSingleton *of_file_stdout = nil;
static OFFileSingleton *of_file_stderr = nil;

@implementation OFFile










+ fileWithPath: (OFString*)path
	  mode: (OFString*)mode
{
	return [[[self alloc] initWithPath: path
				      mode: mode] autorelease];
}

+ fileWithFilePointer: (FILE*)fp_
{
	return [[[self alloc] initWithFilePointer: fp_] autorelease];
}

+ standardInput

{
	if (of_file_stdin == nil)
		of_file_stdin = [[OFFileSingleton alloc]
		    initWithFilePointer: stdin];

	return of_file_stdin;
}

+ standardOutput
{
	if (of_file_stdout == nil)
		of_file_stdout = [[OFFileSingleton alloc]
		    initWithFilePointer: stdout];

	return of_file_stdout;
}

+ standardError
{
	if (of_file_stderr == nil)
		of_file_stderr = [[OFFileSingleton alloc]
		    initWithFilePointer: stderr];

	return of_file_stderr;
}

+ (BOOL)changeModeOfFile: (OFString*)path
		  toMode: (mode_t)mode
{
#ifndef _WIN32
	return (chmod([path cString], mode) == 0 ? YES : NO);

#else

	/*
	 * FIXME: On Win32, change write access
	 */


	return NO;

#endif
}

+ (BOOL)changeOwnerOfFile: (OFString*)path
		    owner: (uid_t)owner
		    group: (gid_t)group
{
	/* FIXME: On error, throw exception */
#ifndef _WIN32
	return (chown([path cString], owner, group) == 0 ? YES : NO);












#else
	return NO;

#endif



}

+ (void)delete: (OFString*)path
{
	/* FIXME: On error, throw exception */
	unlink([path cString]);





}

+ (void)link: (OFString*)src
	  to: (OFString*)dest
{
#ifndef _WIN32
	if (link([src cString], [dest cString]) != 0)







>
>
>
>
|
|
|


>
>
>
>
>
>
>
>
>
>












|
>

|
<
|
|
<
<
|
<
<
<
<
|
|
<
<
|
<
|
<
<
<
|
<
<
|
|
|
|
|
<
>
|
>
|
<
<
>
>
|
>



|
|


<

|
>
>
>
>
>
>
>
>
>
>
>
>

<
>

>
>
>




|
|
>
>
>
>
>







19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

61
62


63




64
65


66

67



68


69
70
71
72
73

74
75
76
77


78
79
80
81
82
83
84
85
86
87
88

89
90
91
92
93
94
95
96
97
98
99
100
101
102
103

104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <sys/types.h>
#include <sys/stat.h>
#endif

#import "OFFile.h"
#import "OFExceptions.h"

#ifdef _WIN32
#import <windows.h>
#endif

OFFile *of_stdin = nil;
OFFile *of_stdout = nil;
OFFile *of_stderr = nil;

@implementation OFFile
+ (void)load
{
	if (self != [OFFile class])
		return;

	of_stdin = [[OFFileSingleton alloc] initWithFilePointer: stdin];
	of_stdout = [[OFFileSingleton alloc] initWithFilePointer: stdout];
	of_stderr = [[OFFileSingleton alloc] initWithFilePointer: stderr];
}

+ fileWithPath: (OFString*)path
	  mode: (OFString*)mode
{
	return [[[self alloc] initWithPath: path
				      mode: mode] autorelease];
}

+ fileWithFilePointer: (FILE*)fp_
{
	return [[[self alloc] initWithFilePointer: fp_] autorelease];
}

+ (void)changeModeOfFile: (OFString*)path
		  toMode: (mode_t)mode
{
#ifndef _WIN32

	if (chmod([path cString], mode))
		@throw [OFChangeFileModeFailedException newWithClass: self


								path: path




								mode: mode];
#else


	DWORD attrs = GetFileAttributes([path cString]);





	if (attrs == INVALID_FILE_ATTRIBUTES)


		@throw [OFChangeFileModeFailedException newWithClass: self
								path: path
								mode: mode];

	if ((mode / 100) & 2)

		attrs &= ~FILE_ATTRIBUTE_READONLY;
	else
		attrs |= FILE_ATTRIBUTE_READONLY;



	if (!SetFileAttributes([path cString], attrs))
		@throw [OFChangeFileModeFailedException newWithClass: self
								path: path
								mode: mode];
#endif
}

+ (void)changeOwnerOfFile: (OFString*)path
		  toOwner: (uid_t)owner
		    group: (gid_t)group
{

#ifndef _WIN32
	if (chown([path cString], owner, group))
		@throw [OFChangeFileOwnerFailedException newWithClass: self
								 path: path
								owner: owner
								group: group];
#endif
}

+ (void)rename: (OFString*)from
	    to: (OFString*)to
{
#ifndef _WIN32
	if (rename([from cString], [to cString]))
#else

	if (!MoveFile([from cString], [to cString]))
#endif
		@throw [OFRenameFileFailedException newWithClass: self
							    from: from
							      to: to];
}

+ (void)delete: (OFString*)path
{
#ifndef _WIN32
	if (unlink([path cString]))
#else
	if (!DeleteFile([path cString]))
#endif
		@throw [OFDeleteFileFailedException newWithClass: self
							    path: path];
}

+ (void)link: (OFString*)src
	  to: (OFString*)dest
{
#ifndef _WIN32
	if (link([src cString], [dest cString]) != 0)