]>
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>
15 * 1. If sequence is public and folder is readonly,
16 * then change it to be private
17 * 2a. If sequence is public, then add it to the sequences file
18 * in folder (name specified by mh-sequences profile entry).
19 * 2b. If sequence is private, then add it to the
24 seq_save (struct msgs
*mp
)
27 char flags
, *cp
, attr
[BUFSIZ
], seqfile
[PATH_MAX
];
31 /* check if sequence information has changed */
32 if (!(mp
->msgflags
& SEQMOD
)) {
34 lkfclosedata (mp
->seqhandle
, mp
->seqname
);
41 mp
->msgflags
&= ~SEQMOD
;
44 flags
= mp
->msgflags
; /* record folder flags */
47 * If no mh-sequences file is defined, or if a mh-sequences file
48 * is defined but empty (*mh_seq == '\0'), then pretend folder
49 * is readonly. This will force all sequences to be private.
51 if (mh_seq
== NULL
|| *mh_seq
== '\0')
54 snprintf (seqfile
, sizeof(seqfile
), "%s/%s", mp
->foldpath
, mh_seq
);
56 for (i
= 0; i
< svector_size (mp
->msgattrs
); i
++) {
57 snprintf (attr
, sizeof(attr
), "atr-%s-%s",
58 svector_at (mp
->msgattrs
, i
), mp
->foldpath
);
60 /* get space separated list of sequence ranges */
61 if (!(cp
= seq_list(mp
, svector_at (mp
->msgattrs
, i
)))) {
62 context_del (attr
); /* delete sequence from context */
66 if (is_readonly(mp
) || is_seq_private(mp
, i
)) {
71 context_replace (attr
, cp
); /* update sequence in context */
76 context_del (attr
); /* delete sequence from context */
79 int failed_to_lock
= 0;
82 * Attempt to open file for public sequences.
83 * If that fails (probably because folder is
84 * readonly), then make sequence private.
93 if (ftruncate(fileno(fp
), 0) < 0) {
94 advise ("sequence file", "ftruncate");
96 } else if ((fp
= lkfopendata (seqfile
, "w", &failed_to_lock
))
98 && (m_unlink (seqfile
) == -1 ||
99 (fp
= lkfopendata (seqfile
, "w", &failed_to_lock
))
101 if (failed_to_lock
) {
102 admonish (attr
, "unable to lock");
104 admonish (attr
, "unable to write");
109 /* block a few signals */
111 sigaddset(&set
, SIGHUP
);
112 sigaddset(&set
, SIGINT
);
113 sigaddset(&set
, SIGQUIT
);
114 sigaddset(&set
, SIGTERM
);
115 sigprocmask (SIG_BLOCK
, &set
, &oset
);
117 fprintf (fp
, "%s: %s\n", svector_at (mp
->msgattrs
, i
), cp
);
122 lkfclosedata (fp
, seqfile
);
123 sigprocmask (SIG_SETMASK
, &oset
, &set
); /* reset signal mask */
126 * If folder is not readonly, and we didn't save any
127 * public sequences, then remove that file.
129 if (!is_readonly(mp
))
130 (void) m_unlink (seqfile
);
134 * Reset folder flag, since we may be
135 * pretending that folder is readonly.
137 mp
->msgflags
= flags
;