FileSystem.hxx 3.82 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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 29 30 31 32

#include "Path.hxx"

#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>

33 34
class AllocatedPath;

35
namespace FOpenMode {
36 37 38
	/**
	 * Open mode for reading text files.
	 */
39
	constexpr PathTraitsFS::const_pointer ReadText = "r";
40 41 42 43

	/**
	 * Open mode for reading binary files.
	 */
44
	constexpr PathTraitsFS::const_pointer ReadBinary = "rb";
45 46 47 48

	/**
	 * Open mode for writing text files.
	 */
49
	constexpr PathTraitsFS::const_pointer WriteText = "w";
50 51 52 53

	/**
	 * Open mode for writing binary files.
	 */
54
	constexpr PathTraitsFS::const_pointer WriteBinary = "wb";
55 56 57 58

	/**
	 * Open mode for appending text files.
	 */
59
	constexpr PathTraitsFS::const_pointer AppendText = "a";
60 61 62 63

	/**
	 * Open mode for appending binary files.
	 */
64
	constexpr PathTraitsFS::const_pointer AppendBinary = "ab";
65 66 67 68 69
}

/**
 * Wrapper for fopen() that uses #Path names.
 */
70
static inline FILE *
71
FOpen(Path file, PathTraitsFS::const_pointer mode)
72 73 74 75 76 77 78
{
	return fopen(file.c_str(), mode);
}

/**
 * Wrapper for open_cloexec() that uses #Path names.
 */
79 80
static inline int
OpenFile(Path file, int flags, int mode)
81 82 83 84 85 86 87
{
	return open_cloexec(file.c_str(), flags, mode);
}

/**
 * Wrapper for rename() that uses #Path names.
 */
88 89
static inline bool
RenameFile(Path oldpath, Path newpath)
90
{
91
	return rename(oldpath.c_str(), newpath.c_str()) == 0;
92 93 94 95 96
}

/**
 * Wrapper for stat() that uses #Path names.
 */
97 98
static inline bool
StatFile(Path file, struct stat &buf, bool follow_symlinks = true)
99
{
100 101 102 103 104 105 106 107 108
#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
109 110 111 112 113
}

/**
 * Wrapper for unlink() that uses #Path names.
 */
114 115
static inline bool
RemoveFile(Path file)
116
{
117
	return unlink(file.c_str()) == 0;
118 119 120 121 122
}

/**
 * Wrapper for readlink() that uses #Path names.
 */
123 124
AllocatedPath
ReadLink(Path path);
125

126 127
#ifndef WIN32

128
static inline bool
129
MakeFifo(Path path, mode_t mode)
130 131 132 133
{
	return mkfifo(path.c_str(), mode) == 0;
}

134 135 136
/**
 * Wrapper for access() that uses #Path names.
 */
137 138
static inline bool
CheckAccess(Path path, int mode)
139
{
140
	return access(path.c_str(), mode) == 0;
141 142
}

143 144
#endif

145 146 147 148 149 150 151 152 153 154 155 156 157 158
/**
 * 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
}

159
/**
160
 * Checks if #Path exists and is a regular file.
161
 */
162 163
static inline bool
FileExists(Path path, bool follow_symlinks = true)
164 165
{
	struct stat buf;
166
	return StatFile(path, buf, follow_symlinks) && S_ISREG(buf.st_mode);
167 168 169
}

/**
170
 * Checks if #Path exists and is a directory.
171
 */
172 173
static inline bool
DirectoryExists(Path path, bool follow_symlinks = true)
174 175
{
	struct stat buf;
176
	return StatFile(path, buf, follow_symlinks) && S_ISDIR(buf.st_mode);
177 178 179 180 181
}

/**
 * Checks if #Path exists.
 */
182 183
static inline bool
PathExists(Path path, bool follow_symlinks = true)
184 185
{
	struct stat buf;
186
	return StatFile(path, buf, follow_symlinks);
187 188 189
}

#endif