Skip to content

Commit dddc231

Browse files
committed
Document trust-file.c usage
Add documentation on how fapolicyd-cli and the daemon share the trust-file.c helpers for linked-list edits versus memfd streaming.
1 parent 3a1953b commit dddc231

1 file changed

Lines changed: 136 additions & 17 deletions

File tree

src/library/trust-file.c

Lines changed: 136 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* trust-file.c - Functions for working with trust files
2+
* trust-file.c - Functions for working with trust files
33
* Copyright (c) 2020 Red Hat Inc.
44
* All Rights Reserved.
55
*
@@ -47,6 +47,16 @@
4747
#include "escape.h"
4848
#include "paths.h"
4949

50+
/*
51+
* fapolicyd-cli relies on this file to materialize trust entries into
52+
* linked lists so they can be inspected, deduplicated, and rewritten.
53+
* The daemon also calls into the same helpers when it needs an in-memory
54+
* snapshot instead of streaming updates through a memfd. The routines
55+
* below therefore serve both the CLI's trust management commands and the
56+
* daemon's backend, with the CLI-only helpers called out explicitly in
57+
* their documentation.
58+
*/
59+
5060
#define BUFFER_SIZE 4096+1+1+1+10+1+64+1
5161
#define FILE_READ_FORMAT "%4096s %lu %64s" // path size SHA256
5262
#define FILE_WRITE_FORMAT "%s %lu %s\n" // path size SHA256
@@ -73,12 +83,13 @@ struct trust_seen_entry {
7383
};
7484

7585

76-
77-
/**
78-
* Take a path and create a string that is ready to be written to the disk.
86+
/*
87+
* make_data_string - Create a trust-file payload for a path.
88+
* @path: Absolute path that should be represented in the trust file.
7989
*
80-
* @param path Path to create a string from
81-
* @return Data string ready to be written to the disk or NULL on error
90+
* The resulting buffer contains the "source size hash" triplet used when
91+
* rewriting trust fragments. The caller takes ownership of the allocated
92+
* string and must free it. Returns NULL if the file cannot be measured.
8293
*/
8394
static char *make_data_string(const char *path)
8495
{
@@ -121,13 +132,14 @@ static char *make_data_string(const char *path)
121132
}
122133
return line;
123134
}
124-
125-
/**
126-
* Write a list into a file
135+
/*
136+
* write_out_list - Persist a linked list of trust entries to disk.
137+
* @list: List of entries created by trust_file_load or CLI helpers.
138+
* @dest: Destination trust file to be rewritten.
127139
*
128-
* @param list List to write into a file
129-
* @param dest Destination file
130-
* @return 0 on success, 1 on error
140+
* This helper is used exclusively by the CLI trust management commands
141+
* after they finish editing an in-memory list. Returns 0 on success and
142+
* 1 when the destination file could not be opened.
131143
*/
132144
static int write_out_list(list_t *list, const char *dest)
133145
{
@@ -172,6 +184,16 @@ static int write_out_list(list_t *list, const char *dest)
172184
return 0;
173185
}
174186

