基于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
相关推荐
苏灿烤鱼8 小时前
一套进程代替八件套,桌面更稳还是单点更大
linux·github·shell
杉氧8 小时前
Flutter 跨平台多端适配与 Android/iOS 一键自动化打包发布
android·前端·flutter
刘梦薇8 小时前
SSH连接失败connection reset解决办法
linux·运维·ubuntu·ssh·腾讯云
Rsingstarzengjx8 小时前
stm32m157 U-boot 测试
linux·运维·服务器
Super 含9 小时前
Android 包体积优化(一):APK 到底大在哪里?从 APK 结构到体积分析
android·ide·vscode
月落汀兰9 小时前
Linux Nginx全套实战通关|静态站点/虚拟主机/HTTPS/PHP/反向代理/七层负载均衡,每行命令逐参数拆解
linux·nginx·https
Super 含9 小时前
Android 包体积优化(四):深入 ReDex——StripDebugInfo、InterDex 与 DEX 重排
android
ShineWinsu9 小时前
对于C++:缓冲区管理的详细解析
linux·c++·面试·编程·笔试·io·缓冲区
安卓与AI研习社9 小时前
主线程明明是空闲的,为什么仍然 ANR?3 个真实 Trace 的证据链复盘
android
张小姐的猫9 小时前
【Linux】网络编程 —— 传输层协议 TCP(下)
linux·运维·服务器·网络·tcp/ip·http·php