Debian Codegraph @

src/README.md

raw symbols SCIP
1 # Developer Guide
2
3 This document outlines the coding standards, development workflows, and
4 documentation practices for the `httpdirfs` project.
5
6 ## Coding Standards
7
8 Adhering to a consistent coding style ensures the codebase remains readable and
9 maintainable.
10
11 ### Naming Conventions
12
13 | Category | Convention | Example |
14 | :-------------------------- | :----------------------------- | :------------------------- |
15 | **External Variables** | UPPERCASE | `GLOBAL_VAR` |
16 | **Static Global Variables** | lowercase | `static_var` |
17 | **Function Names** | `TypeName_verb` or `verb_noun` | `Cache_read`, `get_config` |
18 | **Type Names** | CamelCase | `CacheManager`, `Config` |
19
20 ### Indentation and Formatting
21
22 The project follows the **Kernighan & Ritchie (K&R)** style. We use
23 `clang-format` for automatic formatting.
24
25 Manual formatting can be triggered via Meson:
26
27 ```bash
28 meson compile format
29 ```
30
31 ______________________________________________________________________
32
33 ## Building and Debugging
34
35 The project supports two main build configurations: **Debug** and **Release**.
36
37 ### Debug vs. Release Builds
38
39 | Feature | Debug Build (`--buildtype=debug`) | Release Build (`--buildtype=release`) |
40 | :--------------------- | :-------------------------------------------------------------- | :--------------------------------------------------------- |
41 | **Optimization Level** | None (`-O0`) — code maps exactly to source lines. | High (`-O3`) — optimized for performance and speed. |
42 | **Debug Symbols** | Enabled (`-g`) — for step-through debugging via GDB/LLDB. | Omitted — minimal binary size. |
43 | **`DEBUG` Macro** | Defined (`-DDEBUG`) — enables verbose debug logging by default. | Undefined — standard logging only, maximizing performance. |
44 | **Use Case** | Local development, debugging, and bug reporting. | Production deployment. |
45
46 ### Creating a Debug Build
47
48 By default, the project is configured with a release build configuration
49 (`buildtype=release`). To create a debug build, configure the build directory
50 with the `debug` build type:
51
52 ```bash
53 meson setup builddir --buildtype=debug
54 ```
55
56 Or, if the build directory has already been configured, you can reconfigure it:
57
58 ```bash
59 meson configure builddir --buildtype=debug
60 ```
61
62 Then compile the project:
63
64 ```bash
65 meson compile -C builddir
66 ```
67
68 When building with the `debug` build type, the `DEBUG` macro is automatically
69 defined in `meson.build`. This enables debug-level logging messages by default
70 in `httpdirfs`.
71
72 ______________________________________________________________________
73
74 ## Development Workflow
75
76 We use automated tools to maintain code quality and prevent common errors.
77
78 ### Pre-commit Hooks
79
80 We use `pre-commit` to run a suite of checks before every commit. This includes
81 code formatting, static analysis, and basic sanity checks.
82
83 **Checks performed:**
84
85 - Code formatting (`clang-format`)
86 - Static analysis (`clang-tidy`)
87 - Spelling checks (`codespell`)
88 - Markdown formatting (`prettier`)
89 - Standard file checks (trailing whitespace, merge conflicts, large files)
90
91 #### Setup
92
93 1. Install `pre-commit`:
94
95 ```bash
96 pip install pre-commit
97 ```
98
99 1. Install the git hooks:
100
101 ```bash
102 pre-commit install
103 ```
104
105 #### Usage
106
107 Hooks run automatically on `git commit`. To run all checks manually on all
108 files:
109
110 ```bash
111 pre-commit run --all-files
112 ```
113
114 > [!IMPORTANT]
115 > The `clang-tidy` hook requires `compile_commands.json` to be present in the
116 > `builddir`. If you haven't configured the project yet, run:
117 >
118 > ```bash
119 > meson setup builddir
120 > ```
121
122 > [!IMPORTANT]
123 > **Manual Hooks Requirement Before Pushing:** While basic unit tests and short
124 > integration tests run automatically on every commit, the slow/intensive
125 > integration tests (`integration_long`) are placed in the `manual` stage to
126 > keep the default local commit feedback loop fast.
127 >
128 > **You are required to manually run the full test suite hooks before pushing
129 > your changes:**
130 >
131 > ```bash
132 > # Run all tests using GCC
133 > pre-commit run all-tests-gcc --hook-stage manual --all-files
134 >
135 > # Run all tests (GCC Debug)
136 > pre-commit run all-tests-gcc-debug --hook-stage manual --all-files
137 >
138 > # Run all tests using Clang
139 > pre-commit run all-tests-clang --hook-stage manual --all-files
140 >
141 > # Run all tests (Clang Debug)
142 > pre-commit run all-tests-clang-debug --hook-stage manual --all-files
143 > ```
144 >
145 > Note that the Clang-based hooks require `clang` to be installed (e.g.,
146 > `sudo apt install clang` on Debian/Ubuntu systems).
147
148 ### Running Tests
149
150 We support three test suites configured and managed via Meson:
151
152 1. **`unit_test`**: Runs all C-level unit tests (`test_util`, `test_cache`,
153 `test_config`, `test_link`).
154 1. **`integration_short`**: Runs fast integration tests (excludes the large 1 GB
155 file) verifying core mounting, directory traversal, read-only structures, and
156 integrity. Completes in **~3 seconds**.
157 1. **`integration_long`**: Runs resource-intensive cache integration tests
158 involving concurrent multithreaded reads of a 1 GB file. Completes in **~25
159 seconds**.
160
161 #### Test Execution Environments
162
163 ##### 1. Default/Local Pre-Commit Behavior
164
165 To speed up local development feedback, only the fast tests are executed by
166 default.
167
168 - **Command**: Running a plain `meson test -C builddir` or triggering the
169 default local `git commit` hook.
170 - **Behavior**: Runs the `unit_test` and `integration_short` suites. The
171 `integration_long` suite is explicitly excluded by default via the default
172 test setup in `tests/meson.build`.
173
174 ##### 2. CI Pipeline Behavior
175
176 The GitHub Actions workflow (`build.yml`) validates the entire test suite on
177 every pull request and push to the master branch using GCC and Clang
178 configurations:
179
180 - **Unit tests step**: `meson test -C builddir --no-suite integration --verbose`
181 (runs only the `unit_test` suite)
182 - **Integration tests (short) step**:
183 `meson test -C builddir --suite integration_short --verbose` (runs fast
184 integration tests)
185 - **Integration tests (long) step**:
186 `meson test -C builddir --suite integration_long --verbose` (runs
187 resource-intensive integration tests)
188
189 ##### 3. Required Pre-Push Validation
190
191 Before pushing changes, developers are **required** to manually run the full
192 suite using the manual pre-commit hooks to catch potential cache regressions and
193 memory leaks locally:
194
195 ```bash
196 # Run GCC-based compilation and all tests (unit_test, integration_short, integration_long)
197 pre-commit run all-tests-gcc --hook-stage manual --all-files
198
199 # Run GCC-based compilation in Debug mode (with leak detection enabled) and all tests
200 pre-commit run all-tests-gcc-debug --hook-stage manual --all-files
201
202 # Run Clang-based compilation and all tests
203 pre-commit run all-tests-clang --hook-stage manual --all-files
204
205 # Run Clang-based compilation in Debug mode (with leak detection enabled) and all tests
206 pre-commit run all-tests-clang-debug --hook-stage manual --all-files
207 ```
208
209 ______________________________________________________________________
210
211 #### Running Specific Suites Manually via Meson
212
213 ##### Setup
214
215 Before running tests, ensure that the build directory is configured:
216
217 ```bash
218 meson setup builddir
219 ```
220
221 ##### Run Unit Tests Individually
222
223 If you want to run only the unit tests:
224
225 ```bash
226 meson test -C builddir --suite unit_test
227 ```
228
229 You can also execute specific unit tests by target name:
230
231 ```bash
232 meson test -C builddir test_util
233 meson test -C builddir test_cache
234 meson test -C builddir test_config
235 meson test -C builddir test_link
236 ```
237
238 ##### Run Short Integration Tests Individually
239
240 ```bash
241 meson test -C builddir --suite integration_short
242 ```
243
244 ##### Run Long Integration Tests Individually
245
246 ```bash
247 meson test -C builddir --suite integration_long
248 ```
249
250 ##### Verbose Output and Debugging
251
252 By default, Meson hides test output unless a test fails. To see detailed test
253 results, use the following options:
254
255 - **Print logs on failure only:**
256 ```bash
257 meson test -C builddir --print-errorlogs
258 ```
259 - **Verbose output (all stdout/stderr):**
260 ```bash
261 meson test -C builddir -v
262 ```
263
264 #### Integration Tests Runner Prerequisites
265
266 The integration tests require the following system dependencies:
267
268 - **FUSE** (with `/dev/fuse` access)
269 - `fusermount3` (or `fusermount`)
270 - **Python 3**
271
272 ##### Run Integration Tests Manually
273
274 Alternatively, you can run the test runner script directly by providing the path
275 to the compiled `httpdirfs` binary and the desired mode flag:
276
277 ```bash
278 # Run short integration tests
279 ./tests/integration/run_integration_test.sh --short builddir/httpdirfs
280
281 # Run long integration tests
282 ./tests/integration/run_integration_test.sh --long builddir/httpdirfs
283 ```
284
285 ______________________________________________________________________
286
287 ## Logging and Error Handling
288
289 `httpdirfs` uses a centralized logging system defined in `log.h`.
290
291 - Use the `lprintf(type, ...)` macro for logging. It automatically includes the
292 file name, function name, and line number.
293 - Fatal errors should use the `fatal` log level.
294 - `lprintf(fatal, ...)` will automatically call `exit_failure()`, which prints a
295 backtrace and terminates the program.
296
297 ______________________________________________________________________
298
299 ## Wrapper Functions
300
301 To ensure consistent error handling, memory tracking, and safe concurrency, the
302 project uses custom macro and function wrappers around standard library calls.
303 **Developers should always use these wrappers instead of their standard C
304 counterparts.**
305
306 ### 1. Memory Management Wrappers
307
308 Memory allocation wrappers automatically handle allocation failures by printing
309 a fatal backtrace and exiting, eliminating the need for manual null-checks after
310 every call. They also track allocations to prevent memory leaks in debug
311 configurations.
312
313 | Standard Function | Wrapper Macro | Notes & Safety Rules |
314 | :---------------- | :-------------------- | :--------------------------------------------------------------------------------------- |
315 | `calloc` | `CALLOC(nmemb, size)` | Automatically terminates with a `fatal` error on allocation failure. |
316 | `realloc` | `REALLOC(ptr, size)` | Safely handles resizing and tracks allocations. |
317 | `strdup` | `STRDUP(s)` | Duplicates string with error checking and tracking. |
318 | `strndup` | `STRNDUP(s, n)` | Duplicates at most `n` characters with error checking and tracking. |
319 | `realpath` | `REALPATH(p, r)` | Resolving path names with wrapper checks. |
320 | `free` | `FREE(ptr)` | Automatically sets the pointer variable to `NULL` after freeing to prevent double-frees. |
321
322 > [!WARNING]
323 > **Important rules for `FREE(ptr)`:**
324 >
325 > - `FREE` modifies the pointer itself by setting it to `NULL`. This will fail
326 > to compile if used on const-qualified pointer variables (e.g.,
327 > `char * const p`).
328 > - When used on function parameters (e.g.,
329 > `void func(void *ptr) { FREE(ptr); }`), it only nullifies the local
330 > parameter copy. The caller's original pointer remains unchanged (and
331 > dangling), so be sure to nullify the caller's pointer manually.
332
333 ### 2. Concurrency and Synchronization Wrappers
334
335 All pthread mutexes, condition variables, and semaphores must be manipulated via
336 wrappers. These wrappers perform automatic validation of return codes and assert
337 that no errors occur during locking, unlocking, waiting, or signaling.
338
339 #### Pthread Mutexes
340
341 - **Initialize**: `PTHREAD_MUTEX_INIT(mutex, attr)` (wraps `pthread_mutex_init`)
342 - **Lock**: `PTHREAD_MUTEX_LOCK(mutex)` (wraps `pthread_mutex_lock`)
343 - **Unlock**: `PTHREAD_MUTEX_UNLOCK(mutex)` (wraps `pthread_mutex_unlock`)
344 - **Destroy**: `PTHREAD_MUTEX_DESTROY(mutex)` (wraps `pthread_mutex_destroy`)
345
346 #### Pthread Condition Variables
347
348 - **Initialize**: `PTHREAD_COND_INIT(cond, attr)` (wraps `pthread_cond_init`)
349 - **Wait**: `PTHREAD_COND_WAIT(cond, mutex)` (wraps `pthread_cond_wait`)
350 - **Broadcast**: `PTHREAD_COND_BROADCAST(cond)` (wraps `pthread_cond_broadcast`)
351 - **Destroy**: `PTHREAD_COND_DESTROY(cond)` (wraps `pthread_cond_destroy`)
352
353 #### POSIX Semaphores
354
355 - **Initialize**: `SEM_INIT(sem, pshared, value)` (wraps `sem_init`)
356 - **Wait**: `SEM_WAIT(sem)` (wraps `sem_wait`)
357 - **Post**: `SEM_POST(sem)` (wraps `sem_post`)
358 - **Destroy**: `SEM_DESTROY(sem)` (wraps `sem_destroy`)
359
360 ______________________________________________________________________
361
362 ## Documentation
363
364 ### Doxygen
365
366 We use Doxygen to generate comprehensive API documentation.
367
368 To generate the documentation:
369
370 1. Enter your build directory.
371 1. Run the Doxygen target:
372
373 ```bash
374 meson compile doxygen
375 ```
376
377 The documentation will be generated in the `doxygen` directory at the root of
378 the repository.