mpd/src/db/update/UpdateIO.cxx

114 lines
2.8 KiB
C++
Raw Normal View History

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"
#include "fs/AllocatedPath.hxx"
#include "fs/FileSystem.hxx"
#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
{
const auto path_fs = map_directory_fs(directory);
if (path_fs.IsNull())
2012-02-12 17:00:00 +01:00
return -1;
if (!StatFile(path_fs, *st)) {
int error = errno;
const std::string path_utf8 = path_fs.ToUTF8();
FormatErrno(update_domain, error,
"Failed to stat %s", path_utf8.c_str());
return -1;
}
2012-02-12 17:00:00 +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)
{
const auto path_fs = map_directory_child_fs(parent, name);
if (path_fs.IsNull())
2012-02-12 17:00:00 +01:00
return -1;
if (!StatFile(path_fs, *st)) {
int error = errno;
const std::string path_utf8 = path_fs.ToUTF8();
FormatErrno(update_domain, error,
"Failed to stat %s", path_utf8.c_str());
return -1;
}
2012-02-12 17:00:00 +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
{
const auto path_fs = map_directory_fs(directory);
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
? 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)
{
const auto path_fs = map_directory_child_fs(directory, name_utf8);
if (path_fs.IsNull())
2012-02-12 17:00:00 +01:00
return false;
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
/* CheckAccess() is useless on WIN32 */
2012-02-12 17:00:00 +01:00
(void)directory;
(void)name;
(void)mode;
return true;
#else
const auto path = map_directory_child_fs(directory, name);
if (path.IsNull())
2012-02-12 17:00:00 +01:00
/* something went wrong, but that isn't a permission
problem */
return true;
return CheckAccess(path, mode) || errno != EACCES;
2012-02-12 17:00:00 +01:00
#endif
}