]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
kluautil.list_dir: make it portable to non-Linux
authorVladimír Čunát <vladimir.cunat@nic.cz>
Thu, 22 Oct 2020 09:17:49 +0000 (11:17 +0200)
committerTomas Krizek <tomas.krizek@nic.cz>
Mon, 26 Oct 2020 13:28:50 +0000 (14:28 +0100)
daemon/lua/kluautil.lua
daemon/lua/kres-gen.lua
daemon/lua/kres-gen.sh
lib/utils.c
lib/utils.h

index a1aebc7654278fad99642e3efb0616254abfc282..57912e7ba10f03a2ac5f8ce9772e72c50a3c8d66 100644 (file)
@@ -28,22 +28,15 @@ function kluautil.kr_table_unpack(tab)
        return unpack(tab, 1, tab.n)
 end
 
--- Fetch over HTTPS
 ffi.cdef([[
        typedef struct __dirstream DIR;
-       struct dirent {
-               unsigned long int       d_ino;
-               long int                d_off;
-               unsigned short          d_reclen;
-               unsigned char           d_type;
-               char                    d_name[256];
-       };
        DIR *opendir(const char *name);
        struct dirent *readdir(DIR *dirp);
        int closedir(DIR *dirp);
        char *strerror(int errnum);
 ]])
 
+-- Fetch over HTTPS
 function kluautil.kr_https_fetch(url, out_file, ca_file)
        local http_ok, http_request = pcall(require, 'http.request')
        local httptls_ok, http_tls = pcall(require, 'http.tls')
@@ -105,7 +98,7 @@ function kluautil.list_dir (path)
 
        local entry = ffi.C.readdir(dir)
        while entry ~= nil do
-               local entry_name = ffi.string(entry.d_name)
+               local entry_name = ffi.string(ffi.C.kr_dirent_name(entry))
                if entry_name ~= '.' and entry_name ~= '..' then
                        table.insert(results, entry_name)
                end
index 3d993dedb90957d8adfb12696677c6d62cca77b3..2eb58737a2d9acb8b1d10a63be93c5738290d685 100644 (file)
@@ -374,6 +374,7 @@ uint64_t kr_now();
 const char *kr_strptime_diff(const char *, const char *, const char *, double *);
 time_t kr_file_mtime(const char *);
 long long kr_fssize(const char *);
+const char *kr_dirent_name(const struct dirent *);
 void lru_free_items_impl(struct lru *);
 struct lru *lru_create_impl(unsigned int, unsigned int, knot_mm_t *, knot_mm_t *);
 void *lru_get_impl(struct lru *, const char *, unsigned int, unsigned int, _Bool, _Bool *);
index 1996b2413e7e0b64655579d73da08c50837536c3..7bc6c737438929d1fe9af24bb656c898fc68ab40 100755 (executable)
@@ -230,6 +230,7 @@ ${CDEFS} ${LIBKRES} functions <<-EOF
        kr_strptime_diff
        kr_file_mtime
        kr_fssize
+       kr_dirent_name
        lru_free_items_impl
        lru_create_impl
        lru_get_impl
index 97179409931658bbf93bf198884387c1d6e1c7db..6c1c9157027479873cdf4bd7e1a3e52c53f29cfc 100644 (file)
@@ -1290,3 +1290,8 @@ long long kr_fssize(const char *path)
        return buf.f_frsize * buf.f_blocks;
 }
 
+const char * kr_dirent_name(const struct dirent *de)
+{
+       return de ? de->d_name : NULL;
+}
+
index 3b017951294208a5c87719d68031cea5decb89ce..022ae301e054d87efae2c264a849ebce24be38a1 100644 (file)
@@ -5,6 +5,7 @@
 #pragma once
 
 #include <assert.h>
+#include <dirent.h>
 #include <stdio.h>
 #include <stdbool.h>
 #include <sys/socket.h>
@@ -561,4 +562,6 @@ KR_EXPORT uint16_t kr_rrsig_type_covered(const knot_rdata_t *rdata);
 KR_EXPORT time_t kr_file_mtime (const char* fname);
 /** Return filesystem size in bytes. */
 KR_EXPORT long long kr_fssize(const char *path);
+/** Simply return de->dname. (useful from Lua) */
+KR_EXPORT const char * kr_dirent_name(const struct dirent *de);