]>
diplodocus.org Git - nmh/blob - sbr/folder_addmsg.c
4ea7cd6d88f7873a552773735ddd82dbb980c3c1
1 /* folder_addmsg.c -- Link message into folder
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.
13 * Link message into a folder. Return the new number
14 * of the message. If an error occurs, return -1.
18 folder_addmsg (struct msgs
**mpp
, char *msgfile
, int selected
,
19 int unseen
, int preserve
, int deleting
, char *from_dir
)
21 int infd
, outfd
, linkerr
, msgnum
;
22 char *nmsg
, newmsg
[BUFSIZ
];
29 /* should we preserve the numbering of the message? */
30 if (preserve
&& (msgnum
= m_atoi (msgfile
)) > 0) {
32 } else if (mp
->nummsg
== 0) {
33 /* check if we are adding to empty folder */
36 /* else use highest message number + 1 */
37 msgnum
= mp
->hghmsg
+ 1;
41 * We might need to make several attempts
42 * in order to add the message to the folder.
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.
52 if (msgnum
> mp
->hghoff
) {
53 if (!(mp
= folder_realloc (mp
, mp
->lowoff
, msgnum
+ 100))) {
54 inform("unable to allocate folder storage");
58 } else if (msgnum
< mp
->lowoff
) {
59 if (!(mp
= folder_realloc (mp
, msgnum
, mp
->hghoff
))) {
60 inform("unable to allocate folder storage");
67 * If a message is already in that slot,
68 * then loop to next available slot.
70 if (does_exist (mp
, msgnum
))
73 /* setup the bit flags for this message */
74 clear_msg_flags (mp
, msgnum
);
75 set_exists (mp
, msgnum
);
77 /* should we set the SELECT_UNSEEN bit? */
79 set_unseen (mp
, msgnum
);
82 /* should we set the SELECTED bit? */
84 set_selected (mp
, msgnum
);
86 /* check if highest or lowest selected */
87 if (mp
->numsel
== 0) {
91 if (msgnum
< mp
->lowsel
)
93 if (msgnum
> mp
->hghsel
)
97 /* increment number selected */
102 * check if this is highest or lowest message
104 if (mp
->nummsg
== 0) {
108 if (msgnum
< mp
->lowmsg
)
110 if (msgnum
> mp
->hghmsg
)
114 /* increment message count */
117 nmsg
= m_name (msgnum
);
118 snprintf (newmsg
, sizeof(newmsg
), "%s/%s", mp
->foldpath
, nmsg
);
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.
127 if (link (msgfile
, newmsg
) != -1) {
129 (void)snprintf(oldmsg
, sizeof (oldmsg
), "%s/%s", from_dir
, msgfile
);
130 (void)ext_hook("ref-hook", oldmsg
, newmsg
);
133 (void)ext_hook("add-hook", newmsg
, NULL
);
140 if (linkerr
== EISREMOTE
)
142 #endif /* EISREMOTE */
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).
150 if (linkerr
== EEXIST
) {
151 if (stat (msgfile
, &st2
) == 0 && stat (newmsg
, &st1
) == 0
152 && st2
.st_ino
== st1
.st_ino
) {
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.
165 if (linkerr
== EXDEV
|| linkerr
== EPERM
) {
166 if (stat (newmsg
, &st1
) == 0) {
167 inform("message %s:%s already exists", mp
->foldpath
, newmsg
);
171 if ((infd
= open (msgfile
, O_RDONLY
)) == -1) {
172 advise (msgfile
, "unable to open message %s", msgfile
);
176 if ((outfd
= creat (newmsg
, (int) st1
.st_mode
& 0777)) == -1) {
177 advise (newmsg
, "unable to create");
181 cpydata (infd
, outfd
, msgfile
, newmsg
);
186 (void)snprintf(oldmsg
, sizeof (oldmsg
), "%s/%s", from_dir
, msgfile
);
187 (void)ext_hook("ref-hook", oldmsg
, newmsg
);
190 (void)ext_hook("add-hook", newmsg
, NULL
);
196 * Else, some other type of link error,
197 * so just return error.
199 advise (newmsg
, "error linking %s to", msgfile
);