Testsuite: revert some of the modernish Perl constructs
[exim.git] / src / src / transports / queuefile.c
... / ...
CommitLineData
1/*************************************************
2* Exim - an Internet mail transport agent *
3*************************************************/
4
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. */
8
9
10#include "../exim.h"
11#include "queuefile.h"
12
13/* Options specific to the appendfile transport. They must be in alphabetic
14order (note that "_" comes before the lower case letters). Some of them are
15stored in the publicly visible instance block - these are flagged with the
16opt_public flag. */
17
18optionlist queuefile_transport_options[] = {
19 { "directory", opt_stringptr,
20 (void *)offsetof(queuefile_transport_options_block, dirname) },
21};
22
23/* Size of the options list. An extern variable has to be used so that its
24address can appear in the tables drtables.c. */
25
26int queuefile_transport_options_count =
27 sizeof(queuefile_transport_options) / sizeof(optionlist);
28
29/* Default private options block for the appendfile transport. */
30
31queuefile_transport_options_block queuefile_transport_option_defaults = {
32 NULL, /* dirname */
33};
34
35/*************************************************
36* Initialization entry point *
37*************************************************/
38
39void queuefile_transport_init(transport_instance *tblock)
40{
41queuefile_transport_options_block *ob =
42 (queuefile_transport_options_block *) tblock->options_block;
43
44if (!ob->dirname)
45 log_write(0, LOG_PANIC_DIE | LOG_CONFIG,
46 "directory must be set for the %s transport", tblock->name);
47}
48
49/* This function will copy from a file to another
50
51Arguments:
52 to_fd FILE to write to (the destination queue file)
53 from_fd FILE to read from (the spool queue file)
54
55Returns: TRUE if all went well, FALSE otherwise
56*/
57
58static BOOL
59copy_spool_file (FILE *to_fd, FILE *from_fd)
60{
61int i, j;
62uschar buffer[16384];
63
64rewind(from_fd);
65
66do
67 if ((j = fread(buffer, 1, sizeof(buffer), from_fd)) > 0)
68 if ((i = fwrite(buffer, j, 1, to_fd)) != 1)
69 return FALSE;
70while (j > 0);
71return TRUE;
72}
73
74/* This function performs the actual copying of the header
75and data files to the destination directory
76
77Arguments:
78 tb the transport block
79 addr address_item being processed
80 sdfd int Source directory fd
81 ddfd int Destination directory fd
82 link_file BOOL use linkat instead of data copy
83 srcfd fd for data file, or -1 for header file
84
85Returns: TRUE if all went well, FALSE otherwise
86*/
87
88static BOOL
89copy_spool_files(transport_instance * tb, address_item * addr,
90 int sdfd, int ddfd, BOOL link_file, int srcfd)
91{
92BOOL is_hdr_file = srcfd < 0;
93uschar * suffix = srcfd < 0 ? US"H" : US"D";
94int dstfd;
95FILE * dst_file, * src_file;
96uschar * filename = string_sprintf("%s-%s", message_id, suffix);
97uschar * srcpath = spool_fname(US"input", message_subdir, message_id, suffix);
98uschar * dstpath = string_sprintf("%s/%s-%s",
99 ((queuefile_transport_options_block *) tb->options_block)->dirname,
100 message_id, suffix);
101
102if (link_file)
103 {
104 DEBUG(D_transport) debug_printf("%s transport, linking %s => %s\n",
105 tb->name, srcpath, dstpath);
106
107 return linkat(sdfd, CCS filename, ddfd, CCS filename, 0) >= 0;
108 }
109
110/* use data copy */
111
112DEBUG(D_transport) debug_printf("%s transport, copying %s => %s\n",
113 tb->name, srcpath, dstpath);
114
115if ((dstfd =
116 openat(ddfd, CCS filename, O_RDWR|O_CREAT|O_EXCL, SPOOL_MODE)) < 0)
117 {
118 addr->transport_return = DEFER;
119 addr->basic_errno = errno;
120 addr->message = string_sprintf("%s transport opening file: %s "
121 "failed with error: %s", tb->name, dstpath, strerror(errno));
122 return FALSE;
123 }
124
125fchmod(dstfd, SPOOL_MODE);
126
127if (!(dst_file = fdopen(dstfd, "wb")))
128 {
129 addr->transport_return = DEFER;
130 addr->basic_errno = errno;
131 addr->message = string_sprintf("%s transport opening file fd: %s "
132 "failed with error: %s", tb->name, dstpath, strerror(errno));
133 (void) close(dstfd);
134 return FALSE;
135 }
136
137if (is_hdr_file)
138 if ((srcfd = openat(sdfd, CCS filename, O_RDONLY)) < 0)
139 {
140 addr->basic_errno = errno;
141 addr->message = string_sprintf("%s transport opening file: %s "
142 "failed with error: %s", tb->name, srcpath, strerror(errno));
143 goto bad;
144 }
145
146if (!(src_file = fdopen(srcfd, "rb")))
147 {
148 addr->basic_errno = errno;
149 addr->message = string_sprintf("%s transport opening file fd: "
150 "%s failed with error: %s", tb->name, srcpath, strerror(errno));
151 if (is_hdr_file) (void) close(srcfd);
152 goto bad;
153 }
154
155if (!copy_spool_file(dst_file, src_file))
156 {
157 addr->message = string_sprintf("%s transport creating file: "
158 "%s failed with error: %s", tb->name, dstpath, strerror(errno));
159 if (is_hdr_file) (void) fclose(src_file);
160 goto bad;
161 }
162
163if (is_hdr_file) (void) fclose(src_file);
164(void) fclose(dst_file);
165
166return TRUE;
167
168bad:
169 addr->transport_return = DEFER;
170 (void) fclose(dst_file);
171 return FALSE;
172}
173
174/*************************************************
175* Main entry point *
176*************************************************/
177
178/* This transport always returns FALSE, indicating that the status in
179the first address is the status for all addresses in a batch. */
180
181BOOL
182queuefile_transport_entry(transport_instance * tblock, address_item * addr)
183{
184queuefile_transport_options_block * ob =
185 (queuefile_transport_options_block *) tblock->options_block;
186BOOL can_link;
187uschar * sourcedir = spool_dname(US"input", message_subdir);
188uschar * s;
189struct stat dstatbuf, sstatbuf;
190int ddfd = -1, sdfd = -1;
191
192DEBUG(D_transport)
193 debug_printf("%s transport entered\n", tblock->name);
194
195#ifndef O_DIRECTORY
196# define O_DIRECTORY 0
197#endif
198#ifndef O_NOFOLLOW
199# define O_NOFOLLOW 0
200#endif
201
202if (ob->dirname[0] != '/')
203 {
204 addr->transport_return = PANIC;
205 addr->message = string_sprintf("%s transport directory: "
206 "%s is not absolute", tblock->name, ob->dirname);
207 return FALSE;
208 }
209
210/* Open the source and destination directories and check if they are
211on the same filesystem, so we can hard-link files rather than copying. */
212
213if ( (s = ob->dirname,
214 (ddfd = Uopen(s, O_RDONLY | O_DIRECTORY | O_NOFOLLOW, 0)) < 0)
215 || (s = sourcedir,
216 (sdfd = Uopen(sourcedir, O_RDONLY | O_DIRECTORY | O_NOFOLLOW, 0)) < 0)
217 )
218 {
219 addr->transport_return = PANIC;
220 addr->basic_errno = errno;
221 addr->message = string_sprintf("%s transport accessing directory: %s "
222 "failed with error: %s", tblock->name, s, strerror(errno));
223 if (ddfd >= 0) (void) close(ddfd);
224 return FALSE;
225 }
226
227if ( (s = ob->dirname, fstat(ddfd, &dstatbuf) < 0)
228 || (s = sourcedir, fstat(sdfd, &sstatbuf) < 0)
229 )
230 {
231 addr->transport_return = PANIC;
232 addr->basic_errno = errno;
233 addr->message = string_sprintf("%s transport fstat on directory fd: "
234 "%s failed with error: %s", tblock->name, s, strerror(errno));
235 goto RETURN;
236 }
237can_link = (dstatbuf.st_dev == sstatbuf.st_dev);
238
239if (dont_deliver)
240 {
241 DEBUG(D_transport)
242 debug_printf("*** delivery by %s transport bypassed by -N option\n",
243 tblock->name);
244 addr->transport_return = OK;
245 goto RETURN;
246 }
247
248/* Link or copy the header and data spool files */
249
250DEBUG(D_transport)
251 debug_printf("%s transport, copying header file\n", tblock->name);
252
253if (!copy_spool_files(tblock, addr, sdfd, ddfd, can_link, -1))
254 goto RETURN;
255
256DEBUG(D_transport)
257 debug_printf("%s transport, copying data file\n", tblock->name);
258
259if (!copy_spool_files(tblock, addr, sdfd, ddfd, can_link, deliver_datafile))
260 {
261 DEBUG(D_transport)
262 debug_printf("%s transport, copying data file failed, "
263 "unlinking the header file\n", tblock->name);
264 Uunlink(string_sprintf("%s/%s-H", ob->dirname, message_id));
265 goto RETURN;
266 }
267
268DEBUG(D_transport)
269 debug_printf("%s transport succeeded\n", tblock->name);
270
271addr->transport_return = OK;
272
273RETURN:
274if (ddfd >= 0) (void) close(ddfd);
275if (sdfd >= 0) (void) close(sdfd);
276
277/* A return of FALSE means that if there was an error, a common error was
278put in the first address of a batch. */
279return FALSE;
280}