Add option to specify the directory for temporary files

parent c9e7718d
......@@ -14,6 +14,7 @@ SYNOPSIS
::
./elbestream
[-d <directory>]
[-i <input buffer size> (default: 1048576 bytes)]
[-o <output buffer size> (default: 1048576 bytes)]
[-m <minimum output size>]
......@@ -81,9 +82,13 @@ on the remainder of `stdin(3)`, with its output being sent to
`stdout(3)` as if only *try command* was run to begin with.
If, however, *try command* is considered to have failed, the input
buffer and the rest of `stdin(3)` are written to a temporary
file. Then, the ``%f`` argument of *fin command* is replaced with the
name of that file and *fin command* is run.
buffer and the rest of `stdin(3)` are written to a temporary file
created in the directory specified by the ``-d`` argument or, if it is
not given, the current working directory. Then, the ``%f`` argument of
*fin command* is replaced with the name of that file and *fin command*
is run.
The ``-d`` argument can not be the empty string.
IMPLEMENTATION DETAILS
======================
......
......@@ -66,6 +66,15 @@
#define closefif(fd) if (fd >= 0) closef(fd)
// from varnish-cache
/* Safe printf into a fixed-size buffer */
#define bprintf(buf, fmt, ...) \
do { \
int ibprintf; \
ibprintf = snprintf(buf, sizeof buf, fmt, __VA_ARGS__); \
assert(ibprintf >= 0 && ibprintf < (int)sizeof buf); \
} while (0)
static void
blockf(int fd)
{
......@@ -81,6 +90,7 @@ static void
usage(char * const argv[])
{
fprintf(stderr, "Usage: %s\n"
"\t[-d <directory>]\n"
"\t[-i <input buffer size> (default: %d bytes)]\n"
"\t[-o <output buffer size> (default: %d bytes)]\n"
"\t[-m <minimum output size>]\n"
......@@ -362,6 +372,7 @@ main(int argc, char *argv[], char * const envp[])
{
size_t ibufsz = DEF_IBUF, obufsz = DEF_OBUF, min = 0,
isz = 0, tryisz, osz;
char *dir = ".";
ssize_t r = 0;
char *ibufr;
int icompl, i, opt, finc;
......@@ -372,7 +383,7 @@ main(int argc, char *argv[], char * const envp[])
pid_t pid;
pthread_t thr;
while ((opt = getopt(argc, argv, "i:o:m:")) != -1) {
while ((opt = getopt(argc, argv, "i:o:m:d:")) != -1) {
ep = NULL;
switch (opt) {
case 'i':
......@@ -390,6 +401,9 @@ main(int argc, char *argv[], char * const envp[])
if (*ep != '\0')
argerr(argv, "-m", ep);
break;
case 'd':
dir = optarg;
break;
default: /* '?' */
usage(argv);
exit(EXIT_FAILURE);
......@@ -408,6 +422,10 @@ main(int argc, char *argv[], char * const envp[])
fprintf(stderr, "-o argument can not be zero\n");
exit(EXIT_FAILURE);
}
if (*dir == '\0') {
fprintf(stderr, "-d argument can not be empty\n");
exit(EXIT_FAILURE);
}
tryv = &argv[optind];
......@@ -488,8 +506,14 @@ main(int argc, char *argv[], char * const envp[])
closefif(readfd);
closefif(writefd);
strcpy(fn, "elbebuf.XXXXXX");
bprintf(fn, "%s/elbebuf.XXXXXX", dir);
writefd = mkstemp(fn);
if (writefd < 0) {
fprintf(stderr, "mkstemp(%s) failed: %s (%d)\n\n",
fn, strerror(errno), errno);
cleanup();
exit(EXIT_FAILURE);
}
assert(writefd >= 0);
if (isz > 0) {
r = write(writefd, ibuf, isz);
......
......@@ -22,3 +22,23 @@ for i in 1 4 10 29 30 31 40 ; do
fi
done
done
# empty -d argument
if ./elbestream -d '' -i $i -o $o -m 1 -- $false ---- $cat %f ; then
echo >&2 FAIL
exit 9
fi
# -d OK
if ! echo "${t}" | ./elbestream -d /tmp -i $i -o $o -m 1 -- $false \
---- $cat %f ; then
echo >&2 FAIL
exit 9
fi
# -d does not exist
if echo "${t}" | ./elbestream -d xxx -i $i -o $o -m 1 -- $false \
---- $cat %f ; then
echo >&2 FAIL
exit 9
fi
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment