nxffs_dirent.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /****************************************************************************
  2. * fs/nxffs/nxffs_dirent.c
  3. *
  4. * Copyright (C) 2011, 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 <dirent.h>
  43. #include <assert.h>
  44. #include <errno.h>
  45. #include <debug.h>
  46. #include <nuttx/semaphore.h>
  47. #include <nuttx/fs/fs.h>
  48. #include <nuttx/mtd/mtd.h>
  49. #include <nuttx/fs/dirent.h>
  50. #include "nxffs.h"
  51. /****************************************************************************
  52. * Public Functions
  53. ****************************************************************************/
  54. /****************************************************************************
  55. * Name: nxffs_opendir
  56. *
  57. * Description:
  58. * Open a directory for read access
  59. *
  60. ****************************************************************************/
  61. int nxffs_opendir(FAR struct inode *mountpt, FAR const char *relpath,
  62. FAR struct fs_dirent_s *dir)
  63. {
  64. struct nxffs_volume_s *volume;
  65. int ret;
  66. finfo("relpath: \"%s\"\n", relpath ? relpath : "NULL");
  67. /* Sanity checks */
  68. DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
  69. /* Recover the file system state from the NuttX inode instance */
  70. volume = mountpt->i_private;
  71. ret = nxsem_wait(&volume->exclsem);
  72. if (ret < 0)
  73. {
  74. goto errout;
  75. }
  76. /* The requested directory must be the volume-relative "root" directory */
  77. if (relpath && relpath[0] != '\0')
  78. {
  79. ret = -ENOENT;
  80. goto errout_with_semaphore;
  81. }
  82. /* Set the offset to the offset to the first valid inode */
  83. dir->u.nxffs.nx_offset = volume->inoffset;
  84. ret = OK;
  85. errout_with_semaphore:
  86. nxsem_post(&volume->exclsem);
  87. errout:
  88. return ret;
  89. }
  90. /****************************************************************************
  91. * Name: nxffs_readdir
  92. *
  93. * Description: Read the next directory entry
  94. *
  95. ****************************************************************************/
  96. int nxffs_readdir(FAR struct inode *mountpt, FAR struct fs_dirent_s *dir)
  97. {
  98. FAR struct nxffs_volume_s *volume;
  99. FAR struct nxffs_entry_s entry;
  100. off_t offset;
  101. int ret;
  102. /* Sanity checks */
  103. DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
  104. /* Recover the file system state from the NuttX inode instance */
  105. volume = mountpt->i_private;
  106. ret = nxsem_wait(&volume->exclsem);
  107. if (ret < 0)
  108. {
  109. goto errout;
  110. }
  111. /* Read the next inode header from the offset */
  112. offset = dir->u.nxffs.nx_offset;
  113. ret = nxffs_nextentry(volume, offset, &entry);
  114. /* If the read was successful, then handle the reported inode. Note
  115. * that when the last inode has been reported, the value -ENOENT will
  116. * be returned.. which is correct for the readdir() method.
  117. */
  118. if (ret == OK)
  119. {
  120. /* Return the filename and file type */
  121. finfo("Offset %d: \"%s\"\n", entry.hoffset, entry.name);
  122. dir->fd_dir.d_type = DTYPE_FILE;
  123. strncpy(dir->fd_dir.d_name, entry.name, NAME_MAX+1);
  124. /* Discard this entry and set the next offset. */
  125. dir->u.nxffs.nx_offset = nxffs_inodeend(volume, &entry);
  126. nxffs_freeentry(&entry);
  127. ret = OK;
  128. }
  129. nxsem_post(&volume->exclsem);
  130. errout:
  131. return ret;
  132. }
  133. /****************************************************************************
  134. * Name: nxffs_rewindir
  135. *
  136. * Description:
  137. * Reset directory read to the first entry
  138. *
  139. ****************************************************************************/
  140. int nxffs_rewinddir(FAR struct inode *mountpt, FAR struct fs_dirent_s *dir)
  141. {
  142. FAR struct nxffs_volume_s *volume;
  143. int ret;
  144. finfo("Entry\n");
  145. /* Sanity checks */
  146. DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
  147. /* Recover the file system state from the NuttX inode instance */
  148. volume = mountpt->i_private;
  149. ret = nxsem_wait(&volume->exclsem);
  150. if (ret < 0)
  151. {
  152. goto errout;
  153. }
  154. /* Reset the offset to the FLASH offset to the first valid inode */
  155. dir->u.nxffs.nx_offset = volume->inoffset;
  156. ret = OK;
  157. nxsem_post(&volume->exclsem);
  158. errout:
  159. return ret;
  160. }