Skip to content

Instantly share code, notes, and snippets.

@Brandon7CC
Created April 6, 2024 04:40
Show Gist options
  • Save Brandon7CC/048635b2555c411cf1bffab2c6d7f3ad to your computer and use it in GitHub Desktop.
Save Brandon7CC/048635b2555c411cf1bffab2c6d7f3ad to your computer and use it in GitHub Desktop.
Given a file path is this file quarantined? To do this we use `getxattr` and look for `com.apple.quarantine` which is applied by File Quarantine-aware applications.
//
// FileMetadataHelpers.swift
// FileMetadataHelpers
//
// Created by Brandon Dalton on 10/7/23.
//
import Foundation
/// Helper functions for working with file metadata.
public struct FileMetadataHelpers {
public enum FileQuarantineStatus {
/// The file is quarantined.
case quarantined
/// The file is not quarantined.
case notQuarantined
/// The file was not found.
case fileNotFound
}
/// Checks if a file is quarantined.
///
/// - Parameter filePath: The path to the file to check.
/// - Returns: A `FileQuarantineStatus` value indicating whether the file is quarantined, not quarantined, or not found.
public static func isFileQuarantined(filePath: String) -> FileQuarantineStatus {
let fileManager = FileManager.default
guard fileManager.fileExists(atPath: filePath) else {
return .fileNotFound
}
/// Reference: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/getxattr.2.html
let quarantineAttribute = getxattr(filePath, "com.apple.quarantine", nil, 0, 0, 0)
return quarantineAttribute > 0 ? .quarantined : .notQuarantined
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment