Nagios  4.3.4
Dev docs for Nagios core and neb-module hackers
nspath.h
Go to the documentation of this file.
1 #ifndef LIBNAGIOS_NSPATH_H_INCLUDED
2 #define LIBNAGIOS_NSPATH_H_INCLUDED
3 #ifndef _GNU_SOURCE
4 # ifndef NODOXY
5 # define _GNU_SOURCE 1
6 # endif
7 #endif
8 #include <errno.h>
9 #include <sys/stat.h>
10 #include "snprintf.h"
11 
12 /**
13  * @file nspath.h
14  * @brief path handling functions
15  *
16  * This library handles path normalization and resolution. It's nifty
17  * if you want to turn relative paths into absolute ones, or if you
18  * want to make insane ones sane, but without chdir()'ing your way
19  * around the filesystem.
20  *
21  * @{
22  */
23 
24 /**
25  * Normalize a path
26  * By "normalize", we mean that we convert dot-slash and dot-dot-slash
27  * embedded components into a legible continuous string of characters.
28  * Leading and trailing slashes are kept exactly as they are in input,
29  * but with sequences of slashes reduced to a single one.
30  *
31  * "foo/bar/.././lala.txt" becomes "foo/lala.txt"
32  * "../../../../bar/../foo/" becomes "/foo/"
33  * "////foo////././bar" becomes "/foo/bar"
34  * @param orig_path The path to normalize
35  * @return A newly allocated string containing the normalized path
36  */
37 extern char *nspath_normalize(const char *orig_path);
38 
39 /**
40  * Make the "base"-relative path "rel_path" absolute.
41  * Turns the relative path "rel_path" into an absolute path and
42  * resolves it as if we were currently in "base". If "base" is
43  * NULL, the current working directory is used. If "base" is not
44  * null, it should be an absolute path for the result to make
45  * sense.
46  *
47  * @param rel_path The relative path to convert
48  * @param base The base directory (if NULL, we use current working dir)
49  * @return A newly allocated string containing the absolute path
50  */
51 extern char *nspath_absolute(const char *rel_path, const char *base);
52 
53 /**
54  * Canonicalize the "base"-relative path "rel_path".
55  * errno gets properly set in case of errors.
56  * @param rel_path The path to transform
57  * @param base The base we should operate relative to
58  * @return Newly allocated canonical path on succes, NULL on errors
59  */
60 extern char *nspath_real(const char *rel_path, const char *base);
61 
62 /**
63  * Get absolute dirname of "path", relative to "base"
64  * @param path Full path to target object (file or subdir)
65  * @param base The base directory (if NULL, we use current working dir)
66  * @return NULL on errors, allocated absolute directory name on success
67  */
68 extern char *nspath_absolute_dirname(const char *path, const char *base);
69 
70 
71 /**
72  * Recursively create a directory, just like mkdir_p would.
73  * @note This function *will* taint errno with ENOENT if any path
74  * component has to be created.
75  * @note If "path" has a trailing slash, NSPATH_MKDIR_SKIP_LAST
76  * won't have any effect. That's considered a feature, since the
77  * option is designed so one can send a file-path to the function
78  * and have it create the directory structure for it.
79  * @param path Path to create, in normalized form
80  * @param mode Filemode (same as mkdir() takes)
81  * @param options Options flag. See NSPATH_MKDIR_* for or-able options
82  * @return 0 on success, -1 on errors and errno will hold error code
83  * from either stat() or mkdir().
84  */
85 extern int nspath_mkdir_p(const char *path, mode_t mode, int options);
86 
87 /** Don't mkdir() last element of path when calling nspath_mkdir_p() */
88 #define NSPATH_MKDIR_SKIP_LAST (1 << 0)
89 
90 /** @} */
91 #endif
char * nspath_normalize(const char *orig_path)
Normalize a path By "normalize", we mean that we convert dot-slash and dot-dot-slash embedded compone...
char * nspath_real(const char *rel_path, const char *base)
Canonicalize the "base"-relative path "rel_path".
char * nspath_absolute(const char *rel_path, const char *base)
Make the "base"-relative path "rel_path" absolute.
char * nspath_absolute_dirname(const char *path, const char *base)
Get absolute dirname of "path", relative to "base".
int nspath_mkdir_p(const char *path, mode_t mode, int options)
Recursively create a directory, just like mkdir_p would.