Blame SOURCES/Add-k5_dir_filenames-to-libkrb5support.patch

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