Skip to content

Commit

Permalink
log: Add new open mode OVSDB_LOG_CREATE_EXCL.
Browse files Browse the repository at this point in the history
Until now, OVSDB_LOG_CREATE implied EXCL, but this commit breaks them
apart.

Signed-off-by: Ben Pfaff <blp@ovn.org>
Acked-by: Justin Pettit <jpettit@ovn.org>
  • Loading branch information
blp committed Dec 24, 2017
1 parent c7007aa commit 1e0b7e9
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 9 deletions.
2 changes: 1 addition & 1 deletion ovsdb/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ ovsdb_file_save_copy__(const char *file_name, int locking,
struct json *json;

error = ovsdb_log_open(file_name, OVSDB_MAGIC,
OVSDB_LOG_CREATE, locking, &log);
OVSDB_LOG_CREATE_EXCL, locking, &log);
if (error) {
return error;
}
Expand Down
23 changes: 18 additions & 5 deletions ovsdb/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,16 @@ ovsdb_log_open(const char *name, const char *magic,
lockfile = NULL;
}

if (open_mode == OVSDB_LOG_READ_ONLY) {
switch (open_mode) {
case OVSDB_LOG_READ_ONLY:
flags = O_RDONLY;
} else if (open_mode == OVSDB_LOG_READ_WRITE) {
break;

case OVSDB_LOG_READ_WRITE:
flags = O_RDWR;
} else if (open_mode == OVSDB_LOG_CREATE) {
break;

case OVSDB_LOG_CREATE_EXCL:
#ifndef _WIN32
if (stat(name, &s) == -1 && errno == ENOENT
&& lstat(name, &s) == 0 && S_ISLNK(s.st_mode)) {
Expand All @@ -115,15 +120,23 @@ ovsdb_log_open(const char *name, const char *magic,
#else
flags = O_RDWR | O_CREAT | O_EXCL;
#endif
} else {
break;

case OVSDB_LOG_CREATE:
flags = O_RDWR | O_CREAT;
break;

default:
OVS_NOT_REACHED();
}
#ifdef _WIN32
flags = flags | O_BINARY;
#endif
fd = open(name, flags, 0666);
if (fd < 0) {
const char *op = open_mode == OVSDB_LOG_CREATE ? "create" : "open";
const char *op = (open_mode == OVSDB_LOG_CREATE_EXCL ? "create"
: open_mode == OVSDB_LOG_CREATE ? "create or open"
: "open");
error = ovsdb_io_error(errno, "%s: %s failed", name, op);
goto error_unlock;
}
Expand Down
3 changes: 2 additions & 1 deletion ovsdb/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ struct ovsdb_log;
enum ovsdb_log_open_mode {
OVSDB_LOG_READ_ONLY, /* Open existing file, read-only. */
OVSDB_LOG_READ_WRITE, /* Open existing file, read/write. */
OVSDB_LOG_CREATE /* Create new file, read/write. */
OVSDB_LOG_CREATE_EXCL, /* Create new file, read/write. */
OVSDB_LOG_CREATE /* Create or open file, read/write. */
};

#define OVSDB_MAGIC "OVSDB JSON"
Expand Down
2 changes: 1 addition & 1 deletion ovsdb/ovsdb-tool.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ do_create(struct ovs_cmdl_context *ctx)

/* Create database file. */
check_ovsdb_error(ovsdb_log_open(db_file_name, OVSDB_MAGIC,
OVSDB_LOG_CREATE, -1, &log));
OVSDB_LOG_CREATE_EXCL, -1, &log));
check_ovsdb_error(ovsdb_log_write(log, json));
check_ovsdb_error(ovsdb_log_commit(log));
ovsdb_log_close(log);
Expand Down
7 changes: 6 additions & 1 deletion tests/ovsdb-log.at
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,14 @@ AT_CHECK(
file: read: {"x":1}
]], [ignore])
AT_CHECK(
[test-ovsdb log-io file create read], [1],
[test-ovsdb log-io file create-excl read], [1],
[], [test-ovsdb: I/O error: file: create failed (File exists)
])
AT_CHECK(
[test-ovsdb log-io file create read], [0],
[file: open successful
file: read: {"x":1}
])
AT_CHECK([test -f .file.~lock~])
AT_CLEANUP

Expand Down
2 changes: 2 additions & 0 deletions tests/test-ovsdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ do_log_io(struct ovs_cmdl_context *ctx)
mode = OVSDB_LOG_READ_WRITE;
} else if (!strcmp(mode_string, "create")) {
mode = OVSDB_LOG_CREATE;
} else if (!strcmp(mode_string, "create-excl")) {
mode = OVSDB_LOG_CREATE_EXCL;
} else {
ovs_fatal(0, "unknown log-io open mode \"%s\"", mode_string);
}
Expand Down

0 comments on commit 1e0b7e9

Please sign in to comment.