@archildata/just-bash 0.1.13 → 0.8.1

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/dist/index.js CHANGED
@@ -1,510 +1,602 @@
1
- var __defProp = Object.defineProperty;
2
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
- var __getOwnPropNames = Object.getOwnPropertyNames;
4
- var __hasOwnProp = Object.prototype.hasOwnProperty;
5
- var __esm = (fn, res) => function __init() {
6
- return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
7
- };
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
21
-
22
1
  // src/ArchilFs.ts
23
- var ArchilFs_exports = {};
24
- __export(ArchilFs_exports, {
25
- ArchilFs: () => ArchilFs
26
- });
27
2
  import createDebug from "debug";
28
- var debug, ArchilFs;
29
- var init_ArchilFs = __esm({
30
- "src/ArchilFs.ts"() {
31
- "use strict";
32
- debug = createDebug("archil:fs");
33
- ArchilFs = class {
34
- client;
35
- user;
36
- rootInodeId = 1;
37
- /**
38
- * Create a new ArchilFs adapter
39
- *
40
- * @param client - Connected ArchilClient instance
41
- * @param options - Optional configuration
42
- * @param options.user - Unix user context for permission checks
43
- */
44
- constructor(client, options) {
45
- this.client = client;
46
- this.user = options?.user;
47
- }
48
- // ========================================================================
49
- // Path Resolution
50
- // ========================================================================
51
- /**
52
- * Normalize a path (remove . and .., ensure leading /)
53
- */
54
- normalizePath(path) {
55
- if (!path || path === "") {
56
- return "/";
57
- }
58
- if (!path.startsWith("/")) {
59
- path = "/" + path;
60
- }
61
- const parts = path.split("/").filter((p) => p !== "" && p !== ".");
62
- const result = [];
63
- for (const part of parts) {
64
- if (part === "..") {
65
- result.pop();
66
- } else {
67
- result.push(part);
68
- }
69
- }
70
- return "/" + result.join("/");
71
- }
72
- /**
73
- * Resolve a path to its inode ID, walking the directory tree
74
- */
75
- async resolve(path) {
76
- const normalizedPath = this.normalizePath(path);
77
- const parts = normalizedPath.split("/").filter((p) => p !== "");
78
- let currentInodeId = this.rootInodeId;
79
- for (const part of parts) {
80
- const response = await this.client.lookupInode(currentInodeId, part, { user: this.user });
81
- if (response.inodeId === -1) {
82
- throw new Error(`ENOENT: no such file or directory, '${path}'`);
83
- }
84
- currentInodeId = response.inodeId;
85
- }
86
- const attributes = await this.client.getAttributes(currentInodeId, { user: this.user });
87
- return { inodeId: currentInodeId, attributes };
3
+ var debug = createDebug("archil:fs");
4
+ var ArchilFs = class _ArchilFs {
5
+ client;
6
+ user;
7
+ rootInodeId = 1;
8
+ constructor(client, options) {
9
+ this.client = client;
10
+ this.user = options?.user;
11
+ }
12
+ /**
13
+ * Create an ArchilFs adapter, optionally rooted at a subdirectory.
14
+ *
15
+ * The subdirectory path is resolved eagerly: if any component does not
16
+ * exist or is not a directory, this method throws immediately.
17
+ *
18
+ * @param client - Connected ArchilClient instance
19
+ * @param options - Optional configuration
20
+ * @param options.user - Unix user context for permission checks
21
+ * @param options.subdirectory - Subdirectory to treat as the root of the filesystem
22
+ */
23
+ static async create(client, options) {
24
+ const fs = new _ArchilFs(client, { user: options?.user });
25
+ if (options?.subdirectory) {
26
+ const resolved = await fs.resolveFollow(options.subdirectory);
27
+ if (resolved.attributes.inodeType !== "Directory") {
28
+ throw new Error(`ENOTDIR: subdirectory '${options.subdirectory}' is not a directory`);
88
29
  }
89
- /**
90
- * Resolve parent directory and get child name
91
- */
92
- async resolveParent(path) {
93
- debug("resolveParent raw path=%j (bytes: %o)", path, Buffer.from(path));
94
- const normalizedPath = this.normalizePath(path);
95
- const lastSlash = normalizedPath.lastIndexOf("/");
96
- const parentPath = lastSlash === 0 ? "/" : normalizedPath.substring(0, lastSlash);
97
- const name = normalizedPath.substring(lastSlash + 1);
98
- debug("resolveParent extracted name=%j (bytes: %o)", name, Buffer.from(name));
99
- const { inodeId: parentInodeId } = await this.resolve(parentPath);
100
- return { parentInodeId, name };
30
+ fs.rootInodeId = resolved.inodeId;
31
+ debug("resolved subdirectory '%s' to inode %d", options.subdirectory, fs.rootInodeId);
32
+ }
33
+ return fs;
34
+ }
35
+ // ========================================================================
36
+ // Path Resolution
37
+ // ========================================================================
38
+ /**
39
+ * Normalize a path (remove . and .., ensure leading /)
40
+ */
41
+ normalizePath(path) {
42
+ if (!path || path === "") {
43
+ return "/";
44
+ }
45
+ if (!path.startsWith("/")) {
46
+ path = "/" + path;
47
+ }
48
+ const parts = path.split("/").filter((p) => p !== "" && p !== ".");
49
+ const result = [];
50
+ for (const part of parts) {
51
+ if (part === "..") {
52
+ result.pop();
53
+ } else {
54
+ result.push(part);
101
55
  }
102
- /**
103
- * Convert InodeAttributes to FsStat
104
- */
105
- toStat(attrs) {
106
- return {
107
- isFile: attrs.inodeType === "File",
108
- isDirectory: attrs.inodeType === "Directory",
109
- isSymlink: attrs.inodeType === "Symlink",
110
- mode: attrs.mode,
111
- size: Number(attrs.size),
112
- mtime: new Date(attrs.mtimeMs)
113
- };
56
+ }
57
+ return "/" + result.join("/");
58
+ }
59
+ static MAX_SYMLINKS = 40;
60
+ /**
61
+ * Resolve a path to its inode ID, walking the directory tree.
62
+ * Follows symlinks on intermediate components but NOT on the final
63
+ * component (matching lstat/readlink semantics).
64
+ */
65
+ async resolve(path, symlinkDepth = 0) {
66
+ const normalizedPath = this.normalizePath(path);
67
+ const parts = normalizedPath.split("/").filter((p) => p !== "");
68
+ let currentInodeId = this.rootInodeId;
69
+ let resolvedPath = "/";
70
+ for (let i = 0; i < parts.length; i++) {
71
+ const part = parts[i];
72
+ const response = await this.client.lookupInode(currentInodeId, part, { user: this.user });
73
+ if (response === null) {
74
+ throw new Error(`ENOENT: no such file or directory, '${path}'`);
114
75
  }
115
- // ========================================================================
116
- // IFileSystem Implementation - Read Operations
117
- // ========================================================================
118
- resolvePath(base, ...paths) {
119
- debug("resolvePath base=%s paths=%o", base, paths);
120
- let result = base;
121
- for (const p of paths) {
122
- if (p.startsWith("/")) {
123
- result = p;
124
- } else {
125
- result = result.endsWith("/") ? result + p : result + "/" + p;
126
- }
76
+ const isLast = i === parts.length - 1;
77
+ if (!isLast && response.attributes.inodeType === "Symlink" && response.attributes.symlinkTarget) {
78
+ if (symlinkDepth >= _ArchilFs.MAX_SYMLINKS) {
79
+ throw new Error(`ELOOP: too many levels of symbolic links, '${path}'`);
127
80
  }
128
- const normalized = this.normalizePath(result);
129
- debug("resolvePath result=%s", normalized);
130
- return normalized;
81
+ const targetPath = response.attributes.symlinkTarget.startsWith("/") ? response.attributes.symlinkTarget : this.resolvePath(resolvedPath, response.attributes.symlinkTarget);
82
+ const remaining = parts.slice(i + 1).join("/");
83
+ const fullPath = remaining ? targetPath + "/" + remaining : targetPath;
84
+ return this.resolve(fullPath, symlinkDepth + 1);
131
85
  }
132
- async readFile(path, encoding) {
133
- debug("readFile path=%s encoding=%s", path, encoding);
134
- try {
135
- const buffer = await this.readFileBuffer(path);
136
- debug("readFile got buffer length=%d", buffer.length);
137
- const decoderEncoding = encoding === "binary" ? "latin1" : encoding || "utf-8";
138
- debug("readFile using decoder encoding=%s", decoderEncoding);
139
- const decoder = new TextDecoder(decoderEncoding);
140
- const result = decoder.decode(buffer);
141
- debug("readFile decoded to string length=%d", result.length);
142
- return result;
143
- } catch (err) {
144
- debug("readFile FAILED: %O", err);
145
- throw err;
146
- }
86
+ resolvedPath = resolvedPath === "/" ? "/" + part : resolvedPath + "/" + part;
87
+ currentInodeId = response.inodeId;
88
+ }
89
+ const attributes = await this.client.getAttributes(currentInodeId, { user: this.user });
90
+ return { inodeId: currentInodeId, attributes };
91
+ }
92
+ /**
93
+ * Resolve a path, following symlinks on ALL components including the
94
+ * final one (like stat(2)). Use resolve() when you need lstat semantics.
95
+ */
96
+ async resolveFollow(path, symlinkDepth = 0) {
97
+ if (symlinkDepth >= _ArchilFs.MAX_SYMLINKS) {
98
+ throw new Error(`ELOOP: too many levels of symbolic links, '${path}'`);
99
+ }
100
+ const resolved = await this.resolve(path, symlinkDepth);
101
+ if (resolved.attributes.inodeType === "Symlink" && resolved.attributes.symlinkTarget) {
102
+ const targetPath = resolved.attributes.symlinkTarget.startsWith("/") ? resolved.attributes.symlinkTarget : this.resolvePath(path, "..", resolved.attributes.symlinkTarget);
103
+ return this.resolveFollow(targetPath, symlinkDepth + 1);
104
+ }
105
+ return resolved;
106
+ }
107
+ /**
108
+ * Resolve parent directory and get child name
109
+ */
110
+ async resolveParent(path) {
111
+ debug("resolveParent raw path=%j (bytes: %o)", path, Buffer.from(path));
112
+ const normalizedPath = this.normalizePath(path);
113
+ const lastSlash = normalizedPath.lastIndexOf("/");
114
+ const parentPath = lastSlash === 0 ? "/" : normalizedPath.substring(0, lastSlash);
115
+ const name = normalizedPath.substring(lastSlash + 1);
116
+ debug("resolveParent extracted name=%j (bytes: %o)", name, Buffer.from(name));
117
+ const { inodeId: parentInodeId } = await this.resolve(parentPath);
118
+ return { parentInodeId, name };
119
+ }
120
+ /**
121
+ * Convert InodeAttributes to FsStat
122
+ */
123
+ toStat(attrs) {
124
+ return {
125
+ isFile: attrs.inodeType === "File",
126
+ isDirectory: attrs.inodeType === "Directory",
127
+ isSymbolicLink: attrs.inodeType === "Symlink",
128
+ mode: attrs.mode,
129
+ size: Number(attrs.size),
130
+ mtime: new Date(attrs.mtimeMs)
131
+ };
132
+ }
133
+ static DIR_PAGE_SIZE = 1e3;
134
+ /**
135
+ * Read all directory entries using the paginated API.
136
+ */
137
+ async readAllDirectoryEntries(inodeId) {
138
+ const handle = await this.client.openDirectory(inodeId, { user: this.user });
139
+ try {
140
+ const allEntries = [];
141
+ let cursor;
142
+ for (; ; ) {
143
+ const page = await this.client.readDirectory(
144
+ inodeId,
145
+ handle,
146
+ _ArchilFs.DIR_PAGE_SIZE,
147
+ cursor,
148
+ { user: this.user }
149
+ );
150
+ allEntries.push(...page.entries);
151
+ if (!page.nextCursor) break;
152
+ cursor = page.nextCursor;
147
153
  }
148
- async readFileBuffer(path) {
149
- debug("readFileBuffer path=%s", path);
150
- const { inodeId, attributes } = await this.resolve(path);
151
- debug("readFileBuffer resolved inodeId=%d type=%s size=%d", inodeId, attributes.inodeType, attributes.size);
152
- if (attributes.inodeType !== "File") {
153
- throw new Error(`EISDIR: illegal operation on a directory, read '${path}'`);
154
- }
155
- const size = Number(attributes.size);
156
- if (size === 0) {
157
- debug("readFileBuffer file is empty, returning empty buffer");
158
- return new Uint8Array(0);
159
- }
160
- const MAX_CHUNK = 4 * 1024 * 1024;
161
- if (size <= MAX_CHUNK) {
162
- const buffer = await this.client.readInode(inodeId, 0, size, { user: this.user });
163
- return new Uint8Array(buffer);
164
- }
165
- const result = new Uint8Array(size);
166
- let offset = 0;
167
- while (offset < size) {
168
- const chunkSize = Math.min(MAX_CHUNK, size - offset);
169
- const chunk = await this.client.readInode(inodeId, offset, chunkSize, { user: this.user });
170
- result.set(new Uint8Array(chunk), offset);
171
- offset += chunkSize;
172
- }
173
- return result;
154
+ return allEntries;
155
+ } finally {
156
+ this.client.closeDirectory(inodeId, handle);
157
+ }
158
+ }
159
+ // ========================================================================
160
+ // IFileSystem Implementation - Read Operations
161
+ // ========================================================================
162
+ resolvePath(base, ...paths) {
163
+ debug("resolvePath base=%s paths=%o", base, paths);
164
+ let result = base;
165
+ for (const p of paths) {
166
+ if (p.startsWith("/")) {
167
+ result = p;
168
+ } else {
169
+ result = result.endsWith("/") ? result + p : result + "/" + p;
174
170
  }
175
- async readdir(path) {
176
- debug("readdir path=%s", path);
177
- const { inodeId, attributes } = await this.resolve(path);
178
- if (attributes.inodeType !== "Directory") {
179
- throw new Error(`ENOTDIR: not a directory, scandir '${path}'`);
180
- }
181
- const entries = await this.client.readDirectory(inodeId, { user: this.user });
182
- for (const e of entries) {
183
- debug("readdir entry name=%j (bytes: %o)", e.name, Buffer.from(e.name));
184
- }
185
- return entries.map((e) => e.name).filter((name) => name !== "." && name !== "..");
171
+ }
172
+ const normalized = this.normalizePath(result);
173
+ debug("resolvePath result=%s", normalized);
174
+ return normalized;
175
+ }
176
+ async readFile(path, encoding) {
177
+ debug("readFile path=%s encoding=%s", path, encoding);
178
+ try {
179
+ const buffer = await this.readFileBuffer(path);
180
+ debug("readFile got buffer length=%d", buffer.length);
181
+ const enc = encoding || "utf-8";
182
+ let result;
183
+ if (enc === "base64" || enc === "hex" || enc === "binary") {
184
+ result = Buffer.from(buffer).toString(enc);
185
+ } else {
186
+ result = new TextDecoder(enc).decode(buffer);
186
187
  }
187
- async readdirWithFileTypes(path) {
188
- const { inodeId, attributes } = await this.resolve(path);
189
- if (attributes.inodeType !== "Directory") {
190
- throw new Error(`ENOTDIR: not a directory, scandir '${path}'`);
191
- }
192
- const entries = await this.client.readDirectory(inodeId, { user: this.user });
193
- return entries.filter((e) => e.name !== "." && e.name !== "..").map((e) => ({
194
- name: e.name,
195
- isFile: e.inodeType === "File",
196
- isDirectory: e.inodeType === "Directory",
197
- isSymlink: e.inodeType === "Symlink"
198
- }));
188
+ debug("readFile decoded to string length=%d", result.length);
189
+ return result;
190
+ } catch (err) {
191
+ debug("readFile FAILED: %O", err);
192
+ throw err;
193
+ }
194
+ }
195
+ async readFileBuffer(path) {
196
+ debug("readFileBuffer path=%s", path);
197
+ const { inodeId, attributes } = await this.resolveFollow(path);
198
+ debug("readFileBuffer resolved inodeId=%d type=%s size=%d", inodeId, attributes.inodeType, attributes.size);
199
+ if (attributes.inodeType !== "File") {
200
+ throw new Error(`EISDIR: illegal operation on a directory, read '${path}'`);
201
+ }
202
+ const size = Number(attributes.size);
203
+ if (size === 0) {
204
+ debug("readFileBuffer file is empty, returning empty buffer");
205
+ return new Uint8Array(0);
206
+ }
207
+ const MAX_CHUNK = 4 * 1024 * 1024;
208
+ if (size <= MAX_CHUNK) {
209
+ const buffer = await this.client.readInode(inodeId, 0, size, { user: this.user });
210
+ return new Uint8Array(buffer);
211
+ }
212
+ const result = new Uint8Array(size);
213
+ let offset = 0;
214
+ while (offset < size) {
215
+ const chunkSize = Math.min(MAX_CHUNK, size - offset);
216
+ const chunk = await this.client.readInode(inodeId, offset, chunkSize, { user: this.user });
217
+ result.set(new Uint8Array(chunk), offset);
218
+ offset += chunkSize;
219
+ }
220
+ return result;
221
+ }
222
+ async readdir(path) {
223
+ debug("readdir path=%s", path);
224
+ const { inodeId, attributes } = await this.resolveFollow(path);
225
+ if (attributes.inodeType !== "Directory") {
226
+ throw new Error(`ENOTDIR: not a directory, scandir '${path}'`);
227
+ }
228
+ const entries = await this.readAllDirectoryEntries(inodeId);
229
+ for (const e of entries) {
230
+ debug("readdir entry name=%j (bytes: %o)", e.name, Buffer.from(e.name));
231
+ }
232
+ return entries.map((e) => e.name).filter((name) => name !== "." && name !== "..");
233
+ }
234
+ async readdirWithFileTypes(path) {
235
+ const { inodeId, attributes } = await this.resolveFollow(path);
236
+ if (attributes.inodeType !== "Directory") {
237
+ throw new Error(`ENOTDIR: not a directory, scandir '${path}'`);
238
+ }
239
+ const entries = await this.readAllDirectoryEntries(inodeId);
240
+ return entries.filter((e) => e.name !== "." && e.name !== "..").map((e) => ({
241
+ name: e.name,
242
+ isFile: e.inodeType === "File",
243
+ isDirectory: e.inodeType === "Directory",
244
+ isSymbolicLink: e.inodeType === "Symlink"
245
+ }));
246
+ }
247
+ async stat(path) {
248
+ debug("stat path=%s", path);
249
+ const { attributes } = await this.resolveFollow(path);
250
+ return this.toStat(attributes);
251
+ }
252
+ async lstat(path) {
253
+ debug("lstat path=%s", path);
254
+ const { attributes } = await this.resolve(path);
255
+ return this.toStat(attributes);
256
+ }
257
+ async exists(path) {
258
+ debug("exists path=%s", path);
259
+ try {
260
+ await this.resolve(path);
261
+ debug("exists path=%s -> true", path);
262
+ return true;
263
+ } catch {
264
+ debug("exists path=%s -> false", path);
265
+ return false;
266
+ }
267
+ }
268
+ async readlink(path) {
269
+ const { attributes } = await this.resolve(path);
270
+ if (attributes.inodeType !== "Symlink") {
271
+ throw new Error(`EINVAL: invalid argument, readlink '${path}'`);
272
+ }
273
+ return attributes.symlinkTarget || "";
274
+ }
275
+ async realpath(path, symlinkDepth = 0) {
276
+ const normalizedPath = this.normalizePath(path);
277
+ const parts = normalizedPath.split("/").filter((p) => p !== "");
278
+ let resolvedPath = "/";
279
+ let currentInodeId = this.rootInodeId;
280
+ for (const part of parts) {
281
+ const response = await this.client.lookupInode(currentInodeId, part, { user: this.user });
282
+ if (response === null) {
283
+ throw new Error(`ENOENT: no such file or directory, realpath '${path}'`);
199
284
  }
200
- async stat(path) {
201
- debug("stat path=%s", path);
202
- const { attributes } = await this.resolve(path);
203
- if (attributes.inodeType === "Symlink" && attributes.symlinkTarget) {
204
- const targetPath = attributes.symlinkTarget.startsWith("/") ? attributes.symlinkTarget : this.resolvePath(path, "..", attributes.symlinkTarget);
205
- return this.stat(targetPath);
285
+ const attrs = response.attributes;
286
+ if (attrs.inodeType === "Symlink" && attrs.symlinkTarget) {
287
+ if (symlinkDepth >= _ArchilFs.MAX_SYMLINKS) {
288
+ throw new Error(`ELOOP: too many levels of symbolic links, '${path}'`);
206
289
  }
207
- return this.toStat(attributes);
290
+ const targetPath = attrs.symlinkTarget.startsWith("/") ? attrs.symlinkTarget : this.resolvePath(resolvedPath, attrs.symlinkTarget);
291
+ const resolved = await this.realpath(targetPath, symlinkDepth + 1);
292
+ resolvedPath = resolved;
293
+ const { inodeId } = await this.resolve(resolved);
294
+ currentInodeId = inodeId;
295
+ } else {
296
+ resolvedPath = resolvedPath === "/" ? "/" + part : resolvedPath + "/" + part;
297
+ currentInodeId = response.inodeId;
208
298
  }
209
- async lstat(path) {
210
- debug("lstat path=%s", path);
211
- const { attributes } = await this.resolve(path);
212
- return this.toStat(attributes);
299
+ }
300
+ return resolvedPath;
301
+ }
302
+ // ========================================================================
303
+ // IFileSystem Implementation - Write Operations
304
+ // ========================================================================
305
+ async writeFile(path, content) {
306
+ debug("writeFile path=%s contentLength=%d", path, content.length);
307
+ const data = typeof content === "string" ? new TextEncoder().encode(content) : content;
308
+ let resolved;
309
+ try {
310
+ resolved = await this.resolveFollow(path);
311
+ } catch (err) {
312
+ if (err instanceof Error && err.message.includes("ENOENT")) {
313
+ resolved = null;
314
+ } else {
315
+ throw err;
213
316
  }
214
- async exists(path) {
215
- debug("exists path=%s", path);
317
+ }
318
+ if (resolved !== null) {
319
+ debug("writeFile resolved existing file path=%s inodeId=%d", path, resolved.inodeId);
320
+ if (resolved.attributes.inodeType !== "File") {
321
+ throw new Error(`EISDIR: illegal operation on a directory, write '${path}'`);
322
+ }
323
+ const realPath = await this.realpath(path);
324
+ const { parentInodeId: parentInodeId2, name: name2 } = await this.resolveParent(realPath);
325
+ const tmpName = `.~tmp-${Math.random().toString(36).slice(2)}`;
326
+ debug("writeFile atomic overwrite via temp=%s", tmpName);
327
+ const tmpResult = await this.client.create(
328
+ parentInodeId2,
329
+ tmpName,
330
+ {
331
+ inodeType: "File",
332
+ uid: resolved.attributes.uid,
333
+ gid: resolved.attributes.gid,
334
+ mode: resolved.attributes.mode
335
+ },
336
+ { user: this.user }
337
+ );
338
+ try {
339
+ await this.client.writeData(tmpResult.inodeId, 0, Buffer.from(data), { user: this.user });
340
+ await this.client.rename(parentInodeId2, tmpName, parentInodeId2, name2, { user: this.user });
341
+ debug("writeFile atomic overwrite succeeded");
342
+ } catch (writeErr) {
216
343
  try {
217
- await this.resolve(path);
218
- debug("exists path=%s -> true", path);
219
- return true;
344
+ await this.client.unlink(parentInodeId2, tmpName, { user: this.user });
220
345
  } catch {
221
- debug("exists path=%s -> false", path);
222
- return false;
223
346
  }
347
+ throw writeErr;
224
348
  }
225
- async readlink(path) {
226
- const { attributes } = await this.resolve(path);
227
- if (attributes.inodeType !== "Symlink") {
228
- throw new Error(`EINVAL: invalid argument, readlink '${path}'`);
229
- }
230
- return attributes.symlinkTarget || "";
349
+ return;
350
+ }
351
+ debug("writeFile file doesn't exist, creating: %s", path);
352
+ const { parentInodeId, name } = await this.resolveParent(path);
353
+ debug("writeFile resolved parent parentInodeId=%d name=%s", parentInodeId, name);
354
+ const result = await this.client.create(
355
+ parentInodeId,
356
+ name,
357
+ {
358
+ inodeType: "File",
359
+ uid: this.user?.uid ?? 0,
360
+ gid: this.user?.gid ?? 0,
361
+ mode: 420
362
+ },
363
+ { user: this.user }
364
+ );
365
+ debug("writeFile create succeeded inodeId=%d", result.inodeId);
366
+ await this.client.writeData(result.inodeId, 0, Buffer.from(data), { user: this.user });
367
+ debug("writeFile write succeeded");
368
+ }
369
+ async appendFile(path, content) {
370
+ const data = typeof content === "string" ? new TextEncoder().encode(content) : content;
371
+ let inodeId;
372
+ let size;
373
+ try {
374
+ const resolved = await this.resolveFollow(path);
375
+ if (resolved.attributes.inodeType !== "File") {
376
+ throw new Error(`EISDIR: illegal operation on a directory, write '${path}'`);
231
377
  }
232
- async realpath(path) {
233
- const normalizedPath = this.normalizePath(path);
234
- const parts = normalizedPath.split("/").filter((p) => p !== "");
235
- let resolvedPath = "/";
236
- let currentInodeId = this.rootInodeId;
237
- for (const part of parts) {
238
- const response = await this.client.lookupInode(currentInodeId, part, { user: this.user });
239
- if (response.inodeId === -1) {
240
- throw new Error(`ENOENT: no such file or directory, realpath '${path}'`);
241
- }
242
- const attrs = response.attributes;
243
- if (attrs.inodeType === "Symlink" && attrs.symlinkTarget) {
244
- const targetPath = attrs.symlinkTarget.startsWith("/") ? attrs.symlinkTarget : this.resolvePath(resolvedPath, attrs.symlinkTarget);
245
- const resolved = await this.realpath(targetPath);
246
- resolvedPath = resolved;
247
- const { inodeId } = await this.resolve(resolved);
248
- currentInodeId = inodeId;
249
- } else {
250
- resolvedPath = resolvedPath === "/" ? "/" + part : resolvedPath + "/" + part;
251
- currentInodeId = response.inodeId;
252
- }
253
- }
254
- return resolvedPath;
378
+ inodeId = resolved.inodeId;
379
+ size = Number(resolved.attributes.size);
380
+ } catch (err) {
381
+ if (err instanceof Error && err.message.includes("ENOENT")) {
382
+ await this.writeFile(path, data);
383
+ return;
255
384
  }
256
- // ========================================================================
257
- // IFileSystem Implementation - Write Operations
258
- // ========================================================================
259
- async writeFile(path, content) {
260
- debug("writeFile path=%s contentLength=%d", path, content.length);
261
- const data = typeof content === "string" ? new TextEncoder().encode(content) : content;
262
- let inodeId;
263
- try {
264
- const resolved = await this.resolve(path);
265
- inodeId = resolved.inodeId;
266
- debug("writeFile resolved existing file path=%s inodeId=%d", path, inodeId);
267
- if (resolved.attributes.inodeType !== "File") {
268
- throw new Error(`EISDIR: illegal operation on a directory, write '${path}'`);
269
- }
270
- } catch (err) {
271
- if (err instanceof Error && err.message.includes("EISDIR")) {
272
- throw err;
273
- }
274
- debug("writeFile file doesn't exist, creating: %s", path);
275
- const { parentInodeId, name } = await this.resolveParent(path);
276
- debug("writeFile resolved parent parentInodeId=%d name=%s", parentInodeId, name);
277
- const now = Date.now();
278
- debug("writeFile calling create parent=%d name=%s", parentInodeId, name);
279
- try {
280
- inodeId = await this.client.create(
281
- parentInodeId,
282
- name,
283
- {
284
- inodeId: 0,
285
- // Will be assigned by the filesystem
286
- inodeType: "File",
287
- size: 0,
288
- uid: this.user?.uid ?? 0,
289
- gid: this.user?.gid ?? 0,
290
- mode: 420,
291
- nlink: 1,
292
- ctimeMs: now,
293
- atimeMs: now,
294
- mtimeMs: now,
295
- btimeMs: now,
296
- rdev: void 0,
297
- symlinkTarget: void 0
298
- },
299
- { user: this.user }
300
- );
301
- debug("writeFile create succeeded inodeId=%d", inodeId);
302
- } catch (createErr) {
303
- debug("writeFile create FAILED: %O", createErr);
304
- throw createErr;
305
- }
306
- }
307
- debug("writeFile writing %d bytes to inodeId=%d", data.length, inodeId);
308
- await this.client.writeData(inodeId, 0, Buffer.from(data), { user: this.user });
309
- debug("writeFile write succeeded");
310
- }
311
- async appendFile(path, content) {
312
- let existing;
313
- try {
314
- existing = await this.readFileBuffer(path);
315
- } catch {
316
- existing = new Uint8Array(0);
385
+ throw err;
386
+ }
387
+ await this.client.writeData(inodeId, size, Buffer.from(data), { user: this.user });
388
+ }
389
+ async mkdir(path, options) {
390
+ const normalizedPath = this.normalizePath(path);
391
+ if (options?.recursive) {
392
+ const parts = normalizedPath.split("/").filter((p) => p !== "");
393
+ let currentPath = "";
394
+ for (const part of parts) {
395
+ currentPath += "/" + part;
396
+ const exists = await this.exists(currentPath);
397
+ if (exists) {
398
+ continue;
317
399
  }
318
- const data = typeof content === "string" ? new TextEncoder().encode(content) : content;
319
- const combined = new Uint8Array(existing.length + data.length);
320
- combined.set(existing, 0);
321
- combined.set(data, existing.length);
322
- await this.writeFile(path, combined);
400
+ await this.mkdirSingle(currentPath);
323
401
  }
324
- async mkdir(path, options) {
325
- const normalizedPath = this.normalizePath(path);
326
- if (options?.recursive) {
327
- const parts = normalizedPath.split("/").filter((p) => p !== "");
328
- let currentPath = "";
329
- for (const part of parts) {
330
- currentPath += "/" + part;
331
- const exists = await this.exists(currentPath);
332
- if (exists) {
333
- continue;
334
- }
335
- await this.mkdirSingle(currentPath);
336
- }
337
- } else {
338
- await this.mkdirSingle(normalizedPath);
402
+ } else {
403
+ await this.mkdirSingle(normalizedPath);
404
+ }
405
+ }
406
+ async mkdirSingle(path) {
407
+ const { parentInodeId, name } = await this.resolveParent(path);
408
+ await this.client.create(
409
+ parentInodeId,
410
+ name,
411
+ {
412
+ inodeType: "Directory",
413
+ uid: this.user?.uid ?? 0,
414
+ gid: this.user?.gid ?? 0,
415
+ mode: 493
416
+ },
417
+ { user: this.user }
418
+ );
419
+ }
420
+ async rm(path, options) {
421
+ let resolved;
422
+ try {
423
+ resolved = await this.resolve(path);
424
+ } catch (err) {
425
+ if (err instanceof Error && err.message.includes("ENOENT")) {
426
+ if (options?.force) {
427
+ return;
339
428
  }
429
+ throw err;
340
430
  }
341
- async mkdirSingle(path) {
342
- const { parentInodeId, name } = await this.resolveParent(path);
343
- const now = Date.now();
344
- await this.client.create(
345
- parentInodeId,
346
- name,
347
- {
348
- inodeId: 0,
349
- // Will be assigned by the filesystem
350
- inodeType: "Directory",
351
- size: 4096,
352
- uid: this.user?.uid ?? 0,
353
- gid: this.user?.gid ?? 0,
354
- mode: 493,
355
- nlink: 2,
356
- ctimeMs: now,
357
- atimeMs: now,
358
- mtimeMs: now,
359
- btimeMs: now,
360
- rdev: void 0,
361
- symlinkTarget: void 0
362
- },
363
- { user: this.user }
364
- );
365
- }
366
- async rm(path, options) {
367
- let resolved;
368
- try {
369
- resolved = await this.resolve(path);
370
- } catch {
371
- if (options?.force) {
372
- return;
373
- }
374
- throw new Error(`ENOENT: no such file or directory, rm '${path}'`);
375
- }
376
- if (resolved.attributes.inodeType === "Directory") {
377
- if (!options?.recursive) {
378
- throw new Error(`EISDIR: illegal operation on a directory, rm '${path}'`);
379
- }
380
- const entries = await this.readdir(path);
381
- for (const entry of entries) {
382
- await this.rm(this.resolvePath(path, entry), options);
383
- }
384
- }
385
- const { parentInodeId, name } = await this.resolveParent(path);
386
- await this.client.unlink(parentInodeId, name, { user: this.user });
431
+ throw err;
432
+ }
433
+ if (resolved.attributes.inodeType === "Directory") {
434
+ if (!options?.recursive) {
435
+ throw new Error(`EISDIR: illegal operation on a directory, rm '${path}'`);
387
436
  }
388
- async cp(src, dest, options) {
389
- const srcResolved = await this.resolve(src);
390
- if (srcResolved.attributes.inodeType === "Directory") {
391
- if (!options?.recursive) {
392
- throw new Error(`EISDIR: illegal operation on a directory, cp '${src}'`);
393
- }
394
- await this.mkdir(dest, { recursive: true });
395
- const entries = await this.readdir(src);
396
- for (const entry of entries) {
397
- await this.cp(
398
- this.resolvePath(src, entry),
399
- this.resolvePath(dest, entry),
400
- options
401
- );
402
- }
403
- } else {
404
- const content = await this.readFileBuffer(src);
405
- await this.writeFile(dest, content);
406
- }
437
+ const entries = await this.readdir(path);
438
+ for (const entry of entries) {
439
+ await this.rm(this.resolvePath(path, entry), options);
407
440
  }
408
- async mv(src, dest) {
409
- const { parentInodeId: srcParentInodeId, name: srcName } = await this.resolveParent(src);
410
- const { parentInodeId: destParentInodeId, name: destName } = await this.resolveParent(dest);
411
- await this.client.rename(
412
- srcParentInodeId,
413
- srcName,
414
- destParentInodeId,
415
- destName,
416
- { user: this.user }
417
- );
441
+ }
442
+ const { parentInodeId, name } = await this.resolveParent(path);
443
+ await this.client.unlink(parentInodeId, name, { user: this.user });
444
+ }
445
+ async cp(src, dest, options) {
446
+ const srcResolved = await this.resolveFollow(src);
447
+ if (srcResolved.attributes.inodeType === "Directory") {
448
+ if (!options?.recursive) {
449
+ throw new Error(`EISDIR: illegal operation on a directory, cp '${src}'`);
418
450
  }
419
- async symlink(target, path) {
420
- const { parentInodeId, name } = await this.resolveParent(path);
421
- const now = Date.now();
422
- await this.client.create(
423
- parentInodeId,
424
- name,
425
- {
426
- inodeId: 0,
427
- // Will be assigned by the filesystem
428
- inodeType: "Symlink",
429
- size: target.length,
430
- uid: this.user?.uid ?? 0,
431
- gid: this.user?.gid ?? 0,
432
- mode: 511,
433
- nlink: 1,
434
- ctimeMs: now,
435
- atimeMs: now,
436
- mtimeMs: now,
437
- btimeMs: now,
438
- rdev: void 0,
439
- symlinkTarget: target
440
- },
441
- { user: this.user }
451
+ await this.mkdir(dest, { recursive: true });
452
+ const entries = await this.readdir(src);
453
+ for (const entry of entries) {
454
+ await this.cp(
455
+ this.resolvePath(src, entry),
456
+ this.resolvePath(dest, entry),
457
+ options
442
458
  );
443
459
  }
444
- async link(existingPath, newPath) {
445
- throw new Error(
446
- "Hard link operations not yet implemented. The archil-node bindings need to expose link for hard links."
447
- );
460
+ } else {
461
+ const content = await this.readFileBuffer(src);
462
+ await this.writeFile(dest, content);
463
+ }
464
+ }
465
+ async mv(src, dest) {
466
+ const { parentInodeId: srcParentInodeId, name: srcName } = await this.resolveParent(src);
467
+ const { parentInodeId: destParentInodeId, name: destName } = await this.resolveParent(dest);
468
+ await this.client.rename(
469
+ srcParentInodeId,
470
+ srcName,
471
+ destParentInodeId,
472
+ destName,
473
+ { user: this.user }
474
+ );
475
+ }
476
+ async symlink(target, path) {
477
+ const { parentInodeId, name } = await this.resolveParent(path);
478
+ await this.client.create(
479
+ parentInodeId,
480
+ name,
481
+ {
482
+ inodeType: "Symlink",
483
+ uid: this.user?.uid ?? 0,
484
+ gid: this.user?.gid ?? 0,
485
+ mode: 511,
486
+ symlinkTarget: target
487
+ },
488
+ { user: this.user }
489
+ );
490
+ }
491
+ async link(existingPath, newPath) {
492
+ throw new Error(
493
+ "Hard link operations not yet implemented. The archil-node bindings need to expose link for hard links."
494
+ );
495
+ }
496
+ async chmod(path, mode) {
497
+ const { inodeId } = await this.resolveFollow(path);
498
+ await this.client.setattr(inodeId, { mode }, { user: this.user ?? { uid: 0, gid: 0 } });
499
+ }
500
+ async utimes(path, atime, mtime) {
501
+ debug("utimes path=%s atime=%s mtime=%s", path, atime.toISOString(), mtime.toISOString());
502
+ const { inodeId } = await this.resolveFollow(path);
503
+ debug("utimes resolved path=%s inodeId=%d", path, inodeId);
504
+ const atimeMs = atime.getTime();
505
+ const mtimeMs = mtime.getTime();
506
+ debug("utimes calling setattr inodeId=%d atimeMs=%d mtimeMs=%d", inodeId, atimeMs, mtimeMs);
507
+ await this.client.setattr(
508
+ inodeId,
509
+ { atimeMs, mtimeMs },
510
+ { user: this.user ?? { uid: 0, gid: 0 } }
511
+ );
512
+ debug("utimes setattr succeeded inodeId=%d", inodeId);
513
+ }
514
+ // ========================================================================
515
+ // Utility Methods
516
+ // ========================================================================
517
+ getAllPaths() {
518
+ return [];
519
+ }
520
+ /**
521
+ * Resolve a path to its inode ID (public wrapper for delegation operations)
522
+ */
523
+ async resolveInodeId(path) {
524
+ const { inodeId } = await this.resolve(path);
525
+ return inodeId;
526
+ }
527
+ };
528
+
529
+ // src/commands.ts
530
+ import { defineCommand } from "just-bash";
531
+ function createArchilCommand(client, fs) {
532
+ return defineCommand("archil", async (args, ctx) => {
533
+ const ok = (stdout) => ({ stdout, stderr: "", exitCode: 0 });
534
+ const fail = (stderr) => ({ stdout: "", stderr, exitCode: 1 });
535
+ const subcommand = args[0];
536
+ if (subcommand === "checkout") {
537
+ const rest = args.slice(1);
538
+ const force = rest.includes("--force") || rest.includes("-f");
539
+ const pathArg = rest.find((a) => !a.startsWith("-"));
540
+ if (!pathArg) {
541
+ return fail("Usage: archil checkout [--force|-f] <path>\n");
448
542
  }
449
- async chmod(path, mode) {
450
- const { inodeId } = await this.resolve(path);
451
- await this.client.setattr(inodeId, { mode }, { user: this.user ?? { uid: 0, gid: 0 } });
543
+ const fullPath = fs.resolvePath(ctx.cwd, pathArg);
544
+ try {
545
+ const inodeId = await fs.resolveInodeId(fullPath);
546
+ await client.checkout(inodeId, { force });
547
+ return ok(`Checked out: ${fullPath} (inode ${inodeId})${force ? " (forced)" : ""}
548
+ `);
549
+ } catch (err) {
550
+ return fail(`Failed to checkout ${fullPath}: ${err instanceof Error ? err.message : err}
551
+ `);
452
552
  }
453
- async utimes(path, atime, mtime) {
454
- debug("utimes path=%s atime=%d mtime=%d", path, atime, mtime);
455
- const { inodeId } = await this.resolve(path);
456
- debug("utimes resolved path=%s inodeId=%d", path, inodeId);
457
- const atimeMs = atime === -1 ? -1 : Math.floor(atime * 1e3);
458
- const mtimeMs = mtime === -1 ? -1 : Math.floor(mtime * 1e3);
459
- debug("utimes calling setattr inodeId=%d atimeMs=%d mtimeMs=%d", inodeId, atimeMs, mtimeMs);
460
- await this.client.setattr(
461
- inodeId,
462
- { atimeMs, mtimeMs },
463
- { user: this.user ?? { uid: 0, gid: 0 } }
464
- );
465
- debug("utimes setattr succeeded inodeId=%d", inodeId);
553
+ }
554
+ if (subcommand === "checkin") {
555
+ const pathArg = args[1];
556
+ if (!pathArg) {
557
+ return fail("Usage: archil checkin <path>\n");
466
558
  }
467
- // ========================================================================
468
- // Utility Methods
469
- // ========================================================================
470
- async getAllPaths() {
471
- const paths = [];
472
- const walk = async (dirPath) => {
473
- paths.push(dirPath);
474
- try {
475
- const entries = await this.readdirWithFileTypes(dirPath);
476
- for (const entry of entries) {
477
- const fullPath = this.resolvePath(dirPath, entry.name);
478
- if (entry.isDirectory) {
479
- await walk(fullPath);
480
- } else {
481
- paths.push(fullPath);
482
- }
483
- }
484
- } catch {
485
- }
486
- };
487
- await walk("/");
488
- return paths;
559
+ const fullPath = fs.resolvePath(ctx.cwd, pathArg);
560
+ try {
561
+ const inodeId = await fs.resolveInodeId(fullPath);
562
+ await client.checkin(inodeId);
563
+ return ok(`Checked in: ${fullPath} (inode ${inodeId})
564
+ `);
565
+ } catch (err) {
566
+ return fail(`Failed to checkin ${fullPath}: ${err instanceof Error ? err.message : err}
567
+ `);
489
568
  }
490
- /**
491
- * Resolve a path to its inode ID (public wrapper for delegation operations)
492
- */
493
- async resolveInodeId(path) {
494
- const { inodeId } = await this.resolve(path);
495
- return inodeId;
569
+ }
570
+ if (subcommand === "list-delegations" || subcommand === "delegations") {
571
+ try {
572
+ const delegations = client.listDelegations();
573
+ if (delegations.length === 0) {
574
+ return ok("No delegations held\n");
575
+ }
576
+ const lines = delegations.map((d) => ` inode ${d.inodeId}: ${d.state}`);
577
+ return ok("Delegations:\n" + lines.join("\n") + "\n");
578
+ } catch (err) {
579
+ return fail(`Failed to list delegations: ${err instanceof Error ? err.message : err}
580
+ `);
496
581
  }
497
- };
498
- }
499
- });
582
+ }
583
+ if (subcommand === "help" || !subcommand) {
584
+ return ok(
585
+ "Archil commands:\n archil checkout [--force|-f] <path> - Acquire write delegation\n archil checkin <path> - Release write delegation\n archil list-delegations - Show held delegations\n archil help - Show this help message\n\nThe --force flag revokes any existing delegation from other clients.\n"
586
+ );
587
+ }
588
+ return fail(`Unknown archil command: ${subcommand}
589
+ Run 'archil help' for available commands
590
+ `);
591
+ });
592
+ }
500
593
 
501
594
  // src/index.ts
502
- init_ArchilFs();
503
- function createArchilFs(client, options) {
504
- const { ArchilFs: ArchilFs2 } = (init_ArchilFs(), __toCommonJS(ArchilFs_exports));
505
- return new ArchilFs2(client, options);
595
+ async function createArchilFs(client, options) {
596
+ return ArchilFs.create(client, options);
506
597
  }
507
598
  export {
508
599
  ArchilFs,
600
+ createArchilCommand,
509
601
  createArchilFs
510
602
  };