File: support.h

package info (click to toggle)
snapraid 13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,656 kB
  • sloc: ansic: 46,018; makefile: 1,004; sh: 154
file content (464 lines) | stat: -rw-r--r-- 12,429 bytes parent folder | download
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*
 * Copyright (C) 2011 Andrea Mazzoleni
 *
 * 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 of the License, 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, see <http://www.gnu.org/licenses/>.
 */

#ifndef __SUPPORT_H
#define __SUPPORT_H

/****************************************************************************/
/* lock */

/**
 * Initialize and destroy the locks.
 */
void lock_init(void);
void lock_done(void);

/**
 * Lock used for printf.
 *
 * In Windows printf() is not atomic, and multiple threads
 * will have output interleaved.
 *
 * Note that even defining __USE_MINGW_ANSI_STDIO the problem persists.
 *
 * See for example:
 *
 * Weird output when I use pthread and printf.
 * http://stackoverflow.com/questions/13190254/weird-output-when-i-use-pthread-and-printf
 *
 * This is also required in other OS because we split output in stdlog in
 * two fprintf calls.
 */
void lock_msg(void);
void unlock_msg(void);

/**
 * Lock used for memory counter.
 */
void lock_memory(void);
void unlock_memory(void);

/****************************************************************************/
/* log */

/**
 * Fatal error messages.
 *
 * Messages printed before an early termination.
 *
 * These messages go in the log file and in stderr unconditionally.
 */
void log_fatal(const char* format, ...) __attribute__((format(attribute_printf, 1, 2)));

/**
 * Unexpected error messages.
 *
 * Messages reporting error conditions that don't prevent the program to run.
 *
 * Some of them could be also serious errors, like "silent errors".
 * In such case, the summary result is always printed as error,
 * and we are sure to notify the user in some way.
 *
 * These messages go in the log file if specified, otherwise they go in stderr.
 */
void log_error(const char* format, ...) __attribute__((format(attribute_printf, 1, 2)));

/**
 * Expected error messages, without fallback to stderr.
 *
 * These errors are "someway" expected, and then they never go to screen.
 * For example, when undeleting missing files, the messages for missing files
 * are not shown.
 *
 * These messages go in the log file if specified, otherwise they are lost.
 */
void log_expected(const char* format, ...) __attribute__((format(attribute_printf, 1, 2)));

/**
 * Tag messages.
 *
 * Messages are in tag format, like "tag:entry:...".
 *
 * These messages never go on the screen, but only in the log file if specified.
 *
 * Note that this function, allows not \n terminated strings.
 *
 * These messages are buffered. Use msg_flush() to flush them.
 */
void log_tag(const char* format, ...) __attribute__((format(attribute_printf, 1, 2)));

/**
 * Flush the log.
 */
void log_flush(void);

/**
 * Pointer to log function.
 */
typedef void fptr(const char* format, ...) __attribute__((format(attribute_printf, 1, 2)));

/****************************************************************************/
/* message */

/**
 * Message levels.
 *
 * The levels control the amount of information printed on the screen.
 * Note that log_fatal(), log_error(), log_expected() and log_tag() are not affected by this option.
 *
 * From the most quiet to the most verbose.
 */
#define MSG_STATUS -3
#define MSG_INFO -2
#define MSG_PROGRESS -1
#define MSG_BAR 0
#define MSG_VERBOSE 1

/**
 * Selected message level.
 */
extern int msg_level;

/**
 * State messages.
 *
 * Messages that tell what the program is doing or did, but limited to few lines.
 * They are status information, and summary results.
 */
void msg_status(const char* format, ...) __attribute__((format(attribute_printf, 1, 2)));

/**
 * Info messages.
 *
 * Messages that tell what was done.
 * Potentially a lot of messages are possible. They can still be on the screen,
 * as losing them we don't lose information.
 *
 * These messages never go in the log file, because there is always a corresponding log_tag().
 */
void msg_info(const char* format, ...) __attribute__((format(attribute_printf, 1, 2)));

/**
 * Progress messages.
 *
 * Message that tell the progress of program.
 *
 * These messages also go in the log file.
 */
