summaryrefslogtreecommitdiff
path: root/snapshot.c
blob: df555e2d7d798c66f4528fea315db34437c99fef (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
 * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 2.1
 * as published by the Free Software Foundation
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <sys/stat.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <mtd/mtd-user.h>

#include <glob.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <libgen.h>
#include <unistd.h>
#include <string.h>
#include <inttypes.h>

#include <libubox/list.h>
#include <libubox/blob.h>
#include <libubox/md5.h>
#include <libubox/ulog.h>

#include "libfstools/libfstools.h"
#include "libfstools/volume.h"
#include "libfstools/snapshot.h"

static int
config_write(int argc, char **argv)
{
	struct volume *v = volume_find("rootfs_data");
	int ret;

	if (!v)
		return -1;

	volume_init(v);
	ret = volatile_write(v, 0);
	if (!ret)
		ret = sentinel_write(v, 0);

	return ret;
}

static int
config_read(int argc, char **argv)
{
	struct volume *v = volume_find("rootfs_data");
	struct file_header conf, sentinel;
	int next, block, ret = 0;
	uint32_t seq;

	if (!v)
		return -1;

	volume_init(v);
	block = config_find(v, &conf, &sentinel);
	next = snapshot_next_free(v, &seq);
	if (is_config(&conf) && conf.seq == seq)
		block = next;
	else if (!is_config(&sentinel) || sentinel.seq != seq)
		return -1;

	unlink("/tmp/config.tar.gz");
	ret = snapshot_read_file(v, block, "/tmp/config.tar.gz", CONF);

	if (ret < 1)
		ULOG_ERR("failed to read /tmp/config.tar.gz\n");

	return ret;
}

static int
snapshot_write(int argc, char **argv)
{
	struct volume *v = volume_find("rootfs_data");
	int block, ret;
	uint32_t seq;

	if (!v)
		return -1;

	volume_init(v);
	block = snapshot_next_free(v, &seq);
	if (block < 0)
		block = 0;

	ret = snapshot_write_file(v, block, "/tmp/snapshot.tar.gz", seq + 1, DATA);
	if (ret)
		ULOG_ERR("failed to write /tmp/snapshot.tar.gz\n");
	else
		ULOG_INFO("wrote /tmp/snapshot.tar.gz\n");

	return ret;
}

static int
snapshot_mark(int argc, char **argv)
{
	__be32 owrt = cpu_to_be32(OWRT);
	struct volume *v;
	size_t sz;
	int fd;

	ULOG_WARN("This will remove all snapshot data stored on the system. Are you sure? [N/y]\n");
	if (getchar() != 'y')
		return -1;

	v = volume_find("rootfs_data");
	if (!v) {
		ULOG_ERR("MTD partition 'rootfs_data' not found\n");
		return -1;
	}

	volume_init(v);

	fd = open(v->blk, O_WRONLY);
	ULOG_INFO("%s - marking with 0x%08x\n", v->blk, owrt);
	if (fd < 0) {
		ULOG_ERR("opening %s failed\n", v->blk);
		return -1;
	}

	sz = write(fd, &owrt, sizeof(owrt));
	close(fd);

	if (sz != 1) {
		ULOG_ERR("writing %s failed: %m\n", v->blk);
		return -1;
	}

	return 0;
}

static int
snapshot_read(int argc, char **argv)
{
	struct volume *v = volume_find("rootfs_data");;
	int block = 0, ret = 0;
	char file[64];

	if (!v)
		return -1;

	volume_init(v);
	if (argc > 2) {
		block = atoi(argv[2]);
		if (block >= (v->size / v->block_size)) {
			ULOG_ERR("invalid block %d > %" PRIu64 "\n",
			         block, (uint64_t) v->size / v->block_size);
			goto out;
		}
		snprintf(file, sizeof(file), "/tmp/snapshot/block%d.tar.gz", block);

		ret = snapshot_read_file(v, block, file, DATA);
		goto out;
	}

	do {
		snprintf(file, sizeof(file), "/tmp/snapshot/block%d.tar.gz", block);
		block = snapshot_read_file(v, block, file, DATA);
	} while (block > 0);

out:
	return ret;
}

static int
snapshot_info(void)
{
	struct volume *v = volume_find("rootfs_data");
	struct file_header hdr = { 0 }, conf;
	int block = 0;

	if (!v)
		return -1;

	volume_init(v);
	ULOG_INFO("sectors:\t%" PRIu64 ", block_size:\t%dK\n",
		  (uint64_t) v->size / v->block_size, v->block_size / 1024);
	do {
		if (volume_read(v, &hdr, block * v->block_size, sizeof(struct file_header))) {
			ULOG_ERR("scanning for next free block failed\n");
			return 0;
		}

		be32_to_hdr(&hdr);

		if (hdr.magic != OWRT)
			break;

		if (hdr.type == DATA)
			ULOG_INFO("block %d:\tsnapshot entry, size: %d, sectors: %d, sequence: %d\n", block,  hdr.length, pad_file_size(v, hdr.length) / v->block_size, hdr.seq);
		else if (hdr.type == CONF)
			ULOG_INFO("block %d:\tvolatile entry, size: %d, sectors: %d, sequence: %d\n", block,  hdr.length, pad_file_size(v, hdr.length) / v->block_size, hdr.seq);

		if (hdr.type == DATA && !valid_file_size(hdr.length))
			block += pad_file_size(v, hdr.length) / v->block_size;
	} while (hdr.type == DATA);
	block = config_find(v, &conf, &hdr);
	if (block > 0)
		ULOG_INFO("block %d:\tsentinel entry, size: %d, sectors: %d, sequence: %d\n", block, hdr.length, pad_file_size(v, hdr.length) / v->block_size, hdr.seq);

	return 0;
}

int main(int argc, char **argv)
{
	if (argc < 2)
		return -1;

	if (!strcmp(argv[1], "config_read"))
		return config_read(argc, argv);
	if (!strcmp(argv[1], "config_write"))
		return config_write(argc, argv);
	if (!strcmp(argv[1], "read"))
		return snapshot_read(argc, argv);
	if (!strcmp(argv[1], "write"))
		return snapshot_write(argc, argv);
	if (!strcmp(argv[1], "mark"))
		return snapshot_mark(argc, argv);
	if (!strcmp(argv[1], "info"))
		return snapshot_info();
	return -1;
}