Skip to content

Commit

Permalink
[core] chunkqueue_env_tmpdir()
Browse files Browse the repository at this point in the history
centralize tempdir logic: initialize env_tmpdir at startup using
TMPDIR, then TEMP (on _WIN32), and finally falling back to "/var/tmp"
  • Loading branch information
gstrauss committed Jul 26, 2023
1 parent 9abba32 commit 567f225
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
15 changes: 14 additions & 1 deletion src/chunk.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ static chunk *chunk_buffers;
static int chunks_oversized_n;
static const array *chunkqueue_default_tempdirs = NULL;
static off_t chunkqueue_default_tempfile_size = DEFAULT_TEMPFILE_SIZE;
static const char *env_tmpdir = NULL;

void chunkqueue_set_chunk_size (size_t sz)
{
Expand Down Expand Up @@ -669,6 +670,12 @@ void chunkqueue_set_tempdirs_default (const array *tempdirs, off_t upload_temp_f
upload_temp_file_size = DEFAULT_TEMPFILE_SIZE;
chunkqueue_default_tempdirs = tempdirs;
chunkqueue_default_tempfile_size = upload_temp_file_size;

env_tmpdir = getenv("TMPDIR");
#ifdef _WIN32
if (NULL == env_tmpdir) env_tmpdir = getenv("TEMP");
#endif
if (NULL == env_tmpdir) env_tmpdir = "/var/tmp";
}

void chunkqueue_set_tempdirs(chunkqueue * const restrict cq, const array * const restrict tempdirs, off_t upload_temp_file_size) {
Expand All @@ -679,6 +686,11 @@ void chunkqueue_set_tempdirs(chunkqueue * const restrict cq, const array * const
cq->tempdir_idx = 0;
}

const char *chunkqueue_env_tmpdir(void) {
/*(chunkqueue_set_tempdirs_default() must have been called at startup)*/
return env_tmpdir;
}

__attribute_noinline__
static void chunkqueue_dup_file_chunk_fd (chunk * const restrict d, const chunk * const restrict c) {
/*assert(d != c);*/
Expand Down Expand Up @@ -775,8 +787,9 @@ static chunk *chunkqueue_get_append_newtempfile(chunkqueue * const restrict cq,
}
}
else {
const char *tmpdir = chunkqueue_env_tmpdir();
c->file.fd =
chunkqueue_get_append_mkstemp(template, CONST_STR_LEN("/var/tmp"));
chunkqueue_get_append_mkstemp(template, tmpdir, strlen(tmpdir));
if (-1 != c->file.fd) return c;
}

Expand Down
3 changes: 3 additions & 0 deletions src/chunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ void chunkqueue_set_tempdirs_default (const array *tempdirs, off_t upload_temp_f

void chunkqueue_set_tempdirs(chunkqueue * restrict cq, const array * restrict tempdirs, off_t upload_temp_file_size);

__attribute_cold__
const char *chunkqueue_env_tmpdir(void);

void chunkqueue_append_file(chunkqueue * restrict cq, const buffer * restrict fn, off_t offset, off_t len); /* copies "fn" */
void chunkqueue_append_file_fd(chunkqueue * restrict cq, const buffer * restrict fn, int fd, off_t offset, off_t len); /* copies "fn" */
void chunkqueue_append_mem(chunkqueue * restrict cq, const char * restrict mem, size_t len); /* copies memory */
Expand Down
16 changes: 6 additions & 10 deletions src/configfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -2664,13 +2664,13 @@ int config_set_defaults(server *srv) {
}
}

chunkqueue_set_tempdirs_default(
srv->srvconf.upload_tempdirs,
srv->srvconf.upload_temp_file_size);

if (!srv->srvconf.upload_tempdirs->used) {
const char *tmpdir = getenv("TMPDIR");
#ifdef _WIN32
if (NULL == tmpdir) tmpdir = getenv("TEMP");
#endif
if (NULL == tmpdir) tmpdir = "/var/tmp";
array_insert_value(srv->srvconf.upload_tempdirs, tmpdir, strlen(tmpdir));
const char *tmpdir = chunkqueue_env_tmpdir();
array_insert_value(srv->srvconf.upload_tempdirs,tmpdir,strlen(tmpdir));
}

if (srv->srvconf.upload_tempdirs->used) {
Expand Down Expand Up @@ -2699,10 +2699,6 @@ int config_set_defaults(server *srv) {
}
}

chunkqueue_set_tempdirs_default(
srv->srvconf.upload_tempdirs,
srv->srvconf.upload_temp_file_size);

if (!s->document_root || buffer_is_blank(s->document_root)) {
log_error(srv->errh, __FILE__, __LINE__, "server.document-root is not set");
return -1;
Expand Down
2 changes: 1 addition & 1 deletion src/mod_deflate.c
Original file line number Diff line number Diff line change
Expand Up @@ -1523,7 +1523,7 @@ static int mod_deflate_using_libdeflate (handler_ctx * const hctx, const plugin_
buffer_copy_string_len(fn, BUF_PTR_LEN(&ds->value));
}
else
buffer_copy_string_len(fn, CONST_STR_LEN("/var/tmp"));
buffer_copy_string(fn, chunkqueue_env_tmpdir());
buffer_append_path_len(fn, CONST_STR_LEN("lighttpd-XXXXXX"));
fd = fdevent_mkostemp(fn->ptr, 0);
if (-1 == fd) return 0;
Expand Down
4 changes: 4 additions & 0 deletions src/t/test_mod.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#undef NDEBUG
#include <assert.h>

#include "chunk.h"

void test_mod_access (void);
void test_mod_alias (void);
void test_mod_evhost (void);
Expand All @@ -13,6 +15,8 @@ void test_mod_staticfile (void);
void test_mod_userdir (void);

int main(void) {
chunkqueue_set_tempdirs_default(NULL, 0);

test_mod_access();
test_mod_alias();
test_mod_evhost();
Expand Down

0 comments on commit 567f225

Please sign in to comment.