FileSystem.hxx 3.24 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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.
 */

20 21
#ifndef MPD_FS_FILESYSTEM_HXX
#define MPD_FS_FILESYSTEM_HXX
22 23

#include "Path.hxx"
24
#include "io/UniqueFileDescriptor.hxx"
25

26
#ifdef _WIN32
27 28 29
#include <fileapi.h>
#endif

30 31
#include <sys/stat.h>
#include <unistd.h>
32

33 34
class AllocatedPath;

35 36 37
/**
 * Wrapper for open_cloexec() that uses #Path names.
 */
38
static inline UniqueFileDescriptor
39
OpenFile(Path file, int flags, int mode)
40
{
41 42 43
	UniqueFileDescriptor fd;
	fd.Open(file.c_str(), flags, mode);
	return fd;
44 45
}

46
/*
47
 * Wrapper for rename() that uses #Path names.
48 49
 *
 * Throws std::system_error on error.
50
 */
51 52
void
RenameFile(Path oldpath, Path newpath);
53

54
#ifndef _WIN32
55

56 57 58
/**
 * Wrapper for stat() that uses #Path names.
 */
59 60
static inline bool
StatFile(Path file, struct stat &buf, bool follow_symlinks = true)
61
{
62 63 64 65
	int ret = follow_symlinks
		? stat(file.c_str(), &buf)
		: lstat(file.c_str(), &buf);
	return ret == 0;
66 67
}

68 69
#endif

70 71 72 73 74 75 76
/**
 * Truncate a file that exists already.  Throws std::system_error on
 * error.
 */
void
TruncateFile(Path path);

77
/**
78 79
 * Wrapper for unlink() that uses #Path names.  Throws
 * std::system_error on error.
80
 */
81 82
void
RemoveFile(Path path);
83 84 85 86

/**
 * Wrapper for readlink() that uses #Path names.
 */
87 88
AllocatedPath
ReadLink(Path path);
89

90
#ifndef _WIN32
91

92
static inline bool
93
MakeFifo(Path path, mode_t mode)
94 95 96 97
{
	return mkfifo(path.c_str(), mode) == 0;
}

98 99 100
/**
 * Wrapper for access() that uses #Path names.
 */
101 102
static inline bool
CheckAccess(Path path, int mode)
103
{
104
	return access(path.c_str(), mode) == 0;
105 106
}

107 108
#endif

109
/**
110
 * Checks if #Path exists and is a regular file.
111
 */
112 113
static inline bool
FileExists(Path path, bool follow_symlinks = true)
114
{
115
#ifdef _WIN32
116 117 118
	(void)follow_symlinks;

	const auto a = GetFileAttributes(path.c_str());
119 120
	return a != INVALID_FILE_ATTRIBUTES &&
		(a & (FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_DEVICE)) == 0;
121
#else
122
	struct stat buf;
123
	return StatFile(path, buf, follow_symlinks) && S_ISREG(buf.st_mode);
124
#endif
125 126 127
}

/**
128
 * Checks if #Path exists and is a directory.
129
 */
130 131
static inline bool
DirectoryExists(Path path, bool follow_symlinks = true)
132
{
133
#ifdef _WIN32
134 135 136 137 138
	(void)follow_symlinks;

	const auto a = GetFileAttributes(path.c_str());
	return a != INVALID_FILE_ATTRIBUTES && (a & FILE_ATTRIBUTE_DIRECTORY);
#else
139
	struct stat buf;
140
	return StatFile(path, buf, follow_symlinks) && S_ISDIR(buf.st_mode);
141
#endif
142 143 144 145 146
}

/**
 * Checks if #Path exists.
 */
147
static inline bool
148
PathExists(Path path)
149
{
150
#ifdef _WIN32
151
	return GetFileAttributes(path.c_str()) != INVALID_FILE_ATTRIBUTES;
152 153 154
#else
	return CheckAccess(path, F_OK);
#endif
155 156 157
}

#endif