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
|
/*
** Copyright (C) 2023 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** This program is free software; you can redistribute it and/or modify it
** under the terms of the GNU General Public License as published by the
** Free Software Foundation; either version 3, or (at your option) any
** later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software Foundation,
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
*/
#ifndef MU_UTILS_FILE_HH__
#define MU_UTILS_FILE_HH__
#include <string>
#include <cinttypes>
#include <sys/stat.h>
#include <gio/gio.h>
#include <utils/mu-option.hh>
#include <utils/mu-result.hh>
#include <utils/mu-regex.hh>
namespace Mu {
/**
* Check if the directory has the given attributes
*
* @param path path to dir
* @param readable is it readable? false means "don't care"
* @param writeable is it writable? false means "don't care"
*
* @return true if is is a directory with given attributes; false otherwise.
*/
bool check_dir(const std::string& path, bool readable=false, bool writeable=false);
/**
* See g_canonicalize_filename
*
* @param filename
* @param relative_to
*
* @return
*/
std::string canonicalize_filename(const std::string& path, const std::string& relative_to="");
/**
* Expand the filesystem path (as per wordexp(3))
*
* @param str a filesystem path string
*
* @return the expanded string or some error
*/
Result<std::string> expand_path(const std::string& str);
/**
* Get the basename for path, i.e. without leading directory component,
* @see g_path_get_basename
*
* @param path
*
* @return the basename
*/
std::string basename(const std::string& path);
/**
* Get the dirname for path, i.e. without leading directory component,
* @see g_path_get_dirname
*
* @param path
*
* @return the dirname
*/
std::string dirname(const std::string& path);
/*
* for OSs without support for direntry->d_type, like Solaris
*/
#ifndef DT_UNKNOWN
enum {
DT_UNKNOWN = 0,
#define DT_UNKNOWN DT_UNKNOWN
DT_FIFO = 1,
#define DT_FIFO DT_FIFO
DT_CHR = 2,
#define DT_CHR DT_CHR
DT_DIR = 4,
#define DT_DIR DT_DIR
DT_BLK = 6,
#define DT_BLK DT_BLK
DT_REG = 8,
#define DT_REG DT_REG
DT_LNK = 10,
#define DT_LNK DT_LNK
DT_SOCK = 12,
#define DT_SOCK DT_SOCK
DT_WHT = 14
#define DT_WHT DT_WHT
};
#endif /*DT_UNKNOWN*/
/**
* get the d_type (as in direntry->d_type) for the file at path, using either
* stat(3) or lstat(3)
*
* @param path full path
* @param use_lstat whether to use lstat (otherwise use stat)
*
* @return DT_REG, DT_DIR, DT_LNK, or DT_UNKNOWN (other values are not supported
* currently)
*/
uint8_t determine_dtype(const std::string& path, bool use_lstat=false);
/**
* Well-known runtime paths
*
*/
enum struct RuntimePath {
XapianDb,
Cache,
LogFile,
Config,
Scripts,
Bookmarks
};
/**
* Get some well-known Path for internal use when don't have
* access to the command-line
*
* @param path the RuntimePath to find
* @param muhome path to muhome directory, or empty for the default.
*
* @return the path name
*/
std::string runtime_path(RuntimePath path, const std::string& muhome="");
/**
* Join path components into a path (with '/')
*
* @param s a string-convertible value
* @param args 0 or more string-convertible values
*
* @return the path
*/
static inline std::string join_paths() { return {}; }
template<typename S> std::string join_paths_(S&& s) { return std::string{s}; }
template<typename S, typename...Args>
std::string join_paths_(S&& s, Args...args) {
static std::string sepa{"/"};
auto&& str{std::string{std::forward<S>(s)}};
if (auto&& rest{join_paths_(std::forward<Args>(args)...)}; !rest.empty())
str += (sepa + rest);
return str;
}
template<typename S, typename...Args>
std::string join_paths(S&& s, Args...args) {
constexpr auto sepa = '/';
auto path = join_paths_(std::forward<S>(s), std::forward<Args>(args)...);
auto c{0U};
while (c < path.size()) {
if (path[c] != sepa) {
++c;
continue;
}
while (path[++c] == '/') {
path.erase(c, 1);
--c;
}
}
return path;
}
/**
* Read for standard input
*
* @return data from standard input or an error.
*/
Result<std::string> read_from_stdin();
/**
* Create a randomly-named temporary directory
*
* @return name of the temporary directory or an error.
*/
Result<std::string> make_temp_dir();
/**
* Remove a directory, recursively. Does not have to be empty.
*
* @param path path to directory
*
* @return Ok() or an error.
*/
Result<void> remove_directory(const std::string& path);
/**
* Run some system command.
*
* @param args a list of commmand line arguments (like argv)
* @param try_setsid whether to try setsid(2) (see its manpage for details) if this
* system supports it.
*
* @return Ok(exit code) or an error. Note that exit-code != 0 is _not_
* considered an error from the perspective of run_command, but is for
* run_command0
*/
struct CommandOutput {
int exit_code;
std::string standard_out;
std::string standard_err;
};
Result<CommandOutput> run_command(std::initializer_list<std::string> args,
bool try_setsid=false);
Result<CommandOutput> run_command0(std::initializer_list<std::string> args,
bool try_setsid=false);
/**
* Try to 'play' (ie., open with it's associated program) a file. On MacOS, the
* the program 'open' is used for this; on other platforms 'xdg-open' to do the
* actual opening. In addition you can set it to another program by setting thep
* MU_PLAY_PROGRAM environment variable
*
* This requires a 'native' file, see g_file_is_native()
*
* @param path full path of the file to open
*
* @return Ok() if succeeded, some error otherwise.
*/
Result<void> play(const std::string& path);
/**
* Find program in PATH
*
* @param name the name of the program
*
* @return either the full path to program, or Nothing if not found.
*/
Option<std::string> program_in_path(const std::string& name);
} // namespace Mu
#endif /* MU_UTILS_FILE_HH__ */
|