-
Notifications
You must be signed in to change notification settings - Fork 2
/
cache.c
129 lines (105 loc) · 2.69 KB
/
cache.c
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* $Id$ */
/*
* Copyright (c) 2004 Nicholas Marriott <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/param.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "logfmon.h"
int
save_cache(void)
{
struct file *file;
FILE *fd;
char path[MAXPATHLEN];
int res;
if (conf.cache_file == NULL || *conf.cache_file == '\0')
return (0);
log_debug("saving cache");
res = xsnprintf(path, sizeof path, "%s.new", conf.cache_file);
if (res < 0) {
log_warnx("bad cache file");
return (1);
}
fd = fopen(path, "w+");
if (fd == NULL) {
log_warn("%s", path);
return (1);
}
TAILQ_FOREACH(file, &conf.files, entry) {
if (fprintf(fd, "%lu %s 0 %lld\n",
(unsigned long) strlen(file->path), file->path,
(long long) file->offset) == -1) {
fclose(fd);
log_warnx("error writing cache");
unlink(path);
return (1);
}
}
fclose(fd);
if (rename(path, conf.cache_file) != 0) {
log_warn("rename");
unlink(path);
return (1);
}
return (0);
}
int
load_cache(void)
{
struct file *file;
FILE *fd;
char path[MAXPATHLEN], fmt[32];
off_t off;
int res;
unsigned int len;
if (conf.cache_file == NULL || *conf.cache_file == '\0')
return (0);
log_debug("loading cache");
fd = fopen(conf.cache_file, "r");
if (fd == NULL) {
log_warn("cache not loaded. %s", conf.cache_file);
return (1);
}
while (!feof(fd)) {
if (fscanf(fd, "%u ", &len) != 1)
break;
if (len >= sizeof path)
goto error;
res = xsnprintf(fmt, sizeof fmt, "%%%uc %%*lld %%lld", len);
if (res < 0)
goto error;
memset(path, 0, sizeof path);
if (fscanf(fd, fmt, path, &off) != 2)
goto error;
path[len] = '\0';
file = find_file_by_path(path);
if (file != NULL) {
file->offset = off;
log_debug("file %s, was %lld now %lld",
path, off, file->offset);
}
}
fclose(fd);
return (0);
error:
log_warnx("cannot load cache; possibly corrupted");
fclose(fd);
return (1);
}