Skip to content

Fixing MoveItemToTrash behavior in Windows - #3337

Closed
tylergibson wants to merge 1 commit into
electron:masterfrom
tylergibson:FixMoveItemToTrash-Windows
Closed

tylergibson wants to merge 1 commit into
electron:masterfrom
tylergibson:FixMoveItemToTrash-Windows

Conversation

@tylergibson

Copy link
Copy Markdown
Contributor

Fixes #2715 by implementing IFileOperation (used in Windows Vista forward), replacing SHFileOperation. This allows us to do a filesystem PreDeleteItem check to ensure the file can actually go into the Recycle Bin. The existing behavior actually deletes files that cannot be recycled.

Before moving to IFileOperation, I attempted to extend the current implementation by adding the three checks (Type of Drive, Existence of Recycle Bin on that drive, and free space available in the Bin) with:

INT drive_type = GetDriveType((LPCWSTR)(path_components.at(0) + L"\\").c_str());
QueryDosDevice((LPCWSTR)path_components.at(0).c_str(), dos_path_chars, 100);
rbinfo.cbSize = sizeof(SHQUERYRBINFO);
SHQueryRecycleBin((LPCWSTR)path.value().c_str(), &rbinfo);

But these operations can be extremely slow, especially the SHQueryRecycleBin, which can take seconds to execute, locking the UI in the mean time. And there are still corner cases where this can fail.

So I move to reimplement with IFileOperation. It needs a full implementation of the interface IFileOperationProgressSink to be able to check Pre-Delete for the Recycle Bin. I authored the class implementation inline in platform_util_win.cc and only implemented the minimum methods required for MoveItemToTrash.

It may make sense to actually refactor the IFileOperationProgressSink class externally and use it more generally for other special case file I/O issues on Windows. SHFileOperation is still faster in execution, but has many limitations in use.

Behavior is now correct. Files that cannot be recycled will not be deleted and the function will return false.

@tylergibson
tylergibson force-pushed the FixMoveItemToTrash-Windows branch from 3a5b002 to d10c245 Compare November 4, 2015 22:39
Comment thread atom/common/platform_util_win.cc Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Your spacing is off here, relative to the methods that follow

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless the style in this code is (void), it hasn't been used in C++ in a LONG time, also dtors by definition have no params, so saying void is redundant.

@tylergibson
tylergibson force-pushed the FixMoveItemToTrash-Windows branch from d10c245 to 3af743b Compare November 4, 2015 22:45
Comment thread atom/common/platform_util_win.cc Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I would personally remove all the IDL annotation (/[in]/), it serves no real purpose here and adds noise (especially the ones like [string][unique][in])

@tylergibson
tylergibson force-pushed the FixMoveItemToTrash-Windows branch from 3af743b to 02c9f68 Compare November 4, 2015 23:34
@tylergibson

Copy link
Copy Markdown
Contributor Author

Thanks for the feedback Ryan, code updated for clarity and added smart pointers for leak safety :)

@tylergibson
tylergibson force-pushed the FixMoveItemToTrash-Windows branch 2 times, most recently from ad8d042 to 7f534e0 Compare November 5, 2015 01:41
…moved to a drive's recycle bin before attempting to delete it.
@tylergibson
tylergibson force-pushed the FixMoveItemToTrash-Windows branch from 7f534e0 to c7d92ab Compare November 5, 2015 02:29
zcbenz pushed a commit that referenced this pull request Nov 5, 2015
Modifying MoveItemToTrash for Windows to actually verify file can be
moved to a drive's recycle bin before attempting to delete it.

PR: #3337.
@zcbenz

zcbenz commented Nov 5, 2015

Copy link
Copy Markdown
Contributor

It has been committed at 4ff5ce4, thanks!

@kevinsawicki

Copy link
Copy Markdown
Contributor

@tylergibson @ryanmolden thanks so much for this 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants