2012-06-13 22:07:15 +02:00
|
|
|
/*
|
2014-01-13 22:30:36 +01:00
|
|
|
* Copyright (C) 2003-2014 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h" /* must be first for large file support */
|
2014-01-29 20:16:43 +01:00
|
|
|
#include "Walk.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "UpdateDomain.hxx"
|
2014-01-24 16:18:50 +01:00
|
|
|
#include "db/DatabaseLock.hxx"
|
|
|
|
#include "db/Directory.hxx"
|
|
|
|
#include "db/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-05 18:22:02 +02:00
|
|
|
#include "tag/TagHandler.hxx"
|
2013-09-05 19:11:50 +02:00
|
|
|
#include "tag/TagBuilder.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
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2012-06-13 22:07:15 +02:00
|
|
|
#include <glib.h>
|
|
|
|
|
2014-01-29 20:16:43 +01:00
|
|
|
Directory *
|
|
|
|
UpdateWalk::MakeDirectoryIfModified(Directory &parent, const char *name,
|
2014-01-29 18:14:57 +01:00
|
|
|
const FileInfo &info)
|
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-01-29 18:14:57 +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);
|
2014-01-29 18:14:57 +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
|
|
|
|
SupportsContainerSuffix(const DecoderPlugin &plugin, const char *suffix)
|
|
|
|
{
|
|
|
|
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,
|
2014-01-29 18:14:57 +01:00
|
|
|
const FileInfo &info)
|
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
|
|
|
|
|
|
|
db_lock();
|
2014-01-29 18:14:57 +01:00
|
|
|
Directory *contdir = MakeDirectoryIfModified(directory, name, info);
|
2013-10-19 18:19:03 +02:00
|
|
|
if (contdir == nullptr) {
|
2012-06-13 22:07:15 +02:00
|
|
|
/* not modified */
|
|
|
|
db_unlock();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
contdir->device = DEVICE_CONTAINER;
|
|
|
|
db_unlock();
|
|
|
|
|
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
|
|
|
|
|
|
|
char *vtrack;
|
|
|
|
unsigned int tnum = 0;
|
2013-09-05 19:11:50 +02:00
|
|
|
TagBuilder tag_builder;
|
2013-10-19 18:48:38 +02:00
|
|
|
while ((vtrack = plugin.container_scan(pathname.c_str(), ++tnum)) != nullptr) {
|
2014-01-19 19:57:27 +01:00
|
|
|
Song *song = Song::NewFile(vtrack, *contdir);
|
2012-06-13 22:07:15 +02:00
|
|
|
|
|
|
|
// shouldn't be necessary but it's there..
|
2014-01-29 18:14:57 +01:00
|
|
|
song->mtime = info.mtime;
|
2012-06-13 22:07:15 +02:00
|
|
|
|
2014-01-29 18:14:57 +01:00
|
|
|
const auto child_path_fs = AllocatedPath::Build(pathname,
|
|
|
|
vtrack);
|
2013-10-21 20:36:34 +02:00
|
|
|
plugin.ScanFile(child_path_fs.c_str(),
|
|
|
|
add_tag_handler, &tag_builder);
|
2013-09-05 19:11:50 +02:00
|
|
|
|
2014-01-18 19:08:39 +01:00
|
|
|
tag_builder.Commit(song->tag);
|
2012-06-13 22:07:15 +02:00
|
|
|
|
|
|
|
db_lock();
|
2013-01-02 23:06:10 +01:00
|
|
|
contdir->AddSong(song);
|
2012-06-13 22:07:15 +02:00
|
|
|
db_unlock();
|
|
|
|
|
|
|
|
modified = true;
|
|
|
|
|
2013-11-04 22:20:11 +01:00
|
|
|
FormatDefault(update_domain, "added %s/%s",
|
|
|
|
directory.GetPath(), vtrack);
|
2012-06-13 22:07:15 +02:00
|
|
|
g_free(vtrack);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tnum == 1) {
|
2014-01-31 22:17:49 +01:00
|
|
|
editor.LockDeleteDirectory(contdir);
|
2012-06-13 22:07:15 +02:00
|
|
|
return false;
|
|
|
|
} else
|
|
|
|
return true;
|
|
|
|
}
|