]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Host/common/FileSystem.cpp
MFV r319741: 8156 dbuf_evict_notify() does not need dbuf_evict_lock
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Host / common / FileSystem.cpp
1 //===-- FileSystem.cpp ------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "lldb/Host/FileSystem.h"
11
12 #include "llvm/Support/FileSystem.h"
13 #include "llvm/Support/MD5.h"
14
15 #include <algorithm>
16 #include <fstream>
17 #include <vector>
18
19 using namespace lldb;
20 using namespace lldb_private;
21
22 namespace {
23
24 bool CalcMD5(const FileSpec &file_spec, uint64_t offset, uint64_t length,
25              llvm::MD5::MD5Result &md5_result) {
26   llvm::MD5 md5_hash;
27   std::ifstream file(file_spec.GetPath(), std::ios::binary);
28   if (!file.is_open())
29     return false;
30
31   if (offset > 0)
32     file.seekg(offset, file.beg);
33
34   std::vector<char> read_buf(4096);
35   uint64_t total_read_bytes = 0;
36   while (!file.eof()) {
37     const uint64_t to_read =
38         (length > 0) ? std::min(static_cast<uint64_t>(read_buf.size()),
39                                 length - total_read_bytes)
40                      : read_buf.size();
41     if (to_read == 0)
42       break;
43
44     file.read(&read_buf[0], to_read);
45     const auto read_bytes = file.gcount();
46     if (read_bytes == 0)
47       break;
48
49     md5_hash.update(llvm::StringRef(&read_buf[0], read_bytes));
50     total_read_bytes += read_bytes;
51   }
52
53   md5_hash.final(md5_result);
54   return true;
55 }
56
57 } // namespace
58
59 bool FileSystem::CalculateMD5(const FileSpec &file_spec, uint64_t &low,
60                               uint64_t &high) {
61   return CalculateMD5(file_spec, 0, 0, low, high);
62 }
63
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))
68     return false;
69
70   const auto uint64_res = reinterpret_cast<const uint64_t *>(md5_result);
71   high = uint64_res[0];
72   low = uint64_res[1];
73
74   return true;
75 }
76
77 bool FileSystem::CalculateMD5AsString(const FileSpec &file_spec,
78                                       std::string &digest_str) {
79   return CalculateMD5AsString(file_spec, 0, 0, digest_str);
80 }
81
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))
87     return false;
88
89   llvm::SmallString<32> result_str;
90   llvm::MD5::stringifyResult(md5_result, result_str);
91   digest_str = result_str.c_str();
92   return true;
93 }
94
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);
99   if (ec)
100     return llvm::sys::TimePoint<>();
101   return status.getLastModificationTime();
102 }