summaryrefslogtreecommitdiffstats
path: root/shlibs/mount/src/tab.c
blob: bfd3a6e127673098285a2d142cdb8e7d0d27ac2e (plain) (blame)
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
/*
 * Copyright (C) 2008 Karel Zak <kzak@redhat.com>
 *
 * This file may be redistributed under the terms of the
 * GNU Lesser General Public License.
 *
 * Note:
 *	mnt_tab_find_* functions are mount(8) compatible. It means it tries
 *	to found an entry in more iterations where the first attempt is always
 *	based on comparison with unmodified (non-canonicalized or un-evaluated)
 *	paths or tags. For example fstab with two entries:
 *
 *		LABEL=foo	/foo	auto   rw
 *		/dev/foo	/foo	auto   rw
 *
 *	where both lines are used for the *same* device, then
 *
 *		mnt_tab_find_source(tb, "/dev/foo", &fs);
 *
 *	will returns the second line, and
 *
 *		mnt_tab_find_source(tb, "LABEL=foo", &fs);
 *
 *	will returns the first entry, and
 *
 *		mnt_tab_find_source(tb, "UUID=<anyuuid>", &fs);
 *
 *	will returns the first entry (if UUID matches with the device).
 */

#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <blkid/blkid.h>

#include "nls.h"
#include "mountP.h"

/**
 * mnt_new_tab:
 * @filename: file name or NULL
 *
 * The tab is a container for mnt_fs entries that usually represents a fstab,
 * mtab or mountinfo file from your system.
 *
 * Note that this function does not parse the file. See also
 * mnt_tab_parse_file().
 *
 * Returns newly allocated tab struct.
 */
mnt_tab *mnt_new_tab(const char *filename)
{
	mnt_tab *tb = NULL;

	tb = calloc(1, sizeof(struct _mnt_tab));
	if (!tb)
		goto err;

	if (filename) {
		tb->filename = strdup(filename);
		if (!tb->filename)
			goto err;
	}
	INIT_LIST_HEAD(&tb->ents);
	return tb;
err:
	free(tb);
	return NULL;
}

/**
 * mnt_free_tab:
 * @tab: tab pointer
 *
 * Deallocates tab struct and all entries.
 */
void mnt_free_tab(mnt_tab *tb)
{
	if (!tb)
		return;
	free(tb->filename);

	while (!list_empty(&tb->ents)) {
		mnt_fs *fs = list_entry(tb->ents.next, mnt_fs, ents);
		mnt_free_fs(fs);
	}

	free(tb);
}

/**
 * mnt_tab_get_nents:
 * @tb: pointer to tab
 *
 * Returns number of valid entries in tab.
 */
int mnt_tab_get_nents(mnt_tab *tb)
{
	assert(tb);
	return tb ? tb->nents : 0;
}

/**
 * mnt_tab_set_cache:
 * @tb: pointer to tab
 * @mpc: pointer to mnt_cache instance
 *
 * Setups a cache for canonicalized paths and evaluated tags (LABEL/UUID). The
 * cache is recommended for mnt_tab_find_*() functions.
 *
 * The cache could be shared between more tabs. Be careful when you share the
 * same cache between more threads -- currently the cache does not provide any
 * locking method.
 *
 * See also mnt_new_cache().
 *
 * Returns 0 on success or -1 in case of error.
 */
int mnt_tab_set_cache(mnt_tab *tb, mnt_cache *mpc)
{
	assert(tb);
	if (!tb)
		return -1;
	tb->cache = mpc;
	return 0;
}

/**
 * mnt_tab_get_cache:
 * @tb: pointer to tab
 *
 * Returns pointer to mnt_cache instance or NULL.
 */
mnt_cache *mnt_tab_get_cache(mnt_tab *tb)
{
	assert(tb);
	return tb ? tb->cache : NULL;
}

/**
 * mnt_tab_get_name:
 * @tb: tab pointer
 *
 * Returns tab filename or NULL.
 */
const char *mnt_tab_get_name(mnt_tab *tb)
{
	assert(tb);
	return tb ? tb->filename : NULL;
}

/**
 * mnt_tab_add_fs:
 * @tb: tab pointer
 * @fs: new entry
 *
 * Adds a new entry to tab.
 *
 * Returns 0 on success or -1 in case of error.
 */
