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