【发布时间】:2016-02-28 07:14:59
【问题描述】:
我正在开发一个项目,该项目试图在文件系统(例如 ext2)中搜索特定字节(例如 0xAB)。我能够使用malloc()、realloc() 和memchr() 找到我需要的东西,但它似乎很慢,所以我正在考虑使用mmap()。我想做的是找到一个特定的字节,然后将它们复制到一个结构中,所以我有两个问题:(1)使用mmap()是最好的策略,(2)为什么下面的代码不起作用(我收到 EINVAL 错误)?
更新:以下程序编译并运行,但我仍然有几个问题:
1) 它不会在大文件上显示正确的文件大小(对于 1GB 闪存驱动器显示正确的大小,但对于 32GB 则不正确)*。
2) 没有正确搜索映射**。
*THIS 是使用stat64() 获得正确尺寸的可能解决方案吗?如果是这样,是我在 Makefile 中添加的内容吗?我没有经常使用makefile,所以我不知道如何添加类似的东西。
**这甚至是正确的搜索方式吗?
#define _LARGEFILE64_SOURCE
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0)
int main(int argc, char **argv) {
int fd = open("/dev/sdb1", O_RDONLY);
if(fd < 0) {
printf("Error %s\n", strerror(errno));
return -1;
}
const char * map;
off64_t size;
size = lseek64(fd, 0, SEEK_END);
printf("file size: %llu\n", size);
lseek64(fd, 0, SEEK_SET);
map = mmap(0, size, PROT_READ, MAP_SHARED, fd, 0);
if (map == MAP_FAILED) { handle_error("mmap error"); }
printf("Searching for magic numbers...\n");
for (i=0; i < size; i++) {
if(map[i] == 0X53 && map[i + 1] == 0XEF) {
if ((map[i-32] == 0X00 && map[i-31] == 0X00) ||
(map[i-32] == 0X01 && map[i-31] == 0X00) ||
(map[i-32] == 0X02 && map[i-31] == 0X00)) {
if(j <= 5) {
printf("superblock %d found\n", j);
++j;
} else break;
int q;
for(q=0; q<j; q++) {
printf("SUPERBLOCK[%d]: %d\n", q+1, sb_pos[q]);
}
fclose(fd);
munmap(map, size);
return 0;
}
感谢您的帮助。
【问题讨论】:
-
您应该检查 errno 变量以了解 mmap 失败的原因
-
你读过THIS的问题吗?
-
它可能会失败,因为它找不到您请求的长度的连续内存条(
size)。 -
您的所有资源限制设置为多少?特别是最大虚拟内存? (
ulimit -v, IIRC) 另外,size的回报是什么?它是否有意义并与您的磁盘大小相匹配? -
@Shark 如果程序无法找到连续的内存,那么在搜索整个文件系统之前最好使用
malloc()和realloc()X 字节?