1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) Andrew Colin Kissa <andrew@topdog.za.net> 2016 */
6 /* Copyright (c) University of Cambridge 2016 */
7 /* See the file NOTICE for conditions of use and distribution. */
11 #include "queuefile.h"
13 /* Options specific to the appendfile transport. They must be in alphabetic
14 order (note that "_" comes before the lower case letters). Some of them are
15 stored in the publicly visible instance block - these are flagged with the
18 optionlist queuefile_transport_options[] = {
19 { "directory", opt_stringptr,
20 (void *)offsetof(queuefile_transport_options_block, dirname) },
23 /* Size of the options list. An extern variable has to be used so that its
24 address can appear in the tables drtables.c. */
26 int queuefile_transport_options_count =
27 sizeof(queuefile_transport_options) / sizeof(optionlist);
29 /* Default private options block for the appendfile transport. */
31 queuefile_transport_options_block queuefile_transport_option_defaults = {
35 /*************************************************
36 * Initialization entry point *
37 *************************************************/
39 void queuefile_transport_init(transport_instance *tblock)
41 queuefile_transport_options_block *ob =
42 (queuefile_transport_options_block *) tblock->options_block;
45 log_write(0, LOG_PANIC_DIE | LOG_CONFIG,
46 "directory must be set for the %s transport", tblock->name);
49 /* This function will copy from a file to another
52 dst fd to write to (the destination queue file)
53 src fd to read from (the spool queue file)
55 Returns: TRUE if all went well, FALSE otherwise with errno set
59 copy_spool_file(int dst, int src)
65 if (lseek(src, 0, SEEK_SET) != 0)
69 if ((j = read(src, buffer, sizeof(buffer))) > 0)
70 for (s = buffer; (i = write(dst, s, j)) != j; s += i, j -= i)
79 /* This function performs the actual copying of the header
80 and data files to the destination directory
83 tb the transport block
84 addr address_item being processed
85 sdfd int Source directory fd
86 ddfd int Destination directory fd
87 link_file BOOL use linkat instead of data copy
88 srcfd fd for data file, or -1 for header file
90 Returns: TRUE if all went well, FALSE otherwise
94 copy_spool_files(transport_instance * tb, address_item * addr,
95 int sdfd, int ddfd, BOOL link_file, int srcfd)
97 BOOL is_hdr_file = srcfd < 0;
98 const uschar * suffix = srcfd < 0 ? US"H" : US"D";
100 const uschar * filename = string_sprintf("%s-%s", message_id, suffix);
101 const uschar * srcpath = spool_fname(US"input", message_subdir, message_id, suffix);
102 const uschar * dstpath = string_sprintf("%s/%s-%s",
103 ((queuefile_transport_options_block *) tb->options_block)->dirname,
110 DEBUG(D_transport) debug_printf("%s transport, linking %s => %s\n",
111 tb->name, srcpath, dstpath);
113 if (linkat(sdfd, CCS filename, ddfd, CCS filename, 0) >= 0)
119 else /* use data copy */
121 DEBUG(D_transport) debug_printf("%s transport, copying %s => %s\n",
122 tb->name, srcpath, dstpath);
125 (dstfd = openat(ddfd, CCS filename, O_RDWR|O_CREAT|O_EXCL, SPOOL_MODE))
129 && (s = srcpath, (srcfd = openat(sdfd, CCS filename, O_RDONLY)) < 0)
134 if (s = dstpath, fchmod(dstfd, SPOOL_MODE) != 0)
135 op = US"setting perms on";
137 if (!copy_spool_file(dstfd, srcfd))
143 addr->basic_errno = errno;
144 addr->message = string_sprintf("%s transport %s file: %s failed with error: %s",
145 tb->name, op, s, strerror(errno));
146 addr->transport_return = DEFER;
150 /*************************************************
152 *************************************************/
154 /* This transport always returns FALSE, indicating that the status in
155 the first address is the status for all addresses in a batch. */
158 queuefile_transport_entry(transport_instance * tblock, address_item * addr)
160 queuefile_transport_options_block * ob =
161 (queuefile_transport_options_block *) tblock->options_block;
163 uschar * sourcedir = spool_dname(US"input", message_subdir);
165 struct stat dstatbuf, sstatbuf;
166 int ddfd = -1, sdfd = -1;
169 debug_printf("%s transport entered\n", tblock->name);
172 # define O_DIRECTORY 0
175 # define O_NOFOLLOW 0
178 if (ob->dirname[0] != '/')
180 addr->transport_return = PANIC;
181 addr->message = string_sprintf("%s transport directory: "
182 "%s is not absolute", tblock->name, ob->dirname);
186 /* Open the source and destination directories and check if they are
187 on the same filesystem, so we can hard-link files rather than copying. */
189 if ( (s = ob->dirname,
190 (ddfd = Uopen(s, O_RDONLY | O_DIRECTORY | O_NOFOLLOW, 0)) < 0)
192 (sdfd = Uopen(sourcedir, O_RDONLY | O_DIRECTORY | O_NOFOLLOW, 0)) < 0)
195 addr->transport_return = PANIC;
196 addr->basic_errno = errno;
197 addr->message = string_sprintf("%s transport accessing directory: %s "
198 "failed with error: %s", tblock->name, s, strerror(errno));
199 if (ddfd >= 0) (void) close(ddfd);
203 if ( (s = ob->dirname, fstat(ddfd, &dstatbuf) < 0)
204 || (s = sourcedir, fstat(sdfd, &sstatbuf) < 0)
207 addr->transport_return = PANIC;
208 addr->basic_errno = errno;
209 addr->message = string_sprintf("%s transport fstat on directory fd: "
210 "%s failed with error: %s", tblock->name, s, strerror(errno));
213 can_link = (dstatbuf.st_dev == sstatbuf.st_dev);
218 debug_printf("*** delivery by %s transport bypassed by -N option\n",
220 addr->transport_return = OK;
224 /* Link or copy the header and data spool files */
227 debug_printf("%s transport, copying header file\n", tblock->name);
229 if (!copy_spool_files(tblock, addr, sdfd, ddfd, can_link, -1))
233 debug_printf("%s transport, copying data file\n", tblock->name);
235 if (!copy_spool_files(tblock, addr, sdfd, ddfd, can_link, deliver_datafile))
238 debug_printf("%s transport, copying data file failed, "
239 "unlinking the header file\n", tblock->name);
240 Uunlink(string_sprintf("%s/%s-H", ob->dirname, message_id));
245 debug_printf("%s transport succeeded\n", tblock->name);
247 addr->transport_return = OK;
250 if (ddfd >= 0) (void) close(ddfd);
251 if (sdfd >= 0) (void) close(sdfd);
253 /* A return of FALSE means that if there was an error, a common error was
254 put in the first address of a batch. */