UpdateIO.cxx 2.89 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 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 "UpdateDomain.hxx"
23
#include "db/plugins/simple/Directory.hxx"
24
#include "storage/FileInfo.hxx"
25
#include "storage/StorageInterface.hxx"
26
#include "fs/Traits.hxx"
27
#include "fs/FileSystem.hxx"
28
#include "fs/AllocatedPath.hxx"
29
#include "util/Error.hxx"
30
#include "Log.hxx"
31 32 33 34

#include <errno.h>
#include <unistd.h>

35
bool
36
GetInfo(Storage &storage, const char *uri_utf8, StorageFileInfo &info)
37
{
38 39 40 41 42
	Error error;
	bool success = storage.GetInfo(uri_utf8, true, info, error);
	if (!success)
		LogError(error);
	return success;
43 44
}

45
bool
46
GetInfo(StorageDirectoryReader &reader, StorageFileInfo &info)
47
{
48 49 50 51 52
	Error error;
	bool success = reader.GetInfo(true, info, error);
	if (!success)
		LogError(error);
	return success;
53 54 55
}

bool
56
DirectoryExists(Storage &storage, const Directory &directory)
57
{
58
	StorageFileInfo info;
59
	if (!storage.GetInfo(directory.GetPath(), true, info, IgnoreError()))
60 61
		return false;

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

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

bool
78
directory_child_is_regular(Storage &storage, const Directory &directory,
79 80
			   const char *name_utf8)
{
81
	StorageFileInfo info;
82 83 84
	return GetDirectoryChildInfo(storage, directory, name_utf8, info,
				     IgnoreError()) &&
		info.IsRegular();
85 86 87
}

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

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