From a13874eb3cd1b0bb30ed5cb0d33dde4be124462f Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 14 Apr 2011 20:43:01 +0200 Subject: [PATCH] Initial Commit --- .gitignore | 5 ++ .npmignore | 8 ++ History.md | 2 + LICENSE | 22 +++++ Readme.md | 26 ++++++ index.js | 7 ++ lib/copy.js | 158 +++++++++++++++++++++++++++++++++ package.json | 19 ++++ test/copy.js | 85 ++++++++++++++++++ README => test/data/.gitignore | 0 10 files changed, 332 insertions(+) create mode 100644 .gitignore create mode 100644 .npmignore create mode 100644 History.md create mode 100644 LICENSE create mode 100644 Readme.md create mode 100644 index.js create mode 100644 lib/copy.js create mode 100644 package.json create mode 100644 test/copy.js rename README => test/data/.gitignore (100%) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..216611a --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.DS_Store +.*.swp +._* +*.log +node_modules diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..0d934c3 --- /dev/null +++ b/.npmignore @@ -0,0 +1,8 @@ +.DS_Store +.*.swp +._* +*.log +.git* +docs/ +test/ +testing.js diff --git a/History.md b/History.md new file mode 100644 index 0000000..52e857d --- /dev/null +++ b/History.md @@ -0,0 +1,2 @@ +# 0.0.1alpha1 / 2011-04-14 +* Initial release diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..763e141 --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ +(The MIT License) + +Copyright (c) 2009-2011 Alexander Pirsig + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +'Software'), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..61b44d9 --- /dev/null +++ b/Readme.md @@ -0,0 +1,26 @@ +# FSext +some small nodejs extentions to the node-"fs" module, that where usefull for me. + +## License +(The MIT License) + +Copyright (c) 2009-2011 Alexander Pirsig + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +'Software'), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/index.js b/index.js new file mode 100644 index 0000000..61529b9 --- /dev/null +++ b/index.js @@ -0,0 +1,7 @@ +var fs = require('fs'); + +// Extend Filesystem api +fs.copy = require(__dirname+'/lib/copy').copy; +fs.copySync = require(__dirname+'/lib/copy').copySync; + +exports = module.exports = fs; \ No newline at end of file diff --git a/lib/copy.js b/lib/copy.js new file mode 100644 index 0000000..78e7a90 --- /dev/null +++ b/lib/copy.js @@ -0,0 +1,158 @@ +var sys = require('sys'); +var events = require('events'); +var path = require('path'); + +var copy = function copy(src, dst, callback) { + var self = this; + + if(!callback) { + callback = function(){}; + } + + self.on('error', function(err) { + callback(err); + }); + + self.on('validations', function() { + + path.exists(src, function(exists) { + + if(!exists) { + + self.emit('error', new Error(src + ' does not exists. Nothing to be copied')); + return; + } + + fs.stat(src, function(err, stat) { + + if(err) { + + self.emit('error', err); + return; + } + + if(stat.isDirectory()) { + + self.emit('error', new Error(src + ' is a directory. It must be a file')); + return; + } + + if(src == dst) { + + self.emit('error', new Error(src + ' and ' + dst + 'are identical')); + return; + } + + self.emit('open_infd'); + }); + }); + }); + + self.on('open_infd', function() { + + fs.open(src, 'r', function(err, infd) { + + if(err) { + + self.emit('error', err); + return; + } + + self.emit('open_outfd', infd); + }); + + }); + + self.on('open_outfd', function(infd) { + + fs.open(dst, 'w', function(err, outfd) { + + if(err) { + + self.emit('error', err); + return; + } + + self.emit('sendfile', infd, outfd); + }); + }); + + self.on('sendfile', function(infd, outfd) { + + fs.fstat(infd, function(err, stat) { + + if(err) { + + self.emit('error', err); + return; + } + + fs.sendfile(outfd, infd, 0, stat.size, function() { + + self.emit('close_fds', infd, outfd); + callback(); + }); + }); + }); + + self.on('close_fds', function(infd, outfd) { + + fs.close(infd, function(err) { + + if(err) { + self.emit('error', err); + } + + }); + + fs.close(outfd, function(err) { + + if(err) { + + self.emit('error', err); + } + + }); + }); + + self.emit('validations'); +}; +sys.inherits(copy, events.EventEmitter); + +exports.copy = function(src, dst, callback) { + + return new copy(src, dst, callback); +}; + +exports.copySync = function copySync(src, dst) { + + if(typeof dst == 'undefined') { + + throw new Error('Destination is not defined'); + } + + if(!path.existsSync(src)) { + + throw new Error(src + ' does not exists.'); + } + + if(fs.statSync(src).isDirectory()) { + + throw new Error(src + ' is a directory. It must be a file'); + } + + if(src == dst) { + + throw new Error(src + ' and ' + dst + 'are identical'); + } + + var infd = fs.openSync(src, 'r'); + var size = fs.fstatSync(infd).size; + var outfd = fs.openSync(dst, 'w'); + + + fs.sendfileSync(outfd, infd, 0, size); + + fs.close(infd); + fs.close(outfd); +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..caf26e4 --- /dev/null +++ b/package.json @@ -0,0 +1,19 @@ +{ + "name": "fsext", + "description": "'fsext' is a small extention for the nodejs FS module", + "version": "0.0.1alpha1", + "author": "Alexander Pirsig ", + "contributors": [], + "dependencies": { + }, + "repositories": [ + { + "type": "git", + "url": "git://github.com/piscis/fsext.git" + } + ], + "keywords": ["fs", "file", "copy", "duplicating", "filesystem", "fs extention", "fsext"], + "main": "index", + "directories" : { "lib" : "./lib" }, + "engines": { "node": ">= 0.3.5 < 0.5.0" } +} diff --git a/test/copy.js b/test/copy.js new file mode 100644 index 0000000..96f88b3 --- /dev/null +++ b/test/copy.js @@ -0,0 +1,85 @@ +var testCase = require('nodeunit').testCase; + +module.exports = testCase({ + + setUp: function (callback) { + + // setup test + var filesys = require('fs'); + this.fs = require(__dirname+'/../index'); + this.src = __dirname+'/data/stub.txt'; + this.dst = __dirname+'/data/stub_copy.txt'; + + var that = this; + + filesys.writeFile(that.src, 'CaWaBuNgA', function (err) { + if (err) { + throw err; + } + callback(); + }); + + }, + + tearDown: function (callback) { + + // clean up + var fs = require('fs'); + var that = this; + + fs.unlink(that.src, function (err) { + if (err) { + throw err; + } + + fs.unlink(that.dst, function (err) { + if (err) { + throw err; + } + callback(); + }); + }); + + }, + + testCopy: function (test) { + + var test = test; + test.expect(1); + + var src = this.src, + dst = this.dst + fs = this.fs; + + fs.copy(src,dst,function(err) { + + if(typeof err == 'undefined') { + + fs.readFile(dst,function(err,data) { + test.ok(data.toString()=='CaWaBuNgA'); + test.done(); + }); + + } else { + test.fail('There was an error: '+err); + test.done(); + } + }); + + }, + + testCopySync: function(test) { + + test.expect(1); + + var src = this.src, + dst = this.dst + fs = this.fs; + + fs.copySync(src,dst); + test.ok(fs.readFileSync(dst).toString()=='CaWaBuNgA', 'Content of src and dst file not the same'); + test.done(); + + } + +}); diff --git a/README b/test/data/.gitignore similarity index 100% rename from README rename to test/data/.gitignore