]>
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
)
45 RemoveFile("DeleteAllButMostRecent", *I
);
48 // Takes a input filename (e.g. binary-i386/Packages) and a hashstring
49 // of the Input data and transforms it into a suitable by-hash filename
50 std::string
GenByHashFilename(std::string ByHashOutputFile
, HashString
const &h
)
52 std::string
const ByHash
= "/by-hash/" + h
.HashType() + "/" + h
.HashValue();
53 size_t trailing_slash
= ByHashOutputFile
.find_last_of("/");
54 if (trailing_slash
== std::string::npos
)
56 ByHashOutputFile
= ByHashOutputFile
.replace(
58 ByHashOutputFile
.substr(trailing_slash
+1).size()+1,
60 return ByHashOutputFile
;