fs_inodesearch.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /****************************************************************************
  2. * fs/inode/fs_inodesearch.c
  3. *
  4. * Copyright (C) 2007-2009, 2011-2012, 2016-2017 Gregory Nutt.
  5. * All rights reserved.
  6. * Author: Gregory Nutt <gnutt@nuttx.org>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. * 3. Neither the name NuttX nor the names of its contributors may be
  19. * used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  29. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  30. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  32. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. ****************************************************************************/
  36. /****************************************************************************
  37. * Included Files
  38. ****************************************************************************/
  39. #include <nuttx/config.h>
  40. #include <stdio.h>
  41. #include <string.h>
  42. #include <stdlib.h>
  43. #include <limits.h>
  44. #include <assert.h>
  45. #include <errno.h>
  46. #include <nuttx/fs/fs.h>
  47. #include "inode/inode.h"
  48. /****************************************************************************
  49. * Private Function Prototypes
  50. ****************************************************************************/
  51. static int _inode_compare(FAR const char *fname, FAR struct inode *node);
  52. #ifdef CONFIG_PSEUDOFS_SOFTLINKS
  53. static int _inode_linktarget(FAR struct inode *node,
  54. FAR struct inode_search_s *desc);
  55. #endif
  56. static int _inode_search(FAR struct inode_search_s *desc);
  57. static FAR const char *_inode_getcwd(void);
  58. /****************************************************************************
  59. * Public Data
  60. ****************************************************************************/
  61. FAR struct inode *g_root_inode = NULL;
  62. /****************************************************************************
  63. * Private Functions
  64. ****************************************************************************/
  65. /****************************************************************************
  66. * Name: _inode_compare
  67. *
  68. * Description:
  69. * Compare two inode names
  70. *
  71. ****************************************************************************/
  72. static int _inode_compare(FAR const char *fname, FAR struct inode *node)
  73. {
  74. char *nname = node->i_name;
  75. if (!nname)
  76. {
  77. return 1;
  78. }
  79. if (!fname)
  80. {
  81. return -1;
  82. }
  83. for (; ; )
  84. {
  85. /* At the end of the node name? */
  86. if (!*nname)
  87. {
  88. /* Yes.. also at the end of find name? */
  89. if (!*fname || *fname == '/')
  90. {
  91. /* Yes.. return match */
  92. return 0;
  93. }
  94. else
  95. {
  96. /* No... return find name > node name */
  97. return 1;
  98. }
  99. }
  100. /* At end of the find name? */
  101. else if (!*fname || *fname == '/')
  102. {
  103. /* Yes... return find name < node name */
  104. return -1;
  105. }
  106. /* Check for non-matching characters */
  107. else if (*fname > *nname)
  108. {
  109. return 1;
  110. }
  111. else if (*fname < *nname)
  112. {
  113. return -1;
  114. }
  115. /* Not at the end of either string and all of the
  116. * characters still match. keep looking.
  117. */
  118. else
  119. {
  120. fname++;
  121. nname++;
  122. }
  123. }
  124. }
  125. /****************************************************************************
  126. * Name: _inode_linktarget
  127. *
  128. * Description:
  129. * If the inode is a soft link, then (1) recursively look-up the inode
  130. * referenced by the soft link, and (2) return the inode referenced by
  131. * the soft link.
  132. *
  133. * Assumptions:
  134. * The caller holds the g_inode_sem semaphore
  135. *
  136. ****************************************************************************/
  137. #ifdef CONFIG_PSEUDOFS_SOFTLINKS
  138. static int _inode_linktarget(FAR struct inode *node,
  139. FAR struct inode_search_s *desc)
  140. {
  141. unsigned int count = 0;
  142. bool save;
  143. int ret = -ENOENT;
  144. DEBUGASSERT(desc != NULL && node != NULL);
  145. /* An infinite loop is avoided only by the loop count. */
  146. save = desc->nofollow;
  147. while (INODE_IS_SOFTLINK(node))
  148. {
  149. FAR const char *link = (FAR const char *)node->u.i_link;
  150. /* Reset and reinitialize the search descriptor. */
  151. RELEASE_SEARCH(desc);
  152. SETUP_SEARCH(desc, link, true);
  153. /* Look up inode associated with the target of the symbolic link */
  154. ret = _inode_search(desc);
  155. if (ret < 0)
  156. {
  157. break;
  158. }
  159. /* Limit the number of symbolic links that we pass through */
  160. if (++count > SYMLOOP_MAX)
  161. {
  162. ret = -ELOOP;
  163. break;
  164. }
  165. /* Set up for the next time through the loop */
  166. node = desc->node;
  167. DEBUGASSERT(node != NULL);
  168. }
  169. desc->nofollow = save;
  170. return ret;
  171. }
  172. #endif
  173. /****************************************************************************
  174. * Name: _inode_search
  175. *
  176. * Description:
  177. * Find the inode associated with 'path' returning the inode references
  178. * and references to its companion nodes. This is the internal, common
  179. * implementation of inode_search().
  180. *
  181. * If a mountpoint is encountered in the search prior to encountering the
  182. * terminal node, the search will terminate at the mountpoint inode. That
  183. * inode and the relative path from the mountpoint, 'relpath' will be
  184. * returned.
  185. *
  186. * If a soft link is encountered that is not the terminal node in the path,
  187. * that link WILL be deferenced unconditionally.
  188. *
  189. * Assumptions:
  190. * The caller holds the g_inode_sem semaphore
  191. *
  192. ****************************************************************************/
  193. static int _inode_search(FAR struct inode_search_s *desc)
  194. {
  195. FAR const char *name;
  196. FAR struct inode *node = g_root_inode;
  197. FAR struct inode *left = NULL;
  198. FAR struct inode *above = NULL;
  199. FAR const char *relpath = NULL;
  200. int ret = -ENOENT;
  201. /* Get the search path, skipping over the leading '/'. The leading '/' is
  202. * mandatory because only absolute paths are expected in this context.
  203. */
  204. DEBUGASSERT(desc != NULL && desc->path != NULL);
  205. name = desc->path;
  206. if (*name != '/')
  207. {
  208. return -EINVAL;
  209. }
  210. /* Traverse the pseudo file system node tree until either (1) all nodes
  211. * have been examined without finding the matching node, or (2) the
  212. * matching node is found.
  213. */
  214. while (node != NULL)
  215. {
  216. int result = _inode_compare(name, node);
  217. /* Case 1: The name is less than the name of the node.
  218. * Since the names are ordered, these means that there
  219. * is no peer node with this name and that there can be
  220. * no match in the filesystem.
  221. */
  222. if (result < 0)
  223. {
  224. node = NULL;
  225. break;
  226. }
  227. /* Case 2: the name is greater than the name of the node.
  228. * In this case, the name may still be in the list to the
  229. * "right"
  230. */
  231. else if (result > 0)
  232. {
  233. /* Continue looking to the "right" of this inode. */
  234. left = node;
  235. node = node->i_peer;
  236. }
  237. /* The names match */
  238. else
  239. {
  240. /* Now there are three remaining possibilities:
  241. * (1) This is the node that we are looking for.
  242. * (2) The node we are looking for is "below" this one.
  243. * (3) This node is a mountpoint and will absorb all requests
  244. * below this one
  245. */
  246. name = inode_nextname(name);
  247. if (*name == '\0' || INODE_IS_MOUNTPT(node))
  248. {
  249. /* Either (1) we are at the end of the path, so this must be
  250. * the node we are looking for or else (2) this node is a
  251. * mountpoint and will handle the remaining part of the
  252. * pathname
  253. */
  254. relpath = name;
  255. ret = OK;
  256. break;
  257. }
  258. else
  259. {
  260. /* More nodes to be examined in the path "below" this one. */
  261. #ifdef CONFIG_PSEUDOFS_SOFTLINKS
  262. /* Was the node a soft link? If so, then we need need to
  263. * continue below the target of the link, not the link itself.
  264. */
  265. if (INODE_IS_SOFTLINK(node))
  266. {
  267. int status;
  268. /* If this intermediate inode in the is a soft link, then
  269. * (1) recursively look-up the inode referenced by the
  270. * soft link, and (2) continue searching with that inode
  271. * instead.
  272. */
  273. status = _inode_linktarget(node, desc);
  274. if (status < 0)
  275. {
  276. /* Probably means that the target of the symbolic link
  277. * does not exist.
  278. */
  279. ret = status;
  280. break;
  281. }
  282. else
  283. {
  284. FAR struct inode *newnode = desc->node;
  285. if (newnode != node)
  286. {
  287. /* The node was a valid symbolic link and we have
  288. * jumped to a different, spot in the pseudo file
  289. * system tree.
  290. */
  291. /* Check if this took us to a mountpoint. */
  292. if (INODE_IS_MOUNTPT(newnode))
  293. {
  294. /* Return the mountpoint information.
  295. * NOTE that the last path to the link target
  296. * was already set by _inode_linktarget().
  297. */
  298. node = newnode;
  299. above = desc->parent;
  300. left = desc->peer;
  301. ret = OK;
  302. if (*desc->relpath != '\0')
  303. {
  304. char *buffer = NULL;
  305. asprintf(&buffer,
  306. "%s/%s", desc->relpath, name);
  307. if (buffer != NULL)
  308. {
  309. kmm_free(desc->buffer);
  310. desc->buffer = buffer;
  311. relpath = buffer;
  312. }
  313. else
  314. {
  315. ret = -ENOMEM;
  316. }
  317. }
  318. else
  319. {
  320. relpath = name;
  321. }
  322. break;
  323. }
  324. /* Continue from this new inode. */
  325. node = newnode;
  326. }
  327. }
  328. }
  329. #endif
  330. /* Keep looking at the next level "down" */
  331. above = node;
  332. left = NULL;
  333. node = node->i_child;
  334. }
  335. }
  336. }
  337. /* The node may or may not be null as per one of the following four cases:
  338. *
  339. * With node = NULL
  340. *
  341. * (1) We went left past the final peer: The new node name is larger
  342. * than any existing node name at that level.
  343. * (2) We broke out in the middle of the list of peers because the name
  344. * was not found in the ordered list.
  345. * (3) We went down past the final parent: The new node name is
  346. * "deeper" than anything that we currently have in the tree.
  347. *
  348. * With node != NULL
  349. *
  350. * (4) When the node matching the full path is found
  351. */
  352. desc->path = name;
  353. desc->node = node;
  354. desc->peer = left;
  355. desc->parent = above;
  356. desc->relpath = relpath;
  357. return ret;
  358. }
  359. /****************************************************************************
  360. * Name: _inode_getcwd
  361. *
  362. * Description:
  363. * Return the current working directory
  364. *
  365. ****************************************************************************/
  366. static FAR const char *_inode_getcwd(void)
  367. {
  368. FAR const char *pwd = "";
  369. #ifndef CONFIG_DISABLE_ENVIRON
  370. pwd = getenv("PWD");
  371. if (pwd == NULL)
  372. {
  373. pwd = CONFIG_LIB_HOMEDIR;
  374. }
  375. #endif
  376. return pwd;
  377. }
  378. /****************************************************************************
  379. * Public Functions
  380. ****************************************************************************/
  381. /****************************************************************************
  382. * Name: inode_search
  383. *
  384. * Description:
  385. * Find the inode associated with 'path' returning the inode references
  386. * and references to its companion nodes.
  387. *
  388. * If a mountpoint is encountered in the search prior to encountering the
  389. * terminal node, the search will terminate at the mountpoint inode. That
  390. * inode and the relative path from the mountpoint, 'relpath' will be
  391. * returned.
  392. *
  393. * inode_search will follow soft links in path leading up to the terminal
  394. * node. Whether or no inode_search() will deference that terminal node
  395. * depends on the 'nofollow' input.
  396. *
  397. * If a soft link is encountered that is not the terminal node in the path,
  398. * that link WILL be deferenced unconditionally.
  399. *
  400. * Assumptions:
  401. * The caller holds the g_inode_sem semaphore
  402. *
  403. ****************************************************************************/
  404. int inode_search(FAR struct inode_search_s *desc)
  405. {
  406. int ret;
  407. /* Perform the common _inode_search() logic. This does everything except
  408. * operations special operations that must be performed on the terminal
  409. * node if node is a symbolic link.
  410. */
  411. DEBUGASSERT(desc != NULL && desc->path != NULL);
  412. /* Convert the relative path to the absolute path */
  413. if (*desc->path != '/')
  414. {
  415. asprintf(&desc->buffer, "%s/%s", _inode_getcwd(), desc->path);
  416. if (desc->buffer == NULL)
  417. {
  418. return -ENOMEM;
  419. }
  420. desc->path = desc->buffer;
  421. }
  422. ret = _inode_search(desc);
  423. #ifdef CONFIG_PSEUDOFS_SOFTLINKS
  424. if (ret >= 0)
  425. {
  426. FAR struct inode *node;
  427. /* Search completed successfully */
  428. node = desc->node;
  429. DEBUGASSERT(node != NULL);
  430. /* Is the terminal node a softlink? Should we follow it? */
  431. if (!desc->nofollow && INODE_IS_SOFTLINK(node))
  432. {
  433. /* The terminating inode is a valid soft link: Return the inode,
  434. * corresponding to link target. _inode_linktarget() will follow
  435. * a link (or a series of links to links) and will return the
  436. * link target of the final symbolic link in the series.
  437. */
  438. ret = _inode_linktarget(node, desc);
  439. if (ret < 0)
  440. {
  441. /* The most likely cause for failure is that the target of the
  442. * symbolic link does not exist.
  443. */
  444. return ret;
  445. }
  446. }
  447. }
  448. #endif
  449. return ret;
  450. }
  451. /****************************************************************************
  452. * Name: inode_nextname
  453. *
  454. * Description:
  455. * Given a path with node names separated by '/', return the next path
  456. * segment name.
  457. *
  458. ****************************************************************************/
  459. FAR const char *inode_nextname(FAR const char *name)
  460. {
  461. /* Search for the '/' delimiter or the NUL terminator at the end of the
  462. * path segment.
  463. */
  464. while (*name != '\0' && *name != '/')
  465. {
  466. name++;
  467. }
  468. /* If we found the '/' delimiter, then the path segment we want begins at
  469. * the next character (which might also be the NUL terminator).
  470. */
  471. while (*name == '/')
  472. {
  473. name++;
  474. }
  475. return name;
  476. }