int mnt_tab_add_fs(mnt_tab *tb, mnt_fs *fs)
{
	assert(tb);
	assert(fs);

	if (!tb || !fs)
		return -1;

	list_add_tail(&fs->ents, &tb->ents);

	DBG(DEBUG_TAB, fprintf(stderr,
		"libmount: %s: add entry: %s %s\n",
		tb->filename, mnt_fs_get_source(fs),
		mnt_fs_get_target(fs)));

	if (fs->flags & MNT_FS_ERROR)
		tb->nerrs++;
	else
		tb->nents++;
	return 0;
}

/**
 * mnt_tab_remove_fs:
 * @tb: tab pointer
 * @fs: new entry
 *
 * Returns 0 on success or -1 in case of error.
 */
int mnt_tab_remove_fs(mnt_tab *tb, mnt_fs *fs)
{
	assert(tb);
	assert(fs);

	if (!tb || !fs)
		return -1;

	list_del(&fs->ents);

	if (fs->flags & MNT_FS_ERROR)
		tb->nerrs--;
	else
		tb->nents--;
	return 0;
}

/**
 * mnt_tab_next_fs:
 * @tb: tab pointer
 * @itr: iterator
 * @fs: returns the next tab entry
 *
 * Returns 0 on success, -1 in case of error or 1 at end of list.
 *
 * Example (list all mountpoints from fstab in backward order):
 *
 *	mnt_fs *fs;
 *	mnt_tab *tb = mnt_new_tab("/etc/fstab");
 *	mnt_iter *itr = mnt_new_iter(MNT_ITER_BACKWARD);
 *
 *	mnt_tab_parse_file(tb);
 *
 *	while(mnt_tab_next_fs(tb, itr, &fs) == 0) {
 *		const char *dir = mnt_fs_get_target(fs);
 *		printf("mount point: %s\n", dir);
 *	}
 *	mnt_free_tab(fi);
 */
int mnt_tab_next_fs(mnt_tab *tb, mnt_iter *itr, mnt_fs **fs)
{
	int rc;

	assert(tb);
	assert(itr);
	assert(fs);

	if (!tb || !itr || !fs)
		return -1;
again:
	rc = 1;
	if (!itr->head)
		MNT_ITER_INIT(itr, &tb->ents);
	if (itr->p != itr->head) {
		MNT_ITER_ITERATE(itr, *fs, struct _mnt_fs, ents);
		rc = 0;
	}

	/* ignore broken entries */
	if (*fs && ((*fs)->flags & MNT_FS_ERROR))
		goto again;

	return rc;
}

/**
 * mnt_tab_find_next_fs:
 * @tb: table
 * @itr: iterator
 * @match_func: function returns 1 or 0
 * @fs: returns pointer to the next matching table entry
 *
 * This function allows search in @tb.
 *
 * Returns -1 in case of error, 1 at end of table or 0 o success.
 */
int mnt_tab_find_next_fs(mnt_tab *tb, mnt_iter *itr,
		int (*match_func)(mnt_fs *, void *), void *userdata,
		mnt_fs **fs)
{
	if (!tb || !itr || !fs || !match_func)
		return -1;

	if (!itr->head)
		MNT_ITER_INIT(itr, &tb->ents);

	do {
		if (itr->p != itr->head)
			MNT_ITER_ITERATE(itr, *fs, struct _mnt_fs, ents);
		else
			break;			/* end */

		if ((*fs)->flags & MNT_FS_ERROR)
			continue;
		if (match_func(*fs, userdata))
			return 0;
	} while(1);

	return 1;
}

/**
 * mnt_tab_set_iter:
 * @tb: tab pointer
 * @itr: iterator
 * @fs: tab entry
 *
 * Sets @iter to the position of @fs in the file @tb.
 *
 * Returns 0 on success, -1 in case of error.
 */
int mnt_tab_set_iter(mnt_tab *tb, mnt_iter *itr, mnt_fs *fs)
{
	assert(tb);
	assert(itr);
	assert(fs);

	if (!tb || !itr || !fs)
		return -1;

	MNT_ITER_INIT(itr, &tb->ents);
	itr->p = &fs->ents;

	return 0;
}

/**
 * mnt_tab_find_target:
 * @tb: tab pointer
 * @path: mountpoint directory
 * @direction: MNT_ITER_{FORWARD,BACKWARD}
 *
 * Try to lookup an entry in given tab, possible are three iterations, first
 * with @path, second with realpath(@path) and third with realpath(@path)
 * against realpath(fs->target). The 2nd and 3rd iterations are not performed
 * when @tb cache is not set (see mnt_tab_set_cache()).
 *
 * Returns a tab entry or NULL.
 */
