基于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
相关推荐
C_心欲无痕1 天前
ts - tsconfig.json配置讲解
linux·前端·ubuntu·typescript·json
冰西瓜6001 天前
国科大2025操作系统高级教程期末回忆版
linux
HIT_Weston1 天前
93、【Ubuntu】【Hugo】搭建私人博客:面包屑(一)
linux·运维·ubuntu
cuijiecheng20181 天前
Linux下Beyond Compare过期
linux·运维·服务器
HIT_Weston1 天前
92、【Ubuntu】【Hugo】搭建私人博客:侧边导航栏(六)
linux·运维·ubuntu
CodeAllen嵌入式1 天前
Windows 11 本地安装 WSL 支持 Ubuntu 24.04 完整指南
linux·运维·ubuntu
贺biubiu1 天前
2025 年终总结|总有那么一个人,会让你千里奔赴...
android·程序员·年终总结
xuekai200809011 天前
mysql-组复制 -8.4.7 主从搭建
android·adb
码农小韩1 天前
基于Linux的C++学习——指针
linux·开发语言·c++·学习·算法
nono牛1 天前
ps -A|grep gate
android