1228753Smm/*-
2228753Smm * Copyright (c) 2003-2007 Tim Kientzle
3228753Smm * All rights reserved.
4228753Smm *
5228753Smm * Redistribution and use in source and binary forms, with or without
6228753Smm * modification, are permitted provided that the following conditions
7228753Smm * are met:
8228753Smm * 1. Redistributions of source code must retain the above copyright
9228753Smm *    notice, this list of conditions and the following disclaimer.
10228753Smm * 2. Redistributions in binary form must reproduce the above copyright
11228753Smm *    notice, this list of conditions and the following disclaimer in the
12228753Smm *    documentation and/or other materials provided with the distribution.
13228753Smm *
14228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24228753Smm */
25228753Smm
26228753Smm#include "archive_platform.h"
27229592Smm__FBSDID("$FreeBSD$");
28228753Smm
29228753Smm#ifdef HAVE_SYS_STAT_H
30228753Smm#include <sys/stat.h>
31228753Smm#endif
32228753Smm#ifdef HAVE_STDLIB_H
33228753Smm#include <stdlib.h>
34228753Smm#endif
35228753Smm
36228753Smm#include "archive_entry.h"
37228753Smm#include "archive_entry_private.h"
38228753Smm
39228753Smmconst struct stat *
40228753Smmarchive_entry_stat(struct archive_entry *entry)
41228753Smm{
42228753Smm	struct stat *st;
43228753Smm	if (entry->stat == NULL) {
44228753Smm		entry->stat = malloc(sizeof(*st));
45228753Smm		if (entry->stat == NULL)
46228753Smm			return (NULL);
47228753Smm		entry->stat_valid = 0;
48228753Smm	}
49228753Smm
50228753Smm	/*
51228753Smm	 * If none of the underlying fields have been changed, we
52228753Smm	 * don't need to regenerate.  In theory, we could use a bitmap
53228753Smm	 * here to flag only those items that have changed, but the
54228753Smm	 * extra complexity probably isn't worth it.  It will be very
55228753Smm	 * rare for anyone to change just one field then request a new
56228753Smm	 * stat structure.
57228753Smm	 */
58228753Smm	if (entry->stat_valid)
59228753Smm		return (entry->stat);
60228753Smm
61228753Smm	st = entry->stat;
62228753Smm	/*
63228753Smm	 * Use the public interfaces to extract items, so that
64228753Smm	 * the appropriate conversions get invoked.
65228753Smm	 */
66228753Smm	st->st_atime = archive_entry_atime(entry);
67228753Smm#if HAVE_STRUCT_STAT_ST_BIRTHTIME
68228753Smm	st->st_birthtime = archive_entry_birthtime(entry);
69228753Smm#endif
70228753Smm	st->st_ctime = archive_entry_ctime(entry);
71228753Smm	st->st_mtime = archive_entry_mtime(entry);
72228753Smm	st->st_dev = archive_entry_dev(entry);
73228753Smm	st->st_gid = archive_entry_gid(entry);
74228753Smm	st->st_uid = archive_entry_uid(entry);
75228753Smm	st->st_ino = archive_entry_ino64(entry);
76228753Smm	st->st_nlink = archive_entry_nlink(entry);
77228753Smm	st->st_rdev = archive_entry_rdev(entry);
78228753Smm	st->st_size = archive_entry_size(entry);
79228753Smm	st->st_mode = archive_entry_mode(entry);
80228753Smm
81228753Smm	/*
82228753Smm	 * On systems that support high-res timestamps, copy that
83228753Smm	 * information into struct stat.
84228753Smm	 */
85228753Smm#if HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
86228753Smm	st->st_atimespec.tv_nsec = archive_entry_atime_nsec(entry);
87228753Smm	st->st_ctimespec.tv_nsec = archive_entry_ctime_nsec(entry);
88228753Smm	st->st_mtimespec.tv_nsec = archive_entry_mtime_nsec(entry);
89228753Smm#elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
90228753Smm	st->st_atim.tv_nsec = archive_entry_atime_nsec(entry);
91228753Smm	st->st_ctim.tv_nsec = archive_entry_ctime_nsec(entry);
92228753Smm	st->st_mtim.tv_nsec = archive_entry_mtime_nsec(entry);
93228753Smm#elif HAVE_STRUCT_STAT_ST_MTIME_N
94228753Smm	st->st_atime_n = archive_entry_atime_nsec(entry);
95228753Smm	st->st_ctime_n = archive_entry_ctime_nsec(entry);
96228753Smm	st->st_mtime_n = archive_entry_mtime_nsec(entry);
97228753Smm#elif HAVE_STRUCT_STAT_ST_UMTIME
98228753Smm	st->st_uatime = archive_entry_atime_nsec(entry) / 1000;
99228753Smm	st->st_uctime = archive_entry_ctime_nsec(entry) / 1000;
100228753Smm	st->st_umtime = archive_entry_mtime_nsec(entry) / 1000;
101228753Smm#elif HAVE_STRUCT_STAT_ST_MTIME_USEC
102228753Smm	st->st_atime_usec = archive_entry_atime_nsec(entry) / 1000;
103228753Smm	st->st_ctime_usec = archive_entry_ctime_nsec(entry) / 1000;
104228753Smm	st->st_mtime_usec = archive_entry_mtime_nsec(entry) / 1000;
105228753Smm#endif
106228753Smm#if HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC
107228753Smm	st->st_birthtimespec.tv_nsec = archive_entry_birthtime_nsec(entry);
108228753Smm#endif
109228753Smm
110228753Smm	/*
111228753Smm	 * TODO: On Linux, store 32 or 64 here depending on whether
112228753Smm	 * the cached stat structure is a stat32 or a stat64.  This
113228753Smm	 * will allow us to support both variants interchangably.
114228753Smm	 */
115228753Smm	entry->stat_valid = 1;
116228753Smm
117228753Smm	return (st);
118228753Smm}
119