1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
/* fkt_extract.c
fkt_extract -- program to extract a portion of a trace, given by its page
numbers.
Copyright (C) 2003 Samuel Thibault <samuel.thibault@ens-lyon.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <config.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/sendfile.h>
#include <sys/mman.h>
ssize_t slow_copyfile(int out_fd, int in_fd, off_t *offset, size_t count)
{
void *buffer;
size_t done,donebuffer;
ssize_t res;
int page_size = getpagesize();
if( count%page_size )
{
fprintf(stderr,"size is not page-aligned\n");
exit(EXIT_FAILURE);
}
if( (buffer = mmap(NULL, page_size, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_ANONYMOUS,0,0)) == MAP_FAILED
&& !(buffer = malloc(page_size)))
{
perror("allocating page buffer\n");
exit(EXIT_FAILURE);
}
done=0;
while( done<count )
{
donebuffer=0;
while( donebuffer<page_size )
{
if( (res=read(in_fd, buffer+donebuffer, page_size-donebuffer) < 0) )
{
if( res != EINTR )
{
perror("read");
exit(EXIT_FAILURE);
}
}
else if( res==0 )
{
fprintf(stderr,"unexpected end of file\n");
exit(EXIT_FAILURE);
}
else donebuffer+=res;
}
donebuffer=0;
while( donebuffer<page_size )
{
if( (res=write(in_fd, buffer+donebuffer, page_size-donebuffer) < 0) )
{
if( res != EINTR )
{
perror("write");
exit(EXIT_FAILURE);
}
}
else donebuffer+=res;
}
done+=page_size;
}
return done;
}
ssize_t copyfile(int out_fd, int in_fd, off_t *offset, size_t count)
{
size_t done=0;
ssize_t ret;
while( done<count )
{
if( (ret=sendfile(out_fd, in_fd, offset, count-done)) < 0 )
{
if( errno == EINVAL && done==0 )
return slow_copyfile(out_fd, in_fd, offset, count);
perror("sendfile");
exit(EXIT_FAILURE);
}
else if( ret == 0 )
{
fprintf(stderr,"unexpected end of file\n");
exit(EXIT_FAILURE);
}
else done+=ret;
}
return done;
}
void usage( char *argv0 )
{
fprintf(stderr,"\
usage: %s in-file out-file begin end\n\
where begin and end are page numbers, inclusive\n\
",argv0);
exit(EXIT_FAILURE);
}
int main( int argc, char *argv[] )
{
char *c;
int fdin,fdout;
size_t begin, end;
unsigned int offset;
off_t copyoff;
int res;
int page_size = getpagesize();
if( argc < 5 )
usage(argv[0]);
if( (fdin=open(argv[1],O_RDONLY)) < 0 )
{
fprintf(stderr,"couldn't open %s\n",argv[1]);
perror("open");
exit(EXIT_FAILURE);
}
if( (fdout=open(argv[2],O_WRONLY|O_CREAT|O_TRUNC,0664)) < 0 )
{
fprintf(stderr,"couldn't open %s\n",argv[2]);
perror("open");
exit(EXIT_FAILURE);
}
begin=strtoll(argv[3],&c,0)*page_size;
if( c == argv[3] )
usage(argv[0]);
end=(strtoll(argv[4],&c,0)+1)*page_size;
if( c == argv[4] )
usage(argv[0]);
if( (res=read(fdin, &offset, sizeof(offset))) < sizeof(offset) )
{
if( res<0 )
perror("read offset");
else
fprintf(stderr,"Unexpected end of file\n");
exit(EXIT_FAILURE);
}
if( offset <= 32 || offset%page_size )
{
fprintf(stderr,"incorrect offset\n");
exit(EXIT_FAILURE);
}
lseek(fdin,0,SEEK_SET);
/* copy header */
copyoff=0;
copyfile(fdout,fdin,©off,offset);
/* copy data */
copyoff=offset+begin;
copyfile(fdout,fdin,©off,end-begin);
/* and that's it :) */
return EXIT_SUCCESS;
}
/* vim: ts=4
*/
|