Commit 59423dd3 authored by Benjamin Chelli's avatar Benjamin Chelli

Refactor writeFile to match nodejs File System native interface

parent a4d1cf37
...@@ -268,15 +268,21 @@ proto.readFile = function(filename, options, cb){ ...@@ -268,15 +268,21 @@ proto.readFile = function(filename, options, cb){
* - close the file * - close the file
* *
*/ */
proto.writeFile = function(src, dest, cb){ proto.writeFile = function(filename, data, options, cb){
if(typeof options == 'function'){
cb = options;
options = {};
}
var connection = this var connection = this
, file , file
, fileContent , fileContent = new Buffer(data)
, fileLength , fileLength = new bigint(8, fileContent.length)
; ;
function createFile(fileCreated){ function createFile(fileCreated){
SMB2Request('create', {path:dest}, connection, function(err, f){ SMB2Request('create', {path:filename}, connection, function(err, f){
if(err) cb && cb(err); if(err) cb && cb(err);
// SMB2 set file size // SMB2 set file size
else { else {
...@@ -296,23 +302,6 @@ proto.writeFile = function(src, dest, cb){ ...@@ -296,23 +302,6 @@ proto.writeFile = function(src, dest, cb){
}); });
} }
function openFile(fileOpened){
SMB2Request('open', {path:dest}, connection, function(err, f){
if(err) cb && cb(err);
// SMB2 read file content
else {
file = f;
fileOpened();
}
});
}
function openSrcFile(fileOpened){
fileContent = fs.readFileSync(src);
fileLength = new bigint(8, fileContent.length);
fileOpened();
}
function setFileSize(fileSizeSetted){ function setFileSize(fileSizeSetted){
SMB2Request('set_info', {FileId:file.FileId, FileInfoClass:'FileEndOfFileInformation', Buffer:fileLength.toBuffer()}, connection, function(err){ SMB2Request('set_info', {FileId:file.FileId, FileInfoClass:'FileEndOfFileInformation', Buffer:fileLength.toBuffer()}, connection, function(err){
if(err) cb && cb(err); if(err) cb && cb(err);
...@@ -366,18 +355,16 @@ proto.writeFile = function(src, dest, cb){ ...@@ -366,18 +355,16 @@ proto.writeFile = function(src, dest, cb){
checkDone(); checkDone();
} }
this.exists(dest, function(err, exists){ this.exists(filename, function(err, exists){
if(err) cb && cb(err); if(err) cb && cb(err);
else if(!exists){ else if(!exists){
createFile(function(){ createFile(function(){
openSrcFile(function(){ setFileSize(function(){
setFileSize(function(){ writeFile(function(){
writeFile(function(){ closeFile(cb);
closeFile(cb);
});
}); });
}); });
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment