Skip to content
This repository has been archived by the owner on Dec 21, 2020. It is now read-only.

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
piscis committed Apr 14, 2011
1 parent 60a9f68 commit a13874e
Show file tree
Hide file tree
Showing 10 changed files with 332 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
@@ -0,0 +1,5 @@
.DS_Store
.*.swp
._*
*.log
node_modules
8 changes: 8 additions & 0 deletions .npmignore
@@ -0,0 +1,8 @@
.DS_Store
.*.swp
._*
*.log
.git*
docs/
test/
testing.js
2 changes: 2 additions & 0 deletions History.md
@@ -0,0 +1,2 @@
# 0.0.1alpha1 / 2011-04-14
* Initial release
22 changes: 22 additions & 0 deletions 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.
26 changes: 26 additions & 0 deletions 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.
7 changes: 7 additions & 0 deletions 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;
158 changes: 158 additions & 0 deletions 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);
};
19 changes: 19 additions & 0 deletions 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" }
}
85 changes: 85 additions & 0 deletions 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();

}

});
File renamed without changes.

0 comments on commit a13874e

Please sign in to comment.