void msg_progress(const char* format, ...) __attribute__((format(attribute_printf, 1, 2)));

/**
 * Progress bar messages.
 *
 * Message that show the percentage of the progress.
 *
 * These messages never go in the log file.
 *
 * These messages are buffered. Use msg_flush() to flush them.
 */
void msg_bar(const char* format, ...) __attribute__((format(attribute_printf, 1, 2)));

/**
 * Verbose messages.
 *
 * Message that tell what is already expected.
 *
 * These messages also go in the log file.
 */
void msg_verbose(const char* format, ...) __attribute__((format(attribute_printf, 1, 2)));

/**
 * Flush the output.
 */
void msg_flush(void);

/****************************************************************************/
/* print */

/**
 * Print a repeated char.
 */
void printc(char c, size_t pad);

/**
 * Print a string with right space padding.
 */
void printr(const char* str, size_t pad);

/**
 * Print a string with left space padding.
 */
void printl(const char* str, size_t pad);

/**
 * Print a probability with space padding.
 */
void printp(double v, size_t pad);

/****************************************************************************/
/* string */

#define ESC_MAX (PATH_MAX * 2 + 1)

/**
 * Escape a string for the log.
 *
 * \param buffer Preallocated buffer of ESC_MAX size.
 *
 * Chars ':', '\n', '\r' and '\' are escaped to '\d', '\\n', '\\r' and '\\'.
 */
const char* esc_tag(const char* str, char* buffer);

/**
 * Escape a string for the shell.
 *
 * \param buffer Preallocated buffer of ESC_MAX size.
 */
const char* esc_shell_multi(const char** str_map, unsigned str_max, char* buffer);
static inline const char* esc_shell(const char* str, char* buffer)
{
	return esc_shell_multi(&str, 1, buffer);
}

/**
 * Polish a string.
 *
 * Not printable chars are replaced by spaces.
 *
 * Note that the passed string is modified.
 */
char* strpolish(char* s);

/**
 * Split a string in multiple tokens separated by delimiters.
 *
 * Multiple delimiters are grouped together.
 */
unsigned strsplit(char** split_map, unsigned split_max, char* line, const char* delimiters);

/**
 * Trim spaces from the start and the end
 */
char* strtrim(char* s);

/**
 * Lower case
 */
char* strlwr(char* s);

/*
 * Find the first occurrence of 'needle' in 'haystack' only if it
 * appears as a separate word (space, number or string boundaries).
 *
 * Returns a pointer to the beginning of the match, or NULL if not found.
 */
char* worddigitstr(const char* haystack, const char* needle);

/****************************************************************************/
/* path */

/**
 * Copy a path limiting the size.
 * Abort if too long.
 */
void pathcpy(char* dst, size_t size, const char* src);

/**
 * Concatenate a path limiting the size.
 * Abort if too long.
 */
void pathcat(char* dst, size_t size, const char* src);

/**
 * Concatenate a path limiting the size knowing the length.
 * Abort if too long.
 */
void pathcatl(char* dst, size_t dst_len, size_t size, const char* src);

/**
 * Concatenate a path limiting the size.
 * Abort if too long.
 */
void pathcatc(char* dst, size_t size, char c);

/**
 * Import a path limiting the size.
 * In Windows all the backslash are converted to the C standard of forward slash.
 * Abort if too long.
 */
void pathimport(char* dst, size_t size, const char* src);

/**
 * Export a path limiting the size.
 * In Windows all the C slashes are converted to the Windows backslash.
 * Abort if too long.
 */
void pathexport(char* dst, size_t size, const char* src);

/**
 * Print a path.
 * Abort if too long.
 */
void pathprint(char* dst, size_t size, const char* format, ...) __attribute__((format(attribute_printf, 3, 4)));

/**
 * Ensure the presence of a terminating slash, if it isn't empty.
 * Abort if too long.
 */
void pathslash(char* dst, size_t size);

/**
 * Cut everything after the latest slash.
 *
 * If the string doesn't contain any slash, it returns the empty string.
 */
void pathcut(char* dst);

