]>
diplodocus.org Git - nmh/blob - sbr/seq_save.c
3 * seq_save.c -- 1) synchronize sequences
4 * -- 2) save public sequences
6 * This code is Copyright (c) 2002, by the authors of nmh. See the
7 * COPYRIGHT file in the root directory of the nmh distribution for
8 * complete copyright information.
12 #include <h/signals.h>
16 * 1. If sequence is public and folder is readonly,
17 * then change it to be private
18 * 2a. If sequence is public, then add it to the sequences file
19 * in folder (name specified by mh-sequences profile entry).
20 * 2b. If sequence is private, then add it to the
25 seq_save (struct msgs
*mp
)
28 char flags
, *cp
, attr
[BUFSIZ
], seqfile
[PATH_MAX
];
32 /* check if sequence information has changed */
33 if (!(mp
->msgflags
& SEQMOD
)) {
35 lkfclosedata (mp
->seqhandle
, mp
->seqname
);
42 mp
->msgflags
&= ~SEQMOD
;
45 flags
= mp
->msgflags
; /* record folder flags */
48 * If no mh-sequences file is defined, or if a mh-sequences file
49 * is defined but empty (*mh_seq == '\0'), then pretend folder
50 * is readonly. This will force all sequences to be private.
52 if (mh_seq
== NULL
|| *mh_seq
== '\0')
55 snprintf (seqfile
, sizeof(seqfile
), "%s/%s", mp
->foldpath
, mh_seq
);
57 for (i
= 0; i
< svector_size (mp
->msgattrs
); i
++) {
58 snprintf (attr
, sizeof(attr
), "atr-%s-%s",
59 svector_at (mp
->msgattrs
, i
), mp
->foldpath
);
61 /* get space separated list of sequence ranges */
62 if (!(cp
= seq_list(mp
, svector_at (mp
->msgattrs
, i
)))) {
63 context_del (attr
); /* delete sequence from context */
67 if (is_readonly(mp
) || is_seq_private(mp
, i
)) {
72 context_replace (attr
, cp
); /* update sequence in context */
77 context_del (attr
); /* delete sequence from context */
80 int failed_to_lock
= 0;
83 * Attempt to open file for public sequences.
84 * If that fails (probably because folder is
85 * readonly), then make sequence private.
94 if (ftruncate(fileno(fp
), 0) < 0) {
95 advise ("sequence file", "ftruncate");
97 } else if ((fp
= lkfopendata (seqfile
, "w", &failed_to_lock
))
99 && (m_unlink (seqfile
) == -1 ||
100 (fp
= lkfopendata (seqfile
, "w", &failed_to_lock
))
102 if (failed_to_lock
) {
103 admonish (attr
, "unable to lock");
105 admonish (attr
, "unable to write");
110 /* block a few signals */
112 sigaddset(&set
, SIGHUP
);
113 sigaddset(&set
, SIGINT
);
114 sigaddset(&set
, SIGQUIT
);
115 sigaddset(&set
, SIGTERM
);
116 sigprocmask (SIG_BLOCK
, &set
, &oset
);
118 fprintf (fp
, "%s: %s\n", svector_at (mp
->msgattrs
, i
), cp
);
123 lkfclosedata (fp
, seqfile
);
124 sigprocmask (SIG_SETMASK
, &oset
, &set
); /* reset signal mask */
127 * If folder is not readonly, and we didn't save any
128 * public sequences, then remove that file.
130 if (!is_readonly(mp
))
131 (void) m_unlink (seqfile
);
135 * Reset folder flag, since we may be
136 * pretending that folder is readonly.
138 mp
->msgflags
= flags
;