2012-06-13 21:28:26 +02:00
|
|
|
/*
|
2017-01-03 20:48:59 +01:00
|
|
|
* Copyright 2003-2017 The Music Player Daemon Project
|
2012-06-13 21:28:26 +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"
|
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"
|
2013-10-17 21:59:35 +02:00
|
|
|
#include "fs/AllocatedPath.hxx"
|
2014-01-29 18:14:57 +01:00
|
|
|
#include "storage/FileInfo.hxx"
|
2014-01-24 00:09:37 +01:00
|
|
|
#include "archive/ArchiveList.hxx"
|
|
|
|
#include "archive/ArchivePlugin.hxx"
|
|
|
|
#include "archive/ArchiveFile.hxx"
|
|
|
|
#include "archive/ArchiveVisitor.hxx"
|
2015-11-06 09:37:07 +01:00
|
|
|
#include "util/StringCompare.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2012-06-13 21:28:26 +02:00
|
|
|
|
2013-10-15 22:01:46 +02:00
|
|
|
#include <string>
|
2017-12-19 10:56:23 +01:00
|
|
|
#include <exception>
|
2012-06-13 21:28:26 +02:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-12-15 23:27:05 +01:00
|
|
|
static Directory *
|
|
|
|
LockFindChild(Directory &directory, const char *name)
|
|
|
|
{
|
|
|
|
const ScopeDatabaseLock protect;
|
|
|
|
return directory.FindChild(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Directory *
|
|
|
|
LockMakeChild(Directory &directory, const char *name)
|
|
|
|
{
|
|
|
|
const ScopeDatabaseLock protect;
|
|
|
|
return directory.MakeChild(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Song *
|
|
|
|
LockFindSong(Directory &directory, const char *name)
|
|
|
|
{
|
|
|
|
const ScopeDatabaseLock protect;
|
|
|
|
return directory.FindSong(name);
|
|
|
|
}
|
|
|
|
|
2014-01-29 20:16:43 +01:00
|
|
|
void
|
2016-02-26 15:00:41 +01:00
|
|
|
UpdateWalk::UpdateArchiveTree(ArchiveFile &archive, Directory &directory,
|
|
|
|
const char *name)
|
2012-06-13 21:28:26 +02:00
|
|
|
{
|
2013-01-29 21:11:04 +01:00
|
|
|
const char *tmp = strchr(name, '/');
|
2012-06-13 21:28:26 +02:00
|
|
|
if (tmp) {
|
2013-10-15 22:01:46 +02:00
|
|
|
const std::string child_name(name, tmp);
|
2012-06-13 21:28:26 +02:00
|
|
|
//add dir is not there already
|
2015-12-15 23:27:05 +01:00
|
|
|
Directory *subdir = LockMakeChild(directory,
|
|
|
|
child_name.c_str());
|
2012-06-13 21:28:26 +02:00
|
|
|
subdir->device = DEVICE_INARCHIVE;
|
2013-01-29 21:11:04 +01:00
|
|
|
|
2012-06-13 21:28:26 +02:00
|
|
|
//create directories first
|
2016-02-26 15:00:41 +01:00
|
|
|
UpdateArchiveTree(archive, *subdir, tmp + 1);
|
2012-06-13 21:28:26 +02:00
|
|
|
} else {
|
2015-11-06 09:37:07 +01:00
|
|
|
if (StringIsEmpty(name)) {
|
2013-09-27 22:31:24 +02:00
|
|
|
LogWarning(update_domain,
|
|
|
|
"archive returned directory only");
|
2012-06-13 21:28:26 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//add file
|
2015-12-15 23:27:05 +01:00
|
|
|
Song *song = LockFindSong(directory, name);
|
2013-10-19 18:19:03 +02:00
|
|
|
if (song == nullptr) {
|
2016-02-26 15:00:41 +01:00
|
|
|
song = Song::LoadFromArchive(archive, name, directory);
|
2013-10-19 18:19:03 +02:00
|
|
|
if (song != nullptr) {
|
2015-12-15 23:27:05 +01:00
|
|
|
{
|
|
|
|
const ScopeDatabaseLock protect;
|
|
|
|
directory.AddSong(song);
|
|
|
|
}
|
2012-06-13 21:28:26 +02:00
|
|
|
|
|
|
|
modified = true;
|
2013-11-04 22:20:11 +01:00
|
|
|
FormatDefault(update_domain, "added %s/%s",
|
|
|
|
directory.GetPath(), name);
|
2012-06-13 21:28:26 +02:00
|
|
|
}
|
2016-02-26 15:03:58 +01:00
|
|
|
} else {
|
|
|
|
if (!song->UpdateFileInArchive(archive)) {
|
|
|
|
FormatDebug(update_domain,
|
|
|
|
"deleting unrecognized file %s/%s",
|
|
|
|
directory.GetPath(), name);
|
|
|
|
editor.LockDeleteSong(directory, song);
|
|
|
|
}
|
2012-06-13 21:28:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-29 20:16:43 +01:00
|
|
|
class UpdateArchiveVisitor final : public ArchiveVisitor {
|
|
|
|
UpdateWalk &walk;
|
2016-02-26 15:00:41 +01:00
|
|
|
ArchiveFile &archive;
|
2014-01-29 20:16:43 +01:00
|
|
|
Directory *directory;
|
|
|
|
|
|
|
|
public:
|
2016-02-26 15:00:41 +01:00
|
|
|
UpdateArchiveVisitor(UpdateWalk &_walk, ArchiveFile &_archive,
|
|
|
|
Directory *_directory)
|
|
|
|
:walk(_walk), archive(_archive), directory(_directory) {}
|
2014-01-29 20:16:43 +01:00
|
|
|
|
|
|
|
virtual void VisitArchiveEntry(const char *path_utf8) override {
|
|
|
|
FormatDebug(update_domain,
|
|
|
|
"adding archive file: %s", path_utf8);
|
2016-02-26 15:00:41 +01:00
|
|
|
walk.UpdateArchiveTree(archive, *directory, path_utf8);
|
2014-01-29 20:16:43 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-06-13 21:28:26 +02:00
|
|
|
/**
|
|
|
|
* Updates the file listing from an archive file.
|
|
|
|
*
|
|
|
|
* @param parent the parent directory the archive file resides in
|
|
|
|
* @param name the UTF-8 encoded base name of the archive file
|
|
|
|
* @param st stat() information on the archive file
|
|
|
|
* @param plugin the archive plugin which fits this archive type
|
|
|
|
*/
|
2014-01-29 20:16:43 +01:00
|
|
|
void
|
|
|
|
UpdateWalk::UpdateArchiveFile(Directory &parent, const char *name,
|
2015-02-28 20:50:15 +01:00
|
|
|
const StorageFileInfo &info,
|
2014-02-08 13:22:13 +01:00
|
|
|
const ArchivePlugin &plugin)
|
2012-06-13 21:28:26 +02:00
|
|
|
{
|
2015-12-15 23:27:05 +01:00
|
|
|
Directory *directory = LockFindChild(parent, name);
|
2012-06-13 21:28:26 +02:00
|
|
|
|
2017-02-11 23:08:55 +01:00
|
|
|
if (directory != nullptr && directory->mtime == info.mtime &&
|
2012-06-13 21:28:26 +02:00
|
|
|
!walk_discard)
|
|
|
|
/* MPD has already scanned the archive, and it hasn't
|
|
|
|
changed since - don't consider updating it */
|
|
|
|
return;
|
|
|
|
|
2014-01-29 18:14:57 +01:00
|
|
|
const auto path_fs = storage.MapChildFS(parent.GetPath(), name);
|
|
|
|
if (path_fs.IsNull())
|
|
|
|
/* not a local file: skip, because the archive API
|
|
|
|
supports only local files */
|
|
|
|
return;
|
2012-06-13 21:28:26 +02:00
|
|
|
|
|
|
|
/* open archive */
|
2016-09-09 18:34:55 +02:00
|
|
|
ArchiveFile *file;
|
|
|
|
try {
|
|
|
|
file = archive_file_open(&plugin, path_fs);
|
2017-12-19 10:56:23 +01:00
|
|
|
} catch (...) {
|
|
|
|
LogError(std::current_exception());
|
2014-01-31 22:24:28 +01:00
|
|
|
if (directory != nullptr)
|
|
|
|
editor.LockDeleteDirectory(directory);
|
2012-06-13 21:28:26 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatDebug(update_domain, "archive %s opened", path_fs.c_str());
|
2012-06-13 21:28:26 +02:00
|
|
|
|
2013-10-19 18:19:03 +02:00
|
|
|
if (directory == nullptr) {
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatDebug(update_domain,
|
|
|
|
"creating archive directory: %s", name);
|
2015-12-15 23:27:05 +01:00
|
|
|
|
|
|
|
const ScopeDatabaseLock protect;
|
2013-10-19 18:48:38 +02:00
|
|
|
directory = parent.CreateChild(name);
|
2012-06-13 21:28:26 +02:00
|
|
|
/* mark this directory as archive (we use device for
|
|
|
|
this) */
|
|
|
|
directory->device = DEVICE_INARCHIVE;
|
|
|
|
}
|
|
|
|
|
2017-02-11 23:08:55 +01:00
|
|
|
directory->mtime = info.mtime;
|
2012-06-13 21:28:26 +02:00
|
|
|
|
2016-02-26 15:00:41 +01:00
|
|
|
UpdateArchiveVisitor visitor(*this, *file, directory);
|
2013-01-29 23:36:58 +01:00
|
|
|
file->Visit(visitor);
|
2017-12-22 16:30:17 +01:00
|
|
|
delete file;
|
2012-06-13 21:28:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-01-29 20:16:43 +01:00
|
|
|
UpdateWalk::UpdateArchiveFile(Directory &directory,
|
|
|
|
const char *name, const char *suffix,
|
2015-02-28 20:50:15 +01:00
|
|
|
const StorageFileInfo &info)
|
2012-06-13 21:28:26 +02:00
|
|
|
{
|
2014-02-08 13:22:13 +01:00
|
|
|
const ArchivePlugin *plugin = archive_plugin_from_suffix(suffix);
|
2013-10-19 18:19:03 +02:00
|
|
|
if (plugin == nullptr)
|
2012-06-13 21:28:26 +02:00
|
|
|
return false;
|
|
|
|
|
2014-01-29 18:14:57 +01:00
|
|
|
UpdateArchiveFile(directory, name, info, *plugin);
|
2012-06-13 21:28:26 +02:00
|
|
|
return true;
|
|
|
|
}
|