Wie man einen Symlink in den Papierkorb verschieben?
-
27-09-2019 - |
Frage
Ich sehe keine Optionen für die FSPathMoveObjectToTrashSync()
Funktion für nicht Links folgen.
Hier ist, was ich habe versucht,
Erstellen Sie einen Link und eine Datei
[ 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
Bewegen Sie den Link in den Papierkorb
OSStatus status = FSPathMoveObjectToTrashSync(
"/tmp/my_link",
NULL,
kFSFileOperationDefaultOptions
);
NSLog(@"status: %i", (int)status);
Die Ausgabe ist
status: 0
Allerdings habe die Datei entfernt und nicht der 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 ] $
Wie kann ich bewegen bewegen Symlinks in den Papierkorb?
Die Lösung .. dank 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);
Lösung
Mit FSPathMakeRefWithOptions()
eine FSRef auf den Link zu erzeugen. Dann nutzen Sie FSMoveObjectToTrashSync()
es zu löschen.
Andere Tipps
Die andere Möglichkeit wäre, die NSWorkspace zu „recyceln“ es zu sagen, indem sie entweder das Senden eine performFileOperation:source:destination:files:tag:
Nachricht mit die NSWorkspaceRecycleOperation
Betrieb oder eine recycleURLs:completionHandler:
Nachricht
Ich weiß nicht, wie gut entweder eine davon würde auf Symlinks arbeiten, aber es ist ein Versuch wert, wenn Sie lieber nicht mit FSRef
s befassen.
mein retrofuturistische Ansatz
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)
}