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, char *from_dir)
25 int infd, outfd, linkerr, first_time, msgnum;
26 char *nmsg, newmsg[BUFSIZ];
31 first_time = 1; /* this is first attempt */
35 * We might need to make several attempts
36 * in order to add the message to the folder.
40 * Get the message number we will attempt to add.
43 /* should we preserve the numbering of the message? */
44 if (preserve && (msgnum = m_atoi (msgfile)) > 0) {
46 } else if (mp->nummsg == 0) {
47 /* check if we are adding to empty folder */
50 /* else use highest message number + 1 */
51 msgnum = mp->hghmsg + 1;
55 /* another attempt, so try next higher message number */
60 * See if we need more space. If we need space at the
61 * end, then we allocate space for an addition 100 messages.
62 * If we need space at the beginning of the range, then just
63 * extend message status range to cover this message number.
65 if (msgnum > mp->hghoff) {
66 if ((mp = folder_realloc (mp, mp->lowoff, msgnum + 100)))
69 advise (NULL, "unable to allocate folder storage");
72 } else if (msgnum < mp->lowoff) {
73 if ((mp = folder_realloc (mp, msgnum, mp->hghoff)))
76 advise (NULL, "unable to allocate folder storage");
82 * If a message is already in that slot,
83 * then loop to next available slot.
85 if (does_exist (mp, msgnum))
88 /* setup the bit flags for this message */
89 clear_msg_flags (mp, msgnum);
90 set_exists (mp, msgnum);
92 /* should we set the SELECT_UNSEEN bit? */
94 set_unseen (mp, msgnum);
97 /* should we set the SELECTED bit? */
99 set_selected (mp, msgnum);
101 /* check if highest or lowest selected */
102 if (mp->numsel == 0) {
106 if (msgnum < mp->lowsel)
108 if (msgnum > mp->hghsel)
112 /* increment number selected */
117 * check if this is highest or lowest message
119 if (mp->nummsg == 0) {
123 if (msgnum < mp->lowmsg)
125 if (msgnum > mp->hghmsg)
129 /* increment message count */
132 nmsg = m_name (msgnum);
133 snprintf (newmsg, sizeof(newmsg), "%s/%s", mp->foldpath, nmsg);
136 * Now try to link message into folder.
137 * Then run the external hook on the message if one was specified in the context.
138 * Run the refile hook if we're moving the message from one place to another.
139 * We have to construct the from path name for this because it's not there.
140 * Run the add hook if the message is getting copied or linked somewhere else.
142 if (link (msgfile, newmsg) != -1) {
145 (void)snprintf(oldmsg, sizeof (oldmsg), "%s/%s", from_dir, msgfile);
146 (void)ext_hook("ref-hook", oldmsg, newmsg);
149 (void)ext_hook("add-hook", newmsg, (char *)0);
156 if (linkerr == EISREMOTE)
158 #endif /* EISREMOTE */
161 * Check if the file in our desired location is the same
162 * as the source file. If so, then just leave it alone
163 * and return. Otherwise, we will continue the main loop
164 * and try again at another slot (hghmsg+1).
166 if (linkerr == EEXIST) {
167 if (stat (msgfile, &st2) == 0 && stat (newmsg, &st1) == 0
168 && st2.st_ino == st1.st_ino) {
176 * If link failed because we are trying to link
177 * across devices, then check if there is a message
178 * already in the desired location. If so, then return
179 * error, else just copy the message.
181 if (linkerr == EXDEV) {
182 if (stat (newmsg, &st1) == 0) {
183 advise (NULL, "message %s:%s already exists", mp->foldpath, newmsg);
186 if ((infd = open (msgfile, O_RDONLY)) == -1) {
187 advise (msgfile, "unable to open message %s", msgfile);
191 if ((outfd = creat (newmsg, (int) st1.st_mode & 0777)) == -1) {
192 advise (newmsg, "unable to create");
196 cpydata (infd, outfd, msgfile, newmsg);
201 (void)snprintf(oldmsg, sizeof (oldmsg), "%s/%s", from_dir, msgfile);
202 (void)ext_hook("ref-hook", oldmsg, newmsg);
205 (void)ext_hook("add-hook", newmsg, (char *)0);
212 * Else, some other type of link error,
213 * so just return error.
215 advise (newmsg, "error linking %s to", msgfile);