view src/objstore/objstore.c @ 103:b397469f53d2

objstore: rename "struct objstore" to "struct objstore_vol" The current structure keeps track of exactly one volume yet it uses the most generic and high-level name. In preparation of making volume groups (that will contain zero or more volumes) let's rename the volume's structure to something that more accurately reflects its purpose. Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net> Signed-off-by: Steve Dougherty <steve@asksteved.com>
author Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
date Sat, 17 Oct 2015 15:27:29 -0400
parents e76e3ee73fb5
children 40b4277fb1e1
line wrap: on
line source

/*
 * Copyright (c) 2015 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <dlfcn.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <nomad/error.h>
#include <nomad/objstore.h>
#include <nomad/objstore_impl.h>

struct backend {
	const struct objstore_vol_def *def;
	void *module;
};

/*
 * TODO: eventually turn this into something that can support multiple
 * backends.
 */
static struct backend mem_backend;
static struct backend *backend;

static int load_backend(struct backend *backend, const char *name)
{
	char path[FILENAME_MAX];

	snprintf(path, sizeof(path), "libnomad_objstore_%s.so", name);

	backend->module = dlopen(path, RTLD_NOW | RTLD_LOCAL);
	if (!backend->module)
		return ENOENT;

	backend->def = dlsym(backend->module, "objvol");
	if (!backend->def) {
		dlclose(backend->module);
		return ENOENT;
	}

	return 0;
}

int objstore_init(void)
{
	int ret;

	ret = load_backend(&mem_backend, "mem");
	if (ret)
		return ret;

	backend = &mem_backend;

	return 0;
}

struct objstore_vol *objstore_vol_create(const char *path, enum objstore_mode mode)
{
	struct objstore_vol *s;
	int ret;

	if (!backend->def->vol_ops->create)
		return ERR_PTR(ENOTSUP);

	s = malloc(sizeof(struct objstore_vol));
	if (!s)
		return ERR_PTR(ENOMEM);

	s->def = backend->def;
	s->mode = mode;
	s->path = strdup(path);
	if (!s->path) {
		ret = ENOMEM;
		goto err;
	}

	ret = s->def->vol_ops->create(s);
	if (ret)
		goto err_path;

	return s;

err_path:
	free((char *) s->path);

err:
	free(s);

	return ERR_PTR(ret);
}

struct objstore_vol *objstore_vol_load(struct nuuid *uuid, const char *path)
{
	return ERR_PTR(ENOTSUP);
}

int objstore_getroot(struct objstore_vol *store, struct nobjhndl *hndl)
{
	if (!hndl)
		return EINVAL;

	if (!store || !store->def->vol_ops || !store->def->vol_ops->getroot)
		return EINVAL;

	return store->def->vol_ops->getroot(store, hndl);
}

int objstore_obj_getattr(struct objstore_vol *store, const struct nobjhndl *hndl,
			 struct nattr *attr)
{
	if (!hndl || !hndl->clock)
		return EINVAL;

	if (!store || !store->def->obj_ops || !store->def->obj_ops->getattr)
		return EINVAL;

	return store->def->obj_ops->getattr(store, hndl, attr);
}