aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2015-10-20 12:10:42 +0200
committerMax Kellermann <max@duempel.org>2015-10-20 12:10:42 +0200
commitfc2154ee9278ba7e9ccdc82a210806c1d1279150 (patch)
treebe8571a609e5926c93e0ccd65618332ba00945e4
parent76f85e6f7bea6f286389caf96835f2cd61d0961b (diff)
downloadmpd-fc2154ee9278ba7e9ccdc82a210806c1d1279150.tar.gz
mpd-fc2154ee9278ba7e9ccdc82a210806c1d1279150.tar.xz
mpd-fc2154ee9278ba7e9ccdc82a210806c1d1279150.zip
DetachedSong: move code from Update() to LoadFile()
Avoid duplicate AllocatedPath::FromUTF8() invocations in two callers.
-rw-r--r--src/DetachedSong.hxx6
-rw-r--r--src/SongLoader.cxx14
-rw-r--r--src/SongUpdate.cxx36
-rw-r--r--src/command/OtherCommands.cxx2
-rw-r--r--test/test_translate_song.cxx4
5 files changed, 36 insertions, 26 deletions
diff --git a/src/DetachedSong.hxx b/src/DetachedSong.hxx
index c3d9f6289..844283dcf 100644
--- a/src/DetachedSong.hxx
+++ b/src/DetachedSong.hxx
@@ -32,6 +32,7 @@
struct LightSong;
class Storage;
+class Path;
class DetachedSong {
friend DetachedSong DatabaseDetachSong(const Storage &db,
@@ -220,6 +221,11 @@ public:
* @return true on success
*/
bool Update();
+
+ /**
+ * Load #tag and #mtime from a local file.
+ */
+ bool LoadFile(Path path);
};
#endif
diff --git a/src/SongLoader.cxx b/src/SongLoader.cxx
index e66e60e70..5ea8df80c 100644
--- a/src/SongLoader.cxx
+++ b/src/SongLoader.cxx
@@ -54,17 +54,15 @@ SongLoader::LoadFile(const char *path_utf8, Error &error) const
}
#endif
- if (client != nullptr) {
- const auto path_fs = AllocatedPath::FromUTF8(path_utf8, error);
- if (path_fs.IsNull())
- return nullptr;
+ const auto path_fs = AllocatedPath::FromUTF8(path_utf8, error);
+ if (path_fs.IsNull())
+ return nullptr;
- if (!client->AllowFile(path_fs, error))
- return nullptr;
- }
+ if (client != nullptr && !client->AllowFile(path_fs, error))
+ return nullptr;
DetachedSong *song = new DetachedSong(path_utf8);
- if (!song->Update()) {
+ if (!song->LoadFile(path_fs)) {
error.Set(playlist_domain, int(PlaylistResult::NO_SUCH_SONG),
"No such file");
delete song;
diff --git a/src/SongUpdate.cxx b/src/SongUpdate.cxx
index a8b19a8d1..c2d1f5fed 100644
--- a/src/SongUpdate.cxx
+++ b/src/SongUpdate.cxx
@@ -154,27 +154,33 @@ Song::UpdateFileInArchive(const Storage &storage)
#endif
bool
+DetachedSong::LoadFile(Path path)
+{
+ FileInfo fi;
+ if (!GetFileInfo(path, fi) || !fi.IsRegular())
+ return false;
+
+ TagBuilder tag_builder;
+ if (!tag_file_scan(path, full_tag_handler, &tag_builder))
+ return false;
+
+ if (tag_builder.IsEmpty())
+ tag_scan_fallback(path, &full_tag_handler,
+ &tag_builder);
+
+ mtime = fi.GetModificationTime();
+ tag_builder.Commit(tag);
+ return true;
+}
+
+bool
DetachedSong::Update()
{
if (IsAbsoluteFile()) {
const AllocatedPath path_fs =
AllocatedPath::FromUTF8(GetRealURI());
- FileInfo fi;
- if (!GetFileInfo(path_fs, fi) || !fi.IsRegular())
- return false;
-
- TagBuilder tag_builder;
- if (!tag_file_scan(path_fs, full_tag_handler, &tag_builder))
- return false;
-
- if (tag_builder.IsEmpty())
- tag_scan_fallback(path_fs, &full_tag_handler,
- &tag_builder);
-
- mtime = fi.GetModificationTime();
- tag_builder.Commit(tag);
- return true;
+ return LoadFile(path_fs);
} else if (IsRemote()) {
TagBuilder tag_builder;
if (!tag_stream_scan(uri.c_str(), full_tag_handler,
diff --git a/src/command/OtherCommands.cxx b/src/command/OtherCommands.cxx
index d9c198973..596c9c7f6 100644
--- a/src/command/OtherCommands.cxx
+++ b/src/command/OtherCommands.cxx
@@ -176,7 +176,7 @@ handle_lsinfo(Client &client, Request args, Response &r)
return print_error(r, error);
DetachedSong song(path_utf8);
- if (!song.Update()) {
+ if (!song.LoadFile(path_fs)) {
r.Error(ACK_ERROR_NO_EXIST, "No such file");
return CommandResult::ERROR;
}
diff --git a/test/test_translate_song.cxx b/test/test_translate_song.cxx
index 9e627d170..b6be581e4 100644
--- a/test/test_translate_song.cxx
+++ b/test/test_translate_song.cxx
@@ -121,9 +121,9 @@ DatabaseDetachSong(gcc_unused const Database &db,
}
bool
-DetachedSong::Update()
+DetachedSong::LoadFile(Path path)
{
- if (strcmp(GetURI(), uri1) == 0) {
+ if (path.ToUTF8() == uri1) {
SetTag(MakeTag1a());
return true;
}