mirror of
https://github.com/mhx/dwarfs.git
synced 2025-08-05 18:56:37 -04:00
refactor: introduce and use safe_localtime()
This commit is contained in:
parent
4b277a0507
commit
7b0b1cdc0c
@ -83,4 +83,6 @@ int get_current_umask();
|
|||||||
|
|
||||||
void install_signal_handlers();
|
void install_signal_handlers();
|
||||||
|
|
||||||
|
std::tm safe_localtime(std::time_t t);
|
||||||
|
|
||||||
} // namespace dwarfs
|
} // namespace dwarfs
|
||||||
|
@ -37,6 +37,7 @@
|
|||||||
#include <dwarfs/history.h>
|
#include <dwarfs/history.h>
|
||||||
#include <dwarfs/library_dependencies.h>
|
#include <dwarfs/library_dependencies.h>
|
||||||
#include <dwarfs/malloc_byte_buffer.h>
|
#include <dwarfs/malloc_byte_buffer.h>
|
||||||
|
#include <dwarfs/util.h>
|
||||||
#include <dwarfs/version.h>
|
#include <dwarfs/version.h>
|
||||||
|
|
||||||
#include <dwarfs/gen-cpp2/history_types.h>
|
#include <dwarfs/gen-cpp2/history_types.h>
|
||||||
@ -105,10 +106,9 @@ void history::dump(std::ostream& os) const {
|
|||||||
for (auto const& histent : *history_->entries()) {
|
for (auto const& histent : *history_->entries()) {
|
||||||
os << " " << fmt::format("{:>{}}:", i++, iwidth);
|
os << " " << fmt::format("{:>{}}:", i++, iwidth);
|
||||||
|
|
||||||
if (histent.timestamp().has_value()) {
|
if (auto ts = histent.timestamp(); ts.has_value()) {
|
||||||
os << " "
|
os << " [" << fmt::format("{:%F %T}", safe_localtime(ts.value()))
|
||||||
<< fmt::format("[{:%Y-%m-%d %H:%M:%S}]",
|
<< "]";
|
||||||
fmt::localtime(histent.timestamp().value()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
auto const& version = histent.version().value();
|
auto const& version = histent.version().value();
|
||||||
@ -172,11 +172,10 @@ nlohmann::json history::as_json() const {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (histent.timestamp().has_value()) {
|
if (auto ts = histent.timestamp(); ts.has_value()) {
|
||||||
entry["timestamp"] = {
|
entry["timestamp"] = {
|
||||||
{"epoch", histent.timestamp().value()},
|
{"epoch", ts.value()},
|
||||||
{"local", fmt::format("{:%Y-%m-%dT%H:%M:%S}",
|
{"local", fmt::format("%FT%T", safe_localtime(ts.value()))},
|
||||||
fmt::localtime(histent.timestamp().value()))},
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1335,8 +1335,7 @@ metadata_v2_data::info_as_json(fsinfo_options const& opts,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (auto ts = meta_.create_timestamp()) {
|
if (auto ts = meta_.create_timestamp()) {
|
||||||
info["created_on"] =
|
info["created_on"] = fmt::format("{:%FT%T}", safe_localtime(ts.value()));
|
||||||
fmt::format("{:%Y-%m-%dT%H:%M:%S}", fmt::localtime(ts.value()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (opts.features.has(fsinfo_feature::metadata_summary)) {
|
if (opts.features.has(fsinfo_feature::metadata_summary)) {
|
||||||
@ -1468,11 +1467,8 @@ void metadata_v2_data::dump(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (auto ts = meta_.create_timestamp()) {
|
if (auto ts = meta_.create_timestamp()) {
|
||||||
time_t tp = *ts;
|
os << "created on: " << fmt::format("{:%F %T}", safe_localtime(*ts))
|
||||||
std::string str(32, '\0');
|
<< "\n";
|
||||||
str.resize(
|
|
||||||
std::strftime(str.data(), str.size(), "%F %T", std::localtime(&tp)));
|
|
||||||
os << "created on: " << str << "\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (opts.features.has(fsinfo_feature::metadata_summary)) {
|
if (opts.features.has(fsinfo_feature::metadata_summary)) {
|
||||||
|
15
src/util.cpp
15
src/util.cpp
@ -556,4 +556,19 @@ void install_signal_handlers() {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::tm safe_localtime(std::time_t t) {
|
||||||
|
std::tm buf{};
|
||||||
|
#ifdef _WIN32
|
||||||
|
if (auto r = ::localtime_s(&buf, &t); r != 0) {
|
||||||
|
DWARFS_THROW(runtime_error, fmt::format("localtime_s: error code {}", r));
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
if (!::localtime_r(&t, &buf)) {
|
||||||
|
DWARFS_THROW(runtime_error,
|
||||||
|
fmt::format("localtime_r: error code {}", errno));
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace dwarfs
|
} // namespace dwarfs
|
||||||
|
@ -2631,16 +2631,17 @@ TEST(dwarfsck_test, list_files_verbose) {
|
|||||||
|
|
||||||
auto num_lines = std::count(out.begin(), out.end(), '\n');
|
auto num_lines = std::count(out.begin(), out.end(), '\n');
|
||||||
EXPECT_EQ(12, num_lines);
|
EXPECT_EQ(12, num_lines);
|
||||||
|
auto format_time = [](time_t t) {
|
||||||
|
return fmt::format("{:%F %H:%M}", safe_localtime(t));
|
||||||
|
};
|
||||||
|
|
||||||
std::vector<std::string> expected_re{
|
std::vector<std::string> expected_re{
|
||||||
fmt::format("drwxrwxrwx\\s+1000/100\\s+8\\s+{:%Y-%m-%d %H:%M}\\s*\n",
|
fmt::format("drwxrwxrwx\\s+1000/100\\s+8\\s+{}\\s*\n", format_time(2)),
|
||||||
fmt::localtime(2)),
|
fmt::format("-rw-------\\s+1337/ 0\\s+{:L}\\s+{}\\s+baz.pl\n", 23456,
|
||||||
fmt::format(
|
format_time(8002)),
|
||||||
"-rw-------\\s+1337/ 0\\s+{:L}\\s+{:%Y-%m-%d %H:%M}\\s+baz.pl\n",
|
fmt::format("lrwxrwxrwx\\s+1000/100\\s+16\\s+{}\\s+somelink -> "
|
||||||
23456, fmt::localtime(8002)),
|
"somedir/ipsum.py\n",
|
||||||
fmt::format("lrwxrwxrwx\\s+1000/100\\s+16\\s+{:%Y-%m-%d "
|
format_time(2002)),
|
||||||
"%H:%M}\\s+somelink -> somedir/ipsum.py\n",
|
|
||||||
fmt::localtime(2002)),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
for (auto const& str : expected_re) {
|
for (auto const& str : expected_re) {
|
||||||
|
@ -103,10 +103,10 @@ void do_list_files(reader::filesystem_v2& fs, iolayer const& iol,
|
|||||||
|
|
||||||
auto st = fs.getattr(iv);
|
auto st = fs.getattr(iv);
|
||||||
|
|
||||||
iol.out << fmt::format(
|
iol.out << fmt::format("{3} {4:{0}}/{5:{1}} {6:{2}L} {7:%F %H:%M} {8}\n",
|
||||||
"{3} {4:{0}}/{5:{1}} {6:{2}L} {7:%Y-%m-%d %H:%M} {8}\n", uid_width,
|
uid_width, gid_width, inode_size_width,
|
||||||
gid_width, inode_size_width, iv.mode_string(), iv.getuid(),
|
iv.mode_string(), iv.getuid(), iv.getgid(),
|
||||||
iv.getgid(), st.size(), fmt::localtime(st.mtime()), name);
|
st.size(), safe_localtime(st.mtime()), name);
|
||||||
} else if (!name.empty()) {
|
} else if (!name.empty()) {
|
||||||
iol.out << name << "\n";
|
iol.out << name << "\n";
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user