From 6f37f5752bbf316f6fdfdd0bd25fae432f7e48e6 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sat, 11 Feb 2017 23:09:33 +0100 Subject: [PATCH] db/PlaylistInfo: use std::chrono::system_clock::time_point --- src/PlaylistDatabase.cxx | 17 ++++++++++------- src/PlaylistFile.cxx | 2 +- src/command/OtherCommands.cxx | 3 ++- src/command/PlaylistCommands.cxx | 3 ++- src/db/DatabasePrint.cxx | 3 ++- src/db/PlaylistInfo.hxx | 13 +++++++++---- src/db/plugins/ProxyDatabasePlugin.cxx | 6 +++++- src/db/update/Walk.cxx | 2 +- 8 files changed, 32 insertions(+), 17 deletions(-) diff --git a/src/PlaylistDatabase.cxx b/src/PlaylistDatabase.cxx index 1075d7024..5ee95b3a1 100644 --- a/src/PlaylistDatabase.cxx +++ b/src/PlaylistDatabase.cxx @@ -23,6 +23,7 @@ #include "fs/io/TextFile.hxx" #include "fs/io/BufferedOutputStream.hxx" #include "util/StringStrip.hxx" +#include "util/ChronoUtil.hxx" #include "util/RuntimeError.hxx" #include @@ -31,17 +32,19 @@ void playlist_vector_save(BufferedOutputStream &os, const PlaylistVector &pv) { - for (const PlaylistInfo &pi : pv) - os.Format(PLAYLIST_META_BEGIN "%s\n" - "mtime: %li\n" - "playlist_end\n", - pi.name.c_str(), (long)pi.mtime); + for (const PlaylistInfo &pi : pv) { + os.Format(PLAYLIST_META_BEGIN "%s\n", pi.name.c_str()); + if (!IsNegative(pi.mtime)) + os.Format("mtime: %li\n", + (long)std::chrono::system_clock::to_time_t(pi.mtime)); + os.Write("playlist_end\n"); + } } void playlist_metadata_load(TextFile &file, PlaylistVector &pv, const char *name) { - PlaylistInfo pm(name, 0); + PlaylistInfo pm(name); char *line, *colon; const char *value; @@ -57,7 +60,7 @@ playlist_metadata_load(TextFile &file, PlaylistVector &pv, const char *name) value = StripLeft(colon); if (strcmp(line, "mtime") == 0) - pm.mtime = strtol(value, nullptr, 10); + pm.mtime = std::chrono::system_clock::from_time_t(strtol(value, nullptr, 10)); else throw FormatRuntimeError("unknown line in db: %s", line); diff --git a/src/PlaylistFile.cxx b/src/PlaylistFile.cxx index ec66cacf4..d9846044e 100644 --- a/src/PlaylistFile.cxx +++ b/src/PlaylistFile.cxx @@ -148,7 +148,7 @@ LoadPlaylistFileInfo(PlaylistInfo &info, return false; info.name = std::move(name_utf8); - info.mtime = std::chrono::system_clock::to_time_t(fi.GetModificationTime()); + info.mtime = fi.GetModificationTime(); return true; } diff --git a/src/command/OtherCommands.cxx b/src/command/OtherCommands.cxx index 9d209ce00..ef66cd996 100644 --- a/src/command/OtherCommands.cxx +++ b/src/command/OtherCommands.cxx @@ -35,6 +35,7 @@ #include "decoder/DecoderPrint.hxx" #include "ls.hxx" #include "mixer/Volume.hxx" +#include "util/ChronoUtil.hxx" #include "util/UriUtil.hxx" #include "util/StringAPI.hxx" #include "fs/AllocatedPath.hxx" @@ -63,7 +64,7 @@ print_spl_list(Response &r, const PlaylistVector &list) for (const auto &i : list) { r.Format("playlist: %s\n", i.name.c_str()); - if (i.mtime > 0) + if (!IsNegative(i.mtime)) time_print(r, "Last-Modified", i.mtime); } } diff --git a/src/command/PlaylistCommands.cxx b/src/command/PlaylistCommands.cxx index a8a9d8c57..e96bd2b1d 100644 --- a/src/command/PlaylistCommands.cxx +++ b/src/command/PlaylistCommands.cxx @@ -37,6 +37,7 @@ #include "fs/AllocatedPath.hxx" #include "util/UriUtil.hxx" #include "util/ConstBuffer.hxx" +#include "util/ChronoUtil.hxx" bool playlist_commands_available() noexcept @@ -50,7 +51,7 @@ print_spl_list(Response &r, const PlaylistVector &list) for (const auto &i : list) { r.Format("playlist: %s\n", i.name.c_str()); - if (i.mtime > 0) + if (!IsNegative(i.mtime)) time_print(r, "Last-Modified", i.mtime); } } diff --git a/src/db/DatabasePrint.cxx b/src/db/DatabasePrint.cxx index d00ef1468..9cb772cd5 100644 --- a/src/db/DatabasePrint.cxx +++ b/src/db/DatabasePrint.cxx @@ -34,6 +34,7 @@ #include "PlaylistInfo.hxx" #include "Interface.hxx" #include "fs/Traits.hxx" +#include "util/ChronoUtil.hxx" #include @@ -134,7 +135,7 @@ PrintPlaylistFull(Response &r, bool base, print_playlist_in_directory(r, base, &directory, playlist.name.c_str()); - if (playlist.mtime > 0) + if (!IsNegative(playlist.mtime)) time_print(r, "Last-Modified", playlist.mtime); } diff --git a/src/db/PlaylistInfo.hxx b/src/db/PlaylistInfo.hxx index eacedac58..6874a2b6c 100644 --- a/src/db/PlaylistInfo.hxx +++ b/src/db/PlaylistInfo.hxx @@ -24,8 +24,7 @@ #include "Compiler.h" #include - -#include +#include /** * A directory entry pointing to a playlist file. @@ -36,7 +35,12 @@ struct PlaylistInfo { */ std::string name; - time_t mtime; + /** + * The time stamp of the last file modification. A negative + * value means that this is unknown/unavailable. + */ + std::chrono::system_clock::time_point mtime = + std::chrono::system_clock::time_point::min(); class CompareName { const char *const name; @@ -53,7 +57,8 @@ struct PlaylistInfo { PlaylistInfo() = default; template - PlaylistInfo(N &&_name, time_t _mtime) + explicit PlaylistInfo(N &&_name, + std::chrono::system_clock::time_point _mtime=std::chrono::system_clock::time_point::min()) :name(std::forward(_name)), mtime(_mtime) {} PlaylistInfo(const PlaylistInfo &other) = delete; diff --git a/src/db/plugins/ProxyDatabasePlugin.cxx b/src/db/plugins/ProxyDatabasePlugin.cxx index c2a33e2d1..99c871758 100644 --- a/src/db/plugins/ProxyDatabasePlugin.cxx +++ b/src/db/plugins/ProxyDatabasePlugin.cxx @@ -603,8 +603,12 @@ Visit(const struct mpd_playlist *playlist, if (!visit_playlist) return; + time_t mtime = mpd_playlist_get_last_modified(playlist); + PlaylistInfo p(mpd_playlist_get_path(playlist), - mpd_playlist_get_last_modified(playlist)); + mtime > 0 + ? std::chrono::system_clock::from_time_t(mtime) + : std::chrono::system_clock::time_point::min()); visit_playlist(p, LightDirectory::Root()); } diff --git a/src/db/update/Walk.cxx b/src/db/update/Walk.cxx index 03612d1cc..b18cda30f 100644 --- a/src/db/update/Walk.cxx +++ b/src/db/update/Walk.cxx @@ -192,7 +192,7 @@ UpdateWalk::UpdatePlaylistFile(Directory &directory, if (!playlist_suffix_supported(suffix)) return false; - PlaylistInfo pi(name, std::chrono::system_clock::to_time_t(info.mtime)); + PlaylistInfo pi(name, info.mtime); const ScopeDatabaseLock protect; if (directory.playlists.UpdateOrInsert(std::move(pi)))