Commit d1a31446 authored by Lei Zhang's avatar Lei Zhang Committed by Commit Bot

Refactor PpdCache's FindImpl() function.

- Use early returns to avoid 5 levels of indentations.
- Call base::File::GetInfo() earlier to retrieve the file size, and
  avoid making a separate GetLength() call.
- Use std::vector and avoid a memory allocation with "new".

Change-Id: I8a036047df1b09ac0871ec6b264935c91c9b57c9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2055996
Commit-Queue: Sean Kau <skau@chromium.org>
Reviewed-by: default avatarSean Kau <skau@chromium.org>
Cr-Commit-Position: refs/heads/master@{#742524}
parent 262a71bd
...@@ -60,30 +60,31 @@ PpdCache::FindResult FindImpl(const base::FilePath& cache_dir, ...@@ -60,30 +60,31 @@ PpdCache::FindResult FindImpl(const base::FilePath& cache_dir,
base::File file(FilePathForKey(cache_dir, key), base::File file(FilePathForKey(cache_dir, key),
base::File::FLAG_OPEN | base::File::FLAG_READ); base::File::FLAG_OPEN | base::File::FLAG_READ);
if (file.IsValid()) { base::File::Info info;
int64_t len = file.GetLength(); if (!file.IsValid() || !file.GetInfo(&info))
if (len >= static_cast<int64_t>(crypto::kSHA256Length) && return result;
len <= static_cast<int64_t>(kMaxPpdSizeBytes) +
static_cast<int64_t>(crypto::kSHA256Length)) { if (info.size < static_cast<int64_t>(crypto::kSHA256Length) ||
std::unique_ptr<char[]> buf(new char[len]); info.size > static_cast<int64_t>(kMaxPpdSizeBytes) +
if (file.ReadAtCurrentPos(buf.get(), len) == len) { static_cast<int64_t>(crypto::kSHA256Length)) {
base::StringPiece contents(buf.get(), len - crypto::kSHA256Length); return result;
base::StringPiece checksum(buf.get() + len - crypto::kSHA256Length, }
crypto::kSHA256Length);
if (crypto::SHA256HashString(contents) == checksum) { std::vector<char> buf(info.size);
base::File::Info info; if (file.ReadAtCurrentPos(buf.data(), info.size) != info.size)
if (file.GetInfo(&info)) { return result;
result.success = true;
result.age = base::Time::Now() - info.last_modified; base::StringPiece contents(buf.data(), info.size - crypto::kSHA256Length);
result.contents = std::string(contents); base::StringPiece checksum(buf.data() + info.size - crypto::kSHA256Length,
} crypto::kSHA256Length);
} else { if (crypto::SHA256HashString(contents) != checksum) {
LOG(ERROR) << "Bad checksum for cache key " << key; LOG(ERROR) << "Bad checksum for cache key " << key;
} return result;
}
}
} }
result.success = true;
result.age = base::Time::Now() - info.last_modified;
result.contents = std::string(contents);
return result; return result;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment