1 /**
2  * @file tmpdir.h
3  *
4  * @brief Temporary directory helpers.
5  * @author David Suárez
6  * @date Sun, 21 Oct 2018 18:41:11 +0200
7  *
8  * Copyright (c) 2018-2019 David Suárez.
9  * Email: david.sephirot@gmail.com
10  *
11  */
12 
13 #ifndef __TMPDIR_H__
14 #define __TMPDIR_H__
15 
16 #ifdef HAVE_CONFIG_H
17     #include <config.h>
18 #endif
19 
20 #include <stddef.h>
21 
22 /**
23  * @brief Max filename length for a temp file
24  */
25 #define TMPNAMELEN 64
26 
27 /**
28  * @brief Type of temporary directory.
29  */
30 typedef enum {
31     /** The directory is owned by us, we can delete all without any risk */
32     TMPDIR_APP_OWNED = 0,
33 
34     /** The directory belongs to the user, we can't simply remove all the content and go */
35     TMPDIR_USER_OWNED = 1
36 } tmpdir_type_t;
37 
38 /**
39  * @brief Get the system tmp dir path.
40  *
41  * @return tmp dir path
42  */
43 const char* get_sys_tmpdir(void);
44 
45 /**
46  * @brief Generates a random temporal filename.
47  *
48  * @param extension the new filename extension
49  * @return filename
50  */
51 const char* generate_new_tmp_filename(const char* extension);
52 
53 /**
54  * @brief Configure the tmp dir options
55  *
56  * @param dir tmp dir path
57  * @param type who owns the tmpdir: the user or us
58  * @param max_files maximum number of files
59  * @param preserve_files preserve files on exit
60  */
61 void set_tmpdir(const char *dir, tmpdir_type_t type, int max_files, int preserve_files);
62 
63 /**
64  * @brief Get the configured tmp dir path.
65  *
66  * @return tmp dir path
67  */
68 const char* get_tmpdir(void);
69 
70 /**
71  * @brief Cleans the tmpdir taking in care if we should remove any files.
72  */
73 void clean_tmpdir(void);
74 
75 /**
76  * @brief Makes a new temporary directory in system tmp dir.
77  *
78  * @return the new tmpdir path
79  */
80 const char* make_tmpdir(void);
81 
82 /**
83  * @brief Check if we can write on dir.
84  *
85  * @param tmpdir path directory to check for
86  * @return TRUE if we can write, FALSE on any error
87  */
88 int check_dir_is_rw(const char* tmpdir);
89 
90 /**
91  * @brief Check if the configured maximum number of files is reached
92  *
93  * @return FALSE if not exceed, TRUE if yes
94  */
95 int tmpfiles_limit_reached(void);
96 
97 /**
98  * @brief Writes a file to the temporary directory.
99  *
100  * @param filename filename of the file to create
101  * @param file_data data to write
102  * @param data_len size of data
103  */
104 void tmpfile_write_file(const char* filename, const unsigned char *file_data, const size_t data_len);
105 
106 /**
107  * @brief Deletes a file from the temp dir.
108  *
109  * @param filename filename to delete
110  */
111 void tmpfile_delete_file(const char* filename);
112 
113 /**
114  * @brief Links a file to the temporary directory (the link name is derived from the source filename).
115  *
116  * @param src_file_path file path of the source file to link
117  * @return TRUE on success; FALSE on error
118  */
119 int tmpfile_link_file(const char* src_file_path);
120 
121 /**
122  * @brief Links a file to the temporary directory.
123  *
124  * @param src_file_path file path of the source file to link
125  * @param dest_link_name destination link name
126  * @return TRUE on success; FALSE on error
127  */
128 int tmpfile_link_file_to_dest(const char* src_file_path, const char *dest_link_name);
129 
130 /**
131  * @brief Unlinks a soft link from the temp dir.
132  *
133  * @param dest_link_name link filename to delete
134  */
135 void tmpfile_unlink_file(const char *dest_link_name);
136 
137 #endif /* __TMPDIR_H__ */
138