123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945 |
- #include "zip.h"
- #ifndef UTIL
- #include <time.h>
- #include <dirent.h>
- #include <errno.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <dirent.h>
- #include <kernel/fs_attr.h>
- #include <storage/Mime.h>
- #include <support/byteorder.h>
- #define PAD 0
- #define PATH_END '/'
- #ifdef _POSIX_VERSION
- # include <utime.h>
- #else
- int utime OF((char *, time_t *));
- #endif
- extern char *label;
- local ulg label_time = 0;
- local ulg label_mode = 0;
- local time_t label_utim = 0;
- local char *readd OF((DIR *));
- local int get_attr_dir( const char *, char **, off_t * );
- local int add_UT_ef( struct zlist far * );
- local int add_Ux_ef( struct zlist far * );
- local int add_Be_ef( struct zlist far * );
- #ifdef NO_DIR
- #include <sys/dir.h>
- #ifndef dirent
- # define dirent direct
- #endif
- typedef FILE DIR;
- #define opendir(path) fopen(path, "r")
- struct dirent *readdir(dirp)
- DIR *dirp;
- {
- static struct dirent entry;
- if (dirp == NULL)
- return NULL;
- for (;;)
- if (fread (&entry, sizeof (struct dirent), 1, dirp) == 0)
- return NULL;
- else if (entry.d_ino)
- return (&entry);
- }
- #define closedir(dirp) fclose(dirp)
- #endif
- local char *readd(d)
- DIR *d;
- {
- struct dirent *e;
- e = readdir(d);
- return e == NULL ? (char *) NULL : e->d_name;
- }
- int procname(n, caseflag)
- char *n;
- int caseflag;
- {
- char *a;
- DIR *d;
- char *e;
- int m;
- char *p;
- struct stat s;
- struct zlist far *z;
- if (strcmp(n, "-") == 0)
- return newname(n, 0, caseflag);
- else if (LSSTAT(n, &s))
- {
-
- p = ex2in(n, 0, (int *)NULL);
- m = 1;
- for (z = zfiles; z != NULL; z = z->nxt) {
- if (MATCH(p, z->iname, caseflag))
- {
- z->mark = pcount ? filter(z->zname, caseflag) : 1;
- if (verbose)
- fprintf(mesg, "zip diagnostic: %scluding %s\n",
- z->mark ? "in" : "ex", z->name);
- m = 0;
- }
- }
- free((zvoid *)p);
- return m ? ZE_MISS : ZE_OK;
- }
-
- if ((s.st_mode & S_IFDIR) == 0)
- {
-
- if ((m = newname(n, 0, caseflag)) != ZE_OK)
- return m;
- } else {
-
- if ((p = malloc(strlen(n)+2)) == NULL)
- return ZE_MEM;
- if (strcmp(n, ".") == 0) {
- *p = '\0';
- } else {
- strcpy(p, n);
- a = p + strlen(p);
- if (a[-1] != '/')
- strcpy(a, "/");
- if (dirnames && (m = newname(p, 1, caseflag)) != ZE_OK) {
- free((zvoid *)p);
- return m;
- }
- }
-
- if (recurse && (d = opendir(n)) != NULL)
- {
- while ((e = readd(d)) != NULL) {
- if (strcmp(e, ".") && strcmp(e, ".."))
- {
- if ((a = malloc(strlen(p) + strlen(e) + 1)) == NULL)
- {
- closedir(d);
- free((zvoid *)p);
- return ZE_MEM;
- }
- strcat(strcpy(a, p), e);
- if ((m = procname(a, caseflag)) != ZE_OK)
- {
- if (m == ZE_MISS)
- zipwarn("name not matched: ", a);
- else
- ziperr(m, a);
- }
- free((zvoid *)a);
- }
- }
- closedir(d);
- }
- free((zvoid *)p);
- }
- return ZE_OK;
- }
- char *ex2in(x, isdir, pdosflag)
- char *x;
- int isdir;
- int *pdosflag;
- {
- char *n;
- char *t;
- int dosflag;
- dosflag = dosify;
-
- for (t = x; *t == '/'; t++)
- ;
- while (*t == '.' && t[1] == '/')
- t += 2;
-
- if (!pathput)
- t = last(t, PATH_END);
-
- if ((n = malloc(strlen(t) + 1)) == NULL)
- return NULL;
- strcpy(n, t);
- if (isdir == 42) return n;
- if (dosify)
- msname(n);
-
- if (pdosflag)
- *pdosflag = dosflag;
- return n;
- }
- char *in2ex(n)
- char *n;
- {
- char *x;
- if ((x = malloc(strlen(n) + 1 + PAD)) == NULL)
- return NULL;
- strcpy(x, n);
- return x;
- }
- void stamp(f, d)
- char *f;
- ulg d;
- {
- #ifdef _POSIX_VERSION
- struct utimbuf u;
- #else
- time_t u[2];
- #endif
-
- #ifdef _POSIX_VERSION
- u.actime = u.modtime = dos2unixtime(d);
- utime(f, &u);
- #else
- u[0] = u[1] = dos2unixtime(d);
- utime(f, u);
- #endif
- }
- ulg filetime(f, a, n, t)
- char *f;
- ulg *a;
- long *n;
- iztimes *t;
- {
- struct stat s;
-
- char *name;
- int len = strlen(f);
- if (f == label) {
- if (a != NULL)
- *a = label_mode;
- if (n != NULL)
- *n = -2L;
- if (t != NULL)
- t->atime = t->mtime = t->ctime = label_utim;
- return label_time;
- }
- if ((name = malloc(len + 1)) == NULL) {
- ZIPERR(ZE_MEM, "filetime");
- }
- strcpy(name, f);
- if (name[len - 1] == '/')
- name[len - 1] = '\0';
-
- if (strcmp(f, "-") == 0) {
- if (fstat(fileno(stdin), &s) != 0) {
- free(name);
- error("fstat(stdin)");
- }
- } else if (LSSTAT(name, &s) != 0) {
-
- free(name);
- return 0;
- }
- free(name);
- if (a != NULL) {
- *a = ((ulg)s.st_mode << 16) | !(s.st_mode & S_IWRITE);
- if ((s.st_mode & S_IFMT) == S_IFDIR) {
- *a |= MSDOS_DIR_ATTR;
- }
- }
- if (n != NULL)
- *n = (s.st_mode & S_IFMT) == S_IFREG ? s.st_size : -1L;
- if (t != NULL) {
- t->atime = s.st_atime;
- t->mtime = s.st_mtime;
- t->ctime = s.st_mtime;
- }
- return unix2dostime(&s.st_mtime);
- }
- #define INITIAL_BUFF_SIZE 65536
- int get_attr_dir( const char *name, char **attr_buff, off_t *total_size )
- {
- int retval = EOK;
- int fd;
- DIR *fa_dir;
- struct dirent *fa_ent;
- off_t attrs_size;
- off_t this_size;
- char *ptr;
- struct attr_info fa_info;
- struct attr_info big_fa_info;
- retval = EOK;
- attrs_size = 0;
- *total_size = 0;
-
-
- if( *attr_buff != NULL ) {
- return EINVAL;
- }
-
-
-
-
-
-
-
- if( linkput ) {
- fd = open( name, O_RDONLY | O_NOTRAVERSE );
- } else {
- fd = open( name, O_RDONLY );
- }
- if( fd < 0 ) {
- return errno;
- }
-
-
- *attr_buff = (char *)malloc( INITIAL_BUFF_SIZE );
- ptr = *attr_buff;
- if( ptr == NULL ) {
- close( fd );
- return ENOMEM;
- }
-
-
- fa_dir = fs_fopen_attr_dir( fd );
- if( fa_dir == NULL ) {
- close( fd );
- free( ptr );
- *attr_buff = NULL;
- return retval;
- }
-
-
-
- fa_ent = fs_read_attr_dir( fa_dir );
- while( fa_ent != NULL ) {
- retval = fs_stat_attr( fd, fa_ent->d_name, &fa_info );
-
- this_size = strlen( fa_ent->d_name ) + 1;
- this_size += sizeof( struct attr_info );
- this_size += fa_info.size;
- attrs_size += this_size;
- if( attrs_size > INITIAL_BUFF_SIZE ) {
- unsigned long offset = ptr - *attr_buff;
- *attr_buff = (char *)realloc( *attr_buff, attrs_size );
- if( *attr_buff == NULL ) {
- retval = fs_close_attr_dir( fa_dir );
-
- close( fd );
- return ENOMEM;
- }
- ptr = *attr_buff + offset;
- }
-
- strcpy( ptr, fa_ent->d_name );
- ptr += strlen( fa_ent->d_name );
- *ptr++ = '\0';
-
-
- big_fa_info.type = B_HOST_TO_BENDIAN_INT32( fa_info.type );
- big_fa_info.size = B_HOST_TO_BENDIAN_INT64( fa_info.size );
- memcpy( ptr, &big_fa_info, sizeof( struct attr_info ) );
- ptr += sizeof( struct attr_info );
- if( fa_info.size > 0 ) {
- ssize_t read_bytes;
- read_bytes = fs_read_attr( fd, fa_ent->d_name, fa_info.type, 0,
- ptr, fa_info.size );
- if( read_bytes != fa_info.size ) {
-
- char buff[80];
- sprintf( buff, "read %ld, expected %ld",
- (ssize_t)read_bytes, (ssize_t)fa_info.size );
- zipwarn( "attribute size mismatch: ", buff );
- }
-
-
- (void)swap_data( fa_info.type, ptr, fa_info.size,
- B_SWAP_HOST_TO_BENDIAN );
- ptr += fa_info.size;
- }
- fa_ent = fs_read_attr_dir( fa_dir );
- }
-
-
- retval = fs_close_attr_dir( fa_dir );
-
-
-
- if( attrs_size < INITIAL_BUFF_SIZE ) {
- *attr_buff = (char *)realloc( *attr_buff, attrs_size );
- if( *attr_buff == NULL ) {
-
- close( fd );
- return ENOMEM;
- }
- }
- *total_size = attrs_size;
- close( fd );
- return EOK;
- }
- #define EB_L_UT_SIZE (EB_HEADSIZE + EB_UT_LEN(2))
- #define EB_C_UT_SIZE (EB_HEADSIZE + EB_UT_LEN(1))
- local int add_UT_ef( struct zlist far *z )
- {
- char *l_ef = NULL;
- char *c_ef = NULL;
- struct stat s;
- #ifdef IZ_CHECK_TZ
- if (!zp_tz_is_valid)
- return ZE_OK;
- #endif
-
- if( z == NULL ) {
- return ZE_LOGIC;
- }
-
- if( z->ext + EB_L_UT_SIZE > USHRT_MAX ||
- z->cext + EB_C_UT_SIZE > USHRT_MAX ) {
- return ZE_MEM;
- }
-
-
- if(LSSTAT( z->name, &s ) ) {
- return ZE_OPEN;
- }
-
- if( z->extra && z->ext != 0 ) {
- l_ef = (char *)realloc( z->extra, z->ext + EB_L_UT_SIZE );
- } else {
- l_ef = (char *)malloc( EB_L_UT_SIZE );
- z->ext = 0;
- }
- if( l_ef == NULL ) {
- return ZE_MEM;
- }
- z->extra = l_ef;
- l_ef += z->ext;
- if( z->cextra && z->cext != 0 ) {
- c_ef = (char *)realloc( z->cextra, z->cext + EB_C_UT_SIZE );
- } else {
- c_ef = (char *)malloc( EB_C_UT_SIZE );
- z->cext = 0;
- }
- if( c_ef == NULL ) {
- return ZE_MEM;
- }
- z->cextra = c_ef;
- c_ef += z->cext;
-
- *l_ef++ = 'U';
- *l_ef++ = 'T';
- *l_ef++ = (char)(EB_UT_LEN(2));
- *l_ef++ = (char)0;
- *l_ef++ = (char)(EB_UT_FL_MTIME | EB_UT_FL_ATIME);
- *l_ef++ = (char)(s.st_mtime);
- *l_ef++ = (char)(s.st_mtime >> 8);
- *l_ef++ = (char)(s.st_mtime >> 16);
- *l_ef++ = (char)(s.st_mtime >> 24);
- *l_ef++ = (char)(s.st_atime);
- *l_ef++ = (char)(s.st_atime >> 8);
- *l_ef++ = (char)(s.st_atime >> 16);
- *l_ef++ = (char)(s.st_atime >> 24);
- z->ext += EB_L_UT_SIZE;
-
- memcpy(c_ef, l_ef-EB_L_UT_SIZE, EB_C_UT_SIZE);
- c_ef[EB_LEN] = (char)(EB_UT_LEN(1));
- z->cext += EB_C_UT_SIZE;
- return ZE_OK;
- }
- #define EB_L_UX2_SIZE (EB_HEADSIZE + EB_UX2_MINLEN)
- #define EB_C_UX2_SIZE (EB_HEADSIZE)
- local int add_Ux_ef( struct zlist far *z )
- {
- char *l_ef = NULL;
- char *c_ef = NULL;
- struct stat s;
-
- if( z->ext + EB_L_UX2_SIZE > USHRT_MAX ||
- z->cext + EB_C_UX2_SIZE > USHRT_MAX ) {
- return ZE_MEM;
- }
-
-
- if(LSSTAT( z->name, &s ) ) {
- return ZE_OPEN;
- }
-
- if( z->extra && z->ext != 0 ) {
- l_ef = (char *)realloc( z->extra, z->ext + EB_L_UX2_SIZE );
- } else {
- l_ef = (char *)malloc( EB_L_UX2_SIZE );
- z->ext = 0;
- }
- if( l_ef == NULL ) {
- return ZE_MEM;
- }
- z->extra = l_ef;
- l_ef += z->ext;
- if( z->cextra && z->cext != 0 ) {
- c_ef = (char *)realloc( z->cextra, z->cext + EB_C_UX2_SIZE );
- } else {
- c_ef = (char *)malloc( EB_C_UX2_SIZE );
- z->cext = 0;
- }
- if( c_ef == NULL ) {
- return ZE_MEM;
- }
- z->cextra = c_ef;
- c_ef += z->cext;
-
- *l_ef++ = 'U';
- *l_ef++ = 'x';
- *l_ef++ = (char)(EB_UX2_MINLEN);
- *l_ef++ = (char)(EB_UX2_MINLEN >> 8);
- *l_ef++ = (char)(s.st_uid);
- *l_ef++ = (char)(s.st_uid >> 8);
- *l_ef++ = (char)(s.st_gid);
- *l_ef++ = (char)(s.st_gid >> 8);
- z->ext += EB_L_UX2_SIZE;
-
- *c_ef++ = 'U';
- *c_ef++ = 'x';
- *c_ef++ = 0;
- *c_ef++ = 0;
- z->cext += EB_C_UX2_SIZE;
- return ZE_OK;
- }
- #define EB_L_BE_SIZE (EB_HEADSIZE + EB_L_BE_LEN)
- #define EB_C_BE_SIZE (EB_HEADSIZE + EB_C_BE_LEN)
- #define MEMCOMPRESS_OVERHEAD (EB_MEMCMPR_HSIZ + EB_DEFLAT_EXTRA)
- local int add_Be_ef( struct zlist far *z )
- {
- char *l_ef = NULL;
- char *c_ef = NULL;
- char *attrbuff = NULL;
- off_t attrsize = 0;
- char *compbuff = NULL;
- ush compsize = 0;
- uch flags = 0;
-
- if( z->ext + EB_L_BE_SIZE > USHRT_MAX ||
- z->cext + EB_C_BE_SIZE > USHRT_MAX ) {
- return ZE_MEM;
- }
-
- {
- int retval;
- retval = get_attr_dir( z->name, &attrbuff, &attrsize );
- if( retval != EOK ) {
- return ZE_OPEN;
- }
- if( attrsize == 0 ) {
- return ZE_OK;
- }
- if( attrbuff == NULL ) {
- return ZE_LOGIC;
- }
-
- if( attrsize > (off_t)ULONG_MAX ) {
- zipwarn( "uncompressed attributes truncated", "" );
- attrsize = (off_t)(ULONG_MAX - MEMCOMPRESS_OVERHEAD);
- }
- }
- if( verbose ) {
- printf( "\t[in=%lu]", (unsigned long)attrsize );
- }
-
- compbuff = (char *)malloc( (size_t)attrsize + MEMCOMPRESS_OVERHEAD );
- if( compbuff == NULL ) {
- return ZE_MEM;
- }
- compsize = memcompress( compbuff,
- (size_t)attrsize + MEMCOMPRESS_OVERHEAD,
- attrbuff,
- (size_t)attrsize );
- if( verbose ) {
- printf( " [out=%u]", compsize );
- }
-
- if( compsize > attrsize ) {
- free( compbuff );
- compsize = (ush)attrsize;
- compbuff = attrbuff;
- flags = EB_BE_FL_NATURAL;
- }
-
- if( ( z->ext + compsize + EB_L_BE_LEN ) > USHRT_MAX ) {
- compsize = USHRT_MAX - EB_L_BE_LEN - z->ext;
- }
-
- if( z->extra && z->ext != 0 ) {
- l_ef = (char *)realloc( z->extra, z->ext + EB_L_BE_SIZE + compsize );
- } else {
- l_ef = (char *)malloc( EB_L_BE_SIZE + compsize );
- z->ext = 0;
- }
- if( l_ef == NULL ) {
- return ZE_MEM;
- }
- z->extra = l_ef;
- l_ef += z->ext;
- if( z->cextra && z->cext != 0 ) {
- c_ef = (char *)realloc( z->cextra, z->cext + EB_C_BE_SIZE );
- } else {
- c_ef = (char *)malloc( EB_C_BE_SIZE );
- z->cext = 0;
- }
- if( c_ef == NULL ) {
- return ZE_MEM;
- }
- z->cextra = c_ef;
- c_ef += z->cext;
-
- *l_ef++ = 'B';
- *l_ef++ = 'e';
- *l_ef++ = (char)(compsize + EB_L_BE_LEN);
- *l_ef++ = (char)((compsize + EB_L_BE_LEN) >> 8);
- *l_ef++ = (char)((unsigned long)attrsize);
- *l_ef++ = (char)((unsigned long)attrsize >> 8);
- *l_ef++ = (char)((unsigned long)attrsize >> 16);
- *l_ef++ = (char)((unsigned long)attrsize >> 24);
- *l_ef++ = flags;
- memcpy( l_ef, compbuff, (size_t)compsize );
- z->ext += EB_L_BE_SIZE + compsize;
-
- *c_ef++ = 'B';
- *c_ef++ = 'e';
- *c_ef++ = (char)(EB_C_BE_LEN);
- *c_ef++ = (char)(EB_C_BE_LEN >> 8);
- *c_ef++ = (char)compsize;
- *c_ef++ = (char)(compsize >> 8);
- *c_ef++ = (char)(compsize >> 16);
- *c_ef++ = (char)(compsize >> 24);
- *c_ef++ = flags;
- z->cext += EB_C_BE_SIZE;
- return ZE_OK;
- }
- int set_extra_field(z, z_utim)
- struct zlist far *z;
- iztimes *z_utim;
-
- {
- int retval;
-
- z_utim = z_utim;
-
- if( z == NULL ) {
- return ZE_LOGIC;
- }
-
-
- retval = add_UT_ef( z );
- if( retval != ZE_OK ) {
- return retval;
- }
- retval = add_Ux_ef( z );
- if( retval != ZE_OK ) {
- return retval;
- }
- retval = add_Be_ef( z );
- if( retval != ZE_OK ) {
- return retval;
- }
- return ZE_OK;
- }
- void setfiletype( const char *file, const char *type )
- {
- int fd;
- attr_info fa;
- ssize_t wrote_bytes;
- fd = open( file, O_RDWR );
- if( fd < 0 ) {
- zipwarn( "can't open zipfile to write file type", "" );
- return;
- }
- fa.type = B_MIME_STRING_TYPE;
- fa.size = (off_t)(strlen( type ) + 1);
- wrote_bytes = fs_write_attr( fd, BE_FILE_TYPE_NAME, fa.type, 0,
- type, fa.size );
- if( wrote_bytes != (ssize_t)fa.size ) {
- zipwarn( "couldn't write complete file type", "" );
- }
- close( fd );
- }
- int deletedir(d)
- char *d;
- {
- # ifdef NO_RMDIR
-
- int r, len;
- char *s;
- len = strlen(d);
- if ((s = malloc(len + 34)) == NULL)
- return 127;
- sprintf(s, "IFS=\" \t\n\" /bin/rmdir %s 2>/dev/null", d);
- r = system(s);
- free(s);
- return r;
- # else
- return rmdir(d);
- # endif
- }
- #endif
- void version_local()
- {
- static ZCONST char CompiledWith[] = "Compiled with %s%s for %s%s%s%s.\n\n";
- printf(CompiledWith,
- #ifdef __MWERKS__
- "Metrowerks CodeWarrior", "",
- #else
- # ifdef __GNUC__
- "gcc ", __VERSION__,
- # endif
- #endif
- "BeOS",
- #ifdef __POWERPC__
- " (PowerPC)",
- #else
- # ifdef __INTEL__
- " (x86)",
- # else
- " (UNKNOWN!)",
- # endif
- #endif
- #ifdef __DATE__
- " on ", __DATE__
- #else
- "", ""
- #endif
- );
- }
|