]>
diplodocus.org Git - nmh/blob - sbr/seq_save.c
e829a75f0082efff96eadc9cf329ceb83bf7db3b
1 /* seq_save.c -- 1) synchronize sequences
2 * -- 2) save public sequences
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
12 #include "h/signals.h"
13 #include "lock_file.h"
18 * 1. If sequence is public and folder is readonly,
19 * then change it to be private
20 * 2a. If sequence is public, then add it to the sequences file
21 * in folder (name specified by mh-sequences profile entry).
22 * 2b. If sequence is private, then add it to the
27 seq_save (struct msgs
*mp
)
30 char flags
, *cp
, attr
[BUFSIZ
], seqfile
[PATH_MAX
];
34 /* check if sequence information has changed */
35 if (!(mp
->msgflags
& SEQMOD
)) {
37 lkfclosedata (mp
->seqhandle
, mp
->seqname
);
44 mp
->msgflags
&= ~SEQMOD
;
47 flags
= mp
->msgflags
; /* record folder flags */
50 * If no mh-sequences file is defined, or if a mh-sequences file
51 * is defined but empty (*mh_seq == '\0'), then pretend folder
52 * is readonly. This will force all sequences to be private.
54 if (mh_seq
== NULL
|| *mh_seq
== '\0')
57 snprintf (seqfile
, sizeof(seqfile
), "%s/%s", mp
->foldpath
, mh_seq
);
59 for (i
= 0; i
< svector_size (mp
->msgattrs
); i
++) {
60 snprintf (attr
, sizeof(attr
), "atr-%s-%s",
61 svector_at (mp
->msgattrs
, i
), mp
->foldpath
);
63 /* get space separated list of sequence ranges */
64 if (!(cp
= seq_list(mp
, svector_at (mp
->msgattrs
, i
)))) {
65 context_del (attr
); /* delete sequence from context */
69 if (is_readonly(mp
) || is_seq_private(mp
, i
)) {
74 context_replace (attr
, cp
); /* update sequence in context */
79 context_del (attr
); /* delete sequence from context */
82 int failed_to_lock
= 0;
85 * Attempt to open file for public sequences.
86 * If that fails (probably because folder is
87 * readonly), then make sequence private.
96 if (ftruncate(fileno(fp
), 0) < 0) {
97 advise ("sequence file", "ftruncate");
99 } else if ((fp
= lkfopendata (seqfile
, "w", &failed_to_lock
))
101 && (m_unlink (seqfile
) == -1 ||
102 (fp
= lkfopendata (seqfile
, "w", &failed_to_lock
))
104 if (failed_to_lock
) {
105 admonish (attr
, "unable to lock");
107 admonish (attr
, "unable to write");
112 /* block a few signals */
114 sigaddset(&set
, SIGHUP
);
115 sigaddset(&set
, SIGINT
);
116 sigaddset(&set
, SIGQUIT
);
117 sigaddset(&set
, SIGTERM
);
118 sigprocmask (SIG_BLOCK
, &set
, &oset
);
120 fprintf (fp
, "%s: %s\n", svector_at (mp
->msgattrs
, i
), cp
);
125 lkfclosedata (fp
, seqfile
);
126 sigprocmask (SIG_SETMASK
, &oset
, &set
); /* reset signal mask */
129 * If folder is not readonly, and we didn't save any
130 * public sequences, then remove that file.
132 if (!is_readonly(mp
))
133 (void) m_unlink (seqfile
);
137 * Reset folder flag, since we may be
138 * pretending that folder is readonly.
140 mp
->msgflags
= flags
;