Move mkdirp into libi3

This commit is contained in:
Deiz 2015-03-29 17:18:00 -04:00 committed by Michael Stapelberg
parent 7c2842eae4
commit fbcb227537
4 changed files with 44 additions and 36 deletions

View File

@ -50,12 +50,6 @@ typedef void (*handler_t)(int, uint8_t *, int, uint32_t, uint32_t);
int size, uint32_t message_size, \
uint32_t message_type)
/**
* Emulates mkdir -p (creates any missing folders)
*
*/
bool mkdirp(const char *path);
/**
* Handler for activity on the listening socket, meaning that a new client
* has just connected and we should accept() him. Sets up the event handler

View File

@ -434,3 +434,9 @@ char *get_exe_path(const char *argv0);
*
*/
int logical_px(const int logical);
/**
* Emulates mkdir -p (creates any missing folders)
*
*/
bool mkdirp(const char *path);

38
libi3/mkdirp.c Normal file
View File

@ -0,0 +1,38 @@
#include "libi3.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
/*
* Emulates mkdir -p (creates any missing folders)
*
*/
bool mkdirp(const char *path) {
if (mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0)
return true;
if (errno != ENOENT) {
ELOG("mkdir(%s) failed: %s\n", path, strerror(errno));
return false;
}
char *copy = sstrdup(path);
/* strip trailing slashes, if any */
while (copy[strlen(copy) - 1] == '/')
copy[strlen(copy) - 1] = '\0';
char *sep = strrchr(copy, '/');
if (sep == NULL) {
if (copy != NULL) {
free(copy);
copy = NULL;
}
return false;
}
*sep = '\0';
bool result = false;
if (mkdirp(copy))
result = mkdirp(path);
free(copy);
return result;
}

View File

@ -37,36 +37,6 @@ static void set_nonblock(int sockfd) {
err(-1, "Could not set O_NONBLOCK");
}
/*
* Emulates mkdir -p (creates any missing folders)
*
*/
bool mkdirp(const char *path) {
if (mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0)
return true;
if (errno != ENOENT) {
ELOG("mkdir(%s) failed: %s\n", path, strerror(errno));
return false;
}
char *copy = sstrdup(path);
/* strip trailing slashes, if any */
while (copy[strlen(copy) - 1] == '/')
copy[strlen(copy) - 1] = '\0';
char *sep = strrchr(copy, '/');
if (sep == NULL) {
FREE(copy);
return false;
}
*sep = '\0';
bool result = false;
if (mkdirp(copy))
result = mkdirp(path);
free(copy);
return result;
}
/*
* Sends the specified event to all IPC clients which are currently connected
* and subscribed to this kind of event.