3 * folder_addmsg.c -- Link message into folder
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
17 * Link message into a folder. Return the new number
18 * of the message. If an error occurs, return -1.
22 folder_addmsg (struct msgs **mpp, char *msgfile, int selected,
23 int unseen, int preserve, int deleting)
25 int infd, outfd, linkerr, first_time, msgnum;
26 char *nmsg, newmsg[BUFSIZ];
32 first_time = 1; /* this is first attempt */
36 * We might need to make several attempts
37 * in order to add the message to the folder.
41 * Get the message number we will attempt to add.
44 /* should we preserve the numbering of the message? */
45 if (preserve && (msgnum = m_atoi (msgfile)) > 0) {
47 } else if (mp->nummsg == 0) {
48 /* check if we are adding to empty folder */
51 /* else use highest message number + 1 */
52 msgnum = mp->hghmsg + 1;
56 /* another attempt, so try next higher message number */
61 * See if we need more space. If we need space at the
62 * end, then we allocate space for an addition 100 messages.
63 * If we need space at the beginning of the range, then just
64 * extend message status range to cover this message number.
66 if (msgnum > mp->hghoff) {
67 if ((mp = folder_realloc (mp, mp->lowoff, msgnum + 100)))
70 advise (NULL, "unable to allocate folder storage");
73 } else if (msgnum < mp->lowoff) {
74 if ((mp = folder_realloc (mp, msgnum, mp->hghoff)))
77 advise (NULL, "unable to allocate folder storage");
83 * If a message is already in that slot,
84 * then loop to next available slot.
86 if (does_exist (mp, msgnum))
89 /* setup the bit flags for this message */
90 clear_msg_flags (mp, msgnum);
91 set_exists (mp, msgnum);
93 /* should we set the SELECT_UNSEEN bit? */
95 set_unseen (mp, msgnum);
98 /* should we set the SELECTED bit? */
100 set_selected (mp, msgnum);
102 /* check if highest or lowest selected */
103 if (mp->numsel == 0) {
107 if (msgnum < mp->lowsel)
109 if (msgnum > mp->hghsel)
113 /* increment number selected */
118 * check if this is highest or lowest message
120 if (mp->nummsg == 0) {
124 if (msgnum < mp->lowmsg)
126 if (msgnum > mp->hghmsg)
130 /* increment message count */
133 nmsg = m_name (msgnum);
134 snprintf (newmsg, sizeof(newmsg), "%s/%s", mp->foldpath, nmsg);
137 * Now try to link message into folder.
138 * Then run the external hook on the message if one was specified in the context.
139 * Run the refile hook if we're moving the message from one place to another.
140 * We have to construct the from path name for this because it's not there.
141 * Run the add hook if the message is getting copied or lined somewhere else.
143 if (link (msgfile, newmsg) != -1) {
146 op = folder_read(getfolder(1));
147 (void)snprintf(oldmsg, sizeof (oldmsg), "%s/%s", op->foldpath, msgfile);
149 (void)ext_hook("ref-hook", oldmsg, newmsg);
152 (void)ext_hook("add-hook", newmsg, (char *)0);
159 if (linkerr == EISREMOTE)
161 #endif /* EISREMOTE */
164 * Check if the file in our desired location is the same
165 * as the source file. If so, then just leave it alone
166 * and return. Otherwise, we will continue the main loop
167 * and try again at another slot (hghmsg+1).
169 if (linkerr == EEXIST) {
170 if (stat (msgfile, &st2) == 0 && stat (newmsg, &st1) == 0
171 && st2.st_ino == st1.st_ino) {
179 * If link failed because we are trying to link
180 * across devices, then check if there is a message
181 * already in the desired location. If so, then return
182 * error, else just copy the message.
184 if (linkerr == EXDEV) {
185 if (stat (newmsg, &st1) == 0) {
186 advise (NULL, "message %s:%s already exists", newmsg);
189 if ((infd = open (msgfile, O_RDONLY)) == -1) {
190 advise (msgfile, "unable to open message %s");
194 if ((outfd = creat (newmsg, (int) st1.st_mode & 0777)) == -1) {
195 advise (newmsg, "unable to create");
199 cpydata (infd, outfd, msgfile, newmsg);
204 (void)ext_hook("ref-hook", newmsg, msgfile);
206 (void)ext_hook("add-hook", newmsg, (char *)0);
213 * Else, some other type of link error,
214 * so just return error.
216 advise (newmsg, "error linking %s to", msgfile);