Merge from vendor branch BIND:
[dragonfly.git] / contrib / libarchive-2 / libarchive / archive_entry_stat.c
1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD: src/lib/libarchive/archive_entry_stat.c,v 1.1 2007/05/29 01:00:18 kientzle Exp $");
28
29 #ifdef HAVE_SYS_STAT_H
30 #include <sys/stat.h>
31 #endif
32 #ifdef HAVE_STDLIB_H
33 #include <stdlib.h>
34 #endif
35
36 #include "archive_entry.h"
37 #include "archive_entry_private.h"
38
39 const struct stat *
40 archive_entry_stat(struct archive_entry *entry)
41 {
42         struct stat *st;
43         if (entry->stat == NULL) {
44                 entry->stat = malloc(sizeof(*st));
45                 if (entry->stat == NULL)
46                         return (NULL);
47                 entry->stat_valid = 0;
48         }
49
50         /*
51          * If none of the underlying fields have been changed, we
52          * don't need to regenerate.  In theory, we could use a bitmap
53          * here to flag only those items that have changed, but the
54          * extra complexity probably isn't worth it.  It will be very
55          * rare for anyone to change just one field then request a new
56          * stat structure.
57          */
58         if (entry->stat_valid)
59                 return (entry->stat);
60
61         st = entry->stat;
62         /*
63          * Use the public interfaces to extract items, so that
64          * the appropriate conversions get invoked.
65          */
66         st->st_atime = archive_entry_atime(entry);
67         st->st_ctime = archive_entry_ctime(entry);
68         st->st_mtime = archive_entry_mtime(entry);
69         st->st_dev = archive_entry_dev(entry);
70         st->st_gid = archive_entry_gid(entry);
71         st->st_uid = archive_entry_uid(entry);
72         st->st_ino = archive_entry_ino(entry);
73         st->st_nlink = archive_entry_nlink(entry);
74         st->st_rdev = archive_entry_rdev(entry);
75         st->st_size = archive_entry_size(entry);
76         st->st_mode = archive_entry_mode(entry);
77
78         /*
79          * On systems that support high-res timestamps, copy that
80          * information into struct stat.
81          */
82 #if HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
83         st->st_atimespec.tv_nsec = archive_entry_atime_nsec(entry);
84         st->st_ctimespec.tv_nsec = archive_entry_ctime_nsec(entry);
85         st->st_mtimespec.tv_nsec = archive_entry_mtime_nsec(entry);
86 #elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
87         st->st_atim.tv_nsec = archive_entry_atime_nsec(entry);
88         st->st_ctim.tv_nsec = archive_entry_ctime_nsec(entry);
89         st->st_mtim.tv_nsec = archive_entry_mtime_nsec(entry);
90 #endif
91
92         /*
93          * TODO: On Linux, store 32 or 64 here depending on whether
94          * the cached stat structure is a stat32 or a stat64.  This
95          * will allow us to support both variants interchangably.
96          */
97         entry->stat_valid = 1;
98
99         return (st);
100 }