PageRenderTime 18ms CodeModel.GetById 8ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/src/zziplib/zzip/stat.c

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