Skip to content

Commit d1b59ba

Browse files
authored
Merge pull request #5322 from modelcontextprotocol/v2/fix/5188-filesystem-move-exdev
fix(filesystem): move a file across filesystems by copying it on EXDEV
2 parents 1513547 + 7fabe2a commit d1b59ba

7 files changed

Lines changed: 730 additions & 3 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@modelcontextprotocol/server-filesystem": patch
3+
---
4+
5+
`move_file` now moves a regular file between allowed directories on different filesystems (mounts, disks or shares), where it used to fail with `EXDEV`: it copies the file to a newly created destination, keeping its permissions and modification time, then removes the source, and removes a partial copy if anything fails. A directory still cannot be moved across filesystems, and now fails with an error that says so.

‎src/filesystem/README.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,12 @@ The server's directory access control follows this flow:
161161
- `source` (string)
162162
- `destination` (string)
163163
- Fails if destination exists
164+
- Across filesystems (the source and destination on different mounts or
165+
disks), a regular file is moved by copying it to a newly created
166+
destination, with its permissions and modification time, and then removing
167+
the source. If the copy fails, the partial destination is removed and the
168+
source is left in place. A directory cannot be moved across filesystems
169+
and fails with an error; move its files one at a time instead
164170

165171
- **search_files**
166172
- Recursively search for files/directories that match or do not match patterns

‎src/filesystem/__tests__/lib.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,40 @@ describe("Lib Functions", () => {
703703

704704
expect(mockPinned.renameIntoDirectory).not.toHaveBeenCalled();
705705
});
706+
707+
it("copies and removes instead when the rename crosses a device (EXDEV)", async () => {
708+
mockFs.lstat.mockRejectedValueOnce(
709+
Object.assign(new Error("not found"), { code: "ENOENT" }),
710+
);
711+
mockPinned.renameIntoDirectory.mockImplementationOnce(() => {
712+
throw Object.assign(new Error("EXDEV: cross-device link"), {
713+
code: "EXDEV",
714+
});
715+
});
716+
717+
await moveFile("/test/source.txt", "/mnt/other/dest.txt");
718+
719+
expect(mockPinned.moveFileAcrossDevices).toHaveBeenCalledWith(
720+
"/test/source.txt",
721+
"/mnt/other/dest.txt",
722+
);
723+
});
724+
725+
it("reports a rename failure other than EXDEV as is, without copying", async () => {
726+
mockFs.lstat.mockRejectedValueOnce(
727+
Object.assign(new Error("not found"), { code: "ENOENT" }),
728+
);
729+
mockPinned.renameIntoDirectory.mockImplementationOnce(() => {
730+
throw Object.assign(new Error("EACCES: permission denied"), {
731+
code: "EACCES",
732+
});
733+
});
734+
735+
await expect(
736+
moveFile("/test/source.txt", "/test/dest.txt"),
737+
).rejects.toThrow("EACCES: permission denied");
738+
expect(mockPinned.moveFileAcrossDevices).not.toHaveBeenCalled();
739+
});
706740
});
707741
});
708742

0 commit comments

Comments
 (0)