enum ovsdb_log_mode mode;
};
+/* Attempts to open 'name' with the specified 'open_mode'. On success, stores
+ * the new log into '*filep' and returns NULL; otherwise returns NULL and
+ * stores NULL into '*filep'.
+ *
+ * Whether the file will be locked using lockfile_lock() depends on 'locking':
+ * use true to lock it, false not to lock it, or -1 to lock it only if
+ * 'open_mode' is a mode that allows writing.
+ */
struct ovsdb_error *
-ovsdb_log_open(const char *name, int flags, struct ovsdb_log **filep)
+ovsdb_log_open(const char *name, enum ovsdb_log_open_mode open_mode,
+ int locking, struct ovsdb_log **filep)
{
struct lockfile *lockfile;
struct ovsdb_error *error;
struct ovsdb_log *file;
struct stat s;
FILE *stream;
- int accmode;
+ int flags;
int fd;
*filep = NULL;
- accmode = flags & O_ACCMODE;
- if (accmode == O_RDWR || accmode == O_WRONLY) {
+ assert(locking == -1 || locking == false || locking == true);
+ if (locking < 0) {
+ locking = open_mode != OVSDB_LOG_READ_ONLY;
+ }
+ if (locking) {
int retval = lockfile_lock(name, 0, &lockfile);
if (retval) {
error = ovsdb_io_error(retval, "%s: failed to lock lockfile",
lockfile = NULL;
}
+ if (open_mode == OVSDB_LOG_READ_ONLY) {
+ flags = O_RDONLY;
+ } else if (open_mode == OVSDB_LOG_READ_WRITE) {
+ flags = O_RDWR;
+ } else if (open_mode == OVSDB_LOG_CREATE) {
+ flags = O_RDWR | O_CREAT | O_EXCL;
+ } else {
+ NOT_REACHED();
+ }
fd = open(name, flags, 0666);
if (fd < 0) {
- const char *op = flags & O_CREAT && flags & O_EXCL ? "create" : "open";
+ const char *op = open_mode == OVSDB_LOG_CREATE ? "create" : "open";
error = ovsdb_io_error(errno, "%s: %s failed", op, name);
goto error_unlock;
}
free(dir);
}
- stream = fdopen(fd, (accmode == O_RDONLY ? "rb"
- : accmode == O_WRONLY ? "wb"
- : "w+b"));
+ stream = fdopen(fd, open_mode == OVSDB_LOG_READ_ONLY ? "rb" : "w+b");
if (!stream) {
error = ovsdb_io_error(errno, "%s: fdopen failed", name);
goto error_close;
AT_KEYWORDS([ovsdb log])
AT_CAPTURE_FILE([log])
AT_CHECK(
- [test-ovsdb log-io file 'O_CREAT|O_RDWR'], [0],
+ [test-ovsdb log-io file create], [0],
[file: open successful
], [ignore])
AT_CHECK(
- [test-ovsdb log-io file 'O_RDONLY' read], [0],
+ [test-ovsdb log-io file read-only read], [0],
[file: open successful
file: read: end of log
], [ignore])
AT_KEYWORDS([ovsdb log])
AT_CAPTURE_FILE([file])
AT_CHECK(
- [[test-ovsdb log-io file 'O_CREAT|O_RDWR' 'write:[0]']], [0],
+ [[test-ovsdb log-io file create 'write:[0]']], [0],
[[file: open successful
file: write:[0] successful
]], [ignore])
AT_CHECK(
- [test-ovsdb log-io file 'O_RDONLY' read read], [0],
+ [test-ovsdb log-io file read-only read read], [0],
[[file: open successful
file: read: [0]
file: read: end of log
AT_CHECK([test -f .file.~lock~])
AT_CLEANUP
-AT_SETUP([check that O_EXCL works])
+AT_SETUP([check that create fails if file exists])
AT_KEYWORDS([ovsdb log])
AT_CAPTURE_FILE([file])
AT_CHECK(
- [[test-ovsdb log-io file 'O_CREAT|O_RDWR' 'write:[1]']], [0],
+ [[test-ovsdb log-io file create 'write:[1]']], [0],
[[file: open successful
file: write:[1] successful
]], [ignore])
AT_CHECK(
- [test-ovsdb log-io file 'O_RDONLY' read], [0],
+ [test-ovsdb log-io file read-only read], [0],
[[file: open successful
file: read: [1]
]], [ignore])
AT_CHECK(
- [test-ovsdb -vlockfile:console:emer log-io file 'O_CREAT|O_RDWR|O_EXCL' read], [1],
+ [test-ovsdb -vlockfile:console:emer log-io file create read], [1],
[], [test-ovsdb: I/O error: create: file failed (File exists)
])
AT_CHECK([test -f .file.~lock~])
AT_KEYWORDS([ovsdb log])
AT_CAPTURE_FILE([file])
AT_CHECK(
- [[test-ovsdb log-io file 'O_CREAT|O_RDWR' 'write:[0]' 'write:[1]' 'write:[2]']], [0],
+ [[test-ovsdb log-io file create 'write:[0]' 'write:[1]' 'write:[2]']], [0],
[[file: open successful
file: write:[0] successful
file: write:[1] successful
file: write:[2] successful
]], [ignore])
AT_CHECK(
- [test-ovsdb log-io file 'O_RDONLY' read read read read], [0],
+ [test-ovsdb log-io file read-only read read read read], [0],
[[file: open successful
file: read: [0]
file: read: [1]
AT_KEYWORDS([ovsdb log])
AT_CAPTURE_FILE([file])
AT_CHECK(
- [[test-ovsdb log-io file 'O_CREAT|O_RDWR' 'write:[0]' 'write:[1]' 'write:[2]']], [0],
+ [[test-ovsdb log-io file create 'write:[0]' 'write:[1]' 'write:[2]']], [0],
[[file: open successful
file: write:[0] successful
file: write:[1] successful
file: write:[2] successful
]], [ignore])
AT_CHECK(
- [[test-ovsdb log-io file 'O_RDWR' read read read 'write:["append"]']], [0],
+ [[test-ovsdb log-io file read/write read read read 'write:["append"]']], [0],
[[file: open successful
file: read: [0]
file: read: [1]
file: write:["append"] successful
]], [ignore])
AT_CHECK(
- [test-ovsdb log-io file 'O_RDONLY' read read read read read], [0],
+ [test-ovsdb log-io file read-only read read read read read], [0],
[[file: open successful
file: read: [0]
file: read: [1]
AT_KEYWORDS([ovsdb log])
AT_CAPTURE_FILE([file])
AT_CHECK(
- [[test-ovsdb log-io file 'O_CREAT|O_RDWR' 'write:[0]' 'write:[1]' 'write:[2]']], [0],
+ [[test-ovsdb log-io file create 'write:[0]' 'write:[1]' 'write:[2]']], [0],
[[file: open successful
file: write:[0] successful
file: write:[1] successful
file: write:[2] successful
]], [ignore])
AT_CHECK(
- [[test-ovsdb log-io file 'O_RDWR' read 'write:["more data"]']], [0],
+ [[test-ovsdb log-io file read/write read 'write:["more data"]']], [0],
[[file: open successful
file: read: [0]
file: write:["more data"] successful
]], [ignore])
AT_CHECK(
- [test-ovsdb log-io file 'O_RDONLY' read read read], [0],
+ [test-ovsdb log-io file read-only read read read], [0],
[[file: open successful
file: read: [0]
file: read: ["more data"]
AT_KEYWORDS([ovsdb log])
AT_CAPTURE_FILE([file])
AT_CHECK(
- [[test-ovsdb log-io file 'O_CREAT|O_RDWR' 'write:[0]' 'write:[1]' 'write:[2]']], [0],
+ [[test-ovsdb log-io file create 'write:[0]' 'write:[1]' 'write:[2]']], [0],
[[file: open successful
file: write:[0] successful
file: write:[1] successful
]], [ignore])
AT_CHECK([echo 'xxx' >> file])
AT_CHECK(
- [test-ovsdb log-io file 'O_RDONLY' read read read read], [0],
+ [test-ovsdb log-io file read-only read read read read], [0],
[[file: open successful
file: read: [0]
file: read: [1]
AT_KEYWORDS([ovsdb log])
AT_CAPTURE_FILE([file])
AT_CHECK(
- [[test-ovsdb log-io file 'O_CREAT|O_RDWR' 'write:[0]' 'write:[1]' 'write:[2]']], [0],
+ [[test-ovsdb log-io file create 'write:[0]' 'write:[1]' 'write:[2]']], [0],
[[file: open successful
file: write:[0] successful
file: write:[1] successful
]], [ignore])
AT_CHECK([echo 'xxx' >> file])
AT_CHECK(
- [[test-ovsdb log-io file 'O_RDWR' read read read read 'write:[3]']], [0],
+ [[test-ovsdb log-io file read/write read read read read 'write:[3]']], [0],
[[file: open successful
file: read: [0]
file: read: [1]
file: write:[3] successful
]], [ignore])
AT_CHECK(
- [test-ovsdb log-io file 'O_RDONLY' read read read read read], [0],
+ [test-ovsdb log-io file read-only read read read read read], [0],
[[file: open successful
file: read: [0]
file: read: [1]
AT_KEYWORDS([ovsdb log])
AT_CAPTURE_FILE([file])
AT_CHECK(
- [[test-ovsdb log-io file 'O_CREAT|O_RDWR' 'write:[0]' 'write:[1]' 'write:[2]']], [0],
+ [[test-ovsdb log-io file create 'write:[0]' 'write:[1]' 'write:[2]']], [0],
[[file: open successful
file: write:[0] successful
file: write:[1] successful
AT_CHECK([[grep -c '\[3]' file]], [0], [1
])
AT_CHECK(
- [[test-ovsdb log-io file 'O_RDWR' read read read 'write:["longer data"]']], [0],
+ [[test-ovsdb log-io file read/write read read read 'write:["longer data"]']], [0],
[[file: open successful
file: read: [0]
file: read: [1]
file: write:["longer data"] successful
]], [ignore])
AT_CHECK(
- [test-ovsdb log-io file 'O_RDONLY' read read read read], [0],
+ [test-ovsdb log-io file read-only read read read read], [0],
[[file: open successful
file: read: [0]
file: read: [1]
AT_KEYWORDS([ovsdb log])
AT_CAPTURE_FILE([file])
AT_CHECK(
- [[test-ovsdb log-io file 'O_CREAT|O_RDWR' 'write:[0]' 'write:[1]' 'write:[2]']], [0],
+ [[test-ovsdb log-io file create 'write:[0]' 'write:[1]' 'write:[2]']], [0],
[[file: open successful
file: write:[0] successful
file: write:[1] successful
AT_CHECK([[grep -c '^2$' file]], [0], [1
])
AT_CHECK(
- [[test-ovsdb log-io file 'O_RDWR' read read read 'write:["longer data"]']], [0],
+ [[test-ovsdb log-io file read/write read read read 'write:["longer data"]']], [0],
[[file: open successful
file: read: [0]
file: read: [1]
file: write:["longer data"] successful
]], [ignore])
AT_CHECK(
- [test-ovsdb log-io file 'O_RDONLY' read read read read], [0],
+ [test-ovsdb log-io file read-only read read read read], [0],
[[file: open successful
file: read: [0]
file: read: [1]
AT_KEYWORDS([ovsdb log])
AT_CAPTURE_FILE([file])
AT_CHECK(
- [[test-ovsdb log-io file 'O_CREAT|O_RDWR' 'write:[0]' 'write:[1]' 'write:[2]']], [0],
+ [[test-ovsdb log-io file create 'write:[0]' 'write:[1]' 'write:[2]']], [0],
[[file: open successful
file: write:[0] successful
file: write:[1] successful
]], [ignore])
AT_CHECK([[printf '%s\n%s\n' 'OVSDB JSON 5 d910b02871075d3156ec8675dfc95b7d5d640aa6' 'null' >> file]])
AT_CHECK(
- [[test-ovsdb log-io file 'O_RDWR' read read read read 'write:["replacement data"]']], [0],
+ [[test-ovsdb log-io file read/write read read read read 'write:["replacement data"]']], [0],
[[file: open successful
file: read: [0]
file: read: [1]
file: write:["replacement data"] successful
]], [ignore])
AT_CHECK(
- [test-ovsdb log-io file 'O_RDONLY' read read read read read], [0],
+ [test-ovsdb log-io file read-only read read read read read], [0],
[[file: open successful
file: read: [0]
file: read: [1]
do_log_io(int argc, char *argv[])
{
const char *name = argv[1];
- char *mode = argv[2];
+ char *mode_string = argv[2];
struct ovsdb_error *error;
+ enum ovsdb_log_open_mode mode;
struct ovsdb_log *log;
- char *save_ptr = NULL;
- const char *token;
- int flags;
int i;
- for (flags = 0, token = strtok_r(mode, " |", &save_ptr); token != NULL;
- token = strtok_r(NULL, " |", &save_ptr))
- {
- if (!strcmp(token, "O_RDONLY")) {
- flags |= O_RDONLY;
- } else if (!strcmp(token, "O_RDWR")) {
- flags |= O_RDWR;
- } else if (!strcmp(token, "O_TRUNC")) {
- flags |= O_TRUNC;
- } else if (!strcmp(token, "O_CREAT")) {
- flags |= O_CREAT;
- } else if (!strcmp(token, "O_EXCL")) {
- flags |= O_EXCL;
- } else if (!strcmp(token, "O_TRUNC")) {
- flags |= O_TRUNC;
- }
+ if (!strcmp(mode_string, "read-only")) {
+ mode = OVSDB_LOG_READ_ONLY;
+ } else if (!strcmp(mode_string, "read/write")) {
+ mode = OVSDB_LOG_READ_WRITE;
+ } else if (!strcmp(mode_string, "create")) {
+ mode = OVSDB_LOG_CREATE;
+ } else {
+ ovs_fatal(0, "unknown log-io open mode \"%s\"", mode_string);
}
- check_ovsdb_error(ovsdb_log_open(name, flags, &log));
+ check_ovsdb_error(ovsdb_log_open(name, mode, -1, &log));
printf("%s: open successful\n", name);
for (i = 3; i < argc; i++) {