From: Vladimír Čunát Date: Thu, 22 Oct 2020 09:17:49 +0000 (+0200) Subject: kluautil.list_dir: make it portable to non-Linux X-Git-Tag: v5.2.0~6^2~10 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=eee60640dfacc518c10e39b706c3a562fa7385d8;p=thirdparty%2Fknot-resolver.git kluautil.list_dir: make it portable to non-Linux --- diff --git a/daemon/lua/kluautil.lua b/daemon/lua/kluautil.lua index a1aebc765..57912e7ba 100644 --- a/daemon/lua/kluautil.lua +++ b/daemon/lua/kluautil.lua @@ -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 diff --git a/daemon/lua/kres-gen.lua b/daemon/lua/kres-gen.lua index 3d993dedb..2eb58737a 100644 --- a/daemon/lua/kres-gen.lua +++ b/daemon/lua/kres-gen.lua @@ -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 *); diff --git a/daemon/lua/kres-gen.sh b/daemon/lua/kres-gen.sh index 1996b2413..7bc6c7374 100755 --- a/daemon/lua/kres-gen.sh +++ b/daemon/lua/kres-gen.sh @@ -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 diff --git a/lib/utils.c b/lib/utils.c index 971794099..6c1c91570 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -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; +} + diff --git a/lib/utils.h b/lib/utils.h index 3b0179512..022ae301e 100644 --- a/lib/utils.h +++ b/lib/utils.h @@ -5,6 +5,7 @@ #pragma once #include +#include #include #include #include @@ -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);