fs_rename.c 14 KB

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