home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / ZOO21E.EXE / ZOOPACK.C < prev   
C/C++ Source or Header  |  1991-07-11  |  14KB  |  398 lines

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