dlang / phobos

The standard library of the D programming language
https://dlang.org
Boost Software License 1.0
1.18k stars 703 forks source link

Unified function to remove files/directories #9665

Open dlangBugzillaToGithub opened 9 years ago

dlangBugzillaToGithub commented 9 years ago

doob (@jacob-carlborg) reported this on 2015-09-23T12:36:31Z

Transfered from https://issues.dlang.org/show_bug.cgi?id=15102

CC List

Description

Currently there are at least three different function to remove files and directories in Phobos. It would be nice to have a single one that handles all cases. For when one just want to remove something, regardless if it's a file or a directory and if it's empty or not.
dlangBugzillaToGithub commented 9 years ago

dlang-bugzilla (@CyberShadow) commented on 2015-10-15T17:40:54Z

Why? This corresponds to neither how the OS APIs work nor how it's done in the shell - there are separate commands to remove a file, directory or a directory and its contents.

If the program created a file/directory, it should already know what sort of filesystem object it is. I can't think of many situations where this would be useful.
dlangBugzillaToGithub commented 9 years ago

doob (@jacob-carlborg) commented on 2015-10-15T19:33:20Z

(In reply to Vladimir Panteleev from comment #1)
> Why? This corresponds to neither how the OS APIs work nor how it's done in
> the shell - there are separate commands to remove a file, directory or a
> directory and its contents.

I don't think that matters at all. Why would we build all these abstractions in the first place?

> If the program created a file/directory, it should already know what sort of
> filesystem object it is. I can't think of many situations where this would
> be useful.

Yes, but I can't imagine why I would want to use separate API's for that. Do you want to use different operators for addition for "long" and for "int" as well? I mean, I assume you know what type it is.
dlangBugzillaToGithub commented 9 years ago

dlang-bugzilla (@CyberShadow) commented on 2015-10-15T19:35:47Z

Files and directories are completely different objects in how they behave. Your analogy doesn't hold - a better analogy would be having separate operators for adding strings and ints.
dlangBugzillaToGithub commented 9 years ago

doob (@jacob-carlborg) commented on 2015-10-15T19:38:53Z

I don't agree.

BTW, in a GUI it's the same operation for all there cases.
dlangBugzillaToGithub commented 9 years ago

dlang-bugzilla (@CyberShadow) commented on 2015-10-15T19:43:40Z

(In reply to Jacob Carlborg from comment #4)
> BTW, in a GUI it's the same operation for all there cases.

Not at all, unless it's a very poor GUI.

A good file manager will display to the user the type of object the user is about to delete for confirmation. Additionally, if the directory is not empty, an additional (or more noticeable) confirmation should be displayed.

You still haven't provided a good use case.
dlangBugzillaToGithub commented 9 years ago

doob (@jacob-carlborg) commented on 2015-10-15T20:11:44Z

(In reply to Vladimir Panteleev from comment #5)
> (In reply to Jacob Carlborg from comment #4)
> > BTW, in a GUI it's the same operation for all there cases.
> 
> Not at all, unless it's a very poor GUI.

I said "same operation", i.e. clicking on the same menu item/button.

> A good file manager will display to the user the type of object the user is
> about to delete for confirmation. Additionally, if the directory is not
> empty, an additional (or more noticeable) confirmation should be displayed.

On OS X, both Finder and Path Finder behave like this:

If a file, empty directory or non-empty directory is put in the trash no confirmation are required. When the trash is emptied there's a generic confirmation, regardless of the content.

For something that would bypass the trash, i.e. removing from a network drive, a confirmation is required. But the it's the same message for all three cases.

For Debian with MATE, Debian with Gnome and Windows 10, it's the same as well.

> You still haven't provided a good use case.

The only reason why you might want to have separate functions is because of performance reasons. But that's no reason why there can't be a unified function.
dlangBugzillaToGithub commented 9 years ago

dlang-bugzilla (@CyberShadow) commented on 2015-10-15T20:43:13Z

(In reply to Jacob Carlborg from comment #6)
> On OS X, both Finder and Path Finder behave like this:

Try the operation that bypasses the recycle bin or equivalent.

In either case, you will be doing the deletion from a view which already displays what type the filesystem object is (e.g. via a file/folder icon), so the file manager will already know the object type when it's doing the deletion.

> The only reason why you might want to have separate functions is because of
> performance reasons. But that's no reason why there can't be a unified
> function.

Performance has nothing to do with it. There are two issues:

1. Bloat. It's dumb to bloat the stdlib with functions that can be written in a few lines of code and will be used once in a blue moon.

2. Atomicity. There is no simple way to atomically check what type an object is and perform the corresponding operation. Between the check and the operation the filesystem may have changed, and ugly things might happen. This is the same reason why "rename" has no "bool overwrite" - overwriting is always on, because that's what the OS API provides. In theory you could rename the object to an unique name, check its type, and then do the appropriate operation, but this sort of complicated machinations are best left for user code.
dlangBugzillaToGithub commented 9 years ago

dlang-bugzilla (@CyberShadow) commented on 2015-10-15T21:20:17Z

(In reply to Jacob Carlborg from comment #6)
> For Debian with MATE, Debian with Gnome and Windows 10, it's the same as
> well.

FWIW, Windows 7 says "file" or "folder" in the message depending on what you're deleting. Gnome (not sure which version) uses the same message (to save on localization costs, I guess) but shows a big icon of the item you're about to delete.