FileSystem.hxx 3.86 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 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
namespace FOpenMode {
41 42 43
	/**
	 * Open mode for writing text files.
	 */
44
	constexpr PathTraitsFS::const_pointer WriteText = PATH_LITERAL("w");
45 46 47 48

	/**
	 * Open mode for appending text files.
	 */
49
	constexpr PathTraitsFS::const_pointer AppendText = PATH_LITERAL("a");
50 51 52 53 54
}

/**
 * Wrapper for fopen() that uses #Path names.
 */
55
static inline FILE *
56
FOpen(Path file, PathTraitsFS::const_pointer mode)
57
{
58 59 60
#ifdef WIN32
	return _tfopen(file.c_str(), mode);
#else
61
	return fopen(file.c_str(), mode);
62
#endif
63 64 65 66 67
}

/**
 * Wrapper for open_cloexec() that uses #Path names.
 */
68 69
static inline int
OpenFile(Path file, int flags, int mode)
70
{
71 72 73
#ifdef WIN32
	return _topen(file.c_str(), flags, mode);
#else
74
	return open_cloexec(file.c_str(), flags, mode);
75
#endif
76 77 78 79 80
}

/**
 * Wrapper for rename() that uses #Path names.
 */
81 82
static inline bool
RenameFile(Path oldpath, Path newpath)
83
{
84 85 86
#ifdef WIN32
	return _trename(oldpath.c_str(), newpath.c_str()) == 0;
#else
87
	return rename(oldpath.c_str(), newpath.c_str()) == 0;
88
#endif
89 90
}

91 92
#ifndef WIN32

93 94 95
/**
 * Wrapper for stat() that uses #Path names.
 */
96 97
static inline bool
StatFile(Path file, struct stat &buf, bool follow_symlinks = true)
98
{
99 100 101 102
	int ret = follow_symlinks
		? stat(file.c_str(), &buf)
		: lstat(file.c_str(), &buf);
	return ret == 0;
103 104
}

105 106
#endif

107 108 109
/**
 * Wrapper for unlink() that uses #Path names.
 */
110 111
static inline bool
RemoveFile(Path file)
112
{
113 114 115
#ifdef WIN32
	return _tunlink(file.c_str()) == 0;
#else
116
	return unlink(file.c_str()) == 0;
117
#endif
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
 * Checks if #Path exists and is a regular file.
147
 */
148 149
static inline bool
FileExists(Path path, bool follow_symlinks = true)
150
{
151 152 153 154
#ifdef WIN32
	(void)follow_symlinks;

	const auto a = GetFileAttributes(path.c_str());
155 156
	return a != INVALID_FILE_ATTRIBUTES &&
		(a & (FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_DEVICE)) == 0;
157
#else
158
	struct stat buf;
159
	return StatFile(path, buf, follow_symlinks) && S_ISREG(buf.st_mode);
160
#endif
161 162 163
}

/**
164
 * Checks if #Path exists and is a directory.
165
 */
166 167
static inline bool
DirectoryExists(Path path, bool follow_symlinks = true)
168
{
169 170 171 172 173 174
#ifdef WIN32
	(void)follow_symlinks;

	const auto a = GetFileAttributes(path.c_str());
	return a != INVALID_FILE_ATTRIBUTES && (a & FILE_ATTRIBUTE_DIRECTORY);
#else
175
	struct stat buf;
176
	return StatFile(path, buf, follow_symlinks) && S_ISDIR(buf.st_mode);
177
#endif
178 179 180 181 182
}

/**
 * Checks if #Path exists.
 */
183
static inline bool
184
PathExists(Path path)
185
{
186
#ifdef WIN32
187
	return GetFileAttributes(path.c_str()) != INVALID_FILE_ATTRIBUTES;
188 189 190
#else
	return CheckAccess(path, F_OK);
#endif
191 192 193
}

#endif