|
| 1 | +import CryptoKit |
| 2 | +import Foundation |
| 3 | + |
| 4 | +let documents = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! |
| 5 | +let appGroupData = FileManager.default.containerURL( |
| 6 | + forSecurityApplicationGroupIdentifier: "org.fcitx.Fcitx5")!.appendingPathComponent("data") |
| 7 | + |
| 8 | +extension URL { |
| 9 | + var isDirectory: Bool { |
| 10 | + (try? resourceValues(forKeys: [.isDirectoryKey]))?.isDirectory == true |
| 11 | + } |
| 12 | + |
| 13 | + // Local file name is %-encoded with path() |
| 14 | + func localPath() -> String { |
| 15 | + let path = self.path |
| 16 | + guard let decoded = path.removingPercentEncoding else { |
| 17 | + return path |
| 18 | + } |
| 19 | + return decoded |
| 20 | + } |
| 21 | + |
| 22 | + func exists() -> Bool { |
| 23 | + return FileManager.default.fileExists(atPath: self.path) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +func md5Hash(_ url: URL) -> String { |
| 28 | + guard let fileData = try? Data(contentsOf: url) else { |
| 29 | + return "" |
| 30 | + } |
| 31 | + |
| 32 | + let digest = Insecure.MD5.hash(data: fileData) |
| 33 | + return digest.map { String(format: "%02hhx", $0) }.joined() |
| 34 | +} |
| 35 | + |
| 36 | +// Remove if different type, then copy if different content. |
| 37 | +func sync(_ src: URL, _ dst: URL) -> Bool { |
| 38 | + if !src.exists() { |
| 39 | + return false |
| 40 | + } |
| 41 | + if dst.exists() && dst.isDirectory != src.isDirectory { |
| 42 | + try? FileManager.default.removeItem(at: dst) |
| 43 | + } |
| 44 | + if src.isDirectory { |
| 45 | + try? FileManager.default.createDirectory(at: dst, withIntermediateDirectories: true) |
| 46 | + var success = true |
| 47 | + for fileName in (try? FileManager.default.contentsOfDirectory(atPath: src.localPath())) ?? [] { |
| 48 | + if !sync(src.appendingPathComponent(fileName), dst.appendingPathComponent(fileName)) { |
| 49 | + success = false |
| 50 | + } |
| 51 | + } |
| 52 | + return success |
| 53 | + } |
| 54 | + if dst.exists() { |
| 55 | + let srcHash = md5Hash(src) |
| 56 | + let dstHash = md5Hash(dst) |
| 57 | + if srcHash == dstHash { |
| 58 | + return true |
| 59 | + } |
| 60 | + try? FileManager.default.removeItem(at: dst) |
| 61 | + } else if !dst.deletingLastPathComponent().exists() { |
| 62 | + try? FileManager.default.createDirectory( |
| 63 | + at: dst.deletingLastPathComponent(), withIntermediateDirectories: true) |
| 64 | + } |
| 65 | + do { |
| 66 | + try FileManager.default.copyItem(at: src, to: dst) |
| 67 | + return true |
| 68 | + } catch { |
| 69 | + return false |
| 70 | + } |
| 71 | +} |
0 commit comments