forked from apple/turicreate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemp_files.cpp
440 lines (389 loc) · 13.1 KB
/
temp_files.cpp
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/* Copyright © 2017 Apple Inc. All rights reserved.
*
* Use of this source code is governed by a BSD-3-clause license that can
* be found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
*/
#ifndef _WIN32
#include <pwd.h>
#else
#include <cross_platform/windows_wrapper.hpp>
#include <Lmcons.h>
#endif
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <sstream>
#include <set>
#include <string>
#include <boost/algorithm/string.hpp>
#include <boost/filesystem.hpp>
#include <parallel/mutex.hpp>
#include <logger/logger.hpp>
#include <logger/assertions.hpp>
#include <fileio/fileio_constants.hpp>
#include <fileio/fs_utils.hpp>
#include <util/syserr_reporting.hpp>
#include <process/process_util.hpp>
#include <network/net_util.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <export.hpp>
namespace turi {
// import boost filesystem
namespace fs = boost::filesystem;
namespace fs_impl {
struct tempfile_information {
/// Lock on all the structures below
mutex lock;
/// Lists all the tempfile creates
std::set<std::string> tempfile_history;
/// lists all the temporary directories created by this process
std::set<boost::filesystem::path> process_temp_directories;
/// Counts the number of temp files created
size_t temp_file_counter;
/// Used to generate unique file IDs
boost::uuids::random_generator uuid_generator;
};
/**
* Returns the process-wide singleton managing all the temp file information.
* The object inside is created as a pointer; all the data above is meant to
* leak! Basically we must carefully control the ordering at which this
* object is destroyed. (See \ref turi::global_teardown).
*/
EXPORT tempfile_information& get_temp_info() {
// this is a pointer
static tempfile_information* temp_info = new tempfile_information();
return *temp_info;
}
} // fs_impl
// forward declarations for some stuff the temp_file_deleter needs
static fs::path get_current_process_temp_directory(size_t idx);
using namespace fs_impl;
/**
* Returns all the temp directories available.
*/
std::vector<std::string> get_temp_directories() {
std::vector<std::string> paths;
if(fileio::get_cache_file_locations() == std::string("CHANGEME")) {
fileio::set_cache_file_locations(turi::fileio::get_system_temp_directory());
}
auto cache_file_locations = fileio::get_cache_file_locations();
boost::algorithm::split(paths,
cache_file_locations,
#ifndef _WIN32
boost::algorithm::is_any_of(":"));
#else
boost::algorithm::is_any_of(";"));
#endif
return paths;
}
/**
* Get the current system user name
*/
std::string get_system_user_name() {
#ifndef _WIN32
struct passwd* p = getpwuid(getuid());
if (p != NULL) return p->pw_name;
else return "";
#else
DWORD username_len = UNLEN+1;
char username[UNLEN+1];
if(GetUserName(username, &username_len)) {
return username;
} else {
auto err_code = GetLastError();
logstream(LOG_INFO) << "Could not get username: " <<
get_last_err_str(err_code) << std::endl;
return "";
}
#endif
}
/**
* Return turicreate-[user_name]
*/
static std::string get_turicreate_temp_directory_prefix() {
std::string turicreate_name = "turicreate";
std::string user_name = get_system_user_name();
if (!user_name.empty()) {
turicreate_name = turicreate_name + "-" + user_name;
}
return turicreate_name;
}
#ifdef TC_ENABLE_REMOTEFS
static fs::path get_current_process_hdfs_temp_directory() {
fs::path path;
if (fileio::get_cache_file_hdfs_location() != "") {
path = fs::path(fileio::get_cache_file_hdfs_location());
std::string turicreate_name = get_turicreate_temp_directory_prefix();
path /= turicreate_name;
}
return path;
}
#endif
/**
* Returns the number of temp directories available.
*/
size_t num_temp_directories() {
return get_temp_directories().size();
}
/**
* Gets the 'idx''th Turi temp directory.
*
* The temp directories are turicreate-[username] appended to the temp directory.
*
* idx can be any value in which case the indices will loop around.
*
* Ex: Say there are 2 temp directories. /tmp and /var/tmp.
*
* get_turicreate_temp_directory(0) will return "/tmp/turicreate-[username]"
* get_turicreate_temp_directory(1) will return "/var/tmp/turicreate-[username]"
* and get_turicreate_temp_directory(2) will return "/tmp/turicreate-[username]"
*/
static fs::path get_turicreate_temp_directory(size_t idx) {
auto temp_dirs = get_temp_directories();
ASSERT_GT(temp_dirs.size(), 0);
fs::path temp_path = temp_dirs[idx % temp_dirs.size()];
std::string turicreate_name = get_turicreate_temp_directory_prefix();
fs::path path(temp_path / turicreate_name);
return path;
}
/**
* Finds the temp directory for the current process.
*
* The temp directories are turicreate-[username]/procname appended to the
* temp directory.
*
* idx can be any value in which case the indices will loop around.
*
* Ex: Say there are 2 temp directories. /tmp and /var/tmp.
*
* get_current_process_temp_directory(0) will return "/tmp/turicreate-[username]/[pid]"
* get_current_process_temp_directory(1) will return "/var/tmp/turicreate-[username]/[pid]"
* and get_current_process_temp_directory(2) will return "/tmp/turicreate-[username]/[pid]"
*/
static fs::path get_current_process_temp_directory(size_t idx) {
return get_turicreate_temp_directory(idx) / std::to_string(getpid());
}
/**
* Creates the current process's temp directory if it does not already exist.
*
* idx can be any value in which case the indices will loop around.
*/
static void create_current_process_temp_directory(std::string path) {
bool success = true;
try {
if (fileio::get_file_status(path) != fileio::file_status::DIRECTORY) {
success = fileio::create_directory(path);
if(success)
get_temp_info().process_temp_directories.insert(path);
}
} catch (...) {
success = false;
}
if(! success) {
std::stringstream error_message;
error_message << "Unable to a create temporary directory at \""
<< path << "\". This location can be changed by calling:\n"
<< "turicreate.config.set_runtime_config('TURI_CACHE_FILE_LOCATIONS', <writable path>)\n"
<< std::endl;
log_and_throw(error_message.str());
}
}
static void delete_proc_directory(fs::path path) {
// we could use remove_all but that causes problems, I suspect
// when multiple processes are trying to reap simultaneously.
std::vector<fs::path> files_to_delete;
auto diriter = fs::recursive_directory_iterator(path,
fs::symlink_option::no_recurse);
auto enditer = fs::recursive_directory_iterator();
while(diriter != enditer) {
if (fs::is_regular(diriter->path()) ||
fs::is_directory(diriter->path()) ||
fs::status(diriter->path()).type() == fs::socket_file) {
files_to_delete.push_back(diriter->path());
}
++diriter;
}
// now delete all the files
for (auto& p: files_to_delete) {
// remove if possible. Ignore exceptions
try {
fs::remove(p);
logstream(LOG_DEBUG) << "Deleting " << p << std::endl;
} catch (...) {
logstream(LOG_WARNING) << "Unable to delete " << p << std::endl;
}
}
// delete the root
// remove if possible. Ignore exceptions
try {
fs::remove(path);
logstream(LOG_DEBUG) << "Deleting " << path << std::endl;
} catch (...) { }
}
/**
* we will store the temporary files in [tmp_directory]/turicreate/[procid]
* This searches in turicreate's temp directory for unused temporary files
* (what procids no longer exist) and deletes them.
*/
EXPORT void reap_unused_temp_files() {
// loop through all the subdirectories in get_turicreate_temp_directory()
// and unlink if the pid does not exist
size_t temp_dir_size = num_temp_directories();
for (size_t idx = 0; idx < temp_dir_size; ++idx) {
try {
fs::path temp_dir(get_turicreate_temp_directory(idx));
auto diriter = fs::directory_iterator(temp_dir);
auto enditer = fs::directory_iterator();
while(diriter != enditer) {
auto path = diriter->path();
if (fs::is_directory(path)) {
try {
long pid = std::stol(path.filename().string());
if(!is_process_running(pid)) {
// PID no longer exists.
// delete it
logstream(LOG_EMPH) << "Deleting orphaned temp directory found in "
<< path.string() << std::endl;
delete_proc_directory(path);
}
} catch (...) {
// empty catch. if the path does not parse as an
// integer, ignore it.
logstream(LOG_WARNING)
<< "Unexpcted file in Turi's temp directory: " << path
<< std::endl;
}
}
++diriter;
}
} catch (...) {
// Failures are ok. we just stop.
}
}
}
EXPORT std::string get_temp_name(const std::string& prefix, bool _prefer_hdfs) {
std::lock_guard<mutex> lg(get_temp_info().lock);
// Local system temp dir
fs::path path(get_current_process_temp_directory(get_temp_info().temp_file_counter++));
#ifdef TC_ENABLE_REMOTEFS
// hdfs temp dir
fs::path hdfs_path(get_current_process_hdfs_temp_directory());
if (_prefer_hdfs && !hdfs_path.empty()) {
path = hdfs_path;
}
#endif
// create the directories if they do not exist
create_current_process_temp_directory(path.string());
if (prefix.empty()) {
std::stringstream strm;
strm << boost::lexical_cast<std::string>(get_temp_info().uuid_generator());
path /= strm.str();
} else {
path /= prefix;
}
std::string ret = path.generic_string();
// write the tempfile into the history
get_temp_info().tempfile_history.insert(ret);
return ret;
};
std::string get_temp_name_prefer_hdfs(const std::string& prefix) {
bool prefer_hdfs = true;
return get_temp_name(prefix, prefer_hdfs);
}
/**
* Deletes the file with the name s
*/
bool delete_temp_file(std::string s) {
std::lock_guard<mutex> lg(get_temp_info().lock);
// search in the history of all tempfiles generated
bool found = false;
// I need to check lower_bound and lower_bound - 1
auto iter = get_temp_info().tempfile_history.lower_bound(s);
auto begin = get_temp_info().tempfile_history.begin();
auto end = get_temp_info().tempfile_history.end();
// check lower_bound see if it is a prefix
if (iter != end &&
boost::starts_with(s, *iter)) {
found = true;
get_temp_info().tempfile_history.erase(iter);
}
// check lower_bound - 1 see if it is a prefix
else if (iter != begin) {
iter--;
if (boost::starts_with(s, *iter)) {
found = true;
get_temp_info().tempfile_history.erase(iter);
}
}
if (found) {
logstream(LOG_DEBUG) << "Deleting " << s << "\n";
return fileio::delete_path(s);
} else {
return false;
}
}
void delete_temp_files(std::vector<std::string> files) {
std::lock_guard<mutex> lg(get_temp_info().lock);
// search in the history of all tempfiles generated
// and unlink all matching files
std::set<std::string> found_prefixes;
for(std::string file: files) {
bool found = false;
// I need to check lower_bound and lower_bound - 1
auto iter = get_temp_info().tempfile_history.lower_bound(file);
auto begin = get_temp_info().tempfile_history.begin();
auto end = get_temp_info().tempfile_history.end();
// check lower_bound see if it is a prefix
if (iter != end && boost::starts_with(file, *iter)) {
found = true;
found_prefixes.insert(*iter);
}
// check lower_bound - 1 see if it is a prefix
if (iter != begin) {
iter--;
if (boost::starts_with(file, *iter)) {
found = true;
found_prefixes.insert(*iter);
}
}
if (found) {
logstream(LOG_DEBUG) << "Deleting " << file << "\n";
fileio::delete_path(file);
}
}
// now to clear the found prefixes
for(std::string prefix: found_prefixes) {
get_temp_info().tempfile_history.erase(prefix);
}
}
void reap_current_process_temp_files() {
// remove all if possible. Ignore exceptions
// We go straight to delete_path_impl here to avoid the reference counting
// mechanism on temp filesr At this point we are just doing cleanup.
for (auto fname: get_temp_info().tempfile_history) {
turi::fileio::delete_path_impl(fname);
}
for (auto current_temp_dir: get_temp_info().process_temp_directories) {
auto string_dir = current_temp_dir.string();
try {
if (fileio::get_protocol(string_dir) == "hdfs") {
// we only reap hdfs if the directory is empty
// since other people could be sharing this
logstream(LOG_DEBUG) << "Non-recursive deletion of "
<< current_temp_dir << std::endl;
turi::fileio::delete_path_impl(string_dir);
} else {
logstream(LOG_DEBUG) << "Recursive deletion of "
<< current_temp_dir << std::endl;
turi::fileio::delete_path_recursive(string_dir);
}
} catch (...) { }
}
}
} // namespace turi