1 /* Copyright (c) 2009 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.
29 #include "ovsdb-error.h"
33 #define THIS_MODULE VLM_ovsdb_file
36 enum ovsdb_file_mode {
44 struct lockfile *lockfile;
46 struct ovsdb_error *read_error;
47 struct ovsdb_error *write_error;
48 enum ovsdb_file_mode mode;
52 ovsdb_file_open(const char *name, int flags, struct ovsdb_file **filep)
54 struct lockfile *lockfile;
55 struct ovsdb_error *error;
56 struct ovsdb_file *file;
64 accmode = flags & O_ACCMODE;
65 if (accmode == O_RDWR || accmode == O_WRONLY) {
66 int retval = lockfile_lock(name, 0, &lockfile);
68 error = ovsdb_io_error(retval, "%s: failed to lock lockfile",
76 fd = open(name, flags, 0666);
78 const char *op = flags & O_CREAT && flags & O_EXCL ? "create" : "open";
79 error = ovsdb_io_error(errno, "%s: %s failed", op, name);
83 if (!fstat(fd, &s) && s.st_size == 0) {
84 /* It's (probably) a new file so fsync() its parent directory to ensure
85 * that its directory entry is committed to disk. */
86 char *dir = dir_name(name);
87 int dirfd = open(dir, O_RDONLY);
89 if (fsync(dirfd) && errno != EINVAL) {
90 VLOG_ERR("%s: fsync failed (%s)", dir, strerror(errno));
94 VLOG_ERR("%s: open failed (%s)", dir, strerror(errno));
99 stream = fdopen(fd, (accmode == O_RDONLY ? "rb"
100 : accmode == O_WRONLY ? "wb"
103 error = ovsdb_io_error(errno, "%s: fdopen failed", name);
107 file = xmalloc(sizeof *file);
108 file->name = xstrdup(name);
109 file->lockfile = lockfile;
110 file->stream = stream;
112 file->read_error = NULL;
113 file->write_error = NULL;
114 file->mode = OVSDB_FILE_READ;
121 lockfile_unlock(lockfile);
127 ovsdb_file_close(struct ovsdb_file *file)
131 fclose(file->stream);
132 lockfile_unlock(file->lockfile);
133 ovsdb_error_destroy(file->read_error);
134 ovsdb_error_destroy(file->write_error);
139 static const char magic[] = "OVSDB JSON ";
142 parse_header(char *header, unsigned long int *length,
143 uint8_t sha1[SHA1_DIGEST_SIZE])
147 /* 'header' must consist of a magic string... */
148 if (strncmp(header, magic, strlen(magic))) {
152 /* ...followed by a length in bytes... */
153 *length = strtoul(header + strlen(magic), &p, 10);
154 if (!*length || *length == ULONG_MAX || *p != ' ') {
159 /* ...followed by a SHA-1 hash... */
160 if (!sha1_from_hex(sha1, p)) {
163 p += SHA1_HEX_DIGEST_LEN;
165 /* ...and ended by a new-line. */
173 struct ovsdb_file_read_cbdata {
175 struct ovsdb_file *file;
177 unsigned long length;
180 static struct ovsdb_error *
181 parse_body(struct ovsdb_file *file, off_t offset, unsigned long int length,
182 uint8_t sha1[SHA1_DIGEST_SIZE], struct json **jsonp)
184 unsigned long int bytes_left;
185 struct json_parser *parser;
189 parser = json_parser_create(JSPF_TRAILER);
196 chunk = MIN(length, sizeof input);
197 if (fread(input, 1, chunk, file->stream) != chunk) {
198 json_parser_abort(parser);
199 return ovsdb_io_error(ferror(file->stream) ? errno : EOF,
200 "%s: error reading %lu bytes "
201 "starting at offset %lld", file->name,
202 length, (long long int) offset);
204 sha1_update(&ctx, input, chunk);
205 json_parser_feed(parser, input, chunk);
209 sha1_final(&ctx, sha1);
210 *jsonp = json_parser_finish(parser);
215 ovsdb_file_read(struct ovsdb_file *file, struct json **jsonp)
217 uint8_t expected_sha1[SHA1_DIGEST_SIZE];
218 uint8_t actual_sha1[SHA1_DIGEST_SIZE];
219 struct ovsdb_error *error;
221 unsigned long data_length;
225 *jsonp = json = NULL;
227 if (file->read_error) {
228 return ovsdb_error_clone(file->read_error);
229 } else if (file->mode == OVSDB_FILE_WRITE) {
230 return OVSDB_BUG("reading file in write mode");
233 if (!fgets(header, sizeof header, file->stream)) {
234 if (feof(file->stream)) {
237 error = ovsdb_io_error(errno, "%s: read failed", file->name);
242 if (!parse_header(header, &data_length, expected_sha1)) {
243 error = ovsdb_syntax_error(NULL, NULL, "%s: parse error at offset "
244 "%lld in header line \"%.*s\"",
245 file->name, (long long int) file->offset,
246 (int) strcspn(header, "\n"), header);
250 data_offset = file->offset + strlen(header);
251 error = parse_body(file, data_offset, data_length, actual_sha1, &json);
256 if (memcmp(expected_sha1, actual_sha1, SHA1_DIGEST_SIZE)) {
257 error = ovsdb_syntax_error(NULL, NULL, "%s: %lu bytes starting at "
258 "offset %lld have SHA-1 hash "SHA1_FMT" "
259 "but should have hash "SHA1_FMT,
260 file->name, data_length,
261 (long long int) data_offset,
262 SHA1_ARGS(actual_sha1),
263 SHA1_ARGS(expected_sha1));
267 if (json->type == JSON_STRING) {
268 error = ovsdb_syntax_error(NULL, NULL, "%s: %lu bytes starting at "
269 "offset %lld are not valid JSON (%s)",
270 file->name, data_length,
271 (long long int) data_offset,
276 file->offset = data_offset + data_length;
281 file->read_error = ovsdb_error_clone(error);
287 ovsdb_file_write(struct ovsdb_file *file, struct json *json)
289 uint8_t sha1[SHA1_DIGEST_SIZE];
290 struct ovsdb_error *error;
297 if (file->write_error) {
298 return ovsdb_error_clone(file->write_error);
299 } else if (file->mode == OVSDB_FILE_READ) {
300 file->mode = OVSDB_FILE_WRITE;
301 if (fseeko(file->stream, file->offset, SEEK_SET)) {
302 error = ovsdb_io_error(errno, "%s: cannot seek to offset %lld",
303 file->name, (long long int) file->offset);
306 if (ftruncate(fileno(file->stream), file->offset)) {
307 error = ovsdb_io_error(errno, "%s: cannot truncate to length %lld",
308 file->name, (long long int) file->offset);
313 if (json->type != JSON_OBJECT && json->type != JSON_ARRAY) {
314 error = OVSDB_BUG("bad JSON type");
318 /* Compose content. Add a new-line (replacing the null terminator) to make
319 * the file easier to read, even though it has no semantic value. */
320 json_string = json_to_string(json, 0);
321 length = strlen(json_string) + 1;
322 json_string[length - 1] = '\n';
324 /* Compose header. */
325 sha1_bytes(json_string, length, sha1);
326 snprintf(header, sizeof header, "%s%zu "SHA1_FMT"\n",
327 magic, length, SHA1_ARGS(sha1));
330 if (fwrite(header, strlen(header), 1, file->stream) != 1
331 || fwrite(json_string, length, 1, file->stream) != 1
332 || fflush(file->stream))
334 error = ovsdb_io_error(errno, "%s: write failed", file->name);
336 /* Remove any partially written data, ignoring errors since there is
337 * nothing further we can do. */
338 ftruncate(fileno(file->stream), file->offset);
343 file->offset += strlen(header) + length;
348 file->write_error = ovsdb_error_clone(error);
354 ovsdb_file_commit(struct ovsdb_file *file)
356 if (fsync(fileno(file->stream))) {
357 return ovsdb_io_error(errno, "%s: fsync failed", file->name);