2626#include "config.h"
2727
2828#include <ctype.h>
29+ #include <errno.h>
2930#include <fcntl.h>
3031#include <ftw.h>
3132#include <stddef.h>
6465list_t _list ;
6566char * _path ;
6667int _count ;
68+ int _memfd = -1 ;
6769
6870struct trust_seen_entry {
6971 const char * path ;
@@ -174,7 +176,7 @@ int trust_file_append(const char *fpath, list_t *list)
174176{
175177 list_t content ;
176178 list_init (& content );
177- int rc = trust_file_load (fpath , & content );
179+ int rc = trust_file_load (fpath , & content , -1 );
178180 // if trust file does not exist, we ignore it as it will be created while writing
179181 if (rc == 2 ) {
180182 // exit on parse error, we dont want invalid entries to be autoremoved
@@ -258,7 +260,7 @@ static int parse_line_backwards(char *line, char *path, unsigned long *size, cha
258260 * @param list Trust file will be loaded into this list
259261 * @return 0 on success, 1 if file can't be open, 2 on parsing error
260262 */
261- int trust_file_load (const char * fpath , list_t * list )
263+ int trust_file_load (const char * fpath , list_t * list , int memfd )
262264{
263265 char buffer [BUFFER_SIZE ];
264266 int escaped = 0 ;
@@ -271,7 +273,8 @@ int trust_file_load(const char *fpath, list_t *list)
271273 return 1 ;
272274
273275 while (fgets (buffer , BUFFER_SIZE , file )) {
274- char name [4097 ], sha [65 ], * index = NULL , * data = NULL ;
276+ char name [4097 ], sha [65 ], * index = NULL ;
277+ char data_buf [BUFFER_SIZE ];
275278 unsigned long sz ;
276279 unsigned int tsource = SRC_FILE_DB ;
277280
@@ -289,19 +292,17 @@ int trust_file_load(const char *fpath, list_t *list)
289292 goto out ;
290293 }
291294
292- if (asprintf (& data , DATA_FORMAT , tsource , sz , sha ) == -1 )
293- data = NULL ;
295+ int len = snprintf (data_buf , sizeof (data_buf ),
296+ DATA_FORMAT , tsource , sz , sha );
297+ if (len < 0 || len >= (int )sizeof (data_buf )) {
298+ msg (LOG_ERR , "Entry too large in %s" , fpath );
299+ continue ;
300+ }
294301
295- // if old format unescape
302+ /* If the legacy format was used, unescape the stored path. */
296303 index = escaped ? unescape (name ) : strdup (name );
297304 if (index == NULL ) {
298305 msg (LOG_ERR , "Could not unescape %s from %s" , name , fpath );
299- free (data );
300- continue ;
301- }
302-
303- if (data == NULL ) {
304- free (index );
305306 continue ;
306307 }
307308
@@ -311,28 +312,25 @@ int trust_file_load(const char *fpath, list_t *list)
311312 if (entry ) {
312313 msg (LOG_WARNING , "%s contains a duplicate %s" , fpath , index );
313314 free (index );
314- free (data );
315315 continue ;
316316 }
317317
318318 entry = malloc (sizeof (* entry ));
319319 if (!entry ) {
320320 msg (LOG_ERR , "Out of memory tracking %s" , index );
321321 free (index );
322- free (data );
323322 rc = 3 ;
324323 goto out ;
325324 }
326325
327326 entry -> path = index ;
328- HASH_ADD_KEYPTR (hh , seen , entry -> path , strlen (entry -> path ), entry );
327+ HASH_ADD_KEYPTR (hh , seen , entry -> path ,
328+ strlen (entry -> path ), entry );
329329
330- if (list_append (list , index , data )) {
331- HASH_DEL (seen , entry );
332- free (entry );
333- free (index );
334- free (data );
335- }
330+ if (dprintf (memfd , "%s %s\n" , index , data_buf ) < 0 )
331+ msg (LOG_ERR ,
332+ "dprintf failed writing %s to memfd (%s)" ,
333+ index , strerror (errno ));
336334 }
337335
338336out :
@@ -342,17 +340,20 @@ int trust_file_load(const char *fpath, list_t *list)
342340
343341 HASH_ITER (hh , seen , item , tmp ) {
344342 HASH_DEL (seen , item );
343+ if (memfd >= 0 )
344+ free ((char * )item -> path );
345345 free (item );
346346 }
347347
348348 return rc ;
349349}
350350
351+
351352int trust_file_delete_path (const char * fpath , const char * path )
352353{
353354 list_t list ;
354355 list_init (& list );
355- int rc = trust_file_load (fpath , & list );
356+ int rc = trust_file_load (fpath , & list , -1 );
356357 switch (rc ) {
357358 case 1 :
358359 msg (LOG_ERR , "Cannot open %s" , fpath );
@@ -400,7 +401,7 @@ int trust_file_update_path(const char *fpath, const char *path)
400401{
401402 list_t list ;
402403 list_init (& list );
403- int rc = trust_file_load (fpath , & list );
404+ int rc = trust_file_load (fpath , & list , -1 );
404405 switch (rc ) {
405406 case 1 :
406407 msg (LOG_ERR , "Cannot open %s" , fpath );
@@ -434,7 +435,7 @@ int trust_file_rm_duplicates(const char *fpath, list_t *list)
434435{
435436 list_t trust_file ;
436437 list_init (& trust_file );
437- int rc = trust_file_load (fpath , & trust_file );
438+ int rc = trust_file_load (fpath , & trust_file , -1 );
438439 switch (rc ) {
439440 case 1 :
440441 msg (LOG_ERR , "Cannot open %s" , fpath );
@@ -464,7 +465,7 @@ static int ftw_load(const char *fpath,
464465 struct FTW * ftwbuf __attribute__ ((unused )))
465466{
466467 if (typeflag == FTW_F )
467- trust_file_load (fpath , & _list );
468+ trust_file_load (fpath , & _list , _memfd );
468469 return FTW_CONTINUE ;
469470}
470471
@@ -502,12 +503,18 @@ static int ftw_rm_duplicates(const char *fpath,
502503
503504
504505
505- void trust_file_load_all (list_t * list )
506+ void trust_file_load_all (list_t * list , int memfd )
506507{
507508 list_empty (& _list );
508- trust_file_load (TRUST_FILE_PATH , & _list );
509+ _memfd = memfd ;
510+ /* Populate either the in-memory list or the memfd snapshot. */
511+ trust_file_load (TRUST_FILE_PATH , & _list , memfd );
509512 nftw (TRUST_DIR_PATH , & ftw_load , FTW_NOPENFD , FTW_FLAGS );
510- list_merge (list , & _list );
513+ if (memfd < 0 )
514+ list_merge (list , & _list );
515+ else
516+ list_empty (& _list );
517+ _memfd = -1 ;
511518}
512519
513520int trust_file_delete_path_all (const char * path )
0 commit comments