1 //===-- FileSystem.cpp ------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Host/FileSystem.h"
12 #include "llvm/Support/FileSystem.h"
13 #include "llvm/Support/MD5.h"
20 using namespace lldb_private;
24 bool CalcMD5(const FileSpec &file_spec, uint64_t offset, uint64_t length,
25 llvm::MD5::MD5Result &md5_result) {
27 std::ifstream file(file_spec.GetPath(), std::ios::binary);
32 file.seekg(offset, file.beg);
34 std::vector<char> read_buf(4096);
35 uint64_t total_read_bytes = 0;
37 const uint64_t to_read =
38 (length > 0) ? std::min(static_cast<uint64_t>(read_buf.size()),
39 length - total_read_bytes)
44 file.read(&read_buf[0], to_read);
45 const auto read_bytes = file.gcount();
49 md5_hash.update(llvm::StringRef(&read_buf[0], read_bytes));
50 total_read_bytes += read_bytes;
53 md5_hash.final(md5_result);
59 bool FileSystem::CalculateMD5(const FileSpec &file_spec, uint64_t &low,
61 return CalculateMD5(file_spec, 0, 0, low, high);
64 bool FileSystem::CalculateMD5(const FileSpec &file_spec, uint64_t offset,
65 uint64_t length, uint64_t &low, uint64_t &high) {
66 llvm::MD5::MD5Result md5_result;
67 if (!CalcMD5(file_spec, offset, length, md5_result))
70 const auto uint64_res = reinterpret_cast<const uint64_t *>(md5_result);
77 bool FileSystem::CalculateMD5AsString(const FileSpec &file_spec,
78 std::string &digest_str) {
79 return CalculateMD5AsString(file_spec, 0, 0, digest_str);
82 bool FileSystem::CalculateMD5AsString(const FileSpec &file_spec,
83 uint64_t offset, uint64_t length,
84 std::string &digest_str) {
85 llvm::MD5::MD5Result md5_result;
86 if (!CalcMD5(file_spec, offset, length, md5_result))
89 llvm::SmallString<32> result_str;
90 llvm::MD5::stringifyResult(md5_result, result_str);
91 digest_str = result_str.c_str();
95 llvm::sys::TimePoint<>
96 FileSystem::GetModificationTime(const FileSpec &file_spec) {
97 llvm::sys::fs::file_status status;
98 std::error_code ec = llvm::sys::fs::status(file_spec.GetPath(), status);
100 return llvm::sys::TimePoint<>();
101 return status.getLastModificationTime();