diff options
author | Michael Vogt <mvo@ubuntu.com> | 2015-09-04 23:29:38 +0200 |
---|---|---|
committer | Michael Vogt <mvo@ubuntu.com> | 2015-09-04 23:29:38 +0200 |
commit | 7852873a1347fcab50393b545cc1e6edd65531c8 (patch) | |
tree | 73cfb2912e6676f8a36b6d28c0599175233035cc /ftparchive/byhash.cc | |
parent | c7609dd7a418428ffbca4c81a7950c4f53c92450 (diff) | |
download | apt-7852873a1347fcab50393b545cc1e6edd65531c8.tar.gz |
Add support for writing by-hash dirs in apt-ftparchive
This option is enabled via the APT::FTPArchive::DoByHash switch.
It will also honor the option APT::FTPArchive::By-Hash-Keep that
controls how many previous generation of by-hash files should be
kept (defaults to 3).
Merged from https://github.com/mvo5/apt/tree/feature/apt-ftparchive-by-hash
Diffstat (limited to 'ftparchive/byhash.cc')
-rw-r--r-- | ftparchive/byhash.cc | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/ftparchive/byhash.cc b/ftparchive/byhash.cc new file mode 100644 index 000000000..04f8f1629 --- /dev/null +++ b/ftparchive/byhash.cc @@ -0,0 +1,63 @@ +// -*- mode: cpp; mode: fold -*- +// Description /*{{{*/ +/* ###################################################################### + + ByHash + + ByHash helper functions + + ##################################################################### */ + /*}}}*/ +// Include Files /*{{{*/ +#include <config.h> + +#include<algorithm> +#include<string> + +#include <unistd.h> +#include <sys/stat.h> + +#include <apt-pkg/fileutl.h> +#include <apt-pkg/hashes.h> +#include "byhash.h" + +// Delete all files in a directory except the most recent N ones +void DeleteAllButMostRecent(std::string dir, int KeepFiles) +{ + struct Cmp { + bool operator() (const std::string& lhs, const std::string& rhs) { + struct stat buf_l, buf_r; + stat(lhs.c_str(), &buf_l); + stat(rhs.c_str(), &buf_r); + if (buf_l.st_mtim.tv_sec == buf_r.st_mtim.tv_sec) + return buf_l.st_mtim.tv_nsec < buf_r.st_mtim.tv_nsec; + return buf_l.st_mtim.tv_sec < buf_r.st_mtim.tv_sec; + } + }; + + if (!DirectoryExists(dir)) + return; + + auto files = GetListOfFilesInDir(dir, false); + std::sort(files.begin(), files.end(), Cmp()); + + for (auto I=files.begin(); I<files.end()-KeepFiles; I++) { + unlink((*I).c_str()); + } +} + +// Takes a input filename (e.g. binary-i386/Packages) and a hashstring +// of the Input data and transforms it into a suitable by-hash filename +std::string GenByHashFilename(std::string Input, HashString h) +{ + std::string ByHashOutputFile = Input; + std::string const ByHash = "/by-hash/" + h.HashType() + "/" + h.HashValue(); + size_t trailing_slash = ByHashOutputFile.find_last_of("/"); + if (trailing_slash == std::string::npos) + trailing_slash = 0; + ByHashOutputFile = ByHashOutputFile.replace( + trailing_slash, + ByHashOutputFile.substr(trailing_slash+1).size()+1, + ByHash); + return ByHashOutputFile; +} |