Merge "read() can return fewer bytes than requested" into main

This commit is contained in:
Terry Guan 2024-10-30 19:29:32 +00:00 committed by Gerrit Code Review
commit 5ce7c6a4b2

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);