nxffs_unlink.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /****************************************************************************
  2. * fs/nxffs/nxffs_unlink.c
  3. *
  4. * Copyright (C) 2011, 2013, 2017-2018 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * References: Linux/Documentation/filesystems/romfs.txt
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. * 3. Neither the name NuttX nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  26. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  27. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  28. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  29. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  30. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  31. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  33. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. * POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. ****************************************************************************/
  37. /****************************************************************************
  38. * Included Files
  39. ****************************************************************************/
  40. #include <nuttx/config.h>
  41. #include <string.h>
  42. #include <errno.h>
  43. #include <assert.h>
  44. #include <debug.h>
  45. #include <nuttx/fs/fs.h>
  46. #include <nuttx/mtd/mtd.h>
  47. #include "nxffs.h"
  48. /****************************************************************************
  49. * Public Functions
  50. ****************************************************************************/
  51. /****************************************************************************
  52. * Name: nxffs_rminode
  53. *
  54. * Description:
  55. * Remove an inode from FLASH. This is the internal implementation of
  56. * the file system unlinke operation.
  57. *
  58. * Input Parameters:
  59. * volume - Describes the NXFFS volume.
  60. * name - the name of the inode to be deleted.
  61. *
  62. * Returned Value:
  63. * Zero is returned if the inode is successfully deleted. Otherwise, a
  64. * negated errno value is returned indicating the nature of the failure.
  65. *
  66. ****************************************************************************/
  67. int nxffs_rminode(FAR struct nxffs_volume_s *volume, FAR const char *name)
  68. {
  69. FAR struct nxffs_ofile_s *ofile;
  70. FAR struct nxffs_inode_s *inode;
  71. struct nxffs_entry_s entry;
  72. int ret;
  73. /* Check if the file is open */
  74. ofile = nxffs_findofile(volume, name);
  75. if (ofile)
  76. {
  77. /* We can't remove the inode if it is open */
  78. ferr("ERROR: Inode '%s' is open\n", name);
  79. ret = -EBUSY;
  80. goto errout;
  81. }
  82. /* Find the NXFFS inode */
  83. ret = nxffs_findinode(volume, name, &entry);
  84. if (ret < 0)
  85. {
  86. ferr("ERROR: Inode '%s' not found\n", name);
  87. goto errout;
  88. }
  89. /* Set the position to the FLASH offset of the file header (nxffs_findinode
  90. * should have left the block in the cache).
  91. */
  92. nxffs_ioseek(volume, entry.hoffset);
  93. /* Make sure that the block is in the cache */
  94. ret = nxffs_rdcache(volume, volume->ioblock);
  95. if (ret < 0)
  96. {
  97. ferr("ERROR: Failed to read block %d into cache: %d\n",
  98. volume->ioblock, ret);
  99. goto errout_with_entry;
  100. }
  101. /* Change the file status... it is no longer valid */
  102. inode = (FAR struct nxffs_inode_s *)&volume->cache[volume->iooffset];
  103. inode->state = INODE_STATE_DELETED;
  104. /* Then write the cached block back to FLASH */
  105. ret = nxffs_wrcache(volume);
  106. if (ret < 0)
  107. {
  108. ferr("ERROR: Failed to write block %d: %d\n",
  109. volume->ioblock, ret);
  110. }
  111. errout_with_entry:
  112. nxffs_freeentry(&entry);
  113. errout:
  114. return ret;
  115. }
  116. /****************************************************************************
  117. * Name: nxffs_unlink
  118. *
  119. * Description: Remove a file
  120. *
  121. ****************************************************************************/
  122. int nxffs_unlink(FAR struct inode *mountpt, FAR const char *relpath)
  123. {
  124. FAR struct nxffs_volume_s *volume;
  125. int ret;
  126. finfo("Entry\n");
  127. /* Sanity checks */
  128. DEBUGASSERT(mountpt && mountpt->i_private);
  129. /* Get the mountpoint private data from the NuttX inode structure */
  130. volume = mountpt->i_private;
  131. ret = nxsem_wait(&volume->exclsem);
  132. if (ret != OK)
  133. {
  134. goto errout;
  135. }
  136. /* Then remove the NXFFS inode */
  137. ret = nxffs_rminode(volume, relpath);
  138. nxsem_post(&volume->exclsem);
  139. errout:
  140. return ret;
  141. }