Skip to content
This repository has been archived by the owner on Jan 6, 2023. It is now read-only.

Commit

Permalink
Abstract out dot filename allocation from cve_db_lock_init()
Browse files Browse the repository at this point in the history
This code could be reused later to allocate other dot file names.

Signed-off-by: Sergey Popovich <[email protected]>
  • Loading branch information
Sergey Popovich authored and Serhii Popovych committed Jan 5, 2016
1 parent 568ac0f commit 567126f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 26 deletions.
29 changes: 3 additions & 26 deletions src/library/cve-db-lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <assert.h>

#include "core.h"
#include "util.h"
#include "cve-db-lock.h"

static const short int locktype2l_type[] = {
Expand All @@ -47,38 +48,14 @@ bool cve_db_lock_init(const char *db_path)
{
const int flags = O_RDWR|O_CREAT|O_NONBLOCK|O_NOFOLLOW;
const mode_t mode = S_IRUSR|S_IWUSR;
const char *dir;
char *file, *path;
int ret;

assert(db_lock_fd == -1);
assert(db_lock_fname == NULL);
assert(db_path != NULL);

path = strdup(db_path);
if (!path)
return false;

file = strrchr(path, '/');
if (file) {
*file++ = '\0';
if (!*file)
file = (char *) nvd_file;
dir = *path ? path : ".";
} else {
file = path;
dir = ".";
}

ret = asprintf(&db_lock_fname, "%s/.%s.cve.lock", dir, file);
if (ret == -1) {
/* db_lock_fname contents undefined: force to NULL */
db_lock_fname = NULL;
} else {
db_lock_fname = get_db_dot_fname(db_path, "cve.lock");
if (db_lock_fname)
db_lock_fd = open(db_lock_fname, flags, mode);
}

free(path);

return db_lock_fd != -1;
}
Expand Down
33 changes: 33 additions & 0 deletions src/library/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,39 @@ char *cve_get_file_parent(const char *p)
return dirname(r);
}

char *get_db_dot_fname(const char *db_path, const char *suffix)
{
autofree(char) *path = NULL;
char *dot_fname;
int ret = -1;

path = strdup(db_path);
if (path) {
const char *dir;
char *file;

file = strrchr(path, '/');
if (file) {
*file++ = '\0';
if (!*file)
file = (char *) nvd_file;
dir = *path ? path : ".";
} else {
file = path;
dir = ".";
}

ret = asprintf(&dot_fname, "%s/.%s.%s", dir, file, suffix);
}

if (ret == -1) {
/* dot_fname contents undefined on error: force to NULL */
dot_fname = NULL;
}

return dot_fname;
}

/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
Expand Down
9 changes: 9 additions & 0 deletions src/library/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,15 @@ bool cve_is_dir(const char *p);
*/
char *cve_get_file_parent(const char *p);

/**
* Return absolute path to the dot file in format .basename(db_path).suffix
*
* @note This is an allocated string, and must be freed by the caller
* @param db_path Path to the database directory
* @return a newly allocated string or NULL in case of error
*/
char *get_db_dot_fname(const char *db_path, const char *suffix);

/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
Expand Down

0 comments on commit 567126f

Please sign in to comment.