Coverage Report

Created: 2026-02-09 06:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmFilePathChecksum.cxx
Line
Count
Source
1
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2
   file LICENSE.rst or https://cmake.org/licensing for details.  */
3
#include "cmFilePathChecksum.h"
4
5
#include <vector>
6
7
#include "cmBase32.h"
8
#include "cmCryptoHash.h"
9
#include "cmMakefile.h"
10
#include "cmSystemTools.h"
11
12
0
cmFilePathChecksum::cmFilePathChecksum() = default;
13
14
cmFilePathChecksum::cmFilePathChecksum(std::string const& currentSrcDir,
15
                                       std::string const& currentBinDir,
16
                                       std::string const& projectSrcDir,
17
                                       std::string const& projectBinDir)
18
0
{
19
0
  this->setupParentDirs(currentSrcDir, currentBinDir, projectSrcDir,
20
0
                        projectBinDir);
21
0
}
22
23
cmFilePathChecksum::cmFilePathChecksum(cmMakefile* makefile)
24
0
{
25
0
  this->setupParentDirs(makefile->GetCurrentSourceDirectory(),
26
0
                        makefile->GetCurrentBinaryDirectory(),
27
0
                        makefile->GetHomeDirectory(),
28
0
                        makefile->GetHomeOutputDirectory());
29
0
}
30
31
void cmFilePathChecksum::setupParentDirs(std::string const& currentSrcDir,
32
                                         std::string const& currentBinDir,
33
                                         std::string const& projectSrcDir,
34
                                         std::string const& projectBinDir)
35
0
{
36
0
  this->parentDirs[0].first = cmSystemTools::GetRealPath(currentSrcDir);
37
0
  this->parentDirs[1].first = cmSystemTools::GetRealPath(currentBinDir);
38
0
  this->parentDirs[2].first = cmSystemTools::GetRealPath(projectSrcDir);
39
0
  this->parentDirs[3].first = cmSystemTools::GetRealPath(projectBinDir);
40
41
0
  this->parentDirs[0].second = "CurrentSource";
42
0
  this->parentDirs[1].second = "CurrentBinary";
43
0
  this->parentDirs[2].second = "ProjectSource";
44
0
  this->parentDirs[3].second = "ProjectBinary";
45
0
}
46
47
std::string cmFilePathChecksum::get(std::string const& filePath) const
48
0
{
49
0
  std::string relPath;
50
0
  std::string relSeed;
51
0
  {
52
0
    std::string const fileReal = cmSystemTools::GetRealPath(filePath);
53
0
    std::string parentDir;
54
    // Find closest project parent directory
55
0
    for (auto const& pDir : this->parentDirs) {
56
0
      if (!pDir.first.empty() &&
57
0
          cmsys::SystemTools::IsSubDirectory(fileReal, pDir.first)) {
58
0
        parentDir = pDir.first;
59
0
        relSeed = pDir.second;
60
0
        break;
61
0
      }
62
0
    }
63
    // Use file system root as fallback parent directory
64
0
    if (parentDir.empty()) {
65
0
      relSeed = "FileSystemRoot";
66
0
      cmsys::SystemTools::SplitPathRootComponent(fileReal, &parentDir);
67
0
    }
68
    // Calculate relative path from project parent directory
69
0
    relPath = cmsys::SystemTools::RelativePath(
70
0
      parentDir, cmsys::SystemTools::GetParentDirectory(fileReal));
71
0
  }
72
73
  // Calculate the file ( seed + relative path ) binary checksum
74
0
  std::vector<unsigned char> hashBytes =
75
0
    cmCryptoHash(cmCryptoHash::AlgoSHA256).ByteHashString(relSeed + relPath);
76
77
  // Convert binary checksum to string
78
0
  return cmBase32Encoder().encodeString(hashBytes.data(), hashBytes.size(),
79
0
                                        false);
80
0
}
81
82
std::string cmFilePathChecksum::getPart(std::string const& filePath,
83
                                        size_t length) const
84
0
{
85
0
  return this->get(filePath).substr(0, length);
86
0
}