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: 4cc6e1359b12efe96b6284a62edcef370be48c69 $")
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
31#include <freeradius-devel/util/sbuff.h>
32
33#ifdef HAVE_DIRENT_H
34# include <dirent.h>
35#endif
36
37#ifdef HAVE_GLOB_H
38#include <glob.h>
39#endif
40
41/** Callback for allowing additional operations on newly created directories
42 *
43 * @param[in] fd Of newly created directory.
44 * @param[in] path Either relative or full to the new directory.
45 * Should only be used for debug messages or functions
46 * that don't have an 'at' variant.
47 * @param[in] uctx User data to pass to callback.
48 * @return
49 * - 0 on success.
50 * - -1 on failure.
51 */
52typedef int(*fr_mkdir_func_t)(int fd, char const *path, void *uctx);
53
54typedef struct {
55 uid_t uid;
56 gid_t gid;
58
59int fr_mkdir_chown(int fd, char const *path, void *uctx);
60
61ssize_t fr_mkdir(int *fd_out, char const *path, ssize_t len, mode_t mode,
62 fr_mkdir_func_t func, void *uctx);
63
64char *fr_realpath(TALLOC_CTX *ctx, char const *path, ssize_t len);
65
66ssize_t fr_touch(int *fd_out, char const *filename, mode_t mode, bool mkdir, mode_t dir_mode);
67
68int fr_unlink(char const *filename);
69
70char const *fr_cwd_strip(char const *filename);
71
72int fr_dirfd(int *dirfd, char const **filename, char const *pathname) CC_HINT(nonnull);
73
74typedef enum {
77#ifdef HAVE_DIRENT_H
78 FR_GLOBDIR_DIR,
79#endif
80#ifdef HAVE_GLOB_H
81 FR_GLOBDIR_GLOB,
82#endif
84
85typedef struct {
87
88 char *path;
89 char *filename;
90
91 union {
92 DIR *dir;
93#ifdef HAVE_GLOB_H
94 struct {
95 size_t gl_current;
96 glob_t glob;
97 };
98#endif
99 };
101
102int fr_globdir_iter_init(char const **filename, char const *dir, char const *pattern, fr_globdir_iter_t *iter);
103int fr_globdir_iter_next(char const **filename, fr_globdir_iter_t *iter);
105
108
109#ifdef __cplusplus
110}
111#endif
#define RCSIDH(h, id)
Definition build.h:507
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
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:86
char * path
Definition file.h:88
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:55
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:56
const fr_sbuff_escape_rules_t fr_filename_escape_dots
Definition file.c:932
fr_globdir_type_t
Definition file.h:74
@ FR_GLOBDIR_FILE
Definition file.h:76
@ FR_GLOBDIR_INVALID
Definition file.h:75
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:89
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 * 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:52
long int ssize_t
unsigned int mode_t
int nonnull(2, 5))