LLVM 24.0.0git
BuiltinObjectHasher.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "llvm/ADT/ScopeExit.h"
11
12using namespace llvm;
13using namespace llvm::cas;
14
15template <class HasherT>
18 BuiltinObjectHasher H;
19 H.updateSize(0); // 0 refs
20
22 if (Error E = sys::fs::openNativeFileForRead(FilePath).moveInto(FD))
23 return E;
24
25 llvm::scope_exit CloseFile([&]() { sys::fs::closeFile(FD); });
26
28 if (!FileSize)
29 return createFileError(FilePath, FileSize.getError());
30 H.updateSize(*FileSize);
31
32 size_t ChunkSize = sys::fs::DefaultReadChunkSize;
34 Buffer.resize_for_overwrite(ChunkSize);
35 for (;;) {
36 Expected<size_t> ReadBytes =
37 sys::fs::readNativeFile(FD, MutableArrayRef(Buffer.begin(), ChunkSize));
38 if (!ReadBytes)
39 return ReadBytes.takeError();
40 if (*ReadBytes == 0)
41 break;
42 H.Hasher.update(toStringRef(ArrayRef(Buffer).take_front(*ReadBytes)));
43 }
44
45 return H.finish();
46}
47
48namespace llvm::cas {
50}
#define LLVM_EXPORT_TEMPLATE
Definition Compiler.h:213
#define H(x, y, z)
Definition MD5.cpp:56
This file defines the scope_exit class, which executes user-defined cleanup logic at scope exit.
Represents either an error or a value T.
Definition ErrorOr.h:56
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Tagged union holding either a T or a Error.
Definition Error.h:485
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
void resize_for_overwrite(size_type N)
Like resize, but T is POD, the new values won't be initialized.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Hasher for stored objects in builtin CAS.
static Expected< HashT > hashFile(StringRef FilePath)
LLVM_ABI std::error_code closeFile(file_t &F)
Close the file object.
LLVM_ABI Expected< size_t > readNativeFile(file_t FileHandle, MutableArrayRef< char > Buf)
Reads Buf.size() bytes from FileHandle into Buf.
LLVM_ABI Expected< file_t > openNativeFileForRead(const Twine &Name, OpenFlags Flags=OF_None, SmallVectorImpl< char > *RealPath=nullptr)
Opens the file with the given name in a read-only mode, returning its open file descriptor.
std::error_code file_size(const Twine &Path, uint64_t &Result)
Get file size.
Definition FileSystem.h:696
This is an optimization pass for GlobalISel generic memory operations.
Error createFileError(const Twine &F, Error E)
Concatenate a source file path and/or name with an Error.
Definition Error.h:1415
MutableArrayRef(T &OneElt) -> MutableArrayRef< T >
ArrayRef(const T &OneElt) -> ArrayRef< T >
StringRef toStringRef(bool B)
Construct a string ref from a boolean.
This class wraps the platform specific file handle/descriptor type to provide an unified representati...
Definition File.h:21