]>
diplodocus.org Git - nmh/blob - sbr/seq_save.c
4731030bd4a82939662b79e94d7ffbca89ab0724
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 "context_del.h"
13 #include "h/signals.h"
14 #include "lock_file.h"
19 * 1. If sequence is public and folder is readonly,
20 * then change it to be private
21 * 2a. If sequence is public, then add it to the sequences file
22 * in folder (name specified by mh-sequences profile entry).
23 * 2b. If sequence is private, then add it to the
28 seq_save (struct msgs
*mp
)
31 char flags
, *cp
, attr
[BUFSIZ
], seqfile
[PATH_MAX
];
35 /* check if sequence information has changed */
36 if (!(mp
->msgflags
& SEQMOD
)) {
38 lkfclosedata (mp
->seqhandle
, mp
->seqname
);
45 mp
->msgflags
&= ~SEQMOD
;
48 flags
= mp
->msgflags
; /* record folder flags */
51 * If no mh-sequences file is defined, or if a mh-sequences file
52 * is defined but empty (*mh_seq == '\0'), then pretend folder
53 * is readonly. This will force all sequences to be private.
55 if (mh_seq
== NULL
|| *mh_seq
== '\0')
58 snprintf (seqfile
, sizeof(seqfile
), "%s/%s", mp
->foldpath
, mh_seq
);
60 for (i
= 0; i
< svector_size (mp
->msgattrs
); i
++) {
61 snprintf (attr
, sizeof(attr
), "atr-%s-%s",
62 svector_at (mp
->msgattrs
, i
), mp
->foldpath
);
64 /* get space separated list of sequence ranges */
65 if (!(cp
= seq_list(mp
, svector_at (mp
->msgattrs
, i
)))) {
66 context_del (attr
); /* delete sequence from context */
70 if (is_readonly(mp
) || is_seq_private(mp
, i
)) {
75 context_replace (attr
, cp
); /* update sequence in context */
80 context_del (attr
); /* delete sequence from context */
83 int failed_to_lock
= 0;
86 * Attempt to open file for public sequences.
87 * If that fails (probably because folder is
88 * readonly), then make sequence private.
97 if (ftruncate(fileno(fp
), 0) < 0) {
98 advise ("sequence file", "ftruncate");
100 } else if ((fp
= lkfopendata (seqfile
, "w", &failed_to_lock
))
102 && (m_unlink (seqfile
) == -1 ||
103 (fp
= lkfopendata (seqfile
, "w", &failed_to_lock
))
105 if (failed_to_lock
) {
106 admonish (attr
, "unable to lock");
108 admonish (attr
, "unable to write");
113 /* block a few signals */
115 sigaddset(&set
, SIGHUP
);
116 sigaddset(&set
, SIGINT
);
117 sigaddset(&set
, SIGQUIT
);
118 sigaddset(&set
, SIGTERM
);
119 sigprocmask (SIG_BLOCK
, &set
, &oset
);
121 fprintf (fp
, "%s: %s\n", svector_at (mp
->msgattrs
, i
), cp
);
126 lkfclosedata (fp
, seqfile
);
127 sigprocmask (SIG_SETMASK
, &oset
, &set
); /* reset signal mask */
130 * If folder is not readonly, and we didn't save any
131 * public sequences, then remove that file.
133 if (!is_readonly(mp
))
134 (void) m_unlink (seqfile
);
138 * Reset folder flag, since we may be
139 * pretending that folder is readonly.
141 mp
->msgflags
= flags
;