-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfibmap.c
63 lines (54 loc) · 1.34 KB
/
fibmap.c
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
/*
* fibmap - List blocks of a file
*
* Written in 2012 by Prashant P Shah <[email protected]>
*
* To the extent possible under law, the author(s) have dedicated
* all copyright and related and neighboring rights to this software
* to the public domain worldwide. This software is distributed
* without any warranty.
*
* You should have received a copy of the CC0 Public Domain Dedication
* along with this software.
* If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/fs.h>
#include <assert.h>
int main(int argc, char **argv)
{
int fd, i, block, blocksize, blkcnt;
struct stat st;
assert(argv[1] != NULL);
fd = open(argv[1], O_RDONLY);
if (fd <= 0) {
perror("error opening file");
goto end;
}
if (ioctl(fd, FIGETBSZ, &blocksize)) {
perror("FIBMAP ioctl failed");
goto end;
}
if (fstat(fd, &st)) {
perror("fstat error");
goto end;
}
blkcnt = (st.st_size + blocksize - 1) / blocksize;
printf("File %s size %d blocks %d blocksize %d\n",
argv[1], (int)st.st_size, blkcnt, blocksize);
for (i = 0; i < blkcnt; i++) {
block = i;
if (ioctl(fd, FIBMAP, &block)) {
perror("FIBMAP ioctl failed");
}
printf("%3d %10d\n", i, block);
}
end:
close(fd);
return 0;
}