mnt_fs *mnt_tab_find_target(mnt_tab *tb, const char *path, int direction)
{
	mnt_iter itr;
	mnt_fs *fs = NULL;
	char *cn;

	assert(tb);
	assert(path);

	DBG(DEBUG_TAB, fprintf(stderr,
		"libmount: %s: lookup target: %s\n", tb->filename, path));

	/* native @target */
	mnt_reset_iter(&itr, direction);
	while(mnt_tab_next_fs(tb, &itr, &fs) == 0)
		if (fs->target && strcmp(fs->target, path) == 0)
			return fs;

	if (!tb->cache || !(cn = mnt_resolve_path(path, tb->cache)))
		return NULL;

	/* canonicalized paths in mnt_tab */
	mnt_reset_iter(&itr, direction);
	while(mnt_tab_next_fs(tb, &itr, &fs) == 0) {
		if (fs->target && strcmp(fs->target, cn) == 0)
			return fs;
	}

	/* non-canonicaled path in mnt_tab */
	mnt_reset_iter(&itr, direction);
	while(mnt_tab_next_fs(tb, &itr, &fs) == 0) {
		char *p;
		if (!fs->target)
		       continue;
		p = mnt_resolve_path(fs->target, tb->cache);
		if (strcmp(cn, p) == 0)
			return fs;
	}
	return NULL;
}

/**
 * mnt_tab_find_srcpath:
 * @tb: tab pointer
 * @path: source path (devname or dirname)
 * @direction: MNT_ITER_{FORWARD,BACKWARD}
 *
 * Try to lookup an entry in given tab, possible are four iterations, first
 * with @path, second with realpath(@path), third with tags (LABEL, UUID, ..)
 * from @path and fourth with realpath(@path) against realpath(entry->srcpath).
 *
 * The 2nd, 3rd and 4th iterations are not performed when @tb cache is not
 * set (see mnt_tab_set_cache()).
 *
 * Returns a tab entry or NULL.
 */
mnt_fs *mnt_tab_find_srcpath(mnt_tab *tb, const char *path, int direction)
{
	mnt_iter itr;
	mnt_fs *fs = NULL;
	int ntags = 0;
	char *cn;
	const char *p;

	assert(tb);
	assert(path);

	DBG(DEBUG_TAB, fprintf(stderr,
		"libmount: %s: lookup srcpath: %s\n", tb->filename, path));

	/* native paths */
	mnt_reset_iter(&itr, direction);
	while(mnt_tab_next_fs(tb, &itr, &fs) == 0) {
		p = mnt_fs_get_srcpath(fs);
		if (p && strcmp(p, path) == 0)
			return fs;
		if (!p)
			/* mnt_fs_get_srcpath() returs nothing, it's TAG */
			ntags++;
	}

	if (!tb->cache || !(cn = mnt_resolve_path(path, tb->cache)))
		return NULL;

	/* canonicalized paths in mnt_tab */
	if (ntags < mnt_tab_get_nents(tb)) {
		mnt_reset_iter(&itr, direction);
		while(mnt_tab_next_fs(tb, &itr, &fs) == 0) {
			p = mnt_fs_get_srcpath(fs);
			if (p && strcmp(p, cn) == 0)
				return fs;
		}
	}

	/* evaluated tag */
	if (ntags) {
		mnt_reset_iter(&itr, direction);

		if (mnt_cache_read_tags(tb->cache, cn) > 0) {
			/* @path's TAGs are in the cache */
			while(mnt_tab_next_fs(tb, &itr, &fs) == 0) {
				const char *t, *v;

				if (mnt_fs_get_tag(fs, &t, &v))
					continue;

				if (mnt_cache_device_has_tag(tb->cache, cn, t, v))
					return fs;
			}
		} else if (errno == EACCES) {
			/* @path is unaccessible, try evaluate all TAGs in @tb
			 * by udev symlinks -- this could be expensive on systems
			 * with huge fstab/mtab */
			 while(mnt_tab_next_fs(tb, &itr, &fs) == 0) {
				 const char *t, *v, *x;
				 if (mnt_fs_get_tag(fs, &t, &v))
					 continue;
				 x = mnt_resolve_tag(t, v, tb->cache);
				 if (x && !strcmp(x, cn))
					 return fs;
			 }
		}
	}

	/* non-canonicalized paths in mnt_tab */
	if (ntags <= mnt_tab_get_nents(tb)) {
		mnt_reset_iter(&itr, direction);
		while(mnt_tab_next_fs(tb, &itr, &fs) == 0) {
			p = mnt_fs_get_srcpath(fs);
			if (p)
				p = mnt_resolve_path(p, tb->cache);
			if (p && strcmp(cn, p) == 0)
				return fs;
		}
	}

	return NULL;
}


