基于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
相关推荐
阿凤213 分钟前
uniapp运行到app端怎么打开文件
android·前端·javascript·uni-app
青城山下————5 分钟前
CentOS 7(3.10 内核)成功安装 MySQL 5.7 全流程实践(附问题总结)
linux·mysql·centos
卡比巴拉—林16 分钟前
Linux SSH免密登录配置(双向免密)
linux·运维·ssh
学习使我健康43 分钟前
Android 事件分发机制
android·java·前端
贵沫末1 小时前
Claude Code For VS Code安装以及跳过认证
android
环流_1 小时前
【多线程初识】
linux·运维·服务器
CAAS_IFR_zp1 小时前
vConTACT3: 机器学习实现可扩展和系统的病毒层级分类
linux·运维·服务器
一个人旅程~1 小时前
Linux Mint(Ubuntu)如何在没有网卡驱动情况下利用手机上网安装旧电脑网卡驱动程序指导书
linux·windows·经验分享·电脑
00后程序员张1 小时前
完整教程:如何将iOS应用程序提交到App Store审核和上架
android·macos·ios·小程序·uni-app·cocoa·iphone
aq55356001 小时前
ThinkPHP5.x核心特性全解析
android·数据库·oracle·php·laravel