UpdateIO.cxx 2.76 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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

29 30
#include <stdexcept>

31 32
#include <errno.h>

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

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

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

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

64 65
	return directory.device == DEVICE_INARCHIVE ||
		directory.device == DEVICE_CONTAINER
66 67 68 69
		? info.IsRegular()
		: info.IsDirectory();
}

70
static StorageFileInfo
71
GetDirectoryChildInfo(Storage &storage, const Directory &directory,
72
		      const char *name_utf8)
73 74 75
{
	const auto uri_utf8 = PathTraitsUTF8::Build(directory.GetPath(),
						    name_utf8);
76
	return storage.GetInfo(uri_utf8.c_str(), true);
77 78 79
}

bool
80
directory_child_is_regular(Storage &storage, const Directory &directory,
81
			   const char *name_utf8) noexcept
82
try {
83 84
	return GetDirectoryChildInfo(storage, directory, name_utf8)
		.IsRegular();
85
} catch (...) {
86
	return false;
87 88 89
}

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

107
	return CheckAccess(path, mode) || errno != EACCES;
108 109
#endif
}