如何将符号链接移动到垃圾箱?

neo*_*eye 4 cocoa symlink recycle-bin core-services

我没有看到FSPathMoveObjectToTrashSync()没有关注链接的功能选项.

这是我尝试过的

创建链接和文件

[ 21:32:41 /tmp ] $ touch my_file
[ 21:32:45 /tmp ] $ ln -s my_file my_link
[ 21:32:52 /tmp ] $ la
total 8
drwxrwxrwt   12 root     wheel   408 17 Maj 21:32 .
drwxr-xr-x@   6 root     wheel   204  9 Sep  2009 ..
-rw-r--r--    1 neoneye  wheel     0 17 Maj 21:32 my_file
lrwxr-xr-x    1 neoneye  wheel     7 17 Maj 21:32 my_link -> my_file
Run Code Online (Sandbox Code Playgroud)

将链接移动到垃圾箱

OSStatus status = FSPathMoveObjectToTrashSync(
    "/tmp/my_link",
    NULL,
    kFSFileOperationDefaultOptions
);
NSLog(@"status: %i", (int)status);
Run Code Online (Sandbox Code Playgroud)

输出是

status: 0
Run Code Online (Sandbox Code Playgroud)

但是文件被删除而不是链接

[ 21:32:55 /tmp ] $ la
total 8
drwxrwxrwt   11 root     wheel   374 17 Maj 21:33 .
drwxr-xr-x@   6 root     wheel   204  9 Sep  2009 ..
lrwxr-xr-x    1 neoneye  wheel     7 17 Maj 21:32 my_link -> my_file
[ 21:33:05 /tmp ] $
Run Code Online (Sandbox Code Playgroud)

如何将移动符号链接移动到垃圾箱?


解决方案......感谢Rob Napier

NSString* path = @"/tmp/my_link";
OSStatus status = 0;

FSRef ref;
status = FSPathMakeRefWithOptions(
    (const UInt8 *)[path fileSystemRepresentation], 
    kFSPathMakeRefDoNotFollowLeafSymlink,
    &ref, 
    NULL
);  
NSAssert((status == 0), @"failed to make FSRef");

status = FSMoveObjectToTrashSync(
    &ref,
    NULL,
    kFSFileOperationDefaultOptions
);
NSLog(@"status: %i", (int)status);
Run Code Online (Sandbox Code Playgroud)

Rob*_*ier 5

使用FSPathMakeRefWithOptions()生成FSRef的链接.然后用FSMoveObjectToTrashSync()它来删除它.