-
Notifications
You must be signed in to change notification settings - Fork 15
/
fileops.js
64 lines (54 loc) · 1.28 KB
/
fileops.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
var fs = require('fs');
var path = require('path');
var constants = require('./constants');
var sizes = constants.sizes;
var dataFileTstamps = exports.dataFileTstamps = function(dirname, cb) {
fs.readdir(dirname, function(err, files) {
if (err) {
cb(err);
return;
}
var tstamps = [];
files.forEach(function(file) {
var match = file.match(/^([0-9]+).medea.data/);
if (match && match.length && match[1]) {
tstamps.push(Number(match[1]));
}
});
cb(null, tstamps);
});
};
exports.mostRecentTstamp = function(dirname, cb) {
dataFileTstamps(dirname, function(err, stamps) {
if (err) {
cb(err);
return;
}
if (stamps.length) {
cb(null, stamps.sort(function(a, b) {
if (a > b) return - 1;
if (a < b) return 1;
return 0;
})[0]);
} else {
cb(null, 0);
}
});
};
exports.listDataFiles = function(dirname, cb) {
dataFileTstamps(dirname, function(err, tstamps) {
if (err) {
cb(err);
return;
}
var sorted = tstamps.sort(function(a, b) {
if (a > b) return -1;
if (a < b) return 1;
return 0;
});
var ret = sorted.map(function(t) {
return path.join(dirname, t + '.medea.data');
});
cb(null, ret);
});
};