/**
 * mnt_tab_find_tag:
 * @tb: tab pointer
 * @tag: tag name (e.g "LABEL", "UUID", ...)
 * @val: tag value
 * @direction: MNT_ITER_{FORWARD,BACKWARD}
 *
 * Try to lookup an entry in given tab, first attempt is lookup by @tag and
 * @val, for the second attempt the tag is evaluated (converted to the device
 * name) and mnt_tab_find_srcpath() is preformed. The second attempt is not
 * performed when @tb cache is not set (see mnt_tab_set_cache()).

 * Returns a tab entry or NULL.
 */
mnt_fs *mnt_tab_find_tag(mnt_tab *tb, const char *tag,
			const char *val, int direction)
{
	mnt_iter itr;
	mnt_fs *fs = NULL;

	assert(tb);
	assert(tag);
	assert(val);

	if (!tb || !tag || !val)
		return NULL;

	DBG(DEBUG_TAB, fprintf(stderr,
		"libmount: %s: lookup by TAG: %s %s\n", tb->filename, tag, val));

	/* look up by TAG */
	mnt_reset_iter(&itr, direction);
	while(mnt_tab_next_fs(tb, &itr, &fs) == 0) {
		if (fs->tagname && fs->tagval &&
		    strcmp(fs->tagname, tag) == 0 &&
		    strcmp(fs->tagval, val) == 0)
			return fs;
	}

	if (tb->cache) {
		/* look up by device name */
		char *cn = mnt_resolve_tag(tag, val, tb->cache);
		if (cn)
			return mnt_tab_find_srcpath(tb, cn, direction);
	}
	return NULL;
}

/**
 * mnt_tab_find_source:
 * @tb: tab pointer
 * @source: TAG or path
 *
 * This is high-level API for mnt_tab_find_{srcpath,tag}. You needn't to care
 * about @source format (device, LABEL, UUID, ...). This function parses @source
 * and calls mnt_tab_find_tag() or mnt_tab_find_srcpath().
 *
 * Returns a tab entry or NULL.
 */
mnt_fs *mnt_tab_find_source(mnt_tab *tb, const char *source, int direction)
{
	mnt_fs *fs = NULL;

	assert(tb);
	assert(source);

	if (!tb || !source)
		return NULL;

	DBG(DEBUG_TAB, fprintf(stderr,
		"libmount: %s: lookup SOURCE: %s\n", tb->filename, source));

	if (strchr(source, '=')) {
		char *tag, *val;

		if (blkid_parse_tag_string(source, &tag, &val) == 0) {

			fs = mnt_tab_find_tag(tb, tag, val, direction);

			free(tag);
			free(val);
		}
	} else
		fs = mnt_tab_find_srcpath(tb, source, direction);

	return fs;
}


/**
 * mnt_tab_fprintf:
 * @f: FILE
 * @fmt: per line printf-like format string (see MNT_MFILE_PRINTFMT)
 * @tb: tab pointer
 *
 * Returns 0 on success, -1 in case of error.
 */
int mnt_tab_fprintf(mnt_tab *tb, FILE *f, const char *fmt)
{
	mnt_iter itr;
	mnt_fs *fs;

	assert(f);
	assert(fmt);
	assert(tb);

	if (!f || !fmt || !tb)
		return -1;

	mnt_reset_iter(&itr, MNT_ITER_FORWARD);
	while(mnt_tab_next_fs(tb, &itr, &fs) == 0) {
		if (mnt_fs_fprintf(fs, f, fmt) == -1)
			return -1;
	}

	return 0;
}

/**
 * mnt_tab_update_file
 * @tb: tab pointer
 *
 * Writes tab to disk. Don't forget to lock the file (see mnt_lock()).
 *
 * Returns 0 on success, -1 in case of error.
 */
int mnt_tab_update_file(mnt_tab *tb)
{
	FILE *f = NULL;
	char tmpname[PATH_MAX];
	const char *filename;
	struct stat st;
	int fd;

	assert(tb);
	if (!tb)
		goto error;

	filename = mnt_tab_get_name(tb);
	if (!filename)
		goto error;

	if (snprintf(tmpname, sizeof(tmpname), "%s.tmp", filename)
						>= sizeof(tmpname))
		goto error;

	f = fopen(tmpname, "w");
	if (!f)
		goto error;

	if (mnt_tab_fprintf(tb, f, MNT_MFILE_PRINTFMT) != 0)
		goto error;

	fd = fileno(f);

	if (fchmod(fd, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) < 0)
		goto error;

	/* Copy uid/gid from the present file before renaming. */
	if (stat(filename, &st) == 0) {
		if (fchown(fd, st.st_uid, st.st_gid) < 0)
			goto error;
	}

	fclose(f);
	f = NULL;

	if (rename(tmpname, filename) < 0)
		goto error;

	return 0;
error:
	if (f)
		fclose(f);
	return -1;
}

