Walk.hxx 5.32 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * 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.
 */

#ifndef MPD_UPDATE_WALK_HXX
#define MPD_UPDATE_WALK_HXX

23
#include "Config.hxx"
24
#include "Editor.hxx"
25
#include "config.h"
26

27
#include <atomic>
28
#include <string_view>
29

30
struct StorageFileInfo;
31
struct Directory;
32
struct ArchivePlugin;
33
struct PlaylistPlugin;
34
class SongEnumerator;
35
class ArchiveFile;
36
class Storage;
37 38 39 40 41 42 43
class ExcludeList;

class UpdateWalk final {
#ifdef ENABLE_ARCHIVE
	friend class UpdateArchiveVisitor;
#endif

44
	const UpdateConfig config;
45 46 47 48

	bool walk_discard;
	bool modified;

49 50 51 52 53
	/**
	 * Set to true by the main thread when the update thread shall
	 * cancel as quickly as possible.  Access to this flag is
	 * unprotected.
	 */
54
	std::atomic_bool cancel;
55

56
	Storage &storage;
57

58 59 60
	DatabaseEditor editor;

public:
61 62
	UpdateWalk(const UpdateConfig &_config,
		   EventLoop &_loop, DatabaseListener &_listener,
63
		   Storage &_storage) noexcept;
64

65 66 67 68
	/**
	 * Cancel the current update and quit the Walk() method as
	 * soon as possible.
	 */
69
	void Cancel() noexcept {
70 71 72
		cancel = true;
	}

73 74 75
	/**
	 * Returns true if the database was modified.
	 */
76
	bool Walk(Directory &root, const char *path, bool discard) noexcept;
77 78

private:
79
	[[gnu::pure]]
80
	bool SkipSymlink(const Directory *directory,
81
			 std::string_view utf8_name) const noexcept;
82 83

	void RemoveExcludedFromDirectory(Directory &directory,
84
					 const ExcludeList &exclude_list) noexcept;
85

86
	void PurgeDeletedFromDirectory(Directory &directory) noexcept;
87

88 89 90
	/**
	 * Remove all virtual songs inside playlists whose "target"
	 * field points to a non-existing song file.
91 92 93
	 *
	 * It also looks up all target songs and sets their
	 * "in_playlist" field.
94 95 96
	 */
	void PurgeDanglingFromPlaylists(Directory &directory) noexcept;

97
	void UpdateSongFile2(Directory &directory,
98
			     const char *name, std::string_view suffix,
99
			     const StorageFileInfo &info) noexcept;
100 101

	bool UpdateSongFile(Directory &directory,
102
			    const char *name, std::string_view suffix,
103
			    const StorageFileInfo &info) noexcept;
104 105

	bool UpdateContainerFile(Directory &directory,
106
				 std::string_view name, std::string_view suffix,
107
				 const StorageFileInfo &info) noexcept;
108 109 110


#ifdef ENABLE_ARCHIVE
111
	void UpdateArchiveTree(ArchiveFile &archive, Directory &parent,
112
			       const char *name) noexcept;
113 114

	bool UpdateArchiveFile(Directory &directory,
115
			       std::string_view name, std::string_view suffix,
116
			       const StorageFileInfo &info) noexcept;
117

118
	void UpdateArchiveFile(Directory &directory, std::string_view name,
119
			       const StorageFileInfo &info,
120
			       const ArchivePlugin &plugin) noexcept;
121 122 123


#else
Rosen Penev's avatar
Rosen Penev committed
124 125
	bool UpdateArchiveFile([[maybe_unused]] Directory &directory,
			       [[maybe_unused]] const char *name,
126
			       [[maybe_unused]] std::string_view suffix,
Rosen Penev's avatar
Rosen Penev committed
127
			       [[maybe_unused]] const StorageFileInfo &info) noexcept {
128 129 130 131
		return false;
	}
#endif

132 133 134
	void UpdatePlaylistFile(Directory &directory,
				SongEnumerator &contents) noexcept;

135
	void UpdatePlaylistFile(Directory &parent, std::string_view name,
136 137 138
				const StorageFileInfo &info,
				const PlaylistPlugin &plugin) noexcept;

139
	bool UpdatePlaylistFile(Directory &directory,
140
				std::string_view name, std::string_view suffix,
141
				const StorageFileInfo &info) noexcept;
142 143

	bool UpdateRegularFile(Directory &directory,
144
			       const char *name, const StorageFileInfo &info) noexcept;
145 146

	void UpdateDirectoryChild(Directory &directory,
147
				  const ExcludeList &exclude_list,
148
				  const char *name,
149
				  const StorageFileInfo &info) noexcept;
150

151
	bool UpdateDirectory(Directory &directory,
152
			     const ExcludeList &exclude_list,
153
			     const StorageFileInfo &info) noexcept;
154 155 156

	/**
	 * Create the specified directory object if it does not exist
157
	 * already or if the #StorageFileInfo object indicates that it has been
158 159 160 161
	 * modified since the last update.  Returns nullptr when it
	 * exists already and is unmodified.
	 *
	 * The caller must lock the database.
162 163 164
	 *
	 * @param virtual_device one of the DEVICE_* constants
	 * specifying the kind of virtual directory
165
	 */
166
	Directory *MakeVirtualDirectoryIfModified(Directory &parent,
167
						  std::string_view name,
168 169
						  const StorageFileInfo &info,
						  unsigned virtual_device) noexcept;
170

171
	Directory *LockMakeVirtualDirectoryIfModified(Directory &parent,
172
						      std::string_view name,
173 174 175
						      const StorageFileInfo &info,
						      unsigned virtual_device) noexcept;

176
	Directory *DirectoryMakeChildChecked(Directory &parent,
177
					     const char *uri_utf8,
178
					     std::string_view name_utf8) noexcept;
179

180
	Directory *DirectoryMakeUriParentChecked(Directory &root,
181
						 std::string_view uri) noexcept;
182

183
	void UpdateUri(Directory &root, const char *uri) noexcept;
184 185 186
};

#endif