Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
node_modules
test.js
.vscode
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,15 @@ smb2Client.exists('path\\to\\my\\file.txt', function (err, exists) {
});
```

### smb2Client.stat ( path, callback )
Returns basic info about a file. Example:
```javascript
smb2Client.stat('path\\to\\my\\file.txt', function (err, stats) {
if (err) throw err;
console.log(`The size of a file is: ${stats.size} byte${stats.size === 1 ? '': 's'}.`);
});
```

### smb2Client.unlink ( path, callback )
Asynchronous unlink(2). No arguments other than a possible exception are given to the completion callback.
```javascript
Expand Down
1 change: 0 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
## New functions
- fs.appendFile(filename, data, [options], callback)
- fs.chmod(path, mode, callback)
- fs.stat(path, callback)
- fs.watchFile(filename, [options], listener)
- fs.unwatchFile(filename, [listener])
- fs.watch(filename, [options], [listener])
Expand Down
72 changes: 72 additions & 0 deletions lib/api/stat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const FileTime = require('win32filetime');
const SMB2Forge = require('../tools/smb2-forge');
const SMB2Request = SMB2Forge.request;

/*
* stat
* ======
*
* return basic info about a file
*
* - try to open the file
*
* - process the file info
*
* - close the file
*
* returns stats in callback in similar format like fs.stat:
* {
* size: number;
* atimeMs: number;
* mtimeMs: number;
* ctimeMs: number;
* birthtimeMs: number;
* atime: Date;
* mtime: Date;
* ctime: Date;
* birthtime: Date;
* }
*/
module.exports = function (path, cb) {
if (!cb || typeof cb !== 'function') {
throw new Error('Callback must be a function');
}

var connection = this;

SMB2Request('open', { path }, connection, function (err, fileInfo) {
if (err) cb(err);
else {
SMB2Request('close', fileInfo, connection, function (err) {
if (err) cb(err);
cb(null, translateResponse(fileInfo));
});
}
});

}

function translateResponse(fileInfo) {
const atimeMs = convertToUnixTime(fileInfo['LastAccessTime']);
const mtimeMs = convertToUnixTime(fileInfo['LastWriteTime']);
const ctimeMs = convertToUnixTime(fileInfo['ChangeTime']);
const birthtimeMs = convertToUnixTime(fileInfo['CreationTime']);
return {
size: fileInfo['EndofFile'].readUInt32LE(),

atimeMs,
mtimeMs,
ctimeMs,
birthtimeMs,
atime: new Date(atimeMs),
mtime: new Date(mtimeMs),
ctime: new Date(ctimeMs),
birthtime: new Date(birthtimeMs)
}
}

function convertToUnixTime(buffer) {
const low = buffer.readUInt32LE(0);
const high = buffer.readUInt32LE(4);
return FileTime.toUnix({ low, high });
}
1 change: 1 addition & 0 deletions lib/smb2.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ var proto = SMB.prototype = {};
proto.close = require('./api/close');

proto.exists = SMB2Connection.requireConnect(require('./api/exists'));
proto.stat = SMB2Connection.requireConnect(require('./api/stat'));

proto.rename = SMB2Connection.requireConnect(require('./api/rename'));

Expand Down
4 changes: 2 additions & 2 deletions lib/tools/smb2-forge.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ SMB2Forge.response = function(c){
message.parseBuffer(r);
//debug
if(c.debug){
console.log('--response');
console.log('--response', message.headers['Command']);
console.log(r.toString('hex'));
}
// get the message id
Expand Down Expand Up @@ -92,7 +92,7 @@ function sendNetBiosMessage(connection, message) {
var smbRequest = message.getBuffer(connection);

if(connection.debug){
console.log('--request');
console.log('--request', message.headers['Command']);
console.log(smbRequest.toString('hex'));
}

Expand Down
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"url": "https://github.com/bchelli/node-smb2"
},
"dependencies": {
"ntlm": "^0.1.3"
"ntlm": "^0.1.3",
"win32filetime": "^1.0.2"
},
"keywords": [
"SMB",
Expand Down