[ci] Test Tcl bindings for dragonfly/freebsd
[xapian.git] / xapian-applications / omega / diritor.h
blobde96acdd08f6936ff78fa9913c4764cc53c06776
1 /** @file
2 * @brief Iterator through entries in a directory.
3 */
4 /* Copyright (C) 2007,2008,2010,2011,2012,2013,2014,2015,2018,2019 Olly Betts
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #ifndef OMEGA_INCLUDED_DIRITOR_H
22 #define OMEGA_INCLUDED_DIRITOR_H
24 #ifndef PACKAGE
25 # error config.h must be included first in each C++ source file
26 #endif
28 #include <cerrno>
29 #include <string>
31 #include "safedirent.h"
32 #include "safefcntl.h"
33 #include "safesysstat.h"
34 #include "safeunistd.h"
36 #include <sys/types.h>
38 #ifndef __WIN32__
39 #include <grp.h> // For getgrgid().
40 #include <pwd.h> // For getpwuid().
41 #endif
43 #include <magic.h>
44 #include <zlib.h>
46 #include "loadfile.h"
47 #include "md5wrap.h"
48 #include "runfilter.h" // For class ReadError.
50 struct FileNotFound { };
52 // Exception to signify changes should be committed, but indexing aborted.
53 class CommitAndExit {
54 std::string msg;
56 public:
57 CommitAndExit(const char * msg_, const std::string & path, int errno_);
58 CommitAndExit(const char * msg_, int errno_);
59 CommitAndExit(const char * msg_, const char * error);
61 const std::string & what() const { return msg; }
64 class DirectoryIterator {
65 #if defined O_NOATIME && O_NOATIME != 0
66 static uid_t euid;
67 #endif
69 static magic_t magic_cookie;
71 std::string path;
72 std::string::size_type path_len;
74 DIR * dir = NULL;
75 struct dirent *entry;
76 struct stat statbuf;
77 bool statbuf_valid;
78 bool follow_symlinks;
79 int fd = -1;
81 void call_stat();
83 void ensure_statbuf_valid() {
84 if (!statbuf_valid) {
85 call_stat();
86 statbuf_valid = true;
90 void build_path();
92 void open_fd();
94 void close_fd();
96 public:
98 explicit DirectoryIterator(bool follow_symlinks_)
99 : follow_symlinks(follow_symlinks_) { }
101 ~DirectoryIterator() {
102 if (dir) closedir(dir);
103 if (fd >= 0) close_fd();
106 /// Start iterating through entries in @a path.
108 // Throws a std::string exception upon failure.
109 void start(const std::string & path);
111 /// Read the next directory entry which doesn't start with ".".
113 // We do this to skip ".", "..", and Unix hidden files.
115 // @return false if there are no more entries.
116 bool next() {
117 if (fd >= 0) close_fd();
118 path.resize(path_len);
119 errno = 0;
120 do {
121 entry = readdir(dir);
122 } while (entry && entry->d_name[0] == '.');
123 statbuf_valid = false;
124 if (entry == NULL && errno != 0) next_failed();
125 return (entry != NULL);
128 [[noreturn]]
129 void next_failed() const;
131 const char * leafname() const { return entry->d_name; }
133 const std::string & pathname() const { return path; }
135 typedef enum { REGULAR_FILE, DIRECTORY, OTHER } type;
137 type get_type() {
138 #ifdef DT_UNKNOWN
139 /* Possible values:
140 * DT_UNKNOWN DT_FIFO DT_CHR DT_DIR DT_BLK DT_REG DT_LNK DT_SOCK DT_WHT
142 switch (entry->d_type) {
143 case DT_UNKNOWN:
144 // The current filing system doesn't support d_type.
145 break;
146 case DT_REG:
147 return REGULAR_FILE;
148 case DT_DIR:
149 return DIRECTORY;
150 #ifdef HAVE_LSTAT
151 case DT_LNK:
152 if (follow_symlinks) break;
153 return OTHER;
154 #endif
155 default:
156 return OTHER;
158 #endif
160 ensure_statbuf_valid();
162 if (S_ISREG(statbuf.st_mode)) return REGULAR_FILE;
163 if (S_ISDIR(statbuf.st_mode)) return DIRECTORY;
164 return OTHER;
167 off_t get_size() {
168 ensure_statbuf_valid();
169 return statbuf.st_size;
172 time_t get_mtime() {
173 ensure_statbuf_valid();
174 return statbuf.st_mtime;
177 time_t get_ctime() {
178 ensure_statbuf_valid();
179 return statbuf.st_ctime;
182 const char * get_owner() {
183 #ifndef __WIN32__
184 ensure_statbuf_valid();
185 struct passwd * pwentry = getpwuid(statbuf.st_uid);
186 return pwentry ? pwentry->pw_name : NULL;
187 #else
188 return NULL;
189 #endif
192 const char * get_group() {
193 #ifndef __WIN32__
194 ensure_statbuf_valid();
195 struct group * grentry = getgrgid(statbuf.st_gid);
196 return grentry ? grentry->gr_name : NULL;
197 #else
198 return NULL;
199 #endif
202 bool is_owner_readable() {
203 ensure_statbuf_valid();
204 #ifndef __WIN32__
205 return (statbuf.st_mode & S_IRUSR);
206 #else
207 return (statbuf.st_mode & S_IREAD);
208 #endif
211 bool is_group_readable() {
212 ensure_statbuf_valid();
213 #ifndef __WIN32__
214 return (statbuf.st_mode & S_IRGRP);
215 #else
216 return false;
217 #endif
220 bool is_other_readable() {
221 ensure_statbuf_valid();
222 #ifndef __WIN32__
223 return (statbuf.st_mode & S_IROTH);
224 #else
225 return false;
226 #endif
229 bool try_noatime() {
230 #if defined O_NOATIME && O_NOATIME != 0
231 if (euid == 0) return true;
232 ensure_statbuf_valid();
233 return statbuf.st_uid == euid;
234 #else
235 return false;
236 #endif
239 std::string get_magic_mimetype();
241 std::string file_to_string() {
242 std::string out;
243 if (!load_file_from_fd(get_fd(), out)) {
244 throw ReadError("loading file failed");
246 return out;
249 std::string gzfile_to_string() {
250 int dup_fd = dup(get_fd());
251 if (fd < 0) {
252 throw ReadError("dup() failed");
254 gzFile zfh = gzdopen(dup_fd, "rb");
255 if (zfh == NULL) {
256 throw ReadError("gzdopen() failed");
258 std::string out;
259 char buf[8192];
260 while (true) {
261 int r = gzread(zfh, buf, sizeof(buf));
262 if (r < 0) {
263 gzclose(zfh);
264 throw ReadError("gzread() failed");
266 out.append(buf, r);
267 if (unsigned(r) < sizeof(buf)) break;
269 gzclose(zfh);
270 return out;
273 int get_fd() {
274 if (fd < 0) {
275 open_fd();
276 } else {
277 if (lseek(fd, 0, SEEK_SET) < 0)
278 throw CommitAndExit("Can't rewind file descriptor", path, errno);
280 return fd;
283 bool md5(std::string& out) {
284 return md5_fd(get_fd(), out);
288 #endif // OMEGA_INCLUDED_DIRITOR_H