Line data Source code
1 : /*
2 : * Copyright (C) 2011 Andrea Mazzoleni
3 : *
4 : * This program is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 3 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * This program is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #ifndef __SUPPORT_H
19 : #define __SUPPORT_H
20 :
21 : /****************************************************************************/
22 : /* lock */
23 :
24 : /**
25 : * Initialize and destroy the locks.
26 : */
27 : void lock_init(void);
28 : void lock_done(void);
29 :
30 : /**
31 : * Lock used for printf.
32 : *
33 : * In Windows printf() is not atomic, and multiple threads
34 : * will have output interleaved.
35 : *
36 : * Note that even defining __USE_MINGW_ANSI_STDIO the problem persists.
37 : *
38 : * See for example:
39 : *
40 : * Weird output when I use pthread and printf.
41 : * http://stackoverflow.com/questions/13190254/weird-output-when-i-use-pthread-and-printf
42 : *
43 : * This is also required in other OS because we split output in stdlog in
44 : * two fprintf calls.
45 : */
46 : void lock_msg(void);
47 : void unlock_msg(void);
48 :
49 : /**
50 : * Lock used for memory counter.
51 : */
52 : void lock_memory(void);
53 : void unlock_memory(void);
54 :
55 : /****************************************************************************/
56 : /* random */
57 :
58 : /**
59 : * Reseed the random pseudo random number generator
60 : */
61 : void random_reseed(void);
62 :
63 : /**
64 : * Return a pseudo random number of 8 bits
65 : */
66 : unsigned char random_u8(void);
67 :
68 : /**
69 : * Return a pseudo random number of 64 bits
70 : */
71 : uint64_t random_u64(void);
72 :
73 : /****************************************************************************/
74 : /* error */
75 :
76 : /**
77 : * Return if the error is at hardware level
78 : */
79 : int is_hw(int err);
80 :
81 : /**
82 : * Log errors with standard messages
83 : */
84 : void log_fatal_errno(int err, const char* name);
85 : void log_error_errno(int err, const char* name);
86 :
87 : /****************************************************************************/
88 : /* log */
89 :
90 : /**
91 : * Pointer to log function.
92 : */
93 : typedef void log_ptr(int err, const char* format, ...) __attribute__((format(attribute_printf, 2, 3)));
94 :
95 : /**
96 : * Fatal error messages.
97 : *
98 : * Messages printed before an early termination.
99 : *
100 : * These messages go in the log file and in stderr unconditionally.
101 : */
102 : void log_fatal(int err, const char* format, ...) __attribute__((format(attribute_printf, 2, 3)));
103 :
104 : /**
105 : * Unexpected error messages.
106 : *
107 : * Messages reporting error conditions that don't prevent the program to run.
108 : *
109 : * Some of them could be also serious errors, like "silent errors".
110 : * In such case, the summary result is always printed as error,
111 : * and we are sure to notify the user in some way.
112 : *
113 : * These messages go in the log file if specified, otherwise they go in stderr.
114 : */
115 : void log_error(int err, const char* format, ...) __attribute__((format(attribute_printf, 2, 3)));
116 :
117 : /**
118 : * Expected error messages, without fallback to stderr.
119 : *
120 : * These errors are "someway" expected, and then they never go to screen.
121 : * For example, when undeleting missing files, the messages for missing files
122 : * are not shown.
123 : *
124 : * These messages go in the log file if specified, otherwise they are lost.
125 : */
126 : void log_expected(int err, const char* format, ...) __attribute__((format(attribute_printf, 2, 3)));
127 :
128 : /**
129 : * Tag messages.
130 : *
131 : * Messages are in tag format, like "tag:entry:...".
132 : *
133 : * These messages never go on the screen, but only in the log file if specified.
134 : *
135 : * Note that this function, allows not \n terminated strings.
136 : *
137 : * These messages are buffered. Use msg_flush() to flush them.
138 : */
139 : void log_tag(const char* format, ...) __attribute__((format(attribute_printf, 1, 2)));
140 :
141 : /**
142 : * Flush the log.
143 : */
144 : void log_flush(void);
145 :
146 : /****************************************************************************/
147 : /* message */
148 :
149 : /**
150 : * Pointer to msg function.
151 : */
152 : typedef void msg_ptr(const char* format, ...) __attribute__((format(attribute_printf, 1, 2)));
153 :
154 : /**
155 : * Message levels.
156 : *
157 : * The levels control the amount of information printed on the screen.
158 : * Note that log_fatal(), log_error(), log_expected() and log_tag() are not affected by this option.
159 : *
160 : * From the most quiet to the most verbose.
161 : */
162 : #define MSG_STATUS -3
163 : #define MSG_INFO -2
164 : #define MSG_PROGRESS -1
165 : #define MSG_BAR 0
166 : #define MSG_VERBOSE 1
167 :
168 : /**
169 : * Selected message level.
170 : */
171 : extern int msg_level;
172 :
173 : /**
174 : * State messages.
175 : *
176 : * Messages that tell what the program is doing or did, but limited to few lines.
177 : * They are status information, and summary results.
178 : */
179 : void msg_status(const char* format, ...) __attribute__((format(attribute_printf, 1, 2)));
180 :
181 : /**
182 : * Info messages.
183 : *
184 : * Messages that tell what was done.
185 : * Potentially a lot of messages are possible. They can still be on the screen,
186 : * as losing them we don't lose information.
187 : *
188 : * These messages never go in the log file, because there is always a corresponding log_tag().
189 : */
190 : void msg_info(const char* format, ...) __attribute__((format(attribute_printf, 1, 2)));
191 :
192 : /**
193 : * Progress messages.
194 : *
195 : * Message that tell the progress of program.
196 : *
197 : * These messages also go in the log file.
198 : */
199 : void msg_progress(const char* format, ...) __attribute__((format(attribute_printf, 1, 2)));
200 :
201 : /**
202 : * Progress bar messages.
203 : *
204 : * Message that show the percentage of the progress.
205 : *
206 : * These messages never go in the log file.
207 : *
208 : * These messages are buffered. Use msg_flush() to flush them.
209 : */
210 : void msg_bar(const char* format, ...) __attribute__((format(attribute_printf, 1, 2)));
211 :
212 : /**
213 : * Verbose messages.
214 : *
215 : * Message that tell what is already expected.
216 : *
217 : * These messages also go in the log file.
218 : */
219 : void msg_verbose(const char* format, ...) __attribute__((format(attribute_printf, 1, 2)));
220 :
221 : /**
222 : * Flush the output.
223 : */
224 : void msg_flush(void);
225 :
226 : /****************************************************************************/
227 : /* print */
228 :
229 : /**
230 : * Print a repeated char.
231 : */
232 : void printc(char c, size_t pad);
233 :
234 : /**
235 : * Print a string with right space padding.
236 : */
237 : void printr(const char* str, size_t pad);
238 :
239 : /**
240 : * Print a string with left space padding.
241 : */
242 : void printl(const char* str, size_t pad);
243 :
244 : /**
245 : * Print a probability with space padding.
246 : */
247 : void printp(double v, size_t pad);
248 :
249 : /****************************************************************************/
250 : /* string */
251 :
252 : #define ESC_MAX (PATH_MAX * 2 + 1)
253 :
254 : /**
255 : * Escape a string for the log.
256 : *
257 : * \param buffer Preallocated buffer of ESC_MAX size.
258 : *
259 : * Chars ':', '\n', '\r' and '\' are escaped to '\d', '\\n', '\\r' and '\\'.
260 : */
261 : const char* esc_tag(const char* str, char* buffer);
262 :
263 : /**
264 : * Escape a string for the shell.
265 : *
266 : * \param buffer Preallocated buffer of ESC_MAX size.
267 : */
268 : const char* esc_shell_multi(const char** str_map, unsigned str_max, char* buffer);
269 342153 : static inline const char* esc_shell(const char* str, char* buffer)
270 : {
271 342153 : return esc_shell_multi(&str, 1, buffer);
272 : }
273 :
274 : /**
275 : * Polish a string.
276 : *
277 : * Not printable chars are replaced by spaces.
278 : *
279 : * Note that the passed string is modified.
280 : */
281 : char* strpolish(char* s);
282 :
283 : /**
284 : * Split a string in multiple tokens separated by delimiters.
285 : *
286 : * Multiple delimiters are grouped together.
287 : */
288 : unsigned strsplit(char** split_map, unsigned split_max, char* line, const char* delimiters);
289 :
290 : /**
291 : * Trim spaces from the start and the end
292 : */
293 : char* strtrim(char* s);
294 :
295 : /**
296 : * Lower case
297 : */
298 : char* strlwr(char* s);
299 :
300 : /*
301 : * Find the first occurrence of 'needle' in 'haystack' only if it
302 : * appears as a separate word (space, number or string boundaries).
303 : *
304 : * Returns a pointer to the beginning of the match, or NULL if not found.
305 : */
306 : char* worddigitstr(const char* haystack, const char* needle);
307 :
308 : /****************************************************************************/
309 : /* path */
310 :
311 : /**
312 : * Copy a path limiting the size.
313 : * Abort if too long.
314 : */
315 : void pathcpy(char* dst, size_t size, const char* src);
316 :
317 : /**
318 : * Concatenate a path limiting the size.
319 : * Abort if too long.
320 : */
321 : void pathcat(char* dst, size_t size, const char* src);
322 :
323 : /**
324 : * Concatenate a path limiting the size knowing the length.
325 : * Abort if too long.
326 : */
327 : void pathcatl(char* dst, size_t dst_len, size_t size, const char* src);
328 :
329 : /**
330 : * Concatenate a path limiting the size.
331 : * Abort if too long.
332 : */
333 : void pathcatc(char* dst, size_t size, char c);
334 :
335 : /**
336 : * Import a path limiting the size.
337 : * In Windows all the backslash are converted to the C standard of forward slash.
338 : * Abort if too long.
339 : */
340 : void pathimport(char* dst, size_t size, const char* src);
341 :
342 : /**
343 : * Export a path limiting the size.
344 : * In Windows all the C slashes are converted to the Windows backslash.
345 : * Abort if too long.
346 : */
347 : void pathexport(char* dst, size_t size, const char* src);
348 :
349 : /**
350 : * Print a path.
351 : * Abort if too long.
352 : */
353 : void pathprint(char* dst, size_t size, const char* format, ...) __attribute__((format(attribute_printf, 3, 4)));
354 :
355 : /**
356 : * Ensure the presence of a terminating slash, if it isn't empty.
357 : * Abort if too long.
358 : */
359 : void pathslash(char* dst, size_t size);
360 :
361 : /**
362 : * Cut everything after the latest slash.
363 : *
364 : * If the string doesn't contain any slash, it returns the empty string.
365 : */
366 : void pathcut(char* dst);
367 :
368 : /**
369 : * Compare two paths.
370 : * In Windows it's case insensitive and assumes \ equal at /.
371 : */
372 : int pathcmp(const char* a, const char* b);
373 :
374 : /**
375 : * Check if the root is matching the start of the specified path.
376 : *
377 : * The root MUST end with a /.
378 : *
379 : * In Windows it's case insensitive and assumes \ equal at /.
380 : */
381 : int path_is_root_of(const char* root, const char* path);
382 :
383 : /****************************************************************************/
384 : /* file-system */
385 :
386 : /**
387 : * Create all the ancestor directories if missing.
388 : * The file name, after the last /, is ignored.
389 : */
390 : int mkancestor(const char* file);
391 :
392 : /**
393 : * Change the modification time of an open file.
394 : */
395 : int fmtime(int f, int64_t mtime_sec, int mtime_nsec);
396 :
397 : /**
398 : * Change the modification time of a file or link.
399 : * Note that links are NOT deferenced.
400 : */
401 : int lmtime(const char* path, int64_t mtime_sec, int mtime_nsec);
402 :
403 : /****************************************************************************/
404 : /* advise */
405 :
406 : /**
407 : * Advise modes.
408 : */
409 : #define ADVISE_DEFAULT 0 /**< Default mode. */
410 : #define ADVISE_NONE 1 /**< Bare read/write mode. */
411 : #define ADVISE_SEQUENTIAL 2 /**< Sequential mode. */
412 : #define ADVISE_FLUSH 3 /**< Flush mode. */
413 : #define ADVISE_FLUSH_WINDOW 4 /**< Flush mode with a window of 8MB. */
414 : #define ADVISE_DISCARD 5 /**< Discard the cache after every operation. */
415 : #define ADVISE_DISCARD_WINDOW 6 /**< Discard the cache with a window of 8MB. */
416 : #define ADVISE_DIRECT 7 /**< Direct mode. */
417 :
418 : #define ADVISE_WINDOW_SIZE (8 * 1024 * 1024) /**< Window size. */
419 :
420 : struct advise_struct {
421 : int mode;
422 : data_off_t dirty_begin;
423 : data_off_t dirty_end;
424 : };
425 :
426 : void advise_init(struct advise_struct* advise, int mode);
427 : int advise_flags(struct advise_struct* advise);
428 : int advise_open(struct advise_struct* advise, int f);
429 : int advise_write(struct advise_struct* advise, int f, data_off_t offset, data_off_t size);
430 : int advise_read(struct advise_struct* advise, int f, data_off_t offset, data_off_t size);
431 :
432 : /****************************************************************************/
433 : /* memory */
434 :
435 : /**
436 : * Return the size of the allocated memory.
437 : */
438 : size_t malloc_counter_get(void);
439 :
440 : /**
441 : * Safe malloc.
442 : * If no memory is available, it aborts.
443 : */
444 : void* malloc_nofail(size_t size);
445 :
446 : /**
447 : * Safe cmalloc.
448 : * If no memory is available, it aborts.
449 : */
450 : void* calloc_nofail(size_t count, size_t size);
451 :
452 : /**
453 : * Safe strdup.
454 : * If no memory is available, it aborts.
455 : */
456 : char* strdup_nofail(const char* str);
457 :
458 : /**
459 : * Helper for printing an error about a failed allocation.
460 : */
461 : void malloc_fail(size_t size);
462 :
463 : /****************************************************************************/
464 : /* smartctl */
465 :
466 : /**
467 : * Read smartctl attributes from a stream.
468 : * Return 0 on success.
469 : */
470 : int smartctl_attribute(FILE* f, const char* file, const char* name, struct smart_attr* smart, uint64_t* info, char* serial, char* family, char* model, char* interf);
471 :
472 : /**
473 : * Flush smartctl output from a stream.
474 : */
475 : int smartctl_flush(FILE* f, const char* file, const char* name);
476 :
477 : /**
478 : * Extract the temperature from the SMART info.
479 : * Return -1 if missing
480 : */
481 : int smart_temp(devinfo_t* devinfo);
482 :
483 : /****************************************************************************/
484 : /* thread */
485 :
486 : #if HAVE_THREAD
487 : /**
488 : * Control when to signal the condition variables.
489 : *
490 : * Default is inside the mutex.
491 : *
492 : * Ensure to change that before starting any thread.
493 : */
494 : extern int thread_cond_signal_outside;
495 :
496 : /**
497 : * Thread wrappers to handle error conditions.
498 : */
499 : void thread_mutex_init(thread_mutex_t* mutex);
500 : void thread_mutex_destroy(thread_mutex_t* mutex);
501 : void thread_mutex_lock(thread_mutex_t* mutex);
502 : void thread_mutex_unlock(thread_mutex_t* mutex);
503 : void thread_cond_init(thread_cond_t* cond);
504 : void thread_cond_destroy(thread_cond_t* cond);
505 : void thread_cond_signal(thread_cond_t* cond);
506 : void thread_cond_broadcast(thread_cond_t* cond);
507 : void thread_cond_wait(thread_cond_t* cond, thread_mutex_t* mutex);
508 : void thread_cond_signal_and_unlock(thread_cond_t* cond, thread_mutex_t* mutex);
509 : void thread_cond_broadcast_and_unlock(thread_cond_t* cond, thread_mutex_t* mutex);
510 : void thread_create(thread_id_t* thread, void* (*func)(void*), void* arg);
511 : void thread_join(thread_id_t thread, void** retval);
512 : void thread_yield(void);
513 : #endif
514 :
515 : /****************************************************************************/
516 : /* match */
517 :
518 : /*
519 : * Wild match function.
520 : *
521 : * - ? matches any single character except /
522 : * - * matches any sequence of characters except /
523 : * - ** (nearby a /) matches everything including /
524 : * - ** (not near a /) like *
525 : * - ##/ reduces to nothing in addition to the normal matching of ** (using # instead of * to mess the C comment)
526 : * - [...] matches character classes with support for ranges and negation lile [!...] or [^...], except /
527 : */
528 : int wnmatch(const char* p, const char* t);
529 :
530 : #endif
531 :
|