31 #ifdef HAVE_SYS_TYPES_H
32 #include <sys/types.h>
34 #ifdef HAVE_SYS_STAT_H
44 #define FNAME_MIN_NUM 1000
108 for (idx = FNAME_MIN_NUM; idx < FNAME_MIN_NUM + d->
max_files; ++idx) {
109 char *path = NULL, *tmppath = NULL;
117 problems += sandbox_cfg_allow_rename(cfg,
118 tor_strdup(tmppath), tor_strdup(path));
124 return problems ? -1 : 0;
143 log_warn(
LD_FS,
"Unable to unlink %s while cleaning "
144 "temporary files: %s",
escaped(path), strerror(errno));
151 } SMARTLIST_FOREACH_END(fname);
204 } SMARTLIST_FOREACH_END(cp);
232 const int flags = bin ?
RFTS_BIN : 0;
237 char *contents = read_file_to_str(path, flags, &st);
238 if (contents && sz_out) {
240 #if UINT64_MAX > SIZE_MAX
243 *sz_out = (size_t) st.st_size;
247 return (uint8_t *) contents;
263 for (i = FNAME_MIN_NUM; i < FNAME_MIN_NUM + d->
max_files; ++i) {
266 return tor_strdup(buf);
280 uint64_t total_length = 0;
286 total_length += ch->len);
291 int r = write_chunks_to_file(path, chunks, binary, 0);
294 d->
usage += total_length;
296 *fname_out = tor_strdup(fname);
321 smartlist_free(chunks);
336 (
const uint8_t*)str, strlen(str), binary, fname_out);
360 for (line = labels; line; line = line->next) {
362 sz->len = strlen(line->key) + 1 + strlen(line->value) + 1;
363 const size_t allocated = sz->len + 1;
365 tor_snprintf(bytes, allocated,
"%s %s\n", line->key, line->value);
376 datachunk->bytes = (
const char *)data;
377 datachunk->len = length;
381 smartlist_free(chunks);
402 const uint8_t **data_out,
411 const char *nulp = memchr(m->
data,
'\0', m->
size);
420 size_t offset = nulp - m->
data + 1;
422 *data_out = (
const uint8_t *)(m->
data + offset);
423 *sz_out = m->
size - offset;
440 const uint8_t *data = NULL;
445 uint8_t *result = tor_memdup(data, *sz_out);
453 storage_dir_reduce_usage(
storage_dir_t *d, uint64_t removed_file_size)
456 if (! BUG(d->
usage < removed_file_size)) {
461 d->
usage -= removed_file_size;
490 if (stat(ipath, &st) == 0) {
494 if (unlink(ipath) == 0) {
495 storage_dir_reduce_usage(d, size);
497 log_warn(
LD_FS,
"Unable to unlink %s while removing file: %s",
498 escaped(path), strerror(errno));
523 if (a->mtime < b->mtime)
525 else if (a->mtime > b->mtime)
541 uint64_t target_size,
553 if (orig_usage <= target_size && !min_to_remove) {
558 const int n = smartlist_len(d->
contents);
565 ent->mtime = st.st_mtime;
566 ent->size = st.st_size;
568 } SMARTLIST_FOREACH_END(fname);
573 while ((d->
usage > target_size || min_to_remove > 0) && idx < n) {
575 storage_dir_reduce_usage(d, ents[idx].size);
581 for (idx = 0; idx < n; ++idx) {
int config_get_lines(const char *string, config_line_t **result, int extended)
smartlist_t * tor_listdir(const char *dirname)
int check_private_dir(const char *dirname, cpd_check_t check, const char *effective_user)
const char * escaped(const char *s)
Wrappers for reading and writing data to files on disk.
Headers for util_malloc.c.
memarea_t * memarea_new(void)
void * memarea_alloc(memarea_t *area, size_t sz)
#define memarea_drop_all(area)
int tor_asprintf(char **strp, const char *fmt,...)
int tor_snprintf(char *str, size_t size, const char *format,...)
int sandbox_cfg_allow_open_filename(sandbox_cfg_t **cfg, char *file)
int sandbox_cfg_allow_stat_filename(sandbox_cfg_t **cfg, char *file)
Header file for sandbox.c.
struct sandbox_cfg_elem_t sandbox_cfg_t
#define sandbox_intern_string(s)
void smartlist_string_remove(smartlist_t *sl, const char *element)
int smartlist_contains_string(const smartlist_t *sl, const char *element)
smartlist_t * smartlist_new(void)
void smartlist_add(smartlist_t *sl, void *element)
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
#define SMARTLIST_FOREACH(sl, type, var, cmd)
#define SMARTLIST_DEL_CURRENT(sl, var)
static int storage_dir_rescan(storage_dir_t *d)
const smartlist_t * storage_dir_list(storage_dir_t *d)
uint8_t * storage_dir_read_labeled(storage_dir_t *dir, const char *fname, config_line_t **labels_out, size_t *sz_out)
uint64_t storage_dir_get_usage(storage_dir_t *d)
static int storage_dir_save_chunks_to_file(storage_dir_t *d, const smartlist_t *chunks, int binary, char **fname_out)
static int shrinking_dir_entry_compare(const void *a_, const void *b_)
int storage_dir_save_bytes_to_file(storage_dir_t *d, const uint8_t *data, size_t length, int binary, char **fname_out)
void storage_dir_free_(storage_dir_t *d)
int storage_dir_remove_all(storage_dir_t *d)
uint8_t * storage_dir_read(storage_dir_t *d, const char *fname, int bin, size_t *sz_out)
int storage_dir_save_string_to_file(storage_dir_t *d, const char *str, int binary, char **fname_out)
int storage_dir_save_labeled_to_file(storage_dir_t *d, const config_line_t *labels, const uint8_t *data, size_t length, char **fname_out)
tor_mmap_t * storage_dir_map_labeled(storage_dir_t *dir, const char *fname, config_line_t **labels_out, const uint8_t **data_out, size_t *sz_out)
tor_mmap_t * storage_dir_map(storage_dir_t *d, const char *fname)
storage_dir_t * storage_dir_new(const char *dirname, int max_files)
void storage_dir_remove_file(storage_dir_t *d, const char *fname)
static void storage_dir_clean_tmpfiles(storage_dir_t *d)
int storage_dir_register_with_sandbox(storage_dir_t *d, sandbox_cfg_t **cfg)
int storage_dir_shrink(storage_dir_t *d, uint64_t target_size, int min_to_remove)
static char * find_unused_fname(storage_dir_t *d)
int storage_dir_get_max_files(storage_dir_t *d)
Macros to manage assertions, fatal and non-fatal.
int strcmpend(const char *s1, const char *s2)
Header for util_string.c.