2013-01-20 11:19:53 +01:00
|
|
|
/*
|
2019-06-17 11:17:30 +02:00
|
|
|
* Copyright 2003-2019 The Music Player Daemon Project
|
2013-01-20 11:19:53 +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.
|
|
|
|
*/
|
|
|
|
|
2013-01-21 18:56:40 +01:00
|
|
|
#ifndef MPD_FS_FILESYSTEM_HXX
|
|
|
|
#define MPD_FS_FILESYSTEM_HXX
|
2013-01-20 11:19:53 +01:00
|
|
|
|
2013-10-17 23:23:25 +02:00
|
|
|
#include "Traits.hxx"
|
2013-01-20 11:19:53 +01:00
|
|
|
#include "Path.hxx"
|
2017-08-10 19:32:17 +02:00
|
|
|
#include "system/UniqueFileDescriptor.hxx"
|
2013-01-20 11:19:53 +01:00
|
|
|
|
2017-12-12 10:22:20 +01:00
|
|
|
#ifdef _WIN32
|
2015-02-27 19:08:04 +01:00
|
|
|
#include <fileapi.h>
|
|
|
|
#endif
|
|
|
|
|
2013-01-20 11:19:53 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2015-02-27 19:08:04 +01:00
|
|
|
|
2013-10-17 21:59:35 +02:00
|
|
|
class AllocatedPath;
|
|
|
|
|
2013-01-20 11:19:53 +01:00
|
|
|
/**
|
|
|
|
* Wrapper for fopen() that uses #Path names.
|
|
|
|
*/
|
2013-10-17 23:23:25 +02:00
|
|
|
static inline FILE *
|
2016-04-12 22:49:03 +02:00
|
|
|
FOpen(Path file, PathTraitsFS::const_pointer_type mode)
|
2013-01-20 11:19:53 +01:00
|
|
|
{
|
2017-12-12 10:22:20 +01:00
|
|
|
#ifdef _WIN32
|
2015-03-05 08:03:38 +01:00
|
|
|
return _tfopen(file.c_str(), mode);
|
|
|
|
#else
|
2013-01-20 11:19:53 +01:00
|
|
|
return fopen(file.c_str(), mode);
|
2015-03-05 08:03:38 +01:00
|
|
|
#endif
|
2013-01-20 11:19:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper for open_cloexec() that uses #Path names.
|
|
|
|
*/
|
2017-08-10 19:32:17 +02:00
|
|
|
static inline UniqueFileDescriptor
|
2013-10-17 21:59:35 +02:00
|
|
|
OpenFile(Path file, int flags, int mode)
|
2013-01-20 11:19:53 +01:00
|
|
|
{
|
2017-08-10 19:32:17 +02:00
|
|
|
UniqueFileDescriptor fd;
|
|
|
|
fd.Open(file.c_str(), flags, mode);
|
|
|
|
return fd;
|
2013-01-20 11:19:53 +01:00
|
|
|
}
|
|
|
|
|
2016-12-04 19:59:33 +01:00
|
|
|
/*
|
2013-01-20 11:19:53 +01:00
|
|
|
* Wrapper for rename() that uses #Path names.
|
2016-12-04 19:59:33 +01:00
|
|
|
*
|
|
|
|
* Throws std::system_error on error.
|
2013-01-20 11:19:53 +01:00
|
|
|
*/
|
2016-12-04 19:59:33 +01:00
|
|
|
void
|
|
|
|
RenameFile(Path oldpath, Path newpath);
|
2013-01-20 11:19:53 +01:00
|
|
|
|
2017-12-12 10:22:20 +01:00
|
|
|
#ifndef _WIN32
|
2015-02-28 21:21:08 +01:00
|
|
|
|
2013-01-20 11:19:53 +01:00
|
|
|
/**
|
|
|
|
* Wrapper for stat() that uses #Path names.
|
|
|
|
*/
|
2013-10-17 21:59:35 +02:00
|
|
|
static inline bool
|
|
|
|
StatFile(Path file, struct stat &buf, bool follow_symlinks = true)
|
2013-01-20 11:19:53 +01:00
|
|
|
{
|
2013-01-21 18:56:40 +01:00
|
|
|
int ret = follow_symlinks
|
|
|
|
? stat(file.c_str(), &buf)
|
|
|
|
: lstat(file.c_str(), &buf);
|
|
|
|
return ret == 0;
|
2013-01-20 11:19:53 +01:00
|
|
|
}
|
|
|
|
|
2015-02-28 21:21:08 +01:00
|
|
|
#endif
|
|
|
|
|
2016-08-15 22:13:38 +02:00
|
|
|
/**
|
|
|
|
* Truncate a file that exists already. Throws std::system_error on
|
|
|
|
* error.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
TruncateFile(Path path);
|
|
|
|
|
2013-01-20 11:19:53 +01:00
|
|
|
/**
|
2016-08-15 22:25:15 +02:00
|
|
|
* Wrapper for unlink() that uses #Path names. Throws
|
|
|
|
* std::system_error on error.
|
2013-01-20 11:19:53 +01:00
|
|
|
*/
|
2016-08-15 22:25:15 +02:00
|
|
|
void
|
|
|
|
RemoveFile(Path path);
|
2013-01-20 11:19:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper for readlink() that uses #Path names.
|
|
|
|
*/
|
2013-10-17 21:59:35 +02:00
|
|
|
AllocatedPath
|
|
|
|
ReadLink(Path path);
|
2013-01-20 11:19:53 +01:00
|
|
|
|
2017-12-12 10:22:20 +01:00
|
|
|
#ifndef _WIN32
|
2013-08-07 20:31:27 +02:00
|
|
|
|
2013-08-07 19:54:38 +02:00
|
|
|
static inline bool
|
2013-10-17 21:59:35 +02:00
|
|
|
MakeFifo(Path path, mode_t mode)
|
2013-08-07 19:54:38 +02:00
|
|
|
{
|
|
|
|
return mkfifo(path.c_str(), mode) == 0;
|
|
|
|
}
|
|
|
|
|
2013-01-20 11:19:53 +01:00
|
|
|
/**
|
|
|
|
* Wrapper for access() that uses #Path names.
|
|
|
|
*/
|
2013-10-17 21:59:35 +02:00
|
|
|
static inline bool
|
|
|
|
CheckAccess(Path path, int mode)
|
2013-01-20 11:19:53 +01:00
|
|
|
{
|
2013-01-21 18:56:40 +01:00
|
|
|
return access(path.c_str(), mode) == 0;
|
2013-01-20 11:19:53 +01:00
|
|
|
}
|
|
|
|
|
2013-12-05 09:44:25 +01:00
|
|
|
#endif
|
|
|
|
|
2013-01-20 11:19:53 +01:00
|
|
|
/**
|
2013-01-21 18:56:40 +01:00
|
|
|
* Checks if #Path exists and is a regular file.
|
2013-01-20 11:19:53 +01:00
|
|
|
*/
|
2013-10-17 21:59:35 +02:00
|
|
|
static inline bool
|
|
|
|
FileExists(Path path, bool follow_symlinks = true)
|
2013-01-20 11:19:53 +01:00
|
|
|
{
|
2017-12-12 10:22:20 +01:00
|
|
|
#ifdef _WIN32
|
2015-02-27 19:08:04 +01:00
|
|
|
(void)follow_symlinks;
|
|
|
|
|
|
|
|
const auto a = GetFileAttributes(path.c_str());
|
2015-03-05 09:43:51 +01:00
|
|
|
return a != INVALID_FILE_ATTRIBUTES &&
|
|
|
|
(a & (FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_DEVICE)) == 0;
|
2015-02-27 19:08:04 +01:00
|
|
|
#else
|
2013-01-20 11:19:53 +01:00
|
|
|
struct stat buf;
|
2013-01-21 18:56:40 +01:00
|
|
|
return StatFile(path, buf, follow_symlinks) && S_ISREG(buf.st_mode);
|
2015-02-27 19:08:04 +01:00
|
|
|
#endif
|
2013-01-20 11:19:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-01-21 18:56:40 +01:00
|
|
|
* Checks if #Path exists and is a directory.
|
2013-01-20 11:19:53 +01:00
|
|
|
*/
|
2013-10-17 21:59:35 +02:00
|
|
|
static inline bool
|
|
|
|
DirectoryExists(Path path, bool follow_symlinks = true)
|
2013-01-20 11:19:53 +01:00
|
|
|
{
|
2017-12-12 10:22:20 +01:00
|
|
|
#ifdef _WIN32
|
2015-02-27 19:08:04 +01:00
|
|
|
(void)follow_symlinks;
|
|
|
|
|
|
|
|
const auto a = GetFileAttributes(path.c_str());
|
|
|
|
return a != INVALID_FILE_ATTRIBUTES && (a & FILE_ATTRIBUTE_DIRECTORY);
|
|
|
|
#else
|
2013-01-20 11:19:53 +01:00
|
|
|
struct stat buf;
|
2013-01-21 18:56:40 +01:00
|
|
|
return StatFile(path, buf, follow_symlinks) && S_ISDIR(buf.st_mode);
|
2015-02-27 19:08:04 +01:00
|
|
|
#endif
|
2013-01-20 11:19:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if #Path exists.
|
|
|
|
*/
|
2013-10-17 21:59:35 +02:00
|
|
|
static inline bool
|
2015-02-27 19:13:46 +01:00
|
|
|
PathExists(Path path)
|
2013-01-20 11:19:53 +01:00
|
|
|
{
|
2017-12-12 10:22:20 +01:00
|
|
|
#ifdef _WIN32
|
2015-02-27 19:08:04 +01:00
|
|
|
return GetFileAttributes(path.c_str()) != INVALID_FILE_ATTRIBUTES;
|
2015-02-27 19:13:46 +01:00
|
|
|
#else
|
|
|
|
return CheckAccess(path, F_OK);
|
|
|
|
#endif
|
2013-01-20 11:19:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|