| 1 |
|
| 2 |
/* |
| 3 |
* Author: |
| 4 |
* Guido Draheim <[email protected]> |
| 5 |
* Mike Nordell <tamlin-@-algonet-se> |
| 6 |
* |
| 7 |
* Copyright (c) Guido Draheim, use under copyleft (LGPL,MPL) |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <zzip/lib.h> |
| 11 |
#include <zzip/plugin.h> |
| 12 |
|
| 13 |
#include <string.h> |
| 14 |
#include <sys/stat.h> |
| 15 |
#include <errno.h> |
| 16 |
#include <stdlib.h> |
| 17 |
#ifdef DEBUG |
| 18 |
#include <stdio.h> |
| 19 |
#endif |
| 20 |
|
| 21 |
#include <zzip/file.h> |
| 22 |
#include <zzip/format.h> |
| 23 |
|
| 24 |
/** get file size. |
| 25 |
* This function works on a real file descriptor. |
| 26 |
*/ |
| 27 |
zzip_off_t |
| 28 |
zzip_filesize(int fd) |
| 29 |
{ |
| 30 |
struct stat st; |
| 31 |
|
| 32 |
if (fstat(fd, &st) < 0) |
| 33 |
return -1; |
| 34 |
|
| 35 |
# if defined DEBUG && ! defined _WIN32 |
| 36 |
if (! st.st_size && st.st_blocks > 1) /* seen on some darwin 10.1 machines */ |
| 37 |
fprintf(stderr, "broken fstat(2) ?? st_size=%ld st_blocks=%ld\n", |
| 38 |
(long) st.st_size, (long) st.st_blocks); |
| 39 |
# endif |
| 40 |
|
| 41 |
return st.st_size; |
| 42 |
} |
| 43 |
|
| 44 |
static struct zzip_plugin_io default_io = { |
| 45 |
&open, |
| 46 |
&close, |
| 47 |
&_zzip_read, |
| 48 |
&_zzip_lseek, |
| 49 |
&zzip_filesize, |
| 50 |
1, 1, |
| 51 |
&_zzip_write |
| 52 |
}; |
| 53 |
|
| 54 |
/** => zzip_init_io |
| 55 |
* This function returns a zzip_plugin_io_t handle to static defaults |
| 56 |
* wrapping the posix io file functions for actual file access. The |
| 57 |
* returned structure is shared by all threads in the system. |
| 58 |
*/ |
| 59 |
zzip_plugin_io_t |
| 60 |
zzip_get_default_io(void) |
| 61 |
{ |
| 62 |
return (zzip_plugin_io_t) & default_io; |
| 63 |
} |
| 64 |
|
| 65 |
/** init plugin struct. |
| 66 |
* |
| 67 |
* This function initializes the users handler struct to default values |
| 68 |
* being the posix io functions in default configured environments. |
| 69 |
* |
| 70 |
* Note that the target io_handlers_t structure should be static or |
| 71 |
* atleast it should be kept during the lifetime of zzip operations. |
| 72 |
*/ |
| 73 |
int |
| 74 |
zzip_init_io(zzip_plugin_io_handlers_t io, int flags) |
| 75 |
{ |
| 76 |
if (! io) |
| 77 |
{ |
| 78 |
return ZZIP_ERROR; |
| 79 |
} |
| 80 |
memcpy(io, &default_io, sizeof(default_io)); |
| 81 |
io->fd.sys = flags; |
| 82 |
return 0; |
| 83 |
} |
| 84 |
|
| 85 |
/* |
| 86 |
* Local variables: |
| 87 |
* c-file-style: "stroustrup" |
| 88 |
* End: |
| 89 |
*/ |