import Foundation /// "Cambia attributi" di TC, con gli attributi che esistono sul Mac: Bloccato (al posto di /// Sola lettura), Nascosto, Estensione nascosta, i permessi Unix e la data di modifica. /// Ogni voce a tre stati: nil = lascia com'è. public struct AttributeChange { public var locked: Bool? public var hidden: Bool? public var hiddenExtension: Bool? /// Permessi uno per uno: chiave = bit (0o400 lettura proprietario … 0o001 esecuzione altri). public var permissions: [Int: Bool] = [:] public var modified: Date? public var includeSubfolders = false public init() {} public var isEmpty: Bool { locked == nil && hidden == nil && hiddenExtension == nil && permissions.isEmpty && modified == nil } /// Gli attributi di un file, per "Carica dal file selezionato". public static func read(_ url: URL) -> AttributeChange { var c = AttributeChange() let v = try? url.resourceValues(forKeys: [.isUserImmutableKey, .isHiddenKey, .hasHiddenExtensionKey, .contentModificationDateKey]) c.locked = v?.isUserImmutable ?? false c.hidden = v?.isHidden ?? false c.hiddenExtension = v?.hasHiddenExtension ?? false c.modified = v?.contentModificationDate let perm = ((try? FileManager.default.attributesOfItem(atPath: url.path))?[.posixPermissions] as? Int) ?? 0 for bit in permissionBits { c.permissions[bit] = perm & bit != 0 } return c } public static let permissionBits = [0o400, 0o200, 0o100, 0o040, 0o020, 0o010, 0o004, 0o002, 0o001] /// Applica a ogni elemento (e, se richiesto, a tutto quello che c'è dentro le cartelle). /// Restituisce gli errori come "nome: motivo". public func apply(to urls: [URL], isCancelled: () -> Bool = { false }) -> [String] { var errors: [String] = [] let fm = FileManager() func one(_ u: URL) { do { try applyOne(u, fm: fm) } catch { errors.append("\(u.lastPathComponent): \(error.localizedDescription)") } } for u in urls { if isCancelled() { break } var isDir: ObjCBool = false let exists = fm.fileExists(atPath: u.path, isDirectory: &isDir) guard exists else { errors.append("\(u.lastPathComponent): non c'è più"); continue } if includeSubfolders && isDir.boolValue, let en = fm.enumerator(at: u, includingPropertiesForKeys: nil, options: []) { // Prima il contenuto, poi la cartella: se la si blocca, dentro non si cambia più niente. var inside: [URL] = [] for case let c as URL in en { inside.append(c) } for c in inside.reversed() where !isCancelled() { one(c) } } one(u) } return errors } private func applyOne(_ url: URL, fm: FileManager) throws { var u = url let wasLocked = (try? u.resourceValues(forKeys: [.isUserImmutableKey]))?.isUserImmutable ?? false // Un file bloccato non si cambia: lo si sblocca, si cambia, e si rimette come deve essere. if wasLocked && (hidden != nil || hiddenExtension != nil || !permissions.isEmpty || modified != nil || locked == false) { var v = URLResourceValues() v.isUserImmutable = false try u.setResourceValues(v) } var v = URLResourceValues() if let hidden { v.isHidden = hidden } if let hiddenExtension { v.hasHiddenExtension = hiddenExtension } if hidden != nil || hiddenExtension != nil { try u.setResourceValues(v) } if !permissions.isEmpty { var perm = ((try fm.attributesOfItem(atPath: u.path))[.posixPermissions] as? Int) ?? 0o644 for (bit, on) in permissions { perm = on ? perm | bit : perm & ~bit } try fm.setAttributes([.posixPermissions: perm], ofItemAtPath: u.path) } if let modified { try fm.setAttributes([.modificationDate: modified], ofItemAtPath: u.path) } let lockAfter = locked ?? wasLocked if lockAfter { var l = URLResourceValues() l.isUserImmutable = true try u.setResourceValues(l) } } }