栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

编写扩展文件属性快速示例

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

编写扩展文件属性快速示例

这是Swift 5中可能的实现,作为的扩展

URL
,带有获取,设置,列出和删除文件扩展属性的方法。(可在编辑历史记录中找到快速2、3和4代码。)

extension URL {    /// Get extended attribute.    func extendedAttribute(forName name: String) throws -> Data  {        let data = try self.withUnsafeFileSystemRepresentation { fileSystemPath -> Data in // Determine attribute size: let length = getxattr(fileSystemPath, name, nil, 0, 0, 0) guard length >= 0 else { throw URL.posixError(errno) } // Create buffer with required size: var data = Data(count: length) // Retrieve attribute: let result =  data.withUnsafeMutableBytes { [count = data.count] in     getxattr(fileSystemPath, name, $0.baseAddress, count, 0, 0) } guard result >= 0 else { throw URL.posixError(errno) } return data        }        return data    }    /// Set extended attribute.    func setExtendedAttribute(data: Data, forName name: String) throws {        try self.withUnsafeFileSystemRepresentation { fileSystemPath in let result = data.withUnsafeBytes {     setxattr(fileSystemPath, name, $0.baseAddress, data.count, 0, 0) } guard result >= 0 else { throw URL.posixError(errno) }        }    }    /// Remove extended attribute.    func removeExtendedAttribute(forName name: String) throws {        try self.withUnsafeFileSystemRepresentation { fileSystemPath in let result = removexattr(fileSystemPath, name, 0) guard result >= 0 else { throw URL.posixError(errno) }        }    }    /// Get list of all extended attributes.    func listExtendedAttributes() throws -> [String] {        let list = try self.withUnsafeFileSystemRepresentation { fileSystemPath -> [String] in let length = listxattr(fileSystemPath, nil, 0, 0) guard length >= 0 else { throw URL.posixError(errno) } // Create buffer with required size: var namebuf = Array<CChar>(repeating: 0, count: length) // Retrieve attribute list: let result = listxattr(fileSystemPath, &namebuf, namebuf.count, 0) guard result >= 0 else { throw URL.posixError(errno) } // Extract attribute names: let list = namebuf.split(separator: 0).compactMap {     $0.withUnsafeBufferPointer {         $0.withMemoryRebound(to: UInt8.self) {  String(bytes: $0, encoding: .utf8)         }     } } return list        }        return list    }    /// Helper function to create an NSError from a Unix errno.    private static func posixError(_ err: Int32) -> NSError {        return NSError(domain: NSPOSIXErrorDomain, pre: Int(err), userInfo: [NSLocalizedDescriptionKey: String(cString: strerror(err))])    }}

用法示例:

let fileURL = URL(fileURLWithPath: "/path/to/file")let attr1 = "com.myCompany.myAttribute"let attr2 = "com.myCompany.otherAttribute"let data1 = Data([1, 2, 3, 4])let data2 = Data([5, 6, 7, 8, 9])do {    // Set attributes:    try fileURL.setExtendedAttribute(data: data1, forName: attr1)    try fileURL.setExtendedAttribute(data: data2, forName: attr2)    // List attributes:    let list = try fileURL.listExtendedAttributes()    print(list)    // ["com.myCompany.myAttribute", "com.myCompany.otherAttribute", "other"]    let data1a = try fileURL.extendedAttribute(forName: attr1)    print(data1a as NSData)    // <01020304>    // Remove attributes    for attr in list {        try fileURL.removeExtendedAttribute(forName: attr)    }} catch let error {    print(error.localizedDescription)}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/446444.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号