beos.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545
  1. /*
  2. Copyright (c) 1990-2009 Info-ZIP. All rights reserved.
  3. See the accompanying file LICENSE, version 2009-Jan-02 or later
  4. (the contents of which are also included in unzip.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. beos.c
  10. BeOS-specific routines for use with Info-ZIP's UnZip 5.30 and later.
  11. (based on unix/unix.c)
  12. Contains: do_wild() <-- generic enough to put in fileio.c?
  13. mapattr()
  14. mapname()
  15. checkdir()
  16. close_outfile()
  17. defer_dir_attribs()
  18. set_direc_attribs()
  19. stamp_file()
  20. version()
  21. scanBeOSexfield()
  22. set_file_attrs()
  23. setBeOSexfield()
  24. printBeOSexfield()
  25. assign_MIME()
  26. ---------------------------------------------------------------------------*/
  27. #define UNZIP_INTERNAL
  28. #include "unzip.h"
  29. #include "beos.h"
  30. #include <errno.h> /* Just make sure we've got a few things... */
  31. #include <sys/types.h>
  32. #include <sys/stat.h>
  33. #include <fcntl.h>
  34. #include <dirent.h>
  35. /* For the new post-DR8 file attributes */
  36. #include <kernel/fs_attr.h>
  37. #include <support/byteorder.h>
  38. #include <storage/Mime.h>
  39. static unsigned filtattr OF((__GPRO__ unsigned perms));
  40. static uch *scanBeOSexfield OF((const uch *ef_ptr, unsigned ef_len));
  41. static int set_file_attrs( const char *, const unsigned char *, const off_t );
  42. static void setBeOSexfield OF((const char *path, uch *extra_field));
  43. #ifdef BEOS_USE_PRINTEXFIELD
  44. static void printBeOSexfield OF((int isdir, uch *extra_field));
  45. #endif
  46. #ifdef BEOS_ASSIGN_FILETYPE
  47. static void assign_MIME( const char * );
  48. #endif
  49. #ifdef SET_DIR_ATTRIB
  50. typedef struct uxdirattr { /* struct for holding unix style directory */
  51. struct uxdirattr *next; /* info until can be sorted and set at end */
  52. char *fn; /* filename of directory */
  53. union {
  54. iztimes t3; /* mtime, atime, ctime */
  55. ztimbuf t2; /* modtime, actime */
  56. } u;
  57. unsigned perms; /* same as min_info.file_attr */
  58. int have_uidgid; /* flag */
  59. ulg uidgid[2];
  60. char fnbuf[1]; /* buffer stub for directory name */
  61. } uxdirattr;
  62. #define UxAtt(d) ((uxdirattr *)d) /* typecast shortcut */
  63. #endif /* SET_DIR_ATTRIB */
  64. #ifdef ACORN_FTYPE_NFS
  65. /* Acorn bits for NFS filetyping */
  66. typedef struct {
  67. uch ID[2];
  68. uch size[2];
  69. uch ID_2[4];
  70. uch loadaddr[4];
  71. uch execaddr[4];
  72. uch attr[4];
  73. } RO_extra_block;
  74. #endif /* ACORN_FTYPE_NFS */
  75. /* static int created_dir; */ /* used in mapname(), checkdir() */
  76. /* static int renamed_fullpath; */ /* ditto */
  77. /*****************************/
  78. /* Strings used multiple */
  79. /* times in beos.c */
  80. /*****************************/
  81. /* messages of code for setting file/directory attributes */
  82. static ZCONST char CannotSetItemUidGid[] =
  83. "warning: cannot set UID %lu and/or GID %lu for %s\n %s\n";
  84. static ZCONST char CannotSetUidGid[] =
  85. " (warning) cannot set UID %lu and/or GID %lu\n %s";
  86. static ZCONST char CannotSetItemTimestamps[] =
  87. "warning: cannot set modif./access times for %s\n %s\n";
  88. static ZCONST char CannotSetTimestamps[] =
  89. " (warning) cannot set modif./access times\n %s";
  90. #ifndef SFX
  91. /**********************/
  92. /* Function do_wild() */ /* for porting: dir separator; match(ignore_case) */
  93. /**********************/
  94. char *do_wild(__G__ wildspec)
  95. __GDEF
  96. ZCONST char *wildspec; /* only used first time on a given dir */
  97. {
  98. /* these statics are now declared in SYSTEM_SPECIFIC_GLOBALS in beocfg.h:
  99. static DIR *wild_dir = (DIR *)NULL;
  100. static ZCONST char *wildname;
  101. static char *dirname, matchname[FILNAMSIZ];
  102. static int notfirstcall=FALSE, have_dirname, dirnamelen;
  103. */
  104. struct dirent *file;
  105. /* Even when we're just returning wildspec, we *always* do so in
  106. * matchname[]--calling routine is allowed to append four characters
  107. * to the returned string, and wildspec may be a pointer to argv[].
  108. */
  109. if (!G.notfirstcall) { /* first call: must initialize everything */
  110. G.notfirstcall = TRUE;
  111. if (!iswild(wildspec)) {
  112. strncpy(G.matchname, wildspec, FILNAMSIZ);
  113. G.matchname[FILNAMSIZ-1] = '\0';
  114. G.have_dirname = FALSE;
  115. G.wild_dir = NULL;
  116. return G.matchname;
  117. }
  118. /* break the wildspec into a directory part and a wildcard filename */
  119. if ((G.wildname = (ZCONST char *)strrchr(wildspec, '/')) == NULL) {
  120. G.dirname = ".";
  121. G.dirnamelen = 1;
  122. G.have_dirname = FALSE;
  123. G.wildname = wildspec;
  124. } else {
  125. ++G.wildname; /* point at character after '/' */
  126. G.dirnamelen = G.wildname - wildspec;
  127. if ((G.dirname = (char *)malloc(G.dirnamelen+1)) == (char *)NULL) {
  128. Info(slide, 0x201, ((char *)slide,
  129. "warning: cannot allocate wildcard buffers\n"));
  130. strncpy(G.matchname, wildspec, FILNAMSIZ);
  131. G.matchname[FILNAMSIZ-1] = '\0';
  132. return G.matchname; /* but maybe filespec was not a wildcard */
  133. }
  134. strncpy(G.dirname, wildspec, G.dirnamelen);
  135. G.dirname[G.dirnamelen] = '\0'; /* terminate for strcpy below */
  136. G.have_dirname = TRUE;
  137. }
  138. if ((G.wild_dir = (zvoid *)opendir(G.dirname)) != (zvoid *)NULL) {
  139. while ((file = readdir((DIR *)G.wild_dir)) !=
  140. (struct dirent *)NULL) {
  141. Trace((stderr, "do_wild: readdir returns %s\n",
  142. FnFilter1(file->d_name)));
  143. if (file->d_name[0] == '.' && G.wildname[0] != '.')
  144. continue; /* Unix: '*' and '?' do not match leading dot */
  145. if (match(file->d_name, G.wildname, 0 WISEP) &&/*0=case sens.*/
  146. /* skip "." and ".." directory entries */
  147. strcmp(file->d_name, ".") && strcmp(file->d_name, "..")) {
  148. Trace((stderr, "do_wild: match() succeeds\n"));
  149. if (G.have_dirname) {
  150. strcpy(G.matchname, G.dirname);
  151. strcpy(G.matchname+G.dirnamelen, file->d_name);
  152. } else
  153. strcpy(G.matchname, file->d_name);
  154. return G.matchname;
  155. }
  156. }
  157. /* if we get to here directory is exhausted, so close it */
  158. closedir((DIR *)G.wild_dir);
  159. G.wild_dir = (zvoid *)NULL;
  160. }
  161. Trace((stderr, "do_wild: opendir(%s) returns NULL\n",
  162. FnFilter1(G.dirname)));
  163. /* return the raw wildspec in case that works (e.g., directory not
  164. * searchable, but filespec was not wild and file is readable) */
  165. strncpy(G.matchname, wildspec, FILNAMSIZ);
  166. G.matchname[FILNAMSIZ-1] = '\0';
  167. return G.matchname;
  168. }
  169. /* last time through, might have failed opendir but returned raw wildspec */
  170. if ((DIR *)G.wild_dir == (DIR *)NULL) {
  171. G.notfirstcall = FALSE; /* nothing left--reset for new wildspec */
  172. if (G.have_dirname)
  173. free(G.dirname);
  174. return (char *)NULL;
  175. }
  176. /* If we've gotten this far, we've read and matched at least one entry
  177. * successfully (in a previous call), so dirname has been copied into
  178. * matchname already.
  179. */
  180. while ((file = readdir((DIR *)G.wild_dir)) != (struct dirent *)NULL) {
  181. Trace((stderr, "do_wild: readdir returns %s\n",
  182. FnFilter1(file->d_name)));
  183. if (file->d_name[0] == '.' && G.wildname[0] != '.')
  184. continue; /* Unix: '*' and '?' do not match leading dot */
  185. if (match(file->d_name, G.wildname, 0 WISEP)) { /* 0 == case sens. */
  186. Trace((stderr, "do_wild: match() succeeds\n"));
  187. if (G.have_dirname) {
  188. /* strcpy(G.matchname, G.dirname); */
  189. strcpy(G.matchname+G.dirnamelen, file->d_name);
  190. } else
  191. strcpy(G.matchname, file->d_name);
  192. return G.matchname;
  193. }
  194. }
  195. closedir((DIR *)G.wild_dir); /* at least one entry read; nothing left */
  196. G.wild_dir = (zvoid *)NULL;
  197. G.notfirstcall = FALSE; /* reset for new wildspec */
  198. if (G.have_dirname)
  199. free(G.dirname);
  200. return (char *)NULL;
  201. } /* end function do_wild() */
  202. #endif /* !SFX */
  203. #ifndef S_ISUID
  204. # define S_ISUID 0004000 /* set user id on execution */
  205. #endif
  206. #ifndef S_ISGID
  207. # define S_ISGID 0002000 /* set group id on execution */
  208. #endif
  209. #ifndef S_ISVTX
  210. # define S_ISVTX 0001000 /* save swapped text even after use */
  211. #endif
  212. /************************/
  213. /* Function filtattr() */
  214. /************************/
  215. /* This is used to clear or keep the SUID and SGID bits on file permissions.
  216. * It's possible that a file in an archive could have one of these bits set
  217. * and, unknown to the person unzipping, could allow others to execute the
  218. * file as the user or group. The new option -K bypasses this check.
  219. */
  220. static unsigned filtattr(__G__ perms)
  221. __GDEF
  222. unsigned perms;
  223. {
  224. /* keep setuid/setgid/tacky perms? */
  225. if (!uO.K_flag)
  226. perms &= ~(S_ISUID | S_ISGID | S_ISVTX);
  227. return (0xffff & perms);
  228. } /* end function filtattr() */
  229. /**********************/
  230. /* Function mapattr() */
  231. /**********************/
  232. int mapattr(__G)
  233. __GDEF
  234. {
  235. int r;
  236. ulg tmp = G.crec.external_file_attributes;
  237. G.pInfo->file_attr = 0;
  238. /* initialized to 0 for check in "default" branch below... */
  239. switch (G.pInfo->hostnum) {
  240. case AMIGA_:
  241. tmp = (unsigned)(tmp>>17 & 7); /* Amiga RWE bits */
  242. G.pInfo->file_attr = (unsigned)(tmp<<6 | tmp<<3 | tmp);
  243. break;
  244. case THEOS_:
  245. tmp &= 0xF1FFFFFFL;
  246. if ((tmp & 0xF0000000L) != 0x40000000L)
  247. tmp &= 0x01FFFFFFL; /* not a dir, mask all ftype bits */
  248. else
  249. tmp &= 0x41FFFFFFL; /* leave directory bit as set */
  250. /* fall through! */
  251. case BEOS_:
  252. case UNIX_:
  253. case VMS_:
  254. case ACORN_:
  255. case ATARI_:
  256. case ATHEOS_:
  257. case QDOS_:
  258. case TANDEM_:
  259. r = FALSE;
  260. G.pInfo->file_attr = (unsigned)(tmp >> 16);
  261. if (G.pInfo->file_attr == 0 && G.extra_field) {
  262. /* Some (non-Info-ZIP) implementations of Zip for Unix and
  263. * VMS (and probably others ??) leave 0 in the upper 16-bit
  264. * part of the external_file_attributes field. Instead, they
  265. * store file permission attributes in some extra field.
  266. * As a work-around, we search for the presence of one of
  267. * these extra fields and fall back to the MSDOS compatible
  268. * part of external_file_attributes if one of the known
  269. * e.f. types has been detected.
  270. * Later, we might implement extraction of the permission
  271. * bits from the VMS extra field. But for now, the work-around
  272. * should be sufficient to provide "readable" extracted files.
  273. * (For ASI Unix e.f., an experimental remap of the e.f.
  274. * mode value IS already provided!)
  275. */
  276. ush ebID;
  277. unsigned ebLen;
  278. uch *ef = G.extra_field;
  279. unsigned ef_len = G.crec.extra_field_length;
  280. while (!r && ef_len >= EB_HEADSIZE) {
  281. ebID = makeword(ef);
  282. ebLen = (unsigned)makeword(ef+EB_LEN);
  283. if (ebLen > (ef_len - EB_HEADSIZE))
  284. /* discoverd some e.f. inconsistency! */
  285. break;
  286. switch (ebID) {
  287. case EF_ASIUNIX:
  288. if (ebLen >= (EB_ASI_MODE+2)) {
  289. G.pInfo->file_attr =
  290. (unsigned)makeword(ef+(EB_HEADSIZE+EB_ASI_MODE));
  291. /* force stop of loop: */
  292. ef_len = (ebLen + EB_HEADSIZE);
  293. break;
  294. }
  295. /* else: fall through! */
  296. case EF_PKVMS:
  297. /* "found nondecypherable e.f. with perm. attr" */
  298. r = TRUE;
  299. default:
  300. break;
  301. }
  302. ef_len -= (ebLen + EB_HEADSIZE);
  303. ef += (ebLen + EB_HEADSIZE);
  304. }
  305. }
  306. if (!r) {
  307. #ifdef SYMLINKS
  308. /* Check if the file is a (POSIX-compatible) symbolic link.
  309. * We restrict symlink support to those "made-by" hosts that
  310. * are known to support symbolic links.
  311. */
  312. G.pInfo->symlink = S_ISLNK(G.pInfo->file_attr) &&
  313. SYMLINK_HOST(G.pInfo->hostnum);
  314. #endif
  315. return 0;
  316. }
  317. /* fall through! */
  318. /* all remaining cases: expand MSDOS read-only bit into write perms */
  319. case FS_FAT_:
  320. /* PKWARE's PKZip for Unix marks entries as FS_FAT_, but stores the
  321. * Unix attributes in the upper 16 bits of the external attributes
  322. * field, just like Info-ZIP's Zip for Unix. We try to use that
  323. * value, after a check for consistency with the MSDOS attribute
  324. * bits (see below).
  325. */
  326. G.pInfo->file_attr = (unsigned)(tmp >> 16);
  327. /* fall through! */
  328. case FS_HPFS_:
  329. case FS_NTFS_:
  330. case MAC_:
  331. case TOPS20_:
  332. default:
  333. /* Ensure that DOS subdir bit is set when the entry's name ends
  334. * in a '/'. Some third-party Zip programs fail to set the subdir
  335. * bit for directory entries.
  336. */
  337. if ((tmp & 0x10) == 0) {
  338. extent fnlen = strlen(G.filename);
  339. if (fnlen > 0 && G.filename[fnlen-1] == '/')
  340. tmp |= 0x10;
  341. }
  342. /* read-only bit --> write perms; subdir bit --> dir exec bit */
  343. tmp = !(tmp & 1) << 1 | (tmp & 0x10) >> 4;
  344. if ((G.pInfo->file_attr & 0700) == (unsigned)(0400 | tmp<<6)) {
  345. /* keep previous G.pInfo->file_attr setting, when its "owner"
  346. * part appears to be consistent with DOS attribute flags!
  347. */
  348. #ifdef SYMLINKS
  349. /* Entries "made by FS_FAT_" could have been zipped on a
  350. * system that supports POSIX-style symbolic links.
  351. */
  352. G.pInfo->symlink = S_ISLNK(G.pInfo->file_attr) &&
  353. (G.pInfo->hostnum == FS_FAT_);
  354. #endif
  355. return 0;
  356. }
  357. G.pInfo->file_attr = (unsigned)(0444 | tmp<<6 | tmp<<3 | tmp);
  358. break;
  359. } /* end switch (host-OS-created-by) */
  360. /* for originating systems with no concept of "group," "other," "system": */
  361. umask( (int)(tmp=umask(0)) ); /* apply mask to expanded r/w(/x) perms */
  362. G.pInfo->file_attr &= ~tmp;
  363. return 0;
  364. } /* end function mapattr() */
  365. /************************/
  366. /* Function mapname() */
  367. /************************/
  368. int mapname(__G__ renamed)
  369. __GDEF
  370. int renamed;
  371. /*
  372. * returns:
  373. * MPN_OK - no problem detected
  374. * MPN_INF_TRUNC - caution (truncated filename)
  375. * MPN_INF_SKIP - info "skip entry" (dir doesn't exist)
  376. * MPN_ERR_SKIP - error -> skip entry
  377. * MPN_ERR_TOOLONG - error -> path is too long
  378. * MPN_NOMEM - error (memory allocation failed) -> skip entry
  379. * [also MPN_VOL_LABEL, MPN_CREATED_DIR]
  380. */
  381. {
  382. char pathcomp[FILNAMSIZ]; /* path-component buffer */
  383. char *pp, *cp=(char *)NULL; /* character pointers */
  384. char *lastsemi=(char *)NULL; /* pointer to last semi-colon in pathcomp */
  385. #ifdef ACORN_FTYPE_NFS
  386. char *lastcomma=(char *)NULL; /* pointer to last comma in pathcomp */
  387. RO_extra_block *ef_spark; /* pointer Acorn FTYPE ef block */
  388. #endif
  389. int killed_ddot = FALSE; /* is set when skipping "../" pathcomp */
  390. int error = MPN_OK;
  391. register unsigned workch; /* hold the character being tested */
  392. /*---------------------------------------------------------------------------
  393. Initialize various pointers and counters and stuff.
  394. ---------------------------------------------------------------------------*/
  395. if (G.pInfo->vollabel)
  396. return MPN_VOL_LABEL; /* can't set disk volume labels in BeOS */
  397. /* can create path as long as not just freshening, or if user told us */
  398. G.create_dirs = (!uO.fflag || renamed);
  399. G.created_dir = FALSE; /* not yet */
  400. /* user gave full pathname: don't prepend rootpath */
  401. G.renamed_fullpath = (renamed && (*G.filename == '/'));
  402. if (checkdir(__G__ (char *)NULL, INIT) == MPN_NOMEM)
  403. return MPN_NOMEM; /* initialize path buffer, unless no memory */
  404. *pathcomp = '\0'; /* initialize translation buffer */
  405. pp = pathcomp; /* point to translation buffer */
  406. if (uO.jflag) /* junking directories */
  407. cp = (char *)strrchr(G.filename, '/');
  408. if (cp == (char *)NULL) /* no '/' or not junking dirs */
  409. cp = G.filename; /* point to internal zipfile-member pathname */
  410. else
  411. ++cp; /* point to start of last component of path */
  412. /*---------------------------------------------------------------------------
  413. Begin main loop through characters in filename.
  414. ---------------------------------------------------------------------------*/
  415. while ((workch = (uch)*cp++) != 0) {
  416. switch (workch) {
  417. case '/': /* can assume -j flag not given */
  418. *pp = '\0';
  419. if (strcmp(pathcomp, ".") == 0) {
  420. /* don't bother appending "./" to the path */
  421. *pathcomp = '\0';
  422. } else if (!uO.ddotflag && strcmp(pathcomp, "..") == 0) {
  423. /* "../" dir traversal detected, skip over it */
  424. *pathcomp = '\0';
  425. killed_ddot = TRUE; /* set "show message" flag */
  426. }
  427. /* when path component is not empty, append it now */
  428. if (*pathcomp != '\0' &&
  429. ((error = checkdir(__G__ pathcomp, APPEND_DIR))
  430. & MPN_MASK) > MPN_INF_TRUNC)
  431. return error;
  432. pp = pathcomp; /* reset conversion buffer for next piece */
  433. lastsemi = (char *)NULL; /* leave direct. semi-colons alone */
  434. break;
  435. case ';': /* VMS version (or DEC-20 attrib?) */
  436. lastsemi = pp;
  437. *pp++ = ';'; /* keep for now; remove VMS ";##" */
  438. break; /* later, if requested */
  439. #ifdef ACORN_FTYPE_NFS
  440. case ',': /* NFS filetype extension */
  441. lastcomma = pp;
  442. *pp++ = ','; /* keep for now; may need to remove */
  443. break; /* later, if requested */
  444. #endif
  445. default:
  446. /* allow European characters in filenames: */
  447. if (isprint(workch) || (128 <= workch && workch <= 254))
  448. *pp++ = (char)workch;
  449. } /* end switch */
  450. } /* end while loop */
  451. /* Show warning when stripping insecure "parent dir" path components */
  452. if (killed_ddot && QCOND2) {
  453. Info(slide, 0, ((char *)slide,
  454. "warning: skipped \"../\" path component(s) in %s\n",
  455. FnFilter1(G.filename)));
  456. if (!(error & ~MPN_MASK))
  457. error = (error & MPN_MASK) | PK_WARN;
  458. }
  459. /*---------------------------------------------------------------------------
  460. Report if directory was created (and no file to create: filename ended
  461. in '/'), check name to be sure it exists, and combine path and name be-
  462. fore exiting.
  463. ---------------------------------------------------------------------------*/
  464. if (G.filename[strlen(G.filename) - 1] == '/') {
  465. checkdir(__G__ G.filename, GETPATH);
  466. if (G.created_dir) {
  467. if (QCOND2) {
  468. Info(slide, 0, ((char *)slide, " creating: %s\n",
  469. FnFilter1(G.filename)));
  470. }
  471. if (!uO.J_flag) { /* Handle the BeOS extra field if present. */
  472. void *ptr = scanBeOSexfield(G.extra_field,
  473. G.lrec.extra_field_length);
  474. if (ptr) {
  475. setBeOSexfield(G.filename, ptr);
  476. }
  477. }
  478. #ifndef NO_CHMOD
  479. /* set approx. dir perms (make sure can still read/write in dir) */
  480. if (chmod(G.filename, filtattr(__G__ G.pInfo->file_attr) | 0700))
  481. perror("chmod (directory attributes) error");
  482. #endif
  483. /* set dir time (note trailing '/') */
  484. return (error & ~MPN_MASK) | MPN_CREATED_DIR;
  485. }
  486. /* TODO: should we re-write the BeOS extra field data in case it's */
  487. /* changed? The answer is yes. [Sept 1999 - cjh] */
  488. if (!uO.J_flag) { /* Handle the BeOS extra field if present. */
  489. void *ptr = scanBeOSexfield(G.extra_field,
  490. G.lrec.extra_field_length);
  491. if (ptr) {
  492. setBeOSexfield(G.filename, ptr);
  493. }
  494. }
  495. /* dir existed already; don't look for data to extract */
  496. return (error & ~MPN_MASK) | MPN_INF_SKIP;
  497. }
  498. *pp = '\0'; /* done with pathcomp: terminate it */
  499. /* if not saving them, remove VMS version numbers (appended ";###") */
  500. if (!uO.V_flag && lastsemi) {
  501. pp = lastsemi + 1;
  502. while (isdigit((uch)(*pp)))
  503. ++pp;
  504. if (*pp == '\0') /* only digits between ';' and end: nuke */
  505. *lastsemi = '\0';
  506. }
  507. /* On UNIX (and compatible systems), "." and ".." are reserved for
  508. * directory navigation and cannot be used as regular file names.
  509. * These reserved one-dot and two-dot names are mapped to "_" and "__".
  510. */
  511. if (strcmp(pathcomp, ".") == 0)
  512. *pathcomp = '_';
  513. else if (strcmp(pathcomp, "..") == 0)
  514. strcpy(pathcomp, "__");
  515. #ifdef ACORN_FTYPE_NFS
  516. /* translate Acorn filetype information if asked to do so */
  517. if (uO.acorn_nfs_ext &&
  518. (ef_spark = (RO_extra_block *)
  519. getRISCOSexfield(G.extra_field, G.lrec.extra_field_length))
  520. != (RO_extra_block *)NULL)
  521. {
  522. /* file *must* have a RISC OS extra field */
  523. long ft = (long)makelong(ef_spark->loadaddr);
  524. /*32-bit*/
  525. if (lastcomma) {
  526. pp = lastcomma + 1;
  527. while (isxdigit((uch)(*pp))) ++pp;
  528. if (pp == lastcomma+4 && *pp == '\0') *lastcomma='\0'; /* nuke */
  529. }
  530. if ((ft & 1<<31)==0) ft=0x000FFD00;
  531. sprintf(pathcomp+strlen(pathcomp), ",%03x", (int)(ft>>8) & 0xFFF);
  532. }
  533. #endif /* ACORN_FTYPE_NFS */
  534. if (*pathcomp == '\0') {
  535. Info(slide, 1, ((char *)slide, "mapname: conversion of %s failed\n",
  536. FnFilter1(G.filename)));
  537. return (error & ~MPN_MASK) | MPN_ERR_SKIP;
  538. }
  539. checkdir(__G__ pathcomp, APPEND_NAME); /* returns 1 if truncated: care? */
  540. checkdir(__G__ G.filename, GETPATH);
  541. return error;
  542. } /* end function mapname() */
  543. /***********************/
  544. /* Function checkdir() */
  545. /***********************/
  546. int checkdir(__G__ pathcomp, flag)
  547. __GDEF
  548. char *pathcomp;
  549. int flag;
  550. /*
  551. * returns:
  552. * MPN_OK - no problem detected
  553. * MPN_INF_TRUNC - (on APPEND_NAME) truncated filename
  554. * MPN_INF_SKIP - path doesn't exist, not allowed to create
  555. * MPN_ERR_SKIP - path doesn't exist, tried to create and failed; or path
  556. * exists and is not a directory, but is supposed to be
  557. * MPN_ERR_TOOLONG - path is too long
  558. * MPN_NOMEM - can't allocate memory for filename buffers
  559. */
  560. {
  561. /* static int rootlen = 0; */ /* length of rootpath */
  562. /* static char *rootpath; */ /* user's "extract-to" directory */
  563. /* static char *buildpath; */ /* full path (so far) to extracted file */
  564. /* static char *end; */ /* pointer to end of buildpath ('\0') */
  565. # define FN_MASK 7
  566. # define FUNCTION (flag & FN_MASK)
  567. /*---------------------------------------------------------------------------
  568. APPEND_DIR: append the path component to the path being built and check
  569. for its existence. If doesn't exist and we are creating directories, do
  570. so for this one; else signal success or error as appropriate.
  571. ---------------------------------------------------------------------------*/
  572. if (FUNCTION == APPEND_DIR) {
  573. int too_long = FALSE;
  574. #ifdef SHORT_NAMES
  575. char *old_end = end;
  576. #endif
  577. Trace((stderr, "appending dir segment [%s]\n", FnFilter1(pathcomp)));
  578. while ((*G.end = *pathcomp++) != '\0')
  579. ++G.end;
  580. #ifdef SHORT_NAMES /* path components restricted to 14 chars, typically */
  581. if ((G.end-old_end) > FILENAME_MAX) /* GRR: proper constant? */
  582. *(G.end = old_end + FILENAME_MAX) = '\0';
  583. #endif
  584. /* GRR: could do better check, see if overrunning buffer as we go:
  585. * check end-buildpath after each append, set warning variable if
  586. * within 20 of FILNAMSIZ; then if var set, do careful check when
  587. * appending. Clear variable when begin new path. */
  588. /* next check: need to append '/', at least one-char name, '\0' */
  589. if ((G.end-G.buildpath) > FILNAMSIZ-3)
  590. too_long = TRUE; /* check if extracting dir? */
  591. if (SSTAT(G.buildpath, &G.statbuf)) { /* path doesn't exist */
  592. if (!G.create_dirs) { /* told not to create (freshening) */
  593. free(G.buildpath);
  594. return MPN_INF_SKIP; /* path doesn't exist: nothing to do */
  595. }
  596. if (too_long) {
  597. Info(slide, 1, ((char *)slide,
  598. "checkdir error: path too long: %s\n",
  599. FnFilter1(G.buildpath)));
  600. free(G.buildpath);
  601. /* no room for filenames: fatal */
  602. return MPN_ERR_TOOLONG;
  603. }
  604. if (mkdir(G.buildpath, 0777) == -1) { /* create the directory */
  605. Info(slide, 1, ((char *)slide,
  606. "checkdir error: cannot create %s\n\
  607. %s\n\
  608. unable to process %s.\n",
  609. FnFilter2(G.buildpath),
  610. strerror(errno),
  611. FnFilter1(G.filename)));
  612. free(G.buildpath);
  613. /* path didn't exist, tried to create, failed */
  614. return MPN_ERR_SKIP;
  615. }
  616. G.created_dir = TRUE;
  617. } else if (!S_ISDIR(G.statbuf.st_mode)) {
  618. Info(slide, 1, ((char *)slide,
  619. "checkdir error: %s exists but is not directory\n\
  620. unable to process %s.\n",
  621. FnFilter2(G.buildpath), FnFilter1(G.filename)));
  622. free(G.buildpath);
  623. /* path existed but wasn't dir */
  624. return MPN_ERR_SKIP;
  625. }
  626. if (too_long) {
  627. Info(slide, 1, ((char *)slide,
  628. "checkdir error: path too long: %s\n", FnFilter1(G.buildpath)));
  629. free(G.buildpath);
  630. /* no room for filenames: fatal */
  631. return MPN_ERR_TOOLONG;
  632. }
  633. *G.end++ = '/';
  634. *G.end = '\0';
  635. Trace((stderr, "buildpath now = [%s]\n", FnFilter1(G.buildpath)));
  636. return MPN_OK;
  637. } /* end if (FUNCTION == APPEND_DIR) */
  638. /*---------------------------------------------------------------------------
  639. GETPATH: copy full path to the string pointed at by pathcomp, and free
  640. G.buildpath.
  641. ---------------------------------------------------------------------------*/
  642. if (FUNCTION == GETPATH) {
  643. strcpy(pathcomp, G.buildpath);
  644. Trace((stderr, "getting and freeing path [%s]\n",
  645. FnFilter1(pathcomp)));
  646. free(G.buildpath);
  647. G.buildpath = G.end = (char *)NULL;
  648. return MPN_OK;
  649. }
  650. /*---------------------------------------------------------------------------
  651. APPEND_NAME: assume the path component is the filename; append it and
  652. return without checking for existence.
  653. ---------------------------------------------------------------------------*/
  654. if (FUNCTION == APPEND_NAME) {
  655. #ifdef SHORT_NAMES
  656. char *old_end = end;
  657. #endif
  658. Trace((stderr, "appending filename [%s]\n", FnFilter1(pathcomp)));
  659. while ((*G.end = *pathcomp++) != '\0') {
  660. ++G.end;
  661. #ifdef SHORT_NAMES /* truncate name at 14 characters, typically */
  662. if ((G.end-old_end) > FILENAME_MAX) /* GRR: proper constant? */
  663. *(G.end = old_end + FILENAME_MAX) = '\0';
  664. #endif
  665. if ((G.end-G.buildpath) >= FILNAMSIZ) {
  666. *--G.end = '\0';
  667. Info(slide, 0x201, ((char *)slide,
  668. "checkdir warning: path too long; truncating\n\
  669. %s\n -> %s\n",
  670. FnFilter1(G.filename), FnFilter2(G.buildpath)));
  671. return MPN_INF_TRUNC; /* filename truncated */
  672. }
  673. }
  674. Trace((stderr, "buildpath now = [%s]\n", FnFilter1(G.buildpath)));
  675. /* could check for existence here, prompt for new name... */
  676. return MPN_OK;
  677. }
  678. /*---------------------------------------------------------------------------
  679. INIT: allocate and initialize buffer space for the file currently being
  680. extracted. If file was renamed with an absolute path, don't prepend the
  681. extract-to path.
  682. ---------------------------------------------------------------------------*/
  683. /* GRR: for VMS and TOPS-20, add up to 13 to strlen */
  684. if (FUNCTION == INIT) {
  685. Trace((stderr, "initializing buildpath to "));
  686. #ifdef ACORN_FTYPE_NFS
  687. if ((G.buildpath = (char *)malloc(strlen(G.filename)+G.rootlen+
  688. (uO.acorn_nfs_ext ? 5 : 1)))
  689. #else
  690. if ((G.buildpath = (char *)malloc(strlen(G.filename)+G.rootlen+1))
  691. #endif
  692. == (char *)NULL)
  693. return MPN_NOMEM;
  694. if ((G.rootlen > 0) && !G.renamed_fullpath) {
  695. strcpy(G.buildpath, G.rootpath);
  696. G.end = G.buildpath + G.rootlen;
  697. } else {
  698. *G.buildpath = '\0';
  699. G.end = G.buildpath;
  700. }
  701. Trace((stderr, "[%s]\n", FnFilter1(G.buildpath)));
  702. return MPN_OK;
  703. }
  704. /*---------------------------------------------------------------------------
  705. ROOT: if appropriate, store the path in rootpath and create it if
  706. necessary; else assume it's a zipfile member and return. This path
  707. segment gets used in extracting all members from every zipfile specified
  708. on the command line.
  709. ---------------------------------------------------------------------------*/
  710. #if (!defined(SFX) || defined(SFX_EXDIR))
  711. if (FUNCTION == ROOT) {
  712. Trace((stderr, "initializing root path to [%s]\n",
  713. FnFilter1(pathcomp)));
  714. if (pathcomp == (char *)NULL) {
  715. G.rootlen = 0;
  716. return MPN_OK;
  717. }
  718. if (G.rootlen > 0) /* rootpath was already set, nothing to do */
  719. return MPN_OK;
  720. if ((G.rootlen = strlen(pathcomp)) > 0) {
  721. char *tmproot;
  722. if ((tmproot = (char *)malloc(G.rootlen+2)) == (char *)NULL) {
  723. G.rootlen = 0;
  724. return MPN_NOMEM;
  725. }
  726. strcpy(tmproot, pathcomp);
  727. if (tmproot[G.rootlen-1] == '/') {
  728. tmproot[--G.rootlen] = '\0';
  729. }
  730. if (G.rootlen > 0 && (SSTAT(tmproot, &G.statbuf) ||
  731. !S_ISDIR(G.statbuf.st_mode)))
  732. { /* path does not exist */
  733. if (!G.create_dirs /* || iswild(tmproot) */ ) {
  734. free(tmproot);
  735. G.rootlen = 0;
  736. /* skip (or treat as stored file) */
  737. return MPN_INF_SKIP;
  738. }
  739. /* create the directory (could add loop here scanning tmproot
  740. * to create more than one level, but why really necessary?) */
  741. if (mkdir(tmproot, 0777) == -1) {
  742. Info(slide, 1, ((char *)slide,
  743. "checkdir: cannot create extraction directory: %s\n\
  744. %s\n",
  745. FnFilter1(tmproot), strerror(errno)));
  746. free(tmproot);
  747. G.rootlen = 0;
  748. /* path didn't exist, tried to create, and failed: */
  749. /* file exists, or 2+ subdir levels required */
  750. return MPN_ERR_SKIP;
  751. }
  752. }
  753. tmproot[G.rootlen++] = '/';
  754. tmproot[G.rootlen] = '\0';
  755. if ((G.rootpath = (char *)realloc(tmproot, G.rootlen+1)) == NULL) {
  756. free(tmproot);
  757. G.rootlen = 0;
  758. return MPN_NOMEM;
  759. }
  760. Trace((stderr, "rootpath now = [%s]\n", FnFilter1(G.rootpath)));
  761. }
  762. return MPN_OK;
  763. }
  764. #endif /* !SFX || SFX_EXDIR */
  765. /*---------------------------------------------------------------------------
  766. END: free rootpath, immediately prior to program exit.
  767. ---------------------------------------------------------------------------*/
  768. if (FUNCTION == END) {
  769. Trace((stderr, "freeing rootpath\n"));
  770. if (G.rootlen > 0) {
  771. free(G.rootpath);
  772. G.rootlen = 0;
  773. }
  774. return MPN_OK;
  775. }
  776. return MPN_INVALID; /* should never reach */
  777. } /* end function checkdir() */
  778. static int get_extattribs OF((__GPRO__ iztimes *pzt, ulg z_uidgid[2]));
  779. static int get_extattribs(__G__ pzt, z_uidgid)
  780. __GDEF
  781. iztimes *pzt;
  782. ulg z_uidgid[2];
  783. {
  784. /*---------------------------------------------------------------------------
  785. Convert from MSDOS-format local time and date to Unix-format 32-bit GMT
  786. time: adjust base year from 1980 to 1970, do usual conversions from
  787. yy/mm/dd hh:mm:ss to elapsed seconds, and account for timezone and day-
  788. light savings time differences. If we have a Unix extra field, however,
  789. we're laughing: both mtime and atime are ours. On the other hand, we
  790. then have to check for restoration of UID/GID.
  791. ---------------------------------------------------------------------------*/
  792. int have_uidgid_flg;
  793. unsigned eb_izux_flg;
  794. eb_izux_flg = (G.extra_field ? ef_scan_for_izux(G.extra_field,
  795. G.lrec.extra_field_length, 0, G.lrec.last_mod_dos_datetime,
  796. #ifdef IZ_CHECK_TZ
  797. (G.tz_is_valid ? pzt : NULL),
  798. #else
  799. pzt,
  800. #endif
  801. z_uidgid) : 0);
  802. if (eb_izux_flg & EB_UT_FL_MTIME) {
  803. TTrace((stderr, "\nget_extattribs: Unix e.f. modif. time = %ld\n",
  804. pzt->mtime));
  805. } else {
  806. pzt->mtime = dos_to_unix_time(G.lrec.last_mod_dos_datetime);
  807. }
  808. if (eb_izux_flg & EB_UT_FL_ATIME) {
  809. TTrace((stderr, "get_extattribs: Unix e.f. access time = %ld\n",
  810. pzt->atime));
  811. } else {
  812. pzt->atime = pzt->mtime;
  813. TTrace((stderr, "\nget_extattribs: modification/access times = %ld\n",
  814. pzt->mtime));
  815. }
  816. /* if -X option was specified and we have UID/GID info, restore it */
  817. have_uidgid_flg =
  818. #ifdef RESTORE_UIDGID
  819. (uO.X_flag && (eb_izux_flg & EB_UX2_VALID));
  820. #else
  821. 0;
  822. #endif
  823. return have_uidgid_flg;
  824. }
  825. /****************************/
  826. /* Function close_outfile() */
  827. /****************************/
  828. void close_outfile(__G) /* GRR: change to return PK-style warning level */
  829. __GDEF
  830. {
  831. union {
  832. iztimes t3; /* mtime, atime, ctime */
  833. ztimbuf t2; /* modtime, actime */
  834. } zt;
  835. ulg z_uidgid[2];
  836. int have_uidgid_flg;
  837. have_uidgid_flg = get_extattribs(__G__ &(zt.t3), z_uidgid);
  838. /*---------------------------------------------------------------------------
  839. If symbolic links are supported, allocate storage for a symlink control
  840. structure, put the uncompressed "data" and other required info in it, and
  841. add the structure to the "deferred symlinks" chain. Since we know it's a
  842. symbolic link to start with, we shouldn't have to worry about overflowing
  843. unsigned ints with unsigned longs.
  844. ---------------------------------------------------------------------------*/
  845. #ifdef SYMLINKS
  846. if (G.symlnk) {
  847. extent ucsize = (extent)G.lrec.ucsize;
  848. unsigned BeOSef_len = 0;
  849. extent slnk_entrysize;
  850. uch *BeOS_exfld;
  851. slinkentry *slnk_entry;
  852. if (!uO.J_flag) {
  853. /* Symlinks can have attributes, too. */
  854. BeOS_exfld = scanBeOSexfield(G.extra_field,
  855. G.lrec.extra_field_length);
  856. if (BeOS_exfld) {
  857. BeOSef_len = makeword(EB_LEN + BeOS_exfld) + EB_HEADSIZE;
  858. }
  859. }
  860. /* size of the symlink entry is the sum of
  861. * (struct size (includes 1st '\0') + 1 additional trailing '\0'),
  862. * system specific attribute data size (might be 0),
  863. * and the lengths of name and link target.
  864. */
  865. slnk_entrysize = (sizeof(slinkentry) + 1) + BeOSef_len +
  866. ucsize + strlen(G.filename);
  867. if (slnk_entrysize < ucsize) {
  868. Info(slide, 0x201, ((char *)slide,
  869. "warning: symbolic link (%s) failed: mem alloc overflow\n",
  870. FnFilter1(G.filename)));
  871. fclose(G.outfile);
  872. return;
  873. }
  874. if ((slnk_entry = (slinkentry *)malloc(slnk_entrysize)) == NULL) {
  875. Info(slide, 0x201, ((char *)slide,
  876. "warning: symbolic link (%s) failed: no mem\n",
  877. FnFilter1(G.filename)));
  878. fclose(G.outfile);
  879. return;
  880. }
  881. slnk_entry->next = NULL;
  882. slnk_entry->targetlen = ucsize;
  883. slnk_entry->attriblen = BeOSef_len;
  884. slnk_entry->target = slnk_entry->buf + BeOSef_len;
  885. slnk_entry->fname = slnk_entry->target + ucsize + 1;
  886. strcpy(slnk_entry->fname, G.filename);
  887. if (BeOSef_len > 0)
  888. memcpy(slnk_entry->buf, BeOS_exfld, BeOSef_len);
  889. /* move back to the start of the file to re-read the "link data" */
  890. rewind(G.outfile);
  891. if (fread(slnk_entry->target, 1, ucsize, G.outfile) != ucsize)
  892. {
  893. Info(slide, 0x201, ((char *)slide,
  894. "warning: symbolic link (%s) failed\n",
  895. FnFilter1(G.filename)));
  896. free(slnk_entry);
  897. fclose(G.outfile);
  898. return;
  899. }
  900. fclose(G.outfile); /* close "link" file for good... */
  901. slnk_entry->target[ucsize] = '\0';
  902. if (QCOND2)
  903. Info(slide, 0, ((char *)slide, "-> %s ",
  904. FnFilter1(slnk_entry->target)));
  905. /* add this symlink record to the list of deferred symlinks */
  906. if (G.slink_last != NULL)
  907. G.slink_last->next = slnk_entry;
  908. else
  909. G.slink_head = slnk_entry;
  910. G.slink_last = slnk_entry;
  911. return;
  912. }
  913. #endif /* SYMLINKS */
  914. fclose(G.outfile);
  915. /* handle the BeOS extra field if present */
  916. if (!uO.J_flag) {
  917. void *ptr = scanBeOSexfield(G.extra_field,
  918. G.lrec.extra_field_length);
  919. if (ptr) {
  920. setBeOSexfield(G.filename, ptr);
  921. #ifdef BEOS_ASSIGN_FILETYPE
  922. } else {
  923. /* Otherwise, ask the system to try assigning a MIME type. */
  924. assign_MIME( G.filename );
  925. #endif
  926. }
  927. }
  928. /*---------------------------------------------------------------------------
  929. Change the file permissions from default ones to those stored in the
  930. zipfile.
  931. ---------------------------------------------------------------------------*/
  932. #ifndef NO_CHMOD
  933. if (chmod(G.filename, filtattr(__G__ G.pInfo->file_attr)))
  934. perror("chmod (file attributes) error");
  935. #endif
  936. /* if -X option was specified and we have UID/GID info, restore it */
  937. if (have_uidgid_flg
  938. /* check that both uid and gid values fit into their data sizes */
  939. && ((ulg)(uid_t)(z_uidgid[0]) == z_uidgid[0])
  940. && ((ulg)(gid_t)(z_uidgid[1]) == z_uidgid[1])) {
  941. TTrace((stderr, "close_outfile: restoring Unix UID/GID info\n"));
  942. if (chown(G.filename, (uid_t)z_uidgid[0], (gid_t)z_uidgid[1]))
  943. {
  944. if (uO.qflag)
  945. Info(slide, 0x201, ((char *)slide, CannotSetItemUidGid,
  946. z_uidgid[0], z_uidgid[1], FnFilter1(G.filename),
  947. strerror(errno)));
  948. else
  949. Info(slide, 0x201, ((char *)slide, CannotSetUidGid,
  950. z_uidgid[0], z_uidgid[1], strerror(errno)));
  951. }
  952. }
  953. /* skip restoring time stamps on user's request */
  954. if (uO.D_flag <= 1) {
  955. /* set the file's access and modification times */
  956. if (utime(G.filename, &(zt.t2))) {
  957. if (uO.qflag)
  958. Info(slide, 0x201, ((char *)slide, CannotSetItemTimestamps,
  959. FnFilter1(G.filename), strerror(errno)));
  960. else
  961. Info(slide, 0x201, ((char *)slide, CannotSetTimestamps,
  962. strerror(errno)));
  963. }
  964. }
  965. } /* end function close_outfile() */
  966. #ifdef SYMLINKS
  967. int set_symlnk_attribs(__G__ slnk_entry)
  968. __GDEF
  969. slinkentry *slnk_entry;
  970. {
  971. if (slnk_entry->attriblen > 0)
  972. setBeOSexfield(slnk_entry->fname, (uch *)slnk_entry->buf);
  973. /* currently, no error propagation... */
  974. return PK_OK;
  975. } /* end function set_symlnk_attribs() */
  976. #endif /* SYMLINKS */
  977. #ifdef SET_DIR_ATTRIB
  978. /* messages of code for setting directory attributes */
  979. # ifndef NO_CHMOD
  980. static ZCONST char DirlistChmodFailed[] =
  981. "warning: cannot set permissions for %s\n %s\n";
  982. # endif
  983. int defer_dir_attribs(__G__ pd)
  984. __GDEF
  985. direntry **pd;
  986. {
  987. uxdirattr *d_entry;
  988. d_entry = (uxdirattr *)malloc(sizeof(uxdirattr) + strlen(G.filename));
  989. *pd = (direntry *)d_entry;
  990. if (d_entry == (uxdirattr *)NULL) {
  991. return PK_MEM;
  992. }
  993. d_entry->fn = d_entry->fnbuf;
  994. strcpy(d_entry->fn, G.filename);
  995. d_entry->perms = G.pInfo->file_attr;
  996. d_entry->have_uidgid = get_extattribs(__G__ &(d_entry->u.t3),
  997. d_entry->uidgid);
  998. return PK_OK;
  999. } /* end function defer_dir_attribs() */
  1000. int set_direc_attribs(__G__ d)
  1001. __GDEF
  1002. direntry *d;
  1003. {
  1004. int errval = PK_OK;
  1005. if (UxAtt(d)->have_uidgid &&
  1006. /* check that both uid and gid values fit into their data sizes */
  1007. ((ulg)(uid_t)(UxAtt(d)->uidgid[0]) == UxAtt(d)->uidgid[0]) &&
  1008. ((ulg)(gid_t)(UxAtt(d)->uidgid[1]) == UxAtt(d)->uidgid[1]) &&
  1009. chown(UxAtt(d)->fn, (uid_t)UxAtt(d)->uidgid[0],
  1010. (gid_t)UxAtt(d)->uidgid[1]))
  1011. {
  1012. Info(slide, 0x201, ((char *)slide, CannotSetItemUidGid,
  1013. UxAtt(d)->uidgid[0], UxAtt(d)->uidgid[1], FnFilter1(d->fn),
  1014. strerror(errno)));
  1015. if (!errval)
  1016. errval = PK_WARN;
  1017. }
  1018. /* Skip restoring directory time stamps on user' request. */
  1019. if (uO.D_flag <= 0) {
  1020. /* restore directory timestamps */
  1021. if (utime(d->fn, (const struct utimbuf *)&UxAtt(d)->u.t2)) {
  1022. Info(slide, 0x201, ((char *)slide, CannotSetItemTimestamps,
  1023. FnFilter1(d->fn), strerror(errno)));
  1024. if (!errval)
  1025. errval = PK_WARN;
  1026. }
  1027. }
  1028. #ifndef NO_CHMOD
  1029. if (chmod(d->fn, filtattr(__G__ UxAtt(d)->perms))) {
  1030. Info(slide, 0x201, ((char *)slide, DirlistChmodFailed,
  1031. FnFilter1(d->fn), strerror(errno)));
  1032. if (!errval)
  1033. errval = PK_WARN;
  1034. }
  1035. #endif /* !NO_CHMOD */
  1036. return errval;
  1037. } /* end function set_direc_attribs() */
  1038. #endif /* SET_DIR_ATTRIB */
  1039. #ifdef TIMESTAMP
  1040. /***************************/
  1041. /* Function stamp_file() */
  1042. /***************************/
  1043. int stamp_file(fname, modtime)
  1044. ZCONST char *fname;
  1045. time_t modtime;
  1046. {
  1047. struct utimbuf tp;
  1048. tp.modtime = tp.actime = modtime;
  1049. return (utime(fname, &tp));
  1050. } /* end function stamp_file() */
  1051. #endif /* TIMESTAMP */
  1052. #ifndef SFX
  1053. /************************/
  1054. /* Function version() */
  1055. /************************/
  1056. void version(__G)
  1057. __GDEF
  1058. {
  1059. sprintf((char *)slide, LoadFarString(CompiledWith),
  1060. #if defined(__MWERKS__)
  1061. "Metrowerks CodeWarrior", "",
  1062. #elif defined(__GNUC__)
  1063. "GNU C ", __VERSION__,
  1064. #else
  1065. "(unknown compiler) ","",
  1066. #endif
  1067. "BeOS ",
  1068. #ifdef __POWERPC__
  1069. "(PowerPC)",
  1070. #else
  1071. # ifdef __INTEL__
  1072. "(x86)",
  1073. # else
  1074. "(unknown)", /* someday we may have other architectures... */
  1075. # endif
  1076. #endif
  1077. #ifdef __DATE__
  1078. " on ", __DATE__
  1079. #else
  1080. "", ""
  1081. #endif
  1082. );
  1083. (*G.message)((zvoid *)&G, slide, (ulg)strlen((char *)slide), 0);
  1084. } /* end function version() */
  1085. #endif /* !SFX */
  1086. /******************************/
  1087. /* Extra field functions */
  1088. /******************************/
  1089. /*
  1090. ** Scan the extra fields in extra_field, and look for a BeOS EF; return a
  1091. ** pointer to that EF, or NULL if it's not there.
  1092. */
  1093. static uch *scanBeOSexfield(const uch *ef_ptr, unsigned ef_len)
  1094. {
  1095. while( ef_ptr != NULL && ef_len >= EB_HEADSIZE ) {
  1096. unsigned eb_id = makeword(EB_ID + ef_ptr);
  1097. unsigned eb_len = makeword(EB_LEN + ef_ptr);
  1098. if (eb_len > (ef_len - EB_HEADSIZE)) {
  1099. Trace((stderr,
  1100. "scanBeOSexfield: block length %u > rest ef_size %u\n", eb_len,
  1101. ef_len - EB_HEADSIZE));
  1102. break;
  1103. }
  1104. if (eb_id == EF_BEOS && eb_len >= EB_BEOS_HLEN) {
  1105. return (uch *)ef_ptr;
  1106. }
  1107. ef_ptr += (eb_len + EB_HEADSIZE);
  1108. ef_len -= (eb_len + EB_HEADSIZE);
  1109. }
  1110. return NULL;
  1111. }
  1112. /* Used by setBeOSexfield():
  1113. Set a file/directory's attributes to the attributes passed in.
  1114. If set_file_attrs() fails, an error will be returned:
  1115. EOK - no errors occurred
  1116. (other values will be whatever the failed function returned; no docs
  1117. yet, or I'd list a few)
  1118. */
  1119. static int set_file_attrs( const char *name,
  1120. const unsigned char *attr_buff,
  1121. const off_t attr_size )
  1122. {
  1123. int retval = EOK;
  1124. unsigned char *ptr;
  1125. const unsigned char *guard;
  1126. int fd;
  1127. ptr = (unsigned char *)attr_buff;
  1128. guard = ptr + attr_size;
  1129. fd = open(name, O_RDWR | O_NOTRAVERSE);
  1130. if (fd < 0) {
  1131. return errno; /* should it be -fd ? */
  1132. }
  1133. while (ptr < guard) {
  1134. ssize_t wrote_bytes;
  1135. struct attr_info fa_info;
  1136. const char *attr_name;
  1137. unsigned char *attr_data;
  1138. attr_name = (char *)&(ptr[0]);
  1139. ptr += strlen(attr_name) + 1;
  1140. /* The attr_info data is stored in big-endian format because the */
  1141. /* PowerPC port was here first. */
  1142. memcpy(&fa_info, ptr, sizeof(struct attr_info));
  1143. fa_info.type = (uint32)B_BENDIAN_TO_HOST_INT32( fa_info.type );
  1144. fa_info.size = (off_t)B_BENDIAN_TO_HOST_INT64( fa_info.size );
  1145. ptr += sizeof(struct attr_info);
  1146. if (fa_info.size < 0LL) {
  1147. Info(slide, 0x201, ((char *)slide,
  1148. "warning: skipping attribute with invalid length (%Ld)\n",
  1149. fa_info.size));
  1150. break;
  1151. }
  1152. attr_data = ptr;
  1153. ptr += fa_info.size;
  1154. if (ptr > guard) {
  1155. /* We've got a truncated attribute. */
  1156. Info(slide, 0x201, ((char *)slide,
  1157. "warning: truncated attribute\n"));
  1158. break;
  1159. }
  1160. /* Wave the magic wand... this will swap Be-known types properly. */
  1161. (void)swap_data( fa_info.type, attr_data, fa_info.size,
  1162. B_SWAP_BENDIAN_TO_HOST );
  1163. wrote_bytes = fs_write_attr(fd, attr_name, fa_info.type, 0,
  1164. attr_data, fa_info.size);
  1165. if (wrote_bytes != fa_info.size) {
  1166. Info(slide, 0x201, ((char *)slide,
  1167. "warning: wrote %ld attribute bytes of %ld\n",
  1168. (unsigned long)wrote_bytes,(unsigned long)fa_info.size));
  1169. }
  1170. }
  1171. close(fd);
  1172. return retval;
  1173. }
  1174. static void setBeOSexfield(const char *path, uch *extra_field)
  1175. {
  1176. uch *ptr = extra_field;
  1177. ush id = 0;
  1178. ush size = 0;
  1179. ulg full_size = 0;
  1180. uch flags = 0;
  1181. uch *attrbuff = NULL;
  1182. int retval;
  1183. if( extra_field == NULL ) {
  1184. return;
  1185. }
  1186. /* Collect the data from the extra field buffer. */
  1187. id = makeword(ptr); ptr += 2; /* we don't use this... */
  1188. size = makeword(ptr); ptr += 2;
  1189. full_size = makelong(ptr); ptr += 4;
  1190. flags = *ptr; ptr++;
  1191. /* Do a little sanity checking. */
  1192. if (flags & EB_BE_FL_BADBITS) {
  1193. /* corrupted or unsupported */
  1194. Info(slide, 0x201, ((char *)slide,
  1195. "Unsupported flags set for this BeOS extra field, skipping.\n"));
  1196. return;
  1197. }
  1198. if (size <= EB_BEOS_HLEN) {
  1199. /* corrupted, unsupported, or truncated */
  1200. Info(slide, 0x201, ((char *)slide,
  1201. "BeOS extra field is %d bytes, should be at least %d.\n", size,
  1202. EB_BEOS_HLEN));
  1203. return;
  1204. }
  1205. if (full_size < (size - EB_BEOS_HLEN)) {
  1206. /* possible old archive? will this screw up on valid archives? */
  1207. Info(slide, 0x201, ((char *)slide,
  1208. "Skipping attributes: BeOS extra field is %d bytes, "
  1209. "data size is %ld.\n", size - EB_BEOS_HLEN, full_size));
  1210. return;
  1211. }
  1212. /* Find the BeOS file attribute data. */
  1213. if (flags & EB_BE_FL_UNCMPR) {
  1214. /* Uncompressed data */
  1215. attrbuff = ptr;
  1216. } else {
  1217. /* Compressed data */
  1218. attrbuff = (uch *)malloc( full_size );
  1219. if (attrbuff == NULL) {
  1220. /* No memory to uncompress attributes */
  1221. Info(slide, 0x201, ((char *)slide,
  1222. "Can't allocate memory to uncompress file attributes.\n"));
  1223. return;
  1224. }
  1225. retval = memextract(__G__ attrbuff, full_size,
  1226. ptr, size - EB_BEOS_HLEN);
  1227. if( retval != PK_OK ) {
  1228. /* error uncompressing attributes */
  1229. Info(slide, 0x201, ((char *)slide,
  1230. "Error uncompressing file attributes.\n"));
  1231. /* Some errors here might not be so bad; we should expect */
  1232. /* some truncated data, for example. If the data was */
  1233. /* corrupt, we should _not_ attempt to restore the attrs */
  1234. /* for this file... there's no way to detect what attrs */
  1235. /* are good and which are bad. */
  1236. free (attrbuff);
  1237. return;
  1238. }
  1239. }
  1240. /* Now attempt to set the file attributes on the extracted file. */
  1241. retval = set_file_attrs(path, attrbuff, (off_t)full_size);
  1242. if (retval != EOK) {
  1243. Info(slide, 0x201, ((char *)slide,
  1244. "Error writing file attributes.\n"));
  1245. }
  1246. /* Clean up, if necessary */
  1247. if (attrbuff != ptr) {
  1248. free(attrbuff);
  1249. }
  1250. return;
  1251. }
  1252. #ifdef BEOS_USE_PRINTEXFIELD
  1253. static void printBeOSexfield( int isdir, uch *extra_field )
  1254. {
  1255. uch *ptr = extra_field;
  1256. ush id = 0;
  1257. ush size = 0;
  1258. ulg full_size = 0;
  1259. uch flags = 0;
  1260. /* Tell picky compilers to be quiet. */
  1261. isdir = isdir;
  1262. if( extra_field == NULL ) {
  1263. return;
  1264. }
  1265. /* Collect the data from the buffer. */
  1266. id = makeword( ptr ); ptr += 2;
  1267. size = makeword( ptr ); ptr += 2;
  1268. full_size = makelong( ptr ); ptr += 4;
  1269. flags = *ptr; ptr++;
  1270. if( id != EF_BEOS ) {
  1271. /* not a 'Be' field */
  1272. printf("\t*** Unknown field type (0x%04x, '%c%c')\n", id,
  1273. (char)(id >> 8), (char)id);
  1274. }
  1275. if( flags & EB_BE_FL_BADBITS ) {
  1276. /* corrupted or unsupported */
  1277. printf("\t*** Corrupted BeOS extra field:\n");
  1278. printf("\t*** unknown bits set in the flags\n");
  1279. printf("\t*** (Possibly created by an old version of zip for BeOS.\n");
  1280. }
  1281. if( size <= EB_BEOS_HLEN ) {
  1282. /* corrupted, unsupported, or truncated */
  1283. printf("\t*** Corrupted BeOS extra field:\n");
  1284. printf("\t*** size is %d, should be larger than %d\n", size,
  1285. EB_BEOS_HLEN );
  1286. }
  1287. if( flags & EB_BE_FL_UNCMPR ) {
  1288. /* Uncompressed data */
  1289. printf("\tBeOS extra field data (uncompressed):\n");
  1290. printf("\t\t%ld data bytes\n", full_size);
  1291. } else {
  1292. /* Compressed data */
  1293. printf("\tBeOS extra field data (compressed):\n");
  1294. printf("\t\t%d compressed bytes\n", size - EB_BEOS_HLEN);
  1295. printf("\t\t%ld uncompressed bytes\n", full_size);
  1296. }
  1297. }
  1298. #endif
  1299. #ifdef BEOS_ASSIGN_FILETYPE
  1300. /* Note: This will no longer be necessary in BeOS PR4; update_mime_info() */
  1301. /* will be updated to build its own absolute pathname if it's not given one. */
  1302. static void assign_MIME( const char *file )
  1303. {
  1304. char *fullname;
  1305. char buff[PATH_MAX], cwd_buff[PATH_MAX];
  1306. int retval;
  1307. if( file[0] == '/' ) {
  1308. fullname = (char *)file;
  1309. } else {
  1310. sprintf( buff, "%s/%s", getcwd( cwd_buff, PATH_MAX ), file );
  1311. fullname = buff;
  1312. }
  1313. retval = update_mime_info( fullname, FALSE, TRUE, TRUE );
  1314. }
  1315. #endif