基于fallocate为文件打洞

code

c 复制代码
#define _GNU_SOURCE
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include <sys/types.h>
//#include <string>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <linux/falloc.h>
//using namespace std;

int main(int argc, char *argv[])
{
    long blocks = atoi(argv[1]);
    long SIZE = 4096 * blocks;
    long rec_len = 512*1024;
    long num_rec = SIZE/rec_len;
    long rest_size = SIZE - (rec_len*num_rec);
    char* buf;
    long i = 0;
    long offset=0;
    long len=8192;
    mode_t f_attrib = S_IRUSR|S_IWUSR;
    int fd_ret=open("./1.qwj",O_RDWR|O_CREAT, f_attrib);
    if(fd_ret<0)
        perror("creat file fail");
    else{
        lseek(fd_ret, 0, SEEK_END);

        buf = malloc(512*1024);
        memset(buf, 0xcafe, 512*1024);
        //printf("%ld\n",num_rec);
        for(i=0; i<num_rec; i++)
        {
            write(fd_ret, buf, rec_len);
        }

        if(rest_size)
        {
            write(fd_ret, buf, rest_size);
        }
        fsync(fd_ret);
        free(buf);
        printf("Write OK\n");
    }
    i=0;
    //在i偏移的位置上打一个4k大小的洞
    while (i < SIZE){
        if(fallocate(fd_ret,FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE, i, 4096)<0)
            perror("could not deallocate");
        i = i + len;
    }
    printf("fallocate OK\n");
    return 0;
}

2.交叉编译

bash 复制代码
aarch64-linux-gnu-gcc -static  write_bigfile.c
相关推荐
暗影天帝3 分钟前
BPI-R3 Mini NAND 刷机教程(Webfailsafe 方案)
linux
私人珍藏库6 分钟前
[Android] 视频下载鸟 v20.02 会员
android·人工智能·智能手机·app·工具·多功能
zh_xuan15 分钟前
tv浏览网页工具
android·tv浏览网页
Full Stack Developme17 分钟前
Linux rm-rf 执行后,硬盘空间变化
linux·运维·服务器
插件开发1 小时前
vs2015 cuda c++ cdpSimplePrint范例,递归功能实现演示
linux·c++·算法
java知路1 小时前
centos euler离线下载docker镜像
linux·docker·centos
utf8mb4安全女神2 小时前
MySQL8.0.43的下载安装【二进制安装】【shell脚本】【环境准备】【my.cnf配置】【修改密码】
linux·服务器·网络
凡人叶枫2 小时前
Effective C++ 条款33:避免遮掩继承而来的名字
linux·服务器·开发语言·c++·嵌入式开发
凡人叶枫2 小时前
Effective C++ 条款31:将文件间的编译依存关系降至最低
linux·开发语言·c++·php·嵌入式开发·effective c++
Carson带你学Android2 小时前
Compose 终于上线 FlexBox:换行与弹性伸缩 都轻松搞定!
android·composer