+ struct lockfile *src_lock;
+ struct lockfile *dst_lock;
+ bool in_place = dst_name == NULL;
+ struct ovsdb *db;
+ int retval;
+
+ /* Lock the source, if we will be replacing it. */
+ if (in_place) {
+ retval = lockfile_lock(src_name, 0, &src_lock);
+ if (retval) {
+ ovs_fatal(retval, "%s: failed to lock lockfile", src_name);
+ }
+ }
+
+ /* Get (temporary) destination and lock it. */
+ if (in_place) {
+ dst_name = xasprintf("%s.tmp", src_name);
+ }
+ retval = lockfile_lock(dst_name, 0, &dst_lock);
+ if (retval) {
+ ovs_fatal(retval, "%s: failed to lock lockfile", dst_name);
+ }
+
+ /* Save a copy. */
+ check_ovsdb_error(new_schema
+ ? ovsdb_file_open_as_schema(src_name, new_schema, &db)
+ : ovsdb_file_open(src_name, true, &db, NULL));
+ check_ovsdb_error(ovsdb_file_save_copy(dst_name, false, comment, db));
+ ovsdb_destroy(db);
+
+ /* Replace source. */
+ if (in_place) {
+ if (rename(dst_name, src_name)) {
+ ovs_fatal(errno, "failed to rename \"%s\" to \"%s\"",
+ dst_name, src_name);
+ }
+ fsync_parent_dir(dst_name);
+ lockfile_unlock(src_lock);
+ }
+
+ lockfile_unlock(dst_lock);
+}
+
+static void
+do_compact(int argc, char *argv[])
+{
+ const char *db = argc >= 2 ? argv[1] : default_db();
+ const char *target = argc >= 3 ? argv[2] : NULL;
+
+ compact_or_convert(db, target, NULL, "compacted by ovsdb-tool "VERSION);
+}
+
+static void
+do_convert(int argc, char *argv[])
+{
+ const char *db = argc >= 2 ? argv[1] : default_db();
+ const char *schema = argc >= 3 ? argv[2] : default_schema();
+ const char *target = argc >= 4 ? argv[3] : NULL;
+ struct ovsdb_schema *new_schema;
+
+ check_ovsdb_error(ovsdb_schema_from_file(schema, &new_schema));
+ compact_or_convert(db, target, new_schema,
+ "converted by ovsdb-tool "VERSION);
+ ovsdb_schema_destroy(new_schema);
+}
+
+static void
+do_needs_conversion(int argc, char *argv[])
+{
+ const char *db_file_name = argc >= 2 ? argv[1] : default_db();
+ const char *schema_file_name = argc >= 3 ? argv[2] : default_schema();
+ struct ovsdb_schema *schema1, *schema2;
+
+ check_ovsdb_error(ovsdb_file_read_schema(db_file_name, &schema1));
+ check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema2));
+ puts(ovsdb_schema_equal(schema1, schema2) ? "no" : "yes");
+ ovsdb_schema_destroy(schema1);
+ ovsdb_schema_destroy(schema2);
+}
+
+static void
+do_db_version(int argc, char *argv[])
+{
+ const char *db_file_name = argc >= 2 ? argv[1] : default_db();
+ struct ovsdb_schema *schema;
+
+ check_ovsdb_error(ovsdb_file_read_schema(db_file_name, &schema));
+ puts(schema->version);
+ ovsdb_schema_destroy(schema);
+}
+
+static void
+do_db_cksum(int argc OVS_UNUSED, char *argv[])
+{
+ const char *db_file_name = argc >= 2 ? argv[1] : default_db();
+ struct ovsdb_schema *schema;
+
+ check_ovsdb_error(ovsdb_file_read_schema(db_file_name, &schema));
+ puts(schema->cksum);
+ ovsdb_schema_destroy(schema);
+}
+
+static void
+do_schema_version(int argc, char *argv[])
+{
+ const char *schema_file_name = argc >= 2 ? argv[1] : default_schema();
+ struct ovsdb_schema *schema;
+
+ check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema));
+ puts(schema->version);
+ ovsdb_schema_destroy(schema);
+}
+
+static void
+do_schema_cksum(int argc, char *argv[])
+{
+ const char *schema_file_name = argc >= 2 ? argv[1] : default_schema();
+ struct ovsdb_schema *schema;
+
+ check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema));
+ puts(schema->cksum);
+ ovsdb_schema_destroy(schema);
+}
+
+static void
+transact(bool read_only, int argc, char *argv[])
+{
+ const char *db_file_name = argc >= 3 ? argv[1] : default_db();
+ const char *transaction = argv[argc - 1];