Browse files

Initial Commit

  • Loading branch information...
1 parent 60a9f68 commit a13874eb3cd1b0bb30ed5cb0d33dde4be124462f @piscis committed Apr 14, 2011
Showing with 332 additions and 0 deletions.
  1. +5 −0 .gitignore
  2. +8 −0 .npmignore
  3. +2 −0 History.md
  4. +22 −0 LICENSE
  5. +26 −0 Readme.md
  6. +7 −0 index.js
  7. +158 −0 lib/copy.js
  8. +19 −0 package.json
  9. +85 −0 test/copy.js
  10. 0 README → test/data/.gitignore
View
5 .gitignore
@@ -0,0 +1,5 @@
+.DS_Store
+.*.swp
+._*
+*.log
+node_modules
View
8 .npmignore
@@ -0,0 +1,8 @@
+.DS_Store
+.*.swp
+._*
+*.log
+.git*
+docs/
+test/
+testing.js
View
2 History.md
@@ -0,0 +1,2 @@
+# 0.0.1alpha1 / 2011-04-14
+* Initial release
View
22 LICENSE
@@ -0,0 +1,22 @@
+(The MIT License)
+
+Copyright (c) 2009-2011 Alexander Pirsig <apirsig@web.de>
+
+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.
View
26 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 <apirsig@web.de>
+
+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.
View
7 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;
View
158 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);
+};
View
19 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 <apirsig@web.de>",
+ "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" }
+}
View
85 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();
+
+ }
+
+});
View
0 README → test/data/.gitignore
File renamed without changes.

0 comments on commit a13874e

Please sign in to comment.