aboutsummaryrefslogtreecommitdiffstats
path: root/src/fbuf.c
blob: c4243e6edfec1ca37c69d4fed7b5861cdfc3ecd9 (plain)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
 * Copyright (C) 2018 "IoT.bzh"
 * Author José Bollo <jose.bollo@iot.bzh>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <assert.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "fbuf.h"

/** compute the size to allocate for ensuring 'sz' bytes */
static
uint32_t
get_asz(
	uint32_t sz
) {
	return (sz & 0xfffffc00) + 0x000004cf;
}

/** open in 'fb' the file of 'name' */
static
int
read_file(
	fbuf_t	*fb,
	const char *name
) {
	struct stat st;
	int fd, rc;
	uint32_t sz;

	/* open the file */
	fd = open(name, O_RDWR|O_CREAT, 0600);
	if (fd < 0)
		goto error;

	/* get file stat */
	rc = fstat(fd, &st);
	if (rc < 0)
		goto error2;

	/* check file size */
	if ((off_t)INT32_MAX < st.st_size) {
		errno = EFBIG;
		goto error2;
	}
	sz = (uint32_t)st.st_size;

	/* allocate memory */
	rc = fbuf_ensure_capacity(fb, (uint32_t)st.st_size);
	if (rc < 0)
		goto error2;

	/* read the file */
	if (read(fd, fb->buffer, (size_t)sz) != (ssize_t)sz)
		goto error2;

	/* done */
	fb->used = fb->size = sz;
	close(fd);
	return 0;

error2:
	close(fd);
error:
	rc = -errno;
	fprintf(stderr, "can't read file %s: %m\n", name);
	fb->saved = fb->used = fb->size = 0;
	return rc;
}

/** open in 'fb' the file of 'name' */
int
fbuf_open(
	fbuf_t	*fb,
	const char *name,
	const char *backup
) {
	int rc;
	size_t sz;

	/* reset */
	memset(fb, 0, sizeof *fb);

	/* save name */
	fb->name = strdup(name);
	if (fb->name == NULL)
		goto error;

	/* open the backup */
	if (backup != NULL)
		fb->backup = strdup(backup);
	else {
		sz = strlen(name);
		fb->backup = malloc(sz + 2);
		if (fb->backup != NULL) {
			memcpy(fb->backup, name, sz);
			fb->backup[sz] = '~';
			fb->backup[sz + 1] = 0;
		}
	}
	if (fb->backup == NULL)
		goto error;

	/* read the file */
	rc = read_file(fb, fb->name);
	if (rc < 0)
		goto error;

	fb->saved = fb->used;
	return 0;

error:
	rc = -errno;
	fprintf(stderr, "can't open file %s: %m\n", name);
	fbuf_close(fb);
	return rc;
}

/** close the file 'fb' */
void
fbuf_close(
	fbuf_t	*fb
) {
	free(fb->name);
	free(fb->backup);
	free(fb->buffer);
	memset(fb, 0, sizeof *fb);
}

/** write to file 'fb' the unsaved bytes and flush the content to the file */
int
fbuf_sync(
	fbuf_t	*fb
) {
	ssize_t rcs;
	int rc, fd;

	/* is sync needed? */
	if (fb->used == fb->saved && fb->used == fb->size)
		return 0;

	/* open the file */
	unlink(fb->name);
	fd = creat(fb->name, 0600);
	if (fd < 0)
		goto error;

	/* write unsaved bytes */
	rcs = write(fd, fb->buffer, fb->used);
	close(fd);
	if (rcs < 0)
		goto error;

	fb->size = fb->saved = fb->used;
	return 0;

error:
	rc = -errno;
	fprintf(stderr, "sync of file %s failed: %m\n", fb->name);
	return rc;
}

/** allocate enough memory in 'fb' to store 'count' bytes */
int
fbuf_ensure_capacity(
	fbuf_t	*fb,
	uint32_t count
) {
	uint32_t capacity;
	void *buffer;

	if (count > fb->capacity) {
		capacity = get_asz(count);
		buffer = realloc(fb->buffer, capacity);
		if (buffer == NULL) {
			fprintf(stderr, "alloc %u for file %s failed: %m\n", capacity, fb->name);
			return -ENOMEM;
		}
		fb->buffer = buffer;
		fb->capacity = capacity;
	}
	return 0;
}

/** put at 'offset' in the memory of 'fb' the 'count' bytes pointed by 'buffer' */
int
fbuf_put(
	fbuf_t	*fb,
	const void *buffer,
	uint32_t count,
	uint32_t offset
) {
	int rc;
	uint32_t end = offset + count;

	/* don't call me for nothing */
	assert(count);

	/* grow as necessary */
	if (end > fb->used) {
		rc = fbuf_ensure_capacity(fb, end);
		if (rc < 0)
			return rc;
		fb->used = end;
	}

	/* copy the data */
	memcpy(fb->buffer + offset, buffer, count);

	/* write the data to the disk */
	if (offset < fb->saved)
		fb->saved = offset;
	return 0;
}

/** append at end in the memory of 'fb' the 'count' bytes pointed by 'buffer' */
int
fbuf_append(
	fbuf_t	*fb,
	const void *buffer,
	uint32_t count
) {
	/* don't call me for nothing */
	assert(count);

	return fbuf_put(fb, buffer, count, fb->used);
}

/** check or make identification of file 'fb' by 'id' of 'len' */
int
fbuf_identify(
	fbuf_t	*fb,
	const char *id,
	uint32_t idlen
) {
	/* init if empty */
	if (fb->saved == 0 && fb->used == 0)
		return fbuf_append(fb, id, idlen);

	/* check if not empty */
	if (fb->saved >= idlen && !memcmp(fb->buffer, id, idlen))
		return 0;

	/* bad identification */
	errno = ENOKEY;
	fprintf(stderr, "identification of file %s failed: %m\n", fb->name);
	return -ENOKEY;
}

/** check or make identification by 'uuid' of file 'fb' */
int
fbuf_open_identify(
	fbuf_t	*fb,
	const char *name,
	const char *backup,
	const char *id,
	uint32_t idlen
) {
	int rc;

	rc = fbuf_open(fb, name, backup);
	if (rc == 0) {
		rc = fbuf_identify(fb, id, idlen);
		if (rc < 0)
			fbuf_close(fb);
	}
	return rc;
}

/** make a backup */
int
fbuf_backup(
	fbuf_t	*fb
) {
	unlink(fb->backup);
	return link(fb->name, fb->backup);
}

/** recover from latest backup */
int
fbuf_recover(
	fbuf_t	*fb
) {
	int rc;

	rc = read_file(fb, fb->backup);
	fb->saved = 0; /* ensure rewrite of restored data */
	return rc;
}