ObjFW  Check-in [bc3cdb9ee9]

Overview
Comment:OFFile: Accept usernames and groupnames instead of UIDs and GIDs.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: bc3cdb9ee91cda934f146a414194a0a33bd47ce8667773761cc7ebe3eddbda4e
User & Date: js on 2010-12-13 23:32:34
Other Links: manifest | tags
Context
2010-12-16
18:22
Allow -Wl,* in objfw-compile. check-in: 12e47e3771 user: js tags: trunk
2010-12-13
23:32
OFFile: Accept usernames and groupnames instead of UIDs and GIDs. check-in: bc3cdb9ee9 user: js tags: trunk
22:36
Rename -[decimalValueAsInteger] to -[decimalValue]. check-in: 7564bf4445 user: js tags: trunk
Changes

Modified src/OFExceptions.h from [997d7c3607] to [501618be41].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
 * Copyright (c) 2008 - 2010
 *   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.
 */

#include <sys/stat.h>

#import "OFObject.h"

@class OFString;

/**
 * \brief An exception indicating an object could not be allocated.











|







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
 * Copyright (c) 2008 - 2010
 *   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.
 */

#include <sys/types.h>

#import "OFObject.h"

@class OFString;

/**
 * \brief An exception indicating an object could not be allocated.
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
#ifndef _WIN32
/**
 * \brief An exception indicating that changing the owner of a file failed.
 */
@interface OFChangeFileOwnerFailedException: OFException
{
	OFString *path;
	uid_t owner;
	gid_t group;
	int errNo;
}

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

/**
 * \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
#endif

/**
 * \brief An exception indicating that copying a file failed.
 */
@interface OFCopyFileFailedException: OFException







|
|





|
|












|
|












|
|














|




|







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
#ifndef _WIN32
/**
 * \brief An exception indicating that changing the owner of a file failed.
 */
@interface OFChangeFileOwnerFailedException: OFException
{
	OFString *path;
	OFString *owner;
	OFString *group;
	int errNo;
}

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

/**
 * \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: (OFString*)owner
	 group: (OFString*)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: (OFString*)owner
	  group: (OFString*)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
 */
- (OFString*)owner;

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

/**
 * \brief An exception indicating that copying a file failed.
 */
@interface OFCopyFileFailedException: OFException

Modified src/OFExceptions.m from [809a668c5e] to [e89fd4d506].

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
}
@end

#ifndef _WIN32
@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_
{
	Class c = isa;
	[self release];
	@throw [OFNotImplementedException newWithClass: c
					      selector: _cmd];
}

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

	@try {
		path  = [path_ copy];
		owner = owner_;
		group = group_;
		errNo = GET_ERRNO;
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[path release];



	[super dealloc];
}

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


	description = [[OFString alloc] initWithFormat:
	    @"Failed to change owner for file %s to %d:%d in class %s! " ERRFMT,

	    [path cString], owner, group, [inClass className], ERRPARAM];











	return description;
}

- (int)errNo
{
	return errNo;
}

- (OFString*)path
{
	return path;
}

- (uid_t)owner
{
	return owner;
}

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

@implementation OFCopyFileFailedException







|
|

















|
|





|
|












>
>









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














|




|







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
}
@end

#ifndef _WIN32
@implementation OFChangeFileOwnerFailedException
+ newWithClass: (Class)class_
	  path: (OFString*)path
	 owner: (OFString*)owner
	 group: (OFString*)group
{
	return [[self alloc] initWithClass: class_
				      path: path
				     owner: owner
				     group: group];
}

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

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

	@try {
		path  = [path_ copy];
		owner = [owner_ copy];
		group = [group_ copy];
		errNo = GET_ERRNO;
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[path release];
	[owner release];
	[group release];

	[super dealloc];
}

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

	if (group == nil)
		description = [[OFString alloc] initWithFormat:
		    @"Failed to change owner for file %s to %s in class %s! "
		    ERRFMT, [path cString], [owner cString],
		    [inClass className], ERRPARAM];
	else if (owner == nil)
		description = [[OFString alloc] initWithFormat:
		    @"Failed to change group for file %s to %s in class %s! "
		    ERRFMT, [path cString], [group cString],
		    [inClass className], ERRPARAM];
	else
		description = [[OFString alloc] initWithFormat:
		    @"Failed to change owner for file %s to %s:%s in class %s! "
		    ERRFMT, [path cString], [owner cString], [group cString],
		    [inClass className], ERRPARAM];

	return description;
}

