Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / log4js / lib / streams / RollingFileStream.js
1 "use strict";
2 var BaseRollingFileStream = require('./BaseRollingFileStream')
3 , debug = require('../debug')('RollingFileStream')
4 , util = require('util')
5 , path = require('path')
6 , child_process = require('child_process')
7 , zlib = require("zlib")
8 , fs = require('fs');
9
10 module.exports = RollingFileStream;
11
12 function RollingFileStream (filename, size, backups, options) {
13   this.size = size;
14   this.backups = backups || 1;
15   
16   function throwErrorIfArgumentsAreNotValid() {
17     if (!filename || !size || size <= 0) {
18       throw new Error("You must specify a filename and file size");
19     }
20   }
21   
22   throwErrorIfArgumentsAreNotValid();
23   
24   RollingFileStream.super_.call(this, filename, options);
25 }
26 util.inherits(RollingFileStream, BaseRollingFileStream);
27
28 RollingFileStream.prototype.shouldRoll = function() {
29   debug("should roll with current size " + this.currentSize + " and max size " + this.size);
30   return this.currentSize >= this.size;
31 };
32
33 RollingFileStream.prototype.roll = function(filename, callback) {
34   var that = this,
35   nameMatcher = new RegExp('^' + path.basename(filename));
36   
37   function justTheseFiles (item) {
38     return nameMatcher.test(item);
39   }
40   
41   function index(filename_) {
42     debug('Calculating index of '+filename_);
43     return parseInt(filename_.substring((path.basename(filename) + '.').length), 10) || 0;
44   }
45   
46   function byIndex(a, b) {
47     if (index(a) > index(b)) {
48       return 1;
49     } else if (index(a) < index(b) ) {
50       return -1;
51     } else {
52       return 0;
53     }
54   }
55
56   function compress (filename, cb) {
57
58     var gzip = zlib.createGzip();
59     var inp = fs.createReadStream(filename);
60     var out = fs.createWriteStream(filename+".gz");
61     inp.pipe(gzip).pipe(out);
62     fs.unlink(filename, cb);
63
64   }
65
66   function increaseFileIndex (fileToRename, cb) {
67     var idx = index(fileToRename);
68     debug('Index of ' + fileToRename + ' is ' + idx);
69     if (idx < that.backups) {
70
71       var ext = path.extname(fileToRename);
72       var destination = filename + '.' + (idx+1);
73       if (that.options.compress && /^gz$/.test(ext.substring(1))) {
74         destination+=ext;
75       }
76       //on windows, you can get a EEXIST error if you rename a file to an existing file
77       //so, we'll try to delete the file we're renaming to first
78       fs.unlink(destination, function (err) {
79         //ignore err: if we could not delete, it's most likely that it doesn't exist
80         debug('Renaming ' + fileToRename + ' -> ' + destination);
81         fs.rename(path.join(path.dirname(filename), fileToRename), destination, function(err) {
82           if (err) {
83             cb(err);
84           } else {
85             if (that.options.compress && ext!=".gz") {
86               compress(destination, cb);
87             } else {
88               cb();
89             }
90           }
91         });
92       });
93     } else {
94       cb();
95     }
96   }
97
98   function renameTheFiles(cb) {
99     //roll the backups (rename file.n to file.n+1, where n <= numBackups)
100     debug("Renaming the old files");
101     fs.readdir(path.dirname(filename), function (err, files) {
102       var filesToProcess = files.filter(justTheseFiles).sort(byIndex);
103       (function processOne(err) {
104         var file = filesToProcess.pop();
105         if (!file || err) { return cb(err); }
106         increaseFileIndex(file, processOne);
107       })();
108     });
109   }
110
111   debug("Rolling, rolling, rolling");
112   this.closeTheStream(
113     renameTheFiles.bind(null,
114       this.openTheStream.bind(this,
115         callback)));
116
117 };