Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Issue #23] Allow larger than 2GB files to be read. #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.*.sw*
.deps/*
.libs/*
*.lo
Expand All @@ -9,3 +10,20 @@ NEWS
README
ChangeLog
COPYING
INSTALL
Makefile
Makefile.in
aclocal.m4
autom4te.cache/
compile
config.h
config.h.in
config.log
config.status
configure
depcomp
install-sh
missing
stamp-h1
/bsdiff
/bspatch
23 changes: 21 additions & 2 deletions bsdiff.c
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,25 @@ static int bz2_write(struct bsdiff_stream* stream, const void* buffer, int size)
return 0;
}

static off_t readFileToBuffer(int fd, uint8_t* buffer, off_t bufferSize)
{
off_t bytesRead = 0;
int ret;
while (bytesRead < bufferSize)
{
ret = read(fd, buffer + bytesRead, bufferSize - bytesRead);
if (ret > 0)
{
bytesRead += ret;
}
else
{
break;
}
}
return bytesRead;
}

int main(int argc,char *argv[])
{
int fd;
Expand All @@ -397,7 +416,7 @@ int main(int argc,char *argv[])
((oldsize=lseek(fd,0,SEEK_END))==-1) ||
((old=malloc(oldsize+1))==NULL) ||
(lseek(fd,0,SEEK_SET)!=0) ||
(read(fd,old,oldsize)!=oldsize) ||
(readFileToBuffer(fd,old,oldsize)!=oldsize) ||
(close(fd)==-1)) err(1,"%s",argv[1]);


Expand All @@ -407,7 +426,7 @@ int main(int argc,char *argv[])
((newsize=lseek(fd,0,SEEK_END))==-1) ||
((new=malloc(newsize+1))==NULL) ||
(lseek(fd,0,SEEK_SET)!=0) ||
(read(fd,new,newsize)!=newsize) ||
(readFileToBuffer(fd,new,newsize)!=newsize) ||
(close(fd)==-1)) err(1,"%s",argv[2]);

/* Create the patch file */
Expand Down
42 changes: 40 additions & 2 deletions bspatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,44 @@ static int bz2_read(const struct bspatch_stream* stream, void* buffer, int lengt
return 0;
}

static off_t readFileToBuffer(int fd, uint8_t* buffer, off_t bufferSize)
{
off_t bytesRead = 0;
int ret;
while (bytesRead < bufferSize)
{
ret = read(fd, buffer + bytesRead, bufferSize - bytesRead);
if (ret > 0)
{
bytesRead += ret;
}
else
{
break;
}
}
return bytesRead;
}

static off_t writeFileFromBuffer(int fd, uint8_t* buffer, off_t bufferSize)
{
off_t bytesWritten = 0;
int ret;
while (bytesWritten < bufferSize)
{
ret = write(fd, buffer + bytesWritten, bufferSize - bytesWritten);
if (ret > 0)
{
bytesWritten += ret;
}
else
{
break;
}
}
return bytesWritten;
}

int main(int argc,char * argv[])
{
FILE * f;
Expand Down Expand Up @@ -160,7 +198,7 @@ int main(int argc,char * argv[])
((oldsize=lseek(fd,0,SEEK_END))==-1) ||
((old=malloc(oldsize+1))==NULL) ||
(lseek(fd,0,SEEK_SET)!=0) ||
(read(fd,old,oldsize)!=oldsize) ||
(readFileToBuffer(fd,old,oldsize)!=oldsize) ||
(fstat(fd, &sb)) ||
(close(fd)==-1)) err(1,"%s",argv[1]);
if((new=malloc(newsize+1))==NULL) err(1,NULL);
Expand All @@ -179,7 +217,7 @@ int main(int argc,char * argv[])

/* Write the new file */
if(((fd=open(argv[2],O_CREAT|O_TRUNC|O_WRONLY,sb.st_mode))<0) ||
(write(fd,new,newsize)!=newsize) || (close(fd)==-1))
(writeFileFromBuffer(fd,new,newsize)!=newsize) || (close(fd)==-1))
err(1,"%s",argv[2]);

free(new);
Expand Down