Permalink
Browse files

Make fsutil.cp and fsutil.cp_r synchronous.

And put a postfix '_async' at the end of asyncronous function's name.
  • Loading branch information...
1 parent 436c4b0 commit 9775fadbe7e55f974305723ce6eb984db12eaf6b @npcode committed Apr 8, 2012
Showing with 91 additions and 7 deletions.
  1. +41 −4 fsutil.js
  2. +50 −3 test/fsutil.test.js
View
@@ -51,13 +51,48 @@ var fwrite_p = function(path, data) {
return fs.writeFileSync(path, data);
}
-var cp = function(src, dst, callback) {
+var cp_async = function(src, dst, callback) {
var is = fs.createReadStream(src);
var os = fs.createWriteStream(dst);
util.pump(is, os, callback);
}
-var cp_r = function(src, dst, callback) {
+var cp = function(src, dst) {
+ var block_size = 4096;
+ var buf = new Buffer(block_size);
+ var fd_src = fs.openSync(src, 'r');
+ var fd_dst = fs.openSync(dst, 'w');
+ var offset = 0;
+ var remain = fs.statSync(src).size;
+ var read_size = 0;
+
+ while (remain) {
+ read_size = remain < block_size ? remain : block_size;
+ fs.readSync(fd_src, buf, offset, read_size);
+ fs.writeSync(fd_dst, buf, offset, read_size);
+ remain -= read_size;
+ offset += read_size;
+ }
+
+ fs.closeSync(fd_src);
+ fs.closeSync(fd_dst);
+}
+
+var cp_r = function(src, dst) {
+ var self = this;
+
+ if (fs.statSync(src).isDirectory()) {
+ fs.mkdirSync(dst);
+ var files = fs.readdirSync(src);
+ files.forEach(function (filename) {
+ self.cp_r(pth.join(src, filename), pth.join(dst, filename));
+ });
+ } else {
+ cp(src, dst);
+ }
+}
+
+var cp_r_async = function(src, dst, callback) {
var self = this;
if (fs.statSync(src).isDirectory()) {
@@ -75,10 +110,10 @@ var cp_r = function(src, dst, callback) {
}
}
files.forEach(function (filename) {
- self.cp_r(pth.join(src, filename), pth.join(dst, filename), cb);
+ self.cp_r_async(pth.join(src, filename), pth.join(dst, filename), cb);
});
} else {
- cp(src, dst, callback);
+ cp_async(src, dst, callback);
}
}
@@ -115,7 +150,9 @@ exports.rm_rf = rm_rf;
exports.mkdir_p = mkdir_p;
exports.fwrite_p = fwrite_p;
exports.cp = cp;
+exports.cp_async = cp_async;
exports.cp_r = cp_r;
+exports.cp_r_async = cp_r_async;
exports.ln_s = fs.symlinkSync;
exports.ln_sf = ln_sf;
exports.cd = process.chdir;
View
@@ -115,9 +115,56 @@ suite('fsutil.cp', function() {
done();
});
+ test('Copy src to dest', function(done) {
+ fs.writeFileSync('a', 'hello');
+ fsutil.cp('a', 'b');
+ assert.equal(fs.readFileSync('b').toString(), 'hello');
+ done();
+ });
+
+ teardown(function(done) {
+ fsutil.rm_rf('a');
+ fsutil.rm_rf('b');
+ done();
+ });
+});
+
+suite('fsutil.cp_r', function() {
+ setup(function(done) {
+ fsutil.rm_rf('a');
+ fsutil.rm_rf('b');
+ done();
+ });
+
+ test('Copy src to dest recursively.', function(done) {
+ fs.mkdirSync('a');
+ fs.mkdirSync('a/b');
+ fs.mkdirSync('a/c');
+ fs.writeFileSync('a/d', 'hello');
+
+ fsutil.cp_r('a', 'b');
+ assert.equal(fs.readFileSync('b/d'), 'hello');
+ done();
+ });
+
+ teardown(function(done) {
+ fsutil.rm_rf('a');
+ fsutil.rm_rf('b');
+ done();
+ });
+});
+
+
+suite('fsutil.cp_async', function() {
+ setup(function(done) {
+ fsutil.rm_rf('a');
+ fsutil.rm_rf('b');
+ done();
+ });
+
test('Copy a file content src to dest.', function(done) {
fs.writeFileSync('a', 'hello');
- fsutil.cp('a', 'b', function() {
+ fsutil.cp_async('a', 'b', function() {
assert.equal(fs.readFileSync('b'), 'hello');
done();
});
@@ -130,7 +177,7 @@ suite('fsutil.cp', function() {
});
});
-suite('fsutil.cp_r', function() {
+suite('fsutil.cp_r_async', function() {
setup(function(done) {
fsutil.rm_rf('a');
fsutil.rm_rf('b');
@@ -143,7 +190,7 @@ suite('fsutil.cp_r', function() {
fs.mkdirSync('a/c');
fs.writeFileSync('a/d', 'hello');
- fsutil.cp_r('a', 'b', function() {
+ fsutil.cp_r_async('a', 'b', function() {
assert.equal(fs.readFileSync('b/d'), 'hello');
done();
});

0 comments on commit 9775fad

Please sign in to comment.