2 * @brief Iterator through entries in a directory.
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
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().
48 #include "runfilter.h" // For class ReadError.
50 struct FileNotFound
{ };
52 // Exception to signify changes should be committed, but indexing aborted.
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
69 static magic_t magic_cookie
;
72 std::string::size_type path_len
;
83 void ensure_statbuf_valid() {
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.
117 if (fd
>= 0) close_fd();
118 path
.resize(path_len
);
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
);
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
;
140 * DT_UNKNOWN DT_FIFO DT_CHR DT_DIR DT_BLK DT_REG DT_LNK DT_SOCK DT_WHT
142 switch (entry
->d_type
) {
144 // The current filing system doesn't support d_type.
152 if (follow_symlinks
) break;
160 ensure_statbuf_valid();
162 if (S_ISREG(statbuf
.st_mode
)) return REGULAR_FILE
;
163 if (S_ISDIR(statbuf
.st_mode
)) return DIRECTORY
;
168 ensure_statbuf_valid();
169 return statbuf
.st_size
;
173 ensure_statbuf_valid();
174 return statbuf
.st_mtime
;
178 ensure_statbuf_valid();
179 return statbuf
.st_ctime
;
182 const char * get_owner() {
184 ensure_statbuf_valid();
185 struct passwd
* pwentry
= getpwuid(statbuf
.st_uid
);
186 return pwentry
? pwentry
->pw_name
: NULL
;
192 const char * get_group() {
194 ensure_statbuf_valid();
195 struct group
* grentry
= getgrgid(statbuf
.st_gid
);
196 return grentry
? grentry
->gr_name
: NULL
;
202 bool is_owner_readable() {
203 ensure_statbuf_valid();
205 return (statbuf
.st_mode
& S_IRUSR
);
207 return (statbuf
.st_mode
& S_IREAD
);
211 bool is_group_readable() {
212 ensure_statbuf_valid();
214 return (statbuf
.st_mode
& S_IRGRP
);
220 bool is_other_readable() {
221 ensure_statbuf_valid();
223 return (statbuf
.st_mode
& S_IROTH
);
230 #if defined O_NOATIME && O_NOATIME != 0
231 if (euid
== 0) return true;
232 ensure_statbuf_valid();
233 return statbuf
.st_uid
== euid
;
239 std::string
get_magic_mimetype();
241 std::string
file_to_string() {
243 if (!load_file_from_fd(get_fd(), out
)) {
244 throw ReadError("loading file failed");
249 std::string
gzfile_to_string() {
250 int dup_fd
= dup(get_fd());
252 throw ReadError("dup() failed");
254 gzFile zfh
= gzdopen(dup_fd
, "rb");
256 throw ReadError("gzdopen() failed");
261 int r
= gzread(zfh
, buf
, sizeof(buf
));
264 throw ReadError("gzread() failed");
267 if (unsigned(r
) < sizeof(buf
)) break;
277 if (lseek(fd
, 0, SEEK_SET
) < 0)
278 throw CommitAndExit("Can't rewind file descriptor", path
, errno
);
283 bool md5(std::string
& out
) {
284 return md5_fd(get_fd(), out
);
288 #endif // OMEGA_INCLUDED_DIRITOR_H