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

Cocoa 如何将符号链接移动到垃圾箱?,cocoa,symlink,core-services,recycle-bin,Cocoa,Symlink,Core Services,Recycle Bin,我看不到不跟随链接的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 whee

我看不到不跟随链接的
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);

使用
FSPathMakeRefWithOptions()
生成链接的FSRef。然后使用
fsmoveobjecttotrashync()
将其删除。

另一种方法是告诉NSWorkspace“回收”它,方法是将其与或一起发送


我不知道这两种方法在符号链接上的效果如何,但如果您不想处理我的复古未来主义方法,那么值得一试

//
//梅因·斯威夫特
//回收
//
//用途:回收
//
进口基金会
导入应用程序包
var args=NSProcessInfo.processInfo().arguments
args.removateIndex(0)//列表中的第一项是程序本身
var w=NSWorkspace.sharedWorkspace()
var fm=NSFileManager.defaultManager()
用于arg中的arg{
let path=arg.StringBystandardIngPath;
让file=path.lastPathComponent
让source=path.stringByDeletingLastPathComponent
w、 执行文件操作(NSWorkspaceRecycleOperation,
资料来源:资料来源,
目的地:“,
文件:[文件],
标签:无)
}

在10.6中介绍。很好的发现。neoneye:10.6中只引入了后一种方法。前一种方法从10.0开始就存在,现在仍然受到支持。
//
//  main.swift
//  recycle
//
//  usage: recycle <files or directories to throw out>
//

import Foundation
import AppKit

var args = NSProcessInfo.processInfo().arguments
args.removeAtIndex(0)   // first item in list is the program itself

var w = NSWorkspace.sharedWorkspace()
var fm = NSFileManager.defaultManager()

for arg in args {
    let path = arg.stringByStandardizingPath;

    let file = path.lastPathComponent
    let source = path.stringByDeletingLastPathComponent

    w.performFileOperation(NSWorkspaceRecycleOperation,
        source:source,
        destination: "",
        files: [file],
        tag: nil)
}