home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume19 / rkive / part02 / record_arc.c < prev    next >
C/C++ Source or Header  |  1989-06-29  |  5KB  |  209 lines

  1. /*
  2. **
  3. ** This software is Copyright (c) 1989 by Kent Landfield.
  4. **
  5. ** Permission is hereby granted to copy, distribute or otherwise 
  6. ** use any part of this package as long as you do not try to make 
  7. ** money from it or pretend that you wrote it.  This copyright 
  8. ** notice must be maintained in any copy made.
  9. **
  10. **
  11. **  History:
  12. **    Creation: Tue Feb 21 08:52:35 CST 1989 due to necessity.
  13. **                                                               
  14. */
  15. #ifndef lint
  16. static char SID[] = "@(#)record_arc.c    1.1 6/1/89";
  17. #endif
  18.  
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #include <dirent.h>
  22. #include <stdio.h>
  23. #include "cfg.h"
  24.  
  25. static char work[BUFSIZ];
  26. static int len;
  27. static FILE *archived_fp;
  28.  
  29. FILE *fopen();
  30. FILE *efopen();
  31.  
  32. extern int test;
  33. extern struct group_archive *newsgrp;
  34.  
  35. /*
  36. ** needs_to_be_archived:
  37. **
  38. ** int needs_to_be_archived(filename) char *filename; { return(0); }
  39. **
  40. ** This function is used to determine if the filename
  41. ** passed to it requires archiving. If the filename is
  42. ** found in the .archived file, it has been previously
  43. ** archived and does not need to be now. If it is not
  44. ** found in the .archived file, it needs to be archived.
  45. */
  46.  
  47. int needs_to_be_archived(filename)
  48.     char *filename;
  49. {
  50.     /*
  51.     ** If the .archived file is missing, it must be
  52.     ** the first time that it is to be archived.
  53.     */
  54.     if ((archived_fp = fopen(newsgrp->arc_done,"r")) == NULL) 
  55.     return(1);
  56.  
  57.     len = strlen(filename);
  58.  
  59.     while (fgets(work, sizeof work, archived_fp) != NULL) {
  60.         if (*work == '#')
  61.             continue;
  62.         if (strncmp(work,filename,len) == 0) {
  63.             (void) fclose(archived_fp);
  64.             return(0);
  65.         }
  66.     }
  67.     (void) fclose(archived_fp);
  68.     return(1);
  69. }
  70.  
  71. /*
  72. ** remove_expired:
  73. **
  74. ** This function is used to maintain the .archived file in the
  75. ** newsgroup's archive BASEDIR directory. This file is used to assure
  76. ** that files are not archived over and over and that the 
  77. ** administrator does not need to wait until the files have
  78. ** expired before they are added to the archive. 
  79. ** The .archived file stores a list of filenames of files that have been 
  80. ** archived already. The remove_expired function removes filenames 
  81. ** from the .archived file that no longer exist. In this manner the 
  82. ** file is self maintaining. Expired entries are removed.
  83. */
  84.  
  85. remove_expired()
  86. {
  87.     char news_file[20];
  88.     char fl_loc[MAXNAMLEN];
  89.     char tmp_loc[MAXNAMLEN];
  90.     FILE *tmp_archived;
  91.     struct stat stbuf;
  92.  
  93.     /* Is this being run as a test ? */
  94.  
  95.     if (test)
  96.         return;
  97.  
  98.     /* Is this the first time this group is to be archived ? */
  99.  
  100.     if (stat(newsgrp->arc_done, &stbuf) < 0) 
  101.         return;
  102.     
  103.     /* open the necessary files, creating a temp file */
  104.     (void) sprintf(tmp_loc,"%s/.arch_tmp", newsgrp->location);
  105.  
  106.     tmp_archived = efopen(tmp_loc,"w");
  107.     archived_fp = efopen(newsgrp->arc_done,"r");
  108.  
  109.     /* 
  110.     ** for every line in the .archived file,
  111.     **    check it comment line. If so, save it in the temp file
  112.     **    strip the string of any blanks or tabs inadvertantly 
  113.     **      added by admin in manual mode (editor)
  114.     **    check to see if the file still exists.
  115.     **    if so, write filename to the temp file
  116.     ** endfor
  117.     */
  118.  
  119.     while (fgets(work, sizeof work, archived_fp) != NULL) {
  120.         if (*work == '#')      /* maintain comments in .archived */
  121.             (void) fprintf(tmp_archived,work);
  122.         else {
  123.             (void) sscanf(work, "%s %s", news_file, fl_loc);
  124.             if (stat(news_file, &stbuf) == 0) 
  125.                 (void) fprintf(tmp_archived,work);
  126.         }
  127.     }
  128.  
  129.     /*
  130.     ** Close both files and rename the temp 
  131.     ** file to the new .archived file.
  132.     */
  133.  
  134.     (void) fclose(tmp_archived);
  135.     (void) fclose(archived_fp);
  136.     (void) rename(tmp_loc,newsgrp->arc_done);
  137.     return;
  138. }
  139.  
  140. /*
  141. ** get_archived_rec:
  142. **
  143. ** get_archived_rec(filename) char *filename; { return(filename); }
  144. **
  145. ** get_archived_rec is used to determine if the filename
  146. ** passed to it has been archived. If its name is found
  147. ** in the .archived file, it has been previously archived
  148. ** and its archive information record is  returned to the
  149. ** caling routine. Else it returns NULL.
  150. */
  151.  
  152. char *get_archived_rec(filename)
  153.     char *filename;
  154. {
  155.     /*
  156.     ** If the .archived file is missing, it must be
  157.     ** the first time that it is to be archived.
  158.     */
  159.     if ((archived_fp = fopen(newsgrp->arc_done,"r")) == NULL) 
  160.     return(NULL);
  161.  
  162.     len = strlen(filename);
  163.  
  164.     while (fgets(work, sizeof work, archived_fp) != NULL) {
  165.         if (*work == '#')
  166.             continue;
  167.         if (strncmp(work,filename,len) == 0) {
  168.             (void) fclose(archived_fp);
  169.             *(work+(strlen(work)-1)) = '\0';
  170.             return(work);
  171.         }
  172.     }
  173.     (void) fclose(archived_fp);
  174.     return(NULL);
  175. }
  176.  
  177. /*
  178. ** write_archived:
  179. **
  180. ** write_archived(filename, path) char *filename, path; {}
  181. **
  182. ** This function is used to write a filename and its archived location
  183. ** to the .archived file in the newsgroup's archive BASEDIR
  184. ** directory since we do not want it rearchived tomorrow.
  185. */
  186.  
  187. write_archived(filename, path)
  188.     char *filename;
  189.     char *path;
  190. {
  191.     FILE *fp;
  192.  
  193.     /* 
  194.     ** If in test mode no actual operations are 
  195.     ** to be done...
  196.     */
  197.     if (test)
  198.         return;
  199.  
  200.     /*
  201.     ** open the file and append the record
  202.     */
  203.  
  204.     fp = efopen(newsgrp->arc_done,"a+");
  205.     (void) fprintf(fp,"%s %s\n",filename, path);
  206.     (void) fclose(fp);
  207.     return;
  208. }
  209.