]>
diplodocus.org Git - nmh/blob - sbr/folder_addmsg.c
3 * folder_addmsg.c -- Link message into folder
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
14 * Link message into a folder. Return the new number
15 * of the message. If an error occurs, return -1.
19 folder_addmsg (struct msgs
**mpp
, char *msgfile
, int selected
,
20 int unseen
, int preserve
, int deleting
, char *from_dir
)
22 int infd
, outfd
, linkerr
, msgnum
;
23 char *nmsg
, newmsg
[BUFSIZ
];
30 /* should we preserve the numbering of the message? */
31 if (preserve
&& (msgnum
= m_atoi (msgfile
)) > 0) {
33 } else if (mp
->nummsg
== 0) {
34 /* check if we are adding to empty folder */
37 /* else use highest message number + 1 */
38 msgnum
= mp
->hghmsg
+ 1;
42 * We might need to make several attempts
43 * in order to add the message to the folder.
48 * See if we need more space. If we need space at the
49 * end, then we allocate space for an addition 100 messages.
50 * If we need space at the beginning of the range, then just
51 * extend message status range to cover this message number.
53 if (msgnum
> mp
->hghoff
) {
54 if ((mp
= folder_realloc (mp
, mp
->lowoff
, msgnum
+ 100)))
57 advise (NULL
, "unable to allocate folder storage");
60 } else if (msgnum
< mp
->lowoff
) {
61 if ((mp
= folder_realloc (mp
, msgnum
, mp
->hghoff
)))
64 advise (NULL
, "unable to allocate folder storage");
70 * If a message is already in that slot,
71 * then loop to next available slot.
73 if (does_exist (mp
, msgnum
))
76 /* setup the bit flags for this message */
77 clear_msg_flags (mp
, msgnum
);
78 set_exists (mp
, msgnum
);
80 /* should we set the SELECT_UNSEEN bit? */
82 set_unseen (mp
, msgnum
);
85 /* should we set the SELECTED bit? */
87 set_selected (mp
, msgnum
);
89 /* check if highest or lowest selected */
90 if (mp
->numsel
== 0) {
94 if (msgnum
< mp
->lowsel
)
96 if (msgnum
> mp
->hghsel
)
100 /* increment number selected */
105 * check if this is highest or lowest message
107 if (mp
->nummsg
== 0) {
111 if (msgnum
< mp
->lowmsg
)
113 if (msgnum
> mp
->hghmsg
)
117 /* increment message count */
120 nmsg
= m_name (msgnum
);
121 snprintf (newmsg
, sizeof(newmsg
), "%s/%s", mp
->foldpath
, nmsg
);
124 * Now try to link message into folder.
125 * Then run the external hook on the message if one was specified in the context.
126 * Run the refile hook if we're moving the message from one place to another.
127 * We have to construct the from path name for this because it's not there.
128 * Run the add hook if the message is getting copied or linked somewhere else.
130 if (link (msgfile
, newmsg
) != -1) {
133 (void)snprintf(oldmsg
, sizeof (oldmsg
), "%s/%s", from_dir
, msgfile
);
134 (void)ext_hook("ref-hook", oldmsg
, newmsg
);
137 (void)ext_hook("add-hook", newmsg
, (char *)0);
144 if (linkerr
== EISREMOTE
)
146 #endif /* EISREMOTE */
149 * Check if the file in our desired location is the same
150 * as the source file. If so, then just leave it alone
151 * and return. Otherwise, we will continue the main loop
152 * and try again at another slot (hghmsg+1).
154 if (linkerr
== EEXIST
) {
155 if (stat (msgfile
, &st2
) == 0 && stat (newmsg
, &st1
) == 0
156 && st2
.st_ino
== st1
.st_ino
) {
164 * If link failed because we are trying to link
165 * across devices, then check if there is a message
166 * already in the desired location. If so, then return
167 * error, else just copy the message.
168 * Cygwin with FAT32 filesystem produces EPERM.
170 if (linkerr
== EXDEV
|| linkerr
== EPERM
) {
171 if (stat (newmsg
, &st1
) == 0) {
172 advise (NULL
, "message %s:%s already exists", mp
->foldpath
, newmsg
);
175 if ((infd
= open (msgfile
, O_RDONLY
)) == -1) {
176 advise (msgfile
, "unable to open message %s", msgfile
);
180 if ((outfd
= creat (newmsg
, (int) st1
.st_mode
& 0777)) == -1) {
181 advise (newmsg
, "unable to create");
185 cpydata (infd
, outfd
, msgfile
, newmsg
);
190 (void)snprintf(oldmsg
, sizeof (oldmsg
), "%s/%s", from_dir
, msgfile
);
191 (void)ext_hook("ref-hook", oldmsg
, newmsg
);
194 (void)ext_hook("add-hook", newmsg
, (char *)0);
201 * Else, some other type of link error,
202 * so just return error.
204 advise (newmsg
, "error linking %s to", msgfile
);