3 * folder_pack.c -- pack (renumber) the messages in a folder
4 * -- into a contiguous range from 1 to n.
8 * This code is Copyright (c) 2002, by the authors of nmh. See the
9 * COPYRIGHT file in the root directory of the nmh distribution for
10 * complete copyright information.
16 * Pack the message in a folder.
17 * Return -1 if error, else return 0.
21 folder_pack (struct msgs **mpp, int verbose)
23 int hole, msgnum, newcurrent = 0;
24 char newmsg[BUFSIZ], oldmsg[BUFSIZ];
30 * Just return if folder is empty.
36 * Make sure we have message status space allocated
37 * for all numbers from 1 to current high message.
40 if ((mp = folder_realloc (mp, 1, mp->hghmsg)))
43 advise (NULL, "unable to allocate folder storage");
48 for (msgnum = mp->lowmsg, hole = 1; msgnum <= mp->hghmsg; msgnum++) {
49 if (does_exist (mp, msgnum)) {
51 strncpy (newmsg, m_name (hole), sizeof(newmsg));
52 strncpy (oldmsg, m_name (msgnum), sizeof(oldmsg));
54 printf ("message %s becomes %s\n", oldmsg, newmsg);
56 /* move the message file */
57 if (rename (oldmsg, newmsg) == -1) {
58 advise (newmsg, "unable to rename %s to", oldmsg);
63 * Invoke the external refile hook for each message being renamed.
66 (void)snprintf(oldmsg, sizeof (oldmsg), "%s/%d", mp->foldpath, msgnum);
67 (void)snprintf(newmsg, sizeof (newmsg), "%s/%d", mp->foldpath, hole);
68 ext_hook("ref-hook", oldmsg, newmsg);
70 /* check if this is the current message */
71 if (msgnum == mp->curmsg)
74 /* copy the attribute flags for this message */
75 copy_msg_flags (mp, hole, msgnum);
77 if (msgnum == mp->lowsel)
79 if (msgnum == mp->hghsel)
82 /* mark that sequence information has been modified */
83 mp->msgflags |= SEQMOD;
89 /* record the new number for the high/low message */
91 mp->hghmsg = hole - 1;
93 /* update the "cur" sequence */
95 seq_setcur (mp, newcurrent);