]> diplodocus.org Git - nmh/blob - sbr/folder_pack.c
new.c: Order two return statements to match comment.
[nmh] / sbr / folder_pack.c
1 /* folder_pack.c -- pack (renumber) the messages in a folder
2 * -- into a contiguous range from 1 to n.
3 *
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.
7 */
8
9 #include <h/mh.h>
10
11 /*
12 * Pack the message in a folder.
13 * Return -1 if error, else return 0.
14 */
15
16 int
17 folder_pack (struct msgs **mpp, int verbose)
18 {
19 int hole, msgnum, newcurrent = 0;
20 char newmsg[BUFSIZ], oldmsg[BUFSIZ];
21 struct msgs *mp;
22
23 mp = *mpp;
24
25 /*
26 * Just return if folder is empty.
27 */
28 if (mp->nummsg == 0)
29 return 0;
30
31 /*
32 * Make sure we have message status space allocated
33 * for all numbers from 1 to current high message.
34 */
35 if (mp->lowoff > 1) {
36 if (!(mp = folder_realloc(mp, 1, mp->hghmsg))) {
37 inform("unable to allocate folder storage");
38 return -1;
39 }
40 *mpp = mp;
41 }
42
43 for (msgnum = mp->lowmsg, hole = 1; msgnum <= mp->hghmsg; msgnum++) {
44 if (does_exist (mp, msgnum)) {
45 if (msgnum != hole) {
46 strncpy (newmsg, m_name (hole), sizeof(newmsg));
47 strncpy (oldmsg, m_name (msgnum), sizeof(oldmsg));
48 if (verbose)
49 printf ("message %s becomes %s\n", oldmsg, newmsg);
50
51 /*
52 * Invoke the external refile hook for each message being renamed.
53 * This is done before the file is renamed so that the old message
54 * file is around for the hook.
55 */
56
57 (void)snprintf(oldmsg, sizeof (oldmsg), "%s/%d", mp->foldpath, msgnum);
58 (void)snprintf(newmsg, sizeof (newmsg), "%s/%d", mp->foldpath, hole);
59 ext_hook("ref-hook", oldmsg, newmsg);
60
61 /* move the message file */
62 if (rename (oldmsg, newmsg) == -1) {
63 advise (newmsg, "unable to rename %s to", oldmsg);
64 return -1;
65 }
66
67 /* check if this is the current message */
68 if (msgnum == mp->curmsg)
69 newcurrent = hole;
70
71 /* copy the attribute flags for this message */
72 copy_msg_flags (mp, hole, msgnum);
73
74 if (msgnum == mp->lowsel)
75 mp->lowsel = hole;
76 if (msgnum == mp->hghsel)
77 mp->hghsel = hole;
78
79 /* mark that sequence information has been modified */
80 mp->msgflags |= SEQMOD;
81 }
82 hole++;
83 }
84 }
85
86 /* record the new number for the high/low message */
87 mp->lowmsg = 1;
88 mp->hghmsg = hole - 1;
89
90 /* update the "cur" sequence */
91 if (newcurrent != 0)
92 seq_setcur (mp, newcurrent);
93
94 return 0;
95 }