fixing meta data aging
[nodejs-repoproxy.git] / lib / config.js
1 var fs = require("fs");
2
3 exports.loadConfig = function (conffile) {
4         
5         global.repoproxy = new Object();
6         global.repoproxy.listenPort = 8008;
7         global.repoproxy.cacheDir = "./cache";
8         global.repoproxy.repo = new Object();
9         global.repoproxy.scancache = 1;
10         global.repoproxy.downloads = new Object();
11         
12         var confFileData = fs.readFileSync(conffile, "utf8");
13         
14         // first split the file on carriage returns;
15         var confLines = confFileData.split("\n");
16         
17         // go thru each line looking for config vars
18         for(var i=0; i<confLines.length; i++) {
19                 
20                 // trim a line down
21                 var line_one = confLines[i].trim();
22                 
23                 // split it up with :'s
24                 var line_real = line_one.replace(/#.*/,"").split(":");
25                 
26                 // parse the line
27                 switch(line_real[0]) {
28                 case "repo":
29                         
30                         // TODO: VALIDATE!
31                         console.log("Adding repo: '/%s' type '%s' from '%s', with update interval of '%s' days, and expire time of '%s' days.", line_real[1], line_real[2], line_real[3]+":"+line_real[4], line_real[5], line_real[6]);
32                         var thisrepo = { type : line_real[2], url: line_real[3]+":"+line_real[4], updateinterval: line_real[5], expiretime: line_real[6] };
33                         global.repoproxy.repo[line_real[1]] = thisrepo;
34                         
35                         break;
36                 case "cachedir":
37                         var tmppath = line_real[1].replace(/\/+/g, "/");
38                         console.log("Cache dir set to: ", tmppath);
39                         global.repoproxy.cacheDir = tmppath;
40                         break;
41                 case "listenport":
42                         console.log("Port set to: ", line_real[1]);
43                         global.repoproxy.listenPort = line_real[1];
44                         break;
45                 case "cachescan":
46                         console.log("Set cache scan rate to: '%s' hours", line_real[1]);
47                         global.repoproxy.scancache = parseInt(line_real[1]);
48                         if(global.repoproxy.scancache == 0) {
49                                 console.log("Cache scan rate didnt make sense, it was 0, and should be at least 1 - it is set to 24, but you should check this setting");
50                                 global.repoproxy.scancache = 24;
51                         }
52                         break;
53                 default:
54                         if(line_real[0] != "") {
55                                 console.log("WARNING Invalid line in configuration file ignored: '%s'", line_one);
56                         }
57                 }
58         }
59         
60         createCacheStructure();
61 }
62
63
64 function createCacheStructure() {
65         try {
66                 var state = fs.statSync(global.repoproxy.cacheDir);
67                 //console.log("state is:", state);
68         } catch(e) {
69                 try {
70                         fs.mkdirSync(global.repoproxy.cacheDir);
71                         fs.mkdirSync(global.repoproxy.cacheDir + "/.cleanup");
72                 } catch(ex) {
73                         console.log("ERROR: failure to create cache directory, '%s'", global.repoproxy.cacheDir);
74                 }
75         }
76         
77         try {
78                 fs.mkdirSync(global.repoproxy.cacheDir + "/.cleanup");
79         } catch(ex) {
80                 console.log("ERROR: cant create cleanup directory, '%s'", global.repoproxy.cacheDir + "/.cleanup");
81         }
82         
83         console.log("next: ", global.repoproxy.repo);
84         for(var index in global.repoproxy.repo) {
85                 var fullDir = global.repoproxy.cacheDir + "/" + index;
86                 console.log("on end, ", fullDir);
87                 try {
88                         var state = fs.statSync(fullDir);
89                         console.log("state is:", state);
90                 } catch(e) {
91                         try {
92                                 console.log("attempted to create cache dir, ", fullDir);
93                                 fs.mkdirSync(fullDir);
94                         } catch(ex) {
95                                 console.log("ERROR: failed to create cache directory, '%s' for '%s'", fullDir, index);
96                         }
97                 }
98         }
99 }