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)
25 int infd, outfd, linkerr, first_time, msgnum;
26 char *nmsg, newmsg[BUFSIZ];
30 first_time = 1; /* this is first attempt */
34 * We might need to make several attempts
35 * in order to add the message to the folder.
39 * Get the message number we will attempt to add.
42 /* should we preserve the numbering of the message? */
43 if (preserve && (msgnum = m_atoi (msgfile)) > 0) {
45 } else if (mp->nummsg == 0) {
46 /* check if we are adding to empty folder */
49 /* else use highest message number + 1 */
50 msgnum = mp->hghmsg + 1;
54 /* another attempt, so try next higher message number */
59 * See if we need more space. If we need space at the
60 * end, then we allocate space for an addition 100 messages.
61 * If we need space at the beginning of the range, then just
62 * extend message status range to cover this message number.
64 if (msgnum > mp->hghoff) {
65 if ((mp = folder_realloc (mp, mp->lowoff, msgnum + 100)))
68 advise (NULL, "unable to allocate folder storage");
71 } else if (msgnum < mp->lowoff) {
72 if ((mp = folder_realloc (mp, msgnum, mp->hghoff)))
75 advise (NULL, "unable to allocate folder storage");
81 * If a message is already in that slot,
82 * then loop to next available slot.
84 if (does_exist (mp, msgnum))
87 /* setup the bit flags for this message */
88 clear_msg_flags (mp, msgnum);
89 set_exists (mp, msgnum);
91 /* should we set the SELECT_UNSEEN bit? */
93 set_unseen (mp, msgnum);
96 /* should we set the SELECTED bit? */
98 set_selected (mp, msgnum);
100 /* check if highest or lowest selected */
101 if (mp->numsel == 0) {
105 if (msgnum < mp->lowsel)
107 if (msgnum > mp->hghsel)
111 /* increment number selected */
116 * check if this is highest or lowest message
118 if (mp->nummsg == 0) {
122 if (msgnum < mp->lowmsg)
124 if (msgnum > mp->hghmsg)
128 /* increment message count */
131 nmsg = m_name (msgnum);
132 snprintf (newmsg, sizeof(newmsg), "%s/%s", mp->foldpath, nmsg);
135 * Now try to link message into folder
137 if (link (msgfile, newmsg) != -1) {
143 if (linkerr == EISREMOTE)
145 #endif /* EISREMOTE */
148 * Check if the file in our desired location is the same
149 * as the source file. If so, then just leave it alone
150 * and return. Otherwise, we will continue the main loop
151 * and try again at another slot (hghmsg+1).
153 if (linkerr == EEXIST) {
154 if (stat (msgfile, &st2) == 0 && stat (newmsg, &st1) == 0
155 && st2.st_ino == st1.st_ino) {
163 * If link failed because we are trying to link
164 * across devices, then check if there is a message
165 * already in the desired location. If so, then return
166 * error, else just copy the message.
168 if (linkerr == EXDEV) {
169 if (stat (newmsg, &st1) == 0) {
170 advise (NULL, "message %s:%s already exists", newmsg);
173 if ((infd = open (msgfile, O_RDONLY)) == -1) {
174 advise (msgfile, "unable to open message %s");
178 if ((outfd = creat (newmsg, (int) st1.st_mode & 0777)) == -1) {
179 advise (newmsg, "unable to create");
183 cpydata (infd, outfd, msgfile, newmsg);
191 * Else, some other type of link error,
192 * so just return error.
194 advise (newmsg, "error linking %s to", msgfile);