Skip to content

Commit bedea89

Browse files
committed
shell: return status for moveItemToTrash on all platforms
1 parent 8aae7c4 commit bedea89

File tree

5 files changed

+38
-16
lines changed

5 files changed

+38
-16
lines changed

atom/common/platform_util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void OpenItem(const base::FilePath& full_path);
2626
void OpenExternal(const GURL& url);
2727

2828
// Move a file to trash.
29-
void MoveItemToTrash(const base::FilePath& full_path);
29+
bool MoveItemToTrash(const base::FilePath& full_path);
3030

3131
void Beep();
3232

atom/common/platform_util_linux.cc

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace {
1515

16-
void XDGUtil(const std::string& util, const std::string& arg) {
16+
bool XDGUtil(const std::string& util, const std::string& arg) {
1717
std::vector<std::string> argv;
1818
argv.push_back(util);
1919
argv.push_back(arg);
@@ -27,8 +27,15 @@ void XDGUtil(const std::string& util, const std::string& arg) {
2727
options.environ["MM_NOTTTY"] = "1";
2828

2929
base::ProcessHandle handle;
30-
if (base::LaunchProcess(argv, options, &handle))
31-
base::EnsureProcessGetsReaped(handle);
30+
if (base::LaunchProcess(argv, options, &handle)) {
31+
int exit_code;
32+
base::Process process(handle);
33+
base::EnsureProcessGetsReaped(handle);
34+
process.WaitForExit(&exit_code);
35+
return (exit_code == 0);
36+
}
37+
38+
return false;
3239
}
3340

3441
void XDGOpen(const std::string& path) {
@@ -65,8 +72,8 @@ void OpenExternal(const GURL& url) {
6572
XDGOpen(url.spec());
6673
}
6774

68-
void MoveItemToTrash(const base::FilePath& full_path) {
69-
XDGUtil("gvfs-trash", full_path.value());
75+
bool MoveItemToTrash(const base::FilePath& full_path) {
76+
return XDGUtil("gvfs-trash", full_path.value());
7077
}
7178

7279
void Beep() {

atom/common/platform_util_mac.mm

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,19 +126,21 @@ void OpenExternal(const GURL& url) {
126126
LOG(WARNING) << "NSWorkspace failed to open URL " << url;
127127
}
128128

129-
void MoveItemToTrash(const base::FilePath& full_path) {
129+
bool MoveItemToTrash(const base::FilePath& full_path) {
130130
DCHECK([NSThread isMainThread]);
131131
NSString* path_string = base::SysUTF8ToNSString(full_path.value());
132132
NSArray* file_array =
133133
[NSArray arrayWithObject:[path_string lastPathComponent]];
134-
if (!path_string || !file_array || ![[NSWorkspace sharedWorkspace]
135-
performFileOperation:NSWorkspaceRecycleOperation
136-
source:[path_string stringByDeletingLastPathComponent]
137-
destination:@""
138-
files:file_array
139-
tag:nil])
134+
int status = [[NSWorkspace sharedWorkspace]
135+
performFileOperation:NSWorkspaceRecycleOperation
136+
source:[path_string stringByDeletingLastPathComponent]
137+
destination:@""
138+
files:file_array
139+
tag:nil];
140+
if (!path_string || !file_array || !status)
140141
LOG(WARNING) << "NSWorkspace failed to move file " << full_path.value()
141142
<< " to trash";
143+
return (status == 0);
142144
}
143145

144146
void Beep() {

atom/common/platform_util_win.cc

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ void OpenExternal(const GURL& url) {
164164
}
165165
}
166166

167-
void MoveItemToTrash(const base::FilePath& path) {
167+
bool MoveItemToTrash(const base::FilePath& path) {
168168
// SHFILEOPSTRUCT wants the path to be terminated with two NULLs,
169169
// so we have to use wcscpy because wcscpy_s writes non-NULLs
170170
// into the rest of the buffer.
@@ -176,7 +176,20 @@ void MoveItemToTrash(const base::FilePath& path) {
176176
file_operation.wFunc = FO_DELETE;
177177
file_operation.pFrom = double_terminated_path;
178178
file_operation.fFlags = FOF_ALLOWUNDO | FOF_SILENT | FOF_NOCONFIRMATION;
179-
SHFileOperation(&file_operation);
179+
int err = SHFileOperation(&file_operation);
180+
181+
// Since we're passing flags to the operation telling it to be silent,
182+
// it's possible for the operation to be aborted/cancelled without err
183+
// being set (although MSDN doesn't give any scenarios for how this can
184+
// happen). See MSDN for SHFileOperation and SHFILEOPTSTRUCT.
185+
if (file_operation.fAnyOperationsAborted)
186+
return false;
187+
188+
// Some versions of Windows return ERROR_FILE_NOT_FOUND (0x2) when deleting
189+
// an empty directory and some return 0x402 when they should be returning
190+
// ERROR_FILE_NOT_FOUND. MSDN says Vista and up won't return 0x402. Windows 7
191+
// can return DE_INVALIDFILES (0x7C) for nonexistent directories.
192+
return (err == 0 || err == ERROR_FILE_NOT_FOUND || err == DE_INVALIDFILES);
180193
}
181194

182195
void Beep() {

docs/api/shell.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ example, mailto: URLs in the default mail user agent.)
3232

3333
* `fullPath` String
3434

35-
Move the given file to trash.
35+
Move the given file to trash and returns boolean status for the operation.
3636

3737
## shell.beep()
3838

0 commit comments

Comments
 (0)