summaryrefslogtreecommitdiffstats
path: root/src/doveadm/doveadm-dump-mailboxlog.c
blob: 8ff5a5417634cbf4d3a3e9932409844d2b80a75a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* Copyright (c) 2007-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "hex-binary.h"
#include "mailbox-log.h"
#include "doveadm-dump.h"

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>

static int dump_record(int fd)
{
	off_t offset;
	ssize_t ret;
	struct mailbox_log_record rec;
	time_t timestamp;

	offset = lseek(fd, 0, SEEK_CUR);

	ret = read(fd, &rec, sizeof(rec));
	if (ret == 0)
		return 0;

	if (ret != sizeof(rec)) {
		i_fatal("rec read() %zu != %zu",
			ret, sizeof(rec));
	}

	printf("#%"PRIuUOFF_T": ", offset);
	switch (rec.type) {
	case MAILBOX_LOG_RECORD_DELETE_MAILBOX:
		printf("delete-mailbox");
		break;
	case MAILBOX_LOG_RECORD_DELETE_DIR:
		printf("delete-dir");
		break;
	case MAILBOX_LOG_RECORD_RENAME:
		printf("rename");
		break;
	case MAILBOX_LOG_RECORD_SUBSCRIBE:
		printf("subscribe");
		break;
	case MAILBOX_LOG_RECORD_UNSUBSCRIBE:
		printf("unsubscribe");
		break;
	case MAILBOX_LOG_RECORD_CREATE_DIR:
		printf("create-dir");
		break;
	}
	printf(" %s", binary_to_hex(rec.mailbox_guid,
				    sizeof(rec.mailbox_guid)));

	timestamp = be32_to_cpu_unaligned(rec.timestamp);
	printf(" (%s)\n", unixdate2str(timestamp));
	return 1;
}

static void
cmd_dump_mailboxlog(const char *path, const char *const *args ATTR_UNUSED)
{
	int fd, ret;

	fd = open(path, O_RDONLY);
	if (fd < 0)
		i_fatal("open(%s) failed: %m", path);

	do {
		T_BEGIN {
			ret = dump_record(fd);
		} T_END;
	} while (ret > 0);
	i_close_fd(&fd);
}

static bool test_dump_mailboxlog(const char *path)
{
	const char *p;
	int fd;
	struct mailbox_log_record rec;
	bool ret = FALSE;

	p = strrchr(path, '.');
	if (p == NULL || strcmp(p, ".log") != 0)
		return FALSE;

	fd = open(path, O_RDONLY);
	if (fd == -1)
		return FALSE;

	if (read(fd, &rec, sizeof(rec)) == sizeof(rec) &&
	    rec.padding[0] == 0 && rec.padding[1] == 0 && rec.padding[2] == 0) {
		enum mailbox_log_record_type type = rec.type;
		switch (type) {
		case MAILBOX_LOG_RECORD_DELETE_MAILBOX:
		case MAILBOX_LOG_RECORD_DELETE_DIR:
		case MAILBOX_LOG_RECORD_RENAME:
		case MAILBOX_LOG_RECORD_SUBSCRIBE:
		case MAILBOX_LOG_RECORD_UNSUBSCRIBE:
		case MAILBOX_LOG_RECORD_CREATE_DIR:
			ret = TRUE;
			break;
		}
	}
	i_close_fd(&fd);
	return ret;
}

struct doveadm_cmd_dump doveadm_cmd_dump_mailboxlog = {
	"mailboxlog",
	test_dump_mailboxlog,
	cmd_dump_mailboxlog
};