Commit 01e7a6d3 authored by Benjamin Chelli's avatar Benjamin Chelli

Add rename support

parent 410527ef
......@@ -128,6 +128,15 @@ smb2Client.unlink('path\\to\\my\\file.txt', function (err) {
});
```
### smb2Client.rename ( oldPath, newPath, callback )
Asynchronous rename(2). No arguments other than a possible exception are given to the completion callback.
```javascript
smb2Client.rename('path\\to\\my\\file.txt', 'new\\path\\to\\my\\new-file-name.txt', function (err) {
if (err) throw err;
console.log("file has been renamed");
});
```
### smb2Client.close ( )
This function will close the open connection if opened, it will be called automatically after ```autoCloseTimeout``` ms of no SMB2 call on the server.
......
# Releases
## 0.2.6
- fs.rename(oldPath, newPath, callback)
## 0.2.5
- BUG FIX: Make MessageId, ProcessId, SessionId unique at the connection level
......
......@@ -2,7 +2,6 @@
## New functions
- fs.appendFile(filename, data, [options], callback)
- fs.rename(oldPath, newPath, callback)
- fs.chmod(path, mode, callback)
- fs.stat(path, callback)
- fs.watchFile(filename, [options], listener)
......
var bigint = require('../tools/bigint')
, SMB2Forge = require('../tools/smb2-forge')
, SMB2Request = SMB2Forge.request
;
/*
* rename
* ======
*
* change the name of a file:
*
* - open the file
*
* - change file name
*
* - close the file
*
*/
module.exports = function(oldPath, newPath, cb){
var connection = this;
// SMB2 open the folder / file
SMB2Request('open_folder', {path:oldPath}, connection, function(err, file){
if(err) SMB2Request('create', {path:oldPath}, connection, function(err, file){
if(err) cb && cb(err);
else rename(connection, file, newPath, cb);
});
else rename(connection, file, newPath, cb);
});
}
function rename (connection, file, newPath, cb) {
// SMB2 rename
SMB2Request('set_info', {FileId:file.FileId, FileInfoClass:'FileRenameInformation',Buffer:renameBuffer(newPath)}, connection, function(err){
if(err) cb && cb(err);
// SMB2 close file
else SMB2Request('close', file, connection, function(err){
cb && cb(null);
});
});
}
function renameBuffer (newPath) {
var filename = new Buffer(newPath, 'ucs2');
return Buffer.concat([
// ReplaceIfExists 1 byte
(new bigint(1,0)).toBuffer()
// Reserved 7 bytes
, (new bigint(7,0)).toBuffer()
// RootDirectory 8 bytes
, (new bigint(8,0)).toBuffer()
// FileNameLength 4 bytes
, (new bigint(4,filename.length)).toBuffer()
// FileName
, filename
]);
}
\ No newline at end of file
......@@ -85,6 +85,8 @@ proto.close = require('./api/close');
proto.exists = SMB2Connection.requireConnect(require('./api/exists'));
proto.rename = SMB2Connection.requireConnect(require('./api/rename'));
proto.readFile = SMB2Connection.requireConnect(require('./api/readfile'));
proto.writeFile = SMB2Connection.requireConnect(require('./api/writefile'));
proto.unlink = SMB2Connection.requireConnect(require('./api/unlink'));
......
......@@ -4,7 +4,7 @@
, "description":"SMB2 Client"
, "homepage": "https://github.com/bchelli/node-smb2"
, "version":"0.2.5"
, "version":"0.2.6"
, "engines": [
"node"
......
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