nxffs_unlink.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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/semaphore.h>
  46. #include <nuttx/fs/fs.h>
  47. #include <nuttx/mtd/mtd.h>
  48. #include "nxffs.h"
  49. /****************************************************************************
  50. * Public Functions
  51. ****************************************************************************/
  52. /****************************************************************************
  53. * Name: nxffs_rminode
  54. *
  55. * Description:
  56. * Remove an inode from FLASH. This is the internal implementation of
  57. * the file system unlinke operation.
  58. *
  59. * Input Parameters:
  60. * volume - Describes the NXFFS volume.
  61. * name - the name of the inode to be deleted.
  62. *
  63. * Returned Value:
  64. * Zero is returned if the inode is successfully deleted. Otherwise, a
  65. * negated errno value is returned indicating the nature of the failure.
  66. *
  67. ****************************************************************************/
  68. int nxffs_rminode(FAR struct nxffs_volume_s *volume, FAR const char *name)
  69. {
  70. FAR struct nxffs_ofile_s *ofile;
  71. FAR struct nxffs_inode_s *inode;
  72. struct nxffs_entry_s entry;
  73. int ret;
  74. /* Check if the file is open */
  75. ofile = nxffs_findofile(volume, name);
  76. if (ofile)
  77. {
  78. /* We can't remove the inode if it is open */
  79. ferr("ERROR: Inode '%s' is open\n", name);
  80. ret = -EBUSY;
  81. goto errout;
  82. }
  83. /* Find the NXFFS inode */
  84. ret = nxffs_findinode(volume, name, &entry);
  85. if (ret < 0)
  86. {
  87. ferr("ERROR: Inode '%s' not found\n", name);
  88. goto errout;
  89. }
  90. /* Set the position to the FLASH offset of the file header (nxffs_findinode
  91. * should have left the block in the cache).
  92. */
  93. nxffs_ioseek(volume, entry.hoffset);
  94. /* Make sure that the block is in the cache */
  95. ret = nxffs_rdcache(volume, volume->ioblock);
  96. if (ret < 0)
  97. {
  98. ferr("ERROR: Failed to read block %d into cache: %d\n",
  99. volume->ioblock, ret);
  100. goto errout_with_entry;
  101. }
  102. /* Change the file status... it is no longer valid */
  103. inode = (FAR struct nxffs_inode_s *)&volume->cache[volume->iooffset];
  104. inode->state = INODE_STATE_DELETED;
  105. /* Then write the cached block back to FLASH */
  106. ret = nxffs_wrcache(volume);
  107. if (ret < 0)
  108. {
  109. ferr("ERROR: Failed to write block %d: %d\n",
  110. volume->ioblock, ret);
  111. }
  112. errout_with_entry:
  113. nxffs_freeentry(&entry);
  114. errout:
  115. return ret;
  116. }
  117. /****************************************************************************
  118. * Name: nxffs_unlink
  119. *
  120. * Description: Remove a file
  121. *
  122. ****************************************************************************/
  123. int nxffs_unlink(FAR struct inode *mountpt, FAR const char *relpath)
  124. {
  125. FAR struct nxffs_volume_s *volume;
  126. int ret;
  127. finfo("Entry\n");
  128. /* Sanity checks */
  129. DEBUGASSERT(mountpt && mountpt->i_private);
  130. /* Get the mountpoint private data from the NuttX inode structure */
  131. volume = mountpt->i_private;
  132. ret = nxsem_wait(&volume->exclsem);
  133. if (ret != OK)
  134. {
  135. goto errout;
  136. }
  137. /* Then remove the NXFFS inode */
  138. ret = nxffs_rminode(volume, relpath);
  139. nxsem_post(&volume->exclsem);
  140. errout:
  141. return ret;
  142. }