Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if a volume supports Trash?

Tags:

macos

cocoa

I am writing an app that will be deleting files. If the volume supports the Trash folder, I want to move the files there, otherwise just remove them.

It may just be my search mojo failing me, but I cannot find how to do this.

The intent here is to do something like (pseudocode):

if (itemURL on volume that supports Trash)
    use trashItemAtURL:resultingItemURL:error:
else
    use removeItemAtURL:error:

I am aware of recycleURLs:completionHandler: but, for SMB volumes for example, it puts up a dialog about how the file(s) will be immediately deleted. And I would like to deal the the files individually so that I can update a display. I suppose I could use recycleURLs:completionHandler: if File System Events work on SMB volumes but I am not sure they do.

Anyhoo..Is there some API I am missing or some recommended technique that I just cant seem to find?

like image 242
vagrant Avatar asked Oct 04 '22 03:10

vagrant


1 Answers

A nice person from the Apple Dev Forums hunted up the answer:

From the 10.8 Foundation release notes:

NSFileManager Trash APIs In Mac OS 10.8, NSFileManager has new methods to manage the Trash. The -[NSFileManager trashItemAtURL:resultingItemURL:error:] will attempt to move the item at the given URL to the trash, returning the resulting URL by reference. As part of this operation, the system may rename the file to avoid name conflicts; if so, the resultingItemURL will reflect this new name.

[snip]

Some volumes may not support a Trash folder, so these methods will report failure by returning NO or nil and an NSError with NSFeatureUnsupportedError. NSFeatureUnsupportedError is a new error code in the NSCocoaErrorDomain that indicates a failure to perform the requested operation because the feature is not supported, either because the file system lacks the feature, or required libraries are missing, or other similar reasons.

Not sure why that doc never came up on search.

like image 118
vagrant Avatar answered Oct 13 '22 11:10

vagrant