enumerator: Fall back to lstat() if stat() fails when enumerating dirs/files
authorTobias Brunner <tobias@strongswan.org>
Wed, 12 Feb 2020 16:37:34 +0000 (17:37 +0100)
committerTobias Brunner <tobias@strongswan.org>
Thu, 13 Feb 2020 10:54:19 +0000 (11:54 +0100)
This happens e.g. if the path is for an invalid symlink.

src/libstrongswan/collections/enumerator.c
src/libstrongswan/utils/compat/windows.h

index 21aa9f6..3833d55 100644 (file)
@@ -129,9 +129,10 @@ METHOD(enumerator_t, enumerate_dir_enum, bool,
                {
                        *absolute = this->full;
                }
-               if (st)
+               if (st && stat(this->full, st))
                {
-                       if (stat(this->full, st))
+                       /* try lstat() e.g. if a symlink is not valid anymore */
+                       if ((errno != ENOENT && errno != ENOTDIR) || lstat(this->full, st))
                        {
                                DBG1(DBG_LIB, "stat() on '%s' failed: %s", this->full,
                                         strerror(errno));
index 68e1dca..7c69923 100644 (file)
@@ -223,6 +223,11 @@ static inline int setenv(const char *name, const char *value, int overwrite)
 }
 
 /**
+ * stat(2) behaves like lstat(2) for symbolic links on Windows
+ */
+#define lstat stat
+
+/**
  * Lazy binding, ignored on Windows
  */
 #define RTLD_LAZY 1