view src/common/attr.c @ 884:6552b75f3cc3

common: replace blksize static assert with a runtime one Different systems use different sizes for the st_blksize struct stat member and therefore the static assertion will fail on some of them. One such OS is Linux. There, st_blksize is defined as anything between an int and an unsigned long and therefore it is very sensitive to which exact architecture we're on. With all that said, we should return a negated errno instead of asserting. Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
author Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
date Sun, 18 Dec 2022 11:40:46 -0500
parents 4d7b0736ce83
children
line wrap: on
line source

/*
 * Copyright (c) 2015 Joshua Kahn <josh@joshuak.net>
 * Copyright (c) 2016-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 <jeffpc/error.h>

#include <nomad/types.h>
#include <nomad/config.h>

#define MAP_OR(m, i, o)		case i: m |= o; break

mode_t nmode_to_mode(const uint16_t nmode)
{
	mode_t mode;

	/* copy permission bits */
	mode = nmode & NATTR_AMASK;

	switch (nmode & NATTR_TMASK) {
		case _NATTR_NOTYPE:
			panic("unable to convert a no-type mode");
		MAP_OR(mode, NATTR_REG, S_IFREG);
		MAP_OR(mode, NATTR_DIR, S_IFDIR);
		MAP_OR(mode, NATTR_FIFO, S_IFIFO);
		MAP_OR(mode, NATTR_CHR, S_IFCHR);
		MAP_OR(mode, NATTR_BLK, S_IFBLK);
		MAP_OR(mode, NATTR_LNK, S_IFLNK);
		MAP_OR(mode, NATTR_SOCK, S_IFSOCK);
#ifdef HAVE_DOORS
		MAP_OR(mode, NATTR_DOOR, S_IFDOOR);
#else
		case NATTR_DOOR:
			panic("unable to map NATTR_DOOR");
#endif
		MAP_OR(mode, NATTR_GRAFT, S_IFDIR);
		default:
			panic("unknown nomad mode type: %o", nmode);
	}

	return mode;
}

uint16_t mode_to_nmode(const mode_t mode)
{
	uint16_t nmode;

	/* copy permission bits */
	nmode = mode & 0777;

	switch (mode & S_IFMT) {
		MAP_OR(nmode, 0, _NATTR_NOTYPE);
		MAP_OR(nmode, S_IFREG, NATTR_REG);
		MAP_OR(nmode, S_IFDIR, NATTR_DIR);
		MAP_OR(nmode, S_IFIFO, NATTR_FIFO);
		MAP_OR(nmode, S_IFCHR, NATTR_CHR);
		MAP_OR(nmode, S_IFBLK, NATTR_BLK);
		MAP_OR(nmode, S_IFLNK, NATTR_LNK);
		MAP_OR(nmode, S_IFSOCK, NATTR_SOCK);
#ifdef HAVE_DOORS
		MAP_OR(nmode, S_IFDOOR, NATTR_DOOR);
#endif
		default:
			panic("unknown system mode type: %o", mode);
	}

	return nmode;
}

static inline void cvt_ntime(struct timespec *s, const uint64_t t)
{
	s->tv_sec = t / 1000000000;
	s->tv_nsec = t % 1000000000;
}

void nattr_to_stat(const struct nattr *nattr, struct stat *stat)
{
	stat->st_dev = 0;
	stat->st_rdev = 0;
	stat->st_ino = nattr->ino;
	stat->st_mode = nmode_to_mode(nattr->mode);
	stat->st_nlink = nattr->nlink;
#ifdef HAVE_STAT_FLAGS
	stat->st_flags = 0; /* chflags(2) */
#endif
	stat->st_uid = nattr->owner;
	stat->st_gid = nattr->group;
	stat->st_size = nattr->size;
	cvt_ntime(&stat->st_atim, nattr->atime);
#ifdef HAVE_STAT_BIRTHTIM
	cvt_ntime(&stat->st_birthtim, nattr->btime);
#endif
	cvt_ntime(&stat->st_ctim, nattr->ctime);
	cvt_ntime(&stat->st_mtim, nattr->mtime);
	stat->st_blksize = nattr->blksize;
	stat->st_blocks = nattr->blocks;

	/*
	 * Sanity check that the blksize value didn't get truncated.  The
	 * sizeof comparing condition completely elides the check if the
	 * members' sizes are the same.
	 *
	 * FIXME: return a negated erron instead of asserting
	 */
	if (sizeof(stat->st_blksize) != sizeof(nattr->blksize))
		VERIFY3U(stat->st_blksize, ==, nattr->blksize);

	STATIC_ASSERT(sizeof(stat->st_blocks) == sizeof(nattr->blocks));

	/*
	 * TODO: Illumos has a stat->st_fstype, should we zero it?  Linux
	 * does not have this field.
	 */
}

static inline uint64_t cvt_time(const struct timespec *s)
{
	return ((uint64_t) s->tv_sec) * 1000000000ull + s->tv_nsec;
}

void stat_to_nattr(const struct stat *stat, struct nattr *nattr)
{
	nattr->_reserved = 0;
	nattr->mode = mode_to_nmode(stat->st_mode);
	nattr->nlink = stat->st_nlink;
	nattr->size = stat->st_size;
	nattr->atime = cvt_time(&stat->st_atim);
#ifdef HAVE_STAT_BIRTHTIM
	nattr->btime = cvt_time(&stat->st_birthtim);
#else
	nattr->btime = 0;
#endif
	nattr->ctime = cvt_time(&stat->st_ctim);
	nattr->mtime = cvt_time(&stat->st_mtim);
	nattr->ino = stat->st_ino;
	nattr->owner = stat->st_uid;
	nattr->group = stat->st_gid;
	nattr->blksize = stat->st_blksize;
	nattr->blocks = stat->st_blocks;

	/*
	 * Sanity check that the blksize value didn't get truncated.  The
	 * sizeof comparing condition completely elides the check if the
	 * members' sizes are the same.
	 *
	 * FIXME: return a negated erron instead of asserting
	 */
	if (sizeof(stat->st_blksize) != sizeof(nattr->blksize))
		VERIFY3U(stat->st_blksize, ==, nattr->blksize);

	STATIC_ASSERT(sizeof(stat->st_blocks) == sizeof(nattr->blocks));
}

bool_t xdr_nattr(XDR *xdrs, struct nattr *attr)
{
	if (!xdr_uint16_t(xdrs, &attr->mode))
		return FALSE;
	if (!xdr_uint32_t(xdrs, &attr->nlink))
		return FALSE;
	if (!xdr_uint64_t(xdrs, &attr->size))
		return FALSE;
	if (!xdr_uint64_t(xdrs, &attr->atime))
		return FALSE;
	if (!xdr_uint64_t(xdrs, &attr->btime))
		return FALSE;
	if (!xdr_uint64_t(xdrs, &attr->ctime))
		return FALSE;
	if (!xdr_uint64_t(xdrs, &attr->mtime))
		return FALSE;
	if (!xdr_uint64_t(xdrs, &attr->ino))
		return FALSE;
	if (!xdr_uint32_t(xdrs, &attr->owner))
		return FALSE;
	if (!xdr_uint32_t(xdrs, &attr->group))
		return FALSE;
	if (!xdr_uint32_t(xdrs, &attr->blksize))
		return FALSE;
	if (!xdr_uint64_t(xdrs, &attr->blocks))
		return FALSE;
	return TRUE;
}