?? mapfile.c
字號:
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/ioctl.h>#include <sys/fcntl.h>#include <sys/stat.h>#include <sys/types.h>#include <linux/fs.h>extern void *xmalloc (size_t size);extern void *xrealloc (void *ptr, size_t size);extern char *boot_dev;extern int verbose;static dev_t boot_dev_nr = -1;dev_t get_dev (const char *path){ struct stat statbuf; if (stat (path, &statbuf) < 0) { perror ("stat"); return -1; } if (S_ISBLK(statbuf.st_mode)) return statbuf.st_rdev; else return statbuf.st_dev;}dev_t get_boot_dev (void){ if (boot_dev_nr == (dev_t)-1) boot_dev_nr = get_dev (boot_dev); return boot_dev_nr;}/* * get the block map for a file, checking that it is on the root partition */int map_file(const char *path, unsigned long **bmap){ int fild; struct stat statbuf; unsigned long *buffer, blksize, fblocks; unsigned int blw, curblkindex, blk; if (get_boot_dev() == (dev_t)-1) return -1; if (boot_dev_nr != get_dev (path)) { fprintf(stderr, "Kernel '%s' is not on the boot device '%s' " "- ignored\n", path, boot_dev); return -1; } fild = open(path, O_RDONLY, 0); if (fild < 0) { perror("open"); return -1; } if (ioctl(fild, FIGETBSZ, &blksize) < 0) { perror("ioctl FIGETBSZ"); close(fild); return -1; } if (fstat(fild, &statbuf) < 0) { perror("fstat"); close(fild); return -1; } fblocks = (statbuf.st_size + blksize - 1) / blksize; if (verbose) printf("map_file(%s, %ld blocks): ", path, fblocks); curblkindex = 0; blw = 64; blk = 0; buffer = xmalloc (blw * 4); buffer[0] = 0xC53A4B2D; buffer[1] = blksize; for (blk = 0; blk < fblocks; blk++) { volatile unsigned long fibmap_blk; fibmap_blk = blk; if (ioctl(fild, FIBMAP, &fibmap_blk) < 0) { perror("ioctl FIBMAP"); close(fild); return -1; } if (verbose) printf("[%05lX] ", fibmap_blk); if (fibmap_blk == 0) { fprintf(stderr, "Unable to map file '%s' (has holes).\n", path); close(fild); return -1; } if (curblkindex >= 2 && buffer[curblkindex] + buffer[curblkindex + 1] == fibmap_blk) buffer[curblkindex + 1] += 1; else { curblkindex += 2; if (curblkindex >= blw) buffer = xrealloc (buffer, (blw *= 2) * 4); buffer[curblkindex] = fibmap_blk; buffer[curblkindex + 1] = 1; } } curblkindex += 2; buffer[curblkindex] = 0; buffer[curblkindex+1] = 0; curblkindex += 2; if (verbose) printf("\n"); if (buffer[0] != 0) *bmap = buffer; return curblkindex * sizeof (unsigned long);}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -