fs_rename.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. /****************************************************************************
  2. * fs/vfs/fs_rename.c
  3. *
  4. * Copyright (C) 2007-2009, 2014, 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 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/stat.h>
  40. #include <stdbool.h>
  41. #include <stdio.h>
  42. #include <string.h>
  43. #include <libgen.h>
  44. #include <errno.h>
  45. #include <nuttx/fs/fs.h>
  46. #include "inode/inode.h"
  47. /****************************************************************************
  48. * Pre-processor Definitions
  49. ****************************************************************************/
  50. #undef FS_HAVE_WRITABLE_MOUNTPOINT
  51. #if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_FS_WRITABLE) && \
  52. CONFIG_NFILE_STREAMS > 0
  53. # define FS_HAVE_WRITABLE_MOUNTPOINT 1
  54. #endif
  55. #undef FS_HAVE_PSEUDOFS_OPERATIONS
  56. #if !defined(CONFIG_DISABLE_PSEUDOFS_OPERATIONS) && CONFIG_NFILE_STREAMS > 0
  57. # define FS_HAVE_PSEUDOFS_OPERATIONS 1
  58. #endif
  59. #undef FS_HAVE_RENAME
  60. #if defined(FS_HAVE_WRITABLE_MOUNTPOINT) || defined(FS_HAVE_PSEUDOFS_OPERATIONS)
  61. # define FS_HAVE_RENAME 1
  62. #endif
  63. #ifdef FS_HAVE_RENAME
  64. /****************************************************************************
  65. * Private Functions
  66. ****************************************************************************/
  67. /****************************************************************************
  68. * Name: pseudorename
  69. *
  70. * Description:
  71. * Rename an inode in the pseudo file system
  72. *
  73. ****************************************************************************/
  74. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  75. static int pseudorename(FAR const char *oldpath, FAR struct inode *oldinode,
  76. FAR const char *newpath)
  77. {
  78. struct inode_search_s newdesc;
  79. FAR struct inode *newinode;
  80. FAR char *subdir = NULL;
  81. FAR const char *name;
  82. int ret;
  83. /* Special case the root directory. There is no root inode and there is
  84. * no name for the root. inode_find() will fail to the find the root
  85. * inode -- because there isn't one.
  86. */
  87. name = newpath;
  88. while (*name == '/')
  89. {
  90. name++;
  91. }
  92. if (*name == '\0')
  93. {
  94. FAR char *subdirname;
  95. /* In the newpath is the root directory, the target of the rename must
  96. * be a directory entry under the root.
  97. */
  98. subdirname = basename((FAR char *)oldpath);
  99. (void)asprintf(&subdir, "/%s", subdirname);
  100. if (subdir == NULL)
  101. {
  102. ret = -ENOMEM;
  103. goto errout;
  104. }
  105. newpath = subdir;
  106. }
  107. /* According to POSIX, any old inode at this path should be removed
  108. * first, provided that it is not a directory.
  109. */
  110. next_subdir:
  111. SETUP_SEARCH(&newdesc, newpath, true);
  112. ret = inode_find(&newdesc);
  113. if (ret >= 0)
  114. {
  115. /* We found it. Get the search results */
  116. newinode = newdesc.node;
  117. DEBUGASSERT(newinode != NULL);
  118. /* If the old and new inodes are the same, then this is an attempt to
  119. * move the directory entry onto itself. Let's not but say we did.
  120. */
  121. if (oldinode == newinode)
  122. {
  123. ret = OK;
  124. goto errout; /* Bad naming, this is not an error case. */
  125. }
  126. #ifndef CONFIG_DISABLE_MOUNTPOINT
  127. /* Make sure that the old path does not lie on a mounted volume. */
  128. if (INODE_IS_MOUNTPT(newinode))
  129. {
  130. inode_release(newinode);
  131. ret = -EXDEV;
  132. goto errout;
  133. }
  134. #endif
  135. /* We found it and it appears to be a "normal" inode. Is it a
  136. * directory (i.e, an operation-less inode or an inode with children)?
  137. */
  138. if (newinode->u.i_ops == NULL || newinode->i_child != NULL)
  139. {
  140. FAR char *subdirname;
  141. FAR char *tmp;
  142. /* Yes.. In this case, the target of the rename must be a
  143. * subdirectory of newinode, not the newinode itself. For
  144. * example: mv b a/ must move b to a/b.
  145. */
  146. subdirname = basename((FAR char *)oldpath);
  147. tmp = subdir;
  148. subdir = NULL;
  149. (void)asprintf(&subdir, "%s/%s", newpath, subdirname);
  150. if (tmp != NULL)
  151. {
  152. kmm_free(tmp);
  153. }
  154. if (subdir == NULL)
  155. {
  156. ret = -ENOMEM;
  157. goto errout;
  158. }
  159. newpath = subdir;
  160. /* This can be a recursive case, another inode may already exist
  161. * at oldpth/subdirname. In that case, we need to do this all
  162. * over again. A nasty goto is used because I am lazy.
  163. */
  164. goto next_subdir;
  165. }
  166. else
  167. {
  168. /* Not a directory... remove it. It may still be something
  169. * important (like a driver), but we will just have to suffer
  170. * the consequences.
  171. *
  172. * NOTE (1) that we not bother to check the error. If we
  173. * failed to remove the inode for some reason, then
  174. * inode_reserve() will complain below, and (2) the inode
  175. * won't really be removed until we call inode_release();
  176. */
  177. (void)inode_remove(newpath);
  178. }
  179. inode_release(newinode);
  180. }
  181. /* Create a new, empty inode at the destination location.
  182. * NOTE that the new inode will be created with a reference count
  183. * of zero.
  184. */
  185. inode_semtake();
  186. ret = inode_reserve(newpath, &newinode);
  187. if (ret < 0)
  188. {
  189. /* It is an error if a node at newpath already exists in the tree
  190. * OR if we fail to allocate memory for the new inode (and possibly
  191. * any new intermediate path segments).
  192. */
  193. ret = -EEXIST;
  194. goto errout_with_sem;
  195. }
  196. /* Copy the inode state from the old inode to the newly allocated inode */
  197. newinode->i_child = oldinode->i_child; /* Link to lower level inode */
  198. newinode->i_flags = oldinode->i_flags; /* Flags for inode */
  199. newinode->u.i_ops = oldinode->u.i_ops; /* Inode operations */
  200. #ifdef CONFIG_FILE_MODE
  201. newinode->i_mode = oldinode->i_mode; /* Access mode flags */
  202. #endif
  203. newinode->i_private = oldinode->i_private; /* Per inode driver private data */
  204. #ifdef CONFIG_PSEUDOFS_SOFTLINKS
  205. /* Prevent the link target string from being deallocated. The pointer to
  206. * the allocated link target path was copied above (under the guise of
  207. * u.i_ops). Now we must nullify the u.i_link pointer so that it is not
  208. * deallocated when inode_free() is (eventually called.
  209. */
  210. oldinode->u.i_link = NULL;
  211. #endif
  212. /* We now have two copies of the inode. One with a reference count of
  213. * zero (the new one), and one that may have multiple references
  214. * including one by this logic (the old one)
  215. *
  216. * Remove the old inode. Because we hold a reference count on the
  217. * inode, it will not be deleted now. It will be deleted when all of
  218. * the references to to the inode have been released (perhaps when
  219. * inode_release() is called in remove()). inode_remove() should return
  220. * -EBUSY to indicate that the inode was not deleted now.
  221. */
  222. ret = inode_remove(oldpath);
  223. if (ret < 0 && ret != -EBUSY)
  224. {
  225. /* Remove the new node we just recreated */
  226. (void)inode_remove(newpath);
  227. goto errout_with_sem;
  228. }
  229. /* Remove all of the children from the unlinked inode */
  230. oldinode->i_child = NULL;
  231. ret = OK;
  232. errout_with_sem:
  233. inode_semgive();
  234. errout:
  235. if (subdir != NULL)
  236. {
  237. kmm_free(subdir);
  238. }
  239. return ret;
  240. }
  241. #endif /* CONFIG_DISABLE_PSEUDOFS_OPERATIONS */
  242. /****************************************************************************
  243. * Name: mountptrename
  244. *
  245. * Description:
  246. * Rename a file residing on a mounted volume.
  247. *
  248. ****************************************************************************/
  249. #ifndef CONFIG_DISABLE_MOUNTPOINT
  250. static int mountptrename(FAR const char *oldpath, FAR struct inode *oldinode,
  251. FAR const char *oldrelpath, FAR const char *newpath)
  252. {
  253. struct inode_search_s newdesc;
  254. FAR struct inode *newinode;
  255. FAR const char *newrelpath;
  256. FAR char *subdir = NULL;
  257. int ret;
  258. DEBUGASSERT(oldinode->u.i_mops);
  259. /* If the file system does not support the rename() method, then bail now.
  260. * As of this writing, only NXFFS does not support the rename method. A
  261. * good fallback might be to copy the oldrelpath to the correct location,
  262. * then unlink it.
  263. */
  264. if (oldinode->u.i_mops->rename == NULL)
  265. {
  266. return -ENOSYS;
  267. }
  268. /* Get an inode for the new relpath -- it should lie on the same
  269. * mountpoint
  270. */
  271. SETUP_SEARCH(&newdesc, newpath, true);
  272. ret = inode_find(&newdesc);
  273. if (ret < 0)
  274. {
  275. /* There is no mountpoint that includes in this path */
  276. goto errout_with_newsearch;
  277. }
  278. /* Get the search results */
  279. newinode = newdesc.node;
  280. newrelpath = newdesc.relpath;
  281. DEBUGASSERT(newinode != NULL && newrelpath != NULL);
  282. /* Verify that the two paths lie on the same mountpoint inode */
  283. if (oldinode != newinode)
  284. {
  285. ret = -EXDEV;
  286. goto errout_with_newinode;
  287. }
  288. /* Does a directory entry already exist at the 'rewrelpath'? And is it
  289. * not the same directory entry that we are moving?
  290. *
  291. * If the directory entry at the newrelpath is a regular file, then that
  292. * file should be removed first.
  293. *
  294. * If the directory entry at the target is a directory, then the source
  295. * file should be moved "under" the directory, i.e., if newrelpath is a
  296. * directory, then rename(b,a) should use move the olrelpath should be
  297. * moved as if rename(b,a/basename(b)) had been called.
  298. */
  299. if (oldinode->u.i_mops->stat != NULL &&
  300. strcmp(oldrelpath, newrelpath) != 0)
  301. {
  302. struct stat buf;
  303. next_subdir:
  304. /* Something exists for this directory entry. Do nothing in the
  305. * degenerate case where a directory or file is being moved to
  306. * itself.
  307. */
  308. if (strcmp(oldrelpath, newrelpath) != 0)
  309. {
  310. ret = oldinode->u.i_mops->stat(oldinode, newrelpath, &buf);
  311. if (ret >= 0)
  312. {
  313. /* Is the directory entry a directory? */
  314. if (S_ISDIR(buf.st_mode))
  315. {
  316. FAR char *subdirname;
  317. /* Yes.. In this case, the target of the rename must be a
  318. * subdirectory of newinode, not the newinode itself. For
  319. * example: mv b a/ must move b to a/b.
  320. */
  321. subdirname = basename((FAR char *)oldrelpath);
  322. /* Special case the root directory */
  323. if (*newrelpath == '\0')
  324. {
  325. if (subdir != NULL)
  326. {
  327. kmm_free(subdir);
  328. subdir = NULL;
  329. }
  330. newrelpath = subdirname;
  331. }
  332. else
  333. {
  334. FAR char *tmp = subdir;
  335. subdir = NULL;
  336. (void)asprintf(&subdir, "%s/%s", newrelpath, subdirname);
  337. if (tmp != NULL)
  338. {
  339. kmm_free(tmp);
  340. }
  341. if (subdir == NULL)
  342. {
  343. ret = -ENOMEM;
  344. goto errout_with_newinode;
  345. }
  346. newrelpath = subdir;
  347. }
  348. /* This can be a recursive, another directory may already
  349. * exist at the newrelpath. In that case, we need to
  350. * do this all over again. A nasty goto is used because
  351. * I am lazy.
  352. */
  353. goto next_subdir;
  354. }
  355. else if (oldinode->u.i_mops->unlink)
  356. {
  357. /* No.. newrelpath must refer to a regular file. Attempt
  358. * to remove the file before doing the rename.
  359. *
  360. * NOTE that errors are not handled here. If we failed to
  361. * remove the file, then the file system 'rename' method
  362. * should check that.
  363. */
  364. (void)oldinode->u.i_mops->unlink(oldinode, newrelpath);
  365. }
  366. }
  367. }
  368. }
  369. /* Just declare success of the oldrepath and the newrelpath point to
  370. * the same directory entry. That directory entry should have been
  371. * stat'ed above to assure that it exists.
  372. */
  373. ret = OK;
  374. if (strcmp(oldrelpath, newrelpath) != 0)
  375. {
  376. /* Perform the rename operation using the relative paths at the common
  377. * mountpoint.
  378. */
  379. ret = oldinode->u.i_mops->rename(oldinode, oldrelpath, newrelpath);
  380. }
  381. errout_with_newinode:
  382. inode_release(newinode);
  383. errout_with_newsearch:
  384. RELEASE_SEARCH(&newdesc);
  385. if (subdir != NULL)
  386. {
  387. kmm_free(subdir);
  388. }
  389. return ret;
  390. }
  391. #endif /* CONFIG_DISABLE_MOUNTPOINT */
  392. /****************************************************************************
  393. * Public Functions
  394. ****************************************************************************/
  395. /****************************************************************************
  396. * Name: rename
  397. *
  398. * Description:
  399. * Rename a file or directory.
  400. *
  401. ****************************************************************************/
  402. int rename(FAR const char *oldpath, FAR const char *newpath)
  403. {
  404. struct inode_search_s olddesc;
  405. FAR struct inode *oldinode;
  406. int ret;
  407. /* Ignore paths that are interpreted as the root directory which has no name
  408. * and cannot be moved
  409. */
  410. if (!oldpath || *oldpath == '\0' || oldpath[0] != '/' ||
  411. !newpath || *newpath == '\0' || newpath[0] != '/')
  412. {
  413. ret = -EINVAL;
  414. goto errout;
  415. }
  416. /* Get an inode that includes the oldpath */
  417. SETUP_SEARCH(&olddesc, oldpath, true);
  418. ret = inode_find(&olddesc);
  419. if (ret < 0)
  420. {
  421. /* There is no inode that includes in this path */
  422. goto errout_with_oldsearch;
  423. }
  424. /* Get the search results */
  425. oldinode = olddesc.node;
  426. DEBUGASSERT(oldinode != NULL);
  427. #ifndef CONFIG_DISABLE_MOUNTPOINT
  428. /* Verify that the old inode is a valid mountpoint. */
  429. if (INODE_IS_MOUNTPT(oldinode))
  430. {
  431. ret = mountptrename(oldpath, oldinode, olddesc.relpath, newpath);
  432. }
  433. else
  434. #endif /* CONFIG_DISABLE_MOUNTPOINT */
  435. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  436. {
  437. ret = pseudorename(oldpath, oldinode, newpath);
  438. }
  439. #else
  440. {
  441. ret = -ENXIO;
  442. }
  443. #endif
  444. inode_release(oldinode);
  445. errout_with_oldsearch:
  446. RELEASE_SEARCH(&olddesc);
  447. errout:
  448. if (ret < 0)
  449. {
  450. set_errno(-ret);
  451. return ERROR;
  452. }
  453. return OK;
  454. }
  455. #endif /* FS_HAVE_RENAME */