File: test.c

package info (click to toggle)
ecryptfs-utils 111-8
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,560 kB
  • sloc: ansic: 18,280; sh: 7,047; makefile: 347; python: 41
file content (132 lines) | stat: -rw-r--r-- 3,165 bytes parent folder | download | duplicates (6)
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
/*
 * Author: Colin King <colin.king@canonical.com>
 *
 * Copyright (C) 2013 Canonical, Ltd.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/fs.h>

static int *get_blocks(const char *filename, int *num_blocks)
{
	int fd, block_size, i;
	int *blocks;
	struct stat statinfo;

	if ((fd = open(filename, O_RDONLY)) < 0) {
		fprintf(stderr, "Cannot open %s\n", filename);
		return NULL;
	}

	if (ioctl(fd, FIGETBSZ, &block_size) < 0) {
		fprintf(stderr, "Cannot get block size\n");
		close(fd);
		return NULL;
	}

	if (fstat(fd, &statinfo) < 0) {
		fprintf(stderr, "Cannot stat %s\n", filename);
		close(fd);
		return NULL;
	}

	*num_blocks = (statinfo.st_size + block_size - 1) / block_size;

	blocks = malloc(sizeof(int) * *num_blocks);
	if (!blocks) {
		fprintf(stderr, "Cannot allocate buffer for %d blocks\n", *num_blocks);
		close(fd);
		return NULL;
	}

	/*
	 *  Collect blocks, some file systems may not support FIBMAP, so
	 *  silently ignore errors.
	 */
	for (i = 0; i < *num_blocks; i++) {
		blocks[i] = i;
		if (ioctl(fd, FIBMAP, &blocks[i]) < 0)
			blocks[i] = 0;
	}
	close(fd);

	return blocks;
}

int check_blocks(
	int *lower_blocks, int lower_num_blocks,
	int *upper_blocks, int upper_num_blocks)
{
	int i, j;

	/*  Upper must not have more blocks than lower */
	if (upper_num_blocks > lower_num_blocks)
		return EXIT_FAILURE;

	/*  Upper must have blocks that are in the lower */
	for (i = 0; i < upper_num_blocks; i++) {
		bool found = false;
		for (j = 0; j < lower_num_blocks; j++) {
			if (upper_blocks[i] == lower_blocks[j]) {
				found = true;
				break;
			}
		}
		if (!found)
			return EXIT_FAILURE;
	}
	return EXIT_SUCCESS;
}

int main(int argc, char **argv) {

	int *lower_blocks, *upper_blocks;
	int lower_num_blocks, upper_num_blocks;
	int rc = EXIT_SUCCESS;

	if (argc != 3) {
		fprintf(stderr, "Usage: %s lower-file upper-file\n", argv[0]);
		exit(EXIT_FAILURE);
	}

	lower_blocks = get_blocks(argv[1], &lower_num_blocks);
	if (!lower_blocks)
		exit(EXIT_FAILURE);

	upper_blocks = get_blocks(argv[2], &upper_num_blocks);
	if (!upper_blocks) {
		free(lower_blocks);
		exit(EXIT_FAILURE);
	}

	rc = check_blocks(lower_blocks, lower_num_blocks,
			  upper_blocks, upper_num_blocks);

	free(upper_blocks);
	free(lower_blocks);

	exit(rc);
}