From b2110b75bd8189fcfa52a5411adbdb92acb49ecd Mon Sep 17 00:00:00 2001 From: Peter Bex Date: Sun, 12 Aug 2018 19:40:22 +0200 Subject: [PATCH] Fix stat() with trailing slashes behaviour on Haiku It would return that the file exists even if the target is not a directory. We work around it the same way we do with MinGW. --- chicken.h | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/chicken.h b/chicken.h index 1bbd1ba6..4697560b 100644 --- a/chicken.h +++ b/chicken.h @@ -3419,9 +3419,7 @@ inline static size_t C_strlcat(char *dst, const char *src, size_t sz) * in a slash, since in this case MinGW's stat() will succeed but return a * non-directory mode in buf.st_mode. */ -#ifndef __MINGW32__ -# define C_stat stat -#else +#if defined(__MINGW32__) inline static int C_stat(const char *path, struct stat *buf) { size_t len = C_strlen(path); @@ -3448,6 +3446,29 @@ dircheck: return 0; } +/* + * Haiku's stat() has a similar issue, where it will gladly succeed + * when given a path to a filename with a trailing slash. + */ +#elif defined(__HAIKU__) +inline static int C_stat(const char *path, struct stat *buf) +{ + size_t len = C_strlen(path); + char slash = len && path[len - 1] == '/'; + + if(stat(path, buf) != 0) { + return -1; + } + + if (slash && !S_ISDIR(buf->st_mode)) { + errno = ENOTDIR; + return -1; + } + + return 0; +} +#else +# define C_stat stat #endif /* Safe realpath usage depends on a reliable PATH_MAX. */ -- 2.11.0