Skip to content

Add a Files service for folders you choose in Settings, with writes off by default - #385

Merged
mattt merged 8 commits into
mattt:mainfrom
oliverames:pr/files-service
Nov 29, 2026
Merged

mattt merged 8 commits into
mattt:mainfrom
oliverames:pr/files-service

Conversation

@oliverames

@oliverames oliverames commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Rebased on main now that #377 is merged.

This adds a Files service for folders you choose in Settings. Clients can list, read, stat and search inside them. With "Allow writes" on, which is off by default for Files, they can also write, append, make folders, move, and move things to the Trash. files_trash never deletes anything for good. files_write replacing an existing file does replace its content, which is why it needs the caller's last-seen modified time or hash, or overwrite.

Why

Clients often need a few documents, not the whole disk. A service limited to folders the person adds, with changes switched off until they ask for them, lets a client work with files without broader access.

What changed

  • A Files service with files_roots, files_list, files_stat, files_read and files_search, and the write tools files_write, files_append, files_mkdir, files_move and files_trash.
  • Settings lists the shared folders with Add Folder and Remove. Folders are kept as app-scoped security-scoped bookmarks and opened only while a tool runs.
  • Paths are a shared folder's name plus a path inside it. .., absolute paths and links that point out of a folder are refused.
  • Folders are opened one step at a time without following links, so a link swapped in after the check can't redirect a read or write. Pipes and devices are refused.
  • files_write refuses to replace an existing file unless the caller passes the modified time or SHA-256 it last saw, or sets overwrite.
  • Photos libraries are refused anywhere in a path, including names that only match after Unicode folding, and files_move and files_trash also check the item's name on disk before acting.
  • Replacing a file keeps its extended attributes (Finder tags among them), ACL, mode and the name's spelling on disk, and gives it a new modification date.
  • The write tools use the Allow writes setting from Add Allow writes per service and a shared send confirmation #377 (writesEnabledByDefault is false for Files). They are not listed while it is off, and a direct call is refused.
  • Adds the user-selected read-write and app-scope bookmark entitlements, and documents the service in the README.

Review fixes

  • files_stat no longer unwraps canonicalComponents.last! for a link that resolves to the shared root: an empty canonical path is the root itself. A test covers it.
  • files_trash does not report success when F_GETPATH fails; it reports that the result could not be confirmed. If putting a swapped item back fails, the error is reported instead of dropped.
  • write closes its output descriptor when writeAll throws, on both the replace and the create path.
  • files_append refuses a file with more than one hard link, and the files_trash recovery path is exercised by a test.
  • A Photos library named with a long s (ſ) slipped past the name check, because lowercased() doesn't fold it and APFS opens it as the real library. Names are now compared after folding, and move and trash check the on-disk path as well.

Testing

  • swift format lint --strict -r . is clean.
  • imcp-serverTests: 359 tests, 1 skipped (pre-existing), 0 failures. FilesTests includes stat through a link to the root, the post-trash recovery, the long-s Photos library spelling, and a replace that keeps a file's tags, mode and name and updates its modification date. One Home helper timing test failed once while other builds were running and passed when run again.
  • CI: see the checks below.
  • Signed sandboxed pass on a Debug build with these fixes: add a folder in Settings → Files → Add Folder; quit and relaunch the app (both shared folders stayed available, so the bookmarks persist); files_list, files_read (content matched byte for byte), files_write (created a file), and files_trash (the file went to the Trash and was still there, not deleted). The write tools ran with Allow writes on; the originals were untouched.

Not verified

  • The open panel and bookmarks were exercised on a Debug build, not a Release build.
  • Search and move were not part of the pass, and the pass predates the Photos and metadata changes.
  • Two writes to the same file at the same moment can both pass the precondition check; the later one wins.

@mattt

mattt commented Nov 20, 2026

Copy link
Copy Markdown
Owner

Following up from #364: files_append should refuse a file with more than one hard link, and the test for the files_trash recovery passes with the recovery code removed. I'd also like to see add, relaunch, read, write, and trash run in a signed, sandboxed build.

@oliverames

oliverames commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor Author

@mattt Rebased onto the updated #377 and fixed both items: files_append now refuses any file with more than one hard link (st_nlink > 1), and testTrashPutsBackAnItemThatWasSwappedInAfterTheCheck now swaps the item after the pin check right before trashItem, so the test actually exercises post-trash recovery and fails if that code is removed.

@mattt

mattt commented Nov 20, 2026

Copy link
Copy Markdown
Owner

Thanks, @oliverames. Both fixes are in. Three more things before I merge:

  • files_stat on a link to the shared root can crash. isRoot looks at the path as written, but the canonical path below the root is empty, so canonicalComponents.last! has nothing to unwrap.
  • files_trash reports success when F_GETPATH fails, and when putting the swapped item back fails, the error is dropped and the result says nothing was changed.
  • When writeAll throws, the temporary file is unlinked but output is never closed.

I'd still like to see add, relaunch, read, write, and trash run in a signed, sandboxed build. This also needs a rebase onto main for project.pbxproj.

oliverames pushed a commit to oliverames/iMCP that referenced this pull request Nov 20, 2026
Mattt's mattt#385 review:

- files_stat no longer unwraps canonicalComponents.last! for a link that
  resolves to the shared root: an empty canonical path is the root.
- files_trash does not report success when F_GETPATH fails; it reports
  that the result could not be confirmed. If putting a swapped item back
  fails, the error is reported instead of dropped.
