aboutsummaryrefslogtreecommitdiffstats
path: root/src/fbuf.c
blob: 0c88fcb98d863d37b48c31c669720410f09b3600 (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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
 * 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.
 */
/******************************************************************************/
/******************************************************************************/
/* IMPLEMENTATION OF BUFFERED FILES                                           */
/******************************************************************************/
/******************************************************************************/

#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
 * @param sz the expected size
 * @return a size greater than sz
 */
static
uint32_t
get_asz(
	uint32_t sz
) {
	uint32_t r = (sz & 0xfffffc00) + 0x000004cf;
	return r > sz ? r : UINT32_MAX;
}

/**
 * Open in 'fb' the file of 'name'
 * @param fb the fbuf
 * @param name the name of the file to read
 * @return 0 on success
 *         -EFBIG if the file is too big
 *         -errno system error
 */
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;
}

/* see fbuf.h */
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 */
	sz = strlen(name);
	fb->name = malloc(sz + 1);
	if (fb->name == NULL)
		goto error;
	mempcpy(fb->name, name, sz + 1);

	/* open the backup */
	if (backup != NULL)
		fb->backup = strdup(backup);
	else {
		fb->backup = malloc(sz + 2);
		if (fb->backup != NULL) {
			mempcpy(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;

	/* any read data is already saved */
	fb->saved = fb->used;
	return 0;

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

/* see fbuf.h */
void
fbuf_close(
	fbuf_t	*fb
) {
	free(fb->name);
	free(fb->backup);
	free(fb->buffer);
	memset(fb, 0, sizeof *fb);
}

/* see fbuf.h */
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;
	if ((uint32_t)rcs != fb->used)
		goto error; /* TODO: set some errno? */

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

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

/* see fbuf.h */
int
fbuf_ensure_capacity(
	fbuf_t	*fb,
	uint32_t capacity
) {
	uint32_t asz;
	void *buffer;

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

/* see fbuf.h */
int
fbuf_put(
	fbuf_t	*fb,
	const void *buffer,
	uint32_t count,
	uint32_t offset
) {
	int rc;
	uint32_t end;

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

	/* grow as necessary */
	end = offset + count;
	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;
}

/* see fbuf.h */
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);
}

/* see fbuf.h */
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 */
	fprintf(stderr, "identification of file %s failed: %m\n", fb->name);
	return -ENOKEY;
}

/* see fbuf.h */
int
fbuf_open_identify(
	fbuf_t	*fb,
	const char *name,
	const char *backup,
	const char *id,
	uint32_t idlen
) {
	int rc;

	/* open the files */
	rc = fbuf_open(fb, name, backup);
	if (rc == 0) {
		/* check identifier */
		rc = fbuf_identify(fb, id, idlen);
		if (rc < 0)
			fbuf_close(fb); /* close if error */
	}
	return rc;
}

/* see fbuf.h */
int
fbuf_backup(
	fbuf_t	*fb
) {
	unlink(fb->backup);
	return link(fb->name, fb->backup);
}

/* see fbuf.h */
int
fbuf_recover(
	fbuf_t	*fb
) {
	int rc;

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