基于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
相关推荐
好的佩奇5 小时前
Dart 之可迭代对象与生成器
android·flutter·dart
_祝你今天愉快6 小时前
Paint Cavans高级绘制:[三] Canvas高阶使用技巧-变换,状态保存,离屏缓冲,粒子特效
android
小乖写代码6 小时前
提交注册验证 · WhatsApp /v2/register
android·网络·账号注册
多弗朗皮卡丘7 小时前
网络基础:数据链路层与其他协议或技术
linux·网络·tcp/ip
二流小码农10 小时前
鸿蒙开发:router组件升级,彻底告别手动配置
android·ios·harmonyos
Digitally11 小时前
如何在安卓设备上使用 4 种简单方法将应用移动到 SD 卡
android
booboosui13 小时前
录一遍就会:8 种步骤、6 类失败,卡住时只修那一步
android
阿杰m13 小时前
Android源码第十五章:Camera2 与 CameraService——一帧图像怎样回到应用
android
dingdingfish13 小时前
TLPI 第52章 读书笔记:POSIX Message Queues
linux·mq·ipc·posix·message·tlpi