1 /* Copyright (c) 2009, 2010 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.
30 #include "ovsdb-error.h"
32 #include "socket-util.h"
33 #include "transaction.h"
36 #define THIS_MODULE VLM_ovsdb_log
47 struct lockfile *lockfile;
49 struct ovsdb_error *read_error;
50 struct ovsdb_error *write_error;
51 enum ovsdb_log_mode mode;
54 /* Attempts to open 'name' with the specified 'open_mode'. On success, stores
55 * the new log into '*filep' and returns NULL; otherwise returns NULL and
56 * stores NULL into '*filep'.
58 * Whether the file will be locked using lockfile_lock() depends on 'locking':
59 * use true to lock it, false not to lock it, or -1 to lock it only if
60 * 'open_mode' is a mode that allows writing.
63 ovsdb_log_open(const char *name, enum ovsdb_log_open_mode open_mode,
64 int locking, struct ovsdb_log **filep)
66 struct lockfile *lockfile;
67 struct ovsdb_error *error;
68 struct ovsdb_log *file;
76 assert(locking == -1 || locking == false || locking == true);
78 locking = open_mode != OVSDB_LOG_READ_ONLY;
81 int retval = lockfile_lock(name, 0, &lockfile);
83 error = ovsdb_io_error(retval, "%s: failed to lock lockfile",
91 if (open_mode == OVSDB_LOG_READ_ONLY) {
93 } else if (open_mode == OVSDB_LOG_READ_WRITE) {
95 } else if (open_mode == OVSDB_LOG_CREATE) {
96 flags = O_RDWR | O_CREAT | O_EXCL;
100 fd = open(name, flags, 0666);
102 const char *op = open_mode == OVSDB_LOG_CREATE ? "create" : "open";
103 error = ovsdb_io_error(errno, "%s: %s failed", op, name);
107 if (!fstat(fd, &s) && s.st_size == 0) {
108 /* It's (probably) a new file so fsync() its parent directory to ensure
109 * that its directory entry is committed to disk. */
110 fsync_parent_dir(name);
113 stream = fdopen(fd, open_mode == OVSDB_LOG_READ_ONLY ? "rb" : "w+b");
115 error = ovsdb_io_error(errno, "%s: fdopen failed", name);
119 file = xmalloc(sizeof *file);
120 file->name = xstrdup(name);
121 file->lockfile = lockfile;
122 file->stream = stream;
124 file->read_error = NULL;
125 file->write_error = NULL;
126 file->mode = OVSDB_LOG_READ;
133 lockfile_unlock(lockfile);
139 ovsdb_log_close(struct ovsdb_log *file)
143 fclose(file->stream);
144 lockfile_unlock(file->lockfile);
145 ovsdb_error_destroy(file->read_error);
146 ovsdb_error_destroy(file->write_error);
151 static const char magic[] = "OVSDB JSON ";
154 parse_header(char *header, unsigned long int *length,
155 uint8_t sha1[SHA1_DIGEST_SIZE])
159 /* 'header' must consist of a magic string... */
160 if (strncmp(header, magic, strlen(magic))) {
164 /* ...followed by a length in bytes... */
165 *length = strtoul(header + strlen(magic), &p, 10);
166 if (!*length || *length == ULONG_MAX || *p != ' ') {
171 /* ...followed by a SHA-1 hash... */
172 if (!sha1_from_hex(sha1, p)) {
175 p += SHA1_HEX_DIGEST_LEN;
177 /* ...and ended by a new-line. */
185 struct ovsdb_log_read_cbdata {
187 struct ovsdb_log *file;
189 unsigned long length;
192 static struct ovsdb_error *
193 parse_body(struct ovsdb_log *file, off_t offset, unsigned long int length,
194 uint8_t sha1[SHA1_DIGEST_SIZE], struct json **jsonp)
196 struct json_parser *parser;
200 parser = json_parser_create(JSPF_TRAILER);
206 chunk = MIN(length, sizeof input);
207 if (fread(input, 1, chunk, file->stream) != chunk) {
208 json_parser_abort(parser);
209 return ovsdb_io_error(ferror(file->stream) ? errno : EOF,
210 "%s: error reading %lu bytes "
211 "starting at offset %lld", file->name,
212 length, (long long int) offset);
214 sha1_update(&ctx, input, chunk);
215 json_parser_feed(parser, input, chunk);
219 sha1_final(&ctx, sha1);
220 *jsonp = json_parser_finish(parser);
225 ovsdb_log_read(struct ovsdb_log *file, struct json **jsonp)
227 uint8_t expected_sha1[SHA1_DIGEST_SIZE];
228 uint8_t actual_sha1[SHA1_DIGEST_SIZE];
229 struct ovsdb_error *error;
231 unsigned long data_length;
235 *jsonp = json = NULL;
237 if (file->read_error) {
238 return ovsdb_error_clone(file->read_error);
239 } else if (file->mode == OVSDB_LOG_WRITE) {
240 return OVSDB_BUG("reading file in write mode");
243 if (!fgets(header, sizeof header, file->stream)) {
244 if (feof(file->stream)) {
247 error = ovsdb_io_error(errno, "%s: read failed", file->name);
252 if (!parse_header(header, &data_length, expected_sha1)) {
253 error = ovsdb_syntax_error(NULL, NULL, "%s: parse error at offset "
254 "%lld in header line \"%.*s\"",
255 file->name, (long long int) file->offset,
256 (int) strcspn(header, "\n"), header);
260 data_offset = file->offset + strlen(header);
261 error = parse_body(file, data_offset, data_length, actual_sha1, &json);
266 if (memcmp(expected_sha1, actual_sha1, SHA1_DIGEST_SIZE)) {
267 error = ovsdb_syntax_error(NULL, NULL, "%s: %lu bytes starting at "
268 "offset %lld have SHA-1 hash "SHA1_FMT" "
269 "but should have hash "SHA1_FMT,
270 file->name, data_length,
271 (long long int) data_offset,
272 SHA1_ARGS(actual_sha1),
273 SHA1_ARGS(expected_sha1));
277 if (json->type == JSON_STRING) {
278 error = ovsdb_syntax_error(NULL, NULL, "%s: %lu bytes starting at "
279 "offset %lld are not valid JSON (%s)",
280 file->name, data_length,
281 (long long int) data_offset,
286 file->offset = data_offset + data_length;
291 file->read_error = ovsdb_error_clone(error);
297 ovsdb_log_write(struct ovsdb_log *file, struct json *json)
299 uint8_t sha1[SHA1_DIGEST_SIZE];
300 struct ovsdb_error *error;
307 if (file->write_error) {
308 return ovsdb_error_clone(file->write_error);
309 } else if (file->mode == OVSDB_LOG_READ) {
310 file->mode = OVSDB_LOG_WRITE;
311 if (fseeko(file->stream, file->offset, SEEK_SET)) {
312 error = ovsdb_io_error(errno, "%s: cannot seek to offset %lld",
313 file->name, (long long int) file->offset);
316 if (ftruncate(fileno(file->stream), file->offset)) {
317 error = ovsdb_io_error(errno, "%s: cannot truncate to length %lld",
318 file->name, (long long int) file->offset);
323 if (json->type != JSON_OBJECT && json->type != JSON_ARRAY) {
324 error = OVSDB_BUG("bad JSON type");
328 /* Compose content. Add a new-line (replacing the null terminator) to make
329 * the file easier to read, even though it has no semantic value. */
330 json_string = json_to_string(json, 0);
331 length = strlen(json_string) + 1;
332 json_string[length - 1] = '\n';
334 /* Compose header. */
335 sha1_bytes(json_string, length, sha1);
336 snprintf(header, sizeof header, "%s%zu "SHA1_FMT"\n",
337 magic, length, SHA1_ARGS(sha1));
340 if (fwrite(header, strlen(header), 1, file->stream) != 1
341 || fwrite(json_string, length, 1, file->stream) != 1
342 || fflush(file->stream))
344 error = ovsdb_io_error(errno, "%s: write failed", file->name);
346 /* Remove any partially written data, ignoring errors since there is
347 * nothing further we can do. */
348 ignore(ftruncate(fileno(file->stream), file->offset));
353 file->offset += strlen(header) + length;
358 file->write_error = ovsdb_error_clone(error);
364 ovsdb_log_commit(struct ovsdb_log *file)
366 if (fsync(fileno(file->stream))) {
367 return ovsdb_io_error(errno, "%s: fsync failed", file->name);
372 /* Returns the current offset into the file backing 'log', in bytes. This
373 * reflects the number of bytes that have been read or written in the file. If
374 * the whole file has been read, this is the file size. */
376 ovsdb_log_get_offset(const struct ovsdb_log *log)