2012-06-13 22:07:15 +02:00
|
|
|
/*
|
2018-10-31 17:54:59 +01:00
|
|
|
* Copyright 2003-2018 The Music Player Daemon Project
|
2012-06-13 22:07:15 +02:00
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2014-01-29 20:16:43 +01:00
|
|
|
#include "Walk.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "UpdateDomain.hxx"
|
2018-08-02 13:45:43 +02:00
|
|
|
#include "song/DetachedSong.hxx"
|
2014-01-24 16:18:50 +01:00
|
|
|
#include "db/DatabaseLock.hxx"
|
2014-02-26 09:17:41 +01:00
|
|
|
#include "db/plugins/simple/Directory.hxx"
|
|
|
|
#include "db/plugins/simple/Song.hxx"
|
2014-02-05 19:26:18 +01:00
|
|
|
#include "storage/StorageInterface.hxx"
|
2014-01-24 00:02:24 +01:00
|
|
|
#include "decoder/DecoderPlugin.hxx"
|
|
|
|
#include "decoder/DecoderList.hxx"
|
2013-10-17 21:59:35 +02:00
|
|
|
#include "fs/AllocatedPath.hxx"
|
2014-01-29 18:14:57 +01:00
|
|
|
#include "storage/FileInfo.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2012-06-13 22:07:15 +02:00
|
|
|
|
2014-01-29 20:16:43 +01:00
|
|
|
Directory *
|
|
|
|
UpdateWalk::MakeDirectoryIfModified(Directory &parent, const char *name,
|
2018-01-21 11:22:17 +01:00
|
|
|
const StorageFileInfo &info) noexcept
|
2012-06-13 22:07:15 +02:00
|
|
|
{
|
2013-10-19 18:48:38 +02:00
|
|
|
Directory *directory = parent.FindChild(name);
|
2012-06-13 22:07:15 +02:00
|
|
|
|
|
|
|
// directory exists already
|
2013-10-19 18:19:03 +02:00
|
|
|
if (directory != nullptr) {
|
2014-02-26 08:39:44 +01:00
|
|
|
if (directory->IsMount())
|
|
|
|
return nullptr;
|
|
|
|
|
2017-02-11 23:08:55 +01:00
|
|
|
if (directory->mtime == info.mtime && !walk_discard) {
|
2012-06-13 22:07:15 +02:00
|
|
|
/* not modified */
|
2013-10-19 18:19:03 +02:00
|
|
|
return nullptr;
|
2012-06-13 22:07:15 +02:00
|
|
|
}
|
|
|
|
|
2014-01-29 20:16:43 +01:00
|
|
|
editor.DeleteDirectory(directory);
|
2012-06-13 22:07:15 +02:00
|
|
|
modified = true;
|
|
|
|
}
|
|
|
|
|
2013-10-19 18:48:38 +02:00
|
|
|
directory = parent.MakeChild(name);
|
2017-02-11 23:08:55 +01:00
|
|
|
directory->mtime = info.mtime;
|
2012-06-13 22:07:15 +02:00
|
|
|
return directory;
|
|
|
|
}
|
|
|
|
|
2013-12-29 16:46:02 +01:00
|
|
|
static bool
|
2018-01-21 11:22:17 +01:00
|
|
|
SupportsContainerSuffix(const DecoderPlugin &plugin,
|
|
|
|
const char *suffix) noexcept
|
2013-12-29 16:46:02 +01:00
|
|
|
{
|
|
|
|
return plugin.container_scan != nullptr &&
|
|
|
|
plugin.SupportsSuffix(suffix);
|
|
|
|
}
|
|
|
|
|
2012-06-13 22:07:15 +02:00
|
|
|
bool
|
2014-01-29 20:16:43 +01:00
|
|
|
UpdateWalk::UpdateContainerFile(Directory &directory,
|
|
|
|
const char *name, const char *suffix,
|
2018-01-21 11:22:17 +01:00
|
|
|
const StorageFileInfo &info) noexcept
|
2012-06-13 22:07:15 +02:00
|
|
|
{
|
2013-12-29 16:46:02 +01:00
|
|
|
const DecoderPlugin *_plugin = decoder_plugins_find([suffix](const DecoderPlugin &plugin){
|
|
|
|
return SupportsContainerSuffix(plugin, suffix);
|
|
|
|
});
|
|
|
|
if (_plugin == nullptr)
|
2012-06-13 22:07:15 +02:00
|
|
|
return false;
|
2013-12-29 16:46:02 +01:00
|
|
|
const DecoderPlugin &plugin = *_plugin;
|
2012-06-13 22:07:15 +02:00
|
|
|
|
2015-12-15 23:27:05 +01:00
|
|
|
Directory *contdir;
|
|
|
|
{
|
|
|
|
const ScopeDatabaseLock protect;
|
|
|
|
contdir = MakeDirectoryIfModified(directory, name, info);
|
|
|
|
if (contdir == nullptr)
|
|
|
|
/* not modified */
|
|
|
|
return true;
|
2012-06-13 22:07:15 +02:00
|
|
|
|
2015-12-15 23:27:05 +01:00
|
|
|
contdir->device = DEVICE_CONTAINER;
|
|
|
|
}
|
2012-06-13 22:07:15 +02:00
|
|
|
|
2014-01-29 18:14:57 +01:00
|
|
|
const auto pathname = storage.MapFS(contdir->GetPath());
|
|
|
|
if (pathname.IsNull()) {
|
|
|
|
/* not a local file: skip, because the container API
|
|
|
|
supports only local files */
|
|
|
|
editor.LockDeleteDirectory(contdir);
|
|
|
|
return false;
|
|
|
|
}
|
2012-06-13 22:07:15 +02:00
|
|
|
|
2016-11-22 11:58:18 +01:00
|
|
|
try {
|
2016-11-22 12:25:52 +01:00
|
|
|
auto v = plugin.container_scan(pathname);
|
2016-11-22 11:58:18 +01:00
|
|
|
if (v.empty()) {
|
|
|
|
editor.LockDeleteDirectory(contdir);
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-22 09:33:52 +01:00
|
|
|
|
2016-11-22 12:25:52 +01:00
|
|
|
for (auto &vtrack : v) {
|
|
|
|
Song *song = Song::NewFrom(std::move(vtrack),
|
|
|
|
*contdir);
|
2012-06-13 22:07:15 +02:00
|
|
|
|
2016-11-22 11:58:18 +01:00
|
|
|
// shouldn't be necessary but it's there..
|
2017-01-18 13:19:13 +01:00
|
|
|
song->mtime = info.mtime;
|
2012-06-13 22:07:15 +02:00
|
|
|
|
2016-11-22 12:25:52 +01:00
|
|
|
FormatDefault(update_domain, "added %s/%s",
|
|
|
|
contdir->GetPath(), song->uri);
|
2012-06-13 22:07:15 +02:00
|
|
|
|
2016-11-22 11:58:18 +01:00
|
|
|
{
|
|
|
|
const ScopeDatabaseLock protect;
|
|
|
|
contdir->AddSong(song);
|
|
|
|
}
|
2012-06-13 22:07:15 +02:00
|
|
|
|
2016-11-22 11:58:18 +01:00
|
|
|
modified = true;
|
|
|
|
}
|
2017-12-19 10:56:23 +01:00
|
|
|
} catch (...) {
|
|
|
|
LogError(std::current_exception());
|
2016-11-22 11:58:18 +01:00
|
|
|
editor.LockDeleteDirectory(contdir);
|
|
|
|
return false;
|
2012-06-13 22:07:15 +02:00
|
|
|
}
|
|
|
|
|
2016-11-22 09:33:52 +01:00
|
|
|
return true;
|
2012-06-13 22:07:15 +02:00
|
|
|
}
|