view src/objstore/obj_link.c @ 895:d9e0b59c30f0

objstore: move objstore_{,sym}link to obj_link.c Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
author Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
date Mon, 19 Dec 2022 20:11:14 -0500
parents 9b1ce98807c0
children
line wrap: on
line source

/*
 * Copyright (c) 2020,2022 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 "dir.h"

#include <jeffpc/time.h>

int dir_symlink(struct txn *txn, struct objver *dirver, const char *name,
		uint32_t owner, uint32_t group, uint16_t mode,
		const char *target, struct noid *_child)
{
	const uint64_t now = gettime();
	struct nattr attrs = {
		.mode = mode,
		.nlink = 1,
		.size = strlen(target),
		.atime = now,
		.btime = now,
		.ctime = now,
		.mtime = now,
		.owner = owner,
		.group = group,
	};
	uint8_t raw[DIR_BLOCK_SIZE];
	struct ndirent_mem ent;
	bool update_existing;
	struct objver *child;
	uint16_t ndirents;
	uint64_t diroff;
	ssize_t ret;

	ASSERT(NATTR_ISLNK(mode));

	ret = dir_lookup_entry(dirver, name, raw, &ent, &diroff, &ndirents);
	if (ret) {
		/* error or didn't find anything */
		if (ret != -ENOENT)
			return ret;

		update_existing = false;
	} else {
		/* found something */
		if (!ent.all_deleted)
			return -EEXIST;

		update_existing = true;
	}

	/* make new oid for the child */
	child = obj_create(txn, &attrs, &dirver->obj->oid);
	if (IS_ERR(child))
		return PTR_ERR(child);

	/* write out symlink target */
	ret = obj_write(txn, child, target, strlen(target), 0);
	if (ret < 0)
		goto err;

	if (ret != strlen(target)) {
		ret = -EIO;
		goto err;
	}

	/* add dirent to parent dir */
	if (!update_existing) {
		ret = dir_add_dirent(txn, dirver, name, mode, &child->obj->oid);
		if (ret)
			return ret;
	} else {
		ret = dir_update_dirent(txn, dirver, raw, diroff, ndirents,
					name, mode, &child->obj->oid);
		if (ret)
			return ret;
	}

	*_child = child->obj->oid;

	ret = 0;

err:
	obj_putref(child->obj);

	return ret;
}

int objstore_symlink(struct objstore_open_obj_info *diropen, const char *name,
		     uint32_t owner, uint32_t group, uint16_t mode,
		     const char *target, struct noid *child)
{
	struct objver *newver;
	struct obj *dir;
	struct txn *txn;
	int ret;

	if (!diropen || !name || !target)
		return -EINVAL;

	/* must have a type */
	if (!NATTR_ISLNK(mode))
		return -EINVAL;

	dir = diropen->obj;

	if (diropen->qualified)
		return -EROFS;

	MXLOCK(&dir->lock);

	if (!NATTR_ISDIR(diropen->ver->attrs.mode)) {
		ret = -ENOTDIR;
		goto err;
	}

	txn = txn_begin(diropen->ver->obj->clone);
	if (IS_ERR(txn)) {
		ret = PTR_ERR(txn);
		goto err;
	}

	newver = obj_cow(txn, diropen);
	if (IS_ERR(newver)) {
		ret = PTR_ERR(newver);
		goto err_txn;
	}

	ret = dir_symlink(txn, newver, name, owner, group, mode, target, child);

err_txn:
	ret = txn_commitabort(txn, ret);
	newver = NULL; /* prevent accidental use */

err:
	MXUNLOCK(&dir->lock);

	return ret;
}

int objstore_link(struct objstore_open_obj_info *diropen, const char *name,
		  struct objstore_open_obj_info *tgtopen)
{
	return -ENOTSUP;
}