read() can return fewer bytes than requested

Sometimes read returns fewer bytes than requested. read() only read at
most 0x7ffff000 bytes.

Bug: 376247649
Test: manual, make mkbootfs, mkbootfs out/target/product../VENDOR_BOOT
Change-Id: I8cbbae40c5f5c6c54d19bf77e9a801ed3390ed48
This commit is contained in:
terryguan 2024-10-29 13:20:46 -07:00
parent 95de81f71f
commit e7f1d41237

View file

@ -19,6 +19,9 @@
#include <private/android_filesystem_config.h>
#include <private/fs_config.h>
#include <android-base/file.h>
#include <string>
/* NOTES
**
** - see https://www.kernel.org/doc/Documentation/early-userspace/buffer-format.txt
@ -212,20 +215,12 @@ static void _archive(char *in, char *out, int ilen, int olen)
if(lstat(in, &s)) err(1, "could not stat '%s'", in);
if(S_ISREG(s.st_mode)){
int fd = open(in, O_RDONLY);
if(fd < 0) err(1, "cannot open '%s' for read", in);
char* tmp = (char*) malloc(s.st_size);
if(tmp == 0) errx(1, "cannot allocate %zd bytes", s.st_size);
if(read(fd, tmp, s.st_size) != s.st_size) {
err(1, "cannot read %zd bytes", s.st_size);
std::string content;
if (!android::base::ReadFileToString(in, &content)) {
err(1, "cannot read '%s'", in);
}
_eject(&s, out, olen, tmp, s.st_size);
free(tmp);
close(fd);
_eject(&s, out, olen, content.data(), content.size());
} else if(S_ISDIR(s.st_mode)) {
_eject(&s, out, olen, 0, 0);
_archive_dir(in, out, ilen, olen);