A fast file manager for Windows and macOS that never freezes: tabs, dual panes, archives as folders, Quick Look-style previews. A File Explorer and Finder alternative written in Rust.
Rust archive-managerdesktop-appdual-paneexplorer-alternativefile-browserfile-explorerfile-managerfluent-designgpu-acceleratedgpuimacosmicaproductivityquick-lookrustrust-guitabswindowswindows-11windows-explorer
Mac: six hashes with progress, Stop and a compare box
gelenmedyaf8d1b2260ef3
diff --git a/macos/Sources/RustyFiles/Properties.swift b/macos/Sources/RustyFiles/Properties.swiftindex f54c9323..2c36939b 100644--- a/macos/Sources/RustyFiles/Properties.swift+++ b/macos/Sources/RustyFiles/Properties.swift@@ -10,7 +10,7 @@ import UniformTypeIdentifiers/// Several items show what they have in common and their totals./// - Permissions: owner and group, and what each may do (Windows' Security tab)./// - Details: what Spotlight knows of it (pictures, recordings, documents, apps).-/// - Hashes: MD5, SHA-1 and SHA-256, worked out when asked.+/// - Hashes: MD5, SHA-1, SHA-256, SHA-384, SHA-512 and CRC32, worked out when asked, with a compare box.////// Changes apply as they are made, as in Finder's Get Info. Every disk access happens off the/// main thread.@@ -677,15 +677,16 @@ final class PropertiesWindow: NSWindowController, NSWindowDelegate {// MARK: Hashes+ private static let hashNames = ["MD5", "SHA-1", "SHA-256", "SHA-384", "SHA-512", "CRC32"]++ /// As on Windows (hash_panel.rs): six hashes, worked out when asked, with progress and+ /// Stop; each has a Copy button, and a hash pasted in the box is checked against all of them.private func buildHashes() {let u = urls[0]- let fields = ["MD5", "SHA-1", "SHA-256"].map { _ in value("") }- let go = button(String(localized: "Calculate")) {}- go.target = nil+ let fields = Self.hashNames.map { _ in value("—") }var rows: [(String, NSView)] = []- for (name, f) in zip(["MD5", "SHA-1", "SHA-256"], fields) {+ for (name, f) in zip(Self.hashNames, fields) {f.font = NSFont.monospacedSystemFont(ofSize: 11, weight: .regular)- f.stringValue = "—"let copy = button(String(localized: "Copy")) {NSPasteboard.general.clearContents()NSPasteboard.general.setString(f.stringValue, forType: .string)@@ -697,33 +698,143 @@ final class PropertiesWindow: NSWindowController, NSWindowDelegate {rows.append((name, col))}card(rows)- // the button sits under the card- (go as? ClosureButton)?.handler = { [weak self, weak go] in++ let progress = NSProgressIndicator()+ progress.style = .bar+ progress.controlSize = .small+ progress.isIndeterminate = false+ progress.minValue = 0+ progress.maxValue = 1+ progress.isHidden = true+ progress.widthAnchor.constraint(equalToConstant: 160).isActive = true+ let stop = HashStop()+ let go = button(String(localized: "Calculate")) {}+ let cancel = button(String(localized: "Stop")) { stop.flag = true }+ cancel.isHidden = true+ stack.addArrangedSubview(row([go, cancel, progress]))++ // compare: paste a hash, it is looked for among the six+ let compare = NSTextField()+ compare.placeholderString = String(localized: "Paste a hash to compare")+ compare.font = NSFont.monospacedSystemFont(ofSize: 11, weight: .regular)+ compare.controlSize = .small+ let verdict = NSTextField(labelWithString: "")+ verdict.font = Metrics.textXsSemibold+ let check = { [weak compare, weak verdict] in+ guard let compare, let verdict else { return }+ let typed = Self.normalizedHash(compare.stringValue)+ guard !typed.isEmpty else { verdict.stringValue = ""; return }+ guard fields.allSatisfy({ $0.stringValue.count >= 8 && $0.stringValue.allSatisfy(\.isHexDigit) }) else {+ verdict.stringValue = String(localized: "Calculate the hashes first.")+ verdict.textColor = .secondaryLabelColor+ return+ }+ if let i = fields.firstIndex(where: { $0.stringValue.lowercased() == typed }) {+ verdict.stringValue = "✓ " + String(format: String(localized: "Matches %@"), Self.hashNames[i])+ verdict.textColor = .systemGreen+ } else {+ verdict.stringValue = "✗ " + String(localized: "No hash matches")+ verdict.textColor = .systemRed+ }+ }+ let watcher = HashCompareWatcher(check)+ compare.delegate = watcher+ objc_setAssociatedObject(compare, &HashCompareWatcher.key, watcher, .OBJC_ASSOCIATION_RETAIN)+ add(compare)+ stack.addArrangedSubview(verdict)++ (go as? ClosureButton)?.handler = { [weak self, weak go, weak cancel, weak progress] in+ guard let self else { return }+ stop.flag = falsego?.isEnabled = false+ cancel?.isHidden = false+ progress?.doubleValue = 0+ progress?.isHidden = falsefields.forEach { $0.stringValue = String(localized: "Calculating…") }- self?.later({ Self.hashes(u) }) { result in- guard let result else {+ let token = self.work+ let report: @Sendable (Double) -> Void = { part in+ DispatchQueue.main.async { MainActor.assumeIsolated { if token == self.work { progress?.doubleValue = part } } }+ }+ self.later({ Self.hashes(u, stop: stop, progress: report) }) { result in+ go?.isEnabled = true+ cancel?.isHidden = true+ progress?.isHidden = true+ switch result {+ case .some(let values):+ for (f, h) in zip(fields, values) { f.stringValue = h }+ case .none where stop.flag:+ fields.forEach { $0.stringValue = "—" }+ case .none:fields.forEach { $0.stringValue = String(localized: "The file could not be read.") }- return}- for (f, h) in zip(fields, result) { f.stringValue = h }+ check()}}- stack.addArrangedSubview(go)}- nonisolated private static func hashes(_ u: URL) -> [String]? {+ /// What a pasted hash is compared as: lower case, without spaces, dashes, colons or 0x.+ nonisolated static func normalizedHash(_ s: String) -> String {+ var t = s.lowercased().filter { !$0.isWhitespace && $0 != "-" && $0 != ":" }+ if t.hasPrefix("0x") { t.removeFirst(2) }+ return t+ }++ nonisolated private static func hashes(_ u: URL, stop: HashStop, progress: @Sendable (Double) -> Void) -> [String]? {guard let h = try? FileHandle(forReadingFrom: u) else { return nil }defer { try? h.close() }- var md5 = Insecure.MD5(), sha1 = Insecure.SHA1(), sha256 = SHA256()+ let total = Double((try? u.resourceValues(forKeys: [.fileSizeKey]).fileSize) ?? 0)+ var md5 = Insecure.MD5(), sha1 = Insecure.SHA1(), sha256 = SHA256(), sha384 = SHA384(), sha512 = SHA512()+ var crc = CRC32()+ var done = 0.0, shown = 0.0while let chunk = try? h.read(upToCount: 1 << 20), !chunk.isEmpty {+ if stop.flag { return nil }md5.update(data: chunk)sha1.update(data: chunk)sha256.update(data: chunk)+ sha384.update(data: chunk)+ sha512.update(data: chunk)+ crc.update(chunk)+ done += Double(chunk.count)+ if total > 0, done / total - shown >= 0.01 {+ shown = done / total+ progress(shown)+ }}func hex<D: Digest>(_ d: D) -> String { d.map { String(format: "%02x", $0) }.joined() }- return [hex(md5.finalize()), hex(sha1.finalize()), hex(sha256.finalize())]+ return [hex(md5.finalize()), hex(sha1.finalize()), hex(sha256.finalize()), hex(sha384.finalize()), hex(sha512.finalize()), String(format: "%08x", crc.value)]+ }+}++/// Set from the main thread to stop a hash run; read by the worker.+final class HashStop: @unchecked Sendable {+ var flag = false+}++/// Calls `changed` on every edit of the compare box.+@MainActor+final class HashCompareWatcher: NSObject, NSTextFieldDelegate {+ nonisolated(unsafe) static var key = 0+ let changed: () -> Void+ init(_ changed: @escaping () -> Void) { self.changed = changed }+ func controlTextDidChange(_ obj: Notification) { changed() }+}++/// CRC-32 (IEEE, as zip and Windows' panel use).+struct CRC32 {+ private static let table: [UInt32] = (0..<256).map { i -> UInt32 in+ var c = UInt32(i)+ for _ in 0..<8 { c = c & 1 != 0 ? 0xEDB8_8320 ^ (c >> 1) : c >> 1 }+ return c+ }+ private var crc: UInt32 = 0xFFFF_FFFF+ mutating func update(_ data: Data) {+ data.withUnsafeBytes { (buf: UnsafeRawBufferPointer) in+ var c = crc+ for b in buf { c = Self.table[Int((c ^ UInt32(b)) & 0xFF)] ^ (c >> 8) }+ crc = c+ }}+ var value: UInt32 { crc ^ 0xFFFF_FFFF }}// MARK: - piecesdiff --git a/macos/Sources/RustyFiles/SelfTest.swift b/macos/Sources/RustyFiles/SelfTest.swiftindex a502d7db..57ce54bd 100644--- a/macos/Sources/RustyFiles/SelfTest.swift+++ b/macos/Sources/RustyFiles/SelfTest.swift@@ -33,6 +33,11 @@ enum SelfTest {tileItem.prepareForReuse()check(!tileItem.tile.hovered && tileItem.tile.hoverPoint == nil, "a reused grid tile forgets the old item's hover")+ var crc = CRC32()+ crc.update(Data("123456789".utf8))+ check(crc.value == 0xCBF4_3926, "CRC32 gives the standard check value")+ check(PropertiesWindow.normalizedHash(" 0xAB-CD:ef 12 ") == "abcdef12", "a pasted hash is compared without spaces, dashes and 0x")+// 0. the Git look-up ends at the disk's root and finds a repository from inside it (issue 429)check(GitRepo.repoRoot(URL(fileURLWithPath: "/")) == nil, "no repository above the root")check(GitRepo.repoRoot(URL(string: "file:///../")!) == nil, "a URL above the root ends too")