Skip to content

Commit

Permalink
clean-up headers necessary for treefiles.o, minor source formatting f…
Browse files Browse the repository at this point in the history
…ixes, remove unecessary heap allocation in open_treefile()
  • Loading branch information
roker authored and yoe committed Apr 24, 2024
1 parent 4efb275 commit a956b6b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
29 changes: 12 additions & 17 deletions treefiles.c
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
#include "lfs.h"
#include <fcntl.h>
#include <limits.h> // for PATH_MAX
#include <inttypes.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <glib.h>
#include "config.h"
#include "cliserv.h"
#include "treefiles.h"
#include "nbd-debug.h"

#include <backend.h>
#include <config.h>
#include <cliserv.h>
#include <treefiles.h>
#include <nbd-debug.h>
#include <nbdsrv.h>
/**
* Tree structure helper functions
*/
void construct_path(char* name,int lenmax,off_t size, off_t pos, off_t * ppos) {
void construct_path(char* name, int lenmax, off_t size, off_t pos, off_t* ppos) {
if (lenmax<10)
err("Char buffer overflow. This is likely a bug.");

Expand All @@ -34,7 +31,7 @@ void construct_path(char* name,int lenmax,off_t size, off_t pos, off_t * ppos) {
}
}

void delete_treefile(char* name,off_t size,off_t pos) {
void delete_treefile(char* name, off_t size, off_t pos) {
char filename[PATH_MAX];
off_t ppos;

Expand All @@ -48,7 +45,7 @@ void delete_treefile(char* name,off_t size,off_t pos) {
DEBUG("Deleting failed : %s",strerror(errno));
}

void mkdir_path(char * path) {
void mkdir_path(char* path) {
char *subpath=path+1;
while ((subpath=strchr(subpath,'/'))) {
*subpath='\0'; // path is modified in place with terminating null char instead of slash
Expand All @@ -61,15 +58,15 @@ void mkdir_path(char * path) {
}
}

int open_treefile(char* name,mode_t mode,off_t size,off_t pos, pthread_mutex_t *mutex) {
int open_treefile(char* name, mode_t mode, off_t size, off_t pos, pthread_mutex_t* mutex) {
char filename[PATH_MAX];
off_t ppos;

strncpy(filename,name,PATH_MAX-1);
filename[PATH_MAX - 1] = '\0';
construct_path(filename+strlen(name),PATH_MAX-strlen(name)-1,size,pos,&ppos);

DEBUG("Accessing treefile %s ( offset %llu of %llu)",filename,(unsigned long long)pos,(unsigned long long)size);
DEBUG("Accessing treefile %s (offset %llu of %llu)",filename,(unsigned long long)pos,(unsigned long long)size);

pthread_mutex_lock(mutex);
int handle=open(filename, mode, 0600);
Expand All @@ -86,8 +83,7 @@ int open_treefile(char* name,mode_t mode,off_t size,off_t pos, pthread_mutex_t *
} else {

DEBUG("Creating a dummy tempfile for reading");
gchar * tmpname;
tmpname = g_strdup_printf("dummy-XXXXXX");
char tmpname[] = "dummy-XXXXXX";
mode_t oldmode = umask(77);
handle = mkstemp(tmpname);
umask(oldmode);
Expand All @@ -96,9 +92,8 @@ int open_treefile(char* name,mode_t mode,off_t size,off_t pos, pthread_mutex_t *
} else {
err("Error opening tree block file %m");
}
g_free(tmpname);
}
char *n = "\0";
char* n = "\0";
if(lseek(handle,TREEPAGESIZE-1, SEEK_SET) < 0) {
err("Could not create tree file!\n");
}
Expand Down
3 changes: 3 additions & 0 deletions treefiles.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#ifndef NBD_TREEFILES_H
#define NBD_TREEFILES_H

#include <pthread.h>
#include <sys/types.h>

#define TREEDIRSIZE 1024 /**< number of files per subdirectory (or subdirs per subdirectory) */
#define TREEPAGESIZE 4096 /**< tree (block) files uses those chunks */

Expand Down

0 comments on commit a956b6b

Please sign in to comment.