Commit 9bd5849b5df436a34e10b8b4399786e778c10d55
1 parent
6e7dd7b9
Amelioré PolyUserServ
Le line-reader bugguait lors de lectures en parallèles. La nouvelle méthode utilise plus de mémoire, mais fonctionne dans les cas les plus extrêmes.
Showing
2 changed files
with
52 additions
and
71 deletions
Show diff stats
app/services/PolyUserServ.js
1 | -var LineTransform = require('node-line-reader').LineTransform; | ||
2 | var fs = require('fs'); | 1 | var fs = require('fs'); |
2 | +var async = require('async'); | ||
3 | var Client = require('ssh2').Client; | 3 | var Client = require('ssh2').Client; |
4 | var creds = require('../../config/sshAuth'); | 4 | var creds = require('../../config/sshAuth'); |
5 | var NodeCache = require("node-cache"); | 5 | var NodeCache = require("node-cache"); |
@@ -12,17 +12,18 @@ var PolyUserServ = {}; | @@ -12,17 +12,18 @@ var PolyUserServ = {}; | ||
12 | 12 | ||
13 | PolyUserServ.readPasswd = function (login, cb) { | 13 | PolyUserServ.readPasswd = function (login, cb) { |
14 | passwdF = 'config/passwd'; | 14 | passwdF = 'config/passwd'; |
15 | - fs.exists(passwdF, function (exists) { | ||
16 | - found = false; | ||
17 | - if (exists) { | ||
18 | - stream = fs.createReadStream(passwdF); | ||
19 | - transform = new LineTransform(); | ||
20 | - stream.pipe(transform); | ||
21 | - transform.on('data', function (line) { | 15 | + fs.readFile(passwdF, function (err, file) { |
16 | + if (err) { | ||
17 | + cb(err); | ||
18 | + } else { | ||
19 | + lines = file.toString('utf8').split('\n'); | ||
20 | + async.detect(lines, function (line, cba) { | ||
22 | ex = line.split(':'); | 21 | ex = line.split(':'); |
23 | - if (ex[0] == login) { // Si trouvé | ||
24 | - stream.close(); | ||
25 | - cb({ | 22 | + cba(ex[0] == login); |
23 | + }, function (res) { | ||
24 | + if (res) { | ||
25 | + ex = res.split(':'); | ||
26 | + cb(null, { | ||
26 | 'username': ex[0], | 27 | 'username': ex[0], |
27 | 'password': ex[1], | 28 | 'password': ex[1], |
28 | 'UID': ex[2], | 29 | 'UID': ex[2], |
@@ -31,94 +32,75 @@ PolyUserServ.readPasswd = function (login, cb) { | @@ -31,94 +32,75 @@ PolyUserServ.readPasswd = function (login, cb) { | ||
31 | 'home': ex[5], | 32 | 'home': ex[5], |
32 | 'shell': ex[6] | 33 | 'shell': ex[6] |
33 | }); | 34 | }); |
34 | - found = true; | ||
35 | - } | ||
36 | - }); | ||
37 | - transform.on('end', function () { | ||
38 | - if (!found) { | ||
39 | - cb(false); | 35 | + } else { |
36 | + cb(null, null); | ||
40 | } | 37 | } |
41 | }); | 38 | }); |
42 | - } else { | ||
43 | - cb(undefined); | ||
44 | } | 39 | } |
45 | }); | 40 | }); |
46 | }; | 41 | }; |
47 | 42 | ||
48 | PolyUserServ.readGroup = function (gid, cb) { | 43 | PolyUserServ.readGroup = function (gid, cb) { |
49 | groupF = 'config/group'; | 44 | groupF = 'config/group'; |
50 | - fs.exists(groupF, function (exists) { | ||
51 | - found = false; | ||
52 | - if (exists) { | ||
53 | - stream = fs.createReadStream(groupF); | ||
54 | - transform = new LineTransform(); | ||
55 | - stream.pipe(transform); | ||
56 | - transform.on('data', function (line) { | 45 | + fs.readFile(groupF, function (err, file) { |
46 | + if (err) { | ||
47 | + cb(err); | ||
48 | + } else { | ||
49 | + lines = file.toString('utf8').split('\n'); | ||
50 | + async.detect(lines, function (line, cba) { | ||
57 | ex = line.split(':'); | 51 | ex = line.split(':'); |
58 | - if (ex[2] == gid) { // Si trouvé | ||
59 | - stream.close(); | ||
60 | - cb({ | 52 | + cba(ex[2] == gid); |
53 | + }, function (res) { | ||
54 | + if (res) { | ||
55 | + ex = res.split(':'); | ||
56 | + cb(null, { | ||
61 | 'name': ex[0], | 57 | 'name': ex[0], |
62 | 'password': ex[1], | 58 | 'password': ex[1], |
63 | 'GID': ex[2], | 59 | 'GID': ex[2], |
64 | 'list': ex[3] | 60 | 'list': ex[3] |
65 | }); | 61 | }); |
66 | - found = true; | ||
67 | - } | ||
68 | - }); | ||
69 | - transform.on('end', function () { | ||
70 | - if (!found) { | ||
71 | - cb(false); | 62 | + } else { |
63 | + cb(null, null); | ||
72 | } | 64 | } |
73 | }); | 65 | }); |
74 | - } else { | ||
75 | - cb(undefined); | ||
76 | } | 66 | } |
77 | }); | 67 | }); |
78 | }; | 68 | }; |
79 | 69 | ||
80 | PolyUserServ.grabInfos = function (login, cb) { | 70 | PolyUserServ.grabInfos = function (login, cb) { |
81 | - PolyUserServ.readPasswd(login, function (passwd) { | ||
82 | - if (passwd) { | ||
83 | - PolyUserServ.readGroup(passwd.GID, function (group) { | ||
84 | - if (group) { | ||
85 | - cb({ | ||
86 | - nom: passwd.GECOS, | ||
87 | - section: group.name.toUpperCase() | ||
88 | - }); | ||
89 | - } else { | ||
90 | - if (group === undefined) { | ||
91 | - console.error("Impossible d'ouvrir le fichier des groupes."); | ||
92 | - } else { | ||
93 | - console.error("Impossible d'obtenir le groupe de " + passwd.GID + "."); | ||
94 | - } | ||
95 | - cb({ | ||
96 | - nom: passwd.GECOS, | ||
97 | - section: passwd.GID | ||
98 | - }); | ||
99 | - } | ||
100 | - }); | ||
101 | - } else { | ||
102 | - if (passwd === undefined) { | ||
103 | - console.error("Impossible d'ouvrir le fichier des noms."); | 71 | + async.waterfall([ |
72 | + function (cba) { | ||
73 | + PolyUserServ.readPasswd(login, cba); | ||
74 | + }, | ||
75 | + function (passwd, cba) { | ||
76 | + if (passwd && passwd.GID) { | ||
77 | + PolyUserServ.readGroup(passwd.GID, function (err, group) { | ||
78 | + cba(err, passwd, group); | ||
79 | + }); | ||
104 | } else { | 80 | } else { |
105 | - console.error("Impossible d'obtenir le nom de " + login + "."); | ||
106 | - } | ||
107 | - if (!login) { | ||
108 | - login = 'Inconnu'; | 81 | + cba(null, passwd, null); |
109 | } | 82 | } |
110 | - cb({ | ||
111 | - nom: login.toUpperCase(), | ||
112 | - section: 'Inconnue' | 83 | + } |
84 | + ], function (err, passwd, group) { | ||
85 | + if (err) { | ||
86 | + cb(err); | ||
87 | + } else { | ||
88 | + cb(null, { | ||
89 | + nom: (passwd && passwd.GECOS) ? passwd.GECOS : login.toUpperCase(), | ||
90 | + section: (group && group.name) ? group.name.toUpperCase() : ((passwd && passwd.GID) ? passwd.GID : 'Inconnu') | ||
113 | }); | 91 | }); |
114 | } | 92 | } |
115 | }); | 93 | }); |
116 | }; | 94 | }; |
117 | 95 | ||
118 | PolyUserServ.add = function (login, cb) { | 96 | PolyUserServ.add = function (login, cb) { |
119 | - PolyUserServ.grabInfos(login, function (data) { | ||
120 | - cb(null, data); | ||
121 | - cache.set(login, data); | 97 | + PolyUserServ.grabInfos(login, function (err, data) { |
98 | + if (err) { | ||
99 | + cb(err); | ||
100 | + } else { | ||
101 | + cb(null, data); | ||
102 | + cache.set(login, data); | ||
103 | + } | ||
122 | }); | 104 | }); |
123 | }; | 105 | }; |
124 | 106 |
package.json
@@ -12,7 +12,6 @@ | @@ -12,7 +12,6 @@ | ||
12 | "express-session": "^1.11.1", | 12 | "express-session": "^1.11.1", |
13 | "mongoose": "^4.0.2", | 13 | "mongoose": "^4.0.2", |
14 | "node-cache": "^2.1.1", | 14 | "node-cache": "^2.1.1", |
15 | - "node-line-reader": "0.0.2", | ||
16 | "serve-favicon": "^2.2.0", | 15 | "serve-favicon": "^2.2.0", |
17 | "ssh2": "^0.4.6", | 16 | "ssh2": "^0.4.6", |
18 | "ursa": "^0.8.4" | 17 | "ursa": "^0.8.4" |