2013-01-20 11:19:53 +01:00
|
|
|
/*
|
2015-01-01 19:48:13 +01:00
|
|
|
* Copyright (C) 2003-2015 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
|
|
|
|
|
|
|
#include "check.h"
|
2013-10-17 23:23:25 +02:00
|
|
|
#include "Traits.hxx"
|
2013-08-07 10:31:31 +02:00
|
|
|
#include "system/fd_util.h"
|
2013-01-20 11:19:53 +01:00
|
|
|
|
|
|
|
#include "Path.hxx"
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2013-10-17 21:59:35 +02:00
|
|
|
class AllocatedPath;
|
|
|
|
|
2013-01-20 11:19:53 +01:00
|
|
|
namespace FOpenMode {
|
2013-10-17 23:23:25 +02:00
|
|
|
/**
|
|
|
|
* Open mode for writing text files.
|
|
|
|
*/
|
2013-12-04 22:53:43 +01:00
|
|
|
constexpr PathTraitsFS::const_pointer WriteText = "w";
|
2013-10-17 23:23:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Open mode for appending text files.
|
|
|
|
*/
|
2013-12-04 22:53:43 +01:00
|
|
|
constexpr PathTraitsFS::const_pointer AppendText = "a";
|
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 *
|
2013-12-04 22:53:43 +01:00
|
|
|
FOpen(Path file, PathTraitsFS::const_pointer mode)
|
2013-01-20 11:19:53 +01:00
|
|
|
{
|
|
|
|
return fopen(file.c_str(), mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper for open_cloexec() that uses #Path names.
|
|
|
|
*/
|
2013-10-17 21:59:35 +02:00
|
|
|
static inline int
|
|
|
|
OpenFile(Path file, int flags, int mode)
|
2013-01-20 11:19:53 +01:00
|
|
|
{
|
|
|
|
return open_cloexec(file.c_str(), flags, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper for rename() that uses #Path names.
|
|
|
|
*/
|
2013-10-17 21:59:35 +02:00
|
|
|
static inline bool
|
|
|
|
RenameFile(Path oldpath, Path newpath)
|
2013-01-20 11:19:53 +01:00
|
|
|
{
|
2013-01-21 18:56:40 +01:00
|
|
|
return rename(oldpath.c_str(), newpath.c_str()) == 0;
|
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
|
|
|
#ifdef WIN32
|
|
|
|
(void)follow_symlinks;
|
|
|
|
return stat(file.c_str(), &buf) == 0;
|
|
|
|
#else
|
|
|
|
int ret = follow_symlinks
|
|
|
|
? stat(file.c_str(), &buf)
|
|
|
|
: lstat(file.c_str(), &buf);
|
|
|
|
return ret == 0;
|
|
|
|
#endif
|
2013-01-20 11:19:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper for unlink() that uses #Path names.
|
|
|
|
*/
|
2013-10-17 21:59:35 +02:00
|
|
|
static inline bool
|
|
|
|
RemoveFile(Path file)
|
2013-01-20 11:19:53 +01:00
|
|
|
{
|
2013-01-21 18:56:40 +01:00
|
|
|
return unlink(file.c_str()) == 0;
|
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
|
|
|
|
2013-08-07 20:31:27 +02:00
|
|
|
#ifndef WIN32
|
|
|
|
|
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-12-05 09:39:19 +01:00
|
|
|
/**
|
|
|
|
* Checks is specified path exists and accessible.
|
|
|
|
*/
|
|
|
|
static inline bool
|
|
|
|
CheckAccess(Path path)
|
|
|
|
{
|
|
|
|
#ifdef WIN32
|
|
|
|
struct stat buf;
|
|
|
|
return StatFile(path, buf);
|
|
|
|
#else
|
|
|
|
return CheckAccess(path, F_OK);
|
|
|
|
#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
|
|
|
{
|
|
|
|
struct stat buf;
|
2013-01-21 18:56:40 +01:00
|
|
|
return StatFile(path, buf, follow_symlinks) && S_ISREG(buf.st_mode);
|
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
|
|
|
{
|
|
|
|
struct stat buf;
|
2013-01-21 18:56:40 +01:00
|
|
|
return StatFile(path, buf, follow_symlinks) && S_ISDIR(buf.st_mode);
|
2013-01-20 11:19:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if #Path exists.
|
|
|
|
*/
|
2013-10-17 21:59:35 +02:00
|
|
|
static inline bool
|
|
|
|
PathExists(Path path, bool follow_symlinks = true)
|
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);
|
2013-01-20 11:19:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|