Turi Create  4.0
temp_files.hpp
1 /* Copyright © 2017 Apple Inc. All rights reserved.
2  *
3  * Use of this source code is governed by a BSD-3-clause license that can
4  * be found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
5  */
6 #ifndef FILEIO_TEMP_FILE_HPP
7 #define FILEIO_TEMP_FILE_HPP
8 #include <string>
9 #include <vector>
10 
11 namespace turi {
12 
13 /**
14  * \ingroup fileio
15  * Get the current system user name.
16  */
17 std::string get_system_user_name();
18 
19 /**
20  * \ingroup fileio
21  * Returns a file name which can be used for a temp file.
22  * Returns an empty string on failure, a temporary file name on success.
23  * The file name returned is allowed to be a "prefix". i.e. arbitrary extensions
24  * can be attached to be tail of the file. For instance, if get_temp_name()
25  * returns /tmp/file51apTO, you can use /tmp/file51apTO.csv
26  *
27  * \param prefix Optional. If specified, this exact prefix will be returned in
28  * the temporary path. ex: /var/tmp/turicreate-user/12345/[prefix]. If an empty
29  * string or not specified, a random unique prefix will be generated.
30  *
31  * \param _prefer_hdfs Optional, defaults to false. If true, prefers to use
32  * HDFS if available.
33  *
34  * Note that if you specify your own prefix it is up to you to manage collisions,
35  * i.e. multiple parts of the program using the same prefix for instance.
36  */
37 std::string get_temp_name(const std::string& prefix="", bool _prefer_hdfs=false);
38 
39 /**
40  * \ingroup fileio
41  * Same as get_temp_name but return the temp file
42  * on hdfs if avaiable. The hdfs temp file location is a runtime configurable
43  * variable TURI_CACHE_FILE_HDFS_LOCATION defined in fileio_constant.hpp.
44  *
45  * \param prefix Optional. If specified, this exact prefix will be returned in
46  * the temporary path. ex: /var/tmp/turicreate-user/12345/[prefix]. If an empty
47  * string or not specified, a random unique prefix will be generated.
48  *
49  * Note that if you specify your own prefix it is up to you to manage collisions,
50  * i.e. multiple parts of the program using the same prefix for instance.
51  */
52 std::string get_temp_name_prefer_hdfs(const std::string& prefix="");
53 
54 /**
55  * \ingroup fileio
56  * Deletes the temporary file with the name s.
57  * Returns true on success, false on failure (file does not exist,
58  * or cannot be deleted). The file will only be deleted
59  * if a prefix of s was previously returned by get_temp_name(). This is done
60  * for safety to prevent this function from being used to delete arbitrary files.
61  *
62  * For instance, if get_temp_name() previously returned /tmp/file51apTO ,
63  * delete_temp_file will succeed on /tmp/file51apTO.csv . delete_temp_file will
64  * fail on stuff like /usr/bin/bash
65  */
66 bool delete_temp_file(std::string s);
67 
68 /**
69  * \ingroup fileio
70  * Deletes a collection of temporary files.
71  * The files will only be deleted if a prefix of s was previously returned by
72  * get_temp_name(). This is done for safety to prevent this function from being
73  * used to delete arbitrary files.
74  *
75  * For instance, if get_temp_name() previously returned /tmp/file51apTO ,
76  * delete_temp_files will succeed on a collection of files
77  * {/tmp/file51apTO.csv, /tmp/file51apTO.txt}. delete_temp_file will
78  * fail on stuff like /usr/bin/bash
79  */
80 void delete_temp_files(std::vector<std::string> files);
81 
82 
83 /**
84  * \ingroup fileio
85  * Deletes all temporary directories in the temporary turicreate/ directory
86  * (/var/tmp/turicreate) which are no longer used. i.e. was created by a process
87  * which no longer exists.
88  */
90 
91 /**
92  * \ingroup fileio
93  * Deletes all temp files created by the current process
94  */
96 
97 /**
98  * \ingroup fileio
99  * Returns the set of temp directories
100  */
101 std::vector<std::string> get_temp_directories();
102 
103 /**
104  * \ingroup fileio
105  * Returns the number of temp directories
106  */
107 size_t num_temp_directories();
108 } // namespace turi
109 #endif
std::string get_temp_name(const std::string &prefix="", bool _prefer_hdfs=false)
bool delete_temp_file(std::string s)
size_t num_temp_directories()
std::vector< std::string > get_temp_directories()
std::string get_system_user_name()
std::string get_temp_name_prefer_hdfs(const std::string &prefix="")
void reap_unused_temp_files()
void delete_temp_files(std::vector< std::string > files)
void reap_current_process_temp_files()