Commit 6f37f575 authored by Max Kellermann's avatar Max Kellermann

db/PlaylistInfo: use std::chrono::system_clock::time_point

parent 9d0a71f2
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include "fs/io/TextFile.hxx" #include "fs/io/TextFile.hxx"
#include "fs/io/BufferedOutputStream.hxx" #include "fs/io/BufferedOutputStream.hxx"
#include "util/StringStrip.hxx" #include "util/StringStrip.hxx"
#include "util/ChronoUtil.hxx"
#include "util/RuntimeError.hxx" #include "util/RuntimeError.hxx"
#include <string.h> #include <string.h>
...@@ -31,17 +32,19 @@ ...@@ -31,17 +32,19 @@
void void
playlist_vector_save(BufferedOutputStream &os, const PlaylistVector &pv) playlist_vector_save(BufferedOutputStream &os, const PlaylistVector &pv)
{ {
for (const PlaylistInfo &pi : pv) for (const PlaylistInfo &pi : pv) {
os.Format(PLAYLIST_META_BEGIN "%s\n" os.Format(PLAYLIST_META_BEGIN "%s\n", pi.name.c_str());
"mtime: %li\n" if (!IsNegative(pi.mtime))
"playlist_end\n", os.Format("mtime: %li\n",
pi.name.c_str(), (long)pi.mtime); (long)std::chrono::system_clock::to_time_t(pi.mtime));
os.Write("playlist_end\n");
}
} }
void void
playlist_metadata_load(TextFile &file, PlaylistVector &pv, const char *name) playlist_metadata_load(TextFile &file, PlaylistVector &pv, const char *name)
{ {
PlaylistInfo pm(name, 0); PlaylistInfo pm(name);
char *line, *colon; char *line, *colon;
const char *value; const char *value;
...@@ -57,7 +60,7 @@ playlist_metadata_load(TextFile &file, PlaylistVector &pv, const char *name) ...@@ -57,7 +60,7 @@ playlist_metadata_load(TextFile &file, PlaylistVector &pv, const char *name)
value = StripLeft(colon); value = StripLeft(colon);
if (strcmp(line, "mtime") == 0) 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 else
throw FormatRuntimeError("unknown line in db: %s", throw FormatRuntimeError("unknown line in db: %s",
line); line);
......
...@@ -148,7 +148,7 @@ LoadPlaylistFileInfo(PlaylistInfo &info, ...@@ -148,7 +148,7 @@ LoadPlaylistFileInfo(PlaylistInfo &info,
return false; return false;
info.name = std::move(name_utf8); info.name = std::move(name_utf8);
info.mtime = std::chrono::system_clock::to_time_t(fi.GetModificationTime()); info.mtime = fi.GetModificationTime();
return true; return true;
} }
......
...@@ -35,6 +35,7 @@ ...@@ -35,6 +35,7 @@
#include "decoder/DecoderPrint.hxx" #include "decoder/DecoderPrint.hxx"
#include "ls.hxx" #include "ls.hxx"
#include "mixer/Volume.hxx" #include "mixer/Volume.hxx"
#include "util/ChronoUtil.hxx"
#include "util/UriUtil.hxx" #include "util/UriUtil.hxx"
#include "util/StringAPI.hxx" #include "util/StringAPI.hxx"
#include "fs/AllocatedPath.hxx" #include "fs/AllocatedPath.hxx"
...@@ -63,7 +64,7 @@ print_spl_list(Response &r, const PlaylistVector &list) ...@@ -63,7 +64,7 @@ print_spl_list(Response &r, const PlaylistVector &list)
for (const auto &i : list) { for (const auto &i : list) {
r.Format("playlist: %s\n", i.name.c_str()); r.Format("playlist: %s\n", i.name.c_str());
if (i.mtime > 0) if (!IsNegative(i.mtime))
time_print(r, "Last-Modified", i.mtime); time_print(r, "Last-Modified", i.mtime);
} }
} }
......
...@@ -37,6 +37,7 @@ ...@@ -37,6 +37,7 @@
#include "fs/AllocatedPath.hxx" #include "fs/AllocatedPath.hxx"
#include "util/UriUtil.hxx" #include "util/UriUtil.hxx"
#include "util/ConstBuffer.hxx" #include "util/ConstBuffer.hxx"
#include "util/ChronoUtil.hxx"
bool bool
playlist_commands_available() noexcept playlist_commands_available() noexcept
...@@ -50,7 +51,7 @@ print_spl_list(Response &r, const PlaylistVector &list) ...@@ -50,7 +51,7 @@ print_spl_list(Response &r, const PlaylistVector &list)
for (const auto &i : list) { for (const auto &i : list) {
r.Format("playlist: %s\n", i.name.c_str()); r.Format("playlist: %s\n", i.name.c_str());
if (i.mtime > 0) if (!IsNegative(i.mtime))
time_print(r, "Last-Modified", i.mtime); time_print(r, "Last-Modified", i.mtime);
} }
} }
......
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
#include "PlaylistInfo.hxx" #include "PlaylistInfo.hxx"
#include "Interface.hxx" #include "Interface.hxx"
#include "fs/Traits.hxx" #include "fs/Traits.hxx"
#include "util/ChronoUtil.hxx"
#include <functional> #include <functional>
...@@ -134,7 +135,7 @@ PrintPlaylistFull(Response &r, bool base, ...@@ -134,7 +135,7 @@ PrintPlaylistFull(Response &r, bool base,
print_playlist_in_directory(r, base, print_playlist_in_directory(r, base,
&directory, playlist.name.c_str()); &directory, playlist.name.c_str());
if (playlist.mtime > 0) if (!IsNegative(playlist.mtime))
time_print(r, "Last-Modified", playlist.mtime); time_print(r, "Last-Modified", playlist.mtime);
} }
......
...@@ -24,8 +24,7 @@ ...@@ -24,8 +24,7 @@
#include "Compiler.h" #include "Compiler.h"
#include <string> #include <string>
#include <chrono>
#include <sys/time.h>
/** /**
* A directory entry pointing to a playlist file. * A directory entry pointing to a playlist file.
...@@ -36,7 +35,12 @@ struct PlaylistInfo { ...@@ -36,7 +35,12 @@ struct PlaylistInfo {
*/ */
std::string name; 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 { class CompareName {
const char *const name; const char *const name;
...@@ -53,7 +57,8 @@ struct PlaylistInfo { ...@@ -53,7 +57,8 @@ struct PlaylistInfo {
PlaylistInfo() = default; PlaylistInfo() = default;
template<typename N> template<typename N>
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<N>(_name)), mtime(_mtime) {} :name(std::forward<N>(_name)), mtime(_mtime) {}
PlaylistInfo(const PlaylistInfo &other) = delete; PlaylistInfo(const PlaylistInfo &other) = delete;
......
...@@ -603,8 +603,12 @@ Visit(const struct mpd_playlist *playlist, ...@@ -603,8 +603,12 @@ Visit(const struct mpd_playlist *playlist,
if (!visit_playlist) if (!visit_playlist)
return; return;
time_t mtime = mpd_playlist_get_last_modified(playlist);
PlaylistInfo p(mpd_playlist_get_path(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()); visit_playlist(p, LightDirectory::Root());
} }
......
...@@ -192,7 +192,7 @@ UpdateWalk::UpdatePlaylistFile(Directory &directory, ...@@ -192,7 +192,7 @@ UpdateWalk::UpdatePlaylistFile(Directory &directory,
if (!playlist_suffix_supported(suffix)) if (!playlist_suffix_supported(suffix))
return false; return false;
PlaylistInfo pi(name, std::chrono::system_clock::to_time_t(info.mtime)); PlaylistInfo pi(name, info.mtime);
const ScopeDatabaseLock protect; const ScopeDatabaseLock protect;
if (directory.playlists.UpdateOrInsert(std::move(pi))) if (directory.playlists.UpdateOrInsert(std::move(pi)))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment