如何将符号链接移至垃圾箱?
我没有看到 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
将链接移动到垃圾桶
OSStatus status = FSPathMoveObjectToTrashSync(
"/tmp/my_link",
NULL,
kFSFileOperationDefaultOptions
);
NSLog(@"status: %i", (int)status);
输出是
status: 0
但是文件被删除而不是链接
[ 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 ] $
如何将符号链接移动到垃圾桶?
解决方案..感谢 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);
I don't see any options for the FSPathMoveObjectToTrashSync()
function for not following links.
Here is what I have tried
Create a link and a file
[ 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
Move the link to the trash
OSStatus status = FSPathMoveObjectToTrashSync(
"/tmp/my_link",
NULL,
kFSFileOperationDefaultOptions
);
NSLog(@"status: %i", (int)status);
Output is
status: 0
However the file got removed and not the link
[ 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 ] $
How can I move move symlinks to the trash?
The Solution.. thanks to 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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 FSPathMakeRefWithOptions() 生成链接的 FSRef。然后使用
FSMoveObjectToTrashSync()
将其删除。Use
FSPathMakeRefWithOptions()
to generate an FSRef to the link. Then useFSMoveObjectToTrashSync()
to delete it.另一种方法是告诉 NSWorkspace “回收”它,方法是发送 a
performFileOperation:source:destination:files:tag:消息
与
NSWorkspaceRecycleOperation
操作 或 一条recycleURLs:completionHandler:
消息< /a>.我不知道其中任何一个在符号链接上的效果如何,但如果您不想处理 FSRef ,那么值得一试。
The other way would be to tell the NSWorkspace to “recycle” it, by sending it either a
performFileOperation:source:destination:files:tag:
message with theNSWorkspaceRecycleOperation
operation or arecycleURLs:completionHandler:
message.I don't know how well either one of these would work on symlinks, but it's worth trying if you'd rather not deal with
FSRef
s.我的复古未来主义方法
https://github.com/reklis/recycle
my retro-futuristic approach
https://github.com/reklis/recycle