The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
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: be731cab1fd4d0685b77bbadde5116bab48d117d $")
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
31#include <freeradius-devel/util/talloc.h>
32#include <stdbool.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
107#ifdef __cplusplus
108}
109#endif
#define RCSIDH(h, id)
Definition build.h:486
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:219
int fr_unlink(char const *filename)
Remove a regular file from the filesystem.
Definition file.c:367
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:687
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:39
uid_t uid
Definition file.h:56
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:323
gid_t gid
Definition file.h:57
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:845
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:384
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:412
char * fr_realpath(TALLOC_CTX *ctx, char const *path, ssize_t len)
Convenience wrapper around realpath.
Definition file.c:284
int fr_globdir_iter_free(fr_globdir_iter_t *iter)
Definition file.c:879
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
long int ssize_t
unsigned int mode_t
int nonnull(2, 5))