]>
git.saurik.com Git - apt.git/blob - ftparchive/byhash.cc
1 // -*- mode: cpp; mode: fold -*-
3 /* ######################################################################
7 ByHash helper functions
9 ##################################################################### */
11 // Include Files /*{{{*/
20 #include <apt-pkg/fileutl.h>
21 #include <apt-pkg/hashes.h>
24 // Delete all files in a directory except the most recent N ones
25 void DeleteAllButMostRecent(std::string dir
, int KeepFiles
)
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
;
38 if (!DirectoryExists(dir
))
41 auto files
= GetListOfFilesInDir(dir
, false);
42 std::sort(files
.begin(), files
.end(), Cmp());
44 for (auto I
=files
.begin(); I
<files
.end()-KeepFiles
; I
++) {
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
51 std::string
GenByHashFilename(std::string Input
, HashString h
)
53 std::string ByHashOutputFile
= Input
;
54 std::string
const ByHash
= "/by-hash/" + h
.HashType() + "/" + h
.HashValue();
55 size_t trailing_slash
= ByHashOutputFile
.find_last_of("/");
56 if (trailing_slash
== std::string::npos
)
58 ByHashOutputFile
= ByHashOutputFile
.replace(
60 ByHashOutputFile
.substr(trailing_slash
+1).size()+1,
62 return ByHashOutputFile
;