File: store.c

package info (click to toggle)
dnssec-trigger 0.17%2Brepack-5.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,956 kB
  • sloc: ansic: 18,699; sh: 976; makefile: 495; xml: 444; objc: 421; cpp: 18
file content (75 lines) | stat: -rw-r--r-- 2,067 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "config.h"
#include <string.h>
#include <stdio.h>

#include "store.h"
#include "string_list.h"
#include "log.h"

struct store store_init(const char *dir, const char *full_path, const char *full_path_tmp) {
    struct string_list cache;
    struct store s;
    FILE *fp;
    size_t line_len = 512;
    ssize_t read_len;
    char* line;
    string_list_init(&cache);
    s.dir = dir,
    s.path = full_path,
    s.path_tmp = full_path_tmp,
    s.cache = cache;
    // Read cache into the string list
    fp = fopen(full_path, "r");
    if (fp == NULL) {
	log_err("cannot open %s: %s", full_path, strerror(errno));
        return s;
    }
    line = (char *)calloc_or_die(line_len);
    memset(line, 0, line_len);
    while ((read_len = getline(&line, &line_len, fp)) != -1){
	if(read_len > 0 && line[read_len-1]=='\n')
		line[--read_len] = 0; /* remove \n */
        string_list_push_back(&s.cache, line, read_len);
        memset(line, 0, line_len);
    }
    if(ferror(fp)) {
	    log_err("error reading %s: %s", full_path, strerror(errno));
    }
    free(line);
    fclose(fp);
    return s;
}

int store_commit(const struct store *self) {
    // Open the tmp file
    FILE *fp = fopen(self->path_tmp, "w");
    if (fp == NULL) {
	log_err("cannot open %s for write: %s", self->path_tmp, strerror(errno));
        return -1;
    }
    // Write its content
    FOR_EACH_STRING_IN_LIST(iter, &self->cache) {
        fprintf(fp, "%s\n", iter->string);
    }
    // Close it
    fclose(fp);
    return rename(self->path_tmp, self->path);
}

void store_destroy(struct store *self) {
    string_list_clear(&self->cache);
}

void store_remove(struct store *self, char *string, size_t len) {
    string_list_remove(&self->cache, string, len);
}

void store_add(struct store *self, char *string, size_t len) {
    if (!string_list_contains(&self->cache, string, len)) {
        string_list_push_back(&self->cache, string, len);
    }
}

int store_contains(struct store *self, char *string, size_t len) {
    return string_list_contains(&self->cache, string, len);
}