【问题标题】:How to move a symlink to the trash?如何将符号链接移动到垃圾箱?
【发布时间】:2010-05-17 19:41:26
【问题描述】:

我没有看到 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);

【问题讨论】:

    标签: cocoa symlink core-services recycle-bin


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      另一种方法是告诉 NSWorkspace “回收”它,方法是发送 a performFileOperation:source:destination:files:tag: messagethe NSWorkspaceRecycleOperation operationa recycleURLs:completionHandler: message

      我不知道其中任何一个在符号链接上的效果如何,但如果您不想处理FSRefs,那么值得一试。

      【讨论】:

      • 在 10.6 中引入。很棒的发现。会试试的。
      • neoneye:在 10.6 中只引入了后一种方法。前一种方法从 10.0 开始就已经存在并且仍然受支持。
      【解决方案3】:

      我的复古未来主义方法

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-18
        相关资源
        最近更新 更多