ObjFW  Diff

Differences From Artifact [9f7f9d7344]:

To Artifact [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
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

static OFFileSingleton *of_file_stdin = nil;
static OFFileSingleton *of_file_stdout = nil;
static OFFileSingleton *of_file_stderr = nil;
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];
}

+ standardInput
+ (void)changeModeOfFile: (OFString*)path
		  toMode: (mode_t)mode
{
	if (of_file_stdin == nil)
#ifndef _WIN32
		of_file_stdin = [[OFFileSingleton alloc]
		    initWithFilePointer: stdin];

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

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

								mode: mode];
#else
	return of_file_stdout;
}

	DWORD attrs = GetFileAttributes([path cString]);
+ standardError
{

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

	if (attrs == INVALID_FILE_ATTRIBUTES)
	return of_file_stderr;
}

+ (BOOL)changeModeOfFile: (OFString*)path
		  toMode: (mode_t)mode
{
#ifndef _WIN32
		@throw [OFChangeFileModeFailedException newWithClass: self
								path: path
								mode: mode];

	if ((mode / 100) & 2)
	return (chmod([path cString], mode) == 0 ? YES : NO);
#else
	/*
		attrs &= ~FILE_ATTRIBUTE_READONLY;
	else
		attrs |= FILE_ATTRIBUTE_READONLY;

	 * FIXME: On Win32, change write access
	 */
	return NO;
	if (!SetFileAttributes([path cString], attrs))
		@throw [OFChangeFileModeFailedException newWithClass: self
								path: path
								mode: mode];
#endif
}

+ (BOOL)changeOwnerOfFile: (OFString*)path
		    owner: (uid_t)owner
+ (void)changeOwnerOfFile: (OFString*)path
		  toOwner: (uid_t)owner
		    group: (gid_t)group
{
	/* FIXME: On error, throw exception */
#ifndef _WIN32
	return (chown([path cString], owner, group) == 0 ? YES : NO);
	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
	return NO;
	if (!MoveFile([from cString], [to cString]))
#endif
		@throw [OFRenameFileFailedException newWithClass: self
							    from: from
							      to: to];
}

+ (void)delete: (OFString*)path
{
	/* FIXME: On error, throw exception */
	unlink([path cString]);
#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)