fs_foreachmountpoint.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /****************************************************************************
  2. * fs/mount/fs_foreachmountpoint.c
  3. *
  4. * Copyright (C) 2012 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 SHALL 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/statfs.h>
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <assert.h>
  44. #include <errno.h>
  45. #include <nuttx/fs/fs.h>
  46. #include "inode/inode.h"
  47. #include "mount/mount.h"
  48. #ifndef CONFIG_DISABLE_MOUNTPOINT
  49. /****************************************************************************
  50. * Pre-processor Definitions
  51. ****************************************************************************/
  52. /****************************************************************************
  53. * Private Types
  54. ****************************************************************************/
  55. /* This structure just remembers the final consumer of the mountpoint
  56. * information (and its argument).
  57. */
  58. struct enum_mountpoint_s
  59. {
  60. foreach_mountpoint_t handler;
  61. FAR void *arg;
  62. };
  63. /****************************************************************************
  64. * Private Functions
  65. ****************************************************************************/
  66. static int mountpoint_filter(FAR struct inode *node,
  67. FAR char dirpath[PATH_MAX], FAR void *arg)
  68. {
  69. FAR struct enum_mountpoint_s *info = (FAR struct enum_mountpoint_s *)arg;
  70. struct statfs statbuf;
  71. int pathlen;
  72. int namlen;
  73. int ret = OK;
  74. DEBUGASSERT(node && info && info->handler);
  75. /* Check if the inode is a mountpoint. Mountpoints must support statfs.
  76. * If this one does not for some reason, then it will be ignored.
  77. *
  78. * The root node is a special case: It has no operations (u.i_mops == NULL)
  79. */
  80. if (INODE_IS_MOUNTPT(node) && node->u.i_mops && node->u.i_mops->statfs)
  81. {
  82. /* Yes... get the full path to the inode by concatenating the inode
  83. * name and the path to the directory containing the inode.
  84. */
  85. pathlen = strlen(dirpath);
  86. namlen = strlen(node->i_name) + 1;
  87. /* Make sure that this would not exceed the maximum path length */
  88. if (pathlen + namlen > PATH_MAX)
  89. {
  90. return -ENAMETOOLONG;
  91. }
  92. /* Append the inode name to the directory path */
  93. sprintf(&dirpath[pathlen], "/%s", node->i_name);
  94. /* Get the status of the file system */
  95. ret = node->u.i_mops->statfs(node, &statbuf);
  96. if (ret == OK)
  97. {
  98. /* And pass the full path and file system status to the handler */
  99. ret = info->handler(dirpath, &statbuf, info->arg);
  100. }
  101. /* Truncate the path name back to the correct length */
  102. dirpath[pathlen] = '\0';
  103. }
  104. return ret;
  105. }
  106. /****************************************************************************
  107. * Public Functions
  108. ****************************************************************************/
  109. /****************************************************************************
  110. * Name: foreach_mountpoint
  111. *
  112. * Description:
  113. * Visit each mountpoint in the pseudo-file system. The traversal is
  114. * terminated when the callback 'handler' returns a non-zero value, or when
  115. * all of the mountpoints have been visited.
  116. *
  117. * This is just a front end "filter" to foreach_inode() that forwards only
  118. * mountpoint inodes. It is intended to support the mount() command to
  119. * when the mount command is used to enumerate mounts.
  120. *
  121. * NOTE 1: Use with caution... The pseudo-file system is locked throughout
  122. * the traversal.
  123. * NOTE 2: The search algorithm is recursive and could, in principle, use
  124. * an indeterminant amount of stack space. This will not usually be a
  125. * real work issue.
  126. *
  127. ****************************************************************************/
  128. int foreach_mountpoint(foreach_mountpoint_t handler, FAR void *arg)
  129. {
  130. struct enum_mountpoint_s info;
  131. /* Let foreach_inode do the real work */
  132. info.handler = handler;
  133. info.arg = arg;
  134. return foreach_inode(mountpoint_filter, (FAR void *)&info);
  135. }
  136. #endif