我没有看到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 .
[email protected] 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 .
[email protected] 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纳皮尔
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);
介绍了在10.6。很棒的发现。会尝试。 – neoneye 2010-05-18 07:15:02
neoneye:只有后一种方法在10.6中引入。前一种方法从10.0开始一直存在,并且仍然受到支持。 – 2010-05-18 07:58:54