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