简体   繁体   中英

NSFileManager create symbolic link with content

I need to extract files from zip archive. Part of that files can be symbolic links. Contents of that files looks like ../../../Braintree/BraintreeUI/Public/UIColor+BTUI.h My unzip library (ZipZap) have files properties, so I always know - is this file is a symbolic link, or it is regular file:

... // open and read archive, list entries
ZZArchiveEntry *entry = ... // get one entry
BOOL isSymlink = (entry.fileMode & S_IFLNK) == S_IFLNK;

So, if isSymlink == YES my task is to create symbolic link file and write archive item content to that file. I use this code:

NSData *fileData = [entry newDataWithError:nil]; // valid NSData, contents as described above
NSString *filePath = ... // correct and writable path
NSDictionary *attributes = @{NSFileType:NSFileTypeSymbolicLink};

[[NSFileManager defaultManager] createFileAtPath:filePath contents:fileData attributes:attributes];

But as result I get regular file in Finder. When I extract my archive with built-in Mac archive utility - I get correct symbolic links.

I also try this trick to change file type after file creation:

NSDictionary *original = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];

NSMutableDictionary *newProperties = [original mutableCopy];
newProperties[NSFileType] = NSFileTypeSymbolicLink;

[[NSFileManager defaultManager] setAttributes:newProperties ofItemAtPath:filePath error:nil];

NSDictionary *updated = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];

There is no errors, but original and updated dictionaries are same:

NSFileCreationDate = "2017-04-25 22:09:04 +0000";
NSFileExtensionHidden = 0;
NSFileGroupOwnerAccountID = 20;
NSFileGroupOwnerAccountName = staff;
NSFileHFSCreatorCode = 0;
NSFileHFSTypeCode = 0;
NSFileModificationDate = "2017-04-25 22:09:04 +0000";
NSFileOwnerAccountID = 501;
NSFileOwnerAccountName = wind;
NSFilePosixPermissions = 420;
NSFileReferenceCount = 1;
NSFileSize = 55;
NSFileSystemFileNumber = 43896483;
NSFileSystemNumber = 16777217;
NSFileType = NSFileTypeRegular;

What is the correct way to create (or update) file attributes and file type with NSFileManager, using archive entry.fileMode value? In my tests it was 41453 .

You need to use createSymbolicLinkAtPath:withDestinationPath:error: to create a symbolic link, you cannot create a regular file and convert it to a link. HTH

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM