FileSystem.hxx 3.51 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2019 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 "Traits.hxx"
24
#include "Path.hxx"
25
#include "system/UniqueFileDescriptor.hxx"
26

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

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

35

36 37
class AllocatedPath;

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

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

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

70
#ifndef _WIN32
71

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

84 85
#endif

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

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

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

106
#ifndef _WIN32
107

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

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

123 124
#endif

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

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

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

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

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

#endif