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 if(global.repoproxy.maintain==1) {
12 log.warning("cache maintenance routine started, but its already running");
16 global.repoproxy.maintain = 1;
18 log.info("Cache maintainence routine starting...");
19 for(var index in global.repoproxy.repo) {
20 //log.debug("start cleanup in ", index);
21 //log.debug("cleanup array ", global.repoproxy.repo[index]);
22 switch(global.repoproxy.repo[index]["type"]) {
24 log.debug("Scanning '%s' as apt", index);
25 var walkin = path.normalize(global.repoproxy.cacheDir + "/" + index);
26 walkDir(walkin, function(err, list) {
27 repoapt.maintain(index, global.repoproxy.repo[index], list);
31 //log.debug("Scanning '%s' as apt", index);
32 //repoyum.maintain(global.repoproxy.repo[index]);
33 log.debug("Scanning '%s' as yum", index);
34 var walkin = path.normalize(global.repoproxy.cacheDir + "/" + index);
35 walkDir(walkin, function(err, list) {
36 repoyum.maintain(index, global.repoproxy.repo[index], list);
42 log.info("Cache maintainence routine ended...");
46 log.info("beginning cache trash cleanup");
47 // TODO: do this bit properly, check that globals are set properly
48 var spawn = require('child_process').spawn;
50 // TODO: be carefull we actually have a place to remove
51 if(typeof global.repoproxy == "undefined") {
52 log.error("serious issue, globals not accessible?");
54 } else if(typeof global.repoproxy.cacheDir == "undefined") {
55 log.error("serious issue, globals not accessible (cache check)?");
58 var remove = spawn("rm", ["-r", global.repoproxy.cacheDir + "/.cleanup/*"]);
59 remove.on("close", function(code, sig) {
60 log.debug("remove ended with %d, %d", code, sig);
62 remove.stderr.on("data", function(line) {
63 log.debug("stderr from lazy remove: ", line.toString("utf8"));
65 remove.stdout.on("data", function(line) {
66 log.debug("stdout from lazy remove: ", line.toString("ascii"));
69 log.info("Trash empty");
71 global.repoproxy.maintain = 0;
74 exports.startTimer = function() {
75 // our once-a-day cache maintainer
76 var cacheTimer = global.repoproxy.scancache*3600*1000;
77 //var cacheTimer = global.repoproxy.scancache*100;
79 setInterval(maintainCache, cacheTimer);
82 // this code comes frmo http://stackoverflow.com/questions/5827612/node-js-fs-readdir-recursive-directory-search
83 function walkDir(dir, done) {
86 fs.readdir(dir, function(err, list) {
87 if (err) return done(err);
91 if (!file) return done(null, results);
92 file = path.normalize(dir + "/" + file).replace(/\/+/g, "/");
93 fs.stat(file, function(err, stat) {
94 if (stat && stat.isDirectory()) {
95 walkDir(file, function(err, res) {
96 results = results.concat(res);
100 if(!file.match(/.*\.meta.*\.filesize/)) results.push(file);
110 exports.walkDir = walkDir;