]> Sergey Matveev's repositories - bfs.git/blob - src/ctx.h
Skip mtab
[bfs.git] / src / ctx.h
1 // Copyright © Tavian Barnes <tavianator@tavianator.com>
2 // SPDX-License-Identifier: 0BSD
3
4 /**
5  * bfs execution context.
6  */
7
8 #ifndef BFS_CTX_H
9 #define BFS_CTX_H
10
11 #include "bftw.h"
12 #include "config.h"
13 #include "diag.h"
14 #include "trie.h"
15 #include <stddef.h>
16 #include <sys/resource.h>
17 #include <time.h>
18
19 /**
20  * The execution context for bfs.
21  */
22 struct bfs_ctx {
23         /** The number of command line arguments. */
24         size_t argc;
25         /** The unparsed command line arguments. */
26         char **argv;
27
28         /** The root paths. */
29         const char **paths;
30         /** The main command line expression. */
31         struct bfs_expr *expr;
32         /** An expression for files to filter out. */
33         struct bfs_expr *exclude;
34
35         /** -mindepth option. */
36         int mindepth;
37         /** -maxdepth option. */
38         int maxdepth;
39
40         /** bftw() flags. */
41         enum bftw_flags flags;
42         /** bftw() search strategy. */
43         enum bftw_strategy strategy;
44
45         /** Threads (-j). */
46         int threads;
47         /** Optimization level (-O). */
48         int optlevel;
49         /** Debugging flags (-D). */
50         enum debug_flags debug;
51         /** Whether to ignore deletions that race with bfs (-ignore_readdir_race). */
52         bool ignore_races;
53         /** Whether to follow POSIXisms more closely ($POSIXLY_CORRECT). */
54         bool posixly_correct;
55         /** Whether to show a status bar (-status). */
56         bool status;
57         /** Whether to only return unique files (-unique). */
58         bool unique;
59         /** Whether to print warnings (-warn/-nowarn). */
60         bool warn;
61         /** Whether to only handle paths with xargs-safe characters (-X). */
62         bool xargs_safe;
63
64         /** Color data. */
65         struct colors *colors;
66         /** The error that occurred parsing the color table, if any. */
67         int colors_error;
68         /** Colored stdout. */
69         struct CFILE *cout;
70         /** Colored stderr. */
71         struct CFILE *cerr;
72
73         /** User cache. */
74         struct bfs_users *users;
75         /** Group table. */
76         struct bfs_groups *groups;
77         /** The error that occurred parsing the group table, if any. */
78         int groups_error;
79
80         /** Table of mounted file systems. */
81         struct bfs_mtab *mtab;
82         /** The error that occurred parsing the mount table, if any. */
83         int mtab_error;
84
85         /** All the files owned by the context. */
86         struct trie files;
87         /** The number of files owned by the context. */
88         int nfiles;
89
90         /** The initial RLIMIT_NOFILE soft limit. */
91         rlim_t nofile_soft;
92         /** The initial RLIMIT_NOFILE hard limit. */
93         rlim_t nofile_hard;
94
95         /** The current time. */
96         struct timespec now;
97 };
98
99 /**
100  * @return
101  *         A new bfs context, or NULL on failure.
102  */
103 struct bfs_ctx *bfs_ctx_new(void);
104
105 /**
106  * Get the mount table.
107  *
108  * @param ctx
109  *         The bfs context.
110  * @return
111  *         The cached mount table, or NULL on failure.
112  */
113 const struct bfs_mtab *bfs_ctx_mtab(const struct bfs_ctx *ctx);
114
115 /**
116  * Deduplicate an opened file.
117  *
118  * @param ctx
119  *         The bfs context.
120  * @param cfile
121  *         The opened file.
122  * @param path
123  *         The path to the opened file (or NULL for standard streams).
124  * @return
125  *         If the same file was opened previously, that file is returned.  If cfile is a new file,
126  *         cfile itself is returned.  If an error occurs, NULL is returned.
127  */
128 struct CFILE *bfs_ctx_dedup(struct bfs_ctx *ctx, struct CFILE *cfile, const char *path);
129
130 /**
131  * Flush any caches for consistency with external processes.
132  *
133  * @param ctx
134  *         The bfs context.
135  */
136 void bfs_ctx_flush(const struct bfs_ctx *ctx);
137
138 /**
139  * Dump the parsed command line.
140  *
141  * @param ctx
142  *         The bfs context.
143  * @param flag
144  *         The -D flag that triggered the dump.
145  */
146 void bfs_ctx_dump(const struct bfs_ctx *ctx, enum debug_flags flag);
147
148 /**
149  * Free a bfs context.
150  *
151  * @param ctx
152  *         The context to free.
153  * @return
154  *         0 on success, -1 if any errors occurred.
155  */
156 int bfs_ctx_free(struct bfs_ctx *ctx);
157
158 #endif // BFS_CTX_H