Quantcast
Channel: Linux command to retrieve a byte range from a file - Server Fault
Viewing all articles
Browse latest Browse all 7

Answer by Oki Abrian for Linux command to retrieve a byte range from a file

$
0
0

u can use splice in linux, this example in c program, and then name it fastcat.c

#define _GNU_SOURCE#include <stdio.h>#include <string.h>#include <stdlib.h>#include <fcntl.h>#include <unistd.h>#include <sys/stat.h>#define __IO_BUFSIZE (8192)int main(int argc, char *argv[]){loff_t in_off = 0;loff_t out_off = 0;off_t lenData;int StartReadDataAt=-1;int AmountOfData=-1;char location[100];int err = -1;static int buf_size = __IO_BUFSIZE;struct stat stbuf;for (int i = 1; i < argc; ++i){    if (argv[i][0] == '-'&& argv[i][1] == 'f') //argument for StartReadDataAt    {    int totalIndeks =strlen(argv[i])-3;    char data[totalIndeks];    strncpy(data, argv[i] + 3,totalIndeks);    data[totalIndeks]='\0';    StartReadDataAt=atoi(data);    StartReadDataAt=StartReadDataAt-1;    if(StartReadDataAt == -1){        StartReadDataAt=0;        }    }    else if (argv[i][0] == '-'&& argv[i][1] == 'c') //argument for AmountOfData    {    int totalIndeks =strlen(argv[i])-3;        char data[totalIndeks];        strncpy(data, argv[i] + 3, totalIndeks );    data[totalIndeks]='\0';        AmountOfData=atoi(data);    }    else                        //argument for Location    {        strncpy(location, argv[i], strlen(argv[i]));    location[strlen(argv[i])]='\0';    }}    FILE* f = fopen(location, "r");         //open    if(!f) {        printf("file %s not found:",location);        exit;    }    int IN = fileno(f);    int p[2];     if(pipe(p) < 0)    {    close(p[0]);    close(p[1]);    close(IN);    //perror("pipe:");        exit;    }    if(fstat(IN, &stbuf) < 0)    {       // perror("fstat:");        close(p[0]);        close(p[1]);        close(IN);    exit;    }    lenData = stbuf.st_size;    if ( AmountOfData != -1 && StartReadDataAt != -1 ){                in_off=StartReadDataAt;        if((StartReadDataAt+AmountOfData) > lenData){            lenData= lenData-StartReadDataAt;        }else{                    lenData= AmountOfData;        }    }    else if ( AmountOfData != -1 ){        lenData= AmountOfData;    }else if(StartReadDataAt != -1){        in_off=StartReadDataAt;        lenData=lenData-StartReadDataAt;    }       // fprintf( stderr, "lendData : %i\n", lenData);    while(lenData>0)    {    if(buf_size > lenData){        buf_size = lenData;    }    loff_t len = splice(IN, &in_off, p[1], NULL, buf_size, SPLICE_F_MOVE);  //input to p[1]        if(len < 0)        {        close(p[0]);            close(p[1]);            close(IN);            exit;    }        lenData=lenData-len;    //fprintf( stderr, "lendData : %i\n", lenData);        err = splice(p[0], NULL,STDOUT_FILENO, NULL, len, SPLICE_F_MOVE); //p[0] to STDOUT_FILENO    }    err = 0;}

compile

gcc ./fastcat.c -o ./fastcat

example

./fastcat -f=100 -c=10000 /root/document

Viewing all articles
Browse latest Browse all 7

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>