From 636c7dc17286d93d788c741d15fd756aeda066d5 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 5 May 2024 20:07:13 +0200 Subject: Adding upstream version 1.8.2.3. Signed-off-by: Daniel Baumann --- ftparchive/byhash.cc | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 ftparchive/byhash.cc (limited to 'ftparchive/byhash.cc') diff --git a/ftparchive/byhash.cc b/ftparchive/byhash.cc new file mode 100644 index 0000000..b24f615 --- /dev/null +++ b/ftparchive/byhash.cc @@ -0,0 +1,61 @@ +// -*- mode: cpp; mode: fold -*- +// Description /*{{{*/ +/* ###################################################################### + + ByHash + + ByHash helper functions + + ##################################################################### */ + /*}}}*/ +// Include Files /*{{{*/ +#include + +#include +#include + +#include +#include + +#include "byhash.h" +#include +#include + +// 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