FileSystem.hxx 3.53 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 = "w";
45 46 47 48

	/**
	 * Open mode for appending text files.
	 */
49
	constexpr PathTraitsFS::const_pointer AppendText = "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 61 62 63
{
	return fopen(file.c_str(), mode);
}

/**
 * Wrapper for open_cloexec() that uses #Path names.
 */
64 65
static inline int
OpenFile(Path file, int flags, int mode)
66 67 68 69 70 71 72
{
	return open_cloexec(file.c_str(), flags, mode);
}

/**
 * Wrapper for rename() that uses #Path names.
 */
73 74
static inline bool
RenameFile(Path oldpath, Path newpath)
75
{
76
	return rename(oldpath.c_str(), newpath.c_str()) == 0;
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
/**
 * Wrapper for unlink() that uses #Path names.
 */
98 99
static inline bool
RemoveFile(Path file)
100
{
101
	return unlink(file.c_str()) == 0;
102 103 104 105 106
}

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

110 111
#ifndef WIN32

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 136 137 138 139 140
#ifdef WIN32
	(void)follow_symlinks;

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

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

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

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

#endif