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
) },
24 /* Size of the options list. An extern variable has to be used so that its
25 address can appear in the tables drtables.c. */
27 int queuefile_transport_options_count
=
28 sizeof(queuefile_transport_options
) / sizeof(optionlist
);
34 queuefile_transport_options_block queuefile_transport_option_defaults
= {0};
35 void queuefile_transport_init(transport_instance
*tblock
) {}
36 BOOL
queuefile_transport_entry(transport_instance
*tblock
, address_item
*addr
) {return FALSE
;}
38 #else /*!MACRO_PREDEF*/
42 /* Default private options block for the appendfile transport. */
44 queuefile_transport_options_block queuefile_transport_option_defaults
= {
48 /*************************************************
49 * Initialization entry point *
50 *************************************************/
52 void queuefile_transport_init(transport_instance
*tblock
)
54 queuefile_transport_options_block
*ob
=
55 (queuefile_transport_options_block
*) tblock
->options_block
;
58 log_write(0, LOG_PANIC_DIE
| LOG_CONFIG
,
59 "directory must be set for the %s transport", tblock
->name
);
62 /* This function will copy from a file to another
65 dst fd to write to (the destination queue file)
66 src fd to read from (the spool queue file)
68 Returns: TRUE if all went well, FALSE otherwise with errno set
72 copy_spool_file(int dst
, int src
)
78 if (lseek(src
, 0, SEEK_SET
) != 0)
82 if ((j
= read(src
, buffer
, sizeof(buffer
))) > 0)
83 for (s
= buffer
; (i
= write(dst
, s
, j
)) != j
; s
+= i
, j
-= i
)
92 /* This function performs the actual copying of the header
93 and data files to the destination directory
96 tb the transport block
97 addr address_item being processed
98 sdfd int Source directory fd
99 ddfd int Destination directory fd
100 link_file BOOL use linkat instead of data copy
101 srcfd fd for data file, or -1 for header file
103 Returns: TRUE if all went well, FALSE otherwise
107 copy_spool_files(transport_instance
* tb
, address_item
* addr
,
108 int sdfd
, int ddfd
, BOOL link_file
, int srcfd
)
110 BOOL is_hdr_file
= srcfd
< 0;
111 const uschar
* suffix
= srcfd
< 0 ? US
"H" : US
"D";
113 const uschar
* filename
= string_sprintf("%s-%s", message_id
, suffix
);
114 const uschar
* srcpath
= spool_fname(US
"input", message_subdir
, message_id
, suffix
);
115 const uschar
* dstpath
= string_sprintf("%s/%s-%s",
116 ((queuefile_transport_options_block
*) tb
->options_block
)->dirname
,
123 DEBUG(D_transport
) debug_printf("%s transport, linking %s => %s\n",
124 tb
->name
, srcpath
, dstpath
);
126 if (linkat(sdfd
, CCS filename
, ddfd
, CCS filename
, 0) >= 0)
132 else /* use data copy */
134 DEBUG(D_transport
) debug_printf("%s transport, copying %s => %s\n",
135 tb
->name
, srcpath
, dstpath
);
138 (dstfd
= openat(ddfd
, CCS filename
, O_RDWR
|O_CREAT
|O_EXCL
, SPOOL_MODE
))
142 && (s
= srcpath
, (srcfd
= openat(sdfd
, CCS filename
, O_RDONLY
)) < 0)
147 if (s
= dstpath
, fchmod(dstfd
, SPOOL_MODE
) != 0)
148 op
= US
"setting perms on";
150 if (!copy_spool_file(dstfd
, srcfd
))
156 addr
->basic_errno
= errno
;
157 addr
->message
= string_sprintf("%s transport %s file: %s failed with error: %s",
158 tb
->name
, op
, s
, strerror(errno
));
159 addr
->transport_return
= DEFER
;
163 /*************************************************
165 *************************************************/
167 /* This transport always returns FALSE, indicating that the status in
168 the first address is the status for all addresses in a batch. */
171 queuefile_transport_entry(transport_instance
* tblock
, address_item
* addr
)
173 queuefile_transport_options_block
* ob
=
174 (queuefile_transport_options_block
*) tblock
->options_block
;
176 uschar
* sourcedir
= spool_dname(US
"input", message_subdir
);
178 struct stat dstatbuf
, sstatbuf
;
179 int ddfd
= -1, sdfd
= -1;
182 debug_printf("%s transport entered\n", tblock
->name
);
185 # define O_DIRECTORY 0
188 # define O_NOFOLLOW 0
191 if (ob
->dirname
[0] != '/')
193 addr
->transport_return
= PANIC
;
194 addr
->message
= string_sprintf("%s transport directory: "
195 "%s is not absolute", tblock
->name
, ob
->dirname
);
199 /* Open the source and destination directories and check if they are
200 on the same filesystem, so we can hard-link files rather than copying. */
202 if ( (s
= ob
->dirname
,
203 (ddfd
= Uopen(s
, O_RDONLY
| O_DIRECTORY
| O_NOFOLLOW
, 0)) < 0)
205 (sdfd
= Uopen(sourcedir
, O_RDONLY
| O_DIRECTORY
| O_NOFOLLOW
, 0)) < 0)
208 addr
->transport_return
= PANIC
;
209 addr
->basic_errno
= errno
;
210 addr
->message
= string_sprintf("%s transport accessing directory: %s "
211 "failed with error: %s", tblock
->name
, s
, strerror(errno
));
212 if (ddfd
>= 0) (void) close(ddfd
);
216 if ( (s
= ob
->dirname
, fstat(ddfd
, &dstatbuf
) < 0)
217 || (s
= sourcedir
, fstat(sdfd
, &sstatbuf
) < 0)
220 addr
->transport_return
= PANIC
;
221 addr
->basic_errno
= errno
;
222 addr
->message
= string_sprintf("%s transport fstat on directory fd: "
223 "%s failed with error: %s", tblock
->name
, s
, strerror(errno
));
226 can_link
= (dstatbuf
.st_dev
== sstatbuf
.st_dev
);
231 debug_printf("*** delivery by %s transport bypassed by -N option\n",
233 addr
->transport_return
= OK
;
237 /* Link or copy the header and data spool files */
240 debug_printf("%s transport, copying header file\n", tblock
->name
);
242 if (!copy_spool_files(tblock
, addr
, sdfd
, ddfd
, can_link
, -1))
246 debug_printf("%s transport, copying data file\n", tblock
->name
);
248 if (!copy_spool_files(tblock
, addr
, sdfd
, ddfd
, can_link
, deliver_datafile
))
251 debug_printf("%s transport, copying data file failed, "
252 "unlinking the header file\n", tblock
->name
);
253 Uunlink(string_sprintf("%s/%s-H", ob
->dirname
, message_id
));
258 debug_printf("%s transport succeeded\n", tblock
->name
);
260 addr
->transport_return
= OK
;
263 if (ddfd
>= 0) (void) close(ddfd
);
264 if (sdfd
>= 0) (void) close(sdfd
);
266 /* A return of FALSE means that if there was an error, a common error was
267 put in the first address of a batch. */
271 #endif /*!MACRO_PREDEF*/