Skip to content

Commit 66f1090

Browse files
committed
feature(mode) move out permissions
1 parent 98ad760 commit 66f1090

4 files changed

Lines changed: 115 additions & 84 deletions

File tree

lib/format.js

Lines changed: 1 addition & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -48,85 +48,5 @@ module.exports.size = (size) => {
4848
return (size / l1PB).toFixed(2) + 'pb';
4949
};
5050

51-
/**
52-
* Функция переводит права из цыфрового вида в символьный
53-
* @param perms - строка с правами доступа
54-
* к файлу в 8-миричной системе
55-
*/
56-
module.exports.permissions = {};
57-
module.exports.permissions.symbolic = (perms) => {
58-
const is = typeof perms !== 'undefined';
59-
let permissions = '';
60-
/*
61-
S_IRUSR 0000400 protection: readable by owner
62-
S_IWUSR 0000200 writable by owner
63-
S_IXUSR 0000100 executable by owner
64-
S_IRGRP 0000040 readable by group
65-
S_IWGRP 0000020 writable by group
66-
S_IXGRP 0000010 executable by group
67-
S_IROTH 0000004 readable by all
68-
S_IWOTH 0000002 writable by all
69-
S_IXOTH 0000001 executable by all
70-
*/
71-
72-
if (!is)
73-
return permissions;
74-
75-
const permsStr = perms.slice(-3);
76-
77-
/* Переводим в двоичную систему */
78-
const owner = (permsStr[0] - 0).toString(2);
79-
const group = (permsStr[1] - 0).toString(2);
80-
const all = (permsStr[2] - 0).toString(2);
81-
82-
/* переводим в символьную систему*/
83-
permissions =
84-
(owner[0] - 0 > 0 ? 'r' : '-') +
85-
(owner[1] - 0 > 0 ? 'w' : '-') +
86-
(owner[2] - 0 > 0 ? 'x' : '-') +
87-
' ' +
88-
(group[0] - 0 > 0 ? 'r' : '-') +
89-
(group[1] - 0 > 0 ? 'w' : '-') +
90-
(group[2] - 0 > 0 ? 'x' : '-') +
91-
' ' +
92-
(all[0] - 0 > 0 ? 'r' : '-') +
93-
(all[1] - 0 > 0 ? 'w' : '-') +
94-
(all[2] - 0 > 0 ? 'x' : '-');
95-
96-
return permissions;
97-
};
98-
99-
/**
100-
* Функция конвертирует права доступа к файлам из символьного вида
101-
* в цыфровой
102-
*/
103-
module.exports.permissions.numeric = (perms) => {
104-
const length = perms && perms.length === 11;
105-
106-
if (!length)
107-
throw Error('permissions should be in format "xxx xxx xxx"');
108-
109-
const R = 4;
110-
const W = 2;
111-
const X = 1;
112-
const N = 0;
113-
114-
const owner =
115-
(perms[0] === 'r' ? R : N) +
116-
(perms[1] === 'w' ? W : N) +
117-
(perms[2] === 'x' ? X : N);
118-
119-
const group =
120-
(perms[4] === 'r' ? R : N) +
121-
(perms[5] === 'w' ? W : N) +
122-
(perms[6] === 'x' ? X : N);
123-
124-
const all =
125-
(perms[8] === 'r' ? R : N) +
126-
(perms[9] === 'w' ? W : N) +
127-
(perms[10] === 'x' ? X : N);
128-
129-
/* добавляем 2 цифры до 5 */
130-
return '00' + owner + group + all;
131-
};
51+
module.exports.permissions = require('./mode');
13252

lib/mode.js

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
'use strict';
2+
3+
const currify = require('currify/legacy');
4+
5+
/*
6+
S_IRUSR 0000400 protection: readable by owner
7+
S_IWUSR 0000200 writable by owner
8+
S_IXUSR 0000100 executable by owner
9+
S_IRGRP 0000040 readable by group
10+
S_IWGRP 0000020 writable by group
11+
S_IXGRP 0000010 executable by group
12+
S_IROTH 0000004 readable by all
13+
S_IWOTH 0000002 writable by all
14+
S_IXOTH 0000001 executable by all
15+
*/
16+
17+
const R = {
18+
name: 'r',
19+
value: 4,
20+
};
21+
22+
const W = {
23+
name: 'w',
24+
value: 2,
25+
};
26+
27+
const X = {
28+
name: 'x',
29+
value: 1,
30+
};
31+
32+
const getModeName = currify((value, m) => {
33+
if (value & m.value)
34+
return m.name;
35+
36+
return '-';
37+
});
38+
39+
const toStrMode = currify((fn, value) => {
40+
return [R, W, X]
41+
.map(fn(value))
42+
.join('');
43+
});
44+
45+
46+
/**
47+
* Функция переводит права из цыфрового вида в символьный
48+
* @param perms - строка с правами доступа
49+
* к файлу в 8-миричной системе
50+
*/
51+
module.exports.symbolic = (perms) => {
52+
let permissions = '';
53+
const is = typeof perms !== 'undefined';
54+
55+
if (!is)
56+
return permissions;
57+
58+
const permsStr = perms.slice(-3);
59+
60+
/* Переводим в двоичную систему */
61+
const owner = Number(permsStr[0]).toString(2);
62+
const group = Number(permsStr[1]).toString(2);
63+
const all = Number(permsStr[2]).toString(2);
64+
65+
const allPermissions = [
66+
owner,
67+
group,
68+
all,
69+
];
70+
71+
return allPermissions
72+
.map(toStrMode(getModeName))
73+
.join(' ');
74+
};
75+
76+
/**
77+
* Функция конвертирует права доступа к файлам из символьного вида
78+
* в цыфровой
79+
*/
80+
module.exports.numeric = (perms) => {
81+
const length = perms && perms.length === 11;
82+
83+
if (!length)
84+
throw Error('permissions should be in format "xxx xxx xxx"');
85+
86+
const R = 4;
87+
const W = 2;
88+
const X = 1;
89+
const N = 0;
90+
91+
const owner =
92+
(perms[0] === 'r' ? R : N) +
93+
(perms[1] === 'w' ? W : N) +
94+
(perms[2] === 'x' ? X : N);
95+
96+
const group =
97+
(perms[4] === 'r' ? R : N) +
98+
(perms[5] === 'w' ? W : N) +
99+
(perms[6] === 'x' ? X : N);
100+
101+
const all =
102+
(perms[8] === 'r' ? R : N) +
103+
(perms[9] === 'w' ? W : N) +
104+
(perms[10] === 'x' ? X : N);
105+
106+
/* добавляем 2 цифры до 5 */
107+
return '00' + owner + group + all;
108+
};
109+

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030
"watch:test": "npm run watcher -- npm test",
3131
"watch:lint": "npm run watcher -- 'npm run lint'"
3232
},
33-
"dependencies": {},
33+
"dependencies": {
34+
"currify": "^2.0.6"
35+
},
3436
"license": "MIT",
3537
"engines": {
3638
"node": ">=4"

test/format.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ test('format: size: pb', (t) => {
8383
});
8484

8585
test('format: permissions: symbolic', (t) => {
86-
const perm = (16895).toString(8);
86+
const perm = (16894).toString(8);
8787
const result = permissions.symbolic(perm);
88-
const expected = 'rwx rwx rwx';
88+
const expected = 'rwx rwx rw-';
8989

9090
t.equal(result, expected, 'should equal');
9191
t.end();

0 commit comments

Comments
 (0)