- write closes its output descriptor when writeAll throws, on both the
  replace and the create path.

A test covers stat on a link to the shared root.
oliverames pushed a commit to oliverames/iMCP that referenced this pull request Nov 20, 2026
oliverames pushed a commit to oliverames/iMCP that referenced this pull request Nov 20, 2026
Mattt's mattt#385 review:

- files_stat no longer unwraps canonicalComponents.last! for a link that
  resolves to the shared root: an empty canonical path is the root.
- files_trash does not report success when F_GETPATH fails; it reports
  that the result could not be confirmed. If putting a swapped item back
  fails, the error is reported instead of dropped.
- write closes its output descriptor when writeAll throws, on both the
  replace and the create path.

A test covers stat on a link to the shared root.
@oliverames

oliverames commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks @mattt. All three are fixed, and I ran the signed sandboxed pass you asked for.

files_stat doesn't unwrap canonicalComponents.last! for a link that resolves to the shared root anymore; an empty canonical path is the root itself, and there's a test for it. files_trash says the result couldn't be confirmed when F_GETPATH fails, and it reports the error when putting a swapped item back fails instead of saying nothing was changed. write closes its output descriptor when writeAll throws, on both the replace and create paths.

The signed sandboxed pass ran on a Debug build: I added a folder in Settings → Files → Add Folder, quit and relaunched the app (both shared folders stayed available, so the bookmarks persist), then listed, read (byte for byte), wrote, and trashed a file. The file went to the Trash and was still there, so nothing was deleted. Originals untouched.

imcp-serverTests is at 243 tests with 1 skipped and no failures, and format is clean.

@oliverames

oliverames commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor Author

@mattt The macOS 26 job is failing on the stale Helper module cache described in #393, which is the runner image moving to Xcode 26.0.1. The macOS 27 job passes, and #393's macOS 26 job is green. Once #393 lands I'll rebase onto main and this job should go green.

@oliverames oliverames mentioned this pull request Nov 21, 2026
15 tasks
@oliverames

Copy link
Copy Markdown
Contributor Author

@mattt I went back over this and found one real hole and two smaller things, all fixed.

The Photos library check could be skipped with a long s. Test.photoſlibrary opens the real Test.photoslibrary on APFS, but lowercased() doesn't fold ſ, so files_trash or files_move with that spelling would have trashed or renamed a library. Names are now compared after Unicode folding, and move and trash also check the item's path on disk before acting.

files_write replacing a file dropped its Finder tags and other extended attributes, and a request in different case renamed the file. It now copies the extended attributes and ACL, keeps the mode and the name's spelling on disk, and still gives the new content a new modification date. I also reworded "Nothing is deleted for good" in the description, since a replace does overwrite content.

imcp-serverTests is at 265 with 1 skipped and no failures.

oliverames pushed a commit to oliverames/iMCP that referenced this pull request Nov 21, 2026
Mattt's mattt#385 review:

- files_stat no longer unwraps canonicalComponents.last! for a link that
  resolves to the shared root: an empty canonical path is the root.
- files_trash does not report success when F_GETPATH fails; it reports
  that the result could not be confirmed. If putting a swapped item back
  fails, the error is reported instead of dropped.
- write closes its output descriptor when writeAll throws, on both the
  replace and the create path.

A test covers stat on a link to the shared root.
Oliver Ames added 7 commits November 25, 2026 07:37
Tools list, read, stat, and search the folders a person shares, and write,
append, make folders, move, and trash in them. Paths resolve inside a shared
folder only, folders are opened one step at a time without following links,
Photos libraries are refused, and nothing is deleted for good.
Settings lists the shared folders with Add and Remove. The service uses the
Allow writes setting, which is off by default, so the tools that change files
aren't listed until it is turned on. Folders are kept as app-scoped
security-scoped bookmarks that iMCP opens only while a tool runs.
Mattt's mattt#385 review:

- files_stat no longer unwraps canonicalComponents.last! for a link that
  resolves to the shared root: an empty canonical path is the root.
- files_trash does not report success when F_GETPATH fails; it reports
  that the result could not be confirmed. If putting a swapped item back
  fails, the error is reported instead of dropped.
- write closes its output descriptor when writeAll throws, on both the
  replace and the create path.

A test covers stat on a link to the shared root.
…name when files_write replaces a file

APFS folds the long s, so a Photos library could be named with a spelling that passed the name check, and files_trash and files_move never open their final component. Both now check where the item is on disk, and the name check folds case, diacritics and width.

files_write copies the replaced file's extended attributes, ACL and mode to the new file, and keeps the on-disk spelling of the name.
COPYFILE_METADATA includes COPYFILE_STAT, which copied the old modification
date onto the new content. Copy only extended attributes and the ACL, and set
the mode as before.
@oliverames

Copy link
Copy Markdown
Contributor Author

@mattt Rebased on main now that #384 is in, with the #377 commits dropped. The code you reviewed hasn't changed.

oliverames pushed a commit to oliverames/iMCP that referenced this pull request Nov 25, 2026
@mattt
mattt merged commit 25fda3e into mattt:main Nov 29, 2026
2 checks passed
@mattt

mattt commented Nov 29, 2026

Copy link
Copy Markdown
Owner

@oliverames I merged main into this branch to sort out the conflicts with #373, and both are in now. Thanks for your patience with all the back-and-forth on these.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants