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){
* - 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
, file
, fileContent
, fileLength
, fileContent = new Buffer(data)
, fileLength = new bigint(8, fileContent.length)
;
function createFile(fileCreated){
SMB2Request('create', {path:dest}, connection, function(err, f){
SMB2Request('create', {path:filename}, connection, function(err, f){
if(err) cb && cb(err);
// SMB2 set file size
else {
......@@ -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){
SMB2Request('set_info', {FileId:file.FileId, FileInfoClass:'FileEndOfFileInformation', Buffer:fileLength.toBuffer()}, connection, function(err){
if(err) cb && cb(err);
......@@ -366,18 +355,16 @@ proto.writeFile = function(src, dest, cb){
checkDone();
}
this.exists(dest, function(err, exists){
this.exists(filename, function(err, exists){
if(err) cb && cb(err);
else if(!exists){
createFile(function(){
openSrcFile(function(){
setFileSize(function(){
writeFile(function(){
closeFile(cb);
});
setFileSize(function(){
writeFile(function(){
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