20 void construct_path(
char* name,
int lenmax,off_t size, off_t pos, off_t * ppos) {
22 err(
"Char buffer overflow. This is likely a bug.");
26 snprintf(name,lenmax,
"/FILE%04" PRIX64,(pos/TREEPAGESIZE) %
TREEDIRSIZE);
31 snprintf(buffer,
sizeof(buffer),
"/TREE%04jX",(intmax_t)(*ppos %
TREEDIRSIZE));
32 memcpy(name,buffer,9);
38 char filename[256+strlen(name)];
39 strcpy(filename,name);
43 DEBUG(
"Deleting treefile: %s",filename);
45 if (unlink(filename)==-1)
46 DEBUG(
"Deleting failed : %s",strerror(errno));
51 while ((subpath=strchr(subpath,
'/'))) {
53 if (mkdir(path,0700)==-1) {
55 err(
"Path access error! %m");
62 int open_treefile(
char* name,mode_t mode,off_t size,off_t pos, pthread_mutex_t *mutex) {
63 char filename[256+strlen(name)];
64 strcpy(filename,name);
68 DEBUG(
"Accessing treefile %s ( offset %llu of %llu)",filename,(
unsigned long long)pos,(
unsigned long long)size);
70 pthread_mutex_lock(mutex);
71 int handle=open(filename, mode, 0600);
72 if (handle<0 && errno==ENOENT) {
75 DEBUG(
"Creating new treepath");
78 handle=open(filename, O_RDWR|O_CREAT, 0600);
80 err(
"Error opening tree block file %m");
84 DEBUG(
"Creating a dummy tempfile for reading");
86 tmpname = g_strdup_printf(
"dummy-XXXXXX");
87 handle = mkstemp(tmpname);
91 err(
"Error opening tree block file %m");
97 ssize_t c = write(handle,n,1);
99 err(
"Error setting tree block file size %m");
102 pthread_mutex_unlock(mutex);
void delete_treefile(char *name, off_t size, off_t pos)
void mkdir_path(char *path)
#define TREEPAGESIZE
tree (block) files uses those chunks
void construct_path(char *name, int lenmax, off_t size, off_t pos, off_t *ppos)
Tree structure helper functions.
void myseek(int handle, off_t a)
seek to a position in a file, with error handling.
#define TREEDIRSIZE
number of files per subdirectory (or subdirs per subdirectory)
int open_treefile(char *name, mode_t mode, off_t size, off_t pos, pthread_mutex_t *mutex)