FileSystem.hxx 3.53 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 "Path.hxx"
26
#include "system/UniqueFileDescriptor.hxx"
27

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

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

36

37 38
class AllocatedPath;

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

/**
 * Wrapper for open_cloexec() that uses #Path names.
 */
55
static inline UniqueFileDescriptor
56
OpenFile(Path file, int flags, int mode)
57
{
58 59 60
	UniqueFileDescriptor fd;
	fd.Open(file.c_str(), flags, mode);
	return fd;
61 62
}

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

71
#ifndef _WIN32
72

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

85 86
#endif

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

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

/**
 * Wrapper for readlink() that uses #Path names.
 */
104 105
AllocatedPath
ReadLink(Path path);
106

107
#ifndef _WIN32
108

109
static inline bool
110
MakeFifo(Path path, mode_t mode)
111 112 113 114
{
	return mkfifo(path.c_str(), mode) == 0;
}

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

124 125
#endif

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

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

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

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

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

#endif