forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextractor.cpp
More file actions
343 lines (284 loc) · 11.6 KB
/
extractor.cpp
File metadata and controls
343 lines (284 loc) · 11.6 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#include "extractor.h"
#include "error_codes.h"
#include "dir_utils.h"
#include "pal.h"
#include "utils.h"
#include <cinttypes>
#ifdef __sun
#include <alloca.h>
#endif
#if defined(NATIVE_LIBS_EMBEDDED)
extern "C"
{
#include "pal_zlib.h"
}
#endif
// Suppress prefast warning #6255: alloca indicates failure by raising a stack overflow exception
#pragma warning(disable:6255)
using namespace bundle;
pal::string_t& extractor_t::extraction_dir()
{
if (m_extraction_dir.empty())
{
// Compute the final extraction location as:
// m_extraction_dir = $DOTNET_BUNDLE_EXTRACT_BASE_DIR/<app>/<id>/...
//
// If DOTNET_BUNDLE_EXTRACT_BASE_DIR is not set in the environment,
// a default is chosen within the temporary directory.
if (!pal::getenv(_X("DOTNET_BUNDLE_EXTRACT_BASE_DIR"), &m_extraction_dir))
{
if (!pal::get_default_bundle_extraction_base_dir(m_extraction_dir))
{
trace::error(_X("Failure processing application bundle."));
trace::error(_X("Failed to determine location for extracting embedded files."));
trace::error(_X("DOTNET_BUNDLE_EXTRACT_BASE_DIR is not set, and a read-write cache directory couldn't be created."));
throw StatusCode::BundleExtractionFailure;
}
}
pal::string_t host_name = strip_executable_ext(get_filename(m_bundle_path));
if (!pal::is_path_rooted(m_extraction_dir))
{
pal::string_t relative_path(m_extraction_dir);
if (!pal::getcwd(&m_extraction_dir))
{
trace::error(_X("Failure processing application bundle."));
trace::error(_X("Failed to obtain current working dir."));
assert(m_extraction_dir.empty());
throw StatusCode::BundleExtractionFailure;
}
append_path(&m_extraction_dir, relative_path.c_str());
}
append_path(&m_extraction_dir, host_name.c_str());
append_path(&m_extraction_dir, m_bundle_id.c_str());
trace::info(_X("Files embedded within the bundle will be extracted to [%s] directory."), m_extraction_dir.c_str());
}
return m_extraction_dir;
}
pal::string_t& extractor_t::working_extraction_dir()
{
if (m_working_extraction_dir.empty())
{
// Compute the working extraction location for this process,
// before the extracted files are committed to the final location
// working_extraction_dir = $DOTNET_BUNDLE_EXTRACT_BASE_DIR/<app>/<proc-id-hex>
m_working_extraction_dir = get_directory(extraction_dir());
pal::char_t pid[32];
pal::snwprintf(pid, 32, _X("%x"), pal::get_pid());
append_path(&m_working_extraction_dir, pid);
trace::info(_X("Temporary directory used to extract bundled files is [%s]."), m_working_extraction_dir.c_str());
}
return m_working_extraction_dir;
}
// Create a file to be extracted out on disk, including any intermediate sub-directories.
FILE* extractor_t::create_extraction_file(const pal::string_t& relative_path)
{
pal::string_t file_path = working_extraction_dir();
append_path(&file_path, relative_path.c_str());
// working_extraction_dir is assumed to exist,
// so we only create sub-directories if relative_path contains directories
if (dir_utils_t::has_dirs_in_path(relative_path))
{
dir_utils_t::create_directory_tree(get_directory(file_path));
}
FILE* file = pal::file_open(file_path.c_str(), _X("wb"));
if (file == nullptr)
{
trace::error(_X("Failure processing application bundle."));
trace::error(_X("Failed to open file [%s] for writing."), file_path.c_str());
throw StatusCode::BundleExtractionIOError;
}
return file;
}
// Extract one file from the bundle to disk.
void extractor_t::extract(const file_entry_t &entry, reader_t &reader)
{
FILE* file = create_extraction_file(entry.relative_path());
reader.set_offset(entry.offset());
int64_t size = entry.size();
size_t cast_size = to_size_t_dbgchecked(size);
size_t extracted_size = 0;
if (entry.compressedSize() != 0)
{
#if defined(NATIVE_LIBS_EMBEDDED)
PAL_ZStream zStream;
zStream.nextIn = (uint8_t*)(const void*)reader;
zStream.availIn = static_cast<uint32_t>(entry.compressedSize());
const int Deflate_DefaultWindowBits = -15; // Legal values are 8..15 and -8..-15. 15 is the window size,
// negative val causes deflate to produce raw deflate data (no zlib header).
int ret = CompressionNative_InflateInit2_(&zStream, Deflate_DefaultWindowBits);
if (ret != PAL_Z_OK)
{
trace::error(_X("Failure initializing zLib stream."));
throw StatusCode::BundleExtractionIOError;
}
const int bufSize = 4096;
uint8_t* buf = (uint8_t*)alloca(bufSize);
do
{
zStream.nextOut = buf;
zStream.availOut = bufSize;
ret = CompressionNative_Inflate(&zStream, PAL_Z_NOFLUSH);
if (ret < 0)
{
CompressionNative_InflateEnd(&zStream);
trace::error(_X("Failure inflating zLib stream. %s"), zStream.msg);
throw StatusCode::BundleExtractionIOError;
}
int produced = bufSize - zStream.availOut;
if (fwrite(buf, 1, produced, file) != (size_t)produced)
{
CompressionNative_InflateEnd(&zStream);
trace::error(_X("I/O failure when writing decompressed file."));
throw StatusCode::BundleExtractionIOError;
}
extracted_size += produced;
} while (zStream.availOut == 0);
CompressionNative_InflateEnd(&zStream);
#else
trace::error(_X("Failure extracting contents of the application bundle. Compressed files used with a standalone (not singlefile) apphost."));
throw StatusCode::BundleExtractionIOError;
#endif
}
else
{
extracted_size = fwrite(reader, 1, cast_size, file);
}
if (extracted_size != cast_size)
{
trace::error(_X("Failure extracting contents of the application bundle. Expected size:%" PRId64 " Actual size:%zu"), size, extracted_size);
trace::error(_X("I/O failure when writing extracted files."));
throw StatusCode::BundleExtractionIOError;
}
fclose(file);
}
void extractor_t::begin()
{
// Files are extracted to a specific deterministic location on disk
// on first run, and are available for reuse by subsequent similar runs.
//
// The extraction should be fault tolerant with respect to:
// * Failures/crashes during extraction which result in partial-extraction
// * Race between two or more processes concurrently attempting extraction
//
// In order to solve these issues, we implement a extraction as a two-phase approach:
// 1) Files embedded in a bundle are extracted to a process-specific temporary
// extraction location (working_extraction_dir)
// 2) Upon successful extraction, working_extraction_dir is renamed to the actual
// extraction location (extraction_dir)
//
// This effectively creates a file-lock to protect against races and failed extractions.
dir_utils_t::create_directory_tree(working_extraction_dir());
}
void extractor_t::clean()
{
dir_utils_t::remove_directory_tree(working_extraction_dir());
}
void extractor_t::commit_dir()
{
// Commit an entire new extraction to the final extraction directory
// Retry the move operation with some wait in between the attempts. This is to workaround for possible file locking
// caused by AV software. Basically the extraction process above writes a bunch of executable files to disk
// and some AV software may decide to scan them on write. If this happens the files will be locked which blocks
// our ability to move them.
bool extracted_by_concurrent_process = false;
bool extracted_by_current_process =
dir_utils_t::rename_with_retries(working_extraction_dir(), extraction_dir(), extracted_by_concurrent_process);
if (extracted_by_concurrent_process)
{
// Another process successfully extracted the dependencies
trace::info(_X("Extraction completed by another process, aborting current extraction."));
clean();
}
if (!extracted_by_current_process && !extracted_by_concurrent_process)
{
trace::error(_X("Failure processing application bundle."));
trace::error(_X("Failed to commit extracted files to directory [%s]."), extraction_dir().c_str());
throw StatusCode::BundleExtractionFailure;
}
trace::info(_X("Completed new extraction."));
}
void extractor_t::commit_file(const pal::string_t& relative_path)
{
// Commit individual files to the final extraction directory.
pal::string_t working_file_path = working_extraction_dir();
append_path(&working_file_path, relative_path.c_str());
pal::string_t final_file_path = extraction_dir();
append_path(&final_file_path, relative_path.c_str());
if (dir_utils_t::has_dirs_in_path(relative_path))
{
dir_utils_t::create_directory_tree(get_directory(final_file_path));
}
bool extracted_by_concurrent_process = false;
bool extracted_by_current_process =
dir_utils_t::rename_with_retries(working_file_path, final_file_path, extracted_by_concurrent_process);
if (extracted_by_concurrent_process)
{
// Another process successfully extracted the dependencies
trace::info(_X("Extraction completed by another process, aborting current extraction."));
}
if (!extracted_by_current_process && !extracted_by_concurrent_process)
{
trace::error(_X("Failure processing application bundle."));
trace::error(_X("Failed to commit extracted files to directory [%s]."), extraction_dir().c_str());
throw StatusCode::BundleExtractionFailure;
}
trace::info(_X("Extraction recovered [%s]"), relative_path.c_str());
}
void extractor_t::extract_new(reader_t& reader)
{
begin();
for (const file_entry_t& entry : m_manifest.files)
{
if (entry.needs_extraction())
{
extract(entry, reader);
}
}
commit_dir();
}
// Verify an existing extraction contains all files listed in the bundle manifest.
// If some files are missing, extract them individually.
void extractor_t::verify_recover_extraction(reader_t& reader)
{
pal::string_t& ext_dir = extraction_dir();
bool recovered = false;
for (const file_entry_t& entry : m_manifest.files)
{
if (!entry.needs_extraction())
{
continue;
}
pal::string_t file_path = ext_dir;
append_path(&file_path, entry.relative_path().c_str());
if (!pal::file_exists(file_path))
{
if (!recovered)
{
recovered = true;
begin();
}
extract(entry, reader);
commit_file(entry.relative_path());
}
}
if (recovered)
{
clean();
}
}
pal::string_t& extractor_t::extract(reader_t& reader)
{
if (pal::directory_exists(extraction_dir()))
{
trace::info(_X("Reusing existing extraction of application bundle."));
verify_recover_extraction(reader);
}
else
{
trace::info(_X("Starting new extraction of application bundle."));
extract_new(reader);
}
return m_extraction_dir;
}