feat(files): copy & duplicate, bulk actions, disk-backed uploads, and an accessible file tree (#1409)

* perf(files): spool uploads to disk instead of buffering in memory

Switch the stack file-explorer upload from multer memoryStorage to
diskStorage and stream the spooled temp file through the file-root
gateway, so an upload is never held fully in RAM. Authorization and
root resolution now run before multer spools, so an unauthorized or
read-only-root request is rejected without writing a temp file, and the
spool is removed on every exit path. The named-volume helper write
verifies the written byte count, since cat cannot report a short write.

* feat(files): copy and duplicate files in the explorer

Add a copy capability to the stack file explorer: a same-folder
Duplicate (auto-suffixed name) and a "Copy to..." destination picker,
on both filesystem and named-volume roots. Copying is within-root,
symlink-leaf-safe, blocks a directory copy into its own subtree, and
refuses to create a protected name (compose/.env) at the stack root
while still allowing a protected file to be duplicated under a new name.

* feat(files): make the file tree keyboard accessible

Bring the stack file explorer tree to the WCAG tree pattern: rows are
treeitems carrying aria-level, aria-selected, and aria-expanded, with a
single roving tabindex and full keyboard navigation (arrow keys,
Home/End, Enter/Space) over a flattened visible-node list that stays in
lockstep with the rendered rows. A polite live region announces the
selected file. No visual change to the tree.

* feat(files): bulk select, delete, move, and download files

Add multi-select to the stack file explorer (checkboxes plus Shift and
Ctrl/Cmd click over the visible order) driving three bulk actions:
delete, move, and download as a streamed .tar.gz. All run within the
active root on both filesystem and named-volume backends, report
per-item results so partial failures surface (with the failed items
kept selected for retry), normalize ancestor/descendant selections
server-side, and cap the archive entry and byte counts before any
bytes are streamed. Protected compose/.env files are excluded from
bulk delete and move but may still be downloaded.

* docs(files): document copy, bulk actions, and keyboard navigation

Add the copy/duplicate and multi-select bulk delete/move/download
sections to the Files & Volumes page, a keyboard-navigation note for the
tree, an updated context-menu reference, and bulk troubleshooting entries.

* fix(files): inline path-injection barriers at the new file-op sinks

CodeQL js/path-injection does not credit the wrapped isPathWithinBase
containment check, so the new copy/bulk/disk-upload flows tripped the
gate. Inline the canonical path.resolve + startsWith barrier at the
realpath sink in resolveSafePathWithin (covers every user-relPath flow)
and confirm the multer spool path resolves within UPLOAD_TMP_DIR before
unlinking it or streaming it onward. Behavior is unchanged; the paths
were already validated.

* fix(files): guard the ancestor-walk realpath sink too

The first barrier covered realpath(target), but the ENOENT ancestor
walk re-derives the path via path.dirname, which static analysis treats
as a fresh tainted value. Add the same inline containment barrier before
that realpath and resolve the root case via the untainted base, so the
only tainted realpath input is one the startsWith check has cleared.
Behavior is unchanged.

* fix(files): resolve the root case off the taint path in the ancestor walk

The compound guard on existing (the same variable as the startsWith
subject) was not credited as a sanitizer. Handle the root case before
the barrier by resolving the untainted base directly, leaving a plain
canonical startsWith guard on the strictly-within ancestor. Behavior is
unchanged.

* fix(files): harden helper-backend bulk download and uploads

Address three issues found in the named-volume (helper) backend:

- Bulk download could send 200 headers before discovering a file the
  helper download path refuses, tearing the archive mid-stream. The
  prewalk now rejects symlinks, non-regular ("other") entries, and
  files over the per-file download cap before any header (400/413).
  FileEntry gains an 'other' type so non-regular entries stay distinct
  from regular files as they pass through the gateway.
- The helper directory listing was fully buffered before the archive
  entry cap could fire. listDir now accepts a limit; the list script
  stops after limit+1 rows and the gateway reports truncation.
- A stdin pipeline error during a helper upload masked the container's
  real nonzero exit code (and its 4xx mapping) as a generic 500. The
  nonzero exit now wins; the masked stream error is logged.

* feat(files): add a New file toolbar button with server-enforced create-only

The stack file explorer could create a folder from a toolbar button but a
new file only from a folder's right-click menu, so a file could not be
created at the stack root at all. Add a New file toolbar button beside New
folder, targeting the current directory.

Creating a file now routes through a new createEmptyStackFile helper that
posts a zero-byte file through the existing upload endpoint with overwrite
off, so the server's exclusive-create path rejects an existing name instead
of clobbering it. A file collision surfaces inline in the dialog; a folder
collision and other failures surface as a toast.

* fix(files): widen the tree row hit area and add horizontal scroll for long names

Right-clicking a file tree row only opened the Sencho context menu when the
click landed on the filename; the rest of the row fell through to the native
browser menu, and long names were truncated with no way to read them.

Make each row span the full pane width (and grow with its content) so the
whole row is the context-menu trigger, and let the tree scroll horizontally
so a long name is reachable instead of clipped. A new opt-in horizontal prop
on ScrollArea adds the styled horizontal scrollbar without clamping content
width.

* docs(files): document the New file button, full-row right-click, and long-name scrolling

* test(files): cover createEmptyStackFile targeting the stack root

Add an API-layer case for the empty-directory (stack root) create path, the
primary reason the New file toolbar button exists, so a regression in the
root-level URL would be caught at unit speed rather than only in e2e.
This commit is contained in:
Anso
2026-06-22 19:33:06 -04:00
committed by GitHub
parent 9480cc98bb
commit 37e6e48b40
26 changed files with 3100 additions and 138 deletions
@@ -20,6 +20,7 @@ export type FileExplorerOp =
| 'delete'
| 'mkdir'
| 'rename'
| 'copy'
| 'chmod';
interface FileExplorerOpStats {
+56 -9
View File
@@ -13,9 +13,10 @@
* within the same (seconds-resolution) second.
*/
import type { Readable } from 'stream';
import { createReadStream, promises as fsp } from 'fs';
import { FileSystemService, type FileEntry, type FileRootScope } from './FileSystemService';
import { VolumeBrowserService, makeHelperVersion, type VolumeEntry } from './VolumeBrowserService';
import { VolumeBrowserService, makeHelperVersion, DOWNLOAD_MAX_BYTES, type VolumeEntry } from './VolumeBrowserService';
import type { StackFileRoot } from './StackFileRootsService';
const HELPER_VIEW_MAX_BYTES = 2 * 1024 * 1024; // match the stack-source viewer cap
@@ -51,7 +52,9 @@ export function parseFsVersion(raw: string | undefined): number | null {
function volumeEntryToFileEntry(e: VolumeEntry): FileEntry {
return {
name: e.name,
type: e.type === 'other' ? 'file' : e.type,
// Preserve 'other' (non-regular entries) so the archive guard can reject
// what the helper download path would refuse; the UI renders it like a file.
type: e.type,
size: e.size,
mtime: e.mtime * 1000,
isProtected: false,
@@ -96,8 +99,12 @@ export class FileRootGateway {
limit: number,
): Promise<{ entries: FileEntry[]; total: number; truncated: boolean }> {
if (root.backend === 'helper') {
const entries = (await this.helper().listDir(root.hostPathOrName, relPath)).map(volumeEntryToFileEntry);
return { entries, total: entries.length, truncated: false };
// Ask for one over the limit so a fully-listed directory is distinguishable
// from a truncated one without the helper buffering every entry.
const raw = await this.helper().listDir(root.hostPathOrName, relPath, limit);
const truncated = raw.length > limit;
const entries = raw.slice(0, limit).map(volumeEntryToFileEntry);
return { entries, total: entries.length, truncated };
}
return this.fs().listStackDirectoryPage(stackName, relPath, { limit, scope: this.scopeFor(root) });
}
@@ -173,22 +180,56 @@ export class FileRootGateway {
return this.fs().pathKind(stackName, relPath, this.scopeFor(root));
}
/** Upload write. `exclusive` rejects an existing target (no overwrite). */
async writeBuffer(
/** Stat a single entry (type + size). Used by bulk download to size the archive. */
async stat(root: StackFileRoot, stackName: string, relPath: string): Promise<FileEntry> {
if (root.backend === 'helper') {
return volumeEntryToFileEntry(await this.helper().stat(root.hostPathOrName, relPath));
}
return this.fs().statStackEntry(stackName, relPath, this.scopeFor(root));
}
/**
* Reject a non-directory entry the backend's download path could not stream,
* BEFORE the archive prewalk commits to sending response headers. The fs
* backend streams any in-root file (and follows in-root symlinks), so it has
* no constraint; the helper backend's download refuses symlinks/non-regular
* files and caps each file at DOWNLOAD_MAX_BYTES, which must be enforced here
* or a bulk download would tear mid-archive when gateway.download() later
* throws. Throws ARCHIVE_UNSUPPORTED (-> 400) or ARCHIVE_TOO_LARGE (-> 413).
*/
assertArchivable(root: StackFileRoot, relPath: string, entry: FileEntry): void {
if (root.backend !== 'helper') return;
if (entry.type !== 'file') {
throw Object.assign(new Error(`"${relPath}" cannot be downloaded from this volume`), { code: 'ARCHIVE_UNSUPPORTED' });
}
if (entry.size > DOWNLOAD_MAX_BYTES) {
throw Object.assign(new Error(`"${relPath}" is too large to download from this volume`), { code: 'ARCHIVE_TOO_LARGE' });
}
}
/**
* Upload write sourced from a temp file spooled to disk (multer diskStorage),
* so the upload is never buffered in memory. `exclusive` rejects an existing
* target (no overwrite). The caller owns deleting `tempPath`.
*/
async writeFromTemp(
root: StackFileRoot,
stackName: string,
relPath: string,
buffer: Buffer,
tempPath: string,
exclusive: boolean,
): Promise<void> {
if (root.backend === 'helper') {
if (exclusive && (await this.helper().pathKind(root.hostPathOrName, relPath)) !== null) {
throw Object.assign(new Error('File already exists'), { code: 'FILE_EXISTS' });
}
await this.helper().writeFile(root.hostPathOrName, relPath, buffer);
// The helper writes via `cat`, which cannot report a short write; pass the
// spooled byte count so writeFileStream can verify the volume got it all.
const { size } = await fsp.stat(tempPath);
await this.helper().writeFileStream(root.hostPathOrName, relPath, createReadStream(tempPath), size);
return;
}
await this.fs().writeStackFileBuffer(stackName, relPath, buffer, { exclusive, scope: this.scopeFor(root) });
await this.fs().writeScopedFileFromTemp(stackName, relPath, tempPath, { exclusive, scope: this.scopeFor(root) });
}
async download(
@@ -220,6 +261,12 @@ export class FileRootGateway {
return this.fs().renameStackPath(stackName, fromRel, toRel, this.scopeFor(root));
}
/** Copy a file or directory within a single root (cross-root copy is rejected at the route). */
async copy(root: StackFileRoot, stackName: string, fromRel: string, toRel: string): Promise<void> {
if (root.backend === 'helper') return this.helper().copy(root.hostPathOrName, fromRel, toRel);
return this.fs().copyScopedPath(stackName, fromRel, toRel, this.scopeFor(root));
}
async getMode(root: StackFileRoot, stackName: string, relPath: string): Promise<{ mode: number; octal: string }> {
if (root.backend === 'helper') throw unsupportedOnHelperRoot();
return this.fs().getStackEntryMode(stackName, relPath, this.scopeFor(root));
+119 -27
View File
@@ -1,9 +1,10 @@
import path from 'path';
import os from 'os';
import crypto from 'crypto';
import { promises as fsPromises, createReadStream } from 'fs';
import { promises as fsPromises, createReadStream, createWriteStream } from 'fs';
import type { Dirent } from 'fs';
import type { Readable } from 'stream';
import { Readable } from 'stream';
import { pipeline } from 'stream/promises';
import { NodeRegistry } from './NodeRegistry';
import { isPathWithinBase, isValidStackName } from '../utils/validation';
import { isBinaryBuffer } from '../utils/binaryDetect';
@@ -11,7 +12,10 @@ import { sanitizeForLog } from '../utils/safeLog';
export interface FileEntry {
name: string;
type: 'file' | 'directory' | 'symlink';
// 'other' covers non-regular helper-volume entries (fifo/socket/device): they
// are unrepresentable on the fs backend but the helper can surface them, and
// they must stay distinct from 'file' so the archive guard can reject them.
type: 'file' | 'directory' | 'symlink' | 'other';
size: number;
mtime: number;
isProtected: boolean;
@@ -100,6 +104,18 @@ function fsCaseKey(s: string): string {
return process.platform === 'win32' || process.platform === 'darwin' ? s.toLowerCase() : s;
}
/**
* True when resolved absolute path `candidate` is `parent` itself or sits inside
* it, compared case-folded so the guard stays authoritative on a case-insensitive
* filesystem. Used to block moving/copying a directory into its own subtree.
*/
function isSameOrDescendantFsPath(parent: string, candidate: string): boolean {
const parentKey = fsCaseKey(parent);
const parentKeyWithSep = parentKey.endsWith(path.sep) ? parentKey : parentKey + path.sep;
const candidateKey = fsCaseKey(candidate);
return candidateKey === parentKey || candidateKey.startsWith(parentKeyWithSep);
}
function isProtectedRelPath(relPath: string): boolean {
if (!relPath) return false;
const normalized = stripTrailingSlash(relPath);
@@ -1043,9 +1059,14 @@ export class FileSystemService {
* compose. Tracked as a follow-up hardening, not a per-root regression.
*/
private async resolveSafePathWithin(rootAbsDir: string, relPath: string): Promise<string> {
const target = relPath === '' ? rootAbsDir : path.resolve(rootAbsDir, relPath);
if (!isPathWithinBase(target, rootAbsDir)) {
// Canonical js/path-injection barrier inline with the realpath sinks below:
// isPathWithinBase performs the same containment check, but static analysis
// only credits the path.resolve + startsWith form when it sits at the sink.
// relPath === '' resolves to the (server-controlled) root itself and carries
// no user input, so it needs no containment check.
const baseResolved = path.resolve(rootAbsDir);
const target = path.resolve(baseResolved, relPath);
if (relPath !== '' && !target.startsWith(baseResolved + path.sep)) {
throw Object.assign(new Error('Path escapes root directory'), { code: 'INVALID_PATH' });
}
@@ -1067,6 +1088,21 @@ export class FileSystemService {
}
suffix.unshift(path.basename(existing));
existing = parent;
if (existing === baseResolved) {
// Reached the root: realpath the untainted base (never a tainted input)
// and reattach the not-yet-existing suffix.
const realBase = await fsPromises.realpath(baseResolved);
if (!isPathWithinBase(realBase, rootAbsDir)) {
throw Object.assign(new Error('Symlink escapes root directory'), { code: 'SYMLINK_ESCAPE' });
}
realTarget = path.join(realBase, ...suffix);
break;
}
// Inline js/path-injection barrier: existing is now strictly below the
// root, so the canonical path.resolve + startsWith form credits the sink.
if (!existing.startsWith(baseResolved + path.sep)) {
throw Object.assign(new Error('Path escapes root directory'), { code: 'INVALID_PATH' });
}
try {
const realExisting = await fsPromises.realpath(existing);
if (!isPathWithinBase(realExisting, rootAbsDir)) {
@@ -1255,7 +1291,7 @@ export class FileSystemService {
*/
private async writeStackFileAtomic(
safePath: string,
data: string | Buffer,
data: string | Buffer | Readable,
opts: { exclusive?: boolean } = {},
): Promise<void> {
await fsPromises.mkdir(path.dirname(safePath), { recursive: true });
@@ -1265,13 +1301,28 @@ export class FileSystemService {
const tmpPath = `${safePath}.sencho-tmp-${suffix}`;
let stagedTmp = false;
try {
const fh = await fsPromises.open(tmpPath, 'wx');
stagedTmp = true;
try {
await fh.writeFile(data);
await fh.sync();
} finally {
await fh.close();
if (data instanceof Readable) {
// Stream a temp-file source (an upload spooled to disk) into the staging
// file without buffering it in memory. 'wx' exclusively creates the
// staging file; the random suffix already guarantees a fresh name.
const ws = createWriteStream(tmpPath, { flags: 'wx' });
stagedTmp = true;
await pipeline(data, ws);
const synced = await fsPromises.open(tmpPath, 'r+');
try {
await synced.sync();
} finally {
await synced.close();
}
} else {
const fh = await fsPromises.open(tmpPath, 'wx');
stagedTmp = true;
try {
await fh.writeFile(data);
await fh.sync();
} finally {
await fh.close();
}
}
if (opts.exclusive) {
// link() is atomic against EEXIST. Tmp and target are guaranteed to live
@@ -1306,14 +1357,22 @@ export class FileSystemService {
await this.writeStackFileAtomic(safePath, content, opts);
}
async writeStackFileBuffer(
/**
* Atomic, scoped write whose source is a temp file on disk (an upload spooled
* by multer's diskStorage). Streams the temp file into a staging sibling in the
* target's own directory, fsyncs, then links/renames into place, so a large
* upload is never buffered in memory and the temp file's filesystem can differ
* from the stack/volume filesystem (no cross-device rename). The caller owns
* deleting tempPath.
*/
async writeScopedFileFromTemp(
stackName: string,
relPath: string,
buffer: Buffer,
tempPath: string,
opts?: { exclusive?: boolean; scope?: FileRootScope },
): Promise<void> {
const safePath = await this.resolveScopedPath(stackName, relPath, opts?.scope);
await this.writeStackFileAtomic(safePath, buffer, opts);
await this.writeStackFileAtomic(safePath, createReadStream(tempPath), { exclusive: opts?.exclusive });
}
/**
@@ -1481,17 +1540,10 @@ export class FileSystemService {
throw Object.assign(new Error('Invalid destination name'), { code: 'INVALID_PATH' });
}
// Block moving a directory into itself or one of its own descendants; fs.rename
// would otherwise fail with an opaque EINVAL/EPERM. Compare case-folded so the
// guard stays authoritative when the source is supplied with non-disk casing on
// a case-insensitive filesystem.
// would otherwise fail with an opaque EINVAL/EPERM.
const fromStat = await fsPromises.lstat(fromPath);
if (fromStat.isDirectory()) {
const fromKey = fsCaseKey(fromPath);
const fromKeyWithSep = fromKey.endsWith(path.sep) ? fromKey : fromKey + path.sep;
const toKey = fsCaseKey(toPath);
if (toKey === fromKey || toKey.startsWith(fromKeyWithSep)) {
throw Object.assign(new Error('Cannot move a folder into itself'), { code: 'INVALID_PATH' });
}
if (fromStat.isDirectory() && isSameOrDescendantFsPath(fromPath, toPath)) {
throw Object.assign(new Error('Cannot move a folder into itself'), { code: 'INVALID_PATH' });
}
// Prevent overwriting an existing path. lstat (not access) so a dangling
// symlink already at the destination still counts as occupied.
@@ -1505,6 +1557,46 @@ export class FileSystemService {
await fsPromises.rename(fromPath, toPath);
}
/**
* Copies a file or directory within a single root. The source resolves through
* the leaf helper and the copy does not dereference symlinks, so a symlink
* entry is copied as a link (matching the delete/rename leaf policy) rather
* than followed to its target. Only the destination is protection-checked:
* duplicating a protected file (e.g. compose.yaml) elsewhere is allowed, but a
* copy cannot create a reserved name at a protected root. An existing
* destination is rejected (surfaced as EEXIST, which the route maps to 409).
*/
async copyScopedPath(stackName: string, fromRel: string, toRel: string, scope?: FileRootScope): Promise<void> {
if ((scope?.protectedEnabled ?? true) && isProtectedRelPath(toRel)) throw protectedFileError(toRel);
const fromPath = await this.resolveScopedLeafPath(stackName, fromRel, scope);
const toPath = await this.resolveScopedLeafPath(stackName, toRel, scope);
const toName = path.basename(toPath);
if (!toName || toName === '.' || toName === '..') {
throw Object.assign(new Error('Invalid destination name'), { code: 'INVALID_PATH' });
}
// Block copying a directory into itself or one of its own descendants;
// fs.cp would otherwise recurse into the copy it is creating.
const fromStat = await fsPromises.lstat(fromPath);
if (fromStat.isDirectory() && isSameOrDescendantFsPath(fromPath, toPath)) {
throw Object.assign(new Error('Cannot copy a folder into itself'), { code: 'INVALID_PATH' });
}
try {
await fsPromises.cp(fromPath, toPath, {
recursive: fromStat.isDirectory(),
dereference: false,
errorOnExist: true,
force: false,
});
} catch (err: unknown) {
// fs.cp raises ERR_FS_CP_EEXIST when the destination already exists; remap
// to EEXIST so the route returns 409, matching rename's conflict handling.
if ((err as NodeJS.ErrnoException).code === 'ERR_FS_CP_EEXIST') {
throw Object.assign(new Error('A file or folder with that name already exists'), { code: 'EEXIST' });
}
throw err;
}
}
async getStackEntryMode(stackName: string, relPath: string, scope?: FileRootScope): Promise<{ mode: number; octal: string }> {
const safePath = await this.resolveScopedPath(stackName, relPath, scope);
const stat = await fsPromises.stat(safePath);
+121 -10
View File
@@ -1,4 +1,5 @@
import { Writable } from 'stream';
import { Writable, Readable } from 'stream';
import { pipeline } from 'stream/promises';
import path from 'path';
import { createHash } from 'crypto';
import DockerController from './DockerController';
@@ -10,7 +11,7 @@ const DEFAULT_MAX_BYTES = 5 * 1024 * 1024;
// buffered up to this size and a larger file is rejected rather than silently
// truncated. The bound matches the upload limit so the in-memory footprint is no
// worse than the existing multipart upload path.
const DOWNLOAD_MAX_BYTES = 25 * 1024 * 1024;
export const DOWNLOAD_MAX_BYTES = 25 * 1024 * 1024;
const EXEC_TIMEOUT_MS = 30_000;
// Containment guard run inside the helper after every `cd`. Even though
@@ -27,6 +28,8 @@ const ROOT_GUARD =
const LIST_SCRIPT = `set -e
cd -- "$1" || exit 1
${ROOT_GUARD}
lim="$2"
n=0
for entry in * .[!.]* ..?*; do
[ -e "$entry" ] || [ -L "$entry" ] || continue
if [ -L "$entry" ]; then t=l; link=$(readlink -- "$entry" 2>/dev/null || echo "")
@@ -37,6 +40,8 @@ for entry in * .[!.]* ..?*; do
size=$(stat -c '%s' -- "$entry" 2>/dev/null || echo 0)
mtime=$(stat -c '%Y' -- "$entry" 2>/dev/null || echo 0)
printf '%s\\t%s\\t%s\\t%s\\t%s\\n' "$t" "$size" "$mtime" "$entry" "$link"
n=$((n+1))
if [ -n "$lim" ] && [ "$n" -ge "$lim" ]; then break; fi
done`;
// Contain the parent before statting the leaf: cd into the leaf's directory and
@@ -104,6 +109,26 @@ fd=$(dirname -- "$from"); td=$(dirname -- "$to")
{ [ -e "$to" ] || [ -L "$to" ]; } && { echo "destination exists" >&2; exit 11; }
mv -- "$from" "$to"`;
// $1 = from, $2 = to. Both parents are contained and the destination must not
// exist. cp -RP recurses without dereferencing symlinks (copying links as links,
// matching the fs backend) and does NOT preserve ownership, since the helper runs
// unprivileged and cannot chown; new entries are owned by the helper user, like
// the helper write. A directory may not be copied into itself or a descendant
// (cp would otherwise recurse into the copy it is creating).
const COPY_SCRIPT = `set -e
from="$1"; to="$2"
fd=$(dirname -- "$from"); td=$(dirname -- "$to")
sp=$( cd -- "$fd" 2>/dev/null && pwd -P ) || { echo "source escapes volume root" >&2; exit 7; }
case "$sp" in "${VOLUME_MOUNT}"|"${VOLUME_MOUNT}/"*) ;; *) echo "source escapes volume root" >&2; exit 7 ;; esac
dp=$( cd -- "$td" 2>/dev/null && pwd -P ) || { echo "destination escapes volume root" >&2; exit 7; }
case "$dp" in "${VOLUME_MOUNT}"|"${VOLUME_MOUNT}/"*) ;; *) echo "destination escapes volume root" >&2; exit 7 ;; esac
{ [ -e "$to" ] || [ -L "$to" ]; } && { echo "destination exists" >&2; exit 11; }
src="$sp/$(basename -- "$from")"
if [ -d "$src" ] && [ ! -L "$src" ]; then
case "$dp/" in "$src/"*) echo "cannot copy a folder into itself" >&2; exit 12 ;; esac
fi
cp -RP -- "$from" "$to"`;
// $1 = relative path. Prints directory|file|none. Used for upload-overwrite checks.
const PATHKIND_SCRIPT = `set -e
p="$1"
@@ -186,7 +211,7 @@ export class VolumeBrowserService {
return new VolumeBrowserService(nodeId ?? 1);
}
async listDir(volumeName: string, relPath: string): Promise<VolumeEntry[]> {
async listDir(volumeName: string, relPath: string, limit?: number): Promise<VolumeEntry[]> {
const safe = sanitizeRelPath(relPath);
await this.assertVolumeExists(volumeName);
await this.ensureHelperImage();
@@ -194,10 +219,12 @@ export class VolumeBrowserService {
// Portable across BusyBox (Alpine) and GNU coreutils. Lists each
// direct child with a tab-separated row: type<TAB>size<TAB>mtime<TAB>
// name<TAB>symlinkTarget. We chdir to /v/<safe> first so user input is
// never an argv element passed to find/stat.
const script = LIST_SCRIPT;
// never an argv element passed to find/stat. When a limit is given the
// script breaks after limit+1 rows, so a huge directory is never fully
// buffered; the caller detects truncation from the overflow row.
const limArg = limit !== undefined ? String(limit + 1) : '';
const { stdout, stderr, exitCode } = await this.runHelper(volumeName, [
'sh', '-c', script, 'sh', `./${safe || ''}`,
'sh', '-c', LIST_SCRIPT, 'sh', `./${safe || ''}`, limArg,
]);
if (exitCode !== 0) {
@@ -392,6 +419,39 @@ export class VolumeBrowserService {
return { mtimeMs: meta.mtime * 1000, size: meta.size };
}
/**
* Like writeFile but streams the content from a Readable (an upload spooled to
* a temp file) straight into the helper's stdin, so a large upload is never
* held in memory. Non-atomic in-place write (`cat > file`), matching writeFile
* (see writeFile for the ownership-preservation rationale); the caller owns the
* source stream's underlying temp file. `expectedSize` is the source byte count:
* `cat` cannot report a short write, so the post-write size is checked against
* it to catch a truncated transfer that exited cleanly.
*/
async writeFileStream(volumeName: string, relPath: string, source: Readable, expectedSize: number): Promise<{ mtimeMs: number; size: number }> {
const safe = sanitizeRelPath(relPath);
if (!safe) throw new ExecError('Cannot write the volume root', 400);
await this.assertVolumeExists(volumeName);
await this.ensureHelperImage();
const { stderr, exitCode } = await this.runHelper(
volumeName,
['sh', '-c', WRITE_SCRIPT, 'sh', `./${safe}`],
{ writable: true, stdin: source },
);
if (exitCode !== 0) {
const msg = stderr.toString('utf-8').trim();
if (/Permission denied/i.test(msg) || exitCode === 8) throw new ExecError('Permission denied', 403);
if (exitCode === 9) throw new ExecError('Target is a directory', 400);
throw new ExecError(`Write failed: ${msg.substring(0, 200) || 'unknown error'}`);
}
const meta = await this.stat(volumeName, safe);
if (meta.size !== expectedSize) {
throw new ExecError('Upload did not fully write to the volume', 500);
}
return { mtimeMs: meta.mtime * 1000, size: meta.size };
}
/** Create a single directory; its parent must already exist. */
async mkdir(volumeName: string, relPath: string): Promise<void> {
const safe = sanitizeRelPath(relPath);
@@ -450,6 +510,27 @@ export class VolumeBrowserService {
}
}
/** Copy a file or directory within the same volume (symlink-as-link, no chown). */
async copy(volumeName: string, fromRel: string, toRel: string): Promise<void> {
const from = sanitizeRelPath(fromRel);
const to = sanitizeRelPath(toRel);
if (!from || !to) throw new ExecError('Invalid copy path', 400);
await this.assertVolumeExists(volumeName);
await this.ensureHelperImage();
const { stderr, exitCode } = await this.runHelper(
volumeName,
['sh', '-c', COPY_SCRIPT, 'sh', `./${from}`, `./${to}`],
{ writable: true },
);
if (exitCode !== 0) {
const msg = stderr.toString('utf-8').trim();
if (/Permission denied/i.test(msg)) throw new ExecError('Permission denied', 403);
if (exitCode === 11 || /exists/i.test(msg)) throw new ExecError('A file or folder with that name already exists', 409);
if (exitCode === 12) throw new ExecError('Cannot copy a folder into itself', 400);
throw new ExecError(`Copy failed: ${msg.substring(0, 200) || 'unknown error'}`);
}
}
// --- internals -----------------------------------------------------------
private async assertVolumeExists(volumeName: string): Promise<void> {
@@ -493,7 +574,7 @@ export class VolumeBrowserService {
private async runHelper(
volumeName: string,
cmd: string[],
opts: { writable?: boolean; stdin?: Buffer } = {},
opts: { writable?: boolean; stdin?: Buffer | Readable } = {},
): Promise<{ stdout: Buffer; stderr: Buffer; exitCode: number }> {
const docker = DockerController.getInstance(this.nodeId).getDocker();
const stdoutChunks: Buffer[] = [];
@@ -540,6 +621,7 @@ export class VolumeBrowserService {
timer = setTimeout(() => reject(new ExecError('Helper exec timed out', 504)), EXEC_TIMEOUT_MS);
});
let stdinError: unknown = null;
const runPromise = (async () => {
const stream = await container.attach({ stream: true, stdin: wantStdin, stdout: true, stderr: true, hijack: wantStdin });
const streamEnded = new Promise<void>((resolve) => {
@@ -551,16 +633,45 @@ export class VolumeBrowserService {
if (wantStdin && opts.stdin) {
// Feed the file content to the container's stdin, then close it so the
// helper's `cat > file` sees EOF and exits.
stream.write(opts.stdin);
stream.end();
if (Buffer.isBuffer(opts.stdin)) {
stream.write(opts.stdin);
stream.end();
} else {
// A Readable (an upload spooled to a temp file) is piped so a large
// upload is never held in memory. pipeline tears the stdin side down
// (destroys it) on a source error rather than ending it cleanly, so
// cat sees an abnormal close instead of a normal EOF and does not
// commit a truncated file as a success. Remember the error so the run
// is reported as failed after the container exits.
try {
await pipeline(opts.stdin, stream);
} catch (err) {
stdinError = err;
}
}
}
const exitInfo = await container.wait();
// Wait for the attach stream to finish flushing demuxed output.
await streamEnded;
const exitCode = typeof exitInfo?.StatusCode === 'number' ? exitInfo.StatusCode : 0;
// A nonzero helper exit is the authoritative failure: its exit code carries
// the intended 4xx mapping (target-is-a-directory, permission denied, etc.),
// which a stdin EPIPE from the helper closing stdin early would otherwise
// mask as a generic 500. Only surface the stream error when the helper
// itself exited cleanly (or its status was unreadable).
if (stdinError) {
if (exitCode === 0) {
throw new ExecError('Upload stream failed before the volume write completed', 500);
}
// The nonzero exit's mapping is the user-facing error, but the stream
// failure is the root cause; log it so an intermittent upload failure is
// debuggable rather than being silently attributed to the exit code.
console.error('[VolumeBrowser] helper stdin stream error masked by nonzero exit', exitCode, stdinError);
}
return {
stdout: Buffer.concat(stdoutChunks),
stderr: Buffer.concat(stderrChunks),
exitCode: typeof exitInfo?.StatusCode === 'number' ? exitInfo.StatusCode : 0,
exitCode,
};
})();