Commit 1796d6b7 authored by HamadaBrest's avatar HamadaBrest Committed by Julien Fontanet

feat: new truncate method (#43)

parent 2c08c9a7
...@@ -217,6 +217,23 @@ smb2Client.writeFile('path\\to\\my\\file.txt', 'Hello Node', function(err) { ...@@ -217,6 +217,23 @@ smb2Client.writeFile('path\\to\\my\\file.txt', 'Hello Node', function(err) {
}); });
``` ```
> `smb2Client.truncate ( filename, length, callback )`
- `filename` String
- `length` Number
- `callback` Function
Asynchronously truncate a file to a size of precisely length bytes.
Example:
```javascript
smb2Client.truncate('path\\to\\my\\file.txt', 10, function(err) {
if (err) throw err;
console.log("It's truncated!");
});
```
### Streams ### Streams
> `smb2Client.createReadStream ( fileName, [options], callback )` > `smb2Client.createReadStream ( fileName, [options], callback )`
......
# Releases # Releases
## 0.14.0
- smb2.truncate
## 0.4.0 ## 0.4.0
- smb2.ensureDir - smb2.ensureDir
......
var SMB2Forge = require('../tools/smb2-forge');
var SMB2Request = SMB2Forge.request;
var BigInt = require('../tools/bigint');
var FILE_WRITE_DATA = require('../structures/constants').FILE_WRITE_DATA;
function setFileSize(file, fileLength, connection, cb) {
SMB2Request(
'set_info',
{
FileId: file.FileId,
FileInfoClass: 'FileEndOfFileInformation',
Buffer: fileLength.toBuffer(),
},
connection,
cb
);
}
module.exports = function truncate(path, length, cb) {
var connection = this;
SMB2Request(
'open',
{ path: path, desiredAccess: FILE_WRITE_DATA },
connection,
function(err, file) {
if (err != null) {
return cb(err);
}
setFileSize(file, new BigInt(8, length), connection, function(err) {
SMB2Request('close', file, connection, function() {
cb(err);
});
});
}
);
};
...@@ -4,7 +4,6 @@ var message = require('../tools/message'); ...@@ -4,7 +4,6 @@ var message = require('../tools/message');
module.exports = message({ module.exports = message({
generate: function(connection, params) { generate: function(connection, params) {
var buffer = Buffer.from(params.path, 'ucs2'); var buffer = Buffer.from(params.path, 'ucs2');
return new SMB2Message({ return new SMB2Message({
headers: { headers: {
Command: 'CREATE', Command: 'CREATE',
...@@ -16,6 +15,7 @@ module.exports = message({ ...@@ -16,6 +15,7 @@ module.exports = message({
Buffer: buffer, Buffer: buffer,
NameOffset: 0x0078, NameOffset: 0x0078,
CreateContextsOffset: 0x007a + buffer.length, CreateContextsOffset: 0x007a + buffer.length,
DesiredAccess: params.desiredAccess,
}, },
}); });
}, },
......
...@@ -136,3 +136,6 @@ proto.write = autoPromise(require('./api/write'), function( ...@@ -136,3 +136,6 @@ proto.write = autoPromise(require('./api/write'), function(
}; };
}); });
proto.close = autoPromise(require('./api/close')); proto.close = autoPromise(require('./api/close'));
proto.truncate = autoPromise(
SMB2Connection.requireConnect(require('./api/truncate'))
);
...@@ -13,4 +13,10 @@ module.exports = { ...@@ -13,4 +13,10 @@ module.exports = {
// Where do they come from?! // Where do they come from?!
MAX_READ_LENGTH: 0x00010000, MAX_READ_LENGTH: 0x00010000,
MAX_WRITE_LENGTH: 0x00010000 - 0x71, MAX_WRITE_LENGTH: 0x00010000 - 0x71,
/**
* 2.2.13.1.1 SMB2 File_Pipe_Printer_Access_Mask
* https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-smb2/77b36d0f-6016-458a-a7a0-0f4a72ae1534
*/
FILE_WRITE_DATA: 0x00000002,
}; };
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