Skip to content

Instantly share code, notes, and snippets.

@coolaj86
Last active May 22, 2022 22:45
  • Star 18 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save coolaj86/992478 to your computer and use it in GitHub Desktop.
fs.copy and fs.move for Node.JS
/*
UPDATE: this has finally been pushed to npm as `fs.extra`
URL: https://github.com/coolaj86/utile-fs/tree/master/fs.extra
*/
(function () {
"use strict";
console.warn('[Deprecated] See https://github.com/coolaj86/utile-fs');
var fs = require('fs')
, util = require('util')
;
fs.copy = function (src, dst, cb) {
console.warn('[Deprecated] See https://github.com/coolaj86/utile-fs');
function copy(err) {
var is
, os
;
if (!err) {
return cb(new Error("File " + dst + " exists."));
}
fs.stat(src, function (err) {
if (err) {
return cb(err);
}
is = fs.createReadStream(src);
os = fs.createWriteStream(dst);
util.pump(is, os, cb);
});
}
fs.stat(dst, copy);
};
fs.move = function (src, dst, cb) {
console.warn('[Deprecated] See https://github.com/coolaj86/utile-fs');
function copyIfFailed(err) {
if (!err) {
return cb(null);
}
fs.copy(src, dst, function(err) {
if (!err) {
// TODO
// should we revert the copy if the unlink fails?
fs.unlink(src, cb);
} else {
cb(err);
}
});
}
fs.stat(dst, function (err) {
if (!err) {
return cb(new Error("File " + dst + " exists."));
}
fs.rename(src, dst, copyIfFailed);
});
};
}());
/*
UPDATE: this has finally been pushed to npm as `fs.extra`
URL: https://github.com/coolaj86/node-examples-js/tree/master/fs.extra
*/
(function () {
"use strict";
require('./fs.copy');
var fs = require('fs')
, sequence = require('futures').sequence()
, exec = require('child_process').exec
, count = 0
;
function fsop(op, a, b, shouldpass) {
var c = count;
count += 1;
return function (n) {
function log(err) {
if (shouldpass && !err) {
console.log(c, op, a, b, 'passed');
n();
} else if (!shouldpass && err) {
console.log(c, op, a, b, 'passed');
n();
} else {
console.log(c, op, a, b, 'failed');
}
}
fs[op](
'file.' + a + '.t.txt'
, 'file.' + b + '.t.txt'
, log
);
};
}
sequence
.then(function (next) {
exec('rm ./file.*.t.txt; touch file.0.t.txt', next);
}) // 0
.then(fsop('copy', 0, 1, true)) // 0, 1
.then(fsop('copy', 0, 2, true)) // 0, 1, 2
.then(fsop('copy', 0, 1, false)) // 0, 1, 2
.then(fsop('copy', 3, 4, false)) // 0, 1, 2
.then(fsop('move', 0, 3, true)) // 1, 2, 3
.then(fsop('move', 0, 4, false)) // 1, 2, 3
.then(fsop('move', 4, 0, false)) // 1, 2, 3
.then(fsop('move', 5, 6, false)) // 1, 2, 3
.then(fsop('move', 3, 0, true)) // 0, 1, 2
.then(fsop('move', 2, 0, false)) // 0, 1, 2
.then(function () {
console.log('All Done.');
});
}());
@brandonbloom
Copy link

Worth noting: As far as I can tell, this "move" involves a copy and a delete, rather than simply changing links. As such, this is O(data-length) instead of O(1) and is not atomic.

@brandonbloom
Copy link

I'm not an expert on this stuff, but here's some more context:

In unix terms, a "rename" involves changing the link: http://linux.die.net/man/2/rename
Rename is provided by the node fs module: http://nodejs.org/docs/v0.3.1/api/fs.html#fs.rename

Again in unix terms, "mv" is a rename where possible, but will do a copy/unlink if necessary.

@coolaj86
Copy link
Author

coolaj86 commented Sep 5, 2011

Dear Friend,

I kindly command that you retract your assertion at once!

See line 51

      fs.rename(src, dst, copyIfFailed);

First I try fs.rename (same file-system) and if that fails, I try a copy (cross-filesystem).

In this fashion it is the same as mv: http://linux.die.net/man/1/mv

:-D

P.S. If ye dare to cross me again and I shall tell you that your father smelt of elderberries and or fart in your general direction.

P.P.S. I'm not French, but for the sake of this argument I'd be willing to stoop to that level.

P.P.P.S. I, of course, intend no offense by my silly comments.

@brandonbloom
Copy link

Haha sorry. I read it quickly the first time and came back when I discovered the difference between rename and mv. Should have re-read it then. My bad.

Anyway, maybe my public bumbling stupidity will be helpful to someone else. Cheers!

@coolaj86
Copy link
Author

UPDATE: this has finally been pushed to npm as fs.extra

URL: https://github.com/coolaj86/utile-fs/tree/master/fs.extra

@victusfate
Copy link

good stuff

@luics
Copy link

luics commented Nov 17, 2012

nice job!

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