summaryrefslogtreecommitdiff
path: root/src/util/fs.h
blob: ad0a4496c6624081a49f9e4dcb0d0559042b3428 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#ifndef _SK_UTIL_FS_H
#define _SK_UTIL_FS_H

#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <limits.h>
#include <pwd.h>
#include <sys/stat.h>

#if HAVE_STDBOOL_H
#include <stdbool.h>
#endif

/**
 * exists:
 * Check if a given pathname exists.
 *
 * @param pathname: the path to check
 * @return true if @pathname exists, false otherwise.
 */
#define exists(pathname) (0 == access(pathname, F_OK))

/**
 * is_dir:
 * Check if a given pathname is a directory.
 *
 * @param pathname: the path to check
 * @return true if @pathname exists and is a directory, false otherwise.
 */
bool is_dir(const char *restrict pathname);

/**
 * myhome:
 * Returns a pointer to a string containing the current user’s home
 * directory.
 * The returned pointer points to a static location (don’t free() it) and
 * may be changed on future invocations.
 *
 * This function makes sure that the returned string is null-terminated and
 * truncates the result to fit within PATH_MAX if necessary.
 *
 * @returns home dir
 */
const char *myhome();

/**
 * creatfile:
 * create a file on the file system.
 *
 * @param path: pathname for the file to create
 * @param mode: desired file mode (cf. creat(3p))
 * @return 0 on success, -1 otherwise (errno set)
 */
int creatfile(const char *path, mode_t mode);

/**
 * mktempdir:
 * creates a temporary directory in a suitable location.
 *
 * @param dest: must be a pointer to a buffer of a least PATH_MAX+1 length.
 * @return dest if a directory was created successfully, NULL otherwise.
 */
char *mktempdir(char *dest);

/**
 * rmtree:
 * delete a directory on the file system and all its descendants.
 *
 * @param dir: the path to the directory to delete.
 * @return 0 on success, -1 otherwise.
 */
int rmtree(const char *dir);

#endif