FileSystem.hxx 3.65 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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 30 31 32
#ifdef WIN32
#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 47 48
#ifdef WIN32
	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 60 61
#ifdef WIN32
	return _topen(file.c_str(), flags, mode);
#else
62
	return open_cloexec(file.c_str(), flags, mode);
63
#endif
64 65 66 67 68
}

/**
 * Wrapper for rename() that uses #Path names.
 */
69 70
static inline bool
RenameFile(Path oldpath, Path newpath)
71
{
72 73 74
#ifdef WIN32
	return _trename(oldpath.c_str(), newpath.c_str()) == 0;
#else
75
	return rename(oldpath.c_str(), newpath.c_str()) == 0;
76
#endif
77 78
}

79 80
#ifndef WIN32

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

93 94
#endif

95 96 97 98 99 100 101
/**
 * Truncate a file that exists already.  Throws std::system_error on
 * error.
 */
void
TruncateFile(Path path);

102
/**
103 104
 * Wrapper for unlink() that uses #Path names.  Throws
 * std::system_error on error.
105
 */
106 107
void
RemoveFile(Path path);
108 109 110 111

/**
 * Wrapper for readlink() that uses #Path names.
 */
112 113
AllocatedPath
ReadLink(Path path);
114

115 116
#ifndef WIN32

117
static inline bool
118
MakeFifo(Path path, mode_t mode)
119 120 121 122
{
	return mkfifo(path.c_str(), mode) == 0;
}

123 124 125
/**
 * Wrapper for access() that uses #Path names.
 */
126 127
static inline bool
CheckAccess(Path path, int mode)
128
{
129
	return access(path.c_str(), mode) == 0;
130 131
}

132 133
#endif

134
/**
135
 * Checks if #Path exists and is a regular file.
136
 */
137 138
static inline bool
FileExists(Path path, bool follow_symlinks = true)
139
{
140 141 142 143
#ifdef WIN32
	(void)follow_symlinks;

	const auto a = GetFileAttributes(path.c_str());
144 145
	return a != INVALID_FILE_ATTRIBUTES &&
		(a & (FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_DEVICE)) == 0;
146
#else
147
	struct stat buf;
148
	return StatFile(path, buf, follow_symlinks) && S_ISREG(buf.st_mode);
149
#endif
150 151 152
}

/**
153
 * Checks if #Path exists and is a directory.
154
 */
155 156
static inline bool
DirectoryExists(Path path, bool follow_symlinks = true)
157
{
158 159 160 161 162 163
#ifdef WIN32
	(void)follow_symlinks;

	const auto a = GetFileAttributes(path.c_str());
	return a != INVALID_FILE_ATTRIBUTES && (a & FILE_ATTRIBUTE_DIRECTORY);
#else
164
	struct stat buf;
165
	return StatFile(path, buf, follow_symlinks) && S_ISDIR(buf.st_mode);
166
#endif
167 168 169 170 171
}

/**
 * Checks if #Path exists.
 */
172
static inline bool
173
PathExists(Path path)
174
{
175
#ifdef WIN32
176
	return GetFileAttributes(path.c_str()) != INVALID_FILE_ATTRIBUTES;
177 178 179
#else
	return CheckAccess(path, F_OK);
#endif
180 181 182
}

#endif