couchstore_error_t tree_file_open(tree_file* file, const char *filename, int openflags, const couch_file_ops *ops) { couchstore_error_t errcode = COUCHSTORE_SUCCESS; /* Sanity check input parameters */ if (filename == NULL || file == NULL || ops == NULL || ops->version != 4 || ops->constructor == NULL || ops->open == NULL || ops->close == NULL || ops->pread == NULL || ops->pwrite == NULL || ops->goto_eof == NULL || ops->sync == NULL || ops->destructor == NULL) { return COUCHSTORE_ERROR_INVALID_ARGUMENTS; } memset(file, 0, sizeof(*file)); file->path = strdup(filename); error_unless(file->path, COUCHSTORE_ERROR_ALLOC_FAIL); file->ops = couch_get_buffered_file_ops(ops, &file->handle); error_unless(file->ops, COUCHSTORE_ERROR_ALLOC_FAIL); error_pass(file->ops->open(&file->handle, filename, openflags)); cleanup: return errcode; }
LIBCOUCHSTORE_API couchstore_error_t couchstore_open_db_ex(const char *filename, couchstore_open_flags flags, const couch_file_ops *ops, Db **pDb) { couchstore_error_t errcode = COUCHSTORE_SUCCESS; Db *db; int openflags; /* Sanity check input parameters */ if (filename == NULL || pDb == NULL || ops == NULL || ops->version != 3 || ops->constructor == NULL || ops->open == NULL || ops->close == NULL || ops->pread == NULL || ops->pwrite == NULL || ops->goto_eof == NULL || ops->sync == NULL || ops->destructor == NULL || ((flags & COUCHSTORE_OPEN_FLAG_RDONLY) && (flags & COUCHSTORE_OPEN_FLAG_CREATE))) { return COUCHSTORE_ERROR_INVALID_ARGUMENTS; } if ((db = calloc(1, sizeof(Db))) == NULL) { return COUCHSTORE_ERROR_ALLOC_FAIL; } if (flags & COUCHSTORE_OPEN_FLAG_RDONLY) { openflags = O_RDONLY; } else { openflags = O_RDWR; } if (flags & COUCHSTORE_OPEN_FLAG_CREATE) { openflags |= O_CREAT; } db->filename = strdup(filename); error_unless(db->filename, COUCHSTORE_ERROR_ALLOC_FAIL); db->file_ops = couch_get_buffered_file_ops(ops, &db->file_handle); error_unless(db->file_ops, COUCHSTORE_ERROR_ALLOC_FAIL); error_pass(db->file_ops->open(&db->file_handle, filename, openflags)); if ((db->file_pos = db->file_ops->goto_eof(db->file_handle)) == 0) { /* This is an empty file. Create a new fileheader unless the * user wanted a read-only version of the file */ if (flags & COUCHSTORE_OPEN_FLAG_RDONLY) { error_pass(COUCHSTORE_ERROR_NO_HEADER); } else { error_pass(create_header(db)); } } else { error_pass(find_header(db)); } *pDb = db; return COUCHSTORE_SUCCESS; cleanup: couchstore_close_db(db); return errcode; }