SongUpdate.cxx 3.15 KB
Newer Older
1
/*
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
 * 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.
 */

20
#include "config.h" /* must be first for large file support */
21
#include "Song.hxx"
Max Kellermann's avatar
Max Kellermann committed
22
#include "util/UriUtil.hxx"
23
#include "util/Error.hxx"
24
#include "Directory.hxx"
Max Kellermann's avatar
Max Kellermann committed
25
#include "Mapper.hxx"
26
#include "fs/AllocatedPath.hxx"
27
#include "fs/Traits.hxx"
28
#include "fs/FileSystem.hxx"
29
#include "InputStream.hxx"
30
#include "DecoderPlugin.hxx"
31
#include "DecoderList.hxx"
32
#include "tag/Tag.hxx"
33
#include "tag/TagBuilder.hxx"
34
#include "tag/TagHandler.hxx"
35 36
#include "tag/TagId3.hxx"
#include "tag/ApeTag.hxx"
37
#include "TagFile.hxx"
38
#include "thread/Cond.hxx"
39

40
#include <assert.h>
41
#include <string.h>
42 43
#include <sys/types.h>
#include <sys/stat.h>
44
#include <stdio.h>
45

46 47
Song *
Song::LoadFile(const char *path_utf8, Directory *parent)
48
{
49
	Song *song;
50 51
	bool ret;

52
	assert((parent == nullptr) == PathTraits::IsAbsoluteUTF8(path_utf8));
53
	assert(!uri_has_scheme(path_utf8));
54
	assert(strchr(path_utf8, '\n') == nullptr);
55

56
	song = NewFile(path_utf8, parent);
57 58

	//in archive ?
59
	if (parent != nullptr && parent->device == DEVICE_INARCHIVE) {
60
		ret = song->UpdateFileInArchive();
61
	} else {
62
		ret = song->UpdateFile();
63 64
	}
	if (!ret) {
65
		song->Free();
66
		return nullptr;
67 68 69 70 71 72 73 74
	}

	return song;
}

/**
 * Attempts to load APE or ID3 tags from the specified file.
 */
75
static bool
76
tag_scan_fallback(Path path,
77
		  const struct tag_handler *handler, void *handler_ctx)
78
{
79 80
	return tag_ape_scan2(path, handler, handler_ctx) ||
		tag_id3_scan(path, handler, handler_ctx);
81 82 83
}

bool
84
Song::UpdateFile()
85
{
86
	assert(IsFile());
87

88
	const auto path_fs = map_song_fs(*this);
89
	if (path_fs.IsNull())
90 91
		return false;

92 93
	struct stat st;
	if (!StatFile(path_fs, st) || !S_ISREG(st.st_mode))
94
		return false;
95

96
	TagBuilder tag_builder;
97
	if (!tag_file_scan(path_fs,
98
			   &full_tag_handler, &tag_builder))
99 100 101
		return false;

	if (tag_builder.IsEmpty())
102
		tag_scan_fallback(path_fs, &full_tag_handler,
103
				  &tag_builder);
104

105 106 107
	mtime = st.st_mtime;

	delete tag;
108 109
	tag = tag_builder.Commit();
	return true;
110 111 112
}

bool
113
Song::UpdateFileInArchive()
114 115
{
	const char *suffix;
116
	const struct DecoderPlugin *plugin;
117

118
	assert(IsFile());
119 120 121

	/* check if there's a suffix and a plugin */

122
	suffix = uri_get_suffix(uri);
123
	if (suffix == nullptr)
124 125
		return false;

126 127
	plugin = decoder_plugin_from_suffix(suffix, nullptr);
	if (plugin == nullptr)
128 129
		return false;

Max Kellermann's avatar
Max Kellermann committed
130
	delete tag;
131 132

	//accept every file that has music suffix
133
	//because we don't support tag reading through
134
	//input streams
Max Kellermann's avatar
Max Kellermann committed
135
	tag = new Tag();
136 137 138

	return true;
}