fs_foreachinode.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /****************************************************************************
  2. * fs/inode/fs_foreachinode.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <errno.h>
  28. #include <nuttx/kmalloc.h>
  29. #include <nuttx/fs/fs.h>
  30. #include "inode/inode.h"
  31. /****************************************************************************
  32. * Pre-processor Definitions
  33. ****************************************************************************/
  34. /* Is it better to allocate the struct inode_path_s from the heap? or
  35. * from the stack? This decision depends on how often this is down and
  36. * how much stack space you can afford.
  37. */
  38. #define ENUM_INODE_ALLOC 1
  39. /****************************************************************************
  40. * Private Types
  41. ****************************************************************************/
  42. /* This structure manages the full path to the inode. */
  43. struct inode_path_s
  44. {
  45. foreach_inode_t handler;
  46. FAR void *arg;
  47. char path[CONFIG_PATH_MAX];
  48. };
  49. /****************************************************************************
  50. * Private Functions
  51. ****************************************************************************/
  52. /****************************************************************************
  53. * Name: foreach_inodelevel
  54. *
  55. * Description:
  56. * This is the recursive 'heart' of foreach_inode. It will visit each
  57. * inode at this level in the hierarchy and recurse handle each inode
  58. * at the next level down.
  59. *
  60. * Assumptions:
  61. * The caller holds the inode semaphore.
  62. *
  63. ****************************************************************************/
  64. static int foreach_inodelevel(FAR struct inode *node,
  65. FAR struct inode_path_s *info)
  66. {
  67. int ret = OK;
  68. /* Visit each node at this level */
  69. for (; node; node = node->i_peer)
  70. {
  71. /* Give the next inode to the callback */
  72. ret = info->handler(node, info->path, info->arg);
  73. /* Break out of the loop early if the handler returns a non-zero
  74. * value.
  75. */
  76. if (ret != 0)
  77. {
  78. break;
  79. }
  80. /* If there is a level 'beneath' this one, then recurse to visit all
  81. * of the inodes at that level.
  82. */
  83. if (node->i_child)
  84. {
  85. /* Construct the path to the next level */
  86. int pathlen = strlen(info->path);
  87. int namlen = strlen(node->i_name) + 1;
  88. /* Make sure that this would not exceed the maximum path length */
  89. if (pathlen + namlen > PATH_MAX)
  90. {
  91. ret = -ENAMETOOLONG;
  92. break;
  93. }
  94. /* Append the path segment to this inode and recurse */
  95. sprintf(&info->path[pathlen], "/%s", node->i_name);
  96. ret = foreach_inodelevel(node->i_child, info);
  97. /* Truncate the path name back to the correct length */
  98. info->path[pathlen] = '\0';
  99. /* Return early if the handler at the lower level returned a non-
  100. * zero value
  101. */
  102. if (ret != 0)
  103. {
  104. break;
  105. }
  106. }
  107. }
  108. /* Return the result of the traversal. */
  109. return ret;
  110. }
  111. /****************************************************************************
  112. * Public Functions
  113. ****************************************************************************/
  114. /****************************************************************************
  115. * Name: foreach_inode
  116. *
  117. * Description:
  118. * Visit each inode in the pseudo-file system. The traversal is terminated
  119. * when the callback 'handler' returns a non-zero value, or when all of
  120. * the inodes have been visited.
  121. *
  122. * NOTE 1: Use with caution... The pseudo-file system is locked throughout
  123. * the traversal.
  124. * NOTE 2: The search algorithm is recursive and could, in principle, use
  125. * an indeterminant amount of stack space. This will not usually be a
  126. * real work issue.
  127. *
  128. ****************************************************************************/
  129. int foreach_inode(foreach_inode_t handler, FAR void *arg)
  130. {
  131. #ifdef ENUM_INODE_ALLOC
  132. FAR struct inode_path_s *info;
  133. int ret;
  134. /* Allocate the mountpoint info structure */
  135. info = (FAR struct inode_path_s *)kmm_malloc(sizeof(struct inode_path_s));
  136. if (!info)
  137. {
  138. return -ENOMEM;
  139. }
  140. /* Initialize the info structure */
  141. info->handler = handler;
  142. info->arg = arg;
  143. info->path[0] = '\0';
  144. /* Start the recursion at the root inode */
  145. ret = inode_semtake();
  146. if (ret >= 0)
  147. {
  148. ret = foreach_inodelevel(g_root_inode->i_child, info);
  149. inode_semgive();
  150. }
  151. /* Free the info structure and return the result */
  152. kmm_free(info);
  153. return ret;
  154. #else
  155. struct inode_path_s info;
  156. int ret;
  157. /* Initialize the info structure */
  158. info.handler = handler;
  159. info.arg = arg;
  160. info.path[0] = '\0';
  161. /* Start the recursion at the root inode */
  162. ret = inode_semtake();
  163. if (ret >= 0)
  164. {
  165. ret = foreach_inodelevel(g_root_inode->i_child, &info);
  166. inode_semgive();
  167. }
  168. return ret;
  169. #endif
  170. }