ObjFW  Check-in [d92e0d0313]

Overview
Comment:Add +[copyFileAtPath:toPath:] and +[deleteDirectoryAtPath:] to OFFile.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: d92e0d0313048ccf8419e8815b95d9e610dc03d3d5499518758084c898f15b5a
User & Date: js on 2010-04-21 07:15:12
Other Links: manifest | tags
Context
2010-04-22
09:05
Add OF_PATH_DELIM to macros.h. check-in: d931239e25 user: js tags: trunk
2010-04-21
07:15
Add +[copyFileAtPath:toPath:] and +[deleteDirectoryAtPath:] to OFFile. check-in: d92e0d0313 user: js tags: trunk
2010-04-18
13:53
Add +[lastComponentOfPath:] to OFFile. check-in: 2727e4282f user: js tags: trunk
Changes

Modified src/OFExceptions.h from [d52f2d2889] to [51068b190a].

556
557
558
559
560
561
562






















































563
564
565
566
567
568
569
/**
 * \return The new group for the file
 */
- (gid_t)group;
@end
#endif























































/**
 * \brief An exception indicating that renaming a file failed.
 */
@interface OFRenameFileFailedException: OFException
{
	OFString *sourcePath;
	OFString *destinationPath;







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







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
/**
 * \return The new group for the file
 */
- (gid_t)group;
@end
#endif

/**
 * \brief An exception indicating that copying a file failed.
 */
@interface OFCopyFileFailedException: OFException
{
	OFString *sourcePath;
	OFString *destinationPath;
	int errNo;
}

#ifdef OF_HAVE_PROPERTIES
@property (readonly, nonatomic) OFString *sourcePath;
@property (readonly, nonatomic) OFString *destinationPath;
@property (readonly) int errNo;
#endif

/**
 * \param class_ The class of the object which caused the exception
 * \param src The original path
 * \param dst The new path
 * \return A new copy file failed exception
 */
+    newWithClass: (Class)class_
       sourcePath: (OFString*)src
  destinationPath: (OFString*)dst;

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

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

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

/**
 * \return The destination path
 */
- (OFString*)destinationPath;
@end

/**
 * \brief An exception indicating that renaming a file failed.
 */
@interface OFRenameFileFailedException: OFException
{
	OFString *sourcePath;
	OFString *destinationPath;
650
651
652
653
654
655
656











































657
658
659
660
661
662
663
 * \return The errno from when the exception was created
 */
- (int)errNo;

/**
 * \return The path of the file
 */











































- (OFString*)path;
@end

#ifndef _WIN32
/**
 * \brief An exception indicating that creating a link failed.
 */







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







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
 * \return The errno from when the exception was created
 */
- (int)errNo;

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

/**
 * \brief An exception indicating that deleting a directory failed.
 */
@interface OFDeleteDirectoryFailedException: OFException
{
	OFString *path;
	int errNo;
}

#ifdef OF_HAVE_PROPERTIES
@property (readonly, nonatomic) OFString *path;
@property (readonly) int errNo;
#endif

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

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

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

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

#ifndef _WIN32
/**
 * \brief An exception indicating that creating a link failed.
 */

Modified src/OFExceptions.m from [8232f433f5] to [1b38e5bb50].

715
716
717
718
719
720
721


































































722
723
724
725
726
727
728

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



































































@implementation OFRenameFileFailedException
+    newWithClass: (Class)class_
       sourcePath: (OFString*)src
  destinationPath: (OFString*)dst
{
	return [[self alloc] initWithClass: class_







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







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

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

@implementation OFCopyFileFailedException
+    newWithClass: (Class)class_
       sourcePath: (OFString*)src
  destinationPath: (OFString*)dst
{
	return [[self alloc] initWithClass: class_
				sourcePath: src
			   destinationPath: dst];
}

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

-   initWithClass: (Class)class_
       sourcePath: (OFString*)src
  destinationPath: (OFString*)dst
{
	self = [super initWithClass: class_];

	sourcePath = [src copy];
	destinationPath = [dst copy];
	errNo = GET_ERRNO;

	return self;
}

- (void)dealloc
{
	[sourcePath release];
	[destinationPath release];

	[super dealloc];
}

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

	string = [[OFString alloc] initWithFormat:
	    @"Failed to copy file %s to %s in class %s! " ERRFMT,
	    [sourcePath cString], [destinationPath cString],
	    [inClass className], ERRPARAM];

	return string;
}

- (int)errNo
{
	return errNo;
}

- (OFString*)sourcePath
{
	return sourcePath;
}

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

@implementation OFRenameFileFailedException
+    newWithClass: (Class)class_
       sourcePath: (OFString*)src
  destinationPath: (OFString*)dst
{
	return [[self alloc] initWithClass: class_
823
824
825
826
827
828
829























































830
831
832
833
834
835
836
	if (string != nil)
		return string;

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
























































	return string;
}

- (int)errNo
{
	return errNo;
}







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







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
	if (string != nil)
		return string;

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

	return string;
}

- (int)errNo
{
	return errNo;
}

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

@implementation OFDeleteDirectoryFailedException
+ 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];
	errNo = GET_ERRNO;

	return self;
}

- (void)dealloc
{
	[path release];

	[super dealloc];
}

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

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

	return string;
}

- (int)errNo
{
	return errNo;
}

Modified src/OFFile.h from [17e8c1757d] to [8446a405ae].

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 group The new group for the file
 */
