fixing repo bits
[nodejs-repoproxy.git] / lib / repo-yum.js
index 18b383a..57741ba 100644 (file)
@@ -1,5 +1,119 @@
-function maintain(details) {
-       console.log("doing yum clean for ", details);
+var fs = require("fs");
+var http = require("http");
+var url = require("url");
+var path = require("path");
+var cache = require("./cache.js");
+var log = require("./log.js");
+
+
+
+function maintain(name, repoinfo, files) {
+       log.info("Starting maintenance routine for yum repo %s (%s)", name, repoinfo.url);
+       
+       var metaAge = 24*3600*1000*(repoinfo.updateinterval);
+       var expireAge = 24*3600*1000*(repoinfo.expiretime);
+       
+       
+       function fileCheck(i) {
+               var wasExpired = false;
+               var inDownload = false;
+               
+               log.debug("checking file: ", files[i]);
+               log.debug("checking if file is in download mode");
+
+               // need to check global.repoproxy.downloads
+               if(typeof global.repoproxy.downloads[files[i]] != "undefined") {
+                       if(global.repoproxy.downloads[files[i]] == 1) {
+                               inDownload=true;
+                       }
+               }
+               
+               
+               
+               // we look for repodata/repomd.xml file, if this is past maturity, we clean that whole directory
+               if(!inDownload) {
+                       
+                       var metafile = files[i].replace(/(.*)\/(.[^\/]+$)/, "$1/.meta.$2.filesize");
+
+                       if(files[i].match(/.*repodata\/repomd\.xml$/)) {
+                               log.debug("Found repomd.xml file: ", files[i]);
+                               
+                               // strip the repomd file to get the dir
+                               var repomddir = files[i].replace(/repomd\.xml$/, "");
+                               log.debug("repomd dir is:", repomddir);
+       
+                               
+                               // do the file stat
+                               fs.stat(files[i], function(err, stats) {
+                                       log.debug("stats for file was: ", stats);
+                                       var curtime = new Date().getTime();
+                                       var ctime = stats.ctime.getTime();
+                                       log.debug("curtime is ", curtime);
+                                       log.debug("ctime is ", ctime);
+                                       
+                                       var age = curtime - ctime;
+                                       log.debug("age is (%d) for (%d)", age, metaAge);
+                                       if(age > metaAge) {
+                                               cache.moveToCleanup(repomddir);
+                                               log.info("Sending repomd directory to trash for cleanup (%s)", repomddir);
+                                               wasExpired = true;
+                                       }
+                               })
+                               
+                       } else {
+                               // STUFF!!!
+                               fs.stat(files[i], function(err, stats) {
+                                       log.debug("stats for file was: ", stats);
+                                       var curtime = new Date().getTime();
+                                       var atime = stats.atime.getTime();
+                                       log.debug("curtime is ", curtime);
+                                       log.debug("ctime is ", atime);
+                                       
+                                       var age = curtime - atime;
+                                       //log.debug("age is (%d) for (%d)", age, expireAge);
+                                       if(age > expireAge) {
+                                               // TODO: cleanup singular file
+                                               // TODO: cleanup meta too, fuck me
+                                               //log.debug("clean up file \n", files[i]);
+                                               //log.debug("meta for this file is \n", nfile);
+                                               cache.moveToCleanup(files[i]);
+                                               cache.moveToCleanup(metafile);
+                                               log.info("Sending expired file to trash for cleanup (%s)", files[i]);
+                                               wasExpired = true;
+                                       }
+                               })
+                       }
+                       
+                       // check meta data
+                       fs.stat(files[i], function(err, stats) {
+                               var fsize = stats.size;
+                               var mfile = fs.createReadStream(metafile);
+                               var expSize = "";
+                               mfile.on("data", function(data) {
+                                       expSize += data;
+                               });
+                               
+                               mfile.on("end", function(closed) {
+                                       if(fsize != parseInt(expSize)) {
+                                               log.debug("possible metadata clash for (%s) - (%d),(%s), removing file", files[i], fsize, expSize);
+                                               cache.moveToCleanup(files[i]);
+                                               cache.moveToCleanup(metafile);
+                                       } else {
+                                               log.debug("metadata good for (%s) - (%d),(%s)", files[i], fsize, expSize);
+                                       }
+                               });
+
+                       })
+               } else {
+                       log.debug("file %s was ignored as its in download", files[i]);
+               }
+
+               if(typeof files[i+1] != "undefined") fileCheck(i+1);
+       }
+       
+       if(typeof files[0] != 'undefined') fileCheck(0)
+       else log.info("Skipping (yum) file check as there are none... apprently?");
+
 }
 
 exports.maintain = maintain;
\ No newline at end of file