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)
|