+ (void)changeOwnerOfFile: (OFString*)path
		  toOwner: (uid_t)owner
		    group: (gid_t)group;
#endif










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

/**
 * Deletes a file.
 *
 * \param path The path to the file of which should be deleted as a string
 */
+ (void)deleteFileAtPath: (OFString*)path;








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







>
>
>
>
>
>
>
>
>
















>
>
>
>
>
>
>







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
133
134
135
136
137
138
139
140
141
 * \param group The new group for the file
 */
+ (void)changeOwnerOfFile: (OFString*)path
		  toOwner: (uid_t)owner
		    group: (gid_t)group;
#endif

/**
 * Copies a file.
 *
 * \param from The file to copy
 * \param to The destination path
 */
+ (void)copyFileAtPath: (OFString*)from
		toPath: (OFString*)to;

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

/**
 * Deletes a file.
 *
 * \param path The path to the file of which should be deleted as a string
 */
+ (void)deleteFileAtPath: (OFString*)path;

/**
 * Deletes an empty directory.
 *
 * \param path The path to the directory which should be deleted as a string
 */
+ (void)deleteDirectoryAtPath: (OFString*)path;

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

Modified src/OFFile.m from [b686c0a44c] to [9c28a1ea8f].

37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#ifndef S_IRGRP
# define S_IRGRP 0
#endif
#ifndef S_IROTH
# define S_IROTH 0
#endif

#define DEFAULT_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
#define DIR_MODE DEFAULT_MODE | S_IXUSR | S_IXGRP | S_IXOTH

#ifndef _WIN32
# define PATH_DELIM '/'
#else
# define PATH_DELIM '\\'
#endif







|







37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#ifndef S_IRGRP
# define S_IRGRP 0
#endif
#ifndef S_IROTH
# define S_IROTH 0
#endif

#define DEFAULT_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH
#define DIR_MODE DEFAULT_MODE | S_IXUSR | S_IXGRP | S_IXOTH

#ifndef _WIN32
# define PATH_DELIM '/'
#else
# define PATH_DELIM '\\'
#endif
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
	}

	/*
	 * Only one component, but the trailing delimiter might have been
	 * removed, so return a new string anyway.
	 */
	if (i < 0)
		i++;

	return [OFString stringWithCString: path_c + i
				    length: path_len - i];
}

+ (BOOL)fileExistsAtPath: (OFString*)path
{







|







132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
	}

	/*
	 * Only one component, but the trailing delimiter might have been
	 * removed, so return a new string anyway.
	 */
	if (i < 0)
		i = 0;

	return [OFString stringWithCString: path_c + i
				    length: path_len - i];
}

+ (BOOL)fileExistsAtPath: (OFString*)path
{
286
287
288
289
290
291
292














































293
294
295
296
297
298
299
	if (chown([path cString], owner, group))
		@throw [OFChangeFileOwnerFailedException newWithClass: self
								 path: path
								owner: owner
								group: group];
}
#endif















































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







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







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
	if (chown([path cString], owner, group))
		@throw [OFChangeFileOwnerFailedException newWithClass: self
								 path: path
								owner: owner
								group: group];
}
#endif

+ (void)copyFileAtPath: (OFString*)from
		toPath: (OFString*)to
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	BOOL override;
	OFFile *src;
	OFFile *dest;
	char buf[4096];

	if ([self directoryExistsAtPath: to]) {
		OFString *filename = [self lastComponentOfPath: from];
		to = [OFString stringWithPath: to, filename, nil];
	}

	override = [self fileExistsAtPath: to];

	src = nil;
	dest = nil;

	@try {
		src = [OFFile fileWithPath: from
				      mode: @"rb"];
		dest = [OFFile fileWithPath: to
				       mode: @"wb"];

		while (![src atEndOfStream]) {
			size_t len = [src readNBytes: 4096
					  intoBuffer: buf];
			[dest writeNBytes: len
			       fromBuffer: buf];
		}

		if (!override) {
			struct stat s;

			if (fstat(src->fd, &s) == 0)
				fchmod(dest->fd, s.st_mode);
		}
	} @finally {
		[src close];
		[dest close];
	}

	[pool release];
}

+ (void)renameFileAtPath: (OFString*)from
		  toPath: (OFString*)to
{
#ifndef _WIN32
	if (rename([from cString], [to cString]))
#else
310
311
312
313
314
315
316







317
318
319
320
321
322
323
	if (unlink([path cString]))
#else
	if (!DeleteFile([path cString]))
#endif
		@throw [OFDeleteFileFailedException newWithClass: self
							    path: path];
}








#ifndef _WIN32
+ (void)linkFileAtPath: (OFString*)src
		toPath: (OFString*)dest
{
	if (link([src cString], [dest cString]) != 0)
		@throw [OFLinkFailedException newWithClass: self







>
>
>
>
>
>
>







356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
	if (unlink([path cString]))
#else
	if (!DeleteFile([path cString]))
#endif
		@throw [OFDeleteFileFailedException newWithClass: self
							    path: path];
}

+ (void)deleteDirectoryAtPath: (OFString*)path
{
	if (rmdir([path cString]))
		@throw [OFDeleteDirectoryFailedException newWithClass: self
								 path: path];
}

#ifndef _WIN32
+ (void)linkFileAtPath: (OFString*)src
		toPath: (OFString*)dest
{
	if (link([src cString], [dest cString]) != 0)
		@throw [OFLinkFailedException newWithClass: self