Add the latex and html directories to .gitignore.
[adu.git] / adu.h
1 /*
2  * Copyright (C) 2008 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file adu.h Global definitions. */
8
9 #include <sys/stat.h>
10 #include <fcntl.h>
11 #include <signal.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <time.h> /* time(), localtime() */
15 #include <unistd.h>
16 #include <errno.h>
17 #include <limits.h>
18 #include <stdarg.h>
19 #include <inttypes.h>
20 #include <string.h>
21 #include <assert.h>
22 #include <osl.h>
23 #include "gcc-compat.h"
24 #include "select.cmdline.h"
25
26 /** debug loglevel, gets really noisy */
27 #define DEBUG 1
28 /** still noisy, but won't fill your disk */
29 #define INFO  2
30 /** normal, but significant event */
31 #define NOTICE 3
32 /** unexpected event that can be handled */
33 #define WARNING 4
34 /** unhandled error condition */
35 #define ERROR 5
36 /** system might be unreliable */
37 #define CRIT 6
38 /** last message before exit */
39 #define EMERG 7
40
41 /** Log messages with lower priority than that will not be compiled in. */
42 #define COMPILE_TIME_LOGLEVEL 0
43
44 /** \cond */
45 #if DEBUG > COMPILE_TIME_LOGLEVEL
46 #define DEBUG_LOG(f,...) __log(DEBUG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
47 #else
48 #define DEBUG_LOG(...) do {;} while (0)
49 #endif
50
51 #if INFO > COMPILE_TIME_LOGLEVEL
52 #define INFO_LOG(f,...) __log(INFO, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
53 #else
54 #define INFO_LOG(...) do {;} while (0)
55 #endif
56
57 #if NOTICE > COMPILE_TIME_LOGLEVEL
58 #define NOTICE_LOG(f,...) __log(NOTICE, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
59 #else
60 #define NOTICE_LOG(...) do {;} while (0)
61 #endif
62
63 #if WARNING > COMPILE_TIME_LOGLEVEL
64 #define WARNING_LOG(f,...) __log(WARNING, "%s: " f, __FUNCTION__, ##  __VA_ARGS__)
65 #else
66 #define WARNING_LOG(...) do {;} while (0)
67 #endif
68
69 #if ERROR > COMPILE_TIME_LOGLEVEL
70 #define ERROR_LOG(f,...) __log(ERROR, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
71 #else
72 #define ERROR_LOG(...) do {;} while (0)
73 #endif
74
75 #if CRIT > COMPILE_TIME_LOGLEVEL
76 #define CRIT_LOG(f,...) __log(CRIT, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
77 #else
78 #define CRIT_LOG(...) do {;} while (0)
79 #endif
80
81 #if EMERG > COMPILE_TIME_LOGLEVEL
82 #define EMERG_LOG(f,...) __log(EMERG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
83 #else
84 #define EMERG_LOG(...)
85 #endif
86 /** \endcond */
87
88 /**
89  * Write a log message to a dynamically allocated string.
90  *
91  * \param fmt Usual format string.
92  * \param p Result pointer.
93  *
94  * \sa printf(3). */
95 #define VSPRINTF(fmt, p) \
96 { \
97         int n; \
98         size_t size = 100; \
99         p = adu_malloc(size); \
100         while (1) { \
101                 va_list ap; \
102                 /* Try to print in the allocated space. */ \
103                 va_start(ap, fmt); \
104                 n = vsnprintf(p, size, fmt, ap); \
105                 va_end(ap); \
106                 /* If that worked, return the string. */ \
107                 if (n > -1 && n < size) \
108                         break; \
109                 /* Else try again with more space. */ \
110                 if (n > -1) /* glibc 2.1 */ \
111                         size = n + 1; /* precisely what is needed */ \
112                 else /* glibc 2.0 */ \
113                         size *= 2; /* twice the old size */ \
114                 p = adu_realloc(p, size); \
115         } \
116 }
117
118 /** Evaluates to 1 if x < y, to -1 if x > y and to 0 if x == y. */
119 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
120
121 /** The columns of the directory table. */
122 enum dir_table_columns {
123         /** The name of the directory. */
124         DT_NAME,
125         /** The dir count number. */
126         DT_NUM,
127         /** The number of the parent directory. */
128         DT_PARENT_NUM,
129         /** The number of bytes of all regular files. */
130         DT_BYTES,
131         /** The number of all regular files. */
132         DT_FILES,
133         /** Number of columns in this table. */
134         NUM_DT_COLUMNS
135 };
136
137 /** The columns of the id table. */
138 enum user_table_columns {
139         /** The numer of the directory. */
140         UT_DIR_NUM,
141         /** The number of bytes of all regular files in this dir owned by this id. */
142         UT_BYTES,
143         /** The number of files in this dir owned by this id. */
144         UT_FILES,
145         /** Number of columns in this table. */
146         NUM_UT_COLUMNS
147 };
148
149 /** Flags for the user hash table. */
150 enum uid_info_flags {
151         /** Whether this slot of the hash table is used. */
152         UI_FL_SLOT_USED = 1,
153         /** Whether this uid should be taken into account. */
154         UI_FL_ADMISSIBLE = 2,
155 };
156
157 /** Information about one admissible user. */
158 struct user_info {
159         /** User ID. */
160         uint32_t uid;
161         /** \sa enum uid_info_flags. */
162         uint32_t flags;
163         /** The user name.  */
164         char *pw_name;
165         /** The user table of this user.*/
166         struct osl_table *table;
167         /** Total number of files owned by this user. */
168         uint64_t files;
169         /** Total number of bytes owned by this user. */
170         uint64_t bytes;
171         /** Total number of directories that contain at least one file */
172         uint64_t dirs;
173         /** The description of the user table. */
174         struct osl_table_description *desc;
175 };
176
177 /**
178  * Describes one range of admissible user IDs.
179  *
180  * adu converts the admissible user ids given at the command line
181  * into an array of such structs.
182  */
183 struct uid_range {
184         /** Lowest admissible user ID. */
185         uint32_t low;
186         /** Greatest admissible user ID. */
187         uint32_t high;
188 };
189
190 enum search_uid_flags {
191         OPEN_USER_TABLE = 1,
192         CREATE_USER_TABLE = 2,
193 };
194
195 #define FOR_EACH_UID_RANGE(ur, urs) for (ur = urs; ur->low <= ur->high; ur++)
196
197 extern uint32_t num_uids;
198 extern struct osl_table *dir_table;
199
200 /** The adu command line options. */
201 extern struct gengetopt_args_info conf;
202
203 /**
204  * The select command line options.
205  *
206  * Either given at the command line, or via the \a set command
207  * in interactive mode.
208  */
209 extern struct select_args_info select_conf;
210
211 /* adu.c */
212 __printf_2_3 void __log(int, const char*, ...);
213 int open_dir_table(int create);
214 void check_signals(void);
215 void close_all_tables(void);
216 char *get_uid_list_name(void);
217 void create_hash_table(unsigned bits);
218 int search_uid(uint32_t uid, struct uid_range *urs,
219                 enum search_uid_flags flags, struct user_info **ui_ptr);
220 int for_each_admissible_user(int (*func)(struct user_info *, void *),
221                 void *data);
222 void sort_hash_table(int (*comp)(const void *, const void *));
223
224 /* create.c */
225 int com_create(void);
226
227 /* interactive.c */
228 void print_interactive_help(void);
229 int com_interactive(void);