xapian-core  2.0.0
filetests.h
Go to the documentation of this file.
1 
4 /* Copyright (C) 2012,2018,2022 Olly Betts
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  */
24 
25 #ifndef XAPIAN_INCLUDED_FILETESTS_H
26 #define XAPIAN_INCLUDED_FILETESTS_H
27 
28 #include "safesysstat.h"
29 #include <cerrno>
30 #include <string>
31 #include <type_traits>
32 
40 inline bool file_exists(const char * path) {
41  struct stat st;
42  return stat(path, &st) == 0 && S_ISREG(st.st_mode);
43 }
44 
52 inline bool file_exists(const std::string & path) {
53  return file_exists(path.c_str());
54 }
55 
57 typedef std::make_unsigned_t<off_t> file_size_type;
58 
76 inline file_size_type file_size(const char* path) {
77  struct stat st;
78  if (stat(path, &st) == 0) {
79  if (S_ISREG(st.st_mode)) {
80  errno = 0;
81  return file_size_type(st.st_size);
82  }
83  errno = EINVAL;
84  }
85  return 0;
86 }
87 
105 inline file_size_type file_size(const std::string& path) {
106  return file_size(path.c_str());
107 }
108 
126 inline file_size_type file_size(int fd) {
127  struct stat st;
128  if (fstat(fd, &st) == 0) {
129  if (S_ISREG(st.st_mode)) {
130  errno = 0;
131  return file_size_type(st.st_size);
132  }
133  errno = EINVAL;
134  }
135  return 0;
136 }
137 
145 inline bool dir_exists(const char * path) {
146  struct stat st;
147  return stat(path, &st) == 0 && S_ISDIR(st.st_mode);
148 }
149 
157 inline bool dir_exists(const std::string & path) {
158  return dir_exists(path.c_str());
159 }
160 
167 inline bool path_exists(const char * path) {
168  struct stat st;
169  return stat(path, &st) == 0;
170 }
171 
178 inline bool path_exists(const std::string & path) {
179  return path_exists(path.c_str());
180 }
181 
182 #endif // XAPIAN_INCLUDED_FILETESTS_H
bool dir_exists(const char *path)
Test if a directory exists.
Definition: filetests.h:145
bool path_exists(const char *path)
Test if a path exists.
Definition: filetests.h:167
std::make_unsigned_t< off_t > file_size_type
Unsigned return type of file_size() function.
Definition: filetests.h:57
bool file_exists(const char *path)
Test if a file exists.
Definition: filetests.h:40
file_size_type file_size(const char *path)
Returns the size of a file.
Definition: filetests.h:76
include <sys/stat.h> with portability enhancements
#define S_ISREG(ST_MODE)
Definition: safesysstat.h:59
#define S_ISDIR(ST_MODE)
Definition: safesysstat.h:56