The FreeRADIUS server $Id: f3670dba8951ca10eb4948feb3dc3db9423a334f $
Loading...
Searching...
No Matches
file.h
Go to the documentation of this file.
1#pragma once
2/*
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
16 */
17
18/** Various miscellaneous functions to manipulate files and paths
19 *
20 * @file src/lib/util/file.h
21 *
22 * @copyright 2019 The FreeRADIUS project
23 * @copyright 2019 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
24 */
25RCSIDH(util_file_h, "$Id: 18cccafebe739ba0133538433c664f388e30c58f $")
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
31#include <freeradius-devel/util/sbuff.h>
32#include <freeradius-devel/util/value.h>
33
34#ifdef HAVE_DIRENT_H
35# include <dirent.h>
36#endif
37
38#ifdef HAVE_GLOB_H
39#include <glob.h>
40#endif
41
42/** Callback for allowing additional operations on newly created directories
43 *
44 * @param[in] fd Of newly created directory.
45 * @param[in] path Either relative or full to the new directory.
46 * Should only be used for debug messages or functions
47 * that don't have an 'at' variant.
48 * @param[in] uctx User data to pass to callback.
49 * @return
50 * - 0 on success.
51 * - -1 on failure.
52 */
53typedef int(*fr_mkdir_func_t)(int fd, char const *path, void *uctx);
54
55typedef struct {
56 uid_t uid;
57 gid_t gid;
59
60int fr_mkdir_chown(int fd, char const *path, void *uctx);
61
62ssize_t fr_mkdir(int *fd_out, char const *path, ssize_t len, mode_t mode,
63 fr_mkdir_func_t func, void *uctx);
64
65char *fr_realpath(TALLOC_CTX *ctx, char const *path, ssize_t len);
66
67ssize_t fr_touch(int *fd_out, char const *filename, mode_t mode, bool mkdir, mode_t dir_mode);
68
69int fr_unlink(char const *filename);
70
71char const *fr_cwd_strip(char const *filename);
72
73int fr_dirfd(int *dirfd, char const **filename, char const *pathname) CC_HINT(nonnull);
74
75typedef enum {
78#ifdef HAVE_DIRENT_H
79 FR_GLOBDIR_DIR,
80#endif
81#ifdef HAVE_GLOB_H
82 FR_GLOBDIR_GLOB,
83#endif
85
86typedef struct {
88
89 char *path;
90 char *filename;
91
92 union {
93 DIR *dir;
94#ifdef HAVE_GLOB_H
95 struct {
96 size_t gl_current;
97 glob_t glob;
98 };
99#endif
100 };
102
103int fr_globdir_iter_init(char const **filename, char const *dir, char const *pattern, fr_globdir_iter_t *iter);
104int fr_globdir_iter_next(char const **filename, fr_globdir_iter_t *iter);
106
107bool fr_filename_ok(char const *filename) CC_HINT(nonnull);
108char const *fr_filename(char const *path);
109char const *fr_filename_common_trim(char const *path, char const *common);
110
111int fr_filename_box_make_safe(TALLOC_CTX *ctx, fr_value_box_t *vb) CC_HINT(nonnull(2));
112
115
116#ifdef __cplusplus
117}
118#endif
#define RCSIDH(h, id)
Definition build.h:513
ssize_t fr_mkdir(int *fd_out, char const *path, ssize_t len, mode_t mode, fr_mkdir_func_t func, void *uctx)
Create directories that are missing in the specified path.
Definition file.c:218
bool fr_filename_ok(char const *filename)
See if a filename is OK.
Definition file.c:1015
int fr_unlink(char const *filename)
Remove a regular file from the filesystem.
Definition file.c:366
int fr_globdir_iter_init(char const **filename, char const *dir, char const *pattern, fr_globdir_iter_t *iter)
Initialize an iterator over filenames.
Definition file.c:693
fr_globdir_type_t type
Definition file.h:87
char * path
Definition file.h:89
int fr_mkdir_chown(int fd, char const *path, void *uctx)
Callback for the common case of chown() of the directory.
Definition file.c:38
uid_t uid
Definition file.h:56
const fr_sbuff_escape_rules_t fr_filename_escape
Definition file.c:916
ssize_t fr_touch(int *fd_out, char const *filename, mode_t mode, bool mkdir, mode_t dir_mode)
Create an empty file.
Definition file.c:322
gid_t gid
Definition file.h:57
char const * fr_filename_common_trim(char const *path, char const *common)
Trim a common prefix from a filename.
Definition file.c:1082
const fr_sbuff_escape_rules_t fr_filename_escape_dots
Definition file.c:932
fr_globdir_type_t
Definition file.h:75
@ FR_GLOBDIR_FILE
Definition file.h:77
@ FR_GLOBDIR_INVALID
Definition file.h:76
int fr_globdir_iter_next(char const **filename, fr_globdir_iter_t *iter)
Get the next filename.
Definition file.c:851
char * filename
Definition file.h:90
char const * fr_cwd_strip(char const *filename)
Intended to be used in logging functions to make output more readable.
Definition file.c:383
int fr_dirfd(int *dirfd, char const **filename, char const *pathname)
From a pathname, return fd and filename needed for *at() functions.
Definition file.c:411
char const * fr_filename(char const *path)
Get the filename from a path.
Definition file.c:1066
char * fr_realpath(TALLOC_CTX *ctx, char const *path, ssize_t len)
Convenience wrapper around realpath.
Definition file.c:283
int fr_globdir_iter_free(fr_globdir_iter_t *iter)
Definition file.c:885
int(* fr_mkdir_func_t)(int fd, char const *path, void *uctx)
Callback for allowing additional operations on newly created directories.
Definition file.h:53
int fr_filename_box_make_safe(TALLOC_CTX *ctx, fr_value_box_t *vb))
Escape a string-typed value box so its contents are safe to use in a filename.
Definition file.c:960
long int ssize_t
unsigned int mode_t
int nonnull(2, 5))