Lines Matching defs:log

6 #include "mailbox-log.h"
13 /* How often to reopen the log file to make sure that the changes are written
34 struct mailbox_log *log;
45 static void mailbox_log_close(struct mailbox_log *log);
49 struct mailbox_log *log;
51 log = i_new(struct mailbox_log, 1);
52 log->filepath = i_strdup(path);
53 log->filepath2 = i_strconcat(path, ".2", NULL);
54 log->mode = 0644;
55 log->gid = (gid_t)-1;
56 log->fd = -1;
57 return log;
62 struct mailbox_log *log = *_log;
66 mailbox_log_close(log);
67 i_free(log->gid_origin);
68 i_free(log->filepath);
69 i_free(log->filepath2);
70 i_free(log);
73 static void mailbox_log_close(struct mailbox_log *log)
75 i_close_fd_path(&log->fd, log->filepath);
78 void mailbox_log_set_permissions(struct mailbox_log *log, mode_t mode,
81 log->mode = mode;
82 log->gid = gid;
83 i_free(log->gid_origin);
84 log->gid_origin = i_strdup(gid_origin);
87 static int mailbox_log_open(struct mailbox_log *log)
91 i_assert(log->fd == -1);
93 log->open_timestamp = ioloop_time;
94 log->fd = open(log->filepath, O_RDWR | O_APPEND);
95 if (log->fd != -1)
99 old_mode = umask(0666 ^ log->mode);
100 log->fd = open(log->filepath, O_RDWR | O_APPEND | O_CREAT, 0666);
103 if (log->fd == -1) {
105 i_error("creat(%s) failed: %m", log->filepath);
107 i_error("%s", eacces_error_get("creat", log->filepath));
110 if (fchown(log->fd, (uid_t)-1, log->gid) < 0) {
112 i_error("fchown(%s) failed: %m", log->filepath);
115 log->filepath, log->gid,
116 log->gid_origin));
122 static int mailbox_log_rotate_if_needed(struct mailbox_log *log)
126 if (fstat(log->fd, &st) < 0) {
127 i_error("fstat(%s) failed: %m", log->filepath);
134 if (rename(log->filepath, log->filepath2) < 0 && errno != ENOENT) {
136 log->filepath, log->filepath2);
153 int mailbox_log_append(struct mailbox_log *log,
159 /* we don't have to be too strict about appending to the latest log
162 it shouldn't keep writing to a rotated log forever. */
163 if (log->open_timestamp/MAILBOX_LOG_REOPEN_SECS !=
165 mailbox_log_close(log);
166 if (log->fd == -1) {
167 if (mailbox_log_open(log) < 0)
169 i_assert(log->fd != -1);
175 This whole log isn't supposed to be super-reliable anyway. */
176 ret = write(log->fd, rec, sizeof(*rec));
178 i_error("write(%s) failed: %m", log->filepath);
181 i_error("write(%s) wrote %d/%u bytes", log->filepath,
183 if (fstat(log->fd, &st) == 0) {
184 if (ftruncate(log->fd, st.st_size - ret) < 0) {
186 log->filepath);
192 (void)mailbox_log_rotate_if_needed(log);
200 iter->filepath = iter->log->filepath2;
201 else if (iter->filepath == iter->log->filepath2)
202 iter->filepath = iter->log->filepath;
210 if (iter->filepath == iter->log->filepath2)
219 struct mailbox_log_iter *mailbox_log_iter_init(struct mailbox_log *log)
224 iter->log = log;
264 i_error("Corrupted mailbox log %s at offset %"PRIuUOFF_T": "