+static void create_directories(const char *path)
+{
+ int len = strlen(path);
+ char *buf = malloc(len + 1);
+ const char *slash = path;
+
+ while ((slash = strchr(slash+1, '/')) != NULL) {
+ len = slash - path;
+ memcpy(buf, path, len);
+ buf[len] = 0;
+ mkdir(buf, 0755);
+ }
+}
+
+static int create_file(const char *path, unsigned int mode)
+{
+ int fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0600);
+ if (fd < 0) {
+ if (errno == ENOENT) {
+ create_directories(path);
+ fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0600);
+ }
+ }
+ if (fd >= 0)
+ fchmod(fd, mode);
+ return fd;
+}
+