栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何创建带有文件孔的文件?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何创建带有文件孔的文件?

dd
命令与
seek
参数一起使用。

dd if=/dev/urandom bs=4096 count=2 of=file_with_holesdd if=/dev/urandom bs=4096 seek=7 count=2 of=file_with_holes

这会为您创建一个从字节8192到字节28671的漏洞。

这是一个示例,说明该文件确实存在漏洞(该

ls -s
命令告诉您文件正在使用多少磁盘块):

$ dd if=/dev/urandom bs=4096 count=2 of=fwh # fwh = file with holes2+0 records in2+0 records out8192 bytes (8.2 kB) copied, 0.00195565 s, 4.2 MB/s$ dd if=/dev/urandom seek=7 bs=4096 count=2 of=fwh2+0 records in2+0 records out8192 bytes (8.2 kB) copied, 0.00152742 s, 5.4 MB/s$ dd if=/dev/zero bs=4096 count=9 of=fwnh # fwnh = file with no holes9+0 records in9+0 records out36864 bytes (37 kB) copied, 0.000510568 s, 72.2 MB/s$ ls -ls fw*16 -rw-rw-r-- 1 hopper hopper 36864 Mar 15 10:25 fwh36 -rw-rw-r-- 1 hopper hopper 36864 Mar 15 10:29 fwnh

如您所见,带孔的文件尽管大小相同,但占用的磁盘块较少。

如果您想要执行此操作的程序,则为:

#include <unistd.h>#include <sys/types.h>#include <stdio.h>#include <fcntl.h>int main(int argc, const char *argv[]){    char random_garbage[8192];     int fd = -1;    if (argc < 2) {        fprintf(stderr, "Usage: %s <filename>n", argv[0]);        return 1;    }    fd = open(argv[1], O_WRonLY | O_CREAT | O_TRUNC, 0666);    if (fd < 0) {        perror("Can't open file: ");        return 2;    }    write(fd, random_garbage, 8192);    lseek(fd, 5 * 4096, SEEK_CUR);    write(fd, random_garbage, 8192);    close(fd);    return 0;}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/376658.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号