2012-02-12 17:00:00 +01:00
|
|
|
/*
|
2014-01-13 22:30:36 +01:00
|
|
|
* Copyright (C) 2003-2014 The Music Player Daemon Project
|
2012-02-12 17:00:00 +01:00
|
|
|
* 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 */
|
2013-01-02 19:22:15 +01:00
|
|
|
#include "UpdateIO.hxx"
|
2014-01-24 00:24:43 +01:00
|
|
|
#include "UpdateDomain.hxx"
|
2014-01-24 16:18:50 +01:00
|
|
|
#include "db/Directory.hxx"
|
2013-01-02 22:43:56 +01:00
|
|
|
#include "Mapper.hxx"
|
2013-10-17 21:59:35 +02:00
|
|
|
#include "fs/AllocatedPath.hxx"
|
2013-01-29 17:23:35 +01:00
|
|
|
#include "fs/FileSystem.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2012-02-12 17:00:00 +01:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
int
|
2013-10-19 18:48:38 +02:00
|
|
|
stat_directory(const Directory &directory, struct stat *st)
|
2012-02-12 17:00:00 +01:00
|
|
|
{
|
2013-10-17 21:59:35 +02:00
|
|
|
const auto path_fs = map_directory_fs(directory);
|
2013-01-17 00:56:57 +01:00
|
|
|
if (path_fs.IsNull())
|
2012-02-12 17:00:00 +01:00
|
|
|
return -1;
|
|
|
|
|
2013-02-02 15:25:13 +01:00
|
|
|
if (!StatFile(path_fs, *st)) {
|
|
|
|
int error = errno;
|
|
|
|
const std::string path_utf8 = path_fs.ToUTF8();
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatErrno(update_domain, error,
|
|
|
|
"Failed to stat %s", path_utf8.c_str());
|
2013-02-02 15:25:13 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2012-02-12 17:00:00 +01:00
|
|
|
|
2013-02-02 15:25:13 +01:00
|
|
|
return 0;
|
2012-02-12 17:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2013-10-19 18:48:38 +02:00
|
|
|
stat_directory_child(const Directory &parent, const char *name,
|
2012-02-12 17:00:00 +01:00
|
|
|
struct stat *st)
|
|
|
|
{
|
2013-10-17 21:59:35 +02:00
|
|
|
const auto path_fs = map_directory_child_fs(parent, name);
|
2013-01-17 00:56:57 +01:00
|
|
|
if (path_fs.IsNull())
|
2012-02-12 17:00:00 +01:00
|
|
|
return -1;
|
|
|
|
|
2013-02-02 15:25:13 +01:00
|
|
|
if (!StatFile(path_fs, *st)) {
|
|
|
|
int error = errno;
|
|
|
|
const std::string path_utf8 = path_fs.ToUTF8();
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatErrno(update_domain, error,
|
|
|
|
"Failed to stat %s", path_utf8.c_str());
|
2013-02-02 15:25:13 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2012-02-12 17:00:00 +01:00
|
|
|
|
2013-02-02 15:25:13 +01:00
|
|
|
return 0;
|
2012-02-12 17:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-10-19 18:48:38 +02:00
|
|
|
directory_exists(const Directory &directory)
|
2012-02-12 17:00:00 +01:00
|
|
|
{
|
2013-10-17 21:59:35 +02:00
|
|
|
const auto path_fs = map_directory_fs(directory);
|
2013-01-17 00:56:57 +01:00
|
|
|
if (path_fs.IsNull())
|
2012-02-12 17:00:00 +01:00
|
|
|
/* invalid path: cannot exist */
|
|
|
|
return false;
|
|
|
|
|
2013-10-19 18:48:38 +02:00
|
|
|
return directory.device == DEVICE_INARCHIVE ||
|
|
|
|
directory.device == DEVICE_CONTAINER
|
2013-01-29 17:23:35 +01:00
|
|
|
? FileExists(path_fs)
|
|
|
|
: DirectoryExists(path_fs);
|
2012-02-12 17:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-10-19 18:48:38 +02:00
|
|
|
directory_child_is_regular(const Directory &directory,
|
2012-02-12 17:00:00 +01:00
|
|
|
const char *name_utf8)
|
|
|
|
{
|
2013-10-17 21:59:35 +02:00
|
|
|
const auto path_fs = map_directory_child_fs(directory, name_utf8);
|
2013-01-17 00:56:57 +01:00
|
|
|
if (path_fs.IsNull())
|
2012-02-12 17:00:00 +01:00
|
|
|
return false;
|
|
|
|
|
2013-02-02 15:25:13 +01:00
|
|
|
return FileExists(path_fs);
|
2012-02-12 17:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-10-19 18:48:38 +02:00
|
|
|
directory_child_access(const Directory &directory,
|
2012-02-12 17:00:00 +01:00
|
|
|
const char *name, int mode)
|
|
|
|
{
|
|
|
|
#ifdef WIN32
|
2013-02-02 15:25:13 +01:00
|
|
|
/* CheckAccess() is useless on WIN32 */
|
2012-02-12 17:00:00 +01:00
|
|
|
(void)directory;
|
|
|
|
(void)name;
|
|
|
|
(void)mode;
|
|
|
|
return true;
|
|
|
|
#else
|
2013-10-17 21:59:35 +02:00
|
|
|
const auto path = map_directory_child_fs(directory, name);
|
2013-01-17 00:56:57 +01:00
|
|
|
if (path.IsNull())
|
2012-02-12 17:00:00 +01:00
|
|
|
/* something went wrong, but that isn't a permission
|
|
|
|
problem */
|
|
|
|
return true;
|
|
|
|
|
2013-02-02 15:25:13 +01:00
|
|
|
return CheckAccess(path, mode) || errno != EACCES;
|
2012-02-12 17:00:00 +01:00
|
|
|
#endif
|
|
|
|
}
|