Skip to content

Commit 3a1953b

Browse files
committed
Restore some linked list functionality in file backend
Looks like fapolicyd-cli uses the linked lists for some of what it does. Restore the linked list functionality for the time being.
1 parent dbd0634 commit 3a1953b

1 file changed

Lines changed: 27 additions & 7 deletions

File tree

src/library/trust-file.c

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -273,15 +273,16 @@ int trust_file_load(const char *fpath, list_t *list, int memfd)
273273
return 1;
274274

275275
while (fgets(buffer, BUFFER_SIZE, file)) {
276-
char name[4097], sha[65], *index = NULL;
276+
char name[4097], sha[65], *index = NULL, *data = NULL;
277277
char data_buf[BUFFER_SIZE];
278278
unsigned long sz;
279279
unsigned int tsource = SRC_FILE_DB;
280280

281281
line++;
282282

283283
if (iscntrl(buffer[0]) || buffer[0] == '#') {
284-
if (line == 1 && strncmp(buffer, HEADER_OLD, strlen(HEADER_OLD)) == 0)
284+
if (line == 1 &&
285+
strncmp(buffer, HEADER_OLD, strlen(HEADER_OLD)) == 0)
285286
escaped = 1;
286287
continue;
287288
}
@@ -310,7 +311,8 @@ int trust_file_load(const char *fpath, list_t *list, int memfd)
310311

311312
HASH_FIND_STR(seen, index, entry);
312313
if (entry) {
313-
msg(LOG_WARNING, "%s contains a duplicate %s", fpath, index);
314+
msg(LOG_WARNING, "%s contains a duplicate %s",
315+
fpath, index);
314316
free(index);
315317
continue;
316318
}
@@ -327,10 +329,28 @@ int trust_file_load(const char *fpath, list_t *list, int memfd)
327329
HASH_ADD_KEYPTR(hh, seen, entry->path,
328330
strlen(entry->path), entry);
329331

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));
332+
if (memfd >= 0) {
333+
if (dprintf(memfd, "%s %s\n", index, data_buf) < 0)
334+
msg(LOG_ERR,
335+
"dprintf failed writing %s to memfd (%s)",
336+
index, strerror(errno));
337+
} else {
338+
data = strdup(data_buf);
339+
if (data == NULL) {
340+
msg(LOG_ERR, "Out of memory saving %s", index);
341+
HASH_DEL(seen, entry);
342+
free(entry);
343+
free(index);
344+
continue;
345+
}
346+
347+
if (list_append(list, index, data)) {
348+
HASH_DEL(seen, entry);
349+
free(entry);
350+
free(index);
351+
free(data);
352+
}
353+
}
334354
}
335355

336356
out:

0 commit comments

Comments
 (0)