Cotton code
66 lines · 3.0 KB · SwiftRaw
// The store root is EXCLUDED FROM iCLOUD BACKUP, and re-asserted on every open.//// **Bytes are offline-first — the phone holds them, permanently.** A person's// whole archive lives in the app group container: the replica, every photograph// and document that has been pulled down, the media the Mac pushed. That is// measured in gigabytes and it is a COPY of a file whose original is on a Mac at// home, so putting it in iCloud backup buys nothing and costs the person their// entire quota — and the failure mode is not a slow backup, it is iOS quietly// refusing to back up the phone at all. AGENTS.md has said this since the media// rule was written; nothing in the tree did it, which is what this file fixes.//// **RE-ASSERTED, never "set once".** The flag is an extended attribute on a// directory, and a directory that is deleted and recreated — a fresh install, a// "start over on this phone", a container the OS rebuilt — comes back without// it. There is no event to hang that on, so the assertion runs at the one moment// the app always passes through: opening the core.//// **A failure is LOUD.** `try?` here would turn "this phone is backing up forty// gigabytes of copies" into silence, and the person would learn it from Apple// rather than from us.import Foundation/// The store root, and what must be true about it before the core opens.public enum StoreBackup {    /// Keep `root` and everything under it out of iCloud backup.    ///    /// Answers whether the flag is now set, for a caller that wants to say so;    /// the call itself logs both outcomes, so nothing has to.    ///    /// Reading the value back is not paranoia: `setResourceValues` writes    /// through a cached `URL` resource dictionary, and the read is the only    /// thing that says the extended attribute actually landed on the file the    /// OS has.    @discardableResult    public static func exclude(_ root: URL) -> Bool {        var url = root        // The cache a `URL` carries is per-instance and is populated by a read;        // clearing it means the read below asks the file system rather than        // answering from whatever this instance was last told.        url.removeAllCachedResourceValues()        do {            var values = URLResourceValues()            values.isExcludedFromBackup = true            try url.setResourceValues(values)        } catch {            Log.warn(                "store root could not be excluded from iCloud backup (\(root.lastPathComponent)): "                    + String(describing: error)            )            return false        }        var read = root        read.removeAllCachedResourceValues()        let landed =            (try? read.resourceValues(forKeys: [.isExcludedFromBackupKey]))?            .isExcludedFromBackup ?? false        if !landed {            Log.warn(                "store root still reads as backed up after the exclusion "                    + "(\(root.lastPathComponent))"            )        }        return landed    }}