2 * @brief Utility functions for testing files.
4 /* Copyright (C) 2012,2018,2022 Olly Betts
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #ifndef XAPIAN_INCLUDED_FILETESTS_H
26 #define XAPIAN_INCLUDED_FILETESTS_H
28 #include "safesysstat.h"
31 #include <type_traits>
33 /** Test if a file exists.
35 * @param path The path to test
37 * @return true if @a path is a regular file, or a symbolic link which
38 * resolves to a regular file.
40 inline bool file_exists(const char * path
) {
42 return stat(path
, &st
) == 0 && S_ISREG(st
.st_mode
);
45 /** Test if a file exists.
47 * @param path The path to test
49 * @return true if @a path is a regular file, or a symbolic link which
50 * resolves to a regular file.
52 inline bool file_exists(const std::string
& path
) {
53 return file_exists(path
.c_str());
56 /** Unsigned return type of file_size() function. */
57 typedef std::make_unsigned_t
<off_t
> file_size_type
;
59 /** Returns the size of a file.
61 * @param path The path to test
63 * errno is set to 0 (upon success), or the error returned by stat(), or
64 * EINVAL (if the path isn't a regular file or a symlink resolving to a
67 * If the file's size is larger than the maximum value off_t can represent,
68 * then stat() will fail with errno=EOVERFLOW, and so will this function.
69 * There doesn't seem to be a way to determine the file size in this case,
70 * short of reading it all. This is only likely if the LFS check in configure
71 * doesn't work out how to enable largefile support.
73 * @return The size of the file, or 0 if it doesn't exist or isn't a file.
74 * The return type is file_size_type.
76 inline file_size_type
file_size(const char* path
) {
78 if (stat(path
, &st
) == 0) {
79 if (S_ISREG(st
.st_mode
)) {
81 return file_size_type(st
.st_size
);
88 /** Returns the size of a file.
90 * @param path The path to test
92 * errno is set to 0 (upon success), or the error returned by stat(), or
93 * EINVAL (if the path isn't a regular file or a symlink resolving to a
96 * If the file's size is larger than the maximum value off_t can represent,
97 * then stat() will fail with errno=EOVERFLOW, and so will this function.
98 * There doesn't seem to be a way to determine the file size in this case,
99 * short of reading it all. This is only likely if the LFS check in configure
100 * doesn't work out how to enable largefile support.
102 * @return The size of the file, or 0 if it doesn't exist or isn't a file.
103 * The return type is file_size_type.
105 inline file_size_type
file_size(const std::string
& path
) {
106 return file_size(path
.c_str());
109 /** Returns the size of a file.
111 * @param fd The file descriptor for the file.
113 * errno is set to 0 (upon success), or the error returned by fstat(), or
114 * EINVAL (if the fd isn't a regular file or a symlink resolving to a
117 * If the file's size is larger than the maximum value off_t can represent,
118 * then stat() will fail with errno=EOVERFLOW, and so will this function.
119 * There doesn't seem to be a way to determine the file size in this case,
120 * short of reading it all. This is only likely if the LFS check in configure
121 * doesn't work out how to enable largefile support.
123 * @return The size of the file, or 0 if it doesn't exist or isn't a file.
124 * The return type is file_size_type.
126 inline file_size_type
file_size(int fd
) {
128 if (fstat(fd
, &st
) == 0) {
129 if (S_ISREG(st
.st_mode
)) {
131 return file_size_type(st
.st_size
);
138 /** Test if a directory exists.
140 * @param path The path to test
142 * @return true if @a path is a directory, or a symbolic link which resolves
145 inline bool dir_exists(const char * path
) {
147 return stat(path
, &st
) == 0 && S_ISDIR(st
.st_mode
);
150 /** Test if a directory exists.
152 * @param path The path to test
154 * @return true if @a path is a directory, or a symbolic link which resolves
157 inline bool dir_exists(const std::string
& path
) {
158 return dir_exists(path
.c_str());
161 /** Test if a path exists.
163 * @param path The path to test
165 * @return true if @a path exists (and is not a dangling symlink).
167 inline bool path_exists(const char * path
) {
169 return stat(path
, &st
) == 0;
172 /** Test if a path exists.
174 * @param path The path to test
176 * @return true if @a path exists (and is not a dangling symlink).
178 inline bool path_exists(const std::string
& path
) {
179 return path_exists(path
.c_str());
182 #endif // XAPIAN_INCLUDED_FILETESTS_H