]> granicus.if.org Git - zziplib/blob - zzip/stat.c
indentation
[zziplib] / zzip / stat.c
1 /*
2  * Author: 
3  *      Guido Draheim <guidod@gmx.de>
4  *      Tomi Ollila <Tomi.Ollila@iki.fi>
5  *
6  * Copyright (c) 1999,2000,2001,2002 Guido Draheim
7  *          All rights reserved,
8  *          use under the restrictions of the
9  *          Lesser GNU General Public License
10  *          or alternatively the restrictions 
11  *          of the Mozilla Public License 1.1
12  *
13  * Description:
14  *      although this file is defining a function called zzip_stat it
15  *      will not need a real stat(2) exported by the Operating System.
16  *      It will just try to fill the fields of the ZZIP_STAT structure
17  *      of 
18  */
19
20 #include <zzip/lib.h>                                   /* exported...*/
21 #include <zzip/file.h>
22 #include <string.h>
23 #include <sys/stat.h>
24
25 #define ZZIP_USE_INTERNAL
26 #include <zzip/info.h>
27
28 /**
29  * obtain information about a filename in an opened zip-archive without 
30  * opening that file first. Mostly used to obtain the uncompressed 
31  * size of a file inside a zip-archive. see => zzip_dir_open.
32  */
33 int 
34 zzip_dir_stat(ZZIP_DIR * dir, zzip_char_t* name, ZZIP_STAT * zs, int flags)
35 {
36     struct zzip_dir_hdr * hdr = dir->hdr0;
37     int (*cmp)(zzip_char_t*, zzip_char_t*);
38
39     cmp = (flags & ZZIP_CASEINSENSITIVE) ? strcasecmp : strcmp;
40
41     if (! hdr) {
42         dir->errcode = ZZIP_ENOENT;
43         return -1;
44     }
45
46     if (flags & ZZIP_IGNOREPATH)
47     {
48         char* n = strrchr(name, '/');
49         if (n)  name = n + 1;
50     }
51
52     while (1)
53     {
54         register char* hdr_name = hdr->d_name;
55         if (flags & ZZIP_IGNOREPATH)
56         {
57             register char* n = strrchr(hdr_name, '/');
58             if (n)  hdr_name = n + 1;
59         }
60
61         if (! cmp(hdr_name, name))
62             break;
63
64         if (! hdr->d_reclen)
65         {
66             dir->errcode = ZZIP_ENOENT;
67             return -1;
68         }
69
70         hdr = (struct zzip_dir_hdr *) ((char *)hdr + hdr->d_reclen);
71     }
72
73     zs->d_compr = hdr->d_compr;
74     zs->d_csize = hdr->d_csize;
75     zs->st_size = hdr->d_usize;
76     zs->d_name  = hdr->d_name;
77
78     return 0;
79 }
80
81 /** => zzip_dir_stat
82  * This function will obtain information about a opened file _within_ a 
83  * zip-archive. The file is supposed to be open (otherwise -1 is returned). 
84  * The st_size stat-member contains the uncompressed size. The optional 
85  * d_name is never set here. 
86  */
87 int zzip_file_stat (ZZIP_FILE* file, ZZIP_STAT* zs)
88 {
89     if (! file) return -1;
90     zs->d_compr = file->method;
91     zs->d_csize = file->csize;
92     zs->st_size = file->usize;
93     zs->d_name  = 0;
94     return 0;
95 }
96
97 /** => zzip_dir_stat
98  * This function will obtain information about a opened file which may be
99  * either real/zipped. The file is supposed to be open (otherwise -1 is 
100  * returned). The st_size stat-member contains the uncompressed size. 
101  * The optional d_name is never set here. For a real file, we do set the
102  * d_csize := st_size and d_compr := 0 for meaningful defaults.
103  */
104 int zzip_fstat (ZZIP_FILE* file, ZZIP_STAT* zs)
105 {
106     if (ZZIP_file_real(file))
107     {
108         struct stat st;
109         if (fstat (file->fd, &st) < 0) return -1;
110         zs->st_size = st.st_size;
111         zs->d_csize = st.st_size;
112         zs->d_compr = 0;
113         return 0;
114     }else{
115         return zzip_file_stat (file, zs);
116     }
117 }
118
119 /* 
120  * Local variables:
121  * c-file-style: "stroustrup"
122  * End:
123  */