diff options
author | Ben Pfaff <blp@ovn.org> | 2017-12-05 09:30:26 -0800 |
---|---|---|
committer | Ben Pfaff <blp@ovn.org> | 2017-12-24 11:47:33 -0800 |
commit | 1e0b7e94ecf35e06037b00099fa2f23e13954b99 (patch) | |
tree | 92dec31f7234a1c25e61d12c79f6179ad747098c | |
parent | c7007aa7150b15b610ec0c8c737c2267788fa0ad (diff) | |
download | openvswitch-1e0b7e94ecf35e06037b00099fa2f23e13954b99.tar.gz |
log: Add new open mode OVSDB_LOG_CREATE_EXCL.
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>
-rw-r--r-- | ovsdb/file.c | 2 | ||||
-rw-r--r-- | ovsdb/log.c | 23 | ||||
-rw-r--r-- | ovsdb/log.h | 3 | ||||
-rw-r--r-- | ovsdb/ovsdb-tool.c | 2 | ||||
-rw-r--r-- | tests/ovsdb-log.at | 7 | ||||
-rw-r--r-- | tests/test-ovsdb.c | 2 |
6 files changed, 30 insertions, 9 deletions
diff --git a/ovsdb/file.c b/ovsdb/file.c index 1c5573c32..983759e35 100644 --- a/ovsdb/file.c +++ b/ovsdb/file.c @@ -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; } diff --git a/ovsdb/log.c b/ovsdb/log.c index edce44310..b7f822274 100644 --- a/ovsdb/log.c +++ b/ovsdb/log.c @@ -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)) { @@ -115,7 +120,13 @@ 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 @@ -123,7 +134,9 @@ ovsdb_log_open(const char *name, const char *magic, #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; } diff --git a/ovsdb/log.h b/ovsdb/log.h index 8bba3f1b7..a6cba58e1 100644 --- a/ovsdb/log.h +++ b/ovsdb/log.h @@ -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" diff --git a/ovsdb/ovsdb-tool.c b/ovsdb/ovsdb-tool.c index 157ebabc7..4343e3ce5 100644 --- a/ovsdb/ovsdb-tool.c +++ b/ovsdb/ovsdb-tool.c @@ -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); diff --git a/tests/ovsdb-log.at b/tests/ovsdb-log.at index 29c0c5913..826e334ef 100644 --- a/tests/ovsdb-log.at +++ b/tests/ovsdb-log.at @@ -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 diff --git a/tests/test-ovsdb.c b/tests/test-ovsdb.c index d2ac6a637..1b908ae69 100644 --- a/tests/test-ovsdb.c +++ b/tests/test-ovsdb.c @@ -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); } |