Commit 428a030f authored by Daniel Salzman's avatar Daniel Salzman

contrib: move make_path from zonefile to generic file

parent cc37277f
......@@ -21,6 +21,7 @@
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
......@@ -114,3 +115,28 @@ int make_dir(const char *path, mode_t mode, bool ignore_existing)
return KNOT_EOK;
}
int make_path(const char *path, mode_t mode)
{
if (path == NULL) {
return KNOT_EINVAL;
}
char *dir = strdup(path);
if (dir == NULL) {
return KNOT_ENOMEM;
}
for (char *p = strchr(dir + 1, '/'); p != NULL; p = strchr(p + 1, '/')) {
*p = '\0';
if (mkdir(dir, mode) == -1 && errno != EEXIST) {
free(dir);
return knot_map_errno();
}
*p = '/';
}
free(dir);
return KNOT_EOK;
}
......@@ -30,3 +30,8 @@ bool remove_path(const char *path);
* Equivalent to mkdir(2), can succeed if the directory already exists.
*/
int make_dir(const char *path, mode_t mode, bool ignore_existing);
/*!
* Makes a directory part of the path with all parent directories if not exist.
*/
int make_path(const char *path, mode_t mode);
......@@ -25,6 +25,7 @@
#include <assert.h>
#include "knot/common/log.h"
#include "contrib/files.h"
#include "knot/server/journal.h"
#include "knot/server/serialization.h"
#include "knot/zone/zone.h"
......@@ -105,6 +106,12 @@ static int journal_create_file(const char *fn, uint16_t max_nodes)
struct flock fl = { .l_type = F_WRLCK, .l_whence = SEEK_SET,
.l_start = 0, .l_len = 0, .l_pid = getpid() };
/* Create journal file path. */
int ret = make_path(fn, S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP);
if (ret != KNOT_EOK) {
return ret;
}
/* Create journal file. */
int fd = open(fn, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP);
if (fd < 0) {
......
......@@ -26,6 +26,7 @@
#include <inttypes.h>
#include "libknot/libknot.h"
#include "contrib/files.h"
#include "contrib/macros.h"
#include "contrib/string.h"
#include "knot/common/log.h"
......@@ -339,32 +340,6 @@ open_tmp_failed:
return ret;
}
/*! \brief Prepare a directory for the file. */
static int make_path(const char *path, mode_t mode)
{
if (path == NULL) {
return KNOT_EINVAL;
}
char *dir = strdup(path);
if (dir == NULL) {
return KNOT_ENOMEM;
}
for (char *p = strchr(dir + 1, '/'); p != NULL; p = strchr(p + 1, '/')) {
*p = '\0';
if (mkdir(dir, mode) == -1 && errno != EEXIST) {
free(dir);
return knot_map_errno();
}
*p = '/';
}
free(dir);
return KNOT_EOK;
}
int zonefile_write(const char *path, zone_contents_t *zone)
{
if (!zone || !path) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment