|
| 1 | +import { createWorkspaceError } from "../errors.js"; |
| 2 | +import { canonicalizePath } from "../path.js"; |
| 3 | +import { incrementRev } from "../rev.js"; |
| 4 | +import type { Database } from "../storage.js"; |
| 5 | +import { recordDelete } from "../sync/changes.js"; |
| 6 | +import { pathOf } from "../sync/paths.js"; |
| 7 | +import { assertNotReadOnly } from "./mount-guard.js"; |
| 8 | +import { resolveInode } from "./resolve.js"; |
| 9 | + |
| 10 | +interface DirChild { |
| 11 | + name: string; |
| 12 | + child_inode: number; |
| 13 | + type: NodeType; |
| 14 | +} |
| 15 | + |
| 16 | +interface SubtreeEntry { |
| 17 | + path: string; |
| 18 | + inode: number; |
| 19 | + type: NodeType; |
| 20 | +} |
| 21 | + |
| 22 | +type NodeType = "file" | "dir" | "symlink"; |
| 23 | + |
| 24 | +export function rename(db: Database, oldPath: string, newPath: string): void { |
| 25 | + const { path: oldCanonical } = canonicalizePath(oldPath); |
| 26 | + const { parts: newParts, path: newCanonical } = canonicalizePath(newPath); |
| 27 | + |
| 28 | + if (oldCanonical === "/") { |
| 29 | + throw createWorkspaceError("EINVAL", "cannot rename root", oldCanonical); |
| 30 | + } |
| 31 | + if (newParts.length === 0) { |
| 32 | + throw createWorkspaceError("EINVAL", "cannot rename onto root", newCanonical); |
| 33 | + } |
| 34 | + |
| 35 | + assertNotReadOnly(db, oldCanonical); |
| 36 | + assertNotReadOnly(db, newCanonical); |
| 37 | + |
| 38 | + db.transactionSync(() => { |
| 39 | + const source = resolveInode(db, oldCanonical, { followSymlinks: false }); |
| 40 | + if (source === null) { |
| 41 | + throw createWorkspaceError("ENOENT", `no such path: ${oldCanonical}`, oldCanonical); |
| 42 | + } |
| 43 | + |
| 44 | + // Resolve the source's real parent dirent. The parent path is |
| 45 | + // resolved with symlinks followed so a request through a symlinked |
| 46 | + // directory lands on the real container; the inode is then |
| 47 | + // identified by (parent_inode, name) rather than by child_inode so |
| 48 | + // a hardlinked source touches only the requested name. |
| 49 | + const { parts: oldParts } = canonicalizePath(oldCanonical); |
| 50 | + const oldName = oldParts[oldParts.length - 1]; |
| 51 | + const oldParentPath = oldParts.length === 1 ? "/" : `/${oldParts.slice(0, -1).join("/")}`; |
| 52 | + const oldParent = resolveInode(db, oldParentPath); |
| 53 | + if (oldParent === null || oldParent.type !== "dir") { |
| 54 | + throw createWorkspaceError("ENOENT", `no such path: ${oldCanonical}`, oldCanonical); |
| 55 | + } |
| 56 | + const oldParentReal = pathOf(db, oldParent.inode); |
| 57 | + if (oldParentReal === null) { |
| 58 | + throw createWorkspaceError("ENOENT", `no such path: ${oldCanonical}`, oldCanonical); |
| 59 | + } |
| 60 | + const oldRealPath = oldParentReal === "/" ? `/${oldName}` : `${oldParentReal}/${oldName}`; |
| 61 | + |
| 62 | + if (oldCanonical === newCanonical) return; |
| 63 | + if (source.type === "dir" && newCanonical.startsWith(`${oldRealPath}/`)) { |
| 64 | + throw createWorkspaceError( |
| 65 | + "EINVAL", |
| 66 | + `cannot rename a directory into itself: ${oldRealPath}`, |
| 67 | + newCanonical, |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + const newName = newParts[newParts.length - 1]; |
| 72 | + const newParentPath = newParts.length === 1 ? "/" : `/${newParts.slice(0, -1).join("/")}`; |
| 73 | + const newParent = resolveInode(db, newParentPath); |
| 74 | + if (newParent === null || newParent.type !== "dir") { |
| 75 | + throw createWorkspaceError( |
| 76 | + "ENOENT", |
| 77 | + `parent directory missing: ${newCanonical}`, |
| 78 | + newCanonical, |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + // A rename whose source and destination resolve to the very same |
| 83 | + // dirent (same real parent and name, e.g. through a symlinked path) |
| 84 | + // is a true no-op: leave the tree and the change stream untouched. |
| 85 | + // This is distinct from renaming one hardlink onto another, where |
| 86 | + // the names differ and the source link must still be removed. |
| 87 | + if (oldParent.inode === newParent.inode && oldName === newName) return; |
| 88 | + |
| 89 | + const existing = db.one<{ child_inode: number; type: "file" | "dir" | "symlink" }>( |
| 90 | + `SELECT d.child_inode AS child_inode, n.type AS type |
| 91 | + FROM vfs_dirents d |
| 92 | + JOIN vfs_nodes n ON n.inode = d.child_inode |
| 93 | + WHERE d.parent_inode = ? AND d.name = ?`, |
| 94 | + newParent.inode, |
| 95 | + newName, |
| 96 | + ); |
| 97 | + |
| 98 | + const oldEntries = |
| 99 | + source.type === "dir" |
| 100 | + ? collectSubtree(db, source.inode, oldRealPath) |
| 101 | + : [{ path: oldRealPath, inode: source.inode, type: source.type }]; |
| 102 | + if (source.type === "dir") { |
| 103 | + assertDestinationParentOutsideSource(oldEntries, newParent.inode, oldRealPath, newCanonical); |
| 104 | + } |
| 105 | + |
| 106 | + if (existing !== undefined) { |
| 107 | + assertCompatibleOverwrite(source.type, existing.type, newCanonical); |
| 108 | + if (existing.type === "dir") { |
| 109 | + const childCount = db.scalar<number>( |
| 110 | + "SELECT COUNT(*) FROM vfs_dirents WHERE parent_inode = ?", |
| 111 | + existing.child_inode, |
| 112 | + ); |
| 113 | + if ((childCount ?? 0) > 0) { |
| 114 | + throw createWorkspaceError("ENOTEMPTY", `not empty: ${newCanonical}`, newCanonical); |
| 115 | + } |
| 116 | + } |
| 117 | + // Displace only the destination name. The displaced inode may |
| 118 | + // carry other hardlinks (or be the source inode itself), so reap |
| 119 | + // its chunks and node row only once the final link disappears. |
| 120 | + // Order matters: displace before unlinking the source so a |
| 121 | + // hardlink-onto-hardlink rename never momentarily drops to zero |
| 122 | + // links and reaps the inode it is about to re-point. |
| 123 | + db.run( |
| 124 | + "DELETE FROM vfs_dirents WHERE parent_inode = ? AND name = ?", |
| 125 | + newParent.inode, |
| 126 | + newName, |
| 127 | + ); |
| 128 | + const remaining = db.scalar<number>( |
| 129 | + "SELECT COUNT(*) FROM vfs_dirents WHERE child_inode = ?", |
| 130 | + existing.child_inode, |
| 131 | + ); |
| 132 | + if ((remaining ?? 0) === 0) { |
| 133 | + db.run("DELETE FROM vfs_chunks WHERE inode = ?", existing.child_inode); |
| 134 | + db.run("DELETE FROM vfs_nodes WHERE inode = ?", existing.child_inode); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + // Unlink only the source name; a hardlinked source keeps its other |
| 139 | + // names alive. |
| 140 | + db.run("DELETE FROM vfs_dirents WHERE parent_inode = ? AND name = ?", oldParent.inode, oldName); |
| 141 | + db.run( |
| 142 | + "INSERT INTO vfs_dirents (parent_inode, name, child_inode) VALUES (?, ?, ?)", |
| 143 | + newParent.inode, |
| 144 | + newName, |
| 145 | + source.inode, |
| 146 | + ); |
| 147 | + |
| 148 | + const rev = incrementRev(db); |
| 149 | + // Rename is represented on the wire as old-path tombstones plus |
| 150 | + // live entries for the moved inode subtree, so stamp only that |
| 151 | + // subtree with the shared rev. Parent directory mtimes are left |
| 152 | + // unchanged on purpose; this diverges from POSIX rename(2), but |
| 153 | + // avoids treating the old and new parents as content changes. |
| 154 | + for (const entry of oldEntries) { |
| 155 | + db.run("UPDATE vfs_nodes SET rev = ? WHERE inode = ?", rev, entry.inode); |
| 156 | + recordDelete(db, rev, entry.path); |
| 157 | + } |
| 158 | + }); |
| 159 | +} |
| 160 | + |
| 161 | +function assertCompatibleOverwrite( |
| 162 | + sourceType: NodeType, |
| 163 | + existingType: NodeType, |
| 164 | + path: string, |
| 165 | +): void { |
| 166 | + if (sourceType === "dir" && existingType === "dir") return; |
| 167 | + if (existingType === "dir") { |
| 168 | + throw createWorkspaceError("EISDIR", `cannot overwrite directory: ${path}`, path); |
| 169 | + } |
| 170 | + if (sourceType === "dir") { |
| 171 | + throw createWorkspaceError("ENOTDIR", `cannot overwrite non-directory: ${path}`, path); |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +function assertDestinationParentOutsideSource( |
| 176 | + oldEntries: SubtreeEntry[], |
| 177 | + newParentInode: number, |
| 178 | + oldCanonical: string, |
| 179 | + newCanonical: string, |
| 180 | +): void { |
| 181 | + if (!oldEntries.some((entry) => entry.inode === newParentInode)) return; |
| 182 | + |
| 183 | + throw createWorkspaceError( |
| 184 | + "EINVAL", |
| 185 | + `cannot rename a directory into itself: ${oldCanonical}`, |
| 186 | + newCanonical, |
| 187 | + ); |
| 188 | +} |
| 189 | + |
| 190 | +function collectSubtree(db: Database, rootInode: number, rootPath: string): SubtreeEntry[] { |
| 191 | + const entries: SubtreeEntry[] = [{ path: rootPath, inode: rootInode, type: "dir" }]; |
| 192 | + for (let idx = 0; idx < entries.length; idx++) { |
| 193 | + const entry = entries[idx]; |
| 194 | + if (entry.type !== "dir") continue; |
| 195 | + const children = db.all<DirChild>( |
| 196 | + `SELECT d.name AS name, d.child_inode AS child_inode, n.type AS type |
| 197 | + FROM vfs_dirents d |
| 198 | + JOIN vfs_nodes n ON n.inode = d.child_inode |
| 199 | + WHERE d.parent_inode = ? |
| 200 | + ORDER BY d.name`, |
| 201 | + entry.inode, |
| 202 | + ); |
| 203 | + for (const child of children) { |
| 204 | + const childPath = entry.path === "/" ? `/${child.name}` : `${entry.path}/${child.name}`; |
| 205 | + entries.push({ |
| 206 | + path: childPath, |
| 207 | + inode: child.child_inode, |
| 208 | + type: child.type, |
| 209 | + }); |
| 210 | + } |
| 211 | + } |
| 212 | + return entries; |
| 213 | +} |
0 commit comments