atheos.c 26 KB

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