home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / trn / part04 / mt-write.c next >
Encoding:
C/C++ Source or Header  |  1991-12-02  |  9.4 KB  |  394 lines

  1. /* $Id: mt-write.c,v 4.4.3.1 1991/11/22 04:12:15 davison Trn $
  2. **
  3. ** $Log: mt-write.c,v $
  4. ** Revision 4.4.3.1  1991/11/22  04:12:15  davison
  5. ** Trn Release 2.0
  6. ** 
  7. */
  8.  
  9. #include "EXTERN.h"
  10. #include "common.h"
  11. #include "threads.h"
  12. #include "mthreads.h"
  13.  
  14. static FILE *fp_out;
  15. static int seq;
  16. static int article_seq;
  17.  
  18. static int failure;
  19.  
  20. void write_subjects(), write_authors(), write_roots(), write_ids();
  21. void write_articles(), write_thread(), write_item();
  22. void enumerate_articles(), enumerate_thread();
  23. void free_leftovers();
  24.  
  25. /* Write out all the data in a packed format that is easy for our newsreader
  26. ** to use.  We free things as we go, when we don't need them any longer.  If
  27. ** we encounter any write errors, the write_item routine sets a failure flag
  28. ** to halt our writing of the file, but we keep on plugging away to free
  29. ** everything up.
  30. */
  31. int
  32. write_data(filename)
  33. char *filename;
  34. {
  35.     if (filename == Nullch) {
  36.     failure = 2;    /* A NULL filename indicates just free the data */
  37.     } else if ((fp_out = fopen(filename, FOPEN_WB)) == Nullfp) {
  38.     if (ensure_path(filename)) {
  39.         if ((fp_out = fopen(filename, FOPEN_WB)) == Nullfp) {
  40.         log_error("Unable to create file: `%s'.\n", filename);
  41.         failure = 2;
  42.         }
  43.     } else {
  44.         log_error("Unable to create path: `%s'.\n", filename);
  45.         failure = 2;
  46.     }
  47.     } else {
  48.     failure = 0;
  49. #ifdef SETBUFFER
  50.     setbuffer(fp_out, rwbuf, RWBUFSIZ);
  51. #else
  52. # ifdef SETVBUF
  53.     setvbuf(fp_out, rwbuf, _IOFBF, RWBUFSIZ);
  54. # endif
  55. #endif
  56.     }
  57.  
  58.     /* If there's no roots, there's no data.  Leave the file with no length. */
  59.     if (!total.root && !failure) {
  60.     failure = -1;
  61.     }
  62.  
  63.     write_item(&total, sizeof (TOTAL));
  64.  
  65.     enumerate_articles();
  66.  
  67.     write_authors();
  68.     write_subjects();
  69.     write_roots();
  70.     write_articles();
  71.     write_ids();
  72.     free_leftovers();
  73.  
  74.     if (failure != 2) {
  75.     fclose(fp_out);
  76.     }
  77.     if (failure == 1) {
  78.     log_error("Write failed!  Removing `%s'.\n", filename);
  79.     unlink(filename);
  80.     }
  81.     return failure <= 0;
  82. }
  83.  
  84. /* Recursively descend the article tree, enumerating the articles as we go.
  85. ** This way we can output the article sequence numbers into the data file.
  86. */
  87. void
  88. enumerate_articles()
  89. {
  90.     register ROOT *root;
  91.  
  92.     seq = article_seq = 0;
  93.  
  94.     for (root = root_root; root; root = root->link) {
  95.     root->seq = seq++;
  96.     if (!root->articles) {
  97.         log_error("** No articles on this root??\n");
  98.         continue;
  99.     }
  100.     enumerate_thread(root->articles);
  101.     }
  102.     if (seq != total.root) {
  103.     log_error("** Wrote %d roots instead of %d **\n", seq, total.root);
  104.     }
  105.     if (article_seq != total.article) {
  106.     log_error("** Wrote %d articles instead of %d **\n", article_seq, total.article);
  107.     }
  108. }
  109.  
  110. /* Recursive routine for above-mentioned enumeration. */
  111. void
  112. enumerate_thread(article)
  113. ARTICLE *article;
  114. {
  115.     while (article) {
  116.     article->seq = article_seq++;
  117.     if (article->children) {
  118.         enumerate_thread(article->children);
  119.     }
  120.     article = article->siblings;
  121.     }
  122. }
  123.  
  124. #define write_and_free(str_ptr)    /* Comment for makedepend to     \
  125.                     ** ignore the backslash above */ \
  126. {\
  127.     register int len = strlen(str_ptr) + 1;\
  128.     write_item(str_ptr, len);\
  129.     free(str_ptr);\
  130.     string_offset += len;\
  131. }
  132.  
  133. MEM_SIZE string_offset;
  134.  
  135. /* Write out the author information:  first the use-counts, then the
  136. ** name strings all packed together.
  137. */
  138. void
  139. write_authors()
  140. {
  141.     register AUTHOR *author;
  142.  
  143.     seq = 0;
  144.     for (author = author_root; author; author = author->link) {
  145.     write_item(&author->count, sizeof (WORD));
  146.     author->seq = seq++;
  147.     }
  148.     if (seq != total.author) {
  149.     log_error("** Wrote %d authors instead of %d **\n",
  150.         seq, total.author);
  151.     }
  152.  
  153.     string_offset = 0;
  154.  
  155.     for (author = author_root; author; author = author->link) {
  156.     write_and_free(author->name);
  157.     }
  158. }
  159.  
  160. /* Write out the subject information: first the packed string data, then
  161. ** the use-counts.  The order is important -- it is the order required
  162. ** by the roots for their subject structures.
  163. */
  164. void
  165. write_subjects()
  166. {
  167.     register ROOT *root;
  168.     register SUBJECT *subject;
  169.  
  170.     for (root = root_root; root; root = root->link) {
  171.     for (subject = root->subjects; subject; subject = subject->link) {
  172.         write_and_free(subject->str);
  173.     }
  174.     }
  175.     if (string_offset != total.string1) {
  176.     log_error("** Author/subject strings were %ld bytes instead of %ld **\n",
  177.         string_offset, total.string1);
  178.     }
  179.  
  180.     seq = 0;
  181.     for (root = root_root; root; root = root->link) {
  182.     for (subject = root->subjects; subject; subject = subject->link) {
  183.         write_item(&subject->count, sizeof (WORD));
  184.         subject->seq = seq++;
  185.     }
  186.     }
  187.     if (seq != total.subject) {
  188.     log_error("** Wrote %d subjects instead of %d **\n",
  189.         seq, total.subject);
  190.     }
  191. }
  192.  
  193. /* Write the roots in a packed format.  Interpret the pointers into
  194. ** sequence numbers as we go.
  195. */
  196. void
  197. write_roots()
  198. {
  199.     register ROOT *root;
  200.  
  201.     for (root = root_root; root; root = root->link) {
  202.     p_root.articles = root->articles->seq;
  203.     p_root.root_num = root->root_num;
  204.     p_root.thread_cnt = root->thread_cnt;
  205.     p_root.subject_cnt = root->subject_cnt;
  206.     write_item(&p_root, sizeof (PACKED_ROOT));
  207.     }
  208. }
  209.  
  210. #define rel_article(article, rseq)    ((article)? (article)->seq - (rseq) : 0)
  211. #define valid_seq(ptr)        ((ptr)? (ptr)->seq : -1)
  212.  
  213. /* Write all the articles in the same order that we sequenced them. */
  214. void
  215. write_articles()
  216. {
  217.     register ROOT *root;
  218.  
  219.     for (root = root_root; root; root = root->link) {
  220.     write_thread(root->articles);
  221.     }
  222. }
  223.  
  224. /* Recursive routine to write the articles in thread order.  We depend on
  225. ** the fact that our first child is the very next article written (if we
  226. ** have children).
  227. */
  228. void
  229. write_thread(article)
  230. register ARTICLE *article;
  231. {
  232.     while (article) {
  233.     p_article.num = article->num;
  234.     p_article.date = article->date;
  235.     p_article.subject = valid_seq(article->subject);
  236.     p_article.author = valid_seq(article->author);
  237.     p_article.flags = article->flags;
  238.     p_article.child_cnt = article->child_cnt;
  239.     p_article.parent = rel_article(article->parent, article->seq);
  240.     p_article.siblings = rel_article(article->siblings, article->seq);
  241.     p_article.root = article->root->seq;
  242.     write_item(&p_article, sizeof (PACKED_ARTICLE));
  243.     if (article->children) {
  244.         write_thread(article->children);
  245.     }
  246.     article = article->siblings;
  247.     }
  248. }
  249.  
  250. WORD minus_one = -1;
  251.  
  252. /* Write the message-id strings:  each domain name (not including the
  253. ** ".unknown." domain) followed by all of its associated unique ids.
  254. ** Then output the article sequence numbers they belong to.  This stuff
  255. ** is last because the newsreader doesn't need to read it.
  256. */
  257. void
  258. write_ids()
  259. {
  260.     register DOMAIN *domain;
  261.     register ARTICLE *id;
  262.     register DOMAIN *next_domain;
  263.     register ARTICLE *next_id;
  264.  
  265.     string_offset = 0;
  266.  
  267.     for (domain = &unk_domain; domain; domain = domain->link) {
  268.     if (domain != &unk_domain) {
  269.         write_and_free(domain->name);
  270.         if (!domain->ids) {
  271.         log_error("** Empty domain name!! **\n");
  272.         }
  273.     }
  274.     for (id = domain->ids; id; id = id->id_link) {
  275.         write_and_free(id->id);
  276.     }
  277.     }
  278.     if (string_offset != total.string2) {
  279.     log_error("** Message-id strings were %ld bytes (%ld) **\n",
  280.         string_offset, total.string2);
  281.     }
  282.     for (domain = &unk_domain; domain; domain = next_domain) {
  283.     next_domain = domain->link;
  284.     for (id = domain->ids; id; id = next_id) {
  285.         next_id = id->id_link;
  286.         write_item(&id->seq, sizeof (WORD));
  287.         free(id);
  288.     }
  289.     write_item(&minus_one, sizeof (WORD));
  290.     if (domain != &unk_domain) {
  291.         free(domain);
  292.     }
  293.     }
  294.     unk_domain.ids = Nullart;
  295.     unk_domain.link = Null(DOMAIN*);
  296. }
  297.  
  298. /* Free everything that's left to free.
  299. */
  300. void
  301. free_leftovers()
  302. {
  303.     register ROOT *root, *next_root;
  304.     register SUBJECT *subj, *next_subj;
  305.     register AUTHOR *author, *next_author;
  306.  
  307.     for (root = root_root; root; root = next_root) {
  308.     next_root = root->link;
  309.     for (subj = root->subjects; subj; subj = next_subj) {
  310.         next_subj = subj->link;
  311.         free(subj);
  312.     }
  313.     free(root);
  314.     }
  315.     for (author = author_root; author; author = next_author) {
  316.     next_author = author->link;
  317.     free(author);
  318.     }
  319.     root_root = Null(ROOT*);
  320.     author_root = Null(AUTHOR*);
  321. }
  322.  
  323. /* This routine will check to be sure that the required path exists for
  324. ** the data file, and if not it will attempt to create it.
  325. */
  326. int
  327. ensure_path(filename)
  328. register char *filename;
  329. {
  330.     int status, pid, w;
  331.     char tmpbuf[1024];
  332. #ifdef MAKEDIR
  333.     register char *cp, *last;
  334.     register char *tbptr = tmpbuf+5;
  335.  
  336.     if (!(last = rindex(filename, '/'))) {    /* find filename portion */
  337.     return 1;                /* no path, we're fine */
  338.     }
  339.     *last = '\0';                /* truncate path at filename */
  340.     strcpy(tmpbuf, "mkdir");
  341.  
  342.     for (cp = last;;) {
  343.     if (stat(filename, &filestat) >= 0 && (filestat.st_mode & S_IFDIR)) {
  344.         *cp = '/';
  345.         break;
  346.     }
  347.     if (!(cp = rindex(filename, '/'))) {/* find something that exists */
  348.         break;
  349.     }
  350.     *cp = '\0';
  351.     }
  352.     
  353.     for (cp = filename; cp <= last; cp++) {
  354.     if (!*cp) {
  355.         sprintf(tbptr, " %s", filename);
  356.         tbptr += strlen(tbptr);        /* set up for mkdir call */
  357.         *cp = '/';
  358.     }
  359.     }
  360.     if (tbptr == tmpbuf+5) {
  361.     return 1;
  362.     }
  363. #else
  364.     sprintf(tmpbuf,"%s %s %d", file_exp(DIRMAKER), filename, 1);
  365. #endif
  366.  
  367.     if ((pid = vfork()) == 0) {
  368.     execl(SH, SH, "-c", tmpbuf, Nullch);
  369.     _exit(127);
  370.     }
  371.     while ((w = wait(&status)) != pid && w != -1) {
  372.     ;
  373.     }
  374.     if (w == -1) {
  375.     status = -1;
  376.     }
  377.     return !status;
  378. }
  379.  
  380. /* A simple routine to output some data only if we haven't failed any
  381. ** previous writes.
  382. */
  383. void
  384. write_item(buff, len)
  385. char *buff;
  386. int len;
  387. {
  388.     if (!failure) {
  389.     if (fwrite(buff, 1, len, fp_out) < len) {
  390.         failure = 1;
  391.     }
  392.     }
  393. }
  394.