@gjsify/fs 0.3.13 → 0.3.15
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/cp.js
CHANGED
|
@@ -1,253 +1,233 @@
|
|
|
1
|
-
import Gio from "@girs/gio-2.0";
|
|
2
|
-
import { join } from "node:path";
|
|
3
1
|
import { normalizePath } from "./utils.js";
|
|
4
2
|
import { createNodeError } from "./errors.js";
|
|
3
|
+
import Gio from "@girs/gio-2.0";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
|
|
6
|
+
//#region src/cp.ts
|
|
5
7
|
function makeEEXIST(destStr) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
const e = new Error(`ERR_FS_CP_EEXIST: file already exists, copyfile '${destStr}'`);
|
|
9
|
+
e.code = "ERR_FS_CP_EEXIST";
|
|
10
|
+
e.syscall = "copyfile";
|
|
11
|
+
e.path = destStr;
|
|
12
|
+
return e;
|
|
11
13
|
}
|
|
12
14
|
function makeEISDIR(srcStr) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
const e = new Error(`ERR_FS_EISDIR: illegal operation on a directory, copyfile '${srcStr}'`);
|
|
16
|
+
e.code = "ERR_FS_EISDIR";
|
|
17
|
+
e.syscall = "copyfile";
|
|
18
|
+
e.path = srcStr;
|
|
19
|
+
return e;
|
|
18
20
|
}
|
|
19
21
|
function makeENOTDIR(srcStr, destStr) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
22
|
+
const e = new Error(`ENOTDIR: not a directory, copyfile '${srcStr}' -> '${destStr}'`);
|
|
23
|
+
e.code = "ENOTDIR";
|
|
24
|
+
e.syscall = "copyfile";
|
|
25
|
+
e.path = srcStr;
|
|
26
|
+
e.dest = destStr;
|
|
27
|
+
return e;
|
|
26
28
|
}
|
|
27
29
|
function makeSYMLINKLOOP(srcStr, destStr) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
30
|
+
const e = new Error(`ELOOP: too many levels of symbolic links, copyfile '${srcStr}' -> '${destStr}'`);
|
|
31
|
+
e.code = "ELOOP";
|
|
32
|
+
e.syscall = "copyfile";
|
|
33
|
+
e.path = srcStr;
|
|
34
|
+
e.dest = destStr;
|
|
35
|
+
return e;
|
|
34
36
|
}
|
|
35
37
|
function queryCopyFlags(opts) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
38
|
+
const force = opts.force ?? true;
|
|
39
|
+
let flags = Gio.FileCopyFlags.NONE;
|
|
40
|
+
if (force) flags |= Gio.FileCopyFlags.OVERWRITE;
|
|
41
|
+
if (opts.preserveTimestamps) flags |= Gio.FileCopyFlags.TARGET_DEFAULT_MODIFIED_TIME;
|
|
42
|
+
if (!opts.dereference) flags |= Gio.FileCopyFlags.NOFOLLOW_SYMLINKS;
|
|
43
|
+
return flags;
|
|
42
44
|
}
|
|
43
45
|
function copyOneSyncFile(srcFile, destFile, srcStr, destStr, opts) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
46
|
+
const force = opts.force ?? true;
|
|
47
|
+
try {
|
|
48
|
+
const destInfo = destFile.query_info("standard::type", Gio.FileQueryInfoFlags.NONE, null);
|
|
49
|
+
if (destInfo.get_file_type() === Gio.FileType.DIRECTORY) {
|
|
50
|
+
throw makeENOTDIR(srcStr, destStr);
|
|
51
|
+
}
|
|
52
|
+
if (!force) {
|
|
53
|
+
if (opts.errorOnExist) throw makeEEXIST(destStr);
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
} catch (e) {
|
|
57
|
+
if (typeof e.code === "string") throw e;
|
|
58
|
+
}
|
|
59
|
+
const flags = queryCopyFlags(opts);
|
|
60
|
+
try {
|
|
61
|
+
srcFile.copy(destFile, flags, null, null);
|
|
62
|
+
} catch (err) {
|
|
63
|
+
throw createNodeError(err, "copyfile", srcStr, destStr);
|
|
64
|
+
}
|
|
63
65
|
}
|
|
64
66
|
function cpOneDirSync(srcFile, destFile, srcStr, destStr, opts) {
|
|
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
|
-
cpSyncInternal(childSrc, childDest, opts);
|
|
106
|
-
childInfo = enumerator.next_file(null);
|
|
107
|
-
}
|
|
67
|
+
const sep = srcStr.endsWith("/") ? "" : "/";
|
|
68
|
+
if (destStr.startsWith(srcStr + sep) && destStr !== srcStr) {
|
|
69
|
+
throw makeSYMLINKLOOP(srcStr, destStr);
|
|
70
|
+
}
|
|
71
|
+
if (!destFile.query_exists(null)) {
|
|
72
|
+
try {
|
|
73
|
+
destFile.make_directory_with_parents(null);
|
|
74
|
+
} catch (err) {
|
|
75
|
+
throw createNodeError(err, "mkdir", destStr);
|
|
76
|
+
}
|
|
77
|
+
} else {
|
|
78
|
+
try {
|
|
79
|
+
const destInfo = destFile.query_info("standard::type", Gio.FileQueryInfoFlags.NONE, null);
|
|
80
|
+
if (destInfo.get_file_type() !== Gio.FileType.DIRECTORY) {
|
|
81
|
+
throw makeENOTDIR(destStr, srcStr);
|
|
82
|
+
}
|
|
83
|
+
} catch (e) {
|
|
84
|
+
if (typeof e.code === "string") throw e;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
let enumerator;
|
|
88
|
+
try {
|
|
89
|
+
enumerator = srcFile.enumerate_children("standard::name,standard::type,standard::is-symlink", opts.dereference ? Gio.FileQueryInfoFlags.NONE : Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS, null);
|
|
90
|
+
} catch (err) {
|
|
91
|
+
throw createNodeError(err, "scandir", srcStr);
|
|
92
|
+
}
|
|
93
|
+
let childInfo = enumerator.next_file(null);
|
|
94
|
+
while (childInfo !== null) {
|
|
95
|
+
const name = childInfo.get_name();
|
|
96
|
+
const childSrc = join(srcStr, name);
|
|
97
|
+
const childDest = join(destStr, name);
|
|
98
|
+
const filter = opts.filter;
|
|
99
|
+
if (filter && !filter(childSrc, childDest)) {
|
|
100
|
+
childInfo = enumerator.next_file(null);
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
cpSyncInternal(childSrc, childDest, opts);
|
|
104
|
+
childInfo = enumerator.next_file(null);
|
|
105
|
+
}
|
|
108
106
|
}
|
|
109
107
|
function cpSyncInternal(srcStr, destStr, opts) {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
cpOneDirSync(srcFile, destFile, srcStr, destStr, opts);
|
|
126
|
-
} else {
|
|
127
|
-
copyOneSyncFile(srcFile, destFile, srcStr, destStr, opts);
|
|
128
|
-
}
|
|
108
|
+
const srcFile = Gio.File.new_for_path(srcStr);
|
|
109
|
+
const destFile = Gio.File.new_for_path(destStr);
|
|
110
|
+
let info;
|
|
111
|
+
try {
|
|
112
|
+
info = srcFile.query_info("standard::type,standard::is-symlink", opts.dereference ? Gio.FileQueryInfoFlags.NONE : Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS, null);
|
|
113
|
+
} catch (err) {
|
|
114
|
+
throw createNodeError(err, "stat", srcStr);
|
|
115
|
+
}
|
|
116
|
+
const type = info.get_file_type();
|
|
117
|
+
if (type === Gio.FileType.DIRECTORY) {
|
|
118
|
+
if (!opts.recursive) throw makeEISDIR(srcStr);
|
|
119
|
+
cpOneDirSync(srcFile, destFile, srcStr, destStr, opts);
|
|
120
|
+
} else {
|
|
121
|
+
copyOneSyncFile(srcFile, destFile, srcStr, destStr, opts);
|
|
122
|
+
}
|
|
129
123
|
}
|
|
130
124
|
function cpSync(src, dest, options) {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
if (typeof e.code === "string") throw e;
|
|
148
|
-
throw createNodeError(e, "stat", srcStr);
|
|
149
|
-
}
|
|
150
|
-
cpSyncInternal(srcStr, destStr, opts);
|
|
125
|
+
const srcStr = normalizePath(src);
|
|
126
|
+
const destStr = normalizePath(dest);
|
|
127
|
+
const opts = options ?? {};
|
|
128
|
+
const srcFile = Gio.File.new_for_path(srcStr);
|
|
129
|
+
const filter = opts.filter;
|
|
130
|
+
if (filter && !filter(srcStr, destStr)) return;
|
|
131
|
+
try {
|
|
132
|
+
const info = srcFile.query_info("standard::type", opts.dereference ? Gio.FileQueryInfoFlags.NONE : Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS, null);
|
|
133
|
+
if (info.get_file_type() === Gio.FileType.DIRECTORY && !opts.recursive) {
|
|
134
|
+
throw makeEISDIR(srcStr);
|
|
135
|
+
}
|
|
136
|
+
} catch (e) {
|
|
137
|
+
if (typeof e.code === "string") throw e;
|
|
138
|
+
throw createNodeError(e, "stat", srcStr);
|
|
139
|
+
}
|
|
140
|
+
cpSyncInternal(srcStr, destStr, opts);
|
|
151
141
|
}
|
|
152
142
|
function cp(src, dest, options, callback) {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
143
|
+
let opts;
|
|
144
|
+
let cb;
|
|
145
|
+
if (typeof options === "function") {
|
|
146
|
+
cb = options;
|
|
147
|
+
opts = {};
|
|
148
|
+
} else {
|
|
149
|
+
cb = callback;
|
|
150
|
+
opts = options;
|
|
151
|
+
}
|
|
152
|
+
const asyncFilter = opts.filter;
|
|
153
|
+
if (asyncFilter) {
|
|
154
|
+
Promise.resolve(asyncFilter(normalizePath(src), normalizePath(dest))).then((include) => {
|
|
155
|
+
if (!include) {
|
|
156
|
+
cb(null);
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
try {
|
|
160
|
+
cpPromises(src, dest, opts).then(() => cb(null)).catch(cb);
|
|
161
|
+
} catch (e) {
|
|
162
|
+
cb(e);
|
|
163
|
+
}
|
|
164
|
+
}).catch(cb);
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
Promise.resolve().then(() => {
|
|
168
|
+
try {
|
|
169
|
+
cpSync(src, dest, opts);
|
|
170
|
+
cb(null);
|
|
171
|
+
} catch (e) {
|
|
172
|
+
cb(e);
|
|
173
|
+
}
|
|
174
|
+
});
|
|
185
175
|
}
|
|
186
176
|
async function cpPromisesDir(srcStr, destStr, opts) {
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
}
|
|
223
|
-
await cpPromises(childSrc, childDest, opts);
|
|
224
|
-
childInfo = enumerator.next_file(null);
|
|
225
|
-
}
|
|
177
|
+
const sep = srcStr.endsWith("/") ? "" : "/";
|
|
178
|
+
if (destStr.startsWith(srcStr + sep) && destStr !== srcStr) {
|
|
179
|
+
throw makeSYMLINKLOOP(srcStr, destStr);
|
|
180
|
+
}
|
|
181
|
+
const destFile = Gio.File.new_for_path(destStr);
|
|
182
|
+
if (!destFile.query_exists(null)) {
|
|
183
|
+
try {
|
|
184
|
+
destFile.make_directory_with_parents(null);
|
|
185
|
+
} catch (err) {
|
|
186
|
+
throw createNodeError(err, "mkdir", destStr);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
const srcFile = Gio.File.new_for_path(srcStr);
|
|
190
|
+
let enumerator;
|
|
191
|
+
try {
|
|
192
|
+
enumerator = srcFile.enumerate_children("standard::name,standard::type", opts.dereference ? Gio.FileQueryInfoFlags.NONE : Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS, null);
|
|
193
|
+
} catch (err) {
|
|
194
|
+
throw createNodeError(err, "scandir", srcStr);
|
|
195
|
+
}
|
|
196
|
+
let childInfo = enumerator.next_file(null);
|
|
197
|
+
while (childInfo !== null) {
|
|
198
|
+
const name = childInfo.get_name();
|
|
199
|
+
const childSrc = join(srcStr, name);
|
|
200
|
+
const childDest = join(destStr, name);
|
|
201
|
+
const filter = opts.filter;
|
|
202
|
+
if (filter) {
|
|
203
|
+
const include = await Promise.resolve(filter(childSrc, childDest));
|
|
204
|
+
if (!include) {
|
|
205
|
+
childInfo = enumerator.next_file(null);
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
await cpPromises(childSrc, childDest, opts);
|
|
210
|
+
childInfo = enumerator.next_file(null);
|
|
211
|
+
}
|
|
226
212
|
}
|
|
227
213
|
async function cpPromises(src, dest, opts = {}) {
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
} else {
|
|
245
|
-
const destFile = Gio.File.new_for_path(destStr);
|
|
246
|
-
copyOneSyncFile(srcFile, destFile, srcStr, destStr, opts);
|
|
247
|
-
}
|
|
214
|
+
const srcStr = normalizePath(src);
|
|
215
|
+
const destStr = normalizePath(dest);
|
|
216
|
+
const srcFile = Gio.File.new_for_path(srcStr);
|
|
217
|
+
let info;
|
|
218
|
+
try {
|
|
219
|
+
info = srcFile.query_info("standard::type", opts.dereference ? Gio.FileQueryInfoFlags.NONE : Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS, null);
|
|
220
|
+
} catch (err) {
|
|
221
|
+
throw createNodeError(err, "stat", srcStr);
|
|
222
|
+
}
|
|
223
|
+
if (info.get_file_type() === Gio.FileType.DIRECTORY) {
|
|
224
|
+
if (!opts.recursive) throw makeEISDIR(srcStr);
|
|
225
|
+
await cpPromisesDir(srcStr, destStr, opts);
|
|
226
|
+
} else {
|
|
227
|
+
const destFile = Gio.File.new_for_path(destStr);
|
|
228
|
+
copyOneSyncFile(srcFile, destFile, srcStr, destStr, opts);
|
|
229
|
+
}
|
|
248
230
|
}
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
cpSync
|
|
253
|
-
};
|
|
231
|
+
|
|
232
|
+
//#endregion
|
|
233
|
+
export { cp, cpPromises as cpAsync, cpSync };
|