From 92706b158e87c5da00a05686ff7feeafc9b626b9 Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Wed, 5 Aug 2015 16:30:20 +0200 Subject: [PATCH] move mkdirpp code to util.c --- Makefile.in | 2 +- logging.c | 3 +- mkdirpp.c | 135 ---------------------------------------------------- mkdirpp.h | 31 ------------ sources.c | 3 +- util.c | 81 +++++++++++++++++++++++++++++++ util.h | 3 ++ 7 files changed, 87 insertions(+), 171 deletions(-) delete mode 100644 mkdirpp.c delete mode 100644 mkdirpp.h diff --git a/Makefile.in b/Makefile.in index c2784c4..79be3c4 100644 --- a/Makefile.in +++ b/Makefile.in @@ -38,7 +38,7 @@ DESTDIR= HASH_OBJ = @HASH_OBJ@ -OBJS = array.o cmdparse.o conf.o local.o logging.o main.o memory.o mkdirpp.o \ +OBJS = array.o cmdparse.o conf.o local.o logging.o main.o memory.o \ reference.o regress.o rtc.o sched.o sources.o sourcestats.o stubs.o \ sys.o smooth.o tempcomp.o util.o $(HASH_OBJ) diff --git a/logging.c b/logging.c index 2aff130..37fb572 100644 --- a/logging.c +++ b/logging.c @@ -31,7 +31,6 @@ #include "conf.h" #include "logging.h" -#include "mkdirpp.h" #include "util.h" /* This is used by DEBUG_LOG macro */ @@ -307,7 +306,7 @@ LOG_CreateLogFileDir(void) logdir = CNF_GetLogDir(); - if (!mkdir_and_parents(logdir)) { + if (!UTI_CreateDirAndParents(logdir)) { LOG(LOGS_ERR, LOGF_Logging, "Could not create directory %s", logdir); } } diff --git a/mkdirpp.c b/mkdirpp.c deleted file mode 100644 index ed4b23d..0000000 --- a/mkdirpp.c +++ /dev/null @@ -1,135 +0,0 @@ -/* - chronyd/chronyc - Programs for keeping computer clocks accurate. - - ********************************************************************** - * Copyright (C) Richard P. Curnow 1997-2002 - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of version 2 of the GNU General Public License as - * published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - ********************************************************************** - - ======================================================================= - - A function for creating a directory and any parent directories that - don't exist. - - */ - -#include "config.h" - -#include "sysincl.h" - -#include "memory.h" -#include "mkdirpp.h" - -static int -do_dir(char *p) -{ - int status; - struct stat buf; - -#if defined(TEST) - fprintf(stderr, "do_dir(%s)\n", p); -#endif - - /* See if directory exists */ - status = stat(p, &buf); - - if (status < 0) { - if (errno == ENOENT) { - /* Try to create directory */ - status = mkdir(p, 0755); - return status; - } else { - return status; - } - } - - if (!S_ISDIR(buf.st_mode)) { - return -1; - } - - return 0; -} - -/* ================================================== */ -/* Return 0 if the directory couldn't be created, 1 if it could (or - already existed) */ - -int -mkdir_and_parents(const char *path) -{ - char *p; - int len; - int i, j, k, last; - len = strlen(path); - - p = (char *)Malloc(1 + len); - - i = k = 0; - while (1) { - p[i++] = path[k++]; - - if (path[k] == '/' || !path[k]) { - p[i] = 0; - - if (do_dir(p) < 0) { - Free(p); - return 0; - } - - if (!path[k]) { - /* End of the string */ - break; - } - - /* check whether its a trailing / or group of / */ - last = 1; - j = k+1; - while (path[j]) { - if (path[j] != '/') { - k = j - 1; /* Pick up a / into p[] thru the assignment at the top of the loop */ - last = 0; - break; - } - j++; - } - - if (last) { - break; - } - } - - if (!path[k]) break; - - } - - Free(p); - return 1; - -} - -/* ================================================== */ - -#if defined(TEST) -int main(int argc, char **argv) { - if (argc > 1) { - /* Invert sense of result */ - return mkdir_and_parents(argv[1]) ? 0 : 1; - } else { - return 1; - } -} -#endif - diff --git a/mkdirpp.h b/mkdirpp.h deleted file mode 100644 index 5304833..0000000 --- a/mkdirpp.h +++ /dev/null @@ -1,31 +0,0 @@ -/* - chronyd/chronyc - Programs for keeping computer clocks accurate. - - ********************************************************************** - * Copyright (C) Richard P. Curnow 1997-2002 - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of version 2 of the GNU General Public License as - * published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - ********************************************************************** - - ======================================================================= - - */ - -#ifndef GOT_MKDIRPP_H -#define GOT_MKDIRPP_H - -extern int mkdir_and_parents(const char *path); - -#endif diff --git a/sources.c b/sources.c index c6a67c5..8ae1e6c 100644 --- a/sources.c +++ b/sources.c @@ -44,7 +44,6 @@ #include "logging.h" #include "reports.h" #include "nameserv.h" -#include "mkdirpp.h" #include "sched.h" #include "regress.h" @@ -1093,7 +1092,7 @@ SRC_DumpSources(void) direc_len = strlen(direc); file_len = direc_len + 24; filename = MallocArray(char, file_len); /* a bit of slack */ - if (mkdir_and_parents(direc)) { + if (UTI_CreateDirAndParents(direc)) { for (i=0; iref_id) >> 24; b = ((sources[i]->ref_id) >> 16) & 0xff; diff --git a/util.c b/util.c index 7bee7f0..82bb46a 100644 --- a/util.c +++ b/util.c @@ -29,6 +29,7 @@ #include "sysincl.h" +#include "memory.h" #include "util.h" #include "hash.h" @@ -892,3 +893,83 @@ UTI_SetQuitSignalsHandler(void (*handler)(int)) return 1; } + +/* ================================================== */ + +static int +create_dir(char *p) +{ + int status; + struct stat buf; + + /* See if directory exists */ + status = stat(p, &buf); + + if (status < 0) { + if (errno == ENOENT) { + /* Try to create directory */ + status = mkdir(p, 0755); + return status; + } else { + return status; + } + } + + if (!S_ISDIR(buf.st_mode)) { + return -1; + } + + return 0; +} + +/* ================================================== */ +/* Return 0 if the directory couldn't be created, 1 if it could (or + already existed) */ +int +UTI_CreateDirAndParents(const char *path) +{ + char *p; + int i, j, k, last; + + p = (char *)Malloc(1 + strlen(path)); + + i = k = 0; + while (1) { + p[i++] = path[k++]; + + if (path[k] == '/' || !path[k]) { + p[i] = 0; + + if (create_dir(p) < 0) { + Free(p); + return 0; + } + + if (!path[k]) { + /* End of the string */ + break; + } + + /* Check whether its a trailing / or group of / */ + last = 1; + j = k + 1; + while (path[j]) { + if (path[j] != '/') { + k = j - 1; /* Pick up a / into p[] thru the assignment at the top of the loop */ + last = 0; + break; + } + j++; + } + + if (last) + break; + } + + if (!path[k]) + break; + } + + Free(p); + return 1; +} diff --git a/util.h b/util.h index 11ffb16..a01b7aa 100644 --- a/util.h +++ b/util.h @@ -130,4 +130,7 @@ extern int UTI_DecodePasswordFromText(char *key); extern int UTI_SetQuitSignalsHandler(void (*handler)(int)); +/* Create a directory and any parent directories that don't exist */ +extern int UTI_CreateDirAndParents(const char *path); + #endif /* GOT_UTIL_H */