|
| 1 | +//! macOS-specific file metadata retrieval using NSURL resource values. |
| 2 | +//! |
| 3 | +//! Provides access to metadata not available through standard `std::fs`: |
| 4 | +//! - `added_at`: When the file was added to its current directory (moved/copied) |
| 5 | +//! - `opened_at`: When the file was last opened |
| 6 | +
|
| 7 | +use std::path::Path; |
| 8 | + |
| 9 | +use objc2::rc::Retained; |
| 10 | +use objc2_foundation::{NSDate, NSString, NSURL}; |
| 11 | + |
| 12 | +/// Extended macOS metadata for a file. |
| 13 | +pub struct MacOSMetadata { |
| 14 | + /// Unix timestamp: when the file was added to its current directory |
| 15 | + pub added_at: Option<u64>, |
| 16 | + /// Unix timestamp: when the file was last opened |
| 17 | + pub opened_at: Option<u64>, |
| 18 | +} |
| 19 | + |
| 20 | +/// Retrieves macOS-specific metadata for a file using NSURL resource values. |
| 21 | +/// |
| 22 | +/// Returns `None` values for individual fields if they are unavailable on the volume |
| 23 | +/// or if any error occurs during retrieval. |
| 24 | +pub fn get_macos_metadata(path: &Path) -> MacOSMetadata { |
| 25 | + // Helper to convert NSDate to Unix timestamp |
| 26 | + fn nsdate_to_unix(date: Option<Retained<NSDate>>) -> Option<u64> { |
| 27 | + date.and_then(|d| { |
| 28 | + // NSDate timeIntervalSince1970 returns seconds since Unix epoch as f64 |
| 29 | + let interval = d.timeIntervalSince1970(); |
| 30 | + if interval >= 0.0 { Some(interval as u64) } else { None } |
| 31 | + }) |
| 32 | + } |
| 33 | + |
| 34 | + // Convert path to NSString |
| 35 | + let path_str = match path.to_str() { |
| 36 | + Some(s) => s, |
| 37 | + None => { |
| 38 | + return MacOSMetadata { |
| 39 | + added_at: None, |
| 40 | + opened_at: None, |
| 41 | + }; |
| 42 | + } |
| 43 | + }; |
| 44 | + |
| 45 | + let ns_path = NSString::from_str(path_str); |
| 46 | + |
| 47 | + // Create NSURL from file path |
| 48 | + let url = NSURL::fileURLWithPath(&ns_path); |
| 49 | + |
| 50 | + // Fetch added_at (NSURLAddedToDirectoryDateKey) |
| 51 | + let added_at = { |
| 52 | + let key = NSString::from_str("NSURLAddedToDirectoryDateKey"); |
| 53 | + let mut value: Option<Retained<objc2::runtime::AnyObject>> = None; |
| 54 | + let success = unsafe { url.getResourceValue_forKey_error(&mut value, &key) }; |
| 55 | + if success.is_ok() { |
| 56 | + // Cast AnyObject to NSDate if it's a date |
| 57 | + value.and_then(|obj| { |
| 58 | + // Downcast to NSDate - this is safe because we know the key returns NSDate |
| 59 | + let retained = obj.downcast::<NSDate>().ok(); |
| 60 | + nsdate_to_unix(retained) |
| 61 | + }) |
| 62 | + } else { |
| 63 | + None |
| 64 | + } |
| 65 | + }; |
| 66 | + |
| 67 | + // Fetch opened_at (NSURLContentAccessDateKey) |
| 68 | + let opened_at = { |
| 69 | + let key = NSString::from_str("NSURLContentAccessDateKey"); |
| 70 | + let mut value: Option<Retained<objc2::runtime::AnyObject>> = None; |
| 71 | + let success = unsafe { url.getResourceValue_forKey_error(&mut value, &key) }; |
| 72 | + if success.is_ok() { |
| 73 | + value.and_then(|obj| { |
| 74 | + let retained = obj.downcast::<NSDate>().ok(); |
| 75 | + nsdate_to_unix(retained) |
| 76 | + }) |
| 77 | + } else { |
| 78 | + None |
| 79 | + } |
| 80 | + }; |
| 81 | + |
| 82 | + MacOSMetadata { added_at, opened_at } |
| 83 | +} |
0 commit comments