You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

62 lines
1.8 KiB

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