aboutsummaryrefslogtreecommitdiff
path: root/include/util.h
blob: 28d21bc9c036324a8c7bcd2e6e1e2dc9c94c62d5 (plain)
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
/* SPDX-License-Identifier: GPL-3.0-or-later */
#ifndef UTIL_H
#define UTIL_H

#include <sys/types.h>
#include <stdint.h>

/*
  Removes all preceeding and trailing slashes, shortens all sequences of
  slashes to a single slash and returns failure state if one of the path
  components is '..' or '.'.

  Returns 0 on success.
*/
int canonicalize_name(char *filename);

/*
  A wrapper around the write() system call. It retries the write if it is
  interrupted by a signal or only part of the data was written.
*/
ssize_t write_retry(int fd, const void *data, size_t size);

/*
  A wrapper around the read() system call. It retries the read if it is
  interrupted by a signal or less than the desired size was read.
*/
ssize_t read_retry(int fd, void *buffer, size_t size);

/*
  A common implementation of the '--version' command line flag.

  Prints out version information. The program name is extracted from the
  BSD style __progname global variable.
*/
void print_version(void);

/*
  A wrapper around mkdir() that behaves like 'mkdir -p'. It tries to create
  every component of the given path and skips already existing entries.

  Returns 0 on success.
*/
int mkdir_p(const char *path);

/* Returns 0 on success. On failure, prints error message to stderr. */
int pushd(const char *path);

/* Same as pushd, but the string doesn't have to be null-terminated. */
int pushdn(const char *path, size_t len);

/* Returns 0 on success. On failure, prints error message to stderr. */
int popd(void);

/*
  Write zero bytes to an output file to padd it to specified block size.
  Returns 0 on success. On failure, prints error message to stderr.
*/
int padd_file(int outfd, uint64_t size, size_t blocksize);

#endif /* UTIL_H */