readlinkat.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* Read a symlink relative to an open directory.
  2. Copyright (C) 2009-2017 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /* written by Eric Blake */
  14. #include <config.h>
  15. #include <errno.h>
  16. #include <unistd.h>
  17. #include <string.h>
  18. #include <sys/stat.h>
  19. #if HAVE_READLINKAT
  20. # undef readlinkat
  21. ssize_t
  22. rpl_readlinkat (int fd, char const *file, char *buf, size_t len)
  23. {
  24. # if READLINK_TRAILING_SLASH_BUG
  25. size_t file_len = strlen (file);
  26. if (file_len && file[file_len - 1] == '/')
  27. {
  28. /* Even if FILE without the slash is a symlink to a directory,
  29. both lstat() and stat() must resolve the trailing slash to
  30. the directory rather than the symlink. We can therefore
  31. safely use stat() to distinguish between EINVAL and
  32. ENOTDIR/ENOENT, avoiding extra overhead of rpl_lstat(). */
  33. struct stat st;
  34. if (stat (file, &st) == 0)
  35. errno = EINVAL;
  36. return -1;
  37. }
  38. # endif /* READLINK_TRAILING_SLASH_BUG */
  39. return readlinkat (fd, file, buf, len);
  40. }
  41. #else
  42. /* Gnulib provides a readlink stub for mingw; use it for distinction
  43. between EINVAL and ENOENT, rather than always failing with ENOSYS. */
  44. /* POSIX 2008 says that unlike readlink, readlinkat returns 0 for
  45. success instead of the buffer length. But this would render
  46. readlinkat worthless since readlink does not guarantee a
  47. NUL-terminated buffer. Assume this was a bug in POSIX. */
  48. /* Read the contents of symlink FILE into buffer BUF of size LEN, in the
  49. directory open on descriptor FD. If possible, do it without changing
  50. the working directory. Otherwise, resort to using save_cwd/fchdir,
  51. then readlink/restore_cwd. If either the save_cwd or the restore_cwd
  52. fails, then give a diagnostic and exit nonzero. */
  53. # define AT_FUNC_NAME readlinkat
  54. # define AT_FUNC_F1 readlink
  55. # define AT_FUNC_POST_FILE_PARAM_DECLS , char *buf, size_t len
  56. # define AT_FUNC_POST_FILE_ARGS , buf, len
  57. # define AT_FUNC_RESULT ssize_t
  58. # include "at-func.c"
  59. # undef AT_FUNC_NAME
  60. # undef AT_FUNC_F1
  61. # undef AT_FUNC_POST_FILE_PARAM_DECLS
  62. # undef AT_FUNC_POST_FILE_ARGS
  63. # undef AT_FUNC_RESULT
  64. #endif