LLVM 23.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/Support/BLAKE3.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
26 std::error_code EC = sys::fs::status(FD, Status);
27 if (EC)
28 return createFileError(FilePath, EC);
29 // FIXME: Do we need to add a hash of the data size? If we remove that we can
30 // avoid needing to read the file size before reading the file contents.
31 H.updateSize(Status.getSize());
32
33 size_t ChunkSize = sys::fs::DefaultReadChunkSize;
35 Buffer.resize_for_overwrite(ChunkSize);
36 for (;;) {
37 Expected<size_t> ReadBytes =
38 sys::fs::readNativeFile(FD, MutableArrayRef(Buffer.begin(), ChunkSize));
39 if (!ReadBytes)
40 return ReadBytes.takeError();
41 if (*ReadBytes == 0)
42 break;
43 H.Hasher.update(toStringRef(ArrayRef(Buffer).take_front(*ReadBytes)));
44 }
45
46 return H.finish();
47}
48
49// Provide the definition for when using the BLAKE3 hasher.
#define H(x, y, z)
Definition MD5.cpp: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.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
static Expected< HashT > hashFile(StringRef FilePath)
Represents the result of a call to sys::fs::status().
Definition FileSystem.h:222
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.
LLVM_ABI std::error_code status(const Twine &path, file_status &result, bool follow=true)
Get file status as if by POSIX stat().
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
Error createFileError(const Twine &F, Error E)
Concatenate a source file path and/or name with an Error.
Definition Error.h:1399
MutableArrayRef(T &OneElt) -> MutableArrayRef< T >
ArrayRef(const T &OneElt) -> ArrayRef< T >
StringRef toStringRef(bool B)
Construct a string ref from a boolean.