@gjsify/fs 0.3.13 → 0.3.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/esm/_virtual/_rolldown/runtime.js +18 -0
- package/lib/esm/callback.js +301 -302
- package/lib/esm/cp.js +207 -227
- package/lib/esm/dir.js +140 -148
- package/lib/esm/dirent.js +137 -128
- package/lib/esm/encoding.js +30 -27
- package/lib/esm/errors.js +15 -11
- package/lib/esm/fd-ops.js +105 -122
- package/lib/esm/file-handle.js +648 -653
- package/lib/esm/fs-watcher.js +153 -161
- package/lib/esm/glob.js +148 -149
- package/lib/esm/index.js +158 -351
- package/lib/esm/promises.js +413 -386
- package/lib/esm/read-stream.js +104 -108
- package/lib/esm/stat-watcher.js +113 -114
- package/lib/esm/statfs.js +56 -43
- package/lib/esm/stats.js +166 -159
- package/lib/esm/sync.js +366 -341
- package/lib/esm/types/index.js +6 -6
- package/lib/esm/utils.js +15 -17
- package/lib/esm/utimes.js +26 -37
- package/lib/esm/write-stream.js +96 -108
- package/package.json +10 -10
package/lib/esm/sync.js
CHANGED
|
@@ -1,393 +1,418 @@
|
|
|
1
|
+
import { decode, encodeUint8Array, getEncodingFromOptions } from "./encoding.js";
|
|
2
|
+
import { normalizePath, tempDirPath } from "./utils.js";
|
|
3
|
+
import { FSWatcher } from "./fs-watcher.js";
|
|
4
|
+
import { Dirent } from "./dirent.js";
|
|
5
|
+
import { BigIntStats, STAT_ATTRIBUTES, Stats } from "./stats.js";
|
|
6
|
+
import { FileHandle } from "./file-handle.js";
|
|
7
|
+
import { createNodeError as createNodeError$1, isNotFoundError } from "./errors.js";
|
|
1
8
|
import GLib from "@girs/glib-2.0";
|
|
2
9
|
import Gio from "@girs/gio-2.0";
|
|
3
|
-
import { existsSync } from "@gjsify/utils";
|
|
4
|
-
import { Buffer } from "node:buffer";
|
|
5
10
|
import { join } from "node:path";
|
|
6
|
-
import
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
import { Stats, BigIntStats, STAT_ATTRIBUTES } from "./stats.js";
|
|
11
|
-
import { createNodeError, isNotFoundError } from "./errors.js";
|
|
12
|
-
import { tempDirPath, normalizePath } from "./utils.js";
|
|
11
|
+
import { Buffer } from "node:buffer";
|
|
12
|
+
import { existsSync } from "@gjsify/utils";
|
|
13
|
+
|
|
14
|
+
//#region src/sync.ts
|
|
13
15
|
function statSync(path, options) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
16
|
+
const pathStr = normalizePath(path);
|
|
17
|
+
try {
|
|
18
|
+
const file = Gio.File.new_for_path(pathStr);
|
|
19
|
+
const info = file.query_info(STAT_ATTRIBUTES, Gio.FileQueryInfoFlags.NONE, null);
|
|
20
|
+
return options?.bigint ? new BigIntStats(info, pathStr) : new Stats(info, pathStr);
|
|
21
|
+
} catch (err) {
|
|
22
|
+
if (options?.throwIfNoEntry === false && isNotFoundError(err)) return undefined;
|
|
23
|
+
throw createNodeError$1(err, "stat", pathStr);
|
|
24
|
+
}
|
|
23
25
|
}
|
|
24
26
|
function lstatSync(path, options) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
27
|
+
const pathStr = normalizePath(path);
|
|
28
|
+
try {
|
|
29
|
+
const file = Gio.File.new_for_path(pathStr);
|
|
30
|
+
const info = file.query_info(STAT_ATTRIBUTES, Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS, null);
|
|
31
|
+
return options?.bigint ? new BigIntStats(info, pathStr) : new Stats(info, pathStr);
|
|
32
|
+
} catch (err) {
|
|
33
|
+
if (options?.throwIfNoEntry === false && isNotFoundError(err)) return undefined;
|
|
34
|
+
throw createNodeError$1(err, "lstat", pathStr);
|
|
35
|
+
}
|
|
34
36
|
}
|
|
35
37
|
function readdirSync(path, options) {
|
|
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
|
-
info = enumerator.next_file(null);
|
|
64
|
-
}
|
|
65
|
-
return result;
|
|
38
|
+
const pathStr = normalizePath(path);
|
|
39
|
+
const file = Gio.File.new_for_path(pathStr);
|
|
40
|
+
const enumerator = file.enumerate_children("standard::name,standard::type", Gio.FileQueryInfoFlags.NONE, null);
|
|
41
|
+
const result = [];
|
|
42
|
+
let info = enumerator.next_file(null);
|
|
43
|
+
while (info !== null) {
|
|
44
|
+
const childName = info.get_name();
|
|
45
|
+
const childPath = join(pathStr, childName);
|
|
46
|
+
if (options?.withFileTypes) {
|
|
47
|
+
result.push(new Dirent(childPath, childName));
|
|
48
|
+
} else {
|
|
49
|
+
result.push(childName);
|
|
50
|
+
}
|
|
51
|
+
if (options?.recursive && info.get_file_type() === Gio.FileType.DIRECTORY) {
|
|
52
|
+
const subEntries = readdirSync(childPath, options);
|
|
53
|
+
for (const entry of subEntries) {
|
|
54
|
+
if (typeof entry === "string") {
|
|
55
|
+
result.push(join(childName, entry));
|
|
56
|
+
} else {
|
|
57
|
+
result.push(entry);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
info = enumerator.next_file(null);
|
|
62
|
+
}
|
|
63
|
+
return result;
|
|
66
64
|
}
|
|
67
65
|
const MAX_SYMLINK_DEPTH = 40;
|
|
68
66
|
function realpathSync(path) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
if (++depth > MAX_SYMLINK_DEPTH) {
|
|
85
|
-
throw new Error(`ELOOP: too many levels of symbolic links, realpath '${pathStr}'`);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
67
|
+
const pathStr = normalizePath(path);
|
|
68
|
+
let current = Gio.File.new_for_path(pathStr);
|
|
69
|
+
let depth = 0;
|
|
70
|
+
while (true) {
|
|
71
|
+
const info = current.query_info("standard::is-symlink,standard::symlink-target", Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS, null);
|
|
72
|
+
if (!info.get_is_symlink()) {
|
|
73
|
+
return current.get_path();
|
|
74
|
+
}
|
|
75
|
+
const target = info.get_symlink_target();
|
|
76
|
+
const parent = current.get_parent();
|
|
77
|
+
current = parent ? parent.resolve_relative_path(target) : Gio.File.new_for_path(target);
|
|
78
|
+
if (++depth > MAX_SYMLINK_DEPTH) {
|
|
79
|
+
throw new Error(`ELOOP: too many levels of symbolic links, realpath '${pathStr}'`);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
88
82
|
}
|
|
89
83
|
realpathSync.native = realpathSync;
|
|
90
84
|
function symlinkSync(target, path, _type) {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
85
|
+
const pathStr = normalizePath(path);
|
|
86
|
+
const targetStr = normalizePath(target);
|
|
87
|
+
const file = Gio.File.new_for_path(pathStr);
|
|
88
|
+
file.make_symbolic_link(targetStr, null);
|
|
95
89
|
}
|
|
96
|
-
function readFileSync(path, options = {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
90
|
+
function readFileSync(path, options = {
|
|
91
|
+
encoding: null,
|
|
92
|
+
flag: "r"
|
|
93
|
+
}) {
|
|
94
|
+
const pathStr = normalizePath(path);
|
|
95
|
+
const file = Gio.File.new_for_path(pathStr);
|
|
96
|
+
try {
|
|
97
|
+
const [ok, data] = file.load_contents(null);
|
|
98
|
+
if (!ok) {
|
|
99
|
+
throw createNodeError$1(new Error("failed to read file"), "read", pathStr);
|
|
100
|
+
}
|
|
101
|
+
return encodeUint8Array(getEncodingFromOptions(options, "buffer"), data);
|
|
102
|
+
} catch (err) {
|
|
103
|
+
if (err.code && typeof err.code === "string") throw err;
|
|
104
|
+
throw createNodeError$1(err, "read", pathStr);
|
|
105
|
+
}
|
|
109
106
|
}
|
|
107
|
+
/**
|
|
108
|
+
* Synchronous mkdir(2) - create a directory.
|
|
109
|
+
* @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
|
110
|
+
* @param options Either the file mode, or an object optionally specifying the file mode and whether parent folders
|
|
111
|
+
* should be created. If a string is passed, it is parsed as an octal integer. If not specified, defaults to `0o777`.
|
|
112
|
+
*/
|
|
110
113
|
function mkdirSync(path, options) {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
114
|
+
let recursive = false;
|
|
115
|
+
let mode = 511;
|
|
116
|
+
if (typeof options === "object") {
|
|
117
|
+
if (options?.recursive) recursive = options.recursive;
|
|
118
|
+
if (options?.mode) mode = options.mode;
|
|
119
|
+
} else {
|
|
120
|
+
mode = options || 511;
|
|
121
|
+
}
|
|
122
|
+
path = normalizePath(path);
|
|
123
|
+
if (typeof mode === "string") {
|
|
124
|
+
throw new TypeError("mode as string is currently not supported!");
|
|
125
|
+
}
|
|
126
|
+
if (recursive) {
|
|
127
|
+
return mkdirSyncRecursive(path, mode);
|
|
128
|
+
}
|
|
129
|
+
const file = Gio.File.new_for_path(path);
|
|
130
|
+
try {
|
|
131
|
+
file.make_directory(null);
|
|
132
|
+
} catch (err) {
|
|
133
|
+
throw createNodeError$1(err, "mkdir", path);
|
|
134
|
+
}
|
|
135
|
+
return undefined;
|
|
133
136
|
}
|
|
137
|
+
/**
|
|
138
|
+
* Recursively creates directories, similar to `mkdir -p`.
|
|
139
|
+
* Returns the first directory path created, or undefined if all directories already existed.
|
|
140
|
+
*/
|
|
134
141
|
function mkdirSyncRecursive(pathStr, mode) {
|
|
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
|
-
|
|
160
|
-
|
|
142
|
+
const file = Gio.File.new_for_path(pathStr);
|
|
143
|
+
try {
|
|
144
|
+
file.make_directory(null);
|
|
145
|
+
return pathStr;
|
|
146
|
+
} catch (err) {
|
|
147
|
+
const gErr = err;
|
|
148
|
+
if (gErr.code === Gio.IOErrorEnum.EXISTS) {
|
|
149
|
+
return undefined;
|
|
150
|
+
}
|
|
151
|
+
if (gErr.code === Gio.IOErrorEnum.NOT_FOUND) {
|
|
152
|
+
const parentPath = join(pathStr, "..");
|
|
153
|
+
const resolvedParent = Gio.File.new_for_path(parentPath).get_path();
|
|
154
|
+
if (resolvedParent === pathStr) {
|
|
155
|
+
throw createNodeError$1(err, "mkdir", pathStr);
|
|
156
|
+
}
|
|
157
|
+
const firstCreated = mkdirSyncRecursive(resolvedParent, mode);
|
|
158
|
+
const retryFile = Gio.File.new_for_path(pathStr);
|
|
159
|
+
try {
|
|
160
|
+
retryFile.make_directory(null);
|
|
161
|
+
} catch (retryErr) {
|
|
162
|
+
throw createNodeError$1(retryErr, "mkdir", pathStr);
|
|
163
|
+
}
|
|
164
|
+
return firstCreated ?? pathStr;
|
|
165
|
+
}
|
|
166
|
+
throw createNodeError$1(err, "mkdir", pathStr);
|
|
167
|
+
}
|
|
161
168
|
}
|
|
169
|
+
/**
|
|
170
|
+
* Synchronous [`rmdir(2)`](http://man7.org/linux/man-pages/man2/rmdir.2.html). Returns `undefined`.
|
|
171
|
+
*
|
|
172
|
+
* Using `fs.rmdirSync()` on a file (not a directory) results in an `ENOENT` error
|
|
173
|
+
* on Windows and an `ENOTDIR` error on POSIX.
|
|
174
|
+
*
|
|
175
|
+
* To get a behavior similar to the `rm -rf` Unix command, use {@link rmSync} with options `{ recursive: true, force: true }`.
|
|
176
|
+
* @since v0.1.21
|
|
177
|
+
*/
|
|
162
178
|
function rmdirSync(path, _options) {
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
179
|
+
const pathStr = normalizePath(path);
|
|
180
|
+
const file = Gio.File.new_for_path(pathStr);
|
|
181
|
+
try {
|
|
182
|
+
const info = file.query_info("standard::type", Gio.FileQueryInfoFlags.NONE, null);
|
|
183
|
+
if (info.get_file_type() !== Gio.FileType.DIRECTORY) {
|
|
184
|
+
const err = Object.assign(new Error(), { code: 4 });
|
|
185
|
+
throw createNodeError$1(err, "rmdir", pathStr);
|
|
186
|
+
}
|
|
187
|
+
const enumerator = file.enumerate_children("standard::name", Gio.FileQueryInfoFlags.NONE, null);
|
|
188
|
+
if (enumerator.next_file(null) !== null) {
|
|
189
|
+
const err = Object.assign(new Error(), { code: 5 });
|
|
190
|
+
throw createNodeError$1(err, "rmdir", pathStr);
|
|
191
|
+
}
|
|
192
|
+
file.delete(null);
|
|
193
|
+
} catch (err) {
|
|
194
|
+
if (err.code && typeof err.code === "string") throw err;
|
|
195
|
+
throw createNodeError$1(err, "rmdir", pathStr);
|
|
196
|
+
}
|
|
181
197
|
}
|
|
182
198
|
function unlinkSync(path) {
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
199
|
+
const pathStr = normalizePath(path);
|
|
200
|
+
const file = Gio.File.new_for_path(pathStr);
|
|
201
|
+
try {
|
|
202
|
+
file.delete(null);
|
|
203
|
+
} catch (err) {
|
|
204
|
+
throw createNodeError$1(err, "unlink", pathStr);
|
|
205
|
+
}
|
|
190
206
|
}
|
|
191
207
|
function writeFileSync(path, data) {
|
|
192
|
-
|
|
208
|
+
GLib.file_set_contents(normalizePath(path), data);
|
|
193
209
|
}
|
|
194
210
|
function renameSync(oldPath, newPath) {
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
211
|
+
const oldStr = normalizePath(oldPath);
|
|
212
|
+
const newStr = normalizePath(newPath);
|
|
213
|
+
const src = Gio.File.new_for_path(oldStr);
|
|
214
|
+
const dest = Gio.File.new_for_path(newStr);
|
|
215
|
+
try {
|
|
216
|
+
src.move(dest, Gio.FileCopyFlags.OVERWRITE, null, null);
|
|
217
|
+
} catch (err) {
|
|
218
|
+
throw createNodeError$1(err, "rename", oldStr, newStr);
|
|
219
|
+
}
|
|
204
220
|
}
|
|
205
221
|
function copyFileSync(src, dest, mode) {
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
222
|
+
const srcStr = normalizePath(src);
|
|
223
|
+
const destStr = normalizePath(dest);
|
|
224
|
+
const srcFile = Gio.File.new_for_path(srcStr);
|
|
225
|
+
const destFile = Gio.File.new_for_path(destStr);
|
|
226
|
+
let flags = Gio.FileCopyFlags.NONE;
|
|
227
|
+
if (mode && (mode & 1) === 0) {
|
|
228
|
+
flags = Gio.FileCopyFlags.OVERWRITE;
|
|
229
|
+
} else if (!mode) {
|
|
230
|
+
flags = Gio.FileCopyFlags.OVERWRITE;
|
|
231
|
+
}
|
|
232
|
+
try {
|
|
233
|
+
srcFile.copy(destFile, flags, null, null);
|
|
234
|
+
} catch (err) {
|
|
235
|
+
throw createNodeError$1(err, "copyfile", srcStr, destStr);
|
|
236
|
+
}
|
|
221
237
|
}
|
|
222
238
|
function accessSync(path, mode) {
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
239
|
+
const pathStr = normalizePath(path);
|
|
240
|
+
const file = Gio.File.new_for_path(pathStr);
|
|
241
|
+
try {
|
|
242
|
+
const info = file.query_info("access::*", Gio.FileQueryInfoFlags.NONE, null);
|
|
243
|
+
if (mode !== undefined && mode !== 0) {
|
|
244
|
+
const permErr = {
|
|
245
|
+
code: 14,
|
|
246
|
+
message: `permission denied, access '${pathStr}'`
|
|
247
|
+
};
|
|
248
|
+
if (mode & 4 && !info.get_attribute_boolean("access::can-read")) {
|
|
249
|
+
throw createNodeError$1(permErr, "access", pathStr);
|
|
250
|
+
}
|
|
251
|
+
if (mode & 2 && !info.get_attribute_boolean("access::can-write")) {
|
|
252
|
+
throw createNodeError$1(permErr, "access", pathStr);
|
|
253
|
+
}
|
|
254
|
+
if (mode & 1 && !info.get_attribute_boolean("access::can-execute")) {
|
|
255
|
+
throw createNodeError$1(permErr, "access", pathStr);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
} catch (err) {
|
|
259
|
+
if (err.code && typeof err.code === "string") throw err;
|
|
260
|
+
throw createNodeError$1(err, "access", pathStr);
|
|
261
|
+
}
|
|
243
262
|
}
|
|
244
263
|
function appendFileSync(path, data, options) {
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
264
|
+
const pathStr = normalizePath(path);
|
|
265
|
+
const file = Gio.File.new_for_path(pathStr);
|
|
266
|
+
let bytes;
|
|
267
|
+
if (typeof data === "string") {
|
|
268
|
+
bytes = new TextEncoder().encode(data);
|
|
269
|
+
} else {
|
|
270
|
+
bytes = data;
|
|
271
|
+
}
|
|
272
|
+
try {
|
|
273
|
+
const stream = file.append_to(Gio.FileCreateFlags.NONE, null);
|
|
274
|
+
if (bytes.length > 0) {
|
|
275
|
+
stream.write_bytes(new GLib.Bytes(bytes), null);
|
|
276
|
+
}
|
|
277
|
+
stream.close(null);
|
|
278
|
+
} catch (err) {
|
|
279
|
+
throw createNodeError$1(err, "appendfile", pathStr);
|
|
280
|
+
}
|
|
262
281
|
}
|
|
263
282
|
function readlinkSync(path, options) {
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
283
|
+
const pathStr = normalizePath(path);
|
|
284
|
+
const file = Gio.File.new_for_path(pathStr);
|
|
285
|
+
try {
|
|
286
|
+
const info = file.query_info("standard::symlink-target", Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS, null);
|
|
287
|
+
const target = info.get_symlink_target();
|
|
288
|
+
if (!target) {
|
|
289
|
+
throw Object.assign(new Error(`EINVAL: invalid argument, readlink '${pathStr}'`), {
|
|
290
|
+
code: "EINVAL",
|
|
291
|
+
errno: -22,
|
|
292
|
+
syscall: "readlink",
|
|
293
|
+
path: pathStr
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
const encoding = typeof options === "string" ? options : options?.encoding;
|
|
297
|
+
if (encoding === "buffer") {
|
|
298
|
+
return Buffer.from(target);
|
|
299
|
+
}
|
|
300
|
+
return target;
|
|
301
|
+
} catch (err) {
|
|
302
|
+
if (typeof err.code === "string") throw err;
|
|
303
|
+
throw createNodeError$1(err, "readlink", pathStr);
|
|
304
|
+
}
|
|
281
305
|
}
|
|
282
306
|
function linkSync(existingPath, newPath) {
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
307
|
+
const existingStr = normalizePath(existingPath);
|
|
308
|
+
const newStr = normalizePath(newPath);
|
|
309
|
+
const result = GLib.spawn_command_line_sync(`ln ${existingStr} ${newStr}`);
|
|
310
|
+
if (!result[0]) {
|
|
311
|
+
throw Object.assign(new Error(`EPERM: operation not permitted, link '${existingStr}' -> '${newStr}'`), {
|
|
312
|
+
code: "EPERM",
|
|
313
|
+
errno: -1,
|
|
314
|
+
syscall: "link",
|
|
315
|
+
path: existingStr,
|
|
316
|
+
dest: newStr
|
|
317
|
+
});
|
|
318
|
+
}
|
|
289
319
|
}
|
|
290
320
|
function truncateSync(path, len) {
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
321
|
+
const pathStr = normalizePath(path);
|
|
322
|
+
const file = Gio.File.new_for_path(pathStr);
|
|
323
|
+
try {
|
|
324
|
+
const stream = file.replace(null, false, Gio.FileCreateFlags.NONE, null);
|
|
325
|
+
if (len && len > 0) {
|
|
326
|
+
const [, data] = file.load_contents(null);
|
|
327
|
+
const truncated = data.slice(0, len);
|
|
328
|
+
if (truncated.length > 0) {
|
|
329
|
+
stream.write_bytes(new GLib.Bytes(truncated), null);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
stream.close(null);
|
|
333
|
+
} catch (err) {
|
|
334
|
+
throw createNodeError$1(err, "truncate", pathStr);
|
|
335
|
+
}
|
|
306
336
|
}
|
|
307
337
|
function chmodSync(path, mode) {
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
338
|
+
const pathStr = normalizePath(path);
|
|
339
|
+
const modeNum = typeof mode === "string" ? parseInt(mode, 8) : mode;
|
|
340
|
+
const result = GLib.spawn_command_line_sync(`chmod ${modeNum.toString(8)} ${pathStr}`);
|
|
341
|
+
if (!result[0]) {
|
|
342
|
+
throw Object.assign(new Error(`EPERM: operation not permitted, chmod '${pathStr}'`), {
|
|
343
|
+
code: "EPERM",
|
|
344
|
+
errno: -1,
|
|
345
|
+
syscall: "chmod",
|
|
346
|
+
path: pathStr
|
|
347
|
+
});
|
|
348
|
+
}
|
|
314
349
|
}
|
|
315
350
|
function chownSync(path, uid, gid) {
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
351
|
+
const pathStr = normalizePath(path);
|
|
352
|
+
const result = GLib.spawn_command_line_sync(`chown ${uid}:${gid} ${pathStr}`);
|
|
353
|
+
if (!result[0]) {
|
|
354
|
+
throw Object.assign(new Error(`EPERM: operation not permitted, chown '${pathStr}'`), {
|
|
355
|
+
code: "EPERM",
|
|
356
|
+
errno: -1,
|
|
357
|
+
syscall: "chown",
|
|
358
|
+
path: pathStr
|
|
359
|
+
});
|
|
360
|
+
}
|
|
321
361
|
}
|
|
322
362
|
function watch(filename, options, listener) {
|
|
323
|
-
|
|
363
|
+
return new FSWatcher(normalizePath(filename), options, listener);
|
|
324
364
|
}
|
|
325
365
|
function openSync(path, flags, mode) {
|
|
326
|
-
|
|
366
|
+
return new FileHandle({
|
|
367
|
+
path,
|
|
368
|
+
flags,
|
|
369
|
+
mode
|
|
370
|
+
});
|
|
327
371
|
}
|
|
328
372
|
function mkdtempSync(prefix, options) {
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
373
|
+
const encoding = getEncodingFromOptions(options);
|
|
374
|
+
const path = tempDirPath(prefix);
|
|
375
|
+
mkdirSync(path, {
|
|
376
|
+
recursive: false,
|
|
377
|
+
mode: 511
|
|
378
|
+
});
|
|
379
|
+
return decode(path, encoding);
|
|
336
380
|
}
|
|
381
|
+
/**
|
|
382
|
+
* Synchronously removes files and directories (modeled on the standard POSIX `rm`utility). Returns `undefined`.
|
|
383
|
+
* @since v14.14.0
|
|
384
|
+
*/
|
|
337
385
|
function rmSync(path, options) {
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
386
|
+
const pathStr = normalizePath(path);
|
|
387
|
+
const file = Gio.File.new_for_path(pathStr);
|
|
388
|
+
const recursive = options?.recursive || false;
|
|
389
|
+
const force = options?.force || false;
|
|
390
|
+
let dirent;
|
|
391
|
+
try {
|
|
392
|
+
dirent = new Dirent(pathStr);
|
|
393
|
+
} catch (err) {
|
|
394
|
+
if (force && isNotFoundError(err)) return;
|
|
395
|
+
throw createNodeError$1(err, "rm", path);
|
|
396
|
+
}
|
|
397
|
+
if (dirent.isDirectory()) {
|
|
398
|
+
const childFiles = readdirSync(path, { withFileTypes: true });
|
|
399
|
+
if (!recursive && childFiles.length) {
|
|
400
|
+
const err = Object.assign(new Error(), { code: 5 });
|
|
401
|
+
throw createNodeError$1(err, "rm", path);
|
|
402
|
+
}
|
|
403
|
+
for (const childFile of childFiles) {
|
|
404
|
+
if (typeof childFile !== "string") {
|
|
405
|
+
rmSync(join(pathStr, childFile.name), options);
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
try {
|
|
410
|
+
file.delete(null);
|
|
411
|
+
} catch (err) {
|
|
412
|
+
if (force && isNotFoundError(err)) return;
|
|
413
|
+
throw createNodeError$1(err, "rm", path);
|
|
414
|
+
}
|
|
367
415
|
}
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
chmodSync,
|
|
372
|
-
chownSync,
|
|
373
|
-
copyFileSync,
|
|
374
|
-
existsSync,
|
|
375
|
-
linkSync,
|
|
376
|
-
lstatSync,
|
|
377
|
-
mkdirSync,
|
|
378
|
-
mkdtempSync,
|
|
379
|
-
openSync,
|
|
380
|
-
readFileSync,
|
|
381
|
-
readdirSync,
|
|
382
|
-
readlinkSync,
|
|
383
|
-
realpathSync,
|
|
384
|
-
renameSync,
|
|
385
|
-
rmSync,
|
|
386
|
-
rmdirSync,
|
|
387
|
-
statSync,
|
|
388
|
-
symlinkSync,
|
|
389
|
-
truncateSync,
|
|
390
|
-
unlinkSync,
|
|
391
|
-
watch,
|
|
392
|
-
writeFileSync
|
|
393
|
-
};
|
|
416
|
+
|
|
417
|
+
//#endregion
|
|
418
|
+
export { accessSync, appendFileSync, chmodSync, chownSync, copyFileSync, existsSync, linkSync, lstatSync, mkdirSync, mkdtempSync, openSync, readFileSync, readdirSync, readlinkSync, realpathSync, renameSync, rmSync, rmdirSync, statSync, symlinkSync, truncateSync, unlinkSync, watch, writeFileSync };
|