Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend last_write_time implementation by special-casing file touching #2141

Merged
merged 3 commits into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion libmamba/include/mamba/core/fsutil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace mamba
path = env::expand_user(path);
if (lexists(path))
{
fs::last_write_time(path, fs::file_time_type::clock::now());
fs::last_write_time(path, fs::now());
return true;
}
else
Expand Down
41 changes: 41 additions & 0 deletions libmamba/include/mamba/core/mamba_fs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@

#include "mamba/core/util_string.hpp"

#if !defined(_WIN32)
#include <sys/stat.h>
#include <fcntl.h>

// We can use the presence of UTIME_OMIT to detect platforms that provide
// utimensat.
#if defined(UTIME_OMIT)
#define USE_UTIMENSAT
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering whether to prefix the constant, ie like _MAMBA_USE_UTIMENSAT?

#endif
#endif


//---- RATIONAL: Why do we wrap standard filesystem here? ----
// 1. This codebase relies on `std::string` and `const char*` to denote UTF-8 encoded text.
// However `std::filesystem::path` constructors cannot assume that `std::string` is in
Expand Down Expand Up @@ -63,6 +75,10 @@

namespace fs
{
// sentinel argument for indicating the current time to last_write_time
class now
{
};


#if defined(_WIN32)
Expand Down Expand Up @@ -1148,6 +1164,31 @@ namespace fs
return std::filesystem::last_write_time(path, std::forward<OtherArgs>(args)...);
}

// void last_write_time(const path& p, now _, error_code& ec) noexcept;
inline void last_write_time(const u8path& path, now _, std::error_code& ec) noexcept
{
#if defined(USE_UTIMENSAT)
if (utimensat(AT_FDCWD, path.string().c_str(), NULL, 0) == -1)
{
ec = std::error_code(errno, std::generic_category());
}
#else
auto new_time = fs::file_time_type::clock::now();
std::filesystem::last_write_time(path, new_time, ec);
#endif
}

// void last_write_time(const path& p, now _);
inline void last_write_time(const u8path& path, now sentinel)
{
std::error_code ec;
last_write_time(path, sentinel, ec);
if (ec)
{
throw filesystem_error("last_write_time", path, ec);
}
}

// void last_write_time(const path& p, file_time_type new_time);
// void last_write_time(const path& p, file_time_type new_time, error_code& ec) noexcept;
template <typename... OtherArgs>
Expand Down
6 changes: 3 additions & 3 deletions libmamba/src/core/subdirdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -503,13 +503,13 @@ namespace mamba
{
LOG_TRACE << "Refreshing '" << json_file.string() << "'";
auto lock = LockFile(json_file);
fs::last_write_time(json_file, now);
fs::last_write_time(json_file, fs::now());
}
if (fs::exists(solv_file) && solv_age.count() <= json_age.count())
{
LOG_TRACE << "Refreshing '" << solv_file.string() << "'";
auto lock = LockFile(solv_file);
fs::last_write_time(solv_file, now);
fs::last_write_time(solv_file, fs::now());
m_solv_cache_valid = true;
}

Expand Down Expand Up @@ -606,7 +606,7 @@ namespace mamba
m_temp_file.reset(nullptr);
final_file.close();

fs::last_write_time(json_file, fs::file_time_type::clock::now());
fs::last_write_time(json_file, fs::now());

return true;
}
Expand Down