fixing repo bits
[nodejs-repoproxy.git] / lib / repo-yum.js
1 var fs = require("fs");
2 var http = require("http");
3 var url = require("url");
4 var path = require("path");
5 var cache = require("./cache.js");
6 var log = require("./log.js");
7
8
9
10 function maintain(name, repoinfo, files) {
11         log.info("Starting maintenance routine for yum repo %s (%s)", name, repoinfo.url);
12         
13         var metaAge = 24*3600*1000*(repoinfo.updateinterval);
14         var expireAge = 24*3600*1000*(repoinfo.expiretime);
15         
16         
17         function fileCheck(i) {
18                 var wasExpired = false;
19                 var inDownload = false;
20                 
21                 log.debug("checking file: ", files[i]);
22                 log.debug("checking if file is in download mode");
23
24                 // need to check global.repoproxy.downloads
25                 if(typeof global.repoproxy.downloads[files[i]] != "undefined") {
26                         if(global.repoproxy.downloads[files[i]] == 1) {
27                                 inDownload=true;
28                         }
29                 }
30                 
31                 
32                 
33                 // we look for repodata/repomd.xml file, if this is past maturity, we clean that whole directory
34                 if(!inDownload) {
35                         
36                         var metafile = files[i].replace(/(.*)\/(.[^\/]+$)/, "$1/.meta.$2.filesize");
37
38                         if(files[i].match(/.*repodata\/repomd\.xml$/)) {
39                                 log.debug("Found repomd.xml file: ", files[i]);
40                                 
41                                 // strip the repomd file to get the dir
42                                 var repomddir = files[i].replace(/repomd\.xml$/, "");
43                                 log.debug("repomd dir is:", repomddir);
44         
45                                 
46                                 // do the file stat
47                                 fs.stat(files[i], function(err, stats) {
48                                         log.debug("stats for file was: ", stats);
49                                         var curtime = new Date().getTime();
50                                         var ctime = stats.ctime.getTime();
51                                         log.debug("curtime is ", curtime);
52                                         log.debug("ctime is ", ctime);
53                                         
54                                         var age = curtime - ctime;
55                                         log.debug("age is (%d) for (%d)", age, metaAge);
56                                         if(age > metaAge) {
57                                                 cache.moveToCleanup(repomddir);
58                                                 log.info("Sending repomd directory to trash for cleanup (%s)", repomddir);
59                                                 wasExpired = true;
60                                         }
61                                 })
62                                 
63                         } else {
64                                 // STUFF!!!
65                                 fs.stat(files[i], function(err, stats) {
66                                         log.debug("stats for file was: ", stats);
67                                         var curtime = new Date().getTime();
68                                         var atime = stats.atime.getTime();
69                                         log.debug("curtime is ", curtime);
70                                         log.debug("ctime is ", atime);
71                                         
72                                         var age = curtime - atime;
73                                         //log.debug("age is (%d) for (%d)", age, expireAge);
74                                         if(age > expireAge) {
75                                                 // TODO: cleanup singular file
76                                                 // TODO: cleanup meta too, fuck me
77                                                 //log.debug("clean up file \n", files[i]);
78                                                 //log.debug("meta for this file is \n", nfile);
79                                                 cache.moveToCleanup(files[i]);
80                                                 cache.moveToCleanup(metafile);
81                                                 log.info("Sending expired file to trash for cleanup (%s)", files[i]);
82                                                 wasExpired = true;
83                                         }
84                                 })
85                         }
86                         
87                         // check meta data
88                         fs.stat(files[i], function(err, stats) {
89                                 var fsize = stats.size;
90                                 var mfile = fs.createReadStream(metafile);
91                                 var expSize = "";
92                                 mfile.on("data", function(data) {
93                                         expSize += data;
94                                 });
95                                 
96                                 mfile.on("end", function(closed) {
97                                         if(fsize != parseInt(expSize)) {
98                                                 log.debug("possible metadata clash for (%s) - (%d),(%s), removing file", files[i], fsize, expSize);
99                                                 cache.moveToCleanup(files[i]);
100                                                 cache.moveToCleanup(metafile);
101                                         } else {
102                                                 log.debug("metadata good for (%s) - (%d),(%s)", files[i], fsize, expSize);
103                                         }
104                                 });
105
106                         })
107                 } else {
108                         log.debug("file %s was ignored as its in download", files[i]);
109                 }
110
111                 if(typeof files[i+1] != "undefined") fileCheck(i+1);
112         }
113         
114         if(typeof files[0] != 'undefined') fileCheck(0)
115         else log.info("Skipping (yum) file check as there are none... apprently?");
116
117 }
118
119 exports.maintain = maintain;