- (int)errNo
{
	return errNo;
}

- (OFString*)path
{
	return path;
}

- (OFString*)owner
{
	return owner;
}

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

@implementation OFCopyFileFailedException

Modified src/OFFile.h from [32a0876034] to [59c7528bbb].

92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
 *
 * \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;
#endif

/**
 * Copies a file.
 *
 * \param from The file to copy
 * \param to The destination path







|
|







92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
 *
 * \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: (OFString*)owner
		    group: (OFString*)group;
#endif

/**
 * Copies a file.
 *
 * \param from The file to copy
 * \param to The destination path

Modified src/OFFile.m from [fc04feb323] to [71e165dd8a].

15
16
17
18
19
20
21





22
23
24
25

26
27
28
29
30
31
32
#include <string.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>






#import "OFFile.h"
#import "OFString.h"
#import "OFArray.h"

#import "OFAutoreleasePool.h"
#import "OFExceptions.h"
#import "macros.h"

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







>
>
>
>
>




>







15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <string.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>

#ifndef _WIN32
# include <pwd.h>
# include <grp.h>
#endif

#import "OFFile.h"
#import "OFString.h"
#import "OFArray.h"
#import "OFThread.h"
#import "OFAutoreleasePool.h"
#import "OFExceptions.h"
#import "macros.h"

#ifdef _WIN32
# import <windows.h>
#endif
50
51
52
53
54
55
56




57
58
59
60
61
62
63

#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

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





static int parse_mode(const char *mode)
{
	if (!strcmp(mode, "r"))
		return O_RDONLY;
	if (!strcmp(mode, "rb"))
		return O_RDONLY | O_BINARY;







>
>
>
>







56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

#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

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

#ifndef _WIN32
static OFMutex *mutex;
#endif

static int parse_mode(const char *mode)
{
	if (!strcmp(mode, "r"))
		return O_RDONLY;
	if (!strcmp(mode, "rb"))
		return O_RDONLY | O_BINARY;
94
95
96
97
98
99
100








101
102
103
104
105
106
107
	if (self != [OFFile class])
		return;

	of_stdin = [[OFFileSingleton alloc] initWithFileDescriptor: 0];
	of_stdout = [[OFFileSingleton alloc] initWithFileDescriptor: 1];
	of_stderr = [[OFFileSingleton alloc] initWithFileDescriptor: 2];
}









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







>
>
>
>
>
>
>
>







104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
	if (self != [OFFile class])
		return;

	of_stdin = [[OFFileSingleton alloc] initWithFileDescriptor: 0];
	of_stdout = [[OFFileSingleton alloc] initWithFileDescriptor: 1];
	of_stderr = [[OFFileSingleton alloc] initWithFileDescriptor: 2];
}

#ifndef _WIN32
+ (void)initialize
{
	if (self == [OFFile class])
		mutex = [[OFMutex alloc] init];
}
#endif

+ fileWithPath: (OFString*)path
	  mode: (OFString*)mode
{
	return [[[self alloc] initWithPath: path
				      mode: mode] autorelease];
}
283
284
285
286
287
288
289
290




















291
292



















293
294
295
296
297
298
299
300
								path: path
								mode: mode];
#endif
}

#ifndef _WIN32
+ (void)changeOwnerOfFile: (OFString*)path
		  toOwner: (uid_t)owner




















		    group: (gid_t)group
{



















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








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







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
								path: path
								mode: mode];
#endif
}

#ifndef _WIN32
+ (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];

	[mutex lock];

	@try {
		if (owner != nil) {
			struct passwd *pw;

			if ((pw = getpwnam([owner cString])) == NULL)
				@throw [OFChangeFileOwnerFailedException
				    newWithClass: self
					    path: path
					   owner: owner
					   group: group];

			uid = pw->pw_uid;
		}

		if (group != nil) {
			struct group *gr;

			if ((gr = getgrnam([group cString])) == NULL)
				@throw [OFChangeFileOwnerFailedException
				    newWithClass: self
					    path: path
					   owner: owner
					   group: group];

			gid = gr->gr_gid;
		}
	} @finally {
		[mutex unlock];
	}

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