File size: 4,629 Bytes
19605ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
var fs = require('fs');
var async = require('async');
var path = require('path');

var _DEBUG = false;


function NumberedFileOps(logpath, totalFiles, totalSize, gzip) {
    function getNumberedFile(n, zipped) {

        var parsed = path.parse(logpath);

        var result = '';
        if (n === 0) {
            result = parsed.name
                     .replace('.%N', '')
                     .replace('_%N', '')
                     .replace('-%N', '')
                     .replace('%N', '')
                     + parsed.ext;
        } else if (parsed.name.indexOf('%N') >= 0) {
            result = parsed.name.replace('%N', String(n)) + parsed.ext;
        } else {
            result = parsed.base + '.' + String(n);
        }

        if (zipped) {
            result += '.gz';
        }

        return path.join(parsed.dir, result);
    }

    function recursiveFindFilesToDelete(
        currentFileNumber, cumulativeSize, foundFiles, next
    ) {
        var fileToFind = getNumberedFile(currentFileNumber, gzip);
        fs.stat(fileToFind, function (err, stat) {
            if (err && err.code === 'ENOENT') {
                next(null, foundFiles);
            } else if (err) {
                next(err);
            } else {
                cumulativeSize += stat.size;

                if ((totalSize > 0 && cumulativeSize > totalSize) ||
                    (totalFiles > 0 && currentFileNumber > totalFiles)
                ) {
                    foundFiles.push(fileToFind);
                }
                recursiveFindFilesToDelete(
                    currentFileNumber + 1,
                    cumulativeSize,
                    foundFiles,
                    next
                );
            }
        });
    }

    function findFilesToDelete(next) {
        var foundFiles = [];

        recursiveFindFilesToDelete(1, 0, foundFiles, next);
    }

    function deleteFiles(next) {
        findFilesToDelete(function (err, foundFiles) {
            if (err) {
                next(err);
            } else {
                async.eachSeries(
                    foundFiles,
                    function (toDel, callback) {
                        if (_DEBUG) console.log('rm %s', toDel);

                        fs.unlink(toDel, ignoreFileNotFound(callback));
                    },
                    next
                );
            }
        });
    };

    function ignoreFileNotFound(next) {
        return function (err) {
            if (!err || err.code === 'ENOENT') {
                next();
            } else {
                next(err);
            }
        };
    }

    function recursiveFindFilesToMove(currentFileNumber, foundFiles, next) {
        var fileToFind = getNumberedFile(currentFileNumber, gzip);
        fs.stat(fileToFind, function (err, stat) {
            if (err && err.code === 'ENOENT') {
                next(null, foundFiles);
            } else if (err) {
                next(err);
            } else {
                foundFiles.unshift(currentFileNumber);
                recursiveFindFilesToMove(
                    currentFileNumber + 1,
                    foundFiles,
                    next
                );
            }
        });
    }

    function moveFile(numberToMove, next) {
        var before = getNumberedFile(numberToMove, gzip);
        var after = getNumberedFile(numberToMove+1, gzip);
        fs.rename(before, after, ignoreFileNotFound(next));
    }

    function moveIntermediateFiles(next) {
        var toMove = [];
        recursiveFindFilesToMove(0, toMove, function (err) {
            if (err) {
                return next(err);
            }

            async.eachSeries(
                toMove,
                moveFile,
                next
            );
        });
    };

    function getStreamFilepath(gzipped) {
        return getNumberedFile(0, gzipped);
    }

    function newStreamFilepath(triggerinfo, next) {
        var startNewFile = !triggerinfo.hasOwnProperty('startNewFile') ||
                           triggerinfo.startNewFile;

        if (startNewFile) {
            moveIntermediateFiles(function (err) {
                if (err) {
                    return next(err);
                }

                next(null, getNumberedFile(0, false));
            });
        } else {
            next(null, getNumberedFile(0, false));
        }
    }

    return {
        getStreamFilepath: getStreamFilepath,
        newStreamFilepath: newStreamFilepath,
        deleteFiles: deleteFiles,
        moveIntermediateFiles: moveIntermediateFiles
    };
}

module.exports = NumberedFileOps;