@struktoai/mirage-core
Version:
A Unified Virtual Filesystem For AI Agents
53 lines • 2.65 kB
JavaScript
// ========= Copyright 2026 @ Strukto.AI All Rights Reserved. =========
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ========= Copyright 2026 @ Strukto.AI All Rights Reserved. =========
import { FileType } from "../../types.js";
import { copy, copyTree } from "./copy.js";
import { backendPath } from "./path.js";
import { removeTree } from "./rm.js";
import { stat } from "./stat.js";
import { unlink } from "./unlink.js";
export async function rename(accessor, src, dst) {
// Non-atomic: the Databricks Files API has no native rename, so this is
// implemented as copy + delete and can leave partial state on failure.
const srcStat = await stat(accessor, src);
const remoteSrc = backendPath(accessor.config, src);
const remoteDst = backendPath(accessor.config, dst);
if (remoteSrc === remoteDst) {
// rename(2) onto the same path is a no-op; copy + unlink here would
// upload the file onto itself then delete it, destroying the data.
// Guard runs after stat so a missing source still raises.
return;
}
if (srcStat.type === FileType.DIRECTORY) {
if (remoteDst.startsWith(remoteSrc + '/')) {
// Moving a directory into its own subtree would run away in the
// recursive copy and then removeTree would delete the original.
// Refuse before either side effect.
throw new Error(`cannot move '${src.virtual}' to a subdirectory of itself, '${dst.virtual}'`);
}
// Only what the copy carried is deleted: a file written under the
// source after its listing stays there, and the move is ENOTEMPTY.
const copied = new Map();
await copyTree(accessor, remoteSrc, remoteDst, copied);
await removeTree(accessor, src, copied);
}
else {
await copy(accessor, src, dst);
await unlink(accessor, src);
}
// Only a positive file rules out a subtree at the source. The destination
// keeps the subtree drop: the upload overwrites whatever stands there,
// and nothing checks it is not a non-empty directory.
}
//# sourceMappingURL=rename.js.map