1 /* Copyright (c) 2009, 2010, 2011 Nicira Networks
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
31 #include "ovsdb-error.h"
33 #include "socket-util.h"
34 #include "transaction.h"
38 VLOG_DEFINE_THIS_MODULE(ovsdb_log);
49 struct lockfile *lockfile;
51 struct ovsdb_error *read_error;
52 struct ovsdb_error *write_error;
53 enum ovsdb_log_mode mode;
56 /* Attempts to open 'name' with the specified 'open_mode'. On success, stores
57 * the new log into '*filep' and returns NULL; otherwise returns NULL and
58 * stores NULL into '*filep'.
60 * Whether the file will be locked using lockfile_lock() depends on 'locking':
61 * use true to lock it, false not to lock it, or -1 to lock it only if
62 * 'open_mode' is a mode that allows writing.
65 ovsdb_log_open(const char *name, enum ovsdb_log_open_mode open_mode,
66 int locking, struct ovsdb_log **filep)
68 struct lockfile *lockfile;
69 struct ovsdb_error *error;
70 struct ovsdb_log *file;
78 assert(locking == -1 || locking == false || locking == true);
80 locking = open_mode != OVSDB_LOG_READ_ONLY;
83 int retval = lockfile_lock(name, 0, &lockfile);
85 error = ovsdb_io_error(retval, "%s: failed to lock lockfile",
93 if (open_mode == OVSDB_LOG_READ_ONLY) {
95 } else if (open_mode == OVSDB_LOG_READ_WRITE) {
97 } else if (open_mode == OVSDB_LOG_CREATE) {
98 flags = O_RDWR | O_CREAT | O_EXCL;
102 fd = open(name, flags, 0666);
104 const char *op = open_mode == OVSDB_LOG_CREATE ? "create" : "open";
105 error = ovsdb_io_error(errno, "%s: %s failed", op, name);
109 if (!fstat(fd, &s) && s.st_size == 0) {
110 /* It's (probably) a new file so fsync() its parent directory to ensure
111 * that its directory entry is committed to disk. */
112 fsync_parent_dir(name);
115 stream = fdopen(fd, open_mode == OVSDB_LOG_READ_ONLY ? "rb" : "w+b");
117 error = ovsdb_io_error(errno, "%s: fdopen failed", name);
121 file = xmalloc(sizeof *file);
122 file->name = xstrdup(name);
123 file->lockfile = lockfile;
124 file->stream = stream;
125 file->prev_offset = 0;
127 file->read_error = NULL;
128 file->write_error = NULL;
129 file->mode = OVSDB_LOG_READ;
136 lockfile_unlock(lockfile);
142 ovsdb_log_close(struct ovsdb_log *file)
146 fclose(file->stream);
147 lockfile_unlock(file->lockfile);
148 ovsdb_error_destroy(file->read_error);
149 ovsdb_error_destroy(file->write_error);
154 static const char magic[] = "OVSDB JSON ";
157 parse_header(char *header, unsigned long int *length,
158 uint8_t sha1[SHA1_DIGEST_SIZE])
162 /* 'header' must consist of a magic string... */
163 if (strncmp(header, magic, strlen(magic))) {
167 /* ...followed by a length in bytes... */
168 *length = strtoul(header + strlen(magic), &p, 10);
169 if (!*length || *length == ULONG_MAX || *p != ' ') {
174 /* ...followed by a SHA-1 hash... */
175 if (!sha1_from_hex(sha1, p)) {
178 p += SHA1_HEX_DIGEST_LEN;
180 /* ...and ended by a new-line. */
188 struct ovsdb_log_read_cbdata {
190 struct ovsdb_log *file;
192 unsigned long length;
195 static struct ovsdb_error *
196 parse_body(struct ovsdb_log *file, off_t offset, unsigned long int length,
197 uint8_t sha1[SHA1_DIGEST_SIZE], struct json **jsonp)
199 struct json_parser *parser;
203 parser = json_parser_create(JSPF_TRAILER);
209 chunk = MIN(length, sizeof input);
210 if (fread(input, 1, chunk, file->stream) != chunk) {
211 json_parser_abort(parser);
212 return ovsdb_io_error(ferror(file->stream) ? errno : EOF,
213 "%s: error reading %lu bytes "
214 "starting at offset %lld", file->name,
215 length, (long long int) offset);
217 sha1_update(&ctx, input, chunk);
218 json_parser_feed(parser, input, chunk);
222 sha1_final(&ctx, sha1);
223 *jsonp = json_parser_finish(parser);
228 ovsdb_log_read(struct ovsdb_log *file, struct json **jsonp)
230 uint8_t expected_sha1[SHA1_DIGEST_SIZE];
231 uint8_t actual_sha1[SHA1_DIGEST_SIZE];
232 struct ovsdb_error *error;
234 unsigned long data_length;
238 *jsonp = json = NULL;
240 if (file->read_error) {
241 return ovsdb_error_clone(file->read_error);
242 } else if (file->mode == OVSDB_LOG_WRITE) {
243 return OVSDB_BUG("reading file in write mode");
246 if (!fgets(header, sizeof header, file->stream)) {
247 if (feof(file->stream)) {
250 error = ovsdb_io_error(errno, "%s: read failed", file->name);
255 if (!parse_header(header, &data_length, expected_sha1)) {
256 error = ovsdb_syntax_error(NULL, NULL, "%s: parse error at offset "
257 "%lld in header line \"%.*s\"",
258 file->name, (long long int) file->offset,
259 (int) strcspn(header, "\n"), header);
263 data_offset = file->offset + strlen(header);
264 error = parse_body(file, data_offset, data_length, actual_sha1, &json);
269 if (memcmp(expected_sha1, actual_sha1, SHA1_DIGEST_SIZE)) {
270 error = ovsdb_syntax_error(NULL, NULL, "%s: %lu bytes starting at "
271 "offset %lld have SHA-1 hash "SHA1_FMT" "
272 "but should have hash "SHA1_FMT,
273 file->name, data_length,
274 (long long int) data_offset,
275 SHA1_ARGS(actual_sha1),
276 SHA1_ARGS(expected_sha1));
280 if (json->type == JSON_STRING) {
281 error = ovsdb_syntax_error(NULL, NULL, "%s: %lu bytes starting at "
282 "offset %lld are not valid JSON (%s)",
283 file->name, data_length,
284 (long long int) data_offset,
289 file->prev_offset = file->offset;
290 file->offset = data_offset + data_length;
295 file->read_error = ovsdb_error_clone(error);
300 /* Causes the log record read by the previous call to ovsdb_log_read() to be
301 * effectively discarded. The next call to ovsdb_log_write() will overwrite
302 * that previously read record.
304 * Calling this function more than once has no additional effect.
306 * This function is useful when ovsdb_log_read() successfully reads a record
307 * but that record does not make sense at a higher level (e.g. it specifies an
308 * invalid transaction). */
310 ovsdb_log_unread(struct ovsdb_log *file)
312 assert(file->mode == OVSDB_LOG_READ);
313 file->offset = file->prev_offset;
317 ovsdb_log_write(struct ovsdb_log *file, struct json *json)
319 uint8_t sha1[SHA1_DIGEST_SIZE];
320 struct ovsdb_error *error;
327 if (file->write_error) {
328 return ovsdb_error_clone(file->write_error);
329 } else if (file->mode == OVSDB_LOG_READ) {
330 file->mode = OVSDB_LOG_WRITE;
331 if (fseeko(file->stream, file->offset, SEEK_SET)) {
332 error = ovsdb_io_error(errno, "%s: cannot seek to offset %lld",
333 file->name, (long long int) file->offset);
336 if (ftruncate(fileno(file->stream), file->offset)) {
337 error = ovsdb_io_error(errno, "%s: cannot truncate to length %lld",
338 file->name, (long long int) file->offset);
343 if (json->type != JSON_OBJECT && json->type != JSON_ARRAY) {
344 error = OVSDB_BUG("bad JSON type");
348 /* Compose content. Add a new-line (replacing the null terminator) to make
349 * the file easier to read, even though it has no semantic value. */
350 json_string = json_to_string(json, 0);
351 length = strlen(json_string) + 1;
352 json_string[length - 1] = '\n';
354 /* Compose header. */
355 sha1_bytes(json_string, length, sha1);
356 snprintf(header, sizeof header, "%s%zu "SHA1_FMT"\n",
357 magic, length, SHA1_ARGS(sha1));
360 if (fwrite(header, strlen(header), 1, file->stream) != 1
361 || fwrite(json_string, length, 1, file->stream) != 1
362 || fflush(file->stream))
364 error = ovsdb_io_error(errno, "%s: write failed", file->name);
366 /* Remove any partially written data, ignoring errors since there is
367 * nothing further we can do. */
368 ignore(ftruncate(fileno(file->stream), file->offset));
373 file->offset += strlen(header) + length;
378 file->write_error = ovsdb_error_clone(error);
384 ovsdb_log_commit(struct ovsdb_log *file)
386 if (fsync(fileno(file->stream))) {
387 return ovsdb_io_error(errno, "%s: fsync failed", file->name);
392 /* Returns the current offset into the file backing 'log', in bytes. This
393 * reflects the number of bytes that have been read or written in the file. If
394 * the whole file has been read, this is the file size. */
396 ovsdb_log_get_offset(const struct ovsdb_log *log)