#ifdef TEST_PROGRAM
int test_strerr(struct mtest *ts, int argc, char *argv[])
{
	char buf[BUFSIZ];
	mnt_tab *tb;
	int i;

	tb = mnt_new_tab("-test-");
	if (!tb)
		goto err;

	for (i = 0; i < 10; i++) {
		mnt_fs *fs = mnt_new_fs();
		if (!fs)
			goto err;
		if (i % 2)
			fs->flags |= MNT_FS_ERROR;	/* mark entry as broken */
		fs->lineno = i+1;
		mnt_tab_add_fs(tb, fs);
	}

	printf("\tadded %d valid lines\n", mnt_tab_get_nents(tb));
	printf("\tadded %d broken lines\n", mnt_tab_get_nerrs(tb));

	if (!mnt_tab_get_nerrs(tb))		/* report broken entries */
		goto err;
	mnt_tab_strerror(tb, buf, sizeof(buf));
	printf("\t%s\n", buf);

	mnt_free_tab(tb);
	return 0;
err:
	return -1;
}

mnt_tab *create_tab(const char *file)
{
	mnt_tab *tb;

	if (!file)
		return NULL;
	tb = mnt_new_tab(file);
	if (!tb)
		goto err;
	if (mnt_tab_parse_file(tb) != 0)
		goto err;
	if (mnt_tab_get_nerrs(tb)) {
		char buf[BUFSIZ];
		mnt_tab_strerror(tb, buf, sizeof(buf));
		fprintf(stderr, "%s\n", buf);
		goto err;
	}
	return tb;
err:
	mnt_free_tab(tb);
	return NULL;
}

int test_parse(struct mtest *ts, int argc, char *argv[])
{
	mnt_tab *tb;

	tb = create_tab(argv[1]);
	if (!tb)
		return -1;

	mnt_tab_fprintf(tb, stdout, MNT_MFILE_PRINTFMT);
	mnt_free_tab(tb);
	return 0;
}

int test_find(struct mtest *ts, int argc, char *argv[], int dr)
{
	mnt_tab *tb;
	mnt_fs *fs = NULL;
	mnt_cache *mpc;
	const char *file, *find, *what;

	if (argc != 4) {
		fprintf(stderr, "try --help\n");
		goto err;
	}

	file = argv[1], find = argv[2], what = argv[3];

	tb = create_tab(file);
	if (!tb)
		goto err;

	/* create a cache for canonicalized paths */
	mpc = mnt_new_cache();
	if (!mpc)
		goto err;
	mnt_tab_set_cache(tb, mpc);

	if (strcasecmp(find, "source") == 0)
		fs = mnt_tab_find_source(tb, what, dr);
	else if (strcasecmp(find, "target") == 0)
		fs = mnt_tab_find_target(tb, what, dr);

	if (!fs)
		fprintf(stderr, "%s: not found %s '%s'\n", file, find, what);
	else {
		const char *s = mnt_fs_get_srcpath(fs);
		if (s)
			printf("%s", s);
		else {
			const char *tag, *val;
			mnt_fs_get_tag(fs, &tag, &val);
			printf("%s=%s", tag, val);
		}
		printf("|%s|%s\n", mnt_fs_get_target(fs),
				mnt_fs_get_optstr(fs));
	}
	mnt_free_tab(tb);
	mnt_free_cache(mpc);
	return 0;
err:
	return -1;
}

int test_find_bw(struct mtest *ts, int argc, char *argv[])
{
	return test_find(ts, argc, argv, MNT_ITER_BACKWARD);
}

int test_find_fw(struct mtest *ts, int argc, char *argv[])
{
	return test_find(ts, argc, argv, MNT_ITER_FORWARD);
}

int main(int argc, char *argv[])
{
	struct mtest tss[] = {
	{ "--strerror", test_strerr,       "        test tab error reporting" },
	{ "--parse",    test_parse,        "<file>  parse and print tab" },
	{ "--find-forward",  test_find_fw, "<file> <source|target> <string>" },
	{ "--find-backward", test_find_bw, "<file> <source|target> <string>" },
	{ NULL }
	};

	return mnt_run_test(tss, argc, argv);
}

#endif /* TEST_PROGRAM */