strndup
syslog
unsetenv
- utimes)
+ utimensat
+)
foreach(func IN ITEMS ${functions})
string(TOUPPER ${func} func_var)
set(func_var HAVE_${func_var})
// Define if you have the "unsetenv" function.
#cmakedefine HAVE_UNSETENV
-// Define if you have the "utimes" function.
-#cmakedefine HAVE_UTIMES
+// Define if you have the "utimensat" function.
+#cmakedefine HAVE_UTIMENSAT
// Define if you have the "PTHREAD_MUTEX_ROBUST" constant.
#cmakedefine HAVE_PTHREAD_MUTEX_ROBUST
#include <core/exceptions.hpp>
#include <core/wincompat.hpp>
+#include <util/file.hpp>
#include <fcntl.h>
#include <sys/stat.h>
// Update modification timestamp to save the file from LRU cleanup (and, if
// hard-linked, to make the object file newer than the source file).
- Util::update_mtime(*raw_file);
+ util::set_timestamps(*raw_file);
} else {
LOG("Writing to {}", dest_path);
m_dest_fd = Fd(
# include <sys/time.h>
#endif
-#ifdef HAVE_UTIME_H
-# include <utime.h>
-#elif defined(HAVE_SYS_UTIME_H)
-# include <sys/utime.h>
-#endif
-
#ifdef HAVE_LINUX_FS_H
# include <linux/magic.h>
# include <sys/statfs.h>
#endif
}
-void
-update_mtime(const std::string& path)
-{
-#ifdef HAVE_UTIMES
- utimes(path.c_str(), nullptr);
-#else
- utime(path.c_str(), nullptr);
-#endif
-}
-
void
wipe_path(const std::string& path)
{
// Unset environment variable `name`.
void unsetenv(const std::string& name);
-// Set mtime of `path` to the current timestamp.
-void update_mtime(const std::string& path);
-
// Remove `path` (and its contents if it's a directory). A nonexistent path is
// not considered an error.
//
"Retrieved {} from primary storage ({})", key.to_string(), cache_file.path);
// Update modification timestamp to save file from LRU cleanup.
- Util::update_mtime(cache_file.path);
+ util::set_timestamps(cache_file.path);
return cache_file.path;
}
return;
}
- Util::update_mtime(m_config.cache_dir());
+ util::set_timestamps(m_config.cache_dir());
const std::string& temp_dir = m_config.temporary_dir();
if (!Stat::lstat(temp_dir)) {
if (m_update_mtime) {
// Update modification timestamp for potential LRU cleanup by some external
// mechanism.
- Util::update_mtime(path);
+ util::set_timestamps(path);
}
try {
-// Copyright (C) 2021 Joel Rosdahl and other contributors
+// Copyright (C) 2021-2022 Joel Rosdahl and other contributors
//
// See doc/AUTHORS.adoc for a complete list of contributors.
//
#include <core/exceptions.hpp>
#include <fmtmacros.hpp>
+#ifdef HAVE_UTIMENSAT
+# include <fcntl.h>
+# include <sys/stat.h>
+#else
+# include <sys/types.h>
+# ifdef HAVE_UTIME_H
+# include <utime.h>
+# elif defined(HAVE_SYS_UTIME_H)
+# include <sys/utime.h>
+# endif
+#endif
+
namespace util {
void
}
}
+void
+set_timestamps(const std::string& path,
+ std::optional<timespec> mtime,
+ std::optional<timespec> atime)
+{
+#ifdef HAVE_UTIMENSAT
+ timespec atime_mtime[2];
+ if (mtime) {
+ atime_mtime[0] = atime ? *atime : *mtime;
+ atime_mtime[1] = *mtime;
+ }
+ const timespec* const timespecs = mtime ? atime_mtime : nullptr;
+ utimensat(AT_FDCWD, path.c_str(), timespecs, 0);
+#else
+ utimbuf atime_mtime;
+ if (mtime) {
+ atime_mtime.actime = atime ? atime->tv_sec : mtime->tv_sec;
+ atime_mtime.modtime = mtime->tv_sec;
+ }
+ utime(path.c_str(), &atime_mtime);
+#endif
+}
+
} // namespace util
-// Copyright (C) 2021 Joel Rosdahl and other contributors
+// Copyright (C) 2021-2022 Joel Rosdahl and other contributors
//
// See doc/AUTHORS.adoc for a complete list of contributors.
//
#pragma once
+#include <optional>
#include <string>
namespace util {
void create_cachedir_tag(const std::string& dir);
+// Set atime/mtime of `path`. If `mtime` is std::nullopt, set to the current
+// time. If `atime` is std::nullopt, set to what `mtime` specifies.
+void set_timestamps(const std::string& path,
+ std::optional<timespec> mtime = std::nullopt,
+ std::optional<timespec> atime = std::nullopt);
+
} // namespace util