Mercurial > nomad
view src/objstore/obj_dir_create.c @ 862:9a63d933a60e
objstore: expose dirent add/update helpers to the rest of objstore
These are useful anytime we want to create a new directory entry - create,
link, symlink, etc.
Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
author | Josef 'Jeff' Sipek <jeffpc@josefsipek.net> |
---|---|
date | Sat, 17 Dec 2022 17:02:07 -0500 |
parents | 79102a88827c |
children | 77a35abc6a24 |
line wrap: on
line source
/* * Copyright (c) 2015-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_create(struct txn *txn, struct objver *dirver, const char *name, uint32_t owner, uint32_t group, uint16_t mode, struct noid *_child) { const uint64_t now = gettime(); struct nattr attrs = { .mode = mode, .nlink = 1, /* even new dirs start with link count of 1 */ .size = NATTR_ISDIR(mode) ? DIR_BLOCK_SIZE : 0, .atime = now, .btime = now, .ctime = now, .mtime = now, .owner = owner, .group = group, }; uint8_t raw[DIR_BLOCK_SIZE]; struct objver *child; struct ndirent_mem ent; bool update_existing; uint16_t ndirents; uint64_t diroff; int ret; ASSERT(!NATTR_ISGRAFT(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; } /* create the child */ child = obj_create(txn, &attrs, &dirver->obj->oid); if (IS_ERR(child)) return PTR_ERR(child); /* if the child is a directory, write out empty directory contents */ if (NATTR_ISDIR(child->attrs.mode)) { uint8_t raw[DIR_BLOCK_SIZE]; struct buffer contents; ssize_t ret2; buffer_init_static(&contents, raw, sizeof(raw), sizeof(raw), true); ret = obj_make_dir_buffer(noid_get_allocator(&child->obj->oid), noid_get_uniq(&child->obj->oid), noid_get_allocator(&dirver->obj->oid), noid_get_uniq(&dirver->obj->oid), &contents); if (ret) goto err; ret2 = obj_write(txn, child, raw, sizeof(raw), 0); if (ret2 < 0) { ret = ret2; goto err; } if (ret2 != sizeof(raw)) { 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) goto err; } else { ret = dir_update_dirent(txn, dirver, raw, diroff, ndirents, name, mode, &child->obj->oid); if (ret) goto err; } /* * We don't track nlinks on directories (it is always 1), but if we * did we'd increment the parent dir's link count here. */ *_child = child->obj->oid; ret = 0; err: obj_putref(child->obj); return ret; }