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