1 /* diritor.h: Iterator through entries in a directory.
3 * Copyright (C) 2007,2008,2010,2011,2012,2013,2014,2015 Olly Betts
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #ifndef OMEGA_INCLUDED_DIRITOR_H
21 #define OMEGA_INCLUDED_DIRITOR_H
25 #include "safedirent.h"
26 #include "safeerrno.h"
27 #include "safefcntl.h"
28 #include "safesysstat.h"
29 #include "safeunistd.h"
31 #include <sys/types.h>
34 #include <grp.h> // For getgrgid().
35 #include <pwd.h> // For getpwuid().
42 #include "runfilter.h" // For class ReadError.
44 struct FileNotFound
{ };
46 // Exception to signify changes should be committed, but indexing aborted.
51 CommitAndExit(const char * msg_
, const std::string
& path
, int errno_
);
52 CommitAndExit(const char * msg_
, int errno_
);
53 CommitAndExit(const char * msg_
, const char * error
);
55 const std::string
& what() const { return msg
; }
58 class DirectoryIterator
{
59 #if defined O_NOATIME && O_NOATIME != 0
63 static magic_t magic_cookie
;
66 std::string::size_type path_len
;
77 void ensure_statbuf_valid() {
88 explicit DirectoryIterator(bool follow_symlinks_
)
89 : dir(NULL
), follow_symlinks(follow_symlinks_
), fd(-1) { }
91 ~DirectoryIterator() {
92 if (dir
) closedir(dir
);
93 if (fd
>= 0) close(fd
);
96 /// Start iterating through entries in @a path.
98 // Throws a std::string exception upon failure.
99 void start(const std::string
& path
);
101 /// Read the next directory entry which doesn't start with ".".
103 // We do this to skip ".", "..", and Unix hidden files.
105 // @return false if there are no more entries.
111 path
.resize(path_len
);
114 entry
= readdir(dir
);
115 } while (entry
&& entry
->d_name
[0] == '.');
116 statbuf_valid
= false;
117 if (entry
== NULL
&& errno
!= 0) next_failed();
118 return (entry
!= NULL
);
122 void next_failed() const;
124 const char * leafname() const { return entry
->d_name
; }
126 const std::string
& pathname() const { return path
; }
128 typedef enum { REGULAR_FILE
, DIRECTORY
, OTHER
} type
;
133 * DT_UNKNOWN DT_FIFO DT_CHR DT_DIR DT_BLK DT_REG DT_LNK DT_SOCK DT_WHT
135 switch (entry
->d_type
) {
137 // The current filing system doesn't support d_type.
145 if (follow_symlinks
) break;
153 ensure_statbuf_valid();
155 if (S_ISREG(statbuf
.st_mode
)) return REGULAR_FILE
;
156 if (S_ISDIR(statbuf
.st_mode
)) return DIRECTORY
;
161 ensure_statbuf_valid();
162 return statbuf
.st_size
;
166 ensure_statbuf_valid();
167 return statbuf
.st_mtime
;
171 ensure_statbuf_valid();
172 return statbuf
.st_ctime
;
175 const char * get_owner() {
177 ensure_statbuf_valid();
178 struct passwd
* pwentry
= getpwuid(statbuf
.st_uid
);
179 return pwentry
? pwentry
->pw_name
: NULL
;
185 const char * get_group() {
187 ensure_statbuf_valid();
188 struct group
* grentry
= getgrgid(statbuf
.st_gid
);
189 return grentry
? grentry
->gr_name
: NULL
;
195 bool is_owner_readable() {
196 ensure_statbuf_valid();
198 return (statbuf
.st_mode
& S_IRUSR
);
200 return (statbuf
.st_mode
& S_IREAD
);
204 bool is_group_readable() {
205 ensure_statbuf_valid();
207 return (statbuf
.st_mode
& S_IRGRP
);
213 bool is_other_readable() {
214 ensure_statbuf_valid();
216 return (statbuf
.st_mode
& S_IROTH
);
223 #if defined O_NOATIME && O_NOATIME != 0
224 if (euid
== 0) return true;
225 ensure_statbuf_valid();
226 return statbuf
.st_uid
== euid
;
232 std::string
get_magic_mimetype();
234 std::string
file_to_string() {
238 if (try_noatime()) flags
|= NOATIME
;
239 fd
= load_file_fd(path
, out
, flags
);
241 if (errno
== ENOENT
|| errno
== ENOTDIR
) throw FileNotFound();
242 throw ReadError("load_file failed");
247 std::string
gzfile_to_string() {
250 gzFile zfh
= gzopen(path
.c_str(), "rb");
252 if (errno
== ENOENT
|| errno
== ENOTDIR
) {
253 throw FileNotFound();
255 throw ReadError("gzopen() failed");
259 int r
= gzread(zfh
, buf
, sizeof(buf
));
262 throw ReadError("gzread() failed");
265 if (unsigned(r
) < sizeof(buf
)) break;
272 #endif // OMEGA_INCLUDED_DIRITOR_H