Path: blob/master/Utilities/cmlibarchive/libarchive/archive_entry_stat.c
5028 views
/*-1* Copyright (c) 2003-2007 Tim Kientzle2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR14* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES15* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.16* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,17* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT18* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,19* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY20* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT21* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF22* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.23*/2425#include "archive_platform.h"2627#ifdef HAVE_SYS_STAT_H28#include <sys/stat.h>29#endif30#ifdef HAVE_STDLIB_H31#include <stdlib.h>32#endif3334#include "archive_entry.h"35#include "archive_entry_private.h"3637const struct stat *38archive_entry_stat(struct archive_entry *entry)39{40int64_t size;41struct stat *st;42if (entry->stat == NULL) {43entry->stat = calloc(1, sizeof(*st));44if (entry->stat == NULL)45return (NULL);46entry->stat_valid = 0;47}4849/*50* If none of the underlying fields have been changed, we51* don't need to regenerate. In theory, we could use a bitmap52* here to flag only those items that have changed, but the53* extra complexity probably isn't worth it. It will be very54* rare for anyone to change just one field then request a new55* stat structure.56*/57if (entry->stat_valid)58return (entry->stat);5960st = entry->stat;61/*62* Use the public interfaces to extract items, so that63* the appropriate conversions get invoked.64*/65st->st_atime = archive_entry_atime(entry);66#if HAVE_STRUCT_STAT_ST_BIRTHTIME67st->st_birthtime = archive_entry_birthtime(entry);68#endif69st->st_ctime = archive_entry_ctime(entry);70st->st_mtime = archive_entry_mtime(entry);71st->st_dev = archive_entry_dev(entry);72st->st_gid = (gid_t)archive_entry_gid(entry);73st->st_uid = (uid_t)archive_entry_uid(entry);74st->st_ino = (ino_t)archive_entry_ino64(entry);75st->st_nlink = archive_entry_nlink(entry);76st->st_rdev = archive_entry_rdev(entry);77size = archive_entry_size(entry);78st->st_size = (off_t)size;79if (st->st_size < 0 || (int64_t)st->st_size != size)80st->st_size = 0;81st->st_mode = archive_entry_mode(entry);8283/*84* On systems that support high-res timestamps, copy that85* information into struct stat.86*/87#if HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC88st->st_atimespec.tv_nsec = archive_entry_atime_nsec(entry);89st->st_ctimespec.tv_nsec = archive_entry_ctime_nsec(entry);90st->st_mtimespec.tv_nsec = archive_entry_mtime_nsec(entry);91#elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC92st->st_atim.tv_nsec = archive_entry_atime_nsec(entry);93st->st_ctim.tv_nsec = archive_entry_ctime_nsec(entry);94st->st_mtim.tv_nsec = archive_entry_mtime_nsec(entry);95#elif HAVE_STRUCT_STAT_ST_MTIME_N96st->st_atime_n = archive_entry_atime_nsec(entry);97st->st_ctime_n = archive_entry_ctime_nsec(entry);98st->st_mtime_n = archive_entry_mtime_nsec(entry);99#elif HAVE_STRUCT_STAT_ST_UMTIME100st->st_uatime = archive_entry_atime_nsec(entry) / 1000;101st->st_uctime = archive_entry_ctime_nsec(entry) / 1000;102st->st_umtime = archive_entry_mtime_nsec(entry) / 1000;103#elif HAVE_STRUCT_STAT_ST_MTIME_USEC104st->st_atime_usec = archive_entry_atime_nsec(entry) / 1000;105st->st_ctime_usec = archive_entry_ctime_nsec(entry) / 1000;106st->st_mtime_usec = archive_entry_mtime_nsec(entry) / 1000;107#endif108#if HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC109st->st_birthtimespec.tv_nsec = archive_entry_birthtime_nsec(entry);110#endif111112/*113* TODO: On Linux, store 32 or 64 here depending on whether114* the cached stat structure is a stat32 or a stat64. This115* will allow us to support both variants interchangeably.116*/117entry->stat_valid = 1;118119return (st);120}121122123