UpdateSong.cxx 2.86 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"
Max Kellermann's avatar
Max Kellermann committed
22
#include "UpdateIO.hxx"
23
#include "UpdateDomain.hxx"
Max Kellermann's avatar
Max Kellermann committed
24
#include "db/DatabaseLock.hxx"
25 26
#include "db/plugins/simple/Directory.hxx"
#include "db/plugins/simple/Song.hxx"
27
#include "decoder/DecoderList.hxx"
28
#include "storage/FileInfo.hxx"
29
#include "Log.hxx"
30 31 32

#include <unistd.h>

33 34 35
inline void
UpdateWalk::UpdateSongFile2(Directory &directory,
			    const char *name, const char *suffix,
36
			    const FileInfo &info)
37 38
{
	db_lock();
39
	Song *song = directory.FindSong(name);
40 41
	db_unlock();

42
	if (!directory_child_access(storage, directory, name, R_OK)) {
43 44
		FormatError(update_domain,
			    "no read permissions on %s/%s",
45
			    directory.GetPath(), name);
46 47
		if (song != nullptr)
			editor.LockDeleteSong(directory, song);
48 49 50 51

		return;
	}

52
	if (!(song != nullptr && info.mtime == song->mtime &&
53
	      !walk_discard) &&
54
	    UpdateContainerFile(directory, name, suffix, info)) {
55 56
		if (song != nullptr)
			editor.LockDeleteSong(directory, song);
57 58 59 60

		return;
	}

61
	if (song == nullptr) {
62
		FormatDebug(update_domain, "reading %s/%s",
63
			    directory.GetPath(), name);
64
		song = Song::LoadFile(storage, name, directory);
65
		if (song == nullptr) {
66 67
			FormatDebug(update_domain,
				    "ignoring unrecognized file %s/%s",
68
				    directory.GetPath(), name);
69 70 71 72
			return;
		}

		db_lock();
73
		directory.AddSong(song);
74 75 76
		db_unlock();

		modified = true;
77 78
		FormatDefault(update_domain, "added %s/%s",
			      directory.GetPath(), name);
79
	} else if (info.mtime != song->mtime || walk_discard) {
80 81
		FormatDefault(update_domain, "updating %s/%s",
			      directory.GetPath(), name);
82
		if (!song->UpdateFile(storage)) {
83 84
			FormatDebug(update_domain,
				    "deleting unrecognized file %s/%s",
85
				    directory.GetPath(), name);
86
			editor.LockDeleteSong(directory, song);
87 88 89 90 91 92 93
		}

		modified = true;
	}
}

bool
94 95
UpdateWalk::UpdateSongFile(Directory &directory,
			   const char *name, const char *suffix,
96
			   const FileInfo &info)
97
{
98
	if (!decoder_plugins_supports_suffix(suffix))
99 100
		return false;

101
	UpdateSongFile2(directory, name, suffix, info);
102 103
	return true;
}