Commit f76c541b authored by Julien Fontanet's avatar Julien Fontanet

feat(rename): option to replace existing target

parent 36ec5866
......@@ -154,7 +154,7 @@ smb2Client.readFile('path\\to\\my\\file.txt', function(err, content) {
If no encoding is specified, then the raw buffer is returned.
> `smb2Client.rename ( oldPath, newPath, callback )`
> `smb2Client.rename ( oldPath, newPath, [ options, ] callback )`
Asynchronous `rename(2)`: rename a file.
......@@ -169,6 +169,22 @@ smb2Client.rename(
);
```
Existing files are not replaced by default, you need to pass the `replace: true` option for this use case:
```javascript
smb2Client.rename(
'path\\to\\my\\file.txt',
'path\\to\\existing\\file.txt',
{
replace: true
}
function(err) {
if (err) throw err;
console.log('file has been renamed');
}
);
```
> `smb2Client.rmdir ( path, callback )`
Asynchronous `rmdir(2)`: delete an empty directory.
......
......@@ -15,7 +15,12 @@ var FILE_OPEN_IF = require('../structures/constants').FILE_OPEN_IF;
* - close the file
*
*/
module.exports = function rename(oldPath, newPath, cb) {
module.exports = function rename(oldPath, newPath, options, cb) {
if (typeof options == 'function') {
cb = options;
options = {};
}
var connection = this;
// SMB2 open the folder / file
......@@ -30,21 +35,21 @@ module.exports = function rename(oldPath, newPath, cb) {
connection,
function(err, file) {
if (err) cb && cb(err);
else rename_(connection, file, newPath, cb);
else rename_(connection, file, newPath, options, cb);
}
);
else rename_(connection, file, newPath, cb);
else rename_(connection, file, newPath, options, cb);
});
};
function rename_(connection, file, newPath, cb) {
function rename_(connection, file, newPath, options, cb) {
// SMB2 rename
SMB2Request(
'set_info',
{
FileId: file.FileId,
FileInfoClass: 'FileRenameInformation',
Buffer: renameBuffer(newPath),
Buffer: renameBuffer(newPath, options),
},
connection,
function(err) {
......@@ -58,12 +63,12 @@ function rename_(connection, file, newPath, cb) {
);
}
function renameBuffer(newPath) {
function renameBuffer(newPath, options) {
var filename = Buffer.from(newPath, 'ucs2');
return Buffer.concat([
// ReplaceIfExists 1 byte
new bigint(1, 0).toBuffer(),
new bigint(1, options.replace ? 1 : 0).toBuffer(),
// Reserved 7 bytes
new bigint(7, 0).toBuffer(),
......
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