mpd/src/UpdateContainer.cxx

128 lines
3.2 KiB
C++
Raw Normal View History

/*
2013-01-02 19:22:15 +01:00
* Copyright (C) 2003-2013 The Music Player Daemon Project
* 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 */
2013-01-02 19:22:15 +01:00
#include "UpdateContainer.hxx"
#include "UpdateInternal.hxx"
#include "UpdateDatabase.hxx"
#include "UpdateDomain.hxx"
2013-01-02 20:56:21 +01:00
#include "DatabaseLock.hxx"
2013-01-02 22:52:08 +01:00
#include "Directory.hxx"
2013-07-28 13:25:12 +02:00
#include "Song.hxx"
2013-07-28 13:18:48 +02:00
#include "DecoderPlugin.hxx"
2013-01-02 22:43:56 +01:00
#include "Mapper.hxx"
#include "fs/AllocatedPath.hxx"
2013-09-05 18:22:02 +02:00
#include "tag/TagHandler.hxx"
#include "tag/TagBuilder.hxx"
#include "Log.hxx"
#include <glib.h>
/**
* Create the specified directory object if it does not exist already
* or if the #stat object indicates that it has been modified since
2013-10-19 18:19:03 +02:00
* the last update. Returns nullptr when it exists already and is
* unmodified.
*
* The caller must lock the database.
*/
static Directory *
2013-10-19 18:48:38 +02:00
make_directory_if_modified(Directory &parent, const char *name,
const struct stat *st)
{
2013-10-19 18:48:38 +02:00
Directory *directory = parent.FindChild(name);
// directory exists already
2013-10-19 18:19:03 +02:00
if (directory != nullptr) {
if (directory->mtime == st->st_mtime && !walk_discard) {
/* not modified */
2013-10-19 18:19:03 +02:00
return nullptr;
}
delete_directory(directory);
modified = true;
}
2013-10-19 18:48:38 +02:00
directory = parent.MakeChild(name);
directory->mtime = st->st_mtime;
return directory;
}
bool
2013-10-19 18:48:38 +02:00
update_container_file(Directory &directory,
const char *name,
const struct stat *st,
const DecoderPlugin &plugin)
{
2013-10-19 18:48:38 +02:00
if (plugin.container_scan == nullptr)
return false;
db_lock();
Directory *contdir = make_directory_if_modified(directory, name, st);
2013-10-19 18:19:03 +02:00
if (contdir == nullptr) {
/* not modified */
db_unlock();
return true;
}
contdir->device = DEVICE_CONTAINER;
db_unlock();
const auto pathname = map_directory_child_fs(directory, name);
char *vtrack;
unsigned int tnum = 0;
TagBuilder tag_builder;
2013-10-19 18:48:38 +02:00
while ((vtrack = plugin.container_scan(pathname.c_str(), ++tnum)) != nullptr) {
2013-07-28 13:25:12 +02:00
Song *song = Song::NewFile(vtrack, contdir);
// shouldn't be necessary but it's there..
song->mtime = st->st_mtime;
const auto child_path_fs =
2013-10-19 18:48:38 +02:00
map_directory_child_fs(*contdir, vtrack);
plugin.ScanFile(child_path_fs.c_str(),
add_tag_handler, &tag_builder);
if (tag_builder.IsDefined())
song->tag = tag_builder.Commit();
else
tag_builder.Clear();
db_lock();
2013-01-02 23:06:10 +01:00
contdir->AddSong(song);
db_unlock();
modified = true;
FormatInfo(update_domain, "added %s/%s",
2013-10-19 18:48:38 +02:00
directory.GetPath(), vtrack);
g_free(vtrack);
}
if (tnum == 1) {
db_lock();
delete_directory(contdir);
db_unlock();
return false;
} else
return true;
}