summaryrefslogtreecommitdiffstats
path: root/libmount/src/tab_diff.c
blob: d5fbc4d49c8d9839650e100bfbf1d63d06a76641 (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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * This file is part of libmount from util-linux project.
 *
 * Copyright (C) 2011-2018 Karel Zak <kzak@redhat.com>
 *
 * libmount is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 */

/**
 * SECTION: tabdiff
 * @title: Compare changes in mount tables
 * @short_description: compare changes in the list of the mounted filesystems
 */
#include "mountP.h"

struct tabdiff_entry {
	int	oper;			/* MNT_TABDIFF_* flags; */

	struct libmnt_fs *old_fs;	/* pointer to the old FS */
	struct libmnt_fs *new_fs;	/* pointer to the new FS */

	struct list_head changes;
};

struct libmnt_tabdiff {
	int nchanges;			/* number of changes */

	struct list_head changes;	/* list with modified entries */
	struct list_head unused;	/* list with unused entries */
};

/**
 * mnt_new_tabdiff:
 *
 * Allocates a new table diff struct.
 *
 * Returns: new diff handler or NULL.
 */
struct libmnt_tabdiff *mnt_new_tabdiff(void)
{
	struct libmnt_tabdiff *df = calloc(1, sizeof(*df));

	if (!df)
		return NULL;

	DBG(DIFF, ul_debugobj(df, "alloc"));

	INIT_LIST_HEAD(&df->changes);
	INIT_LIST_HEAD(&df->unused);
	return df;
}

static void free_tabdiff_entry(struct tabdiff_entry *de)
{
	if (!de)
		return;
	list_del(&de->changes);
	mnt_unref_fs(de->new_fs);
	mnt_unref_fs(de->old_fs);
	free(de);
}

/**
 * mnt_free_tabdiff:
 * @df: tab diff
 *
 * Deallocates tab diff struct and all entries.
 */
void mnt_free_tabdiff(struct libmnt_tabdiff *df)
{
	if (!df)
		return;

	DBG(DIFF, ul_debugobj(df, "free"));

	while (!list_empty(&df->changes)) {
		struct tabdiff_entry *de = list_entry(df->changes.next,
			                  struct tabdiff_entry, changes);
		free_tabdiff_entry(de);
	}

	free(df);
}

/**
 * mnt_tabdiff_next_change:
 * @df: tabdiff pointer
 * @itr: iterator
 * @old_fs: returns the old entry or NULL if new entry added
 * @new_fs: returns the new entry or NULL if old entry removed
 * @oper: MNT_TABDIFF_{MOVE,UMOUNT,REMOUNT,MOUNT} flags
 *
 * The options @old_fs, @new_fs and @oper are optional.
 *
 * Returns: 0 on success, negative number in case of error or 1 at the end of list.
 */
int mnt_tabdiff_next_change(struct libmnt_tabdiff *df, struct libmnt_iter *itr,
		struct libmnt_fs **old_fs, struct libmnt_fs **new_fs, int *oper)
{
	int rc = 1;
	struct tabdiff_entry *de = NULL;

	if (!df || !itr)
		return -EINVAL;

	if (!itr->head)
		MNT_ITER_INIT(itr, &df->changes);
	if (itr->p != itr->head) {
		de = MNT_ITER_GET_ENTRY(itr, struct tabdiff_entry, changes);
		MNT_ITER_ITERATE(itr);
		rc = 0;
	}

	if (old_fs)
		*old_fs = de ? de->old_fs : NULL;
	if (new_fs)
		*new_fs = de ? de->new_fs : NULL;
	if (oper)
		*oper = de ? de->oper : 0;

	return rc;
}

static int tabdiff_reset(struct libmnt_tabdiff *df)
{
	assert(df);

	DBG(DIFF, ul_debugobj(df, "resetting"));

	/* zeroize all entries and move them to the list of unused
	 */
	while (!list_empty(&df->changes)) {
		struct tabdiff_entry *de = list_entry(df->changes.next,
			                  struct tabdiff_entry, changes);

		list_del_init(&de->changes);
		list_add_tail(&de->changes, &df->unused);

		mnt_unref_fs(de->new_fs);
		mnt_unref_fs(de->old_fs);

		de->new_fs = de->old_fs = NULL;
		de->oper = 0;
	}

	df->nchanges = 0;
	return 0;
}

static int tabdiff_add_entry(struct libmnt_tabdiff *df, struct libmnt_fs *old,
			     struct libmnt_fs *new, int oper)
{
	struct tabdiff_entry *de;

	assert(df);

	DBG(DIFF, ul_debugobj(df, "add change on %s",
				mnt_fs_get_target(new ? new : old)));

	if (!list_empty(&df->unused)) {
		de = list_entry(df->unused.next, struct tabdiff_entry, changes);
		list_del(&de->changes);
	} else {
		de = calloc(1, sizeof(*de));
		if (!de)
			return -ENOMEM;
	}

	INIT_LIST_HEAD(&de->changes);

	mnt_ref_fs(new);
	mnt_ref_fs(old);

	mnt_unref_fs(de->new_fs);
	mnt_unref_fs(de->old_fs);

	de->old_fs = old;
	de->new_fs = new;
	de->oper = oper;

	list_add_tail(&de->changes, &df->changes);
	df->nchanges++;
	return 0;
}

static struct tabdiff_entry *tabdiff_get_mount(struct libmnt_tabdiff *df,
					       const char *src,
					       int id)
{
	struct list_head *p;

	assert(df);

	list_for_each(p, &df->changes) {
		struct tabdiff_entry *de;

		de = list_entry(p, struct tabdiff_entry, changes);

		if (de->oper == MNT_TABDIFF_MOUNT && de->new_fs &&
		    mnt_fs_get_id(de->new_fs) == id) {

			const char *s = mnt_fs_get_source(de->new_fs);

			if (s == NULL && src == NULL)
				return de;
			if (s && src && strcmp(s, src) == 0)
				return de;
		}
	}
	return NULL;
}

/**
 * mnt_diff_tables:
 * @df: diff handler
 * @old_tab: old table
 * @new_tab: new table
 *
 * Compares @old_tab and @new_tab, the result is stored in @df and accessible by
 * mnt_tabdiff_next_change().
 *
 * Returns: number of changes, negative number in case of error.
 */
int mnt_diff_tables(struct libmnt_tabdiff *df, struct libmnt_table *old_tab,
		    struct libmnt_table *new_tab)
{
	struct libmnt_fs *fs;
	struct libmnt_iter itr;
	int no, nn;

	if (!df || !old_tab || !new_tab)
		return -EINVAL;

	tabdiff_reset(df);

	no = mnt_table_get_nents(old_tab);
	nn = mnt_table_get_nents(new_tab);

	if (!no && !nn)			/* both tables are empty */
		return 0;

	DBG(DIFF, ul_debugobj(df, "analyze new (%d entries), "
				          "old (%d entries)",
				nn, no));

	mnt_reset_iter(&itr, MNT_ITER_FORWARD);

	/* all mounted or umounted */
	if (!no && nn) {
		while(mnt_table_next_fs(new_tab, &itr, &fs) == 0)
			tabdiff_add_entry(df, NULL, fs, MNT_TABDIFF_MOUNT);
		goto done;

	} else if (no && !nn) {
		while(mnt_table_next_fs(old_tab, &itr, &fs) == 0)
			tabdiff_add_entry(df, fs, NULL, MNT_TABDIFF_UMOUNT);
		goto done;
	}

	/* search newly mounted or modified */
	while(mnt_table_next_fs(new_tab, &itr, &fs) == 0) {
		struct libmnt_fs *o_fs;
		const char *src = mnt_fs_get_source(fs),
			   *tgt = mnt_fs_get_target(fs);

		o_fs = mnt_table_find_pair(old_tab, src, tgt, MNT_ITER_FORWARD);
		if (!o_fs)
			/* 'fs' is not in the old table -- so newly mounted */
			tabdiff_add_entry(df, NULL, fs, MNT_TABDIFF_MOUNT);
		else {
			/* is modified? */
			const char *v1 = mnt_fs_get_vfs_options(o_fs),
				   *v2 = mnt_fs_get_vfs_options(fs),
				   *f1 = mnt_fs_get_fs_options(o_fs),
				   *f2 = mnt_fs_get_fs_options(fs);

			if ((v1 && v2 && strcmp(v1, v2) != 0) || (f1 && f2 && strcmp(f1, f2) != 0))
				tabdiff_add_entry(df, o_fs, fs, MNT_TABDIFF_REMOUNT);
		}
	}

	/* search umounted or moved */
	mnt_reset_iter(&itr, MNT_ITER_FORWARD);
	while(mnt_table_next_fs(old_tab, &itr, &fs) == 0) {
		const char *src = mnt_fs_get_source(fs),
			   *tgt = mnt_fs_get_target(fs);

		if (!mnt_table_find_pair(new_tab, src, tgt, MNT_ITER_FORWARD)) {
			struct tabdiff_entry *de;

			de = tabdiff_get_mount(df, src,	mnt_fs_get_id(fs));
			if (de) {
				mnt_ref_fs(fs);
				mnt_unref_fs(de->old_fs);
				de->oper = MNT_TABDIFF_MOVE;
				de->old_fs = fs;
			} else
				tabdiff_add_entry(df, fs, NULL, MNT_TABDIFF_UMOUNT);
		}
	}
done:
	DBG(DIFF, ul_debugobj(df, "%d changes detected", df->nchanges));
	return df->nchanges;
}

#ifdef TEST_PROGRAM

static int test_diff(struct libmnt_test *ts, int argc, char *argv[])
{
	struct libmnt_table *tb_old, *tb_new;
	struct libmnt_tabdiff *diff;
	struct libmnt_iter *itr;
	struct libmnt_fs *old, *new;
	int rc = -1, change;

	tb_old = mnt_new_table_from_file(argv[1]);
	tb_new = mnt_new_table_from_file(argv[2]);
	diff = mnt_new_tabdiff();
	itr = mnt_new_iter(MNT_ITER_FORWARD);

	if (!tb_old || !tb_new || !diff || !itr) {
		warnx("failed to allocate resources");
		goto done;
	}

	rc = mnt_diff_tables(diff, tb_old, tb_new);
	if (rc < 0)
		goto done;

	while(mnt_tabdiff_next_change(diff, itr, &old, &new, &change) == 0) {

		printf("%s on %s: ", mnt_fs_get_source(new ? new : old),
				     mnt_fs_get_target(new ? new : old));

		switch(change) {
		case MNT_TABDIFF_MOVE:
			printf("MOVED to %s\n", mnt_fs_get_target(new));
			break;
		case MNT_TABDIFF_UMOUNT:
			printf("UMOUNTED\n");
			break;
		case MNT_TABDIFF_REMOUNT:
			printf("REMOUNTED from '%s' to '%s'\n",
					mnt_fs_get_options(old),
					mnt_fs_get_options(new));
			break;
		case MNT_TABDIFF_MOUNT:
			printf("MOUNTED\n");
			break;
		default:
			printf("unknown change!\n");
		}
	}

	rc = 0;
done:
	mnt_unref_table(tb_old);
	mnt_unref_table(tb_new);
	mnt_free_tabdiff(diff);
	mnt_free_iter(itr);
	return rc;
}

int main(int argc, char *argv[])
{
	struct libmnt_test tss[] = {
		{ "--diff", test_diff, "<old> <new> prints change" },
		{ NULL }
	};

	return mnt_run_test(tss, argc, argv);
}

#endif /* TEST_PROGRAM */