/**
 * Compare two paths.
 * In Windows it's case insensitive and assumes \ equal at /.
 */
int pathcmp(const char* a, const char* b);

/****************************************************************************/
/* file-system */

/**
 * Create all the ancestor directories if missing.
 * The file name, after the last /, is ignored.
 */
int mkancestor(const char* file);

/**
 * Change the modification time of an open file.
 */
int fmtime(int f, int64_t mtime_sec, int mtime_nsec);

/**
 * Change the modification time of a file or link.
 * Note that links are NOT deferenced.
 */
int lmtime(const char* path, int64_t mtime_sec, int mtime_nsec);

/****************************************************************************/
/* advise */

/**
 * Advise modes.
 */
#define ADVISE_DEFAULT 0 /**< Default mode. */
#define ADVISE_NONE 1 /**< Bare read/write mode. */
#define ADVISE_SEQUENTIAL 2 /**< Sequential mode. */
#define ADVISE_FLUSH 3 /**< Flush mode. */
#define ADVISE_FLUSH_WINDOW 4 /**< Flush mode with a window of 8MB. */
#define ADVISE_DISCARD 5 /**< Discard the cache after every operation. */
#define ADVISE_DISCARD_WINDOW 6 /**< Discard the cache with a window of 8MB. */
#define ADVISE_DIRECT 7 /**< Direct mode. */

#define ADVISE_WINDOW_SIZE (8 * 1024 * 1024) /**< Window size. */

struct advise_struct {
	int mode;
	data_off_t dirty_begin;
	data_off_t dirty_end;
};

void advise_init(struct advise_struct* advise, int mode);
int advise_flags(struct advise_struct* advise);
int advise_open(struct advise_struct* advise, int f);
int advise_write(struct advise_struct* advise, int f, data_off_t offset, data_off_t size);
int advise_read(struct advise_struct* advise, int f, data_off_t offset, data_off_t size);

/****************************************************************************/
/* memory */

/**
 * Return the size of the allocated memory.
 */
size_t malloc_counter_get(void);

/**
 * Safe malloc.
 * If no memory is available, it aborts.
 */
void* malloc_nofail(size_t size);

/**
 * Safe cmalloc.
 * If no memory is available, it aborts.
 */
void* calloc_nofail(size_t count, size_t size);

/**
 * Safe strdup.
 * If no memory is available, it aborts.
 */
char* strdup_nofail(const char* str);

/**
 * Helper for printing an error about a failed allocation.
 */
void malloc_fail(size_t size);

/****************************************************************************/
/* smartctl */

/**
 * Read smartctl attributes from a stream.
 * Return 0 on success.
 */
int smartctl_attribute(FILE* f, const char* file, const char* name, uint64_t* smart, char* serial, char* vendor, char* model);

/**
 * Flush smartctl output from a stream.
 */
int smartctl_flush(FILE* f, const char* file, const char* name);

/****************************************************************************/
/* thread */

#if HAVE_THREAD
/**
 * Control when to signal the condition variables.
 *
 * Default is inside the mutex.
 *
 * Ensure to change that before starting any thread.
 */
extern int thread_cond_signal_outside;

/**
 * Thread wrappers to handle error conditions.
 */
void thread_mutex_init(thread_mutex_t* mutex);
void thread_mutex_destroy(thread_mutex_t* mutex);
void thread_mutex_lock(thread_mutex_t* mutex);
void thread_mutex_unlock(thread_mutex_t* mutex);
void thread_cond_init(thread_cond_t* cond);
void thread_cond_destroy(thread_cond_t* cond);
void thread_cond_signal(thread_cond_t* cond);
void thread_cond_broadcast(thread_cond_t* cond);
void thread_cond_wait(thread_cond_t* cond, thread_mutex_t* mutex);
void thread_cond_signal_and_unlock(thread_cond_t* cond, thread_mutex_t* mutex);
void thread_cond_broadcast_and_unlock(thread_cond_t* cond, thread_mutex_t* mutex);
void thread_create(thread_id_t* thread, void* (* func)(void *), void *arg);
void thread_join(thread_id_t thread, void** retval);
void thread_yield(void);
#endif

#endif