2 ** folder_pack.c -- pack (renumber) the messages in a folder
3 ** -- into a contiguous range from 1 to n.
5 ** This code is Copyright (c) 2002, by the authors of nmh. See the
6 ** COPYRIGHT file in the root directory of the nmh distribution for
7 ** complete copyright information.
13 ** Pack the message in a folder.
14 ** Return -1 if error, else return 0.
18 folder_pack(struct msgs **mpp, int verbose)
20 int hole, msgnum, newcurrent = 0;
21 char newmsg[BUFSIZ], oldmsg[BUFSIZ];
27 ** Just return if folder is empty.
33 ** Make sure we have message status space allocated
34 ** for all numbers from 1 to current high message.
37 if ((mp = folder_realloc(mp, 1, mp->hghmsg)))
40 advise(NULL, "unable to allocate folder storage");
45 for (msgnum = mp->lowmsg, hole = 1; msgnum <= mp->hghmsg; msgnum++) {
46 if (does_exist(mp, msgnum)) {
48 strncpy(newmsg, m_name(hole), sizeof(newmsg));
49 strncpy(oldmsg, m_name(msgnum), sizeof(oldmsg));
51 printf("message %s becomes %s\n", oldmsg, newmsg);
54 ** Invoke the external refile hook for each
55 ** message being renamed. This is done
56 ** before the file is renamed so that the
57 ** old message file is around for the hook.
60 snprintf(oldmsg, sizeof (oldmsg), "%s/%d",
61 mp->foldpath, msgnum);
62 snprintf(newmsg, sizeof (newmsg), "%s/%d",
64 ext_hook("ref-hook", oldmsg, newmsg);
66 /* move the message file */
67 if (rename(oldmsg, newmsg) == -1) {
68 advise(newmsg, "unable to rename %s to", oldmsg);
72 /* check if this is the current message */
73 if (msgnum == mp->curmsg)
76 /* copy the attribute flags for this message */
77 copy_msg_flags(mp, hole, msgnum);
79 if (msgnum == mp->lowsel)
81 if (msgnum == mp->hghsel)
85 ** mark that sequence information has
88 mp->msgflags |= SEQMOD;
94 /* record the new number for the high/low message */
96 mp->hghmsg = hole - 1;
99 seq_setcur(mp, newcurrent);