예제 #1
0
static couchstore_error_t open_view_group_file(const char *path,
                                               couchstore_open_flags open_flags,
                                               tree_file *file)
{
    couchstore_error_t ret;
    const couch_file_ops *file_ops = couchstore_get_default_file_ops();
    int flags = 0;

    if ((open_flags & COUCHSTORE_OPEN_FLAG_RDONLY) &&
        (open_flags & COUCHSTORE_OPEN_FLAG_CREATE)) {
        return COUCHSTORE_ERROR_INVALID_ARGUMENTS;
    }

    if (open_flags & COUCHSTORE_OPEN_FLAG_RDONLY) {
        flags |= O_RDONLY;
    } else {
        flags |= O_RDWR;
    }

    if (open_flags & COUCHSTORE_OPEN_FLAG_CREATE) {
        flags |= O_CREAT;
    }

    ret = tree_file_open(file, path, flags, file_ops);

    return ret;
}
예제 #2
0
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 ((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;
    }

    error_pass(tree_file_open(&db->file, filename, openflags, ops));

    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;
}