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

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