fs_readlink.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /****************************************************************************
  2. * fs/vfs/fs_readlink.c
  3. *
  4. * Copyright (C) 2017 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT will THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <sys/types.h>
  40. #include <stdbool.h>
  41. #include <string.h>
  42. #include <assert.h>
  43. #include <errno.h>
  44. #include <nuttx/fs/fs.h>
  45. #include "inode/inode.h"
  46. #ifdef CONFIG_PSEUDOFS_SOFTLINKS
  47. /****************************************************************************
  48. * Public Functions
  49. ****************************************************************************/
  50. /****************************************************************************
  51. * Name: mkdir
  52. *
  53. * Description:
  54. * The readlink() function will place the contents of the symbolic link
  55. * referred to by 'path' in the buffer 'buf' which has size 'bufsize'. If
  56. * the number of bytes in the symbolic link is less than bufsize, the
  57. * contents of the remainder of 'buf' are unspecified. If the buf argument
  58. * is not large enough to contain the link content, the first bufsize bytes
  59. * will be placed in buf. If the value of bufsize is greater than
  60. * {SSIZE_MAX}, the result is implementation-defined. *
  61. *
  62. * Input Parameters:
  63. * path - The full path to the symbolic link
  64. * buf - The user-provided buffer in which to return the path to the
  65. * link target.
  66. * bufixe - The size of 'buf'
  67. *
  68. * Returned Value:
  69. * Upon successful completion, readlink() will return the count of bytes
  70. * placed in the buffer. Otherwise, it will return a value of -1, leave the
  71. * buffer unchanged, and set errno to indicate the error.
  72. *
  73. ****************************************************************************/
  74. ssize_t readlink(FAR const char *path, FAR char *buf, size_t bufsize)
  75. {
  76. struct inode_search_s desc;
  77. FAR struct inode *node;
  78. int errcode;
  79. int ret;
  80. DEBUGASSERT(path != NULL && buf != NULL && bufsize > 0);
  81. /* Find the inode that includes this path (without derefencing the final)
  82. * symbolic link node.
  83. */
  84. SETUP_SEARCH(&desc, path, true);
  85. ret = inode_find(&desc);
  86. if (ret < 0)
  87. {
  88. errcode = -ret;
  89. goto errout_with_search;
  90. }
  91. /* Get the search results */
  92. node = desc.node;
  93. DEBUGASSERT(node != NULL);
  94. /* An inode was found that includes this path and possibly refers to a
  95. * symbolic link.
  96. *
  97. * Check if the inode is a valid symbolic link.
  98. */
  99. if (!INODE_IS_SOFTLINK(node))
  100. {
  101. errcode = EINVAL;
  102. goto errout_with_inode;
  103. }
  104. /* Copy the link target pathto the user-provided buffer. */
  105. *buf = '\0';
  106. (void)strncpy(buf, node->u.i_link, bufsize);
  107. /* Release our reference on the inode and return the length */
  108. inode_release(node);
  109. RELEASE_SEARCH(&desc);
  110. return strlen(buf);
  111. errout_with_inode:
  112. inode_release(node);
  113. errout_with_search:
  114. RELEASE_SEARCH(&desc);
  115. set_errno(errcode);
  116. return ERROR;
  117. }
  118. #endif /* CONFIG_PSEUDOFS_SOFTLINKS */