X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fsrc%2Ftransports%2Fqueuefile.c;h=cde6e53c129f87db7ab37c64d513d61d8b6cd1ce;hb=251b9eb4698f569864c35127ddb7c309b92ccecb;hp=79178ef0cd0edeabee56cefee73f6fe0f6c60375;hpb=f09fe41fff3137ca40feb35e7c1bd314bb109f68;p=user%2Fhenk%2Fcode%2Fexim.git diff --git a/src/src/transports/queuefile.c b/src/src/transports/queuefile.c index 79178ef0c..cde6e53c1 100644 --- a/src/src/transports/queuefile.c +++ b/src/src/transports/queuefile.c @@ -4,6 +4,7 @@ /* Copyright (c) Andrew Colin Kissa 2016 */ /* Copyright (c) University of Cambridge 2016 */ +/* Copyright (c) The Exim Maintainers 1995 - 2018 */ /* See the file NOTICE for conditions of use and distribution. */ @@ -20,12 +21,25 @@ optionlist queuefile_transport_options[] = { (void *)offsetof(queuefile_transport_options_block, dirname) }, }; + /* Size of the options list. An extern variable has to be used so that its address can appear in the tables drtables.c. */ int queuefile_transport_options_count = sizeof(queuefile_transport_options) / sizeof(optionlist); + +#ifdef MACRO_PREDEF + +/* Dummy values */ +queuefile_transport_options_block queuefile_transport_option_defaults = {0}; +void queuefile_transport_init(transport_instance *tblock) {} +BOOL queuefile_transport_entry(transport_instance *tblock, address_item *addr) {return FALSE;} + +#else /*!MACRO_PREDEF*/ + + + /* Default private options block for the appendfile transport. */ queuefile_transport_options_block queuefile_transport_option_defaults = { @@ -49,24 +63,29 @@ if (!ob->dirname) /* This function will copy from a file to another Arguments: - to_fd FILE to write to (the destination queue file) - from_fd FILE to read from (the spool queue file) + dst fd to write to (the destination queue file) + src fd to read from (the spool queue file) -Returns: TRUE if all went well, FALSE otherwise +Returns: TRUE if all went well, FALSE otherwise with errno set */ static BOOL -copy_spool_file (FILE *to_fd, FILE *from_fd) +copy_spool_file(int dst, int src) { int i, j; uschar buffer[16384]; +uschar * s; -rewind(from_fd); +if (lseek(src, 0, SEEK_SET) != 0) + return FALSE; do - if ((j = fread(buffer, 1, sizeof(buffer), from_fd)) > 0) - if ((i = fwrite(buffer, j, 1, to_fd)) != 1) - return FALSE; + if ((j = read(src, buffer, sizeof(buffer))) > 0) + for (s = buffer; (i = write(dst, s, j)) != j; s += i, j -= i) + if (i < 0) + return FALSE; + else if (j < 0) + return FALSE; while (j > 0); return TRUE; } @@ -77,6 +96,7 @@ and data files to the destination directory Arguments: tb the transport block addr address_item being processed + dstpath destination directory name sdfd int Source directory fd ddfd int Destination directory fd link_file BOOL use linkat instead of data copy @@ -87,88 +107,57 @@ Returns: TRUE if all went well, FALSE otherwise static BOOL copy_spool_files(transport_instance * tb, address_item * addr, - int sdfd, int ddfd, BOOL link_file, int srcfd) + const uschar * dstpath, int sdfd, int ddfd, BOOL link_file, int srcfd) { BOOL is_hdr_file = srcfd < 0; -uschar * suffix = srcfd < 0 ? US"H" : US"D"; +const uschar * suffix = srcfd < 0 ? US"H" : US"D"; int dstfd; -FILE * dst_file, * src_file; -uschar * filename = string_sprintf("%s-%s", message_id, suffix); -uschar * srcpath = spool_fname(US"input", message_subdir, message_id, suffix); -uschar * dstpath = string_sprintf("%s/%s-%s", - ((queuefile_transport_options_block *) tb->options_block)->dirname, - message_id, suffix); +const uschar * filename = string_sprintf("%s-%s", message_id, suffix); +const uschar * srcpath = spool_fname(US"input", message_subdir, message_id, suffix); +const uschar * s, * op; + +dstpath = string_sprintf("%s/%s-%s", dstpath, message_id, suffix); if (link_file) { DEBUG(D_transport) debug_printf("%s transport, linking %s => %s\n", tb->name, srcpath, dstpath); - return linkat(sdfd, CCS filename, ddfd, CCS filename, 0) >= 0; - } - -/* use data copy */ - -DEBUG(D_transport) debug_printf("%s transport, copying %s => %s\n", - tb->name, srcpath, dstpath); + if (linkat(sdfd, CCS filename, ddfd, CCS filename, 0) >= 0) + return TRUE; -if ((dstfd = - openat(ddfd, CCS filename, O_RDWR|O_CREAT|O_EXCL, SPOOL_MODE)) < 0) - { - addr->transport_return = DEFER; - addr->basic_errno = errno; - addr->message = string_sprintf("%s transport opening file: %s " - "failed with error: %s", tb->name, dstpath, strerror(errno)); - return FALSE; + op = US"linking"; + s = dstpath; } - -fchmod(dstfd, SPOOL_MODE); - -if (!(dst_file = fdopen(dstfd, "wb"))) +else /* use data copy */ { - addr->transport_return = DEFER; - addr->basic_errno = errno; - addr->message = string_sprintf("%s transport opening file fd: %s " - "failed with error: %s", tb->name, dstpath, strerror(errno)); - (void) close(dstfd); - return FALSE; - } - -if (is_hdr_file) - if ((srcfd = openat(sdfd, CCS filename, O_RDONLY)) < 0) - { - addr->basic_errno = errno; - addr->message = string_sprintf("%s transport opening file: %s " - "failed with error: %s", tb->name, srcpath, strerror(errno)); - goto bad; - } - -if (!(src_file = fdopen(srcfd, "rb"))) - { - addr->basic_errno = errno; - addr->message = string_sprintf("%s transport opening file fd: " - "%s failed with error: %s", tb->name, srcpath, strerror(errno)); - if (is_hdr_file) (void) close(srcfd); - goto bad; - } + DEBUG(D_transport) debug_printf("%s transport, copying %s => %s\n", + tb->name, srcpath, dstpath); -if (!copy_spool_file(dst_file, src_file)) - { - addr->message = string_sprintf("%s transport creating file: " - "%s failed with error: %s", tb->name, dstpath, strerror(errno)); - if (is_hdr_file) (void) fclose(src_file); - goto bad; + if ( (s = dstpath, + (dstfd = openat(ddfd, CCS filename, O_RDWR|O_CREAT|O_EXCL, SPOOL_MODE)) + < 0 + ) + || is_hdr_file + && (s = srcpath, (srcfd = openat(sdfd, CCS filename, O_RDONLY)) < 0) + ) + op = US"opening"; + + else + if (s = dstpath, fchmod(dstfd, SPOOL_MODE) != 0) + op = US"setting perms on"; + else + if (!copy_spool_file(dstfd, srcfd)) + op = US"creating"; + else + return TRUE; } -if (is_hdr_file) (void) fclose(src_file); -(void) fclose(dst_file); - -return TRUE; - -bad: - addr->transport_return = DEFER; - (void) fclose(dst_file); - return FALSE; +addr->basic_errno = errno; +addr->message = string_sprintf("%s transport %s file: %s failed with error: %s", + tb->name, op, s, strerror(errno)); +addr->transport_return = DEFER; +return FALSE; } /************************************************* @@ -185,7 +174,7 @@ queuefile_transport_options_block * ob = (queuefile_transport_options_block *) tblock->options_block; BOOL can_link; uschar * sourcedir = spool_dname(US"input", message_subdir); -uschar * s; +uschar * s, * dstdir; struct stat dstatbuf, sstatbuf; int ddfd = -1, sdfd = -1; @@ -199,18 +188,25 @@ DEBUG(D_transport) # define O_NOFOLLOW 0 #endif -if (ob->dirname[0] != '/') +if (!(dstdir = expand_string(ob->dirname))) + { + addr->message = string_sprintf("%s transport: failed to expand dirname option", + tblock->name); + addr->transport_return = DEFER; + return FALSE; + } +if (*dstdir != '/') { addr->transport_return = PANIC; addr->message = string_sprintf("%s transport directory: " - "%s is not absolute", tblock->name, ob->dirname); + "%s is not absolute", tblock->name, dstdir); return FALSE; } /* Open the source and destination directories and check if they are on the same filesystem, so we can hard-link files rather than copying. */ -if ( (s = ob->dirname, +if ( (s = dstdir, (ddfd = Uopen(s, O_RDONLY | O_DIRECTORY | O_NOFOLLOW, 0)) < 0) || (s = sourcedir, (sdfd = Uopen(sourcedir, O_RDONLY | O_DIRECTORY | O_NOFOLLOW, 0)) < 0) @@ -224,8 +220,8 @@ if ( (s = ob->dirname, return FALSE; } -if ( (s = ob->dirname, fstat(ddfd, &dstatbuf) < 0) - || (s = sourcedir, fstat(sdfd, &sstatbuf) < 0) +if ( (s = dstdir, fstat(ddfd, &dstatbuf) < 0) + || (s = sourcedir, fstat(sdfd, &sstatbuf) < 0) ) { addr->transport_return = PANIC; @@ -236,7 +232,7 @@ if ( (s = ob->dirname, fstat(ddfd, &dstatbuf) < 0) } can_link = (dstatbuf.st_dev == sstatbuf.st_dev); -if (dont_deliver) +if (f.dont_deliver) { DEBUG(D_transport) debug_printf("*** delivery by %s transport bypassed by -N option\n", @@ -250,18 +246,19 @@ if (dont_deliver) DEBUG(D_transport) debug_printf("%s transport, copying header file\n", tblock->name); -if (!copy_spool_files(tblock, addr, sdfd, ddfd, can_link, -1)) +if (!copy_spool_files(tblock, addr, dstdir, sdfd, ddfd, can_link, -1)) goto RETURN; DEBUG(D_transport) debug_printf("%s transport, copying data file\n", tblock->name); -if (!copy_spool_files(tblock, addr, sdfd, ddfd, can_link, deliver_datafile)) +if (!copy_spool_files(tblock, addr, dstdir, sdfd, ddfd, can_link, + deliver_datafile)) { DEBUG(D_transport) debug_printf("%s transport, copying data file failed, " "unlinking the header file\n", tblock->name); - Uunlink(string_sprintf("%s/%s-H", ob->dirname, message_id)); + Uunlink(string_sprintf("%s/%s-H", dstdir, message_id)); goto RETURN; } @@ -278,3 +275,5 @@ if (sdfd >= 0) (void) close(sdfd); put in the first address of a batch. */ return FALSE; } + +#endif /*!MACRO_PREDEF*/