Blame libarchive/archive_entry_stat.c

Packit Service 1d0348
/*-
Packit Service 1d0348
 * Copyright (c) 2003-2007 Tim Kientzle
Packit Service 1d0348
 * All rights reserved.
Packit Service 1d0348
 *
Packit Service 1d0348
 * Redistribution and use in source and binary forms, with or without
Packit Service 1d0348
 * modification, are permitted provided that the following conditions
Packit Service 1d0348
 * are met:
Packit Service 1d0348
 * 1. Redistributions of source code must retain the above copyright
Packit Service 1d0348
 *    notice, this list of conditions and the following disclaimer.
Packit Service 1d0348
 * 2. Redistributions in binary form must reproduce the above copyright
Packit Service 1d0348
 *    notice, this list of conditions and the following disclaimer in the
Packit Service 1d0348
 *    documentation and/or other materials provided with the distribution.
Packit Service 1d0348
 *
Packit Service 1d0348
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
Packit Service 1d0348
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Packit Service 1d0348
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Packit Service 1d0348
 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
Packit Service 1d0348
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
Packit Service 1d0348
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit Service 1d0348
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit Service 1d0348
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit Service 1d0348
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
Packit Service 1d0348
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit Service 1d0348
 */
Packit Service 1d0348
Packit Service 1d0348
#include "archive_platform.h"
Packit Service 1d0348
__FBSDID("$FreeBSD: head/lib/libarchive/archive_entry_stat.c 201100 2009-12-28 03:05:31Z kientzle $");
Packit Service 1d0348
Packit Service 1d0348
#ifdef HAVE_SYS_STAT_H
Packit Service 1d0348
#include <sys/stat.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
#ifdef HAVE_STDLIB_H
Packit Service 1d0348
#include <stdlib.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
Packit Service 1d0348
#include "archive_entry.h"
Packit Service 1d0348
#include "archive_entry_private.h"
Packit Service 1d0348
Packit Service 1d0348
const struct stat *
Packit Service 1d0348
archive_entry_stat(struct archive_entry *entry)
Packit Service 1d0348
{
Packit Service 1d0348
	struct stat *st;
Packit Service 1d0348
	if (entry->stat == NULL) {
Packit Service 1d0348
		entry->stat = calloc(1, sizeof(*st));
Packit Service 1d0348
		if (entry->stat == NULL)
Packit Service 1d0348
			return (NULL);
Packit Service 1d0348
		entry->stat_valid = 0;
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * If none of the underlying fields have been changed, we
Packit Service 1d0348
	 * don't need to regenerate.  In theory, we could use a bitmap
Packit Service 1d0348
	 * here to flag only those items that have changed, but the
Packit Service 1d0348
	 * extra complexity probably isn't worth it.  It will be very
Packit Service 1d0348
	 * rare for anyone to change just one field then request a new
Packit Service 1d0348
	 * stat structure.
Packit Service 1d0348
	 */
Packit Service 1d0348
	if (entry->stat_valid)
Packit Service 1d0348
		return (entry->stat);
Packit Service 1d0348
Packit Service 1d0348
	st = entry->stat;
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * Use the public interfaces to extract items, so that
Packit Service 1d0348
	 * the appropriate conversions get invoked.
Packit Service 1d0348
	 */
Packit Service 1d0348
	st->st_atime = archive_entry_atime(entry);
Packit Service 1d0348
#if HAVE_STRUCT_STAT_ST_BIRTHTIME
Packit Service 1d0348
	st->st_birthtime = archive_entry_birthtime(entry);
Packit Service 1d0348
#endif
Packit Service 1d0348
	st->st_ctime = archive_entry_ctime(entry);
Packit Service 1d0348
	st->st_mtime = archive_entry_mtime(entry);
Packit Service 1d0348
	st->st_dev = archive_entry_dev(entry);
Packit Service 1d0348
	st->st_gid = (gid_t)archive_entry_gid(entry);
Packit Service 1d0348
	st->st_uid = (uid_t)archive_entry_uid(entry);
Packit Service 1d0348
	st->st_ino = (ino_t)archive_entry_ino64(entry);
Packit Service 1d0348
	st->st_nlink = archive_entry_nlink(entry);
Packit Service 1d0348
	st->st_rdev = archive_entry_rdev(entry);
Packit Service 1d0348
	st->st_size = (off_t)archive_entry_size(entry);
Packit Service 1d0348
	st->st_mode = archive_entry_mode(entry);
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * On systems that support high-res timestamps, copy that
Packit Service 1d0348
	 * information into struct stat.
Packit Service 1d0348
	 */
Packit Service 1d0348
#if HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
Packit Service 1d0348
	st->st_atimespec.tv_nsec = archive_entry_atime_nsec(entry);
Packit Service 1d0348
	st->st_ctimespec.tv_nsec = archive_entry_ctime_nsec(entry);
Packit Service 1d0348
	st->st_mtimespec.tv_nsec = archive_entry_mtime_nsec(entry);
Packit Service 1d0348
#elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
Packit Service 1d0348
	st->st_atim.tv_nsec = archive_entry_atime_nsec(entry);
Packit Service 1d0348
	st->st_ctim.tv_nsec = archive_entry_ctime_nsec(entry);
Packit Service 1d0348
	st->st_mtim.tv_nsec = archive_entry_mtime_nsec(entry);
Packit Service 1d0348
#elif HAVE_STRUCT_STAT_ST_MTIME_N
Packit Service 1d0348
	st->st_atime_n = archive_entry_atime_nsec(entry);
Packit Service 1d0348
	st->st_ctime_n = archive_entry_ctime_nsec(entry);
Packit Service 1d0348
	st->st_mtime_n = archive_entry_mtime_nsec(entry);
Packit Service 1d0348
#elif HAVE_STRUCT_STAT_ST_UMTIME
Packit Service 1d0348
	st->st_uatime = archive_entry_atime_nsec(entry) / 1000;
Packit Service 1d0348
	st->st_uctime = archive_entry_ctime_nsec(entry) / 1000;
Packit Service 1d0348
	st->st_umtime = archive_entry_mtime_nsec(entry) / 1000;
Packit Service 1d0348
#elif HAVE_STRUCT_STAT_ST_MTIME_USEC
Packit Service 1d0348
	st->st_atime_usec = archive_entry_atime_nsec(entry) / 1000;
Packit Service 1d0348
	st->st_ctime_usec = archive_entry_ctime_nsec(entry) / 1000;
Packit Service 1d0348
	st->st_mtime_usec = archive_entry_mtime_nsec(entry) / 1000;
Packit Service 1d0348
#endif
Packit Service 1d0348
#if HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC
Packit Service 1d0348
	st->st_birthtimespec.tv_nsec = archive_entry_birthtime_nsec(entry);
Packit Service 1d0348
#endif
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * TODO: On Linux, store 32 or 64 here depending on whether
Packit Service 1d0348
	 * the cached stat structure is a stat32 or a stat64.  This
Packit Service 1d0348
	 * will allow us to support both variants interchangeably.
Packit Service 1d0348
	 */
Packit Service 1d0348
	entry->stat_valid = 1;
Packit Service 1d0348
Packit Service 1d0348
	return (st);
Packit Service 1d0348
}