|
|
749169 |
From a3f548a88d0bbf16ccc60843fb72e02e32a765f3 Mon Sep 17 00:00:00 2001
|
|
|
665228 |
From: Greg Hudson <ghudson@mit.edu>
|
|
|
665228 |
Date: Tue, 5 Jun 2018 14:01:05 -0400
|
|
|
665228 |
Subject: [PATCH] Add k5_dir_filenames() to libkrb5support
|
|
|
665228 |
|
|
|
665228 |
Add a support function to get a list of filenames from a directory in
|
|
|
665228 |
sorted order.
|
|
|
665228 |
|
|
|
665228 |
(cherry picked from commit 27534121eb39089ff4335d8b465027e9ba783682)
|
|
|
665228 |
(cherry picked from commit 9010a0dbf59771cb0a9c1e6fd5a18a92a1200ca7)
|
|
|
665228 |
[rharwood@redhat.com: exports file context doesn't match]
|
|
|
665228 |
---
|
|
|
665228 |
src/include/k5-platform.h | 7 +
|
|
|
665228 |
src/util/support/Makefile.in | 3 +
|
|
|
665228 |
src/util/support/dir_filenames.c | 135 ++++++++++++++++++
|
|
|
665228 |
src/util/support/libkrb5support-fixed.exports | 2 +
|
|
|
665228 |
4 files changed, 147 insertions(+)
|
|
|
665228 |
create mode 100644 src/util/support/dir_filenames.c
|
|
|
665228 |
|
|
|
665228 |
diff --git a/src/include/k5-platform.h b/src/include/k5-platform.h
|
|
|
665228 |
index 994f46323..5a58ccba2 100644
|
|
|
665228 |
--- a/src/include/k5-platform.h
|
|
|
665228 |
+++ b/src/include/k5-platform.h
|
|
|
665228 |
@@ -44,6 +44,8 @@
|
|
|
665228 |
* + constant time memory comparison
|
|
|
665228 |
* + path manipulation
|
|
|
665228 |
* + _, N_, dgettext, bindtextdomain (for localization)
|
|
|
665228 |
+ * + getopt_long
|
|
|
665228 |
+ * + fetching filenames from a directory
|
|
|
665228 |
*/
|
|
|
665228 |
|
|
|
665228 |
#ifndef K5_PLATFORM_H
|
|
|
665228 |
@@ -1099,4 +1101,9 @@ extern int k5_getopt_long(int nargc, char **nargv, char *options,
|
|
|
665228 |
#define getopt_long k5_getopt_long
|
|
|
665228 |
#endif /* HAVE_GETOPT_LONG */
|
|
|
665228 |
|
|
|
665228 |
+/* Set *fnames_out to a null-terminated list of filenames within dirname,
|
|
|
665228 |
+ * sorted according to strcmp(). Return 0 on success, or ENOENT/ENOMEM. */
|
|
|
665228 |
+int k5_dir_filenames(const char *dirname, char ***fnames_out);
|
|
|
665228 |
+void k5_free_filenames(char **fnames);
|
|
|
665228 |
+
|
|
|
665228 |
#endif /* K5_PLATFORM_H */
|
|
|
665228 |
diff --git a/src/util/support/Makefile.in b/src/util/support/Makefile.in
|
|
|
665228 |
index 17bcd2a67..9326742d7 100644
|
|
|
665228 |
--- a/src/util/support/Makefile.in
|
|
|
665228 |
+++ b/src/util/support/Makefile.in
|
|
|
665228 |
@@ -84,6 +84,7 @@ STLIBOBJS= \
|
|
|
665228 |
json.o \
|
|
|
665228 |
bcmp.o \
|
|
|
665228 |
strerror_r.o \
|
|
|
665228 |
+ dir_filenames.o \
|
|
|
665228 |
$(GETTIMEOFDAY_ST_OBJ) \
|
|
|
665228 |
$(IPC_ST_OBJ) \
|
|
|
665228 |
$(STRLCPY_ST_OBJ) \
|
|
|
665228 |
@@ -109,6 +110,7 @@ LIBOBJS= \
|
|
|
665228 |
$(OUTPRE)json.$(OBJEXT) \
|
|
|
665228 |
$(OUTPRE)bcmp.$(OBJEXT) \
|
|
|
665228 |
$(OUTPRE)strerror_r.$(OBJEXT) \
|
|
|
665228 |
+ $(OUTPRE)dir_filenames.$(OBJEXT) \
|
|
|
665228 |
$(GETTIMEOFDAY_OBJ) \
|
|
|
665228 |
$(IPC_OBJ) \
|
|
|
665228 |
$(STRLCPY_OBJ) \
|
|
|
665228 |
@@ -143,6 +145,7 @@ SRCS=\
|
|
|
665228 |
$(srcdir)/json.c \
|
|
|
665228 |
$(srcdir)/bcmp.c \
|
|
|
665228 |
$(srcdir)/strerror_r.c \
|
|
|
665228 |
+ $(srcdir)/dir_filenames.c \
|
|
|
665228 |
$(srcdir)/t_utf8.c \
|
|
|
665228 |
$(srcdir)/getopt.c \
|
|
|
665228 |
$(srcdir)/getopt_long.c
|
|
|
665228 |
diff --git a/src/util/support/dir_filenames.c b/src/util/support/dir_filenames.c
|
|
|
665228 |
new file mode 100644
|
|
|
665228 |
index 000000000..9312b0238
|
|
|
665228 |
--- /dev/null
|
|
|
665228 |
+++ b/src/util/support/dir_filenames.c
|
|
|
665228 |
@@ -0,0 +1,135 @@
|
|
|
665228 |
+/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
|
|
|
665228 |
+/* util/support/dir_filenames.c - fetch filenames in a directory */
|
|
|
665228 |
+/*
|
|
|
665228 |
+ * Copyright (C) 2018 by the Massachusetts Institute of Technology.
|
|
|
665228 |
+ * All rights reserved.
|
|
|
665228 |
+ *
|
|
|
665228 |
+ * Redistribution and use in source and binary forms, with or without
|
|
|
665228 |
+ * modification, are permitted provided that the following conditions
|
|
|
665228 |
+ * are met:
|
|
|
665228 |
+ *
|
|
|
665228 |
+ * * Redistributions of source code must retain the above copyright
|
|
|
665228 |
+ * notice, this list of conditions and the following disclaimer.
|
|
|
665228 |
+ *
|
|
|
665228 |
+ * * Redistributions in binary form must reproduce the above copyright
|
|
|
665228 |
+ * notice, this list of conditions and the following disclaimer in
|
|
|
665228 |
+ * the documentation and/or other materials provided with the
|
|
|
665228 |
+ * distribution.
|
|
|
665228 |
+ *
|
|
|
665228 |
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
665228 |
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
665228 |
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
665228 |
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
665228 |
+ * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
|
|
665228 |
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
665228 |
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
665228 |
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
665228 |
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
665228 |
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
665228 |
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
|
665228 |
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
665228 |
+ */
|
|
|
665228 |
+
|
|
|
665228 |
+#include "k5-platform.h"
|
|
|
665228 |
+
|
|
|
665228 |
+void
|
|
|
665228 |
+k5_free_filenames(char **fnames)
|
|
|
665228 |
+{
|
|
|
665228 |
+ char **fn;
|
|
|
665228 |
+
|
|
|
665228 |
+ for (fn = fnames; fn != NULL && *fn != NULL; fn++)
|
|
|
665228 |
+ free(*fn);
|
|
|
665228 |
+ free(fnames);
|
|
|
665228 |
+}
|
|
|
665228 |
+
|
|
|
665228 |
+/* Resize the filename list and add a name. */
|
|
|
665228 |
+static int
|
|
|
665228 |
+add_filename(char ***fnames, int *n_fnames, const char *name)
|
|
|
665228 |
+{
|
|
|
665228 |
+ char **newlist;
|
|
|
665228 |
+
|
|
|
665228 |
+ newlist = realloc(*fnames, (*n_fnames + 2) * sizeof(*newlist));
|
|
|
665228 |
+ if (newlist == NULL)
|
|
|
665228 |
+ return ENOMEM;
|
|
|
665228 |
+ *fnames = newlist;
|
|
|
665228 |
+ newlist[*n_fnames] = strdup(name);
|
|
|
665228 |
+ if (newlist[*n_fnames] == NULL)
|
|
|
665228 |
+ return ENOMEM;
|
|
|
665228 |
+ (*n_fnames)++;
|
|
|
665228 |
+ newlist[*n_fnames] = NULL;
|
|
|
665228 |
+ return 0;
|
|
|
665228 |
+}
|
|
|
665228 |
+
|
|
|
665228 |
+static int
|
|
|
665228 |
+compare_with_strcmp(const void *a, const void *b)
|
|
|
665228 |
+{
|
|
|
665228 |
+ return strcmp(*(char **)a, *(char **)b);
|
|
|
665228 |
+}
|
|
|
665228 |
+
|
|
|
665228 |
+#ifdef _WIN32
|
|
|
665228 |
+
|
|
|
665228 |
+int
|
|
|
665228 |
+k5_dir_filenames(const char *dirname, char ***fnames_out)
|
|
|
665228 |
+{
|
|
|
665228 |
+ char *wildcard;
|
|
|
665228 |
+ WIN32_FIND_DATA ffd;
|
|
|
665228 |
+ HANDLE handle;
|
|
|
665228 |
+ char **fnames = NULL;
|
|
|
665228 |
+ int n_fnames = 0;
|
|
|
665228 |
+
|
|
|
665228 |
+ *fnames_out = NULL;
|
|
|
665228 |
+
|
|
|
665228 |
+ if (asprintf(&wildcard, "%s\\*", dirname) < 0)
|
|
|
665228 |
+ return ENOMEM;
|
|
|
665228 |
+ handle = FindFirstFile(wildcard, &ffd;;
|
|
|
665228 |
+ free(wildcard);
|
|
|
665228 |
+ if (handle == INVALID_HANDLE_VALUE)
|
|
|
665228 |
+ return ENOENT;
|
|
|
665228 |
+
|
|
|
665228 |
+ do {
|
|
|
665228 |
+ if (add_filename(&fnames, &n_fnames, &ffd.cFileName) != 0) {
|
|
|
665228 |
+ k5_free_filenames(fnames);
|
|
|
665228 |
+ FindClose(handle);
|
|
|
665228 |
+ return ENOMEM;
|
|
|
665228 |
+ }
|
|
|
665228 |
+ } while (FindNextFile(handle, &ffd) != 0);
|
|
|
665228 |
+
|
|
|
665228 |
+ FindClose(handle);
|
|
|
665228 |
+ qsort(fnames, n_fnames, sizeof(*fnames), compare_with_strcmp);
|
|
|
665228 |
+ *fnames_out = fnames;
|
|
|
665228 |
+ return 0;
|
|
|
665228 |
+}
|
|
|
665228 |
+
|
|
|
665228 |
+#else /* _WIN32 */
|
|
|
665228 |
+
|
|
|
665228 |
+#include <dirent.h>
|
|
|
665228 |
+
|
|
|
665228 |
+int
|
|
|
665228 |
+k5_dir_filenames(const char *dirname, char ***fnames_out)
|
|
|
665228 |
+{
|
|
|
665228 |
+ DIR *dir;
|
|
|
665228 |
+ struct dirent *ent;
|
|
|
665228 |
+ char **fnames = NULL;
|
|
|
665228 |
+ int n_fnames = 0;
|
|
|
665228 |
+
|
|
|
665228 |
+ *fnames_out = NULL;
|
|
|
665228 |
+
|
|
|
665228 |
+ dir = opendir(dirname);
|
|
|
665228 |
+ if (dir == NULL)
|
|
|
665228 |
+ return ENOENT;
|
|
|
665228 |
+
|
|
|
665228 |
+ while ((ent = readdir(dir)) != NULL) {
|
|
|
665228 |
+ if (add_filename(&fnames, &n_fnames, ent->d_name) != 0) {
|
|
|
665228 |
+ k5_free_filenames(fnames);
|
|
|
665228 |
+ closedir(dir);
|
|
|
665228 |
+ return ENOMEM;
|
|
|
665228 |
+ }
|
|
|
665228 |
+ }
|
|
|
665228 |
+
|
|
|
665228 |
+ closedir(dir);
|
|
|
665228 |
+ qsort(fnames, n_fnames, sizeof(*fnames), compare_with_strcmp);
|
|
|
665228 |
+ *fnames_out = fnames;
|
|
|
665228 |
+ return 0;
|
|
|
665228 |
+}
|
|
|
665228 |
+
|
|
|
665228 |
+#endif /* not _WIN32 */
|
|
|
665228 |
diff --git a/src/util/support/libkrb5support-fixed.exports b/src/util/support/libkrb5support-fixed.exports
|
|
|
665228 |
index d5d4177b7..2cdcddfe0 100644
|
|
|
665228 |
--- a/src/util/support/libkrb5support-fixed.exports
|
|
|
665228 |
+++ b/src/util/support/libkrb5support-fixed.exports
|
|
|
665228 |
@@ -52,6 +52,8 @@ k5_path_isabs
|
|
|
665228 |
k5_path_join
|
|
|
665228 |
k5_path_split
|
|
|
665228 |
k5_strerror_r
|
|
|
665228 |
+k5_dir_filenames
|
|
|
665228 |
+k5_free_filenames
|
|
|
665228 |
krb5int_key_register
|
|
|
665228 |
krb5int_key_delete
|
|
|
665228 |
krb5int_getspecific
|