fix: correct ls and ep file stat formats

master

master

master

master
This commit is contained in:
Tyler Stewart
2017-05-11 16:38:28 -06:00
parent cf71243729
commit c54045e0b9
4 changed files with 42 additions and 33 deletions

View File

@@ -2,17 +2,17 @@ const _ = require('lodash');
const moment = require('moment');
const errors = require('../errors');
const FORMATS = {
ls,
ep
};
module.exports = function (fileStat, format = 'ls') {
if (typeof format === 'function') return format(fileStat);
const formats = {
ls: ls,
ep: ep
};
if (!formats.hasOwnProperty(format)) {
if (!FORMATS.hasOwnProperty(format)) {
throw new errors.FileSystemError('Bad file stat formatter');
}
return formats[format](fileStat);
return FORMATS[format](fileStat);
};
function ls(fileStat) {
@@ -21,23 +21,21 @@ function ls(fileStat) {
const dateFormat = now.diff(mtime, 'months') < 6 ? 'MMM DD HH:mm' : 'MMM DD YYYY';
return [
fileStat.mode !== null
? [
fileStat.isDirectory() ? 'd' : '-',
400 & fileStat.mode ? 'r' : '-',
200 & fileStat.mode ? 'w' : '-',
100 & fileStat.mode ? 'x' : '-',
40 & fileStat.mode ? 'r' : '-',
20 & fileStat.mode ? 'w' : '-',
10 & fileStat.mode ? 'x' : '-',
4 & fileStat.mode ? 'r' : '-',
2 & fileStat.mode ? 'w' : '-',
1 & fileStat.mode ? 'x' : '-'
].join('')
: fileStat.isDirectory() ? 'drwxr-xr-x' : '-rwxr-xr-x',
fileStat.mode ? [
fileStat.isDirectory() ? 'd' : '-',
fileStat.mode & 256 ? 'r' : '-',
fileStat.mode & 128 ? 'w' : '-',
fileStat.mode & 64 ? 'x' : '-',
fileStat.mode & 32 ? 'r' : '-',
fileStat.mode & 16 ? 'w' : '-',
fileStat.mode & 8 ? 'x' : '-',
fileStat.mode & 4 ? 'r' : '-',
fileStat.mode & 2 ? 'w' : '-',
fileStat.mode & 1 ? 'x' : '-'
].join('') : fileStat.isDirectory() ? 'drwxr-xr-x' : '-rwxr-xr-x',
'1',
fileStat.uid,
fileStat.gid,
fileStat.uid || 1,
fileStat.gid || 1,
_.padStart(fileStat.size, 12),
_.padStart(mtime.format(dateFormat), 12),
fileStat.name
@@ -45,12 +43,12 @@ function ls(fileStat) {
}
function ep(fileStat) {
const facts = [
const facts = _.compact([
fileStat.dev && fileStat.ino ? `i${fileStat.dev.toString(16)}.${fileStat.ino.toString(16)}` : null,
fileStat.size ? `s${fileStat.size}` : null,
fileStat.mtime ? `m${moment.utc(new Date(fileStat.mtime)).format('X')}` : null,
fileStat.mode ? `up${fileStat.mode.toString(8).substr(fileStat.mode.toString(8).length - 3)}` : null,
fileStat.isDirectory() ? 'r' : '/'
].join(',');
fileStat.mode ? `up${(fileStat.mode & 4095).toString(8)}` : null,
fileStat.isDirectory() ? '/' : 'r'
]).join(',');
return `+${facts}\t${fileStat.name}`;
}

View File

@@ -8,7 +8,7 @@ describe('helpers // file-stat', function () {
name: 'test1',
dev: 2114,
ino: 48064969,
mode: 33188,
mode: 33279,
nlink: 1,
uid: 85,
gid: 100,
@@ -27,7 +27,7 @@ describe('helpers // file-stat', function () {
name: 'test2',
dev: 2114,
ino: 48064969,
mode: 33188,
mode: 33279,
nlink: 1,
uid: 84,
gid: 101,
@@ -45,19 +45,28 @@ describe('helpers // file-stat', function () {
describe('format - ls //', function () {
it('formats correctly', () => {
const format = fileStat(STAT, 'ls');
expect(format).to.equal('-rwxrw-r-- 1 85 100 527 Oct 10 23:24 test1');
expect(format).to.equal('-rwxrwxrwx 1 85 100 527 Oct 10 23:24 test1');
});
it('formats correctly for files over 6 months old', () => {
const format = fileStat(STAT_OLD, 'ls');
expect(format).to.equal('-rwxrw-r-- 1 84 101 530 Oct 10 2011 test2');
expect(format).to.equal('-rwxrwxrwx 1 84 101 530 Oct 10 2011 test2');
});
it('formats without some attributes', () => {
const format = fileStat({
name: 'missing stuff',
mtime: 'Mon, 10 Oct 2011 14:05:12 GMT',
isDirectory: () => true
}, 'ls');
expect(format).to.equal('drwxr-xr-x 1 1 1 Oct 10 2011 missing stuff');
});
});
describe('format - ep //', function () {
it('formats correctly', () => {
const format = fileStat(STAT, 'ep');
expect(format).to.equal('+i842.2dd69c9,s527,m1507677851,up644,/ test1');
expect(format).to.equal('+i842.2dd69c9,s527,m1507677851,up777,r test1');
});
});

View File

@@ -2,6 +2,8 @@ const {expect} = require('chai');
const resolveHost = require('../../src/helpers/resolve-host');
describe('helpers //resolve-host', function () {
this.timeout(4000);
it('fetches ip address', done => {
const hostname = '0.0.0.0';
resolveHost(hostname)

View File

@@ -14,7 +14,7 @@ const server = new FtpServer('ftp://127.0.0.1:8880', {
cert: `${process.cwd()}/test/cert/server.crt`,
ca: `${process.cwd()}/test/cert/server.csr`
},
anonymous: true
file_format: 'ep'
});
server.on('login', ({username, password}, resolve, reject) => {
if (username === 'test' && password === 'test' || username === 'anonymous') {