home *** CD-ROM | disk | FTP | other *** search
/ Amiga Elysian Archive / AmigaElysianArchive.iso / compress / zoosrc20.zoo / zoopack.c < prev   
C/C++ Source or Header  |  1989-07-25  |  14KB  |  404 lines

  1. #ifndef LINT
  2. /* @(#) zoopack.c 2.16 88/08/22 15:51:20 */
  3. static char sccsid[]="@(#) zoopack.c 2.16 88/08/22 15:51:20";
  4. #endif /* LINT */
  5.  
  6. /*
  7. Copyright (C) 1986, 1987 Rahul Dhesi -- All rights reserved
  8. (C) Copyright 1988 Rahul Dhesi -- All rights reserved
  9. */
  10. #include "options.h"
  11. /* Packs an archive.  The sequence is:
  12.    1. Copy all files from current archive to new one.
  13.    2. If the user didn't want a backup, delete the old archive
  14.       else rename it to same name with extension of .BAK.
  15.    3. Rename temporary archive to old name.
  16. */
  17.  
  18. /* define this to make packing noisless */
  19. #define QUIETPACK 1
  20.  
  21.  
  22. #include "portable.h"
  23. #include "zooio.h"
  24. #include "various.h"
  25. #include "zoo.h"
  26. #include "zoofns.h"
  27. #include "errors.i"
  28. #ifndef NOSIGNAL
  29. #include <signal.h>
  30. #endif
  31.  
  32. #ifdef LINT_ARGS
  33. char *mktemp (char *);
  34. #else
  35. char *mktemp ();
  36. #endif
  37.  
  38.  
  39. struct zoo_header zoo_header = {
  40.    TEXT,
  41.    ZOO_TAG,
  42.    (long) SIZ_ZOOH,
  43.    (long) (-SIZ_ZOOH),
  44.    MAJOR_VER,
  45.    MINOR_VER,
  46.     H_TYPE,
  47.     0L,                        /* comment position */
  48.     0,                            /* comment length */
  49.     GEN_DEFAULT                /* generations */
  50. };
  51. char file_leader[] = FILE_LEADER;
  52. extern int quiet;
  53. int break_hit;
  54.  
  55. #ifdef LINT_ARGS
  56. int ver_too_high (struct zoo_header *);
  57. #else
  58. int ver_too_high();
  59. #endif
  60.  
  61. void zoopack(zoo_path, option)
  62. char *zoo_path, *option;
  63. {
  64. char temp_file[PATHSIZE];
  65. static char xes[]="XXXXXX";               /* template for temp file */
  66.  
  67. #ifndef NOSIGNAL
  68. int (*oldsignal)();
  69. #endif
  70. register ZOOFILE zoo_file;                /* archive */
  71. ZOOFILE new_file;                         /* destination archive */
  72. long next_ptr;                            /* pointer to within archive */
  73. long new_dir_pos;                         /* ditto */
  74. struct direntry direntry;                 /* directory entry */
  75. struct zoo_header old_zoo_header;         /* just for reading old header */
  76. int status;                               /* error status */
  77. int nobackup = 0;                         /* keep backup */
  78. int force = 0;                            /* force overwrite of old backup */
  79. int extcount = 0;                         /* how many files moved */
  80. char backup_name[PATHSIZE];               /* name of backup */
  81. int bad_header = 0;                       /* if archive has bad header */
  82. int latest_date = 0;                      /* latest date on any file moved */
  83. int latest_time = 0;                      /*  ...likewise */
  84. int curr_dir = 0;                                    /* create backup in curr dir */
  85. static char partial_msg[] =
  86.    "Partially packed archive left in %s.\n";
  87.  
  88. #ifdef FATTR
  89. unsigned long zoofattr;                            /* zoo archive protection */
  90. int setfattr PARMS ((char *, unsigned long));
  91. unsigned long getfattr                            /* params below */
  92. # ifdef FATTR_FNAME
  93.   PARMS ((char *));
  94. # else
  95.   PARMS ((ZOOFILE));
  96. # endif /* FATTR_FNAME */
  97. #endif /* FATTR */
  98.  
  99. while (*option) {
  100.    switch (*option) {
  101.       case 'P': force++; break;
  102.       case 'E': nobackup++; break;
  103.       case 'q': quiet++; break;
  104.         case '.': curr_dir++; break;
  105.       default:
  106.          prterror ('f', inv_option, *option);
  107.    }
  108.    option++;
  109. }
  110. if (force == 1)         /* force only if P was doubled */
  111.    force--;
  112.  
  113. zoo_path = addext (zoo_path, EXT_DFLT);      /* add default extension */
  114.  
  115. /* Create a backup name by replacing any extension by backup extension. */
  116. strcpy (backup_name, zoo_path);
  117. {
  118.    char *temp;
  119.    if ((temp = strrchr (backup_name,EXT_CH)) != 0)  /* if dot found */
  120.       strcpy (temp, BACKUP_EXT);                /* replace old extension */
  121.    else
  122.       strcat (backup_name, BACKUP_EXT);         /* else just append */
  123. }
  124.  
  125. /*
  126. Open original archive for read-write access.  Although we will only
  127. read from it and never write to it, we want to avoid packing an
  128. archive that is read-only, since presumably the user didn't want
  129. to risk changing it in any way.
  130. */
  131. zoo_file = zooopen(zoo_path, Z_RDWR);
  132.  
  133. if (zoo_file == NOFILE)
  134.    prterror ('f', could_not_open, zoo_path);
  135.  
  136. /* If possible, save protection code of old archive for propagation to new */
  137. #ifdef FATTR
  138. # ifdef FATTR_FNAME
  139.    zoofattr = getfattr (zoo_path);
  140. # else
  141.    zoofattr = getfattr (zoo_file);
  142. # endif /* FATTR_FNAME */
  143. #endif /* FATTR */
  144.  
  145. /* Read the header of the old archive. */
  146. frd_zooh(&old_zoo_header, zoo_file);
  147.  
  148. if ((old_zoo_header.zoo_start + old_zoo_header.zoo_minus) != 0L) {
  149.    prterror ('w', failed_consistency);
  150.    ++bad_header;                    /* remember for future error message */
  151. }
  152.  
  153. /* Refuse to pack it if its version number is higher than we can accept */
  154. if (ver_too_high (&old_zoo_header))
  155.    prterror ('f', wrong_version, old_zoo_header.major_ver,
  156.                                     old_zoo_header.minor_ver);
  157.  
  158. /* Now see if the archive already exists with the backup extension.  If so,
  159.    give an error message and abort.  However, we skip this test if the user
  160.    specified overwriting the backup */
  161.  
  162. if (!force) {
  163.    if (exists (backup_name))
  164.       prterror ('f', "File %s already exists.  Delete it or use PP option.\n",
  165.                       backup_name);
  166. }
  167.  
  168. /*
  169. Open the new archive by a temporary name.  If not otherwise specified,
  170. we open the new archive in the same directory as the original.  But if
  171. the curr_dir switch was given, we just put XXXXXX into temp_file.
  172. */
  173. if (!curr_dir) {
  174.     strcpy (temp_file, zoo_path);          /* original archive name */
  175.     *nameptr (temp_file) = '\0';           /* ... minus original filename */
  176.     strcat (temp_file, xes);               /* ... plus XXXXXX */
  177. } else {
  178.    strcpy (temp_file, xes);
  179. }
  180. mktemp (temp_file);                    /* ... and make unique */
  181. new_file = zoocreate (temp_file);
  182. if (new_file == NOFILE)
  183.    prterror ('f', "Could not create temporary file %s.\n", temp_file);
  184.  
  185. /*
  186. If old_zoo_header greater than type 0, we update zoo_header as follows:  
  187. new archive comment will be just after archive header;  zoo_start will 
  188. point to just beyond archive comment.  But if old_zoo_header is of 
  189. type 0, we leave zoo_header unchanged.  However, we always 
  190. unconditionally update the header type to be type H_TYPE.  
  191. (Note:  zoo_header.type is initialized to H_TYPE in the
  192. global declaration of zoo_header.)
  193. */
  194. if (old_zoo_header.type > 0) {
  195.     zoo_header.zoo_start = SIZ_ZOOH + old_zoo_header.acmt_len;
  196.     zoo_header.zoo_minus = -zoo_header.zoo_start;
  197.     zoo_header.acmt_pos = SIZ_ZOOH;    /* new comment just after header */
  198.     zoo_header.acmt_len = old_zoo_header.acmt_len;
  199.     zoo_header.vdata      = old_zoo_header.vdata;
  200. } else /* keep generations off if using old format archive */
  201.     zoo_header.vdata &=  (~VFL_ON);
  202.  
  203. /* Write the header of the new archive, updated with our own data */
  204. fwr_zooh (&zoo_header, new_file);
  205.  
  206. /* copy archive comment */
  207. if (old_zoo_header.acmt_len != 0) {
  208.     zooseek (zoo_file, old_zoo_header.acmt_pos, 0);    /* find archive comment */
  209.     getfile (zoo_file, new_file, (long) zoo_header.acmt_len, 0); /* copy it */
  210. }
  211.  
  212. /* WARNING: CHECK FOR SEEK BEYOND END OF FILE */
  213. zooseek (new_file, zoo_header.zoo_start, 0);       /* position to add files */
  214.  
  215. zooseek (zoo_file, old_zoo_header.zoo_start, 0); /* seek to where data begins */
  216.  
  217. /* Now we loop through the old archive's files and add each to the new
  218. archive.  The only changes needed are to update the .next and .offset
  219. fields of the directory entry. */
  220.  
  221. while (1) {
  222.    frd_dir(&direntry, zoo_file);
  223.    if (direntry.zoo_tag != ZOO_TAG) {
  224.       long currpos, zoolength;
  225.       prterror ('F', bad_directory);
  226.       if (bad_header) {    /* bad headers means don't save temp file */
  227.          zooclose (new_file);
  228.          unlink (temp_file);
  229.       } else {
  230.          writenull (new_file, MAXDIRSIZE);    /* write final null entry */
  231.          printf (partial_msg, temp_file);
  232.          if ((currpos = ftell (zoo_file)) != -1L)
  233.             if (zooseek (zoo_file, 0L, 2) == 0)
  234.                if ((zoolength = ftell (zoo_file)) != -1L)
  235.                   printf (cant_process, zoolength - currpos);
  236.       }
  237.       zooexit (1);
  238.    }
  239.    if (direntry.next == 0L) {                /* END OF CHAIN */
  240.       break;                                 /* EXIT on end of chain */
  241.    }
  242.    next_ptr = direntry.next;                 /* ptr to next dir entry */
  243.  
  244.    if (!direntry.deleted) {
  245. #ifdef QUIETPACK
  246. /* nothing */
  247. #else
  248.       prterror ('m', "%-14s -- ",
  249.          direntry.namlen > 0 ? direntry.lfname : direntry.fname);
  250. #endif
  251.  
  252.       if (zooseek (zoo_file, direntry.offset, 0) == -1L) {
  253.          prterror ('f', "Could not seek to file data.\n");
  254.       } else {
  255.          extcount++;          /* update count of files extracted */
  256.  
  257.          /* write a directory entry for this file */
  258.          new_dir_pos = zootell (new_file); /* new direntry pos in new archive */
  259.  
  260.          /*
  261.          Write a null directory entry to preserve integrity in case of
  262.          program being interrupted.  Note:  I don't think it is
  263.          necessary to save direntry.next but I haven't checked.
  264.          */
  265.          {
  266.             long oldnext;
  267.             oldnext = direntry.next;
  268.             direntry.next = 0L;
  269.             fwr_dir(&direntry, new_file);
  270.             direntry.next = oldnext;
  271.          }
  272.  
  273.          zooseek (zoo_file, direntry.offset, 0);  /* where to start copying */
  274.          /* Write file leader and remember position of new file data */
  275.          zoowrite (new_file, file_leader, SIZ_FLDR);
  276.          direntry.offset = zootell (new_file);
  277.          status = getfile (zoo_file, new_file, direntry.size_now, 0);
  278.          /* if no error copy any comment attached to file */
  279.          if (status == 0 && direntry.cmt_size != 0) {
  280.             zooseek (zoo_file, direntry.comment, 0);  /* seek to old comment  */
  281.             direntry.comment = zootell (new_file); /* location of new comment */
  282.             status = getfile (zoo_file, new_file,
  283.                                  (long) direntry.cmt_size, 0);
  284.          }
  285.  
  286.          if (status != 0) {
  287.             if (status == 1) {
  288.                memerr();
  289.             } else
  290.                if (status == 2 || status == 3) {
  291.                   prterror ('F', disk_full);
  292.                   printf (partial_msg, temp_file);
  293.                   zooexit (1);
  294.                } else
  295.                   prterror ('f', internal_error);
  296.          } else {
  297.             if (latest_date < direntry.date ||
  298.                      (latest_date == direntry.date &&
  299.                            latest_time < direntry.time))  {
  300.                latest_date = direntry.date;
  301.                latest_time = direntry.time;
  302.             }
  303.          }
  304.          direntry.next = zootell (new_file);
  305.          zooseek (new_file, new_dir_pos, 0);  /* position to write direntry */
  306.  
  307.          break_hit = 0;
  308. #ifndef NOSIGNAL
  309.          oldsignal = signal (SIGINT, SIG_IGN);
  310.          if (oldsignal != SIG_IGN)
  311.             signal (SIGINT, handle_break);
  312. #endif
  313.  
  314.             /* Bug fix thanks to Mark Alexander */
  315.          if (fwr_dir (&direntry, new_file) != -1 &&
  316.             zoowrite (new_file, file_leader, SIZ_FLDR) == SIZ_FLDR) {
  317. #ifdef QUIETPACK
  318.             /* prterror ('M', "."); */ ;
  319. #else
  320.             prterror ('M', "moved\n");
  321. #endif
  322.          } else
  323.             prterror ('f', "Write to temporary packed archive %s failed.\n", temp_file);
  324. #ifndef NOSIGNAL
  325.          signal (SIGINT, oldsignal);
  326. #endif
  327.          if (break_hit)
  328.             zooexit (1);
  329.          zooseek (new_file, direntry.next, 0);  /* back to end of new archive */
  330.       }  /* end if (lseek ... */
  331.    } /* end if (!direntry.deleted) */
  332.  
  333. zooseek (zoo_file, next_ptr, 0);   /* ..seek to next dir entry */
  334. } /* end while */
  335.  
  336. zooclose (zoo_file);
  337.  
  338. /* write a final null entry */
  339. writenull (new_file, MAXDIRSIZE);
  340.  
  341. #ifdef NIXTIME
  342. zooclose (new_file);
  343. setutime (temp_file, latest_date, latest_time);
  344. #else
  345. settime (new_file, latest_date, latest_time);    /* adjust its time */
  346. zooclose (new_file);
  347. #endif
  348.  
  349. /* Important note:  At this point, it is assumed that the archive was
  350.    packed and written to a new file without error.  If control reaches
  351.    here without the new archive having been written properly, then
  352.    loss of data due to deletion of the original file could occur.  In
  353.    other words, if something went wrong, execution MUST NOT reach here. */
  354.  
  355. if (extcount == 0) {
  356.    unlink (temp_file);
  357.    prterror ('m', "No files moved.\n");
  358. } else {
  359.    /* (a) if user requested, delete original, else rename it to
  360.    *.bak.  (b) rename temp file to same base name as zoo_file. */
  361.  
  362. #ifdef QUIETPACK
  363.    /* prterror('M', "\n"); */
  364. #endif
  365.  
  366.    unlink (backup_name);    /* remove any previous backup in any case */
  367.    if (nobackup)
  368.       unlink (zoo_path);
  369.    else
  370.       chname (backup_name, zoo_path);
  371.  
  372.     /* if we are packing into current directory, we will rename temp file
  373.         to same basename but without the preceding pathname */
  374.     if (curr_dir)
  375.         zoo_path = nameptr (zoo_path);        /* strip pathname */
  376.  
  377.  
  378.    if (chname (zoo_path, temp_file)) {
  379.       prterror ('w', "Renaming error.  Packed archive is now in %s.\n", temp_file);
  380.       zooexit (1);
  381.    }
  382.  
  383. /*
  384. Set protection on packed archive -- after renaming, since some
  385. OSs might not allow renaming of read-only files
  386. */
  387. #ifdef FATTR
  388.     setfattr (zoo_path, zoofattr);
  389. #endif /* FATTR */
  390.  
  391. } /* end if */
  392.  
  393. } /* end zoopack() */
  394.  
  395. /* handle_break() */
  396. /* Sets break_hit to 1 when called */
  397. int handle_break()
  398. {
  399. #ifndef NOSIGNAL
  400.    signal (SIGINT, SIG_IGN);     /* ignore future control ^Cs for now */
  401.    break_hit = 1;
  402. #endif
  403. }
  404.