FileSystem.hxx 3.54 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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 "check.h"
24
#include "Traits.hxx"
25
#include "system/fd_util.h"
26 27 28

#include "Path.hxx"

29
#ifdef _WIN32
30 31 32
#include <fileapi.h>
#endif

33 34 35 36
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>

37

38 39
class AllocatedPath;

40 41 42
/**
 * Wrapper for fopen() that uses #Path names.
 */
43
static inline FILE *
44
FOpen(Path file, PathTraitsFS::const_pointer_type mode)
45
{
46
#ifdef _WIN32
47 48
	return _tfopen(file.c_str(), mode);
#else
49
	return fopen(file.c_str(), mode);
50
#endif
51 52 53 54 55
}

/**
 * Wrapper for open_cloexec() that uses #Path names.
 */
56 57
static inline int
OpenFile(Path file, int flags, int mode)
58
{
59
#ifdef _WIN32
60 61
	return _topen(file.c_str(), flags, mode);
#else
62
	return open_cloexec(file.c_str(), flags, mode);
63
#endif
64 65
}

66
/*
67
 * Wrapper for rename() that uses #Path names.
68 69
 *
 * Throws std::system_error on error.
70
 */
71 72
void
RenameFile(Path oldpath, Path newpath);
73

74
#ifndef _WIN32
75

76 77 78
/**
 * Wrapper for stat() that uses #Path names.
 */
79 80
static inline bool
StatFile(Path file, struct stat &buf, bool follow_symlinks = true)
81
{
82 83 84 85
	int ret = follow_symlinks
		? stat(file.c_str(), &buf)
		: lstat(file.c_str(), &buf);
	return ret == 0;
86 87
}

88 89
#endif

90 91 92 93 94 95 96
/**
 * Truncate a file that exists already.  Throws std::system_error on
 * error.
 */
void
TruncateFile(Path path);

97
/**
98 99
 * Wrapper for unlink() that uses #Path names.  Throws
 * std::system_error on error.
100
 */
101 102
void
RemoveFile(Path path);
103 104 105 106

/**
 * Wrapper for readlink() that uses #Path names.
 */
107 108
AllocatedPath
ReadLink(Path path);
109

110
#ifndef _WIN32
111

112
static inline bool
113
MakeFifo(Path path, mode_t mode)
114 115 116 117
{
	return mkfifo(path.c_str(), mode) == 0;
}

118 119 120
/**
 * Wrapper for access() that uses #Path names.
 */
121 122
static inline bool
CheckAccess(Path path, int mode)
123
{
124
	return access(path.c_str(), mode) == 0;
125 126
}

127 128
#endif

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

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

/**
148
 * Checks if #Path exists and is a directory.
149
 */
150 151
static inline bool
DirectoryExists(Path path, bool follow_symlinks = true)
152
{
153
#ifdef _WIN32
154 155 156 157 158
	(void)follow_symlinks;

	const auto a = GetFileAttributes(path.c_str());
	return a != INVALID_FILE_ATTRIBUTES && (a & FILE_ATTRIBUTE_DIRECTORY);
#else
159
	struct stat buf;
160
	return StatFile(path, buf, follow_symlinks) && S_ISDIR(buf.st_mode);
161
#endif
162 163 164 165 166
}

/**
 * Checks if #Path exists.
 */
167
static inline bool
168
PathExists(Path path)
169
{
170
#ifdef _WIN32
171
	return GetFileAttributes(path.c_str()) != INVALID_FILE_ATTRIBUTES;
172 173 174
#else
	return CheckAccess(path, F_OK);
#endif
175 176 177
}

#endif