Cotton code
92 lines · 4.2 KB · SwiftRaw
// Where the phone keeps its database: the app's OWN container, never the app// group's.//// iOS kills a suspended app that holds a lock on a file in a SHARED container// (RunningBoard, 0xDEAD10CC): another process may need that file, and a// suspended holder could keep it locked for ever. The core keeps its replica// open on worker threads, so with the data folder in the app group every trip// to the home screen risked a kill — the owner's phone was killed three times// on 6 Oct 2026, each one a cold launch and the app lock asking again. The// extension never needed the database: the share sheet drops files into// `share-inbox`.//// So the data folder is `Application Support/Cotton` in the app's own// container, and the app group keeps only what the extensions exchange with the// app: `Library` (the shared `UserDefaults`), `share-inbox` and `recordings`.//// A phone that ran an earlier build has its data folder at the group's root —// the replica, the media, `client/connection.json` that says which server it is// paired with. `adopt` moves each of those entries across, one rename at a// time, so the phone keeps its pairing and its downloaded media. It runs on// every open and moves only what is still left behind, so an open that was// killed half-way finishes on the next one. The media rows name their files by// absolute path; the core rewrites them on open (`follow_data_dir`), told the// group's path as `previousContainerPath`.import Foundationpublic enum PhoneDataDirectory {    /// The folder under Application Support that holds the core's data.    public static let folderName = "Cotton"    /// What stays in the app group: the shared `UserDefaults` (and anything    /// else the system keeps under `Library`), the share sheet's inbox and the    /// meeting audio's holding area — plain files, no lock held on any of    /// them. Everything else at the group's root was written by the core.    public static let groupOwned: Set<String> = [        "Library", ShareChannel.inboxDirectoryName, MeetingFiles.directoryName,    ]    /// The app's own data folder, created if it is missing.    public static func own(fileManager: FileManager = .default) throws -> URL {        let support = try fileManager.url(            for: .applicationSupportDirectory,            in: .userDomainMask,            appropriateFor: nil,            create: true        )        let folder = support.appendingPathComponent(folderName, isDirectory: true)        try fileManager.createDirectory(at: folder, withIntermediateDirectories: true)        return folder    }    /// Move the core's entries out of the app group's container into `own`.    ///    /// Returns the names moved. An entry already present in `own` is left    /// where it is and logged: the copy in `own` is the one the core has been    /// using, and replacing it would lose its writes. Hidden names are the    /// system's (the container's own metadata file) and are never moved.    @discardableResult    public static func adopt(        from group: URL,        into own: URL,        fileManager: FileManager = .default    ) -> [String] {        let names: [String]        do {            names = try fileManager.contentsOfDirectory(atPath: group.path)        } catch {            Log.warn("data folder: the app group could not be listed (\(error))")            return []        }        var moved: [String] = []        for name in names.sorted() where !name.hasPrefix(".") && !groupOwned.contains(name) {            let from = group.appendingPathComponent(name)            let to = own.appendingPathComponent(name)            if fileManager.fileExists(atPath: to.path) {                Log.warn("data folder: \(name) is in both the app group and the app's own folder; the app group's copy is left")                continue            }            do {                try fileManager.moveItem(at: from, to: to)                moved.append(name)            } catch {                Log.warn("data folder: \(name) could not be moved out of the app group (\(error))")            }        }        if !moved.isEmpty {            Log.write("data folder: moved \(moved.joined(separator: ", ")) out of the app group")        }        return moved    }}