00001 #ifndef LIBNAGIOS_NSPATH_H_INCLUDED 00002 #define LIBNAGIOS_NSPATH_H_INCLUDED 00003 #ifndef _GNU_SOURCE 00004 # ifndef NODOXY 00005 # define _GNU_SOURCE 1 00006 # endif 00007 #endif 00008 #include <errno.h> 00009 #include <sys/stat.h> 00010 #include "snprintf.h" 00011 00012 /** 00013 * @file nspath.h 00014 * @brief path handling functions 00015 * 00016 * This library handles path normalization and resolution. It's nifty 00017 * if you want to turn relative paths into absolute ones, or if you 00018 * want to make insane ones sane, but without chdir()'ing your way 00019 * around the filesystem. 00020 * 00021 * @{ 00022 */ 00023 00024 /** 00025 * Normalize a path 00026 * By "normalize", we mean that we convert dot-slash and dot-dot-slash 00027 * embedded components into a legible continuous string of characters. 00028 * Leading and trailing slashes are kept exactly as they are in input, 00029 * but with sequences of slashes reduced to a single one. 00030 * 00031 * "foo/bar/.././lala.txt" becomes "foo/lala.txt" 00032 * "../../../../bar/../foo/" becomes "/foo/" 00033 * "////foo////././bar" becomes "/foo/bar" 00034 * @param orig_path The path to normalize 00035 * @return A newly allocated string containing the normalized path 00036 */ 00037 extern char *nspath_normalize(const char *orig_path); 00038 00039 /** 00040 * Make the "base"-relative path "rel_path" absolute. 00041 * Turns the relative path "rel_path" into an absolute path and 00042 * resolves it as if we were currently in "base". If "base" is 00043 * NULL, the current working directory is used. If "base" is not 00044 * null, it should be an absolute path for the result to make 00045 * sense. 00046 * 00047 * @param rel_path The relative path to convert 00048 * @param base The base directory (if NULL, we use current working dir) 00049 * @return A newly allocated string containing the absolute path 00050 */ 00051 extern char *nspath_absolute(const char *rel_path, const char *base); 00052 00053 /** 00054 * Canonicalize the "base"-relative path "rel_path". 00055 * errno gets properly set in case of errors. 00056 * @param rel_path The path to transform 00057 * @param base The base we should operate relative to 00058 * @return Newly allocated canonical path on success, NULL on errors 00059 */ 00060 extern char *nspath_real(const char *rel_path, const char *base); 00061 00062 /** 00063 * Get absolute dirname of "path", relative to "base" 00064 * @param path Full path to target object (file or subdir) 00065 * @param base The base directory (if NULL, we use current working dir) 00066 * @return NULL on errors, allocated absolute directory name on success 00067 */ 00068 extern char *nspath_absolute_dirname(const char *path, const char *base); 00069 00070 00071 /** 00072 * Recursively create a directory, just like mkdir_p would. 00073 * @note This function *will* taint errno with ENOENT if any path 00074 * component has to be created. 00075 * @note If "path" has a trailing slash, NSPATH_MKDIR_SKIP_LAST 00076 * won't have any effect. That's considered a feature, since the 00077 * option is designed so one can send a file-path to the function 00078 * and have it create the directory structure for it. 00079 * @param path Path to create, in normalized form 00080 * @param mode Filemode (same as mkdir() takes) 00081 * @param options Options flag. See NSPATH_MKDIR_* for or-able options 00082 * @return 0 on success, -1 on errors and errno will hold error code 00083 * from either stat() or mkdir(). 00084 */ 00085 extern int nspath_mkdir_p(const char *path, mode_t mode, int options); 00086 00087 /** Don't mkdir() last element of path when calling nspath_mkdir_p() */ 00088 #define NSPATH_MKDIR_SKIP_LAST (1 << 0) 00089 00090 /** @} */ 00091 #endif