Skip to content

Commit 27ffe8a

Browse files
authored
Merge pull request #148 from stmarkevich/bigfile
fix reading and writing big files (e.g. > 2Gb)
2 parents 1fa4d36 + ba2695a commit 27ffe8a

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/patchelf.cc

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,12 @@ static FileContents readFile(std::string fileName,
332332
int fd = open(fileName.c_str(), O_RDONLY);
333333
if (fd == -1) throw SysError(fmt("opening '", fileName, "'"));
334334

335-
if ((size_t) read(fd, contents->data(), size) != size)
335+
size_t bytesRead = 0;
336+
ssize_t portion;
337+
while ((portion = read(fd, contents->data() + bytesRead, size - bytesRead)) > 0)
338+
bytesRead += portion;
339+
340+
if (bytesRead != size)
336341
throw SysError(fmt("reading '", fileName, "'"));
337342

338343
close(fd);
@@ -496,7 +501,12 @@ static void writeFile(std::string fileName, FileContents contents)
496501
if (fd == -1)
497502
error("open");
498503

499-
if (write(fd, contents->data(), contents->size()) != (off_t) contents->size())
504+
size_t bytesWritten = 0;
505+
ssize_t portion;
506+
while ((portion = write(fd, contents->data() + bytesWritten, contents->size() - bytesWritten)) > 0)
507+
bytesWritten += portion;
508+
509+
if (bytesWritten != contents->size())
500510
error("write");
501511

502512
if (close(fd) != 0)

0 commit comments

Comments
 (0)