Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

fs.copy and fs.move for Node.JS

View fs.extra.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
/*
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);
});
};
}());
View fs.extra.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
/*
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.');
});
}());

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.

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.

Owner

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.

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!

Owner

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

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

good stuff

nice job!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.