]>
diplodocus.org Git - nmh/blob - sbr/seq_save.c
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.
10 #include <h/signals.h>
14 * 1. If sequence is public and folder is readonly,
15 * then change it to be private
16 * 2a. If sequence is public, then add it to the sequences file
17 * in folder (name specified by mh-sequences profile entry).
18 * 2b. If sequence is private, then add it to the
23 seq_save (struct msgs
*mp
)
26 char flags
, *cp
, attr
[BUFSIZ
], seqfile
[PATH_MAX
];
30 /* check if sequence information has changed */
31 if (!(mp
->msgflags
& SEQMOD
)) {
33 lkfclosedata (mp
->seqhandle
, mp
->seqname
);
40 mp
->msgflags
&= ~SEQMOD
;
43 flags
= mp
->msgflags
; /* record folder flags */
46 * If no mh-sequences file is defined, or if a mh-sequences file
47 * is defined but empty (*mh_seq == '\0'), then pretend folder
48 * is readonly. This will force all sequences to be private.
50 if (mh_seq
== NULL
|| *mh_seq
== '\0')
53 snprintf (seqfile
, sizeof(seqfile
), "%s/%s", mp
->foldpath
, mh_seq
);
55 for (i
= 0; i
< svector_size (mp
->msgattrs
); i
++) {
56 snprintf (attr
, sizeof(attr
), "atr-%s-%s",
57 svector_at (mp
->msgattrs
, i
), mp
->foldpath
);
59 /* get space separated list of sequence ranges */
60 if (!(cp
= seq_list(mp
, svector_at (mp
->msgattrs
, i
)))) {
61 context_del (attr
); /* delete sequence from context */
65 if (is_readonly(mp
) || is_seq_private(mp
, i
)) {
70 context_replace (attr
, cp
); /* update sequence in context */
75 context_del (attr
); /* delete sequence from context */
78 int failed_to_lock
= 0;
81 * Attempt to open file for public sequences.
82 * If that fails (probably because folder is
83 * readonly), then make sequence private.
92 if (ftruncate(fileno(fp
), 0) < 0) {
93 advise ("sequence file", "ftruncate");
95 } else if ((fp
= lkfopendata (seqfile
, "w", &failed_to_lock
))
97 && (m_unlink (seqfile
) == -1 ||
98 (fp
= lkfopendata (seqfile
, "w", &failed_to_lock
))
100 if (failed_to_lock
) {
101 admonish (attr
, "unable to lock");
103 admonish (attr
, "unable to write");
108 /* block a few signals */
110 sigaddset(&set
, SIGHUP
);
111 sigaddset(&set
, SIGINT
);
112 sigaddset(&set
, SIGQUIT
);
113 sigaddset(&set
, SIGTERM
);
114 sigprocmask (SIG_BLOCK
, &set
, &oset
);
116 fprintf (fp
, "%s: %s\n", svector_at (mp
->msgattrs
, i
), cp
);
121 lkfclosedata (fp
, seqfile
);
122 sigprocmask (SIG_SETMASK
, &oset
, &set
); /* reset signal mask */
125 * If folder is not readonly, and we didn't save any
126 * public sequences, then remove that file.
128 if (!is_readonly(mp
))
129 (void) m_unlink (seqfile
);
133 * Reset folder flag, since we may be
134 * pretending that folder is readonly.
136 mp
->msgflags
= flags
;