tor  0.4.2.0-alpha-dev
Macros | Typedefs | Functions
storagedir.h File Reference
#include "lib/cc/torint.h"
#include <stddef.h>

Go to the source code of this file.

Macros

#define storage_dir_free(d)   FREE_AND_NULL(storage_dir_t, storage_dir_free_, (d))
 

Typedefs

typedef struct storage_dir_t storage_dir_t
 

Functions

storage_dir_tstorage_dir_new (const char *dirname, int n_files)
 
void storage_dir_free_ (storage_dir_t *d)
 
int storage_dir_register_with_sandbox (storage_dir_t *d, struct sandbox_cfg_elem **cfg)
 
const struct smartlist_tstorage_dir_list (storage_dir_t *d)
 
uint64_t storage_dir_get_usage (storage_dir_t *d)
 
struct tor_mmap_tstorage_dir_map (storage_dir_t *d, const char *fname)
 
uint8_t * storage_dir_read (storage_dir_t *d, const char *fname, int bin, size_t *sz_out)
 
int storage_dir_save_bytes_to_file (storage_dir_t *d, const uint8_t *data, size_t length, int binary, char **fname_out)
 
int storage_dir_save_string_to_file (storage_dir_t *d, const char *data, int binary, char **fname_out)
 
int storage_dir_save_labeled_to_file (storage_dir_t *d, const struct config_line_t *labels, const uint8_t *data, size_t length, char **fname_out)
 
struct tor_mmap_tstorage_dir_map_labeled (storage_dir_t *dir, const char *fname, struct config_line_t **labels_out, const uint8_t **data_out, size_t *size_out)
 
uint8_t * storage_dir_read_labeled (storage_dir_t *d, const char *fname, struct config_line_t **labels_out, size_t *sz_out)
 
void storage_dir_remove_file (storage_dir_t *d, const char *fname)
 
int storage_dir_shrink (storage_dir_t *d, uint64_t target_size, int min_to_remove)
 
int storage_dir_remove_all (storage_dir_t *d)
 
int storage_dir_get_max_files (storage_dir_t *d)
 

Detailed Description

Header for storagedir.c.

Definition in file storagedir.h.

Function Documentation

◆ storage_dir_free_()

void storage_dir_free_ ( storage_dir_t d)

Drop all in-RAM storage for d. Does not delete any files.

Definition at line 83 of file storagedir.c.

References storage_dir_t::contents, storage_dir_t::directory, SMARTLIST_FOREACH, and tor_free.

◆ storage_dir_get_max_files()

int storage_dir_get_max_files ( storage_dir_t d)

Return the largest number of non-temporary files we're willing to store in d.

Definition at line 603 of file storagedir.c.

References storage_dir_t::max_files.

◆ storage_dir_get_usage()

uint64_t storage_dir_get_usage ( storage_dir_t d)

Return the total number of bytes used for storage in d.

Definition at line 190 of file storagedir.c.

References storage_dir_t::directory, sandbox_intern_string, SMARTLIST_FOREACH_BEGIN, storage_dir_list(), tor_asprintf(), tor_free, storage_dir_t::usage, and storage_dir_t::usage_known.

Referenced by storage_dir_shrink().

◆ storage_dir_list()

const struct smartlist_t* storage_dir_list ( storage_dir_t d)

Return a smartlist containing the filenames within d.

Definition at line 179 of file storagedir.c.

References storage_dir_t::contents, and storage_dir_rescan().

Referenced by storage_dir_get_usage().

◆ storage_dir_map()

struct tor_mmap_t* storage_dir_map ( storage_dir_t d,
const char *  fname 
)

Mmap a specified file within d.

On failure, return NULL and set errno as for tor_mmap_file().

Definition at line 215 of file storagedir.c.

References storage_dir_t::directory, and tor_asprintf().

Referenced by storage_dir_map_labeled().

◆ storage_dir_map_labeled()

struct 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 
)

Map a file that was created with storage_dir_save_labeled_to_file(). On failure, return NULL. On success, write a set of newly allocated labels into *labels_out, a pointer to the data into *data_out, and the data's size into *sz_out. On success, also return a tor_mmap_t object whose contents should not be used – it needs to be kept around, though, for as long as data_out is going to be valid.

On failure, set errno as for tor_mmap_file() if the file was missing or empty, and set errno to EINVAL if the file was not in the labeled format expected.

Definition at line 399 of file storagedir.c.

References config_get_lines(), tor_mmap_t::data, tor_mmap_t::size, storage_dir_map(), and tor_assert().

Referenced by storage_dir_read_labeled().

◆ storage_dir_new()

storage_dir_t* storage_dir_new ( const char *  dirname,
int  max_files 
)

Create or open a new storage directory at dirname, with capacity for up to max_files files.

Definition at line 68 of file storagedir.c.

◆ storage_dir_read()

uint8_t* storage_dir_read ( storage_dir_t d,
const char *  fname,
int  bin,
size_t *  sz_out 
)

Read a file within d into a newly allocated buffer. Set *sz_out to its size.

Definition at line 230 of file storagedir.c.

References storage_dir_t::directory, RFTS_BIN, tor_asprintf(), tor_assert(), and tor_free.

◆ storage_dir_read_labeled()

uint8_t* storage_dir_read_labeled ( storage_dir_t dir,
const char *  fname,
config_line_t **  labels_out,
size_t *  sz_out 
)

As storage_dir_map_labeled, but return a new byte array containing the data.

Definition at line 435 of file storagedir.c.

References storage_dir_map_labeled().

◆ storage_dir_remove_all()

int storage_dir_remove_all ( storage_dir_t d)

Remove all files in d.

Definition at line 593 of file storagedir.c.

References storage_dir_t::max_files, and storage_dir_shrink().

◆ storage_dir_remove_file()

void storage_dir_remove_file ( storage_dir_t d,
const char *  fname 
)

Remove the file called fname from d.

Definition at line 480 of file storagedir.c.

References storage_dir_t::directory, sandbox_intern_string, tor_asprintf(), and storage_dir_t::usage_known.

◆ storage_dir_save_bytes_to_file()

int storage_dir_save_bytes_to_file ( storage_dir_t d,
const uint8_t *  data,
size_t  length,
int  binary,
char **  fname_out 
)

Try to write the length bytes at data into a new file in d. On success, return 0 and set *fname_out to a newly allocated string containing the filename. On failure, return -1.

Definition at line 311 of file storagedir.c.

References smartlist_add(), and storage_dir_save_chunks_to_file().

Referenced by storage_dir_save_string_to_file().

◆ storage_dir_save_string_to_file()

int storage_dir_save_string_to_file ( storage_dir_t d,
const char *  str,
int  binary,
char **  fname_out 
)

As storage_dir_save_bytes_to_file, but saves a NUL-terminated string str.

Definition at line 330 of file storagedir.c.

References storage_dir_save_bytes_to_file().

◆ storage_dir_shrink()

int storage_dir_shrink ( storage_dir_t d,
uint64_t  target_size,
int  min_to_remove 
)

Try to free space by removing the oldest files in d. Delete until no more than target_size bytes are left, and at least min_to_remove files have been removed... or until there is nothing left to remove.

Return 0 on success; -1 on failure.

Definition at line 540 of file storagedir.c.

References storage_dir_get_usage(), storage_dir_rescan(), storage_dir_t::usage, and storage_dir_t::usage_known.

Referenced by storage_dir_remove_all().