]> diplodocus.org Git - nmh/blob - sbr/folder_addmsg.c
4ea7cd6d88f7873a552773735ddd82dbb980c3c1
[nmh] / sbr / folder_addmsg.c
1 /* folder_addmsg.c -- Link message into folder
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include "h/mh.h"
9 #include "error.h"
10 #include <fcntl.h>
11
12 /*
13 * Link message into a folder. Return the new number
14 * of the message. If an error occurs, return -1.
15 */
16
17 int
18 folder_addmsg (struct msgs **mpp, char *msgfile, int selected,
19 int unseen, int preserve, int deleting, char *from_dir)
20 {
21 int infd, outfd, linkerr, msgnum;
22 char *nmsg, newmsg[BUFSIZ];
23 char oldmsg[BUFSIZ];
24 struct msgs *mp;
25 struct stat st1, st2;
26
27 mp = *mpp;
28
29 /* should we preserve the numbering of the message? */
30 if (preserve && (msgnum = m_atoi (msgfile)) > 0) {
31 ;
32 } else if (mp->nummsg == 0) {
33 /* check if we are adding to empty folder */
34 msgnum = 1;
35 } else {
36 /* else use highest message number + 1 */
37 msgnum = mp->hghmsg + 1;
38 }
39
40 /*
41 * We might need to make several attempts
42 * in order to add the message to the folder.
43 */
44 for (;; msgnum++) {
45
46 /*
47 * See if we need more space. If we need space at the
48 * end, then we allocate space for an addition 100 messages.
49 * If we need space at the beginning of the range, then just
50 * extend message status range to cover this message number.
51 */
52 if (msgnum > mp->hghoff) {
53 if (!(mp = folder_realloc (mp, mp->lowoff, msgnum + 100))) {
54 inform("unable to allocate folder storage");
55 return -1;
56 }
57 *mpp = mp;
58 } else if (msgnum < mp->lowoff) {
59 if (!(mp = folder_realloc (mp, msgnum, mp->hghoff))) {
60 inform("unable to allocate folder storage");
61 return -1;
62 }
63 *mpp = mp;
64 }
65
66 /*
67 * If a message is already in that slot,
68 * then loop to next available slot.
69 */
70 if (does_exist (mp, msgnum))
71 continue;
72
73 /* setup the bit flags for this message */
74 clear_msg_flags (mp, msgnum);
75 set_exists (mp, msgnum);
76
77 /* should we set the SELECT_UNSEEN bit? */
78 if (unseen) {
79 set_unseen (mp, msgnum);
80 }
81
82 /* should we set the SELECTED bit? */
83 if (selected) {
84 set_selected (mp, msgnum);
85
86 /* check if highest or lowest selected */
87 if (mp->numsel == 0) {
88 mp->lowsel = msgnum;
89 mp->hghsel = msgnum;
90 } else {
91 if (msgnum < mp->lowsel)
92 mp->lowsel = msgnum;
93 if (msgnum > mp->hghsel)
94 mp->hghsel = msgnum;
95 }
96
97 /* increment number selected */
98 mp->numsel++;
99 }
100
101 /*
102 * check if this is highest or lowest message
103 */
104 if (mp->nummsg == 0) {
105 mp->lowmsg = msgnum;
106 mp->hghmsg = msgnum;
107 } else {
108 if (msgnum < mp->lowmsg)
109 mp->lowmsg = msgnum;
110 if (msgnum > mp->hghmsg)
111 mp->hghmsg = msgnum;
112 }
113
114 /* increment message count */
115 mp->nummsg++;
116
117 nmsg = m_name (msgnum);
118 snprintf (newmsg, sizeof(newmsg), "%s/%s", mp->foldpath, nmsg);
119
120 /*
121 * Now try to link message into folder.
122 * Then run the external hook on the message if one was specified in the context.
123 * Run the refile hook if we're moving the message from one place to another.
124 * We have to construct the from path name for this because it's not there.
125 * Run the add hook if the message is getting copied or linked somewhere else.
126 */
127 if (link (msgfile, newmsg) != -1) {
128 if (deleting) {
129 (void)snprintf(oldmsg, sizeof (oldmsg), "%s/%s", from_dir, msgfile);
130 (void)ext_hook("ref-hook", oldmsg, newmsg);
131 }
132 else
133 (void)ext_hook("add-hook", newmsg, NULL);
134
135 return msgnum;
136 }
137 linkerr = errno;
138
139 #ifdef EISREMOTE
140 if (linkerr == EISREMOTE)
141 linkerr = EXDEV;
142 #endif /* EISREMOTE */
143
144 /*
145 * Check if the file in our desired location is the same
146 * as the source file. If so, then just leave it alone
147 * and return. Otherwise, we will continue the main loop
148 * and try again at another slot (hghmsg+1).
149 */
150 if (linkerr == EEXIST) {
151 if (stat (msgfile, &st2) == 0 && stat (newmsg, &st1) == 0
152 && st2.st_ino == st1.st_ino) {
153 return msgnum;
154 }
155 continue;
156 }
157
158 /*
159 * If link failed because we are trying to link
160 * across devices, then check if there is a message
161 * already in the desired location. If so, then return
162 * error, else just copy the message.
163 * Cygwin with FAT32 filesystem produces EPERM.
164 */
165 if (linkerr == EXDEV || linkerr == EPERM) {
166 if (stat (newmsg, &st1) == 0) {
167 inform("message %s:%s already exists", mp->foldpath, newmsg);
168 return -1;
169 }
170
171 if ((infd = open (msgfile, O_RDONLY)) == -1) {
172 advise (msgfile, "unable to open message %s", msgfile);
173 return -1;
174 }
175 fstat (infd, &st1);
176 if ((outfd = creat (newmsg, (int) st1.st_mode & 0777)) == -1) {
177 advise (newmsg, "unable to create");
178 close (infd);
179 return -1;
180 }
181 cpydata (infd, outfd, msgfile, newmsg);
182 close (infd);
183 close (outfd);
184
185 if (deleting) {
186 (void)snprintf(oldmsg, sizeof (oldmsg), "%s/%s", from_dir, msgfile);
187 (void)ext_hook("ref-hook", oldmsg, newmsg);
188 }
189 else
190 (void)ext_hook("add-hook", newmsg, NULL);
191
192 return msgnum;
193 }
194
195 /*
196 * Else, some other type of link error,
197 * so just return error.
198 */
199 advise (newmsg, "error linking %s to", msgfile);
200 return -1;
201 }
202 }