Repository navigation
systems native files local io
Active contributors: Jason, Erik
This page traces native document I/O, staged content replacement, and in-process mutation serialization. It explains the implementation behind File access, without treating local completion as proof of cloud freshness or upload completion.
| Abstraction | Role |
|---|---|
ICloudDocument |
Path-to-path stream copies through platform document overrides |
ICloudInPlaceDocument / ICloudInPlaceBinaryDocument
|
Whole text/byte document content for in-place writes |
CoordinatedReplaceWriter |
Stage new content and replace an existing file under writing coordination |
PerPathMutationLane |
Async mutex per standardized path, plus sorted two-endpoint acquisition |
CoordinatedIO / CoordinateBridgeError
|
Dispatch-backed synchronous coordination and separate coordination/accessor failures |
CoordinatedIO.readFilesIfPresent / CoordinatedBatchReadFailure
|
One coordinated batch read for many files; the failure names the index of the file that could not be read |
UbiquitousItemMover |
setUbiquitous moves into and out of the container, plus a coordinated directory-creation helper |
VersionExposure |
Explicit version operations; not part of automatic write completion |
| Source | Responsibility |
|---|---|
ios/icloud_storage_plus/Sources/icloud_storage_plus/ICloudDocument.swift |
iOS open/save/close flows and replacement-first write routing |
macos/icloud_storage_plus/Sources/icloud_storage_plus/ICloudDocument.swift |
macOS document reads/writes and presenter-aware replacement selection |
ios/icloud_storage_plus/Sources/icloud_storage_plus/FileWriteHelpers.swift, macos/icloud_storage_plus/Sources/icloud_storage_plus/FileWriteHelpers.swift
|
Stream-copy and atomic text/data staging |
ios/icloud_storage_plus/Sources/icloud_storage_plus_foundation/CoordinatedReplaceWriter.swift, macos/icloud_storage_plus/Sources/icloud_storage_plus_foundation/CoordinatedReplaceWriter.swift
|
Replacement algorithm and coordination dispatch |
ios/icloud_storage_plus/Sources/icloud_storage_plus_foundation/PerPathMutationLane.swift, macos/icloud_storage_plus/Sources/icloud_storage_plus_foundation/PerPathMutationLane.swift
|
Lane map, cancellation-aware async mutex, two-path ordering |
ios/icloud_storage_plus/Sources/icloud_storage_plus_foundation/CoordinatedIO.swift, macos/icloud_storage_plus/Sources/icloud_storage_plus_foundation/CoordinatedIO.swift
|
One- and two-URL coordination bridges and the many-file batch read |
ios/icloud_storage_plus/Sources/icloud_storage_plus/iOSICloudStoragePlugin.swift, macos/icloud_storage_plus/Sources/icloud_storage_plus/macOSICloudStoragePlugin.swift
|
Lane call sites and delete/move/copy implementations |
ios/icloud_storage_plus/Sources/icloud_storage_plus_foundation/UbiquitousItemMover.swift, macos/icloud_storage_plus/Sources/icloud_storage_plus_foundation/UbiquitousItemMover.swift
|
setUbiquitous moves and coordinatedCreateDirectory
|
ios/icloud_storage_plus/Sources/icloud_storage_plus_foundation/VersionExposure.swift, macos/icloud_storage_plus/Sources/icloud_storage_plus_foundation/VersionExposure.swift
|
Explicit NSFileVersion integration |
The streaming document overrides copy from a source URL to a destination URL through InputStream and OutputStream. Both document implementations and the staging helper use a 64 KiB buffer. Partial positive writes are retried until that buffer slice is written; a zero or negative write throws instead of spinning. Parent directories are created when absent. This bounds the copy buffer, not all memory used by the surrounding application (ios/icloud_storage_plus/Sources/icloud_storage_plus/ICloudDocument.swift, macos/icloud_storage_plus/Sources/icloud_storage_plus/ICloudDocument.swift, both file-writing helper files above).
In-place text and binary document subclasses hold a String or Data for in-place writes. Text writes use UTF-8. These are whole-content operations, unlike the path-copy loop.
In-place reads do not open a document. readInPlace and readInPlaceBytes call the plugin's private coordinatedReadData(at:), which runs CoordinatedIO.coordinateReadingReturning with Data(contentsOf:) on a global worker and returns the Flutter result on the main queue. readInPlace decodes the bytes as UTF-8, and invalid UTF-8 throws. Results and error mapping are the same as the earlier document-based reads, and no document object is opened or closed per read (both plugin files, CHANGELOG.md).
readInPlaceBytesMany resolves the container once, then calls CoordinatedIO.readFilesIfPresent. That helper builds one NSFileAccessIntent.readingIntent(with:options:) per file and calls NSFileCoordinator.coordinate(with:queue:byAccessor:) once, with a private OperationQueue. Inside the accessor it reads each intent.url with Data(contentsOf:). A Cocoa NSFileReadNoSuchFileError or NSFileNoSuchFileError becomes a missing entry; any other error stops the batch as a CoordinatedBatchReadFailure, which the plugin maps with the same mapFileNotFoundError / nativeCodeError path as readInPlaceBytes, tagged with the failing relative path. A coordination error fails the call without a path.
For copy-out, iOS opens the path-copy document and reports success before its asynchronous close finishes. macOS directly invokes the document's URL-read override on a global worker. New-file writes fall back to UIDocument.save(..., .forCreating) on iOS and direct NSDocument.write(..., .saveAsOperation, ...) on macOS. The macOS wrappers do not add an explicit CoordinatedIO call around these direct document methods. Read the actual entry path rather than assuming all document-named methods share one coordination wrapper (both document files).
Upload and in-place writes first ask CoordinatedReplaceWriter.overwriteExistingItem to handle an existing destination:
- Check existence with
FileManager.fileExists. Returnfalsewhen absent so the document wrapper can create it. - Read
.isDirectoryKeyand reject an existing directory as a file-content destination. - Create an
.itemReplacementDirectoryappropriate for the destination. - Prepare the entire replacement file there: stream source bytes, write UTF-8 text atomically, or write
Dataatomically. - Coordinate writing with empty writing options and call
FileManager.replaceItemAtusing the URL supplied to the coordinator accessor. - Attempt to remove the staging directory on success or failure; cleanup is best-effort via
try?.
The sources for this sequence are the two CoordinatedReplaceWriter files and both file-writing helpers in the source table. No .current wait, compare-before-write check, conflict winner choice, or automatic version deletion occurs inside this writer.
The iOS document wrappers use CoordinatedReplaceWriter.live, without a presenter. The macOS plugin finds same-path passive watchers, passes the first presenter's reference into a writer, and updates modification-date baselines for the distinct union of initial and current matching registrations after successful replacement. See Queries and presenter lifecycle for that integration (macos/icloud_storage_plus/Sources/icloud_storage_plus/macOSICloudStoragePlugin.swift).
The lane protects the local operation, not Apple's sync lifecycle. Upload, in-place writes, and delete acquire one path; move and copy acquire both endpoints. moveIntoContainer holds the destination's lane and moveOutOfContainer holds the container item's lane; the local endpoint of either move gets no lane. createDirectory and reads do not enter these lanes. Version-exposure handlers are also outside this lane mechanism (both plugin entry files).
sequenceDiagram
participant Task as Async native task
participant Lanes as PerPathMutationLane
participant Worker as Dispatch worker
participant Coord as NSFileCoordinator
Task->>Lanes: Acquire one key or sorted endpoint keys
Note over Task,Lanes: A queued acquisition suspends the task
Lanes-->>Task: Lane acquired
Task->>Worker: Await continuation-backed coordination
Worker->>Coord: Synchronous coordinate call
Coord->>Worker: Accessor with coordinated URL(s)
Worker->>Worker: Synchronous FileManager operation
Worker-->>Task: Resume success or error
Task->>Lanes: Release lane(s) on either outcome
PerPathMutationLane.normalizationKey uses standardizedFileURL.path and removes a non-root trailing slash. The key is a string, not a file identifier. There is no explicit resolvingSymlinksInPath call, filesystem-identity lookup, or ancestor/descendant locking. Do not infer that symlink aliases, case aliases, hard links, or directory/child operations necessarily share a lane (ios/icloud_storage_plus/Sources/icloud_storage_plus_foundation/PerPathMutationLane.swift, macos/icloud_storage_plus/Sources/icloud_storage_plus_foundation/PerPathMutationLane.swift).
Each key owns a FIFO async mutex. FIFO describes waiter registration at that mutex; resolution and preflight happen earlier, so this does not define an end-to-end ordering of Flutter calls. Contended acquisition stores a throwing continuation rather than blocking a worker. Cancelling a queued waiter removes it; throwing bodies release their lanes through defer. Pending counts reclaim unused entries. Cancellation here does not promise interruption of an already running Foundation operation (both lane files).
For two distinct keys, withLanes acquires them in lexicographic order and retains both throughout the body. Equal keys use one lane. Call sites acquire lanes before entering coordinator accessors. This avoids reversed endpoint acquisition and a lane wait inside a coordinator callback; it is not a proof that all filesystem interactions are globally deadlock-free (both lane files and plugin entry files).
CoordinatedIO sends blocking coordination to global dispatch workers and keeps accessors synchronous. A suspended Swift task waits on the continuation, not a DispatchSemaphore. Coordination errors and accessor errors travel separately as CoordinateBridgeError, with coordinator errors checked first (both coordination bridge files).
Replacement dispatch has a platform distinction: the iOS helper can enqueue coordination on a supplied presenter's operation queue, although its plugin content-write path supplies none. The macOS helper always uses a global dispatch queue, including presenter-owned replacements. These copies should not be flattened into one assumed scheduling rule (both replacement-writer files).
The implementations live in both plugin entry files, not in the document subclasses:
| Operation | Native implementation |
|---|---|
| Delete | Check local existence; coordinate a .forDeleting write; remove the accessor's URL |
| Move | Hold both lanes; coordinate source .forMoving and destination .forReplacing; create destination parents and call moveItem on both accessor URLs |
| Copy to an existing destination | Hold both lanes; coordinate the source read; stage a copyItem; synchronously coordinate destination .forReplacing; replace the destination and attempt staging cleanup |
| Copy fallback | Coordinate source .withoutChanges and destination .forReplacing; create parents, remove a destination if one now exists, then copyItem
|
| Create directory |
UbiquitousItemMover.coordinatedCreateDirectory: one coordinated write with empty options that calls createDirectory(withIntermediateDirectories: true)
|
| Move into container | Hold the destination lane; create parents with coordinatedCreateDirectory; call setUbiquitous(true, ...) on a global worker |
| Move out of container | Hold the item lane; fail as not found when neither the item nor its .name.icloud placeholder exists; call setUbiquitous(false, ...) on a global worker |
setUbiquitous coordinates the move itself, so the mover calls it on a DispatchQueue.global worker and never inside a plugin coordinator accessor. Foundation refuses an existing destination; the mover adds no overwrite or merge step (both UbiquitousItemMover files).
The existing-destination copy helper is separate from CoordinatedReplaceWriter. It uses .forReplacing, a nil presenter, and nested synchronous destination coordination inside the source-read accessor. It does not use macOS's presenter-baseline callback. The nesting runs on the dispatch worker entered by CoordinatedIO; it does not acquire a lane from inside the accessor.
Do not generalize staged replacement into a rollback guarantee for every mutation. For example, the copy fallback removes a newly encountered destination before copying and has no explicit partial-destination cleanup in its catch path. These paths report actual Foundation errors; comments about safety are not broader transactional guarantees (both plugin entry files).
VersionExposure delegates enumeration, copy-out, and marking to NSFileVersion. Descriptors encode an archived persistent identifier as base64; copy-out matches against re-archived current candidates rather than decoding an untrusted channel identifier. Empty or unmatched identifiers fail. Marking sets unresolved versions' isResolved flags and removes other versions only when requested (both VersionExposure files).
No write automatically calls those primitives, and the native mark handler does not verify that the application made a backup first. The app's explicit policy is described in Conflict versions.
Change both platform copies of a helper and inspect both document/plugin call sites. Existing coverage includes lane ordering/cancellation/reclamation, replacement accessor URLs and cleanup, Foundation replacement, presenter-owned replacement, coordination-vs-accessor failures, batch reads, and ubiquity moves:
ios/icloud_storage_plus/Sources/icloud_storage_plus_foundation/Tests/icloud_storage_plus_foundationTests/PerPathMutationLaneTests.swiftmacos/icloud_storage_plus/Sources/icloud_storage_plus_foundation/Tests/icloud_storage_plus_foundationTests/PerPathMutationLaneTests.swiftios/icloud_storage_plus/Sources/icloud_storage_plus_foundation/Tests/icloud_storage_plus_foundationTests/CoordinatedReplaceWriterTests.swiftmacos/icloud_storage_plus/Sources/icloud_storage_plus_foundation/Tests/icloud_storage_plus_foundationTests/CoordinatedReplaceWriterTests.swiftios/icloud_storage_plus/Sources/icloud_storage_plus_foundation/Tests/icloud_storage_plus_foundationTests/CoordinatedBatchReadTests.swiftmacos/icloud_storage_plus/Sources/icloud_storage_plus_foundation/Tests/icloud_storage_plus_foundationTests/CoordinatedBatchReadTests.swiftios/icloud_storage_plus/Sources/icloud_storage_plus_foundation/Tests/icloud_storage_plus_foundationTests/UbiquitousItemMoverTests.swiftmacos/icloud_storage_plus/Sources/icloud_storage_plus_foundation/Tests/icloud_storage_plus_foundationTests/UbiquitousItemMoverTests.swift
Some tests inspect source text; helper seams and local Foundation tests do not establish real iCloud/device behavior. Use the Testing guide for the appropriate supported-platform check. The native section snapshot records file/test and author-label counts for both platform trees.