UpdateContainer.cxx 3.21 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2013 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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 */
Max Kellermann's avatar
Max Kellermann committed
21 22 23
#include "UpdateContainer.hxx"
#include "UpdateInternal.hxx"
#include "UpdateDatabase.hxx"
24
#include "UpdateDomain.hxx"
Max Kellermann's avatar
Max Kellermann committed
25
#include "DatabaseLock.hxx"
26
#include "Directory.hxx"
27
#include "Song.hxx"
28
#include "DecoderPlugin.hxx"
Max Kellermann's avatar
Max Kellermann committed
29
#include "Mapper.hxx"
30
#include "fs/AllocatedPath.hxx"
31
#include "tag/TagHandler.hxx"
32
#include "tag/TagBuilder.hxx"
33
#include "Log.hxx"
34 35 36 37 38 39

#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
40
 * the last update.  Returns nullptr when it exists already and is
41 42 43 44
 * unmodified.
 *
 * The caller must lock the database.
 */
45
static Directory *
46
make_directory_if_modified(Directory &parent, const char *name,
47 48
			   const struct stat *st)
{
49
	Directory *directory = parent.FindChild(name);
50 51

	// directory exists already
52
	if (directory != nullptr) {
53 54
		if (directory->mtime == st->st_mtime && !walk_discard) {
			/* not modified */
55
			return nullptr;
56 57 58 59 60 61
		}

		delete_directory(directory);
		modified = true;
	}

62
	directory = parent.MakeChild(name);
63 64 65 66 67
	directory->mtime = st->st_mtime;
	return directory;
}

bool
68
update_container_file(Directory &directory,
69 70
		      const char *name,
		      const struct stat *st,
71
		      const DecoderPlugin &plugin)
72
{
73
	if (plugin.container_scan == nullptr)
74 75 76
		return false;

	db_lock();
77
	Directory *contdir = make_directory_if_modified(directory, name, st);
78
	if (contdir == nullptr) {
79 80 81 82 83 84 85 86
		/* not modified */
		db_unlock();
		return true;
	}

	contdir->device = DEVICE_CONTAINER;
	db_unlock();

87
	const auto pathname = map_directory_child_fs(directory, name);
88 89 90

	char *vtrack;
	unsigned int tnum = 0;
91
	TagBuilder tag_builder;
92
	while ((vtrack = plugin.container_scan(pathname.c_str(), ++tnum)) != nullptr) {
93
		Song *song = Song::NewFile(vtrack, contdir);
94 95 96 97

		// shouldn't be necessary but it's there..
		song->mtime = st->st_mtime;

98
		const auto child_path_fs =
99
			map_directory_child_fs(*contdir, vtrack);
100

101 102
		plugin.ScanFile(child_path_fs.c_str(),
				add_tag_handler, &tag_builder);
103 104 105 106 107

		if (tag_builder.IsDefined())
			song->tag = tag_builder.Commit();
		else
			tag_builder.Clear();
108 109

		db_lock();
110
		contdir->AddSong(song);
111 112 113 114
		db_unlock();

		modified = true;

115
		FormatInfo(update_domain, "added %s/%s",
116
			   directory.GetPath(), vtrack);
117 118 119 120 121 122 123 124 125 126 127
		g_free(vtrack);
	}

	if (tnum == 1) {
		db_lock();
		delete_directory(contdir);
		db_unlock();
		return false;
	} else
		return true;
}