Archive.cxx 4.53 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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 */
21
#include "Walk.hxx"
22
#include "UpdateDomain.hxx"
Max Kellermann's avatar
Max Kellermann committed
23
#include "db/DatabaseLock.hxx"
24 25
#include "db/plugins/simple/Directory.hxx"
#include "db/plugins/simple/Song.hxx"
26
#include "storage/StorageInterface.hxx"
27
#include "fs/AllocatedPath.hxx"
28
#include "storage/FileInfo.hxx"
29 30 31 32
#include "archive/ArchiveList.hxx"
#include "archive/ArchivePlugin.hxx"
#include "archive/ArchiveFile.hxx"
#include "archive/ArchiveVisitor.hxx"
33
#include "util/Error.hxx"
34
#include "Log.hxx"
35

36
#include <string>
37

38
#include <sys/stat.h>
39 40
#include <string.h>

41 42
void
UpdateWalk::UpdateArchiveTree(Directory &directory, const char *name)
43
{
44
	const char *tmp = strchr(name, '/');
45
	if (tmp) {
46
		const std::string child_name(name, tmp);
47 48
		//add dir is not there already
		db_lock();
49
		Directory *subdir =
50
			directory.MakeChild(child_name.c_str());
51 52
		subdir->device = DEVICE_INARCHIVE;
		db_unlock();
53

54
		//create directories first
55
		UpdateArchiveTree(*subdir, tmp + 1);
56 57
	} else {
		if (strlen(name) == 0) {
58 59
			LogWarning(update_domain,
				   "archive returned directory only");
60 61 62 63 64
			return;
		}

		//add file
		db_lock();
65
		Song *song = directory.FindSong(name);
66
		db_unlock();
67
		if (song == nullptr) {
68
			song = Song::LoadFile(storage, name, directory);
69
			if (song != nullptr) {
70
				db_lock();
71
				directory.AddSong(song);
72 73 74
				db_unlock();

				modified = true;
75 76
				FormatDefault(update_domain, "added %s/%s",
					      directory.GetPath(), name);
77 78 79 80 81
			}
		}
	}
}

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
class UpdateArchiveVisitor final : public ArchiveVisitor {
	UpdateWalk &walk;
	Directory *directory;

 public:
	UpdateArchiveVisitor(UpdateWalk &_walk, Directory *_directory)
		:walk(_walk), directory(_directory) {}

	virtual void VisitArchiveEntry(const char *path_utf8) override {
		FormatDebug(update_domain,
			    "adding archive file: %s", path_utf8);
		walk.UpdateArchiveTree(*directory, path_utf8);
	}
};

97 98 99 100 101 102 103 104
/**
 * 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
 */
105 106
void
UpdateWalk::UpdateArchiveFile(Directory &parent, const char *name,
107
			      const FileInfo &info,
108
			      const ArchivePlugin &plugin)
109 110
{
	db_lock();
111
	Directory *directory = parent.FindChild(name);
112 113
	db_unlock();

114
	if (directory != nullptr && directory->mtime == info.mtime &&
115 116 117 118 119
	    !walk_discard)
		/* MPD has already scanned the archive, and it hasn't
		   changed since - don't consider updating it */
		return;

120 121 122 123 124
	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;
125 126

	/* open archive */
127
	Error error;
128
	ArchiveFile *file = archive_file_open(&plugin, path_fs, error);
129
	if (file == nullptr) {
130
		LogError(error);
131 132
		if (directory != nullptr)
			editor.LockDeleteDirectory(directory);
133 134 135
		return;
	}

136
	FormatDebug(update_domain, "archive %s opened", path_fs.c_str());
137

138
	if (directory == nullptr) {
139 140
		FormatDebug(update_domain,
			    "creating archive directory: %s", name);
141
		db_lock();
142
		directory = parent.CreateChild(name);
143 144 145 146 147 148
		/* mark this directory as archive (we use device for
		   this) */
		directory->device = DEVICE_INARCHIVE;
		db_unlock();
	}

149
	directory->mtime = info.mtime;
150

151
	UpdateArchiveVisitor visitor(*this, directory);
152 153
	file->Visit(visitor);
	file->Close();
154 155 156
}

bool
157 158
UpdateWalk::UpdateArchiveFile(Directory &directory,
			      const char *name, const char *suffix,
159
			      const FileInfo &info)
160
{
161
	const ArchivePlugin *plugin = archive_plugin_from_suffix(suffix);
162
	if (plugin == nullptr)
163 164
		return false;

165
	UpdateArchiveFile(directory, name, info, *plugin);
166 167
	return true;
}