如何将符号链接移动到垃圾桶? [英] How to move a symlink to the trash?

查看:135
本文介绍了如何将符号链接移动到垃圾桶?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有看到 FSPathMoveObjectToTrashSync()没有任何选项用于未关联的链接。



是我尝试过的



创建一个链接和一个文件

  [21:32:41 / tmp] $ touch my_file 
[21:32:45 / tmp] $ ln -s my_file my_link
[21:32:52 / tmp] $ la
共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 
共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 b
$ b

  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);

解决方案

使用 FSPathMakeRefWithOptions / code>生成到链接的FSRef。然后使用 FSMoveObjectToTrashSync()将其删除。


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);

解决方案

Use FSPathMakeRefWithOptions() to generate an FSRef to the link. Then use FSMoveObjectToTrashSync() to delete it.

这篇关于如何将符号链接移动到垃圾桶?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