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