node-fs-extra icon indicating copy to clipboard operation
node-fs-extra copied to clipboard

feature request: fs.softDelete or the like?

Open Pomax opened this issue 6 years ago • 12 comments

Both Windows and MacOS has "safe" delete APIs for moving things to, respectively, the Recycle Bin and the Trash, from where files can be undeleted again - it would be super nice if fs-extra had a way to unlink files in a way that made them end up in those locations, rather than outright unlinking.

And I honestly have no idea what a good name for that would be, softDelete is kind of weird, maybe safeUnlink? trash? (there is a trash package already named that, so... maybe that? Though I couldn't get it to actually delete files to the recycle bin on the machine I tested it on)

Pomax avatar May 14 '19 23:05 Pomax

Yeah, https://github.com/sindresorhus/trash should be the package for that. If you're having issues with it, might try opening an issue there. Not sure if it's within scope for fs-extra to include that functionality, though.

RyanZim avatar May 15 '19 00:05 RyanZim

Maybe, but let's be fair: the whole reason we have fs-extra at all is because "[JP] got tired of including mkdirp, rimraf, and ncp in most of my projects.", so adding trash as a safe unlink for the vast majority of developers is probably not really out of scope? That feels like it should fit right in.

Pomax avatar May 15 '19 03:05 Pomax

@jprichardson @manidlou @JPeer264 what's your opinions here?

RyanZim avatar May 15 '19 13:05 RyanZim

@jprichardson @manidlou @JPeer264 bump

RyanZim avatar Jan 30 '20 15:01 RyanZim

I am not completely sure but I feel like it is out of scope. For this, we probably need to have OS specific implementations. Also, I don't think we can achieve this without running some shell commands (if there is a way without using shell, I would love to know about it) and we all know that we don't deal with shell in this lib because of its security issues.

manidlou avatar Feb 06 '20 03:02 manidlou

what if you just wrap the https://github.com/sindresorhus/trash package, and expose its functions as part of fs-extra?

Pomax avatar Feb 06 '20 15:02 Pomax

The functions of trash are too few. We should need a nodejs global file permission, similar to fs.lock() or fs.auth(), to restrict file deletion and modification to a certain secure directory, such as the same level or src folder. Will you add this feature to this project? @RyanZim

idler8 avatar Mar 14 '20 04:03 idler8

@idler8 I'm not understanding what you're proposing?

RyanZim avatar Mar 14 '20 14:03 RyanZim

"being able to secure a directory" feels pretty out of scope for fs-extra, which as far as I understand it is a convenience package that lets you import one package, instead of always having to import rimraf, mkdirp, etc. because almost any project that works with the filesystem will end up relying on those.

Pomax avatar Mar 14 '20 20:03 Pomax

If I run this code at the beginning of the scriptfs.lock('./');. I will return an error when I run fs.unlink('../a.json')/fs.write('../a.json') It will be handled correctly when I run fs.unlink('./b.json') Until I run fs.lock (null) or fs.unlock () Can you understand what i mean @RyanZim

idler8 avatar Mar 19 '20 02:03 idler8

I have an immature idea

fs.lock = function(lockpath) {
	if (!lockpath && fs.unlink.lock) return (fs.unlink = fs.unlink.original);
	if (fs.unlink.lock) return (fs.unlink.lock = lockpath);
	var unlink = fs.unlink;
	fs.unlink = function(filepath, callback) {
		//Check filepath before running unlink
	};
	fs.unlink.lock = lockpath;
	fs.unlink.original = unlink;
};

idler8 avatar Mar 19 '20 02:03 idler8

Ah, I got your idea; basically a sandboxed filesystem. That's another whole level of complexity, and out of scope for fs-extra. Might make a great standalone package, though.

RyanZim avatar Mar 19 '20 14:03 RyanZim