1 var fs = require("fs");
2 var http = require("http");
3 var url = require("url");
4 var path = require("path");
5 var repoapt = require("./repo-apt.js");
6 var repoyum = require("./repo-yum.js");
7 var log = require("./log.js");
9 function maintainCache() {
10 // TODO i should check that im already running here and exit if i am
11 log.debug("Cache maintainence routine starting...");
12 for(var index in global.repoproxy.repo) {
13 //log.debug("start cleanup in ", index);
14 //log.debug("cleanup array ", global.repoproxy.repo[index]);
15 switch(global.repoproxy.repo[index]["type"]) {
17 log.debug("Scanning '%s' as apt", index);
18 var walkin = path.normalize(global.repoproxy.cacheDir + "/" + index);
19 walkDir(walkin, function(err, list) {
20 repoapt.maintain(index, global.repoproxy.repo[index], list);
24 //log.debug("Scanning '%s' as apt", index);
25 //repoyum.maintain(global.repoproxy.repo[index]);
26 log.debug("Scanning '%s' as yum", index);
27 var walkin = path.normalize(global.repoproxy.cacheDir + "/" + index);
28 walkDir(walkin, function(err, list) {
29 repoyum.maintain(index, global.repoproxy.repo[index], list);
35 log.debug("Cache maintainence routine ended...");
38 exports.startTimer = function() {
39 // our once-a-day cache maintainer
40 var cacheTimer = global.repoproxy.scancache*3600*1000;
41 //var cacheTimer = global.repoproxy.scancache*100;
43 setInterval(maintainCache, cacheTimer);
46 // this code comes frmo http://stackoverflow.com/questions/5827612/node-js-fs-readdir-recursive-directory-search
47 function walkDir(dir, done) {
50 fs.readdir(dir, function(err, list) {
51 if (err) return done(err);
55 if (!file) return done(null, results);
56 file = path.normalize(dir + "/" + file);
57 fs.stat(file, function(err, stat) {
58 if (stat && stat.isDirectory()) {
59 walkDir(file, function(err, res) {
60 results = results.concat(res);
64 if(!file.match(/.*\.meta.*\.filesize/)) results.push(file);
74 exports.walkDir = walkDir;