2 * @brief Utility functions for testing files.
4 /* Copyright (C) 2012,2018 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 "safeerrno.h"
29 #include "safesysstat.h"
32 /** Test if a file exists.
34 * @param path The path to test
36 * @return true if @a path is a regular file, or a symbolic link which
37 * resolves to a regular file.
39 inline bool file_exists(const char * path
) {
41 return stat(path
, &st
) == 0 && S_ISREG(st
.st_mode
);
44 /** Test if a file exists.
46 * @param path The path to test
48 * @return true if @a path is a regular file, or a symbolic link which
49 * resolves to a regular file.
51 inline bool file_exists(const std::string
& path
) {
52 return file_exists(path
.c_str());
55 /** Returns the size of a file.
57 * @param path The path to test
59 * errno is set to 0 (upon success), or the error returned by stat(), or
60 * EINVAL (if the path isn't a regular file or a symlink resolving to a
63 * If the file's size is larger than the maximum value off_t can represent,
64 * then stat() will fail with errno=EOVERFLOW, and so will this function.
65 * There doesn't seem to be a way to determine the file size in this case,
66 * short of reading it all. This is only likely if the LFS check in configure
67 * doesn't work out how to enable largefile support.
69 * @return The size of the file, or 0 if it doesn't exist or isn't a file.
71 inline off_t
file_size(const char * path
) {
73 if (stat(path
, &st
) == 0) {
74 if (S_ISREG(st
.st_mode
)) {
83 /** Returns the size of a file.
85 * @param path The path to test
87 * Note: If the file's size is larger than the maximum value off_t can
88 * represent, then stat() will fail with EOVERFLOW, and so will this
89 * function. There doesn't seem to be a way to determine the file size
90 * in this case, short of reading it all. This is only likely if the LFS
91 * check in configure doesn't work out how to enable largefile support.
93 * @return The size of the file, or 0 if it doesn't exist or isn't a file;
94 * errno is set to 0 (upon success), or the error returned by
95 * stat(), or EINVAL (if the path isn't a regular file or a symlink
96 * resolving to a regular file).
98 inline off_t
file_size(const std::string
& path
) {
99 return file_size(path
.c_str());
102 /** Returns the size of a file.
104 * @param fd The file descriptor for the file.
106 * Note: If the file's size is larger than the maximum value off_t can
107 * represent, then stat() will fail with EOVERFLOW, and so will this
108 * function. There doesn't seem to be a way to determine the file size
109 * in this case, short of reading it all. This is only likely if the LFS
110 * check in configure doesn't work out how to enable largefile support.
112 * @return The size of the file, or 0 if it doesn't exist or isn't a file;
113 * errno is set to 0 (upon success), or the error returned by
114 * stat(), or EINVAL (if the path isn't a regular file or a symlink
115 * resolving to a regular file).
117 inline off_t
file_size(int fd
) {
119 if (fstat(fd
, &st
) == 0) {
120 if (S_ISREG(st
.st_mode
)) {
129 /** Test if a directory exists.
131 * @param path The path to test
133 * @return true if @a path is a directory, or a symbolic link which resolves
136 inline bool dir_exists(const char * path
) {
138 return stat(path
, &st
) == 0 && S_ISDIR(st
.st_mode
);
141 /** Test if a directory exists.
143 * @param path The path to test
145 * @return true if @a path is a directory, or a symbolic link which resolves
148 inline bool dir_exists(const std::string
& path
) {
149 return dir_exists(path
.c_str());
152 /** Test if a path exists.
154 * @param path The path to test
156 * @return true if @a path exists (and is not a dangling symlink).
158 inline bool path_exists(const char * path
) {
160 return stat(path
, &st
) == 0;
163 /** Test if a path exists.
165 * @param path The path to test
167 * @return true if @a path exists (and is not a dangling symlink).
169 inline bool path_exists(const std::string
& path
) {
170 return path_exists(path
.c_str());
173 #endif // XAPIAN_INCLUDED_FILETESTS_H