xapian-core  1.4.19
filetests.h
Go to the documentation of this file.
1 
4 /* Copyright (C) 2012,2018 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 
39 inline bool file_exists(const char * path) {
40  struct stat st;
41  return stat(path, &st) == 0 && S_ISREG(st.st_mode);
42 }
43 
51 inline bool file_exists(const std::string & path) {
52  return file_exists(path.c_str());
53 }
54 
71 inline off_t file_size(const char * path) {
72  struct stat st;
73  if (stat(path, &st) == 0) {
74  if (S_ISREG(st.st_mode)) {
75  errno = 0;
76  return st.st_size;
77  }
78  errno = EINVAL;
79  }
80  return 0;
81 }
82 
98 inline off_t file_size(const std::string & path) {
99  return file_size(path.c_str());
100 }
101 
117 inline off_t file_size(int fd) {
118  struct stat st;
119  if (fstat(fd, &st) == 0) {
120  if (S_ISREG(st.st_mode)) {
121  errno = 0;
122  return st.st_size;
123  }
124  errno = EINVAL;
125  }
126  return 0;
127 }
128 
136 inline bool dir_exists(const char * path) {
137  struct stat st;
138  return stat(path, &st) == 0 && S_ISDIR(st.st_mode);
139 }
140 
148 inline bool dir_exists(const std::string & path) {
149  return dir_exists(path.c_str());
150 }
151 
158 inline bool path_exists(const char * path) {
159  struct stat st;
160  return stat(path, &st) == 0;
161 }
162 
169 inline bool path_exists(const std::string & path) {
170  return path_exists(path.c_str());
171 }
172 
173 #endif // XAPIAN_INCLUDED_FILETESTS_H
#define S_ISDIR(ST_MODE)
Definition: safesysstat.h:57
include <sys/stat.h> with portability enhancements
#define S_ISREG(ST_MODE)
Definition: safesysstat.h:60
bool dir_exists(const char *path)
Test if a directory exists.
Definition: filetests.h:136
off_t file_size(const char *path)
Returns the size of a file.
Definition: filetests.h:71
bool path_exists(const char *path)
Test if a path exists.
Definition: filetests.h:158
bool file_exists(const char *path)
Test if a file exists.
Definition: filetests.h:39