如何推动一个链接到垃圾桶?
-
27-09-2019 - |
题
我没有看到任何选择 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 ] $
我怎么可以移动,移动链接到垃圾桶?
解决..由于抢劫皮尔
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到的链接。然后使用FSMoveObjectToTrashSync()
将其删除。
其他提示
其他的方法就是告诉NSWorkspace"回收",通过发送它 一个 performFileOperation:source:destination:files:tag:
消息 与 的 NSWorkspaceRecycleOperation
操作 或 一个 recycleURLs:completionHandler:
消息.
我不知道如何以及这些会工作上的链接,但它值得一试,如果你不想处理 FSRef
s.
我的复古未来方法
https://github.com/reklis/recycle
//
// 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)
}
不隶属于 StackOverflow