]>
Commit | Line | Data |
---|---|---|
7852873a MV |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
3 | /* ###################################################################### | |
4 | ||
5 | ByHash | |
6 | ||
7 | ByHash helper functions | |
8 | ||
9 | ##################################################################### */ | |
10 | /*}}}*/ | |
11 | // Include Files /*{{{*/ | |
12 | #include <config.h> | |
13 | ||
14 | #include<algorithm> | |
15 | #include<string> | |
16 | ||
17 | #include <unistd.h> | |
18 | #include <sys/stat.h> | |
19 | ||
20 | #include <apt-pkg/fileutl.h> | |
21 | #include <apt-pkg/hashes.h> | |
22 | #include "byhash.h" | |
23 | ||
24 | // Delete all files in a directory except the most recent N ones | |
25 | void DeleteAllButMostRecent(std::string dir, int KeepFiles) | |
26 | { | |
27 | struct Cmp { | |
28 | bool operator() (const std::string& lhs, const std::string& rhs) { | |
29 | struct stat buf_l, buf_r; | |
30 | stat(lhs.c_str(), &buf_l); | |
31 | stat(rhs.c_str(), &buf_r); | |
32 | if (buf_l.st_mtim.tv_sec == buf_r.st_mtim.tv_sec) | |
33 | return buf_l.st_mtim.tv_nsec < buf_r.st_mtim.tv_nsec; | |
34 | return buf_l.st_mtim.tv_sec < buf_r.st_mtim.tv_sec; | |
35 | } | |
36 | }; | |
37 | ||
38 | if (!DirectoryExists(dir)) | |
39 | return; | |
40 | ||
41 | auto files = GetListOfFilesInDir(dir, false); | |
42 | std::sort(files.begin(), files.end(), Cmp()); | |
43 | ||
44 | for (auto I=files.begin(); I<files.end()-KeepFiles; I++) { | |
45 | unlink((*I).c_str()); | |
46 | } | |
47 | } | |
48 | ||
49 | // Takes a input filename (e.g. binary-i386/Packages) and a hashstring | |
50 | // of the Input data and transforms it into a suitable by-hash filename | |
e2ea6b63 | 51 | std::string GenByHashFilename(std::string ByHashOutputFile, HashString const &h) |
7852873a | 52 | { |
7852873a MV |
53 | std::string const ByHash = "/by-hash/" + h.HashType() + "/" + h.HashValue(); |
54 | size_t trailing_slash = ByHashOutputFile.find_last_of("/"); | |
55 | if (trailing_slash == std::string::npos) | |
56 | trailing_slash = 0; | |
57 | ByHashOutputFile = ByHashOutputFile.replace( | |
58 | trailing_slash, | |
59 | ByHashOutputFile.substr(trailing_slash+1).size()+1, | |
60 | ByHash); | |
61 | return ByHashOutputFile; | |
62 | } |