MPD 0.20.6

- Main Page
- Namespaces
- Data Structures
- Files

- File List
- Globals

FileSystem.hxx
Go to the documentation of this file.
1 /*
2 * Copyright 2003-2017 The Music Player Daemon Project
3 * http://www.musicpd.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef MPD_FS_FILESYSTEM_HXX
21 #define MPD_FS_FILESYSTEM_HXX
22
23 #include "check.h"
24 #include "Traits.hxx"
25 #include "system/fd_util.h"
26
27 #include "Path.hxx"
28
29 #ifdef WIN32
30 #include <fileapi.h>
31 #endif
32
33 #include <sys/stat.h>
34 #include <unistd.h>
35 #include <stdio.h>
36
37
38 class AllocatedPath;
39
43 static inline FILE *
44 FOpen(Path file, PathTraitsFS::const_pointer_type mode)
45 {
46 #ifdef WIN32
47 return _tfopen(file.c_str(), mode);
48 #else
49 return fopen(file.c_str(), mode);
50 #endif
51 }
52
56 static inline int
57 OpenFile(Path file, int flags, int mode)
58 {
59 #ifdef WIN32
60 return _topen(file.c_str(), flags, mode);
61 #else
62 return open_cloexec(file.c_str(), flags, mode);
63 #endif
64 }
65
66 /*
67 * Wrapper for rename() that uses #Path names.
68 *
69 * Throws std::system_error on error.
70 */
71 void
72 RenameFile(Path oldpath, Path newpath);
73
74 #ifndef WIN32
75
79 static inline bool
80 StatFile(Path file, struct stat &buf, bool follow_symlinks = true)
81 {
82 int ret = follow_symlinks
83 ? stat(file.c_str(), &buf)
84 : lstat(file.c_str(), &buf);
85 return ret == 0;
86 }
87
88 #endif
89
94 void
95 TruncateFile(Path path);
96
101 void
102 RemoveFile(Path path);
103
107 AllocatedPath
108 ReadLink(Path path);
109
110 #ifndef WIN32
111
112 static inline bool
113 MakeFifo(Path path, mode_t mode)
114 {
115 return mkfifo(path.c_str(), mode) == 0;
116 }
117
121 static inline bool
122 CheckAccess(Path path, int mode)
123 {
124 return access(path.c_str(), mode) == 0;
125 }
126
127 #endif
128
132 static inline bool
133 FileExists(Path path, bool follow_symlinks = true)
134 {
135 #ifdef WIN32
136 (void)follow_symlinks;
137
138 const auto a = GetFileAttributes(path.c_str());
139 return a != INVALID_FILE_ATTRIBUTES &&
140 (a & (FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_DEVICE)) == 0;
141 #else
142 struct stat buf;
143 return StatFile(path, buf, follow_symlinks) && S_ISREG(buf.st_mode);
144 #endif
145 }
146
150 static inline bool
151 DirectoryExists(Path path, bool follow_symlinks = true)
152 {
153 #ifdef WIN32
154 (void)follow_symlinks;
155
156 const auto a = GetFileAttributes(path.c_str());
157 return a != INVALID_FILE_ATTRIBUTES && (a & FILE_ATTRIBUTE_DIRECTORY);
158 #else
159 struct stat buf;
160 return StatFile(path, buf, follow_symlinks) && S_ISDIR(buf.st_mode);
161 #endif
162 }
163
167 static inline bool
168 PathExists(Path path)
169 {
170 #ifdef WIN32
171 return GetFileAttributes(path.c_str()) != INVALID_FILE_ATTRIBUTES;
172 #else
173 return CheckAccess(path, F_OK);
174 #endif
175 }
176
177 #endif
open_cloexec
int open_cloexec(const char *path_fs, int flags, int mode)
Wrapper for open(), which sets the CLOEXEC flag (atomically if supported by the OS).
check.h
Traits.hxx
PathExists
static bool PathExists(Path path)
Checks if Path exists.
Definition: FileSystem.hxx:168
TruncateFile
void TruncateFile(Path path)
Truncate a file that exists already.
FOpen
static FILE * FOpen(Path file, PathTraitsFS::const_pointer_type mode)
Wrapper for fopen() that uses Path names.
Definition: FileSystem.hxx:44
StatFile
static bool StatFile(Path file, struct stat &buf, bool follow_symlinks=true)
Wrapper for stat() that uses Path names.
Definition: FileSystem.hxx:80
AllocatedPath
A path name in the native file system character set.
Definition: AllocatedPath.hxx:38
MakeFifo
static bool MakeFifo(Path path, mode_t mode)
Definition: FileSystem.hxx:113
RemoveFile
void RemoveFile(Path path)
Wrapper for unlink() that uses Path names.
PathTraitsFS::const_pointer_type
Pointer::const_pointer_type const_pointer_type
Definition: Traits.hxx:56
Path
A path name in the native file system character set.
Definition: Path.hxx:39
RenameFile
void RenameFile(Path oldpath, Path newpath)
Path.hxx
ReadLink
AllocatedPath ReadLink(Path path)
Wrapper for readlink() that uses Path names.
DirectoryExists
static bool DirectoryExists(Path path, bool follow_symlinks=true)
Checks if Path exists and is a directory.
Definition: FileSystem.hxx:151
fd_util.h
CheckAccess
static bool CheckAccess(Path path, int mode)
Wrapper for access() that uses Path names.
Definition: FileSystem.hxx:122
Path::c_str
gcc_pure const_pointer_type c_str() const
Returns the value as a const C string.
Definition: Path.hxx:107
OpenFile
static int OpenFile(Path file, int flags, int mode)
Wrapper for open_cloexec() that uses Path names.
Definition: FileSystem.hxx:57
FileExists
static bool FileExists(Path path, bool follow_symlinks=true)
Checks if Path exists and is a regular file.
Definition: FileSystem.hxx:133