diff options
author | Baptiste Daroussin <bapt@FreeBSD.org> | 2013-04-09 13:44:16 +0400 |
---|---|---|
committer | Baptiste Daroussin <bapt@FreeBSD.org> | 2013-04-09 13:44:16 +0400 |
commit | 82d0c32503b76f0f188bcd01d0c1feab9b450a04 (patch) | |
tree | 77fd49a35623c33046f887017987f095de3a67b3 /src/libexec | |
parent | cadbea88ef1bf51f8b154b89bd7fd9725c74be37 (diff) |
Add a small C code to determine if a directory is empty (faster then find for this)
Diffstat (limited to 'src/libexec')
-rw-r--r-- | src/libexec/poudriere/Makefile | 10 | ||||
-rw-r--r-- | src/libexec/poudriere/dirempty.c | 65 |
2 files changed, 72 insertions, 3 deletions
diff --git a/src/libexec/poudriere/Makefile b/src/libexec/poudriere/Makefile index 623d58e1..4124700a 100644 --- a/src/libexec/poudriere/Makefile +++ b/src/libexec/poudriere/Makefile @@ -1,6 +1,9 @@ PREFIX?= /usr/local -all: dirwatch make_index +all: dirwatch make_index dirempty + +dirempty: dirempty.c + ${CC} -Wall -Werror ${CFLAGS} dirempty.c -o dirempty dirwatch: dirwatch.c ${CC} -Wall -Werror ${CFLAGS} dirwatch.c -o dirwatch @@ -8,10 +11,11 @@ dirwatch: dirwatch.c make_index: make_index.c ${CC} -Wall -Werror ${CFLAGS} make_index.c -o make_index -lsbuf -install: dirwatch make_index +install: dirwatch make_index dirempty mkdir -p ${PREFIX}/libexec/poudriere install -m 755 -o root -g wheel dirwatch ${PREFIX}/libexec/poudriere install -m 755 -o root -g wheel make_index ${PREFIX}/libexec/poudriere + install -m 755 -o root -g wheel dirempty ${PREFIX}/libexec/poudriere clean: - rm -f dirwatch make_index + rm -f dirwatch make_index dirempty diff --git a/src/libexec/poudriere/dirempty.c b/src/libexec/poudriere/dirempty.c new file mode 100644 index 00000000..ebbeeb45 --- /dev/null +++ b/src/libexec/poudriere/dirempty.c @@ -0,0 +1,65 @@ +/*- + * Copyright (c) 2013 Baptiste Daroussin <bapt@FreeBSD.org> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer + * in this position and unchanged. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/types.h> +#include <stdbool.h> +#include <dirent.h> +#include <string.h> +#include <stdlib.h> +#include <err.h> + +bool +dir_empty(const char *path) +{ + struct dirent *ent; + bool ret = true; + DIR *d; + + if ((d = opendir(path)) == NULL) + err(EXIT_FAILURE, "%s: ", path); + + while ((ent = readdir(d))) { + if (strcmp(ent->d_name, ".") == 0 || (strcmp(ent->d_name, "..")) == 0) + continue; + ret = false; + break; + } + closedir(d); + + return (ret); +} + +int +main(int argc, char **argv) +{ + if (argc != 2) + err(EXIT_FAILURE, "Argument missing"); + + if (dir_empty(argv[1])) + return (EXIT_SUCCESS); + + return (EXIT_FAILURE); +} |