Path: blob/master/Utilities/cmlibarchive/libarchive/archive_entry_stat.c
3153 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{40struct stat *st;41if (entry->stat == NULL) {42entry->stat = calloc(1, sizeof(*st));43if (entry->stat == NULL)44return (NULL);45entry->stat_valid = 0;46}4748/*49* If none of the underlying fields have been changed, we50* don't need to regenerate. In theory, we could use a bitmap51* here to flag only those items that have changed, but the52* extra complexity probably isn't worth it. It will be very53* rare for anyone to change just one field then request a new54* stat structure.55*/56if (entry->stat_valid)57return (entry->stat);5859st = entry->stat;60/*61* Use the public interfaces to extract items, so that62* the appropriate conversions get invoked.63*/64st->st_atime = archive_entry_atime(entry);65#if HAVE_STRUCT_STAT_ST_BIRTHTIME66st->st_birthtime = archive_entry_birthtime(entry);67#endif68st->st_ctime = archive_entry_ctime(entry);69st->st_mtime = archive_entry_mtime(entry);70st->st_dev = archive_entry_dev(entry);71st->st_gid = (gid_t)archive_entry_gid(entry);72st->st_uid = (uid_t)archive_entry_uid(entry);73st->st_ino = (ino_t)archive_entry_ino64(entry);74st->st_nlink = archive_entry_nlink(entry);75st->st_rdev = archive_entry_rdev(entry);76st->st_size = (off_t)archive_entry_size(entry);77st->st_mode = archive_entry_mode(entry);7879/*80* On systems that support high-res timestamps, copy that81* information into struct stat.82*/83#if HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC84st->st_atimespec.tv_nsec = archive_entry_atime_nsec(entry);85st->st_ctimespec.tv_nsec = archive_entry_ctime_nsec(entry);86st->st_mtimespec.tv_nsec = archive_entry_mtime_nsec(entry);87#elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC88st->st_atim.tv_nsec = archive_entry_atime_nsec(entry);89st->st_ctim.tv_nsec = archive_entry_ctime_nsec(entry);90st->st_mtim.tv_nsec = archive_entry_mtime_nsec(entry);91#elif HAVE_STRUCT_STAT_ST_MTIME_N92st->st_atime_n = archive_entry_atime_nsec(entry);93st->st_ctime_n = archive_entry_ctime_nsec(entry);94st->st_mtime_n = archive_entry_mtime_nsec(entry);95#elif HAVE_STRUCT_STAT_ST_UMTIME96st->st_uatime = archive_entry_atime_nsec(entry) / 1000;97st->st_uctime = archive_entry_ctime_nsec(entry) / 1000;98st->st_umtime = archive_entry_mtime_nsec(entry) / 1000;99#elif HAVE_STRUCT_STAT_ST_MTIME_USEC100st->st_atime_usec = archive_entry_atime_nsec(entry) / 1000;101st->st_ctime_usec = archive_entry_ctime_nsec(entry) / 1000;102st->st_mtime_usec = archive_entry_mtime_nsec(entry) / 1000;103#endif104#if HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC105st->st_birthtimespec.tv_nsec = archive_entry_birthtime_nsec(entry);106#endif107108/*109* TODO: On Linux, store 32 or 64 here depending on whether110* the cached stat structure is a stat32 or a stat64. This111* will allow us to support both variants interchangeably.112*/113entry->stat_valid = 1;114115return (st);116}117118119