187+
/*
188+
* trust_file_append - Add entries to a trust file for the CLI.
189+
* @fpath: Path to the trust fragment that should be extended.
190+
* @list: List of paths prepared by the CLI for insertion.
191+
*
192+
* The CLI populates @list with path indexes and this helper computes the
193+
* hash/size payloads before merging the new entries into @fpath. Returns
194+
* 0 when the update succeeds and 1 if the existing file could not be
195+
* parsed.
196+
*/
175197
int trust_file_append(const char *fpath, list_t *list)
176198
{
177199
list_t content;
@@ -195,6 +217,15 @@ int trust_file_append(const char *fpath, list_t *list)
195217

196218
#define DELIM ' '
197219
#define MAX_DELIMS 2
220+
/*
221+
* parse_line_backwards - Split a trust-file line into its components.
222+
* @line: Buffer containing the raw line (modified in place).
223+
* @path: Output buffer for the stored path.
224+
* @size: Output parameter for the recorded size.
225+
* @sha: Output buffer for the SHA256 string.
226+
*
227+
* Returns 0 when parsing succeeds or -1 when the line is malformed.
228+
*/
198229
static int parse_line_backwards(char *line, char *path, unsigned long *size, char *sha)
199230
{
200231
if (line == NULL || path == NULL || size == NULL || sha == NULL)
@@ -253,12 +284,15 @@ static int parse_line_backwards(char *line, char *path, unsigned long *size, cha
253284

254285
return 0;
255286
}
256-
/**
257-
* @brief Load trust file into list
287+
/*
288+
* trust_file_load - Load a trust fragment into a list or memfd.
289+
* @fpath: Full path to the trust fragment.
290+
* @list: Destination list when @memfd is negative.
291+
* @memfd: File descriptor used for streaming output, or -1 for lists.
258292
*
259-
* @param fpath Full path to trust file
260-
* @param list Trust file will be loaded into this list
261-
* @return 0 on success, 1 if file can't be open, 2 on parsing error
293+
* This helper is shared by the daemon and CLI. It returns 0 on success,
294+
* 1 when the file cannot be opened, 2 on parse errors, and 3 when memory
295+
* could not be allocated while tracking duplicates.
262296
*/
263297
int trust_file_load(const char *fpath, list_t *list, int memfd)
264298
{
@@ -369,6 +403,15 @@ int trust_file_load(const char *fpath, list_t *list, int memfd)
369403
}
370404

371405

406+
/*
407+
* trust_file_delete_path - Remove matching entries from a trust file.
408+
* @fpath: Path to the trust fragment being edited.
409+
* @path: Prefix that identifies entries scheduled for removal.
410+
*
411+
* Used only by the CLI trust management commands. Returns the number of
412+
* entries deleted, 0 when the file could not be opened,
413+
* and -1 on parse errors.
414+
*/
372415
int trust_file_delete_path(const char *fpath, const char *path)
373416
{
374417
list_t list;
@@ -417,6 +460,15 @@ int trust_file_delete_path(const char *fpath, const char *path)
417460
return count;
418461
}
419462

463+
/*
464+
* trust_file_update_path - Refresh hashes for matching entries.
465+
* @fpath: Trust fragment that should be rewritten.
466+
* @path: Prefix designating entries that must be re-measured.
467+
*
468+
* Used only by the CLI trust management commands. Returns the number of
469+
* entries updated, 0 when the file could not be opened,
470+
* and -1 when the existing file cannot be parsed.
471+
*/
420472
int trust_file_update_path(const char *fpath, const char *path)
421473
{
422474
list_t list;
@@ -451,6 +503,15 @@ int trust_file_update_path(const char *fpath, const char *path)
451503
return count;
452504
}
453505

506+
/*
507+
* trust_file_rm_duplicates - Prune CLI additions already present on disk.
508+
* @fpath: Trust fragment checked for duplicates.
509+
* @list: Pending CLI additions to compare against existing entries.
510+
*
511+
* Used only by the CLI trust management commands before appending new
512+
* entries. Returns 0 after pruning,
513+
* or -1 when the trust fragment could not be opened or parsed.
514+
*/
454515
int trust_file_rm_duplicates(const char *fpath, list_t *list)
455516
{
456517
list_t trust_file;
@@ -479,6 +540,13 @@ int trust_file_rm_duplicates(const char *fpath, list_t *list)
479540

480541

481542

543+
/*
544+
* ftw_load - nftw callback that aggregates trust fragments.
545+
* @fpath: Current file discovered by nftw.
546+
* @sb: (unused) file metadata supplied by nftw.
547+
* @typeflag: nftw entry type.
548+
* @ftwbuf: (unused) traversal context from nftw.
549+
*/
482550
static int ftw_load(const char *fpath,
483551
const struct stat *sb __attribute__ ((unused)),
484552
int typeflag,
@@ -489,6 +557,13 @@ static int ftw_load(const char *fpath,
489557
return FTW_CONTINUE;
490558
}
491559

560+
/*
561+
* ftw_delete_path - nftw callback that deletes matching entries.
562+
* @fpath: Current trust fragment examined by nftw.
563+
* @sb: (unused) file metadata supplied by nftw.
564+
* @typeflag: nftw entry type.
565+
* @ftwbuf: (unused) traversal context from nftw.
566+
*/
492567
static int ftw_delete_path(const char *fpath,
493568
const struct stat *sb __attribute__ ((unused)),
494569
int typeflag,
@@ -499,6 +574,13 @@ static int ftw_delete_path(const char *fpath,
499574
return FTW_CONTINUE;
500575
}
501576

577+
/*
578+
* ftw_update_path - nftw callback that updates matching entries.
579+
* @fpath: Current trust fragment examined by nftw.
580+
* @sb: (unused) file metadata supplied by nftw.
581+
* @typeflag: nftw entry type.
582+
* @ftwbuf: (unused) traversal context from nftw.
583+
*/
502584
static int ftw_update_path(const char *fpath,
503585
const struct stat *sb __attribute__ ((unused)),
504586
int typeflag,
@@ -509,6 +591,13 @@ static int ftw_update_path(const char *fpath,
509591
return FTW_CONTINUE;
510592
}
511593

594+
/*
595+
* ftw_rm_duplicates - nftw callback removing duplicates from CLI lists.
596+
* @fpath: Current trust fragment examined by nftw.
597+
* @sb: (unused) file metadata supplied by nftw.
598+
* @typeflag: nftw entry type.
599+
* @ftwbuf: (unused) traversal context from nftw.
600+
*/
512601
static int ftw_rm_duplicates(const char *fpath,
513602
const struct stat *sb __attribute__ ((unused)),
514603
int typeflag,
@@ -523,6 +612,15 @@ static int ftw_rm_duplicates(const char *fpath,
523612

524613

525614

615+
/*
616+
* trust_file_load_all - Aggregate every trust fragment.
617+
* @list: Destination list when @memfd is negative.
618+
* @memfd: File descriptor that receives streamed entries, or -1.
619+
*
620+
* Used by both the daemon and CLI to populate either an in-memory list or
621+
* a memfd-backed snapshot covering the primary trust file plus the tree
622+
* of per-package fragments.
623+
*/
526624
void trust_file_load_all(list_t *list, int memfd)
527625
{
528626
list_empty(&_list);
@@ -537,6 +635,13 @@ void trust_file_load_all(list_t *list, int memfd)
537635
_memfd = -1;
538636
}
539637

638+
/*
639+
* trust_file_delete_path_all - Delete matching entries across all files.
640+
* @path: Prefix designating entries to remove.
641+
*
642+
* Used only by the CLI trust management commands to remove a path from
643+
* every trust fragment. Returns the number of entries deleted.
644+
*/
540645
int trust_file_delete_path_all(const char *path)
541646
{
542647
_path = strdup(path);
@@ -546,6 +651,13 @@ int trust_file_delete_path_all(const char *path)
546651
return _count;
547652
}
548653

654+
/*
655+
* trust_file_update_path_all - Refresh hashes across every trust file.
656+
* @path: Prefix designating entries that must be re-measured.
657+
*
658+
* Used only by the CLI trust management commands. Returns the number of
659+
* entries updated.
660+
*/
549661
int trust_file_update_path_all(const char *path)
550662
{
551663
_path = strdup(path);
@@ -555,6 +667,13 @@ int trust_file_update_path_all(const char *path)
555667
return _count;
556668
}
557669

670+
/*
671+
* trust_file_rm_duplicates_all - Remove duplicates across trust files.
672+
* @list: Pending CLI additions to prune before appending.
673+
*
674+
* Used only by the CLI trust management commands prior to calling
675+
* trust_file_append().
676+
*/
558677
void trust_file_rm_duplicates_all(list_t *list)
559678
{
560679
list_empty(&_list);

0 commit comments

Comments
 (0)