1 
2 /*
3  * Author:
4  *      Guido Draheim <guidod@gmx.de>
5  *
6  * Copyright (c) Guido Draheim, use under copyleft (LGPL,MPL)
7  */
8 
9 #include <zzip/lib.h>           /* exported... */
10 #include <zzip/file.h>
11 #include <zzip/format.h>
12 
13 #ifdef ZZIP_HAVE_SYS_STAT_H
14 #include <sys/stat.h>
15 #else
16 #include <stdlib.h>
17 #include <stdio.h>
18 #endif
19 
20 /** get errror status.
21  *  This function just returns dir->errcode of the ZZIP_DIR handle
22  *  see: => zzip_dir_open, => zzip_dir_open, => zzip_readdir, => zzip_dir_read
23  */
24 int
zzip_error(ZZIP_DIR * dir)25 zzip_error(ZZIP_DIR * dir)
26 {
27     return dir->errcode;
28 }
29 
30 /** => zzip_error
31  *  This function just does dir->errcode = errcode
32  */
33 void
zzip_seterror(ZZIP_DIR * dir,int errcode)34 zzip_seterror(ZZIP_DIR * dir, int errcode) { dir->errcode = errcode; }
35 
36 /** get handle.
37  * This function will just return the fp->dir value.
38  *
39  * If a ZZIP_FILE is contained within a zip-file that one will be a valid
40  * pointer, otherwise a NULL is returned and the ZZIP_FILE wraps a real file.
41  */
42 ZZIP_DIR *
zzip_dirhandle(ZZIP_FILE * fp)43 zzip_dirhandle(ZZIP_FILE * fp)
44 {
45     return fp->dir;
46 }
47 
48 /** => zzip_dirhandle
49  *  This function will just return dir->fd
50  *
51  * If a ZZIP_DIR does point to a zipfile then the file-descriptor of that
52  * zipfile is returned, otherwise a NULL is returned and the ZZIP_DIR wraps
53  * a real directory DIR (if you have dirent on your system).
54  */
55 int
zzip_dirfd(ZZIP_DIR * dir)56 zzip_dirfd(ZZIP_DIR * dir)
57 {
58     return dir->fd;
59 }
60 
61 #define LENGTH(x) (sizeof(x) / sizeof(*x))
62 static const char* comprlevel[] = {
63     "stored",   "shrunk",   "redu:1",   "redu:2",   "redu:3",   "redu:4",
64     "impl:N",   "toknze",   "defl:N",   "defl:B",   "impl:B" };
65 
66 /** compr name.
67  * This function returns the static const string of the known compression methods,
68  * Unknown id values will return just "zipped" as the string code.
69  */
70 zzip_char_t *
zzip_compr_str(int compr)71 zzip_compr_str(int compr)
72 {
73     if (0 <= compr && (unsigned) compr < LENGTH(comprlevel))
74     {
75         return comprlevel[compr];
76     } else if (0 < compr && compr < 256)
77     {
78         return "zipped";
79     } else
80     {
81 #	ifdef S_ISDIR
82         if (S_ISDIR(compr))		return "directory";
83 #	endif
84 #	ifdef S_ISCHR
85         if (S_ISCHR(compr))		return "is/chr";
86 #	endif
87 #	ifdef S_ISBLK
88         if (S_ISBLK(compr))		return "is/blk";
89 #	endif
90 #	ifdef S_ISFIFO
91         if (S_ISFIFO(compr))	return "is/fifo";
92 #	endif
93 #	ifdef S_ISSOCK
94         if (S_ISSOCK(compr))	return "is/sock";
95 #	endif
96 #	ifdef S_ISLNK
97         if (S_ISLNK(compr))		return "is/lnk";
98 #	endif
99         return "special";
100     }
101 }
102 
103 /** => zzip_file_real
104  * This function checks if the ZZIP_DIR-handle is wrapping
105  * a real directory or a zip-archive.
106  * Returns 1 for a stat'able directory, and 0 for a handle to zip-archive.
107  */
108 int
zzip_dir_real(ZZIP_DIR * dir)109 zzip_dir_real(ZZIP_DIR * dir)
110 {
111     return dir->realdir != 0;
112 }
113 
114 /** check real or zipped file.
115  * This function checks if the ZZIP_FILE-handle is wrapping
116  * a real file or a zip-contained file.
117  * Returns 1 for a stat'able file, and 0 for a file inside a zip-archive.
118  */
119 int
zzip_file_real(ZZIP_FILE * fp)120 zzip_file_real(ZZIP_FILE * fp)
121 {
122     return fp->dir == 0;        /* ie. not dependent on a zip-arch-dir  */
123 }
124 
125 /** => zzip_file_real
126  * This function returns the posix DIR* handle (if one exists).
127  * Check before with => zzip_dir_real if the
128  * the ZZIP_DIR points to a real directory.
129  */
130 void *
zzip_realdir(ZZIP_DIR * dir)131 zzip_realdir(ZZIP_DIR * dir)
132 {
133     return dir->realdir;
134 }
135 
136 /** => zzip_file_real
137  * This function returns the posix file descriptor (if one exists).
138  * Check before with => zzip_file_real if the
139  * the ZZIP_FILE points to a real file.
140  */
141 int
zzip_realfd(ZZIP_FILE * fp)142 zzip_realfd(ZZIP_FILE * fp)
143 {
144     return fp->fd;
145 }
146 
147 /*
148  * Local variables:
149  * c-file-style: "stroustrup"
150  * End:
151  */
152