UpdateIO.cxx 2.71 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
 * 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.
 */

Max Kellermann's avatar
Max Kellermann committed
20
#include "UpdateIO.hxx"
21
#include "db/plugins/simple/Directory.hxx"
22
#include "storage/FileInfo.hxx"
23
#include "storage/StorageInterface.hxx"
24
#include "fs/Traits.hxx"
25
#include "fs/FileSystem.hxx"
26
#include "fs/AllocatedPath.hxx"
27
#include "Log.hxx"
28

Rosen Penev's avatar
Rosen Penev committed
29
#include <cerrno>
30

31
bool
32
GetInfo(Storage &storage, const char *uri_utf8, StorageFileInfo &info) noexcept
33
try {
34 35
	info = storage.GetInfo(uri_utf8, true);
	return true;
36 37
} catch (...) {
	LogError(std::current_exception());
38
	return false;
39 40
}

41
bool
42
GetInfo(StorageDirectoryReader &reader, StorageFileInfo &info) noexcept
43
try {
44 45
	info = reader.GetInfo(true);
	return true;
46 47
} catch (...) {
	LogError(std::current_exception());
48
	return false;
49 50 51
}

bool
52
DirectoryExists(Storage &storage, const Directory &directory) noexcept
53
{
54
	StorageFileInfo info;
55 56

	try {
57
		info = storage.GetInfo(directory.GetPath(), true);
58
	} catch (...) {
59
		return false;
60
	}
61

62
	return directory.IsReallyAFile()
63 64 65 66
		? info.IsRegular()
		: info.IsDirectory();
}

67
static StorageFileInfo
68
GetDirectoryChildInfo(Storage &storage, const Directory &directory,
69
		      std::string_view name_utf8)
70 71 72
{
	const auto uri_utf8 = PathTraitsUTF8::Build(directory.GetPath(),
						    name_utf8);
73
	return storage.GetInfo(uri_utf8.c_str(), true);
74 75 76
}

bool
77
directory_child_is_regular(Storage &storage, const Directory &directory,
78
			   std::string_view name_utf8) noexcept
79
try {
80 81
	return GetDirectoryChildInfo(storage, directory, name_utf8)
		.IsRegular();
82
} catch (...) {
83
	return false;
84 85 86
}

bool
87
directory_child_access(Storage &storage, const Directory &directory,
88
		       std::string_view name, int mode) noexcept
89
{
90
#ifdef _WIN32
91
	/* CheckAccess() is useless on WIN32 */
92
	(void)storage;
93 94 95 96 97
	(void)directory;
	(void)name;
	(void)mode;
	return true;
#else
98
	const auto path = storage.MapChildFS(directory.GetPath(), name);
99
	if (path.IsNull())
100 101
		/* does not point to local file: silently ignore the
		   check */
102 103
		return true;

104
	return CheckAccess(path, mode) || errno != EACCES;
105 106
#endif
}