2 * @brief Iterator through entries in a directory.
4 /* Copyright (C) 2007,2008,2010,2011,2012,2013,2014,2015,2018 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
25 # error config.h must be included first in each C++ source file
31 #include "safedirent.h"
32 #include "safefcntl.h"
33 #include "safesysstat.h"
34 #include "safeunistd.h"
36 #include <sys/types.h>
39 #include <grp.h> // For getgrgid().
40 #include <pwd.h> // For getpwuid().
46 #include "common/noreturn.h"
50 #include "runfilter.h" // For class ReadError.
52 struct FileNotFound
{ };
54 // Exception to signify changes should be committed, but indexing aborted.
59 CommitAndExit(const char * msg_
, const std::string
& path
, int errno_
);
60 CommitAndExit(const char * msg_
, int errno_
);
61 CommitAndExit(const char * msg_
, const char * error
);
63 const std::string
& what() const { return msg
; }
66 class DirectoryIterator
{
67 #if defined O_NOATIME && O_NOATIME != 0
71 static magic_t magic_cookie
;
74 std::string::size_type path_len
;
85 void ensure_statbuf_valid() {
100 explicit DirectoryIterator(bool follow_symlinks_
)
101 : dir(NULL
), follow_symlinks(follow_symlinks_
), fd(-1) { }
103 ~DirectoryIterator() {
104 if (dir
) closedir(dir
);
105 if (fd
>= 0) close_fd();
108 /// Start iterating through entries in @a path.
110 // Throws a std::string exception upon failure.
111 void start(const std::string
& path
);
113 /// Read the next directory entry which doesn't start with ".".
115 // We do this to skip ".", "..", and Unix hidden files.
117 // @return false if there are no more entries.
119 if (fd
>= 0) close_fd();
120 path
.resize(path_len
);
123 entry
= readdir(dir
);
124 } while (entry
&& entry
->d_name
[0] == '.');
125 statbuf_valid
= false;
126 if (entry
== NULL
&& errno
!= 0) next_failed();
127 return (entry
!= NULL
);
130 XAPIAN_NORETURN(void next_failed() const);
132 const char * leafname() const { return entry
->d_name
; }
134 const std::string
& pathname() const { return path
; }
136 typedef enum { REGULAR_FILE
, DIRECTORY
, OTHER
} type
;
141 * DT_UNKNOWN DT_FIFO DT_CHR DT_DIR DT_BLK DT_REG DT_LNK DT_SOCK DT_WHT
143 switch (entry
->d_type
) {
145 // The current filing system doesn't support d_type.
153 if (follow_symlinks
) break;
161 ensure_statbuf_valid();
163 if (S_ISREG(statbuf
.st_mode
)) return REGULAR_FILE
;
164 if (S_ISDIR(statbuf
.st_mode
)) return DIRECTORY
;
169 ensure_statbuf_valid();
170 return statbuf
.st_size
;
174 ensure_statbuf_valid();
175 return statbuf
.st_mtime
;
179 ensure_statbuf_valid();
180 return statbuf
.st_ctime
;
183 const char * get_owner() {
185 ensure_statbuf_valid();
186 struct passwd
* pwentry
= getpwuid(statbuf
.st_uid
);
187 return pwentry
? pwentry
->pw_name
: NULL
;
193 const char * get_group() {
195 ensure_statbuf_valid();
196 struct group
* grentry
= getgrgid(statbuf
.st_gid
);
197 return grentry
? grentry
->gr_name
: NULL
;
203 bool is_owner_readable() {
204 ensure_statbuf_valid();
206 return (statbuf
.st_mode
& S_IRUSR
);
208 return (statbuf
.st_mode
& S_IREAD
);
212 bool is_group_readable() {
213 ensure_statbuf_valid();
215 return (statbuf
.st_mode
& S_IRGRP
);
221 bool is_other_readable() {
222 ensure_statbuf_valid();
224 return (statbuf
.st_mode
& S_IROTH
);
231 #if defined O_NOATIME && O_NOATIME != 0
232 if (euid
== 0) return true;
233 ensure_statbuf_valid();
234 return statbuf
.st_uid
== euid
;
240 std::string
get_magic_mimetype();
242 std::string
file_to_string() {
244 if (!load_file_from_fd(get_fd(), out
)) {
245 throw ReadError("loading file failed");
250 std::string
gzfile_to_string() {
251 int dup_fd
= dup(get_fd());
253 throw ReadError("dup() failed");
255 gzFile zfh
= gzdopen(dup_fd
, "rb");
257 throw ReadError("gzdopen() failed");
262 int r
= gzread(zfh
, buf
, sizeof(buf
));
265 throw ReadError("gzread() failed");
268 if (unsigned(r
) < sizeof(buf
)) break;
278 if (lseek(fd
, 0, SEEK_SET
) < 0)
279 throw CommitAndExit("Can't rewind file descriptor", path
, errno
);
284 bool md5(std::string
& out
) {
285 return md5_fd(get_fd(), out
);
289 #endif // OMEGA_INCLUDED_DIRITOR_H