Use ReadFileToString() in GetHash()

On some devices the previous implementation of GetHash() was failing at
reading files, returning "Bad file descriptor" error.
Using ReadFileToString() that seems to have fixed the issue.

Test: libsnapshot_test
Change-Id: Ie3598d8bad5b3db1cf816c120e6922a066aa2743
Signed-off-by: Alessio Balsini <balsini@google.com>
This commit is contained in:
Alessio Balsini 2019-10-15 18:49:44 +01:00
parent e34d239023
commit 0355d22d64
1 changed files with 6 additions and 13 deletions

View File

@ -92,21 +92,14 @@ std::string ToHexString(const uint8_t* buf, size_t len) {
}
std::optional<std::string> GetHash(const std::string& path) {
unique_fd fd(open(path.c_str(), O_RDONLY));
char buf[4096];
std::string content;
if (!android::base::ReadFileToString(path, &content, true)) {
PLOG(ERROR) << "Cannot access " << path;
return std::nullopt;
}
SHA256_CTX ctx;
SHA256_Init(&ctx);
while (true) {
ssize_t n = TEMP_FAILURE_RETRY(read(fd.get(), buf, sizeof(buf)));
if (n < 0) {
PLOG(ERROR) << "Cannot read " << path;
return std::nullopt;
}
if (n == 0) {
break;
}
SHA256_Update(&ctx, buf, n);
}
SHA256_Update(&ctx, content.c_str(), content.size());
uint8_t out[32];
SHA256_Final(out, &ctx);
return ToHexString(out, sizeof(out));