| 1 |
|
| 2 |
/* |
| 3 |
* Author: |
| 4 |
* Guido Draheim <[email protected]> |
| 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 <stddef.h> /*offsetof */ |
| 12 |
#include <stdlib.h> |
| 13 |
#include <string.h> |
| 14 |
#include <errno.h> |
| 15 |
|
| 16 |
#ifdef ZZIP_HAVE_SYS_STAT_H |
| 17 |
#include <sys/stat.h> |
| 18 |
#else |
| 19 |
#include <stdio.h> |
| 20 |
#endif |
| 21 |
|
| 22 |
#include <zzip/zzip32.h> |
| 23 |
#include <zzip/__dirent.h> |
| 24 |
|
| 25 |
#ifndef offsetof |
| 26 |
#pragma warning had to DEFINE offsetof as it was not in stddef.h |
| 27 |
#define offsetof(T,M) ((unsigned)(& ((T*)0)->M)) |
| 28 |
#endif |
| 29 |
|
| 30 |
#ifdef ZZIP_HAVE_SYS_STAT_H |
| 31 |
|
| 32 |
/* MSVC does have IFbitmask but not the corresponding IStests */ |
| 33 |
# if ! defined S_ISDIR && defined S_IFDIR |
| 34 |
# define S_ISDIR(_X_) ((_X_) & S_IFDIR) |
| 35 |
# endif |
| 36 |
# if ! defined S_ISREG && defined S_IFREG |
| 37 |
# define S_ISREG(_X_) ((_X_) & S_IFREG) |
| 38 |
# endif |
| 39 |
#endif |
| 40 |
|
| 41 |
/** rewind. |
| 42 |
* |
| 43 |
* This function is the equivalent of a => rewinddir(2) for a realdir or |
| 44 |
* the zipfile in place of a directory. The ZZIP_DIR handle returned from |
| 45 |
* => zzip_opendir has a flag saying realdir or zipfile. As for a zipfile, |
| 46 |
* the filenames will include the filesubpath, so take care. |
| 47 |
*/ |
| 48 |
void |
| 49 |
zzip_rewinddir(ZZIP_DIR * dir) |
| 50 |
{ |
| 51 |
if (! dir) |
| 52 |
return; |
| 53 |
|
| 54 |
if (USE_DIRENT && dir->realdir) |
| 55 |
{ |
| 56 |
_zzip_rewinddir(dir->realdir); |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
if (dir->hdr0) |
| 61 |
dir->hdr = dir->hdr0; |
| 62 |
else |
| 63 |
dir->hdr = 0; |
| 64 |
} |
| 65 |
|
| 66 |
#if ! USE_DIRENT |
| 67 |
#define real_readdir(_X_) 1 |
| 68 |
#else |
| 69 |
static int |
| 70 |
real_readdir(ZZIP_DIR * dir) |
| 71 |
{ |
| 72 |
struct stat st = { 0 }; |
| 73 |
char filename[PATH_MAX]; |
| 74 |
struct dirent *dirent = _zzip_readdir(dir->realdir); |
| 75 |
|
| 76 |
if (! dirent) |
| 77 |
return 0; |
| 78 |
|
| 79 |
dir->dirent.d_name = dirent->d_name; |
| 80 |
strcpy(filename, dir->realname); |
| 81 |
strcat(filename, "/"); |
| 82 |
strcat(filename, dirent->d_name); |
| 83 |
|
| 84 |
if (stat(filename, &st) == -1) |
| 85 |
return -1; |
| 86 |
|
| 87 |
dir->dirent.d_csize = dir->dirent.st_size = st.st_size; |
| 88 |
|
| 89 |
if (st.st_mode) |
| 90 |
{ |
| 91 |
if (! S_ISREG(st.st_mode)) |
| 92 |
{ |
| 93 |
dir->dirent.d_compr = st.st_mode; |
| 94 |
dir->dirent.d_compr |= 0x80000000; |
| 95 |
/* makes it effectively negative, |
| 96 |
* but can still be fed to S_ISXXX(x) */ |
| 97 |
} else |
| 98 |
{ |
| 99 |
dir->dirent.d_compr = 0; /* stored */ |
| 100 |
} |
| 101 |
} else |
| 102 |
{ |
| 103 |
dir->dirent.d_compr = 0; /* stored */ |
| 104 |
} |
| 105 |
|
| 106 |
return 1; |
| 107 |
} |
| 108 |
#endif |
| 109 |
|
| 110 |
/** read dir. |
| 111 |
* |
| 112 |
* This function is the equivalent of a => readdir(2) for a realdir |
| 113 |
* or a zipfile referenced by the ZZIP_DIR returned from => zzip_opendir. |
| 114 |
* |
| 115 |
* The ZZIP_DIR handle (as returned by => zzip_opendir) contains a few more |
| 116 |
* entries than being copied into the ZZIP_DIRENT. The only valid fields in |
| 117 |
* a ZZIP_DIRENT are d_name (the file name), d_compr (compression), d_csize |
| 118 |
* (compressed size), st_size (uncompressed size). |
| 119 |
*/ |
| 120 |
ZZIP_DIRENT * |
| 121 |
zzip_readdir(ZZIP_DIR * dir) |
| 122 |
{ |
| 123 |
if (! dir) |
| 124 |
{ errno=EBADF; return 0; } |
| 125 |
|
| 126 |
if (USE_DIRENT && dir->realdir) |
| 127 |
{ |
| 128 |
if (! real_readdir(dir)) |
| 129 |
return 0; |
| 130 |
} else |
| 131 |
{ |
| 132 |
if (! dir->hdr) |
| 133 |
return 0; |
| 134 |
|
| 135 |
dir->dirent.d_name = dir->hdr->d_name; |
| 136 |
dir->dirent.d_compr = dir->hdr->d_compr; |
| 137 |
|
| 138 |
dir->dirent.d_csize = dir->hdr->d_csize; |
| 139 |
dir->dirent.st_size = dir->hdr->d_usize; |
| 140 |
|
| 141 |
if (! dir->hdr->d_reclen) |
| 142 |
dir->hdr = 0; |
| 143 |
else |
| 144 |
dir->hdr = (struct zzip_dir_hdr *) |
| 145 |
((char *) dir->hdr + dir->hdr->d_reclen); |
| 146 |
} |
| 147 |
return &dir->dirent; |
| 148 |
} |
| 149 |
|
| 150 |
/** => zzip_rewinddir |
| 151 |
* This function is the equivalent of => telldir(2) for a realdir or zipfile. |
| 152 |
*/ |
| 153 |
zzip_off_t |
| 154 |
zzip_telldir(ZZIP_DIR * dir) |
| 155 |
{ |
| 156 |
if (! dir) |
| 157 |
{ errno=EBADF; return -1; } |
| 158 |
|
| 159 |
if (USE_DIRENT && dir->realdir) |
| 160 |
{ |
| 161 |
return _zzip_telldir(dir->realdir); |
| 162 |
} else |
| 163 |
{ |
| 164 |
return ((zzip_off_t) ((char *) dir->hdr - (char *) dir->hdr0)); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
/** => zzip_rewinddir |
| 169 |
* This function is the equivalent of => seekdir(2) for a realdir or zipfile. |
| 170 |
*/ |
| 171 |
void |
| 172 |
zzip_seekdir(ZZIP_DIR * dir, zzip_off_t offset) |
| 173 |
{ |
| 174 |
if (! dir) |
| 175 |
return; |
| 176 |
|
| 177 |
if (USE_DIRENT && dir->realdir) |
| 178 |
{ |
| 179 |
_zzip_seekdir(dir->realdir, offset); |
| 180 |
} else |
| 181 |
{ |
| 182 |
dir->hdr = (struct zzip_dir_hdr *) |
| 183 |
(dir->hdr0 ? (char *) dir->hdr0 + (size_t) offset : 0); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
#ifndef EOVERFLOW |
| 188 |
#define EOVERFLOW EFBIG |
| 189 |
#endif |
| 190 |
|
| 191 |
/** => zzip_rewinddir |
| 192 |
* This function is provided for users who can not use any largefile-mode. |
| 193 |
*/ |
| 194 |
long |
| 195 |
zzip_telldir32(ZZIP_DIR * dir) |
| 196 |
{ |
| 197 |
if (sizeof(zzip_off_t) == sizeof(long)) |
| 198 |
{ |
| 199 |
return zzip_telldir(dir); |
| 200 |
} else |
| 201 |
{ |
| 202 |
off_t off = zzip_telldir(dir); |
| 203 |
if (off >= 0) { |
| 204 |
register long off32 = off; |
| 205 |
if (off32 == off) return off32; |
| 206 |
errno = EOVERFLOW; |
| 207 |
} |
| 208 |
return -1; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
/** => zzip_rewinddir |
| 213 |
* This function is provided for users who can not use any largefile-mode. |
| 214 |
*/ |
| 215 |
void |
| 216 |
zzip_seekdir32(ZZIP_DIR * dir, long offset) |
| 217 |
{ |
| 218 |
zzip_seekdir(dir, offset); |
| 219 |
} |
| 220 |
|
| 221 |
#if defined ZZIP_LARGEFILE_RENAME && defined EOVERFLOW && defined PIC |
| 222 |
/* DLL compatibility layer - so that 32bit code can link with a 64on32 too */ |
| 223 |
|
| 224 |
#undef zzip_seekdir /* zzip_seekdir64 */ |
| 225 |
#undef zzip_telldir /* zzip_telldir64 */ |
| 226 |
|
| 227 |
long zzip_telldir(ZZIP_DIR * dir) { return zzip_telldir32(dir); } |
| 228 |
void zzip_seekdir(ZZIP_DIR * dir, long offset) { zzip_seekdir32(dir, offset); } |
| 229 |
#endif |
| 230 |
|
| 231 |
/** start usage. |
| 232 |
* |
| 233 |
* This function is the equivalent of => opendir(3) for a realdir or zipfile. |
| 234 |
* |
| 235 |
* This function has some magic - if the given argument-path |
| 236 |
* is a directory, it will wrap a real => opendir(3) into the ZZIP_DIR |
| 237 |
* structure. Otherwise it will divert to => zzip_dir_open which |
| 238 |
* can also attach a ".zip" extension if needed to find the archive. |
| 239 |
* |
| 240 |
* the error-code is mapped to => errno(3). |
| 241 |
*/ |
| 242 |
ZZIP_DIR * |
| 243 |
zzip_opendir(zzip_char_t * filename) |
| 244 |
{ |
| 245 |
return zzip_opendir_ext_io(filename, 0, 0, 0); |
| 246 |
} |
| 247 |
|
| 248 |
/** => zzip_opendir |
| 249 |
* This function uses explicit ext and io instead of the internal |
| 250 |
* defaults, setting them to zero is equivalent to => zzip_opendir |
| 251 |
*/ |
| 252 |
ZZIP_DIR * |
| 253 |
zzip_opendir_ext_io(zzip_char_t * filename, int o_modes, |
| 254 |
zzip_strings_t * ext, zzip_plugin_io_t io) |
| 255 |
{ |
| 256 |
zzip_error_t e; |
| 257 |
ZZIP_DIR *dir; |
| 258 |
|
| 259 |
# ifdef ZZIP_HAVE_SYS_STAT_H |
| 260 |
struct stat st; |
| 261 |
# endif |
| 262 |
|
| 263 |
if (o_modes & (ZZIP_PREFERZIP | ZZIP_ONLYZIP)) |
| 264 |
goto try_zzip; |
| 265 |
try_real: |
| 266 |
|
| 267 |
# ifdef ZZIP_HAVE_SYS_STAT_H |
| 268 |
if (stat(filename, &st) >= 0 && S_ISDIR(st.st_mode)) |
| 269 |
{ |
| 270 |
if (USE_DIRENT) |
| 271 |
{ |
| 272 |
_zzip_DIR *realdir = _zzip_opendir(filename); |
| 273 |
|
| 274 |
if (realdir) |
| 275 |
{ |
| 276 |
if (! (dir = (ZZIP_DIR *) calloc(1, sizeof(*dir)))) |
| 277 |
{ |
| 278 |
_zzip_closedir(realdir); |
| 279 |
return 0; |
| 280 |
} else |
| 281 |
{ |
| 282 |
dir->realdir = realdir; |
| 283 |
dir->realname = strdup(filename); |
| 284 |
return dir; |
| 285 |
} |
| 286 |
} |
| 287 |
} |
| 288 |
return 0; |
| 289 |
} |
| 290 |
# endif /* HAVE_SYS_STAT_H */ |
| 291 |
|
| 292 |
try_zzip: |
| 293 |
dir = zzip_dir_open_ext_io(filename, &e, ext, io); |
| 294 |
if (! dir && (o_modes & ZZIP_PREFERZIP)) |
| 295 |
goto try_real; |
| 296 |
if (e) |
| 297 |
errno = zzip_errno(e); |
| 298 |
return dir; |
| 299 |
} |
| 300 |
|
| 301 |
/** stop usage. |
| 302 |
* |
| 303 |
* This function is the equivalent of => closedir(3) for a realdir or zipfile. |
| 304 |
* |
| 305 |
* This function is magic - if the given arg-ZZIP_DIR |
| 306 |
* is a real directory, it will call the real => closedir(3) and then |
| 307 |
* free the wrapping ZZIP_DIR structure. Otherwise it will divert |
| 308 |
* to => zzip_dir_close which will free the ZZIP_DIR structure. |
| 309 |
*/ |
| 310 |
int |
| 311 |
zzip_closedir(ZZIP_DIR * dir) |
| 312 |
{ |
| 313 |
if (! dir) |
| 314 |
{ errno = EBADF; return -1; } |
| 315 |
|
| 316 |
if (USE_DIRENT && dir->realdir) |
| 317 |
{ |
| 318 |
_zzip_closedir(dir->realdir); |
| 319 |
free(dir->realname); |
| 320 |
free(dir); |
| 321 |
return 0; |
| 322 |
} else |
| 323 |
{ |
| 324 |
zzip_dir_close(dir); |
| 325 |
return 0; |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
/* |
| 330 |
* Local variables: |
| 331 |
* c-file-style: "stroustrup" |
| 332 |
* End: |
| 333 |
*/ |