-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildCache.cpp
More file actions
243 lines (210 loc) · 4.46 KB
/
BuildCache.cpp
File metadata and controls
243 lines (210 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include "BuildCache.h"
#include "sha256.h"
#include <fstream>
#include <sstream>
#include <iomanip>
#include <cstdlib>
#include <cstring>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
using namespace std;
static bool fileExists( const string &path )
{
return access( path.c_str(), F_OK ) == 0;
}
static bool mkdirRecursive( const string &path )
{
string current;
for ( size_t i = 0; i < path.size(); i++ )
{
current += path[i];
if ( path[i] == '/' && i > 0 )
{
mkdir( current.c_str(), 0755 );
}
}
mkdir( path.c_str(), 0755 );
return fileExists( path );
}
static bool copyFile( const string &src, const string &dst )
{
ifstream in( src, ios::binary );
if ( !in.is_open() )
{
return false;
}
ofstream out( dst, ios::binary );
if ( !out.is_open() )
{
return false;
}
out << in.rdbuf();
return out.good();
}
static bool removeRecursive( const string &path )
{
struct stat st;
if ( stat( path.c_str(), &st ) != 0 )
{
return true;
}
if ( S_ISDIR( st.st_mode ) )
{
DIR *dir = opendir( path.c_str() );
if ( !dir )
{
return false;
}
struct dirent *entry;
while ( ( entry = readdir( dir ) ) != nullptr )
{
if ( strcmp( entry->d_name, "." ) == 0 || strcmp( entry->d_name, ".." ) == 0 )
{
continue;
}
string child = path + "/" + entry->d_name;
if ( !removeRecursive( child ) )
{
closedir( dir );
return false;
}
}
closedir( dir );
return rmdir( path.c_str() ) == 0;
}
else
{
return unlink( path.c_str() ) == 0;
}
}
static string hashToHex( const uint8_t hash[32] )
{
ostringstream ss;
for ( int i = 0; i < 32; i++ )
{
ss << hex << setfill( '0' ) << setw( 2 ) << (int)hash[i];
}
return ss.str();
}
static string readFileContent( const string &path )
{
ifstream f( path, ios::binary );
if ( !f.is_open() )
{
return "";
}
ostringstream ss;
ss << f.rdbuf();
return ss.str();
}
static string extractFilename( const string &path )
{
size_t pos = path.rfind( '/' );
if ( pos == string::npos )
{
return path;
}
return path.substr( pos + 1 );
}
string BuildCache::computeKey( const vector<string> &sourceFiles,
const string &tomlContent,
const vector<string> &depHashes )
{
SHA256_CTX ctx;
sha256_init( &ctx );
for ( const auto &file : sourceFiles )
{
string content = readFileContent( file );
sha256_update( &ctx, (const uint8_t *)content.data(), content.size() );
}
sha256_update( &ctx, (const uint8_t *)tomlContent.data(), tomlContent.size() );
for ( const auto &dep : depHashes )
{
sha256_update( &ctx, (const uint8_t *)dep.data(), dep.size() );
}
uint8_t hash[32];
sha256_final( &ctx, hash );
return hashToHex( hash );
}
bool BuildCache::lookup( const string &hash,
string &aFilePath,
string &bmodFilePath )
{
string dir = getCacheDir() + "/" + hash;
if ( !fileExists( dir ) )
{
return false;
}
/* Scan directory for .a and .bmod files */
DIR *d = opendir( dir.c_str() );
if ( !d )
{
return false;
}
bool foundA = false;
bool foundBmod = false;
struct dirent *entry;
while ( ( entry = readdir( d ) ) != nullptr )
{
string name = entry->d_name;
size_t len = name.size();
if ( len > 2 && name.substr( len - 2 ) == ".a" )
{
aFilePath = dir + "/" + name;
foundA = true;
}
else if ( len > 5 && name.substr( len - 5 ) == ".bmod" )
{
bmodFilePath = dir + "/" + name;
foundBmod = true;
}
}
closedir( d );
return foundA && foundBmod;
}
bool BuildCache::store( const string &hash,
const string &aFile,
const string &bmodFile )
{
string dir = getCacheDir() + "/" + hash;
if ( !mkdirRecursive( dir ) )
{
return false;
}
string aDst = dir + "/" + extractFilename( aFile );
string bmodDst = dir + "/" + extractFilename( bmodFile );
if ( !copyFile( aFile, aDst ) )
{
return false;
}
if ( !copyFile( bmodFile, bmodDst ) )
{
return false;
}
return true;
}
string BuildCache::getCacheDir()
{
const char *xdg = getenv( "XDG_CACHE_HOME" );
if ( xdg && xdg[0] != '\0' )
{
return string( xdg ) + "/blang/objects";
}
const char *home = getenv( "HOME" );
if ( home && home[0] != '\0' )
{
return string( home ) + "/.cache/blang/objects";
}
return "/tmp/.cache/blang/objects";
}
bool BuildCache::clean()
{
string dir = getCacheDir();
/* Walk up to remove /blang/objects -> /blang -> only if empty */
if ( !removeRecursive( dir ) )
{
return false;
}
return true;
}