home *** CD-ROM | disk | FTP | other *** search
/ Hall of Fame / HallofFameCDROM.cdr / proglc / zoo141_c.lzh / ZOOPACK.C < prev   
C/C++ Source or Header  |  1987-02-07  |  11KB  |  339 lines

  1. /* zoopack.c */
  2. /*
  3. Copyright (C) 1986 Rahul Dhesi -- All rights reserved
  4. */
  5. #include "options.h"
  6. /* Packs an archive.  The sequence is:
  7.    1. Copy all files from current archive to new one.
  8.    2. If the user didn't want a backup, delete the old archive
  9.       else rename it to same name with extension of .BAK.
  10.    3. Rename temporary archive to old name.
  11. */
  12.  
  13. /* define this to make packing noisless except for dots */
  14. #define QUIETPACK 1
  15.  
  16.  
  17. #include "portable.h"
  18. #include <stdio.h>
  19. #include "various.h"
  20. #include "zoo.h"
  21. #include "zoofns.h"
  22. #include "errors.i"
  23. #ifndef NOSIGNAL
  24. #include <signal.h>
  25. #endif
  26.  
  27. #ifdef FLAT
  28. #include <types.h>
  29. #include <stat.h>
  30. #else
  31. #include <sys/types.h>
  32. #include <sys/stat.h>
  33. #endif
  34.  
  35. #ifdef NOFCNTL
  36. #include <file.h>
  37. #else
  38. #include <fcntl.h>
  39. #endif
  40.  
  41. struct zoo_header zoo_header = {
  42.    TEXT,
  43.    ZOO_TAG,
  44.    (long) SIZ_ZOOH,
  45.    (long) (-SIZ_ZOOH),
  46.    MAJOR_VER,
  47.    MINOR_VER
  48. };
  49. char file_leader[] = FILE_LEADER;
  50. extern int quiet;
  51. int break_hit;
  52.  
  53. #ifdef LINT_ARGS
  54. int ver_too_high (struct zoo_header *);
  55. #else
  56. int ver_too_high();
  57. #endif
  58.  
  59. void zoopack(zoo_path, option)
  60. char *zoo_path, *option;
  61. {
  62. char temp_file[PATHSIZE];
  63. #ifndef NOSIGNAL  
  64. int (*oldsignal)();
  65. #endif
  66. register int zoo_han;                     /* handle for open archive */
  67. register FILE *zoo_file;                  /* stream for open archive */
  68. int new_han;                              /* handle of destination archive */
  69. long next_ptr;                            /* pointer to within archive */
  70. long new_dir_pos;                         /* ditto */
  71. struct direntry direntry;                 /* directory entry */
  72. struct zoo_header old_zoo_header;         /* just for reading old header */
  73. int status;                               /* error status */
  74. int nobackup = 0;                         /* keep backup */
  75. int force = 0;                            /* force overwrite of old backup */
  76. int extcount = 0;                         /* how many files moved */
  77. char backup_name[PATHSIZE];               /* name of backup */
  78. int bad_header = 0;                       /* if archive has bad header */
  79. int latest_date = 0;                      /* latest date on any file moved */
  80. int latest_time = 0;                      /*  ...likewise */
  81. static char partial_msg[] =
  82.    "Partially packed archive left in %s.\n";
  83.  
  84. while (*option) {
  85.    switch (*option) {
  86.       case 'P': force++; break;
  87.       case 'E': nobackup++; break;
  88.       case 'q': quiet++; break;
  89.       default:
  90.          prterror ('f', inv_option, *option);
  91.    }
  92.    option++;
  93. }
  94. if (force == 1)         /* force only if P was doubled */
  95.    force--;
  96.  
  97. /* Create a backup name by replacing any extension by backup extension. */
  98. strcpy (backup_name, zoo_path);
  99. {  
  100.    char *temp;
  101.    if (temp = strrchr (backup_name,EXT_CH))      /* if dot found */
  102.       strcpy (temp, BACKUP_EXT);                /* replace old extension */
  103.    else
  104.       strcat (backup_name, BACKUP_EXT);         /* else just append */
  105. }
  106.  
  107. /* Open original archive for read-only access */
  108. if ((zoo_han = OPEN(zoo_path, F_READ)) == -1)
  109.    prterror ('f', could_not_open, zoo_path);
  110. /* associate stream with handle. "r+" means read/write, must exist */
  111. zoo_file = fdopen (zoo_han, "r+"); 
  112. if (zoo_file == NULL)
  113.    prterror ('f', could_not_open, zoo_path);
  114.  
  115. /* Read the header of the old archive. */
  116. frd_zooh(&old_zoo_header, zoo_file);
  117. /* fread ((char *) &old_zoo_header, sizeof(old_zoo_header), 1, zoo_file); */
  118.  
  119. if ((old_zoo_header.zoo_start + old_zoo_header.zoo_minus) != 0L) {
  120.    prterror ('w', failed_consistency);
  121.    ++bad_header;                    /* remember for future error message */
  122. }
  123.  
  124. /* Refuse to pack it if its version number is higher than we can accept */
  125. if (ver_too_high (&old_zoo_header))
  126.    prterror ('f', wrong_version, old_zoo_header.major_ver, 
  127.                                     old_zoo_header.minor_ver);
  128.  
  129. fseek (zoo_file, old_zoo_header.zoo_start, 0); /* seek to where data begins */
  130.  
  131. /* Now see if the archive already exists with the backup extension.  If so,
  132.    give an error message and abort.  However, we skip this test if the user
  133.    specified overwriting the backup */
  134.  
  135. if (!force) {
  136.    if (exists (backup_name))
  137.       prterror ('f', "File %s already exists.  Delete it or use PP option.\n", 
  138.                       backup_name);
  139. }
  140.  
  141. /* Open the new archive by a temporary name.  It is an  error for it 
  142. to already exist */
  143. strcpy(temp_file, "zXXXXXX");
  144. mktemp(temp_file);
  145. if (exists (temp_file))
  146.    prterror ('f', "%s already exists.  Delete it and try again.\n",temp_file);
  147.  
  148. new_han = CREATE(temp_file, F_RDWR);
  149. if (new_han == -1)
  150.    prterror ('f', "Could not create temporary file %s.\n", temp_file);
  151.  
  152. /* Write the header of the new archive, updated with our own data */
  153. wr_zooh (&zoo_header, new_han);
  154. /* write (new_han, (char *) &zoo_header, sizeof(zoo_header)); */
  155. lseek (new_han, zoo_header.zoo_start, 0);       /* position to add files */
  156.  
  157. /* Now we loop through the old archive's files and add each to the new
  158. archive.  The only changes needed are to update the .next and .offset
  159. fields of the directory entry. */
  160.  
  161. while (1) {
  162.  
  163.    frd_dir(&direntry, zoo_file);
  164. /* fread ((char *) &direntry, sizeof (direntry), 1, zoo_file); */
  165.  
  166.    if (direntry.zoo_tag != ZOO_TAG) {
  167.       long currpos, zoolength;
  168.       prterror ('F', bad_directory);
  169.       if (bad_header) {    /* bad headers means don't save temp file */
  170.          close (new_han); 
  171.          unlink (temp_file);
  172.       } else {
  173.          writenull (new_han, MAXDIRSIZE);    /* write final null entry */
  174.          printf (partial_msg, temp_file);
  175.          if ((currpos = ftell (zoo_file)) != -1L)
  176.             if (fseek (zoo_file, 0L, 2) == 0)
  177.                if ((zoolength = ftell (zoo_file)) != -1L)
  178.                   printf (cant_process, zoolength - currpos);              
  179.       }
  180.       exit (1);
  181.    }
  182.    if (direntry.next == 0L) {                /* END OF CHAIN */
  183.       break;                                 /* EXIT on end of chain */
  184.    }
  185.    next_ptr = direntry.next;                 /* ptr to next dir entry */
  186.  
  187.    if (!direntry.deleted) {
  188. #ifdef QUIETPACK
  189. /* nothing */
  190. #else
  191.       prterror ('m', "%-14s -- ", 
  192.          direntry.namlen > 0 ? direntry.lfname : direntry.fname);
  193. #endif
  194.  
  195.       if (lseek (zoo_han, direntry.offset, 0) == -1L) {
  196.          prterror ('f', "Could not seek to file data.\n");
  197.       } else {
  198.          extcount++;          /* update count of files extracted */
  199.  
  200.          /* write a directory entry for this file */
  201.          new_dir_pos = tell (new_han);   /* new direntry pos in new archive */
  202.  
  203.          /* 
  204.          Write a null directory entry to preserve integrity in case of
  205.          program being interrupted.  Note:  I don't think it is
  206.          necessary to save direntry.next but I haven't checked.
  207.  
  208.          Old code was:
  209.          writenull(new_han, SIZ_DIRL + direntry.var_dir_len);
  210.          */
  211.          {
  212.             long oldnext;
  213.             oldnext = direntry.next;
  214.             direntry.next = 0L;
  215.             wr_dir(&direntry, new_han);
  216.             direntry.next = oldnext;
  217.          }
  218.  
  219.          fseek (zoo_file, direntry.offset, 0);     /* where to start copying */
  220.          lseek (zoo_han, direntry.offset, 0);      /*DEBUG*/
  221.          /* Write file leader and remember position of new file data */
  222.          write (new_han, file_leader, SIZ_FLDR);
  223.          direntry.offset = tell(new_han);
  224.          status = getfile (zoo_han, new_han, direntry.size_now, 0);
  225.          /* if no error copy any comment attached to file */
  226.          if (status == 0 && direntry.cmt_size != 0) {
  227.             lseek (zoo_han, direntry.comment, 0);  /* seek to old comment  */
  228.             direntry.comment = tell (new_han);  /* location of new comment */
  229.             status = getfile (zoo_han, new_han, 
  230.                                  (long) direntry.cmt_size, 0);
  231.          }
  232.  
  233.          if (status != 0) {
  234.             if (status == 1) {
  235.                memerr();
  236.             } else 
  237.                if (status == 2 || status == 3) {
  238.                   prterror ('F', disk_full);
  239.                   printf (partial_msg, temp_file);
  240.                   exit (1);
  241.                } else
  242.                   prterror ('f', internal_error);
  243.          } else {
  244.             if (latest_date < direntry.date ||
  245.                      (latest_date == direntry.date && 
  246.                            latest_time < direntry.time))  {
  247.                latest_date = direntry.date;
  248.                latest_time = direntry.time;
  249.             }
  250.          }
  251.          direntry.next = tell (new_han);
  252.          lseek (new_han, new_dir_pos, 0);    /* position to write direntry */
  253.  
  254.          break_hit = 0;
  255. #ifndef NOSIGNAL
  256.          oldsignal = signal (SIGINT, SIG_IGN);
  257.          if (oldsignal != SIG_IGN)
  258.             signal (SIGINT, handle_break);
  259. #endif
  260.  
  261.          if (wr_dir (&direntry, new_han) != -1 &&
  262.             write (new_han, file_leader, SIZ_FLDR) != -1) {
  263. #ifdef QUIETPACK
  264.             /* prterror ('M', "."); */ ;
  265. #else
  266.             prterror ('M', "moved\n");
  267. #endif
  268.          } else
  269.             prterror ('f', "Write to temporary packed archive %s failed.\n", temp_file);
  270. #ifndef NOSIGNAL
  271.          signal (SIGINT, oldsignal);
  272. #endif
  273.          if (break_hit)
  274.             exit (1);
  275.  
  276.          lseek (new_han, direntry.next, 0);  /* back to end of new archive */
  277.       }  /* end if (lseek ... */
  278.    } /* end if (!direntry.deleted) */
  279.  
  280. fseek (zoo_file, next_ptr, 0);   /* ..seek to next dir entry */
  281. } /* end while */
  282.  
  283. fclose (zoo_file);
  284.  
  285. /* write a final null entry */
  286. writenull (new_han, MAXDIRSIZE);
  287.  
  288. #ifdef NIXTIME
  289. close (new_han);
  290. setutime (temp_file, latest_date, latest_time);
  291. #else
  292. settime (new_han, latest_date, latest_time);    /* adjust its time */
  293. close (new_han);
  294. #endif
  295.  
  296. /* Important note:  At this point, it is assumed that the archive was
  297.    packed and written to a new file without error.  If control reaches
  298.    here without the new archive having been written properly, then
  299.    loss of data due to deletion of the original file could occur.  In
  300.    other words, if something went wrong, execution MUST NOT reach here. */
  301.  
  302. if (extcount == 0) {
  303.    unlink (temp_file);  
  304.    prterror ('m', "No files moved.\n");
  305. } else {
  306.    char new_name[FNAMESIZE];
  307.    /* (a) if user requested, delete original, else rename it to
  308.    *.bak.  (b) rename temp file to same base name as zoo_file. */
  309.  
  310. #ifdef QUIETPACK
  311.    /* prterror('M', "\n"); */
  312. #endif
  313.  
  314.    unlink (backup_name);    /* remove any previous backup in any case */
  315.    if (nobackup)
  316.       unlink (zoo_path);
  317.    else
  318.       chname (backup_name, zoo_path);
  319.    basename (zoo_path, new_name);
  320.    if (chname (new_name, temp_file)) {
  321.       prterror ('w', "Renaming error.  Packed archive is now in %s.\n", temp_file);
  322.       exit (1);
  323.    }
  324. } /* end if */
  325.  
  326. } /* end zoopack() */
  327.  
  328.  
  329. /* handle_break() */
  330. /* Sets break_hit to 1 when called */
  331. int handle_break()
  332. {
  333. #ifndef NOSIGNAL
  334.    signal (SIGINT, SIG_IGN);     /* ignore future control ^Cs for now */
  335.    break_hit = 1;
  336. #endif
  337. }
  338.  
  339.