-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathutils.js
159 lines (150 loc) · 4.34 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
module.exports.parseStat = function parseStat(result) {
return {
dev: String(result.NSFileDeviceIdentifier),
// ino: 48064969, The file system specific "Inode" number for the file.
mode: result.NSFileType | result.NSFilePosixPermissions,
nlink: Number(result.NSFileReferenceCount),
uid: String(result.NSFileOwnerAccountID),
gid: String(result.NSFileGroupOwnerAccountID),
// rdev: 0, A numeric device identifier if the file is considered "special".
size: Number(result.NSFileSize),
// blksize: 4096, The file system block size for i/o operations.
// blocks: 8, The number of blocks allocated for this file.
atimeMs:
Number(result.NSFileModificationDate.timeIntervalSince1970()) * 1000,
mtimeMs:
Number(result.NSFileModificationDate.timeIntervalSince1970()) * 1000,
ctimeMs:
Number(result.NSFileModificationDate.timeIntervalSince1970()) * 1000,
birthtimeMs:
Number(result.NSFileCreationDate.timeIntervalSince1970()) * 1000,
atime: new Date(
Number(result.NSFileModificationDate.timeIntervalSince1970()) * 1000 + 0.5
), // the 0.5 comes from the node source. Not sure why it's added but in doubt...
mtime: new Date(
Number(result.NSFileModificationDate.timeIntervalSince1970()) * 1000 + 0.5
),
ctime: new Date(
Number(result.NSFileModificationDate.timeIntervalSince1970()) * 1000 + 0.5
),
birthtime: new Date(
Number(result.NSFileCreationDate.timeIntervalSince1970()) * 1000 + 0.5
),
isBlockDevice: function () {
return result.NSFileType === NSFileTypeBlockSpecial;
},
isCharacterDevice: function () {
return result.NSFileType === NSFileTypeCharacterSpecial;
},
isDirectory: function () {
return result.NSFileType === NSFileTypeDirectory;
},
isFIFO: function () {
return false;
},
isFile: function () {
return result.NSFileType === NSFileTypeRegular;
},
isSocket: function () {
return result.NSFileType === NSFileTypeSocket;
},
isSymbolicLink: function () {
return result.NSFileType === NSFileTypeSymbolicLink;
},
};
};
var ERRORS = {
EPERM: {
message: "operation not permitted",
errno: -1,
},
ENOENT: {
message: "no such file or directory",
errno: -2,
},
EACCES: {
message: "permission denied",
errno: -13,
},
ENOTDIR: {
message: "not a directory",
errno: -20,
},
EISDIR: {
message: "illegal operation on a directory",
errno: -21,
},
};
function fsError(code, options) {
var error = new Error(
code +
": " +
ERRORS[code].message +
", " +
(options.syscall || "") +
(options.path ? " '" + options.path + "'" : "")
);
Object.keys(options).forEach(function (k) {
error[k] = options[k];
});
error.code = code;
error.errno = ERRORS[code].errno;
return error;
}
module.exports.fsError = fsError;
module.exports.fsErrorForPath = function fsErrorForPath(
path,
shouldBeDir,
err,
syscall
) {
var fileManager = NSFileManager.defaultManager();
var doesExist = fileManager.fileExistsAtPath(path);
if (!doesExist) {
return fsError("ENOENT", {
path: path,
syscall: syscall || "open",
});
}
var isReadable = fileManager.isReadableFileAtPath(path);
if (!isReadable) {
return fsError("EACCES", {
path: path,
syscall: syscall || "open",
});
}
if (typeof shouldBeDir !== "undefined") {
var isDirectory = require("./index").lstatSync(path).isDirectory();
if (isDirectory && !shouldBeDir) {
return fsError("EISDIR", {
path: path,
syscall: syscall || "read",
});
} else if (!isDirectory && shouldBeDir) {
return fsError("ENOTDIR", {
path: path,
syscall: syscall || "read",
});
}
}
return new Error(err || "Unknown error while manipulating " + path);
};
module.exports.encodingFromOptions = function encodingFromOptions(
options,
defaultValue
) {
return options && options.encoding
? String(options.encoding)
: options
? String(options)
: defaultValue;
};
module.exports.NOT_IMPLEMENTED = function NOT_IMPLEMENTED(name) {
return function () {
throw new Error(
"fs." +
name +
" is not implemented yet. If you feel like implementing it, any contribution will be gladly accepted on https://github.com/skpm/fs"
);
};
};