Walk.hxx 4.98 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 "util/Compiler.h"
26
#include "config.h"
27

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

31
struct StorageFileInfo;
32
struct Directory;
33
struct ArchivePlugin;
34
struct PlaylistPlugin;
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 79 80

private:
	gcc_pure
	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

	void UpdateSongFile2(Directory &directory,
89
			     const char *name, std::string_view suffix,
90
			     const StorageFileInfo &info) noexcept;
91 92

	bool UpdateSongFile(Directory &directory,
93
			    const char *name, std::string_view suffix,
94
			    const StorageFileInfo &info) noexcept;
95 96

	bool UpdateContainerFile(Directory &directory,
97
				 std::string_view name, std::string_view suffix,
98
				 const StorageFileInfo &info) noexcept;
99 100 101


#ifdef ENABLE_ARCHIVE
102
	void UpdateArchiveTree(ArchiveFile &archive, Directory &parent,
103
			       const char *name) noexcept;
104 105

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

109
	void UpdateArchiveFile(Directory &directory, std::string_view name,
110
			       const StorageFileInfo &info,
111
			       const ArchivePlugin &plugin) noexcept;
112 113 114


#else
Rosen Penev's avatar
Rosen Penev committed
115 116
	bool UpdateArchiveFile([[maybe_unused]] Directory &directory,
			       [[maybe_unused]] const char *name,
117
			       [[maybe_unused]] std::string_view suffix,
Rosen Penev's avatar
Rosen Penev committed
118
			       [[maybe_unused]] const StorageFileInfo &info) noexcept {
119 120 121 122
		return false;
	}
#endif

123
	void UpdatePlaylistFile(Directory &parent, std::string_view name,
124 125 126
				const StorageFileInfo &info,
				const PlaylistPlugin &plugin) noexcept;

127
	bool UpdatePlaylistFile(Directory &directory,
128
				std::string_view name, std::string_view suffix,
129
				const StorageFileInfo &info) noexcept;
130 131

	bool UpdateRegularFile(Directory &directory,
132
			       const char *name, const StorageFileInfo &info) noexcept;
133 134

	void UpdateDirectoryChild(Directory &directory,
135
				  const ExcludeList &exclude_list,
136
				  const char *name,
137
				  const StorageFileInfo &info) noexcept;
138

139
	bool UpdateDirectory(Directory &directory,
140
			     const ExcludeList &exclude_list,
141
			     const StorageFileInfo &info) noexcept;
142 143 144

	/**
	 * Create the specified directory object if it does not exist
145
	 * already or if the #StorageFileInfo object indicates that it has been
146 147 148 149
	 * modified since the last update.  Returns nullptr when it
	 * exists already and is unmodified.
	 *
	 * The caller must lock the database.
150 151 152
	 *
	 * @param virtual_device one of the DEVICE_* constants
	 * specifying the kind of virtual directory
153
	 */
154
	Directory *MakeVirtualDirectoryIfModified(Directory &parent,
155
						  std::string_view name,
156 157
						  const StorageFileInfo &info,
						  unsigned virtual_device) noexcept;
158

159
	Directory *LockMakeVirtualDirectoryIfModified(Directory &parent,
160
						      std::string_view name,
161 162 163
						      const StorageFileInfo &info,
						      unsigned virtual_device) noexcept;

164
	Directory *DirectoryMakeChildChecked(Directory &parent,
165
					     const char *uri_utf8,
166
					     std::string_view name_utf8) noexcept;
167

168
	Directory *DirectoryMakeUriParentChecked(Directory &root,
169
						 std::string_view uri) noexcept;
170

171
	void UpdateUri(Directory &root, const char *uri) noexcept;
172 173 174
};

#endif