beos.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  1. /*
  2. Copyright (c) 1990-2002 Info-ZIP. All rights reserved.
  3. See the accompanying file LICENSE, version 2000-Apr-09 or later
  4. (the contents of which are also included in zip.h) for terms of use.
  5. If, for some reason, all these files are missing, the Info-ZIP license
  6. also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html
  7. */
  8. /*
  9. This BeOS-specific file is based on unix.c in the unix directory; changes
  10. by Chris Herborth (chrish@pobox.com).
  11. */
  12. #include "zip.h"
  13. #ifndef UTIL /* the companion #endif is a bit of ways down ... */
  14. #include <time.h>
  15. #include <dirent.h>
  16. #include <errno.h>
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #include <fcntl.h>
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. #include <string.h>
  23. #include <dirent.h>
  24. #include <kernel/fs_attr.h>
  25. #include <storage/Mime.h>
  26. #include <support/byteorder.h>
  27. #define PAD 0
  28. #define PATH_END '/'
  29. /* Library functions not in (most) header files */
  30. #ifdef _POSIX_VERSION
  31. # include <utime.h>
  32. #else
  33. int utime OF((char *, time_t *));
  34. #endif
  35. extern char *label;
  36. local ulg label_time = 0;
  37. local ulg label_mode = 0;
  38. local time_t label_utim = 0;
  39. /* Local functions */
  40. local char *readd OF((DIR *));
  41. local int get_attr_dir( const char *, char **, off_t * );
  42. local int add_UT_ef( struct zlist far * );
  43. local int add_Ux_ef( struct zlist far * );
  44. local int add_Be_ef( struct zlist far * );
  45. #ifdef NO_DIR /* for AT&T 3B1 */
  46. #include <sys/dir.h>
  47. #ifndef dirent
  48. # define dirent direct
  49. #endif
  50. typedef FILE DIR;
  51. /*
  52. ** Apparently originally by Rich Salz.
  53. ** Cleaned up and modified by James W. Birdsall.
  54. */
  55. #define opendir(path) fopen(path, "r")
  56. struct dirent *readdir(dirp)
  57. DIR *dirp;
  58. {
  59. static struct dirent entry;
  60. if (dirp == NULL)
  61. return NULL;
  62. for (;;)
  63. if (fread (&entry, sizeof (struct dirent), 1, dirp) == 0)
  64. return NULL;
  65. else if (entry.d_ino)
  66. return (&entry);
  67. } /* end of readdir() */
  68. #define closedir(dirp) fclose(dirp)
  69. #endif /* NO_DIR */
  70. local char *readd(d)
  71. DIR *d; /* directory stream to read from */
  72. /* Return a pointer to the next name in the directory stream d, or NULL if
  73. no more entries or an error occurs. */
  74. {
  75. struct dirent *e;
  76. e = readdir(d);
  77. return e == NULL ? (char *) NULL : e->d_name;
  78. }
  79. int procname(n, caseflag)
  80. char *n; /* name to process */
  81. int caseflag; /* true to force case-sensitive match */
  82. /* Process a name or sh expression to operate on (or exclude). Return
  83. an error code in the ZE_ class. */
  84. {
  85. char *a; /* path and name for recursion */
  86. DIR *d; /* directory stream from opendir() */
  87. char *e; /* pointer to name from readd() */
  88. int m; /* matched flag */
  89. char *p; /* path for recursion */
  90. struct stat s; /* result of stat() */
  91. struct zlist far *z; /* steps through zfiles list */
  92. if (strcmp(n, "-") == 0) /* if compressing stdin */
  93. return newname(n, 0, caseflag);
  94. else if (LSSTAT(n, &s))
  95. {
  96. /* Not a file or directory--search for shell expression in zip file */
  97. p = ex2in(n, 0, (int *)NULL); /* shouldn't affect matching chars */
  98. m = 1;
  99. for (z = zfiles; z != NULL; z = z->nxt) {
  100. if (MATCH(p, z->iname, caseflag))
  101. {
  102. z->mark = pcount ? filter(z->zname, caseflag) : 1;
  103. if (verbose)
  104. fprintf(mesg, "zip diagnostic: %scluding %s\n",
  105. z->mark ? "in" : "ex", z->name);
  106. m = 0;
  107. }
  108. }
  109. free((zvoid *)p);
  110. return m ? ZE_MISS : ZE_OK;
  111. }
  112. /* Live name--use if file, recurse if directory */
  113. if ((s.st_mode & S_IFDIR) == 0)
  114. {
  115. /* add or remove name of file */
  116. if ((m = newname(n, 0, caseflag)) != ZE_OK)
  117. return m;
  118. } else {
  119. /* Add trailing / to the directory name */
  120. if ((p = malloc(strlen(n)+2)) == NULL)
  121. return ZE_MEM;
  122. if (strcmp(n, ".") == 0) {
  123. *p = '\0'; /* avoid "./" prefix and do not create zip entry */
  124. } else {
  125. strcpy(p, n);
  126. a = p + strlen(p);
  127. if (a[-1] != '/')
  128. strcpy(a, "/");
  129. if (dirnames && (m = newname(p, 1, caseflag)) != ZE_OK) {
  130. free((zvoid *)p);
  131. return m;
  132. }
  133. }
  134. /* recurse into directory */
  135. if (recurse && (d = opendir(n)) != NULL)
  136. {
  137. while ((e = readd(d)) != NULL) {
  138. if (strcmp(e, ".") && strcmp(e, ".."))
  139. {
  140. if ((a = malloc(strlen(p) + strlen(e) + 1)) == NULL)
  141. {
  142. closedir(d);
  143. free((zvoid *)p);
  144. return ZE_MEM;
  145. }
  146. strcat(strcpy(a, p), e);
  147. if ((m = procname(a, caseflag)) != ZE_OK) /* recurse on name */
  148. {
  149. if (m == ZE_MISS)
  150. zipwarn("name not matched: ", a);
  151. else
  152. ziperr(m, a);
  153. }
  154. free((zvoid *)a);
  155. }
  156. }
  157. closedir(d);
  158. }
  159. free((zvoid *)p);
  160. } /* (s.st_mode & S_IFDIR) == 0) */
  161. return ZE_OK;
  162. }
  163. char *ex2in(x, isdir, pdosflag)
  164. char *x; /* external file name */
  165. int isdir; /* input: x is a directory */
  166. int *pdosflag; /* output: force MSDOS file attributes? */
  167. /* Convert the external file name to a zip file name, returning the malloc'ed
  168. string or NULL if not enough memory. */
  169. {
  170. char *n; /* internal file name (malloc'ed) */
  171. char *t; /* shortened name */
  172. int dosflag;
  173. dosflag = dosify; /* default for non-DOS and non-OS/2 */
  174. /* Find starting point in name before doing malloc */
  175. for (t = x; *t == '/'; t++)
  176. ; /* strip leading '/' chars to get a relative path */
  177. while (*t == '.' && t[1] == '/')
  178. t += 2; /* strip redundant leading "./" sections */
  179. /* Make changes, if any, to the copied name (leave original intact) */
  180. if (!pathput)
  181. t = last(t, PATH_END);
  182. /* Malloc space for internal name and copy it */
  183. if ((n = malloc(strlen(t) + 1)) == NULL)
  184. return NULL;
  185. strcpy(n, t);
  186. if (isdir == 42) return n; /* avoid warning on unused variable */
  187. if (dosify)
  188. msname(n);
  189. /* Returned malloc'ed name */
  190. if (pdosflag)
  191. *pdosflag = dosflag;
  192. return n;
  193. }
  194. char *in2ex(n)
  195. char *n; /* internal file name */
  196. /* Convert the zip file name to an external file name, returning the malloc'ed
  197. string or NULL if not enough memory. */
  198. {
  199. char *x; /* external file name */
  200. if ((x = malloc(strlen(n) + 1 + PAD)) == NULL)
  201. return NULL;
  202. strcpy(x, n);
  203. return x;
  204. }
  205. /*
  206. * XXX use ztimbuf in both POSIX and non POSIX cases ?
  207. */
  208. void stamp(f, d)
  209. char *f; /* name of file to change */
  210. ulg d; /* dos-style time to change it to */
  211. /* Set last updated and accessed time of file f to the DOS time d. */
  212. {
  213. #ifdef _POSIX_VERSION
  214. struct utimbuf u; /* argument for utime() const ?? */
  215. #else
  216. time_t u[2]; /* argument for utime() */
  217. #endif
  218. /* Convert DOS time to time_t format in u */
  219. #ifdef _POSIX_VERSION
  220. u.actime = u.modtime = dos2unixtime(d);
  221. utime(f, &u);
  222. #else
  223. u[0] = u[1] = dos2unixtime(d);
  224. utime(f, u);
  225. #endif
  226. }
  227. ulg filetime(f, a, n, t)
  228. char *f; /* name of file to get info on */
  229. ulg *a; /* return value: file attributes */
  230. long *n; /* return value: file size */
  231. iztimes *t; /* return value: access, modific. and creation times */
  232. /* If file *f does not exist, return 0. Else, return the file's last
  233. modified date and time as an MSDOS date and time. The date and
  234. time is returned in a long with the date most significant to allow
  235. unsigned integer comparison of absolute times. Also, if a is not
  236. a NULL pointer, store the file attributes there, with the high two
  237. bytes being the Unix attributes, and the low byte being a mapping
  238. of that to DOS attributes. If n is not NULL, store the file size
  239. there. If t is not NULL, the file's access, modification and creation
  240. times are stored there as UNIX time_t values.
  241. If f is "-", use standard input as the file. If f is a device, return
  242. a file size of -1 */
  243. {
  244. struct stat s; /* results of stat() */
  245. /* convert FNAMX to malloc - 11/8/04 EG */
  246. char *name;
  247. int len = strlen(f);
  248. if (f == label) {
  249. if (a != NULL)
  250. *a = label_mode;
  251. if (n != NULL)
  252. *n = -2L; /* convention for a label name */
  253. if (t != NULL)
  254. t->atime = t->mtime = t->ctime = label_utim;
  255. return label_time;
  256. }
  257. if ((name = malloc(len + 1)) == NULL) {
  258. ZIPERR(ZE_MEM, "filetime");
  259. }
  260. strcpy(name, f);
  261. if (name[len - 1] == '/')
  262. name[len - 1] = '\0';
  263. /* not all systems allow stat'ing a file with / appended */
  264. if (strcmp(f, "-") == 0) {
  265. if (fstat(fileno(stdin), &s) != 0) {
  266. free(name);
  267. error("fstat(stdin)");
  268. }
  269. } else if (LSSTAT(name, &s) != 0) {
  270. /* Accept about any file kind including directories
  271. * (stored with trailing / with -r option)
  272. */
  273. free(name);
  274. return 0;
  275. }
  276. free(name);
  277. if (a != NULL) {
  278. *a = ((ulg)s.st_mode << 16) | !(s.st_mode & S_IWRITE);
  279. if ((s.st_mode & S_IFMT) == S_IFDIR) {
  280. *a |= MSDOS_DIR_ATTR;
  281. }
  282. }
  283. if (n != NULL)
  284. *n = (s.st_mode & S_IFMT) == S_IFREG ? s.st_size : -1L;
  285. if (t != NULL) {
  286. t->atime = s.st_atime;
  287. t->mtime = s.st_mtime;
  288. t->ctime = s.st_mtime; /* best guess (s.st_ctime: last status change!) */
  289. }
  290. return unix2dostime(&s.st_mtime);
  291. }
  292. /* ----------------------------------------------------------------------
  293. Return a malloc()'d buffer containing all of the attributes and their names
  294. for the file specified in name. You have to free() this yourself. The length
  295. of the buffer is also returned.
  296. If get_attr_dir() fails, the buffer will be NULL, total_size will be 0,
  297. and an error will be returned:
  298. EOK - no errors occurred
  299. EINVAL - attr_buff was pointing at a buffer
  300. ENOMEM - insufficient memory for attribute buffer
  301. Other errors are possible (whatever is returned by the fs_attr.h functions).
  302. PROBLEMS:
  303. - pointers are 32-bits; attributes are limited to off_t in size so it's
  304. possible to overflow... in practice, this isn't too likely... your
  305. machine will thrash like hell before that happens
  306. */
  307. #define INITIAL_BUFF_SIZE 65536
  308. int get_attr_dir( const char *name, char **attr_buff, off_t *total_size )
  309. {
  310. int retval = EOK;
  311. int fd;
  312. DIR *fa_dir;
  313. struct dirent *fa_ent;
  314. off_t attrs_size;
  315. off_t this_size;
  316. char *ptr;
  317. struct attr_info fa_info;
  318. struct attr_info big_fa_info;
  319. retval = EOK;
  320. attrs_size = 0; /* gcc still says this is used uninitialized... */
  321. *total_size = 0;
  322. /* ----------------------------------------------------------------- */
  323. /* Sanity-check. */
  324. if( *attr_buff != NULL ) {
  325. return EINVAL;
  326. }
  327. /* ----------------------------------------------------------------- */
  328. /* Can we open the file/directory? */
  329. /* */
  330. /* linkput is a zip global; it's set to 1 if we're storing symbolic */
  331. /* links as symbolic links (instead of storing the thing the link */
  332. /* points to)... if we're storing the symbolic link as a link, we'll */
  333. /* want the link's file attributes, otherwise we want the target's. */
  334. if( linkput ) {
  335. fd = open( name, O_RDONLY | O_NOTRAVERSE );
  336. } else {
  337. fd = open( name, O_RDONLY );
  338. }
  339. if( fd < 0 ) {
  340. return errno;
  341. }
  342. /* ----------------------------------------------------------------- */
  343. /* Allocate an initial buffer; 64k should usually be enough. */
  344. *attr_buff = (char *)malloc( INITIAL_BUFF_SIZE );
  345. ptr = *attr_buff;
  346. if( ptr == NULL ) {
  347. close( fd );
  348. return ENOMEM;
  349. }
  350. /* ----------------------------------------------------------------- */
  351. /* Open the attributes directory for this file. */
  352. fa_dir = fs_fopen_attr_dir( fd );
  353. if( fa_dir == NULL ) {
  354. close( fd );
  355. free( ptr );
  356. *attr_buff = NULL;
  357. return retval;
  358. }
  359. /* ----------------------------------------------------------------- */
  360. /* Read all the attributes; the buffer could grow > 64K if there are */
  361. /* many and/or they are large. */
  362. fa_ent = fs_read_attr_dir( fa_dir );
  363. while( fa_ent != NULL ) {
  364. retval = fs_stat_attr( fd, fa_ent->d_name, &fa_info );
  365. /* TODO: check retval != EOK */
  366. this_size = strlen( fa_ent->d_name ) + 1;
  367. this_size += sizeof( struct attr_info );
  368. this_size += fa_info.size;
  369. attrs_size += this_size;
  370. if( attrs_size > INITIAL_BUFF_SIZE ) {
  371. unsigned long offset = ptr - *attr_buff;
  372. *attr_buff = (char *)realloc( *attr_buff, attrs_size );
  373. if( *attr_buff == NULL ) {
  374. retval = fs_close_attr_dir( fa_dir );
  375. /* TODO: check retval != EOK */
  376. close( fd );
  377. return ENOMEM;
  378. }
  379. ptr = *attr_buff + offset;
  380. }
  381. /* Now copy the data for this attribute into the buffer. */
  382. strcpy( ptr, fa_ent->d_name );
  383. ptr += strlen( fa_ent->d_name );
  384. *ptr++ = '\0';
  385. /* We need to put a big-endian version of the fa_info data into */
  386. /* the archive. */
  387. big_fa_info.type = B_HOST_TO_BENDIAN_INT32( fa_info.type );
  388. big_fa_info.size = B_HOST_TO_BENDIAN_INT64( fa_info.size );
  389. memcpy( ptr, &big_fa_info, sizeof( struct attr_info ) );
  390. ptr += sizeof( struct attr_info );
  391. if( fa_info.size > 0 ) {
  392. ssize_t read_bytes;
  393. read_bytes = fs_read_attr( fd, fa_ent->d_name, fa_info.type, 0,
  394. ptr, fa_info.size );
  395. if( read_bytes != fa_info.size ) {
  396. /* print a warning about mismatched sizes */
  397. char buff[80];
  398. sprintf( buff, "read %ld, expected %ld",
  399. (ssize_t)read_bytes, (ssize_t)fa_info.size );
  400. zipwarn( "attribute size mismatch: ", buff );
  401. }
  402. /* Wave my magic wand... this swaps all the Be types to big- */
  403. /* endian automagically. */
  404. (void)swap_data( fa_info.type, ptr, fa_info.size,
  405. B_SWAP_HOST_TO_BENDIAN );
  406. ptr += fa_info.size;
  407. }
  408. fa_ent = fs_read_attr_dir( fa_dir );
  409. }
  410. /* ----------------------------------------------------------------- */
  411. /* Close the attribute directory. */
  412. retval = fs_close_attr_dir( fa_dir );
  413. /* TODO: check retval != EOK */
  414. /* ----------------------------------------------------------------- */
  415. /* If the buffer is too big, shrink it. */
  416. if( attrs_size < INITIAL_BUFF_SIZE ) {
  417. *attr_buff = (char *)realloc( *attr_buff, attrs_size );
  418. if( *attr_buff == NULL ) {
  419. /* This really shouldn't happen... */
  420. close( fd );
  421. return ENOMEM;
  422. }
  423. }
  424. *total_size = attrs_size;
  425. close( fd );
  426. return EOK;
  427. }
  428. /* ---------------------------------------------------------------------- */
  429. /* Add a 'UT' extra field to the zlist data pointed to by z. */
  430. #define EB_L_UT_SIZE (EB_HEADSIZE + EB_UT_LEN(2))
  431. #define EB_C_UT_SIZE (EB_HEADSIZE + EB_UT_LEN(1))
  432. local int add_UT_ef( struct zlist far *z )
  433. {
  434. char *l_ef = NULL;
  435. char *c_ef = NULL;
  436. struct stat s;
  437. #ifdef IZ_CHECK_TZ
  438. if (!zp_tz_is_valid)
  439. return ZE_OK; /* skip silently if no valid TZ info */
  440. #endif
  441. /* We can't work if there's no entry to work on. */
  442. if( z == NULL ) {
  443. return ZE_LOGIC;
  444. }
  445. /* Check to make sure we've got enough room in the extra fields. */
  446. if( z->ext + EB_L_UT_SIZE > USHRT_MAX ||
  447. z->cext + EB_C_UT_SIZE > USHRT_MAX ) {
  448. return ZE_MEM;
  449. }
  450. /* stat() the file (or the symlink) to get the data; if we can't get */
  451. /* the data, there's no point in trying to fill out the fields. */
  452. if(LSSTAT( z->name, &s ) ) {
  453. return ZE_OPEN;
  454. }
  455. /* Allocate memory for the local and central extra fields. */
  456. if( z->extra && z->ext != 0 ) {
  457. l_ef = (char *)realloc( z->extra, z->ext + EB_L_UT_SIZE );
  458. } else {
  459. l_ef = (char *)malloc( EB_L_UT_SIZE );
  460. z->ext = 0;
  461. }
  462. if( l_ef == NULL ) {
  463. return ZE_MEM;
  464. }
  465. z->extra = l_ef;
  466. l_ef += z->ext;
  467. if( z->cextra && z->cext != 0 ) {
  468. c_ef = (char *)realloc( z->cextra, z->cext + EB_C_UT_SIZE );
  469. } else {
  470. c_ef = (char *)malloc( EB_C_UT_SIZE );
  471. z->cext = 0;
  472. }
  473. if( c_ef == NULL ) {
  474. return ZE_MEM;
  475. }
  476. z->cextra = c_ef;
  477. c_ef += z->cext;
  478. /* Now add the local version of the field. */
  479. *l_ef++ = 'U';
  480. *l_ef++ = 'T';
  481. *l_ef++ = (char)(EB_UT_LEN(2)); /* length of data in local EF */
  482. *l_ef++ = (char)0;
  483. *l_ef++ = (char)(EB_UT_FL_MTIME | EB_UT_FL_ATIME);
  484. *l_ef++ = (char)(s.st_mtime);
  485. *l_ef++ = (char)(s.st_mtime >> 8);
  486. *l_ef++ = (char)(s.st_mtime >> 16);
  487. *l_ef++ = (char)(s.st_mtime >> 24);
  488. *l_ef++ = (char)(s.st_atime);
  489. *l_ef++ = (char)(s.st_atime >> 8);
  490. *l_ef++ = (char)(s.st_atime >> 16);
  491. *l_ef++ = (char)(s.st_atime >> 24);
  492. z->ext += EB_L_UT_SIZE;
  493. /* Now add the central version. */
  494. memcpy(c_ef, l_ef-EB_L_UT_SIZE, EB_C_UT_SIZE);
  495. c_ef[EB_LEN] = (char)(EB_UT_LEN(1)); /* length of data in central EF */
  496. z->cext += EB_C_UT_SIZE;
  497. return ZE_OK;
  498. }
  499. /* ---------------------------------------------------------------------- */
  500. /* Add a 'Ux' extra field to the zlist data pointed to by z. */
  501. #define EB_L_UX2_SIZE (EB_HEADSIZE + EB_UX2_MINLEN)
  502. #define EB_C_UX2_SIZE (EB_HEADSIZE)
  503. local int add_Ux_ef( struct zlist far *z )
  504. {
  505. char *l_ef = NULL;
  506. char *c_ef = NULL;
  507. struct stat s;
  508. /* Check to make sure we've got enough room in the extra fields. */
  509. if( z->ext + EB_L_UX2_SIZE > USHRT_MAX ||
  510. z->cext + EB_C_UX2_SIZE > USHRT_MAX ) {
  511. return ZE_MEM;
  512. }
  513. /* stat() the file (or the symlink) to get the data; if we can't get */
  514. /* the data, there's no point in trying to fill out the fields. */
  515. if(LSSTAT( z->name, &s ) ) {
  516. return ZE_OPEN;
  517. }
  518. /* Allocate memory for the local and central extra fields. */
  519. if( z->extra && z->ext != 0 ) {
  520. l_ef = (char *)realloc( z->extra, z->ext + EB_L_UX2_SIZE );
  521. } else {
  522. l_ef = (char *)malloc( EB_L_UX2_SIZE );
  523. z->ext = 0;
  524. }
  525. if( l_ef == NULL ) {
  526. return ZE_MEM;
  527. }
  528. z->extra = l_ef;
  529. l_ef += z->ext;
  530. if( z->cextra && z->cext != 0 ) {
  531. c_ef = (char *)realloc( z->cextra, z->cext + EB_C_UX2_SIZE );
  532. } else {
  533. c_ef = (char *)malloc( EB_C_UX2_SIZE );
  534. z->cext = 0;
  535. }
  536. if( c_ef == NULL ) {
  537. return ZE_MEM;
  538. }
  539. z->cextra = c_ef;
  540. c_ef += z->cext;
  541. /* Now add the local version of the field. */
  542. *l_ef++ = 'U';
  543. *l_ef++ = 'x';
  544. *l_ef++ = (char)(EB_UX2_MINLEN);
  545. *l_ef++ = (char)(EB_UX2_MINLEN >> 8);
  546. *l_ef++ = (char)(s.st_uid);
  547. *l_ef++ = (char)(s.st_uid >> 8);
  548. *l_ef++ = (char)(s.st_gid);
  549. *l_ef++ = (char)(s.st_gid >> 8);
  550. z->ext += EB_L_UX2_SIZE;
  551. /* Now add the central version of the field. */
  552. *c_ef++ = 'U';
  553. *c_ef++ = 'x';
  554. *c_ef++ = 0;
  555. *c_ef++ = 0;
  556. z->cext += EB_C_UX2_SIZE;
  557. return ZE_OK;
  558. }
  559. /* ---------------------------------------------------------------------- */
  560. /* Add a 'Be' extra field to the zlist data pointed to by z. */
  561. #define EB_L_BE_SIZE (EB_HEADSIZE + EB_L_BE_LEN) /* + attr size */
  562. #define EB_C_BE_SIZE (EB_HEADSIZE + EB_C_BE_LEN)
  563. /* maximum memcompress overhead is the sum of the compression header length */
  564. /* (6 = ush compression type, ulg CRC) and the worstcase deflate overhead */
  565. /* when uncompressible data are kept in 2 "stored" blocks (5 per block = */
  566. /* byte blocktype + 2 * ush blocklength) */
  567. #define MEMCOMPRESS_OVERHEAD (EB_MEMCMPR_HSIZ + EB_DEFLAT_EXTRA)
  568. local int add_Be_ef( struct zlist far *z )
  569. {
  570. char *l_ef = NULL;
  571. char *c_ef = NULL;
  572. char *attrbuff = NULL;
  573. off_t attrsize = 0;
  574. char *compbuff = NULL;
  575. ush compsize = 0;
  576. uch flags = 0;
  577. /* Check to make sure we've got enough room in the extra fields. */
  578. if( z->ext + EB_L_BE_SIZE > USHRT_MAX ||
  579. z->cext + EB_C_BE_SIZE > USHRT_MAX ) {
  580. return ZE_MEM;
  581. }
  582. /* Attempt to load up a buffer full of the file's attributes. */
  583. {
  584. int retval;
  585. retval = get_attr_dir( z->name, &attrbuff, &attrsize );
  586. if( retval != EOK ) {
  587. return ZE_OPEN;
  588. }
  589. if( attrsize == 0 ) {
  590. return ZE_OK;
  591. }
  592. if( attrbuff == NULL ) {
  593. return ZE_LOGIC;
  594. }
  595. /* Check for way too much data. */
  596. if( attrsize > (off_t)ULONG_MAX ) {
  597. zipwarn( "uncompressed attributes truncated", "" );
  598. attrsize = (off_t)(ULONG_MAX - MEMCOMPRESS_OVERHEAD);
  599. }
  600. }
  601. if( verbose ) {
  602. printf( "\t[in=%lu]", (unsigned long)attrsize );
  603. }
  604. /* Try compressing the data */
  605. compbuff = (char *)malloc( (size_t)attrsize + MEMCOMPRESS_OVERHEAD );
  606. if( compbuff == NULL ) {
  607. return ZE_MEM;
  608. }
  609. compsize = memcompress( compbuff,
  610. (size_t)attrsize + MEMCOMPRESS_OVERHEAD,
  611. attrbuff,
  612. (size_t)attrsize );
  613. if( verbose ) {
  614. printf( " [out=%u]", compsize );
  615. }
  616. /* Attempt to optimise very small attributes. */
  617. if( compsize > attrsize ) {
  618. free( compbuff );
  619. compsize = (ush)attrsize;
  620. compbuff = attrbuff;
  621. flags = EB_BE_FL_NATURAL;
  622. }
  623. /* Check to see if we really have enough room in the EF for the data. */
  624. if( ( z->ext + compsize + EB_L_BE_LEN ) > USHRT_MAX ) {
  625. compsize = USHRT_MAX - EB_L_BE_LEN - z->ext;
  626. }
  627. /* Allocate memory for the local and central extra fields. */
  628. if( z->extra && z->ext != 0 ) {
  629. l_ef = (char *)realloc( z->extra, z->ext + EB_L_BE_SIZE + compsize );
  630. } else {
  631. l_ef = (char *)malloc( EB_L_BE_SIZE + compsize );
  632. z->ext = 0;
  633. }
  634. if( l_ef == NULL ) {
  635. return ZE_MEM;
  636. }
  637. z->extra = l_ef;
  638. l_ef += z->ext;
  639. if( z->cextra && z->cext != 0 ) {
  640. c_ef = (char *)realloc( z->cextra, z->cext + EB_C_BE_SIZE );
  641. } else {
  642. c_ef = (char *)malloc( EB_C_BE_SIZE );
  643. z->cext = 0;
  644. }
  645. if( c_ef == NULL ) {
  646. return ZE_MEM;
  647. }
  648. z->cextra = c_ef;
  649. c_ef += z->cext;
  650. /* Now add the local version of the field. */
  651. *l_ef++ = 'B';
  652. *l_ef++ = 'e';
  653. *l_ef++ = (char)(compsize + EB_L_BE_LEN);
  654. *l_ef++ = (char)((compsize + EB_L_BE_LEN) >> 8);
  655. *l_ef++ = (char)((unsigned long)attrsize);
  656. *l_ef++ = (char)((unsigned long)attrsize >> 8);
  657. *l_ef++ = (char)((unsigned long)attrsize >> 16);
  658. *l_ef++ = (char)((unsigned long)attrsize >> 24);
  659. *l_ef++ = flags;
  660. memcpy( l_ef, compbuff, (size_t)compsize );
  661. z->ext += EB_L_BE_SIZE + compsize;
  662. /* And the central version. */
  663. *c_ef++ = 'B';
  664. *c_ef++ = 'e';
  665. *c_ef++ = (char)(EB_C_BE_LEN);
  666. *c_ef++ = (char)(EB_C_BE_LEN >> 8);
  667. *c_ef++ = (char)compsize;
  668. *c_ef++ = (char)(compsize >> 8);
  669. *c_ef++ = (char)(compsize >> 16);
  670. *c_ef++ = (char)(compsize >> 24);
  671. *c_ef++ = flags;
  672. z->cext += EB_C_BE_SIZE;
  673. return ZE_OK;
  674. }
  675. /* Extra field info:
  676. - 'UT' - UNIX time extra field
  677. - 'Ux' - UNIX uid/gid extra field
  678. - 'Be' - BeOS file attributes extra field
  679. This is done the same way ../unix/unix.c stores the 'UT'/'Ux' fields
  680. (full data in local header, only modification time in central header),
  681. with the 'Be' field added to the end and the size of the 'Be' field
  682. in the central header.
  683. See the end of beos/osdep.h for a simple explanation of the 'Be' EF
  684. layout.
  685. */
  686. int set_extra_field(z, z_utim)
  687. struct zlist far *z;
  688. iztimes *z_utim;
  689. /* store full data in local header but just modification time stamp info
  690. in central header */
  691. {
  692. int retval;
  693. /* Tell picky compilers to shut up about unused variables. */
  694. z_utim = z_utim;
  695. /* Check to make sure z is valid. */
  696. if( z == NULL ) {
  697. return ZE_LOGIC;
  698. }
  699. /* This function is much simpler now that I've moved the extra fields */
  700. /* out... it simplified the 'Be' code, too. */
  701. retval = add_UT_ef( z );
  702. if( retval != ZE_OK ) {
  703. return retval;
  704. }
  705. retval = add_Ux_ef( z );
  706. if( retval != ZE_OK ) {
  707. return retval;
  708. }
  709. retval = add_Be_ef( z );
  710. if( retval != ZE_OK ) {
  711. return retval;
  712. }
  713. return ZE_OK;
  714. }
  715. /* ---------------------------------------------------------------------- */
  716. /* Set a file's MIME type. */
  717. void setfiletype( const char *file, const char *type )
  718. {
  719. int fd;
  720. attr_info fa;
  721. ssize_t wrote_bytes;
  722. fd = open( file, O_RDWR );
  723. if( fd < 0 ) {
  724. zipwarn( "can't open zipfile to write file type", "" );
  725. return;
  726. }
  727. fa.type = B_MIME_STRING_TYPE;
  728. fa.size = (off_t)(strlen( type ) + 1);
  729. wrote_bytes = fs_write_attr( fd, BE_FILE_TYPE_NAME, fa.type, 0,
  730. type, fa.size );
  731. if( wrote_bytes != (ssize_t)fa.size ) {
  732. zipwarn( "couldn't write complete file type", "" );
  733. }
  734. close( fd );
  735. }
  736. int deletedir(d)
  737. char *d; /* directory to delete */
  738. /* Delete the directory *d if it is empty, do nothing otherwise.
  739. Return the result of rmdir(), delete(), or system().
  740. For VMS, d must be in format [x.y]z.dir;1 (not [x.y.z]).
  741. */
  742. {
  743. # ifdef NO_RMDIR
  744. /* code from Greg Roelofs, who horked it from Mark Edwards (unzip) */
  745. int r, len;
  746. char *s; /* malloc'd string for system command */
  747. len = strlen(d);
  748. if ((s = malloc(len + 34)) == NULL)
  749. return 127;
  750. sprintf(s, "IFS=\" \t\n\" /bin/rmdir %s 2>/dev/null", d);
  751. r = system(s);
  752. free(s);
  753. return r;
  754. # else /* !NO_RMDIR */
  755. return rmdir(d);
  756. # endif /* ?NO_RMDIR */
  757. }
  758. #endif /* !UTIL */
  759. /******************************/
  760. /* Function version_local() */
  761. /******************************/
  762. void version_local()
  763. {
  764. static ZCONST char CompiledWith[] = "Compiled with %s%s for %s%s%s%s.\n\n";
  765. printf(CompiledWith,
  766. #ifdef __MWERKS__
  767. "Metrowerks CodeWarrior", "",
  768. #else
  769. # ifdef __GNUC__
  770. "gcc ", __VERSION__,
  771. # endif
  772. #endif
  773. "BeOS",
  774. #ifdef __POWERPC__
  775. " (PowerPC)",
  776. #else
  777. # ifdef __INTEL__
  778. " (x86)",
  779. # else
  780. " (UNKNOWN!)",
  781. # endif
  782. #endif
  783. #ifdef __DATE__
  784. " on ", __DATE__
  785. #else
  786. "", ""
  787. #endif
  788. );
  789. } /* end function version_local() */