UpdateIO.cxx 2.83 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
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 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 */
Max Kellermann's avatar
Max Kellermann committed
21
#include "UpdateIO.hxx"
22
#include "db/plugins/simple/Directory.hxx"
23
#include "storage/FileInfo.hxx"
24
#include "storage/StorageInterface.hxx"
25
#include "fs/Traits.hxx"
26
#include "fs/FileSystem.hxx"
27
#include "fs/AllocatedPath.hxx"
28
#include "Log.hxx"
29

30 31
#include <stdexcept>

32 33
#include <errno.h>

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

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

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

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

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

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

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

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

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