nxffs_stat.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /****************************************************************************
  2. * fs/nxffs/nxffs_stat.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 <sys/stat.h>
  42. #include <sys/statfs.h>
  43. #include <string.h>
  44. #include <fcntl.h>
  45. #include <assert.h>
  46. #include <errno.h>
  47. #include <debug.h>
  48. #include <nuttx/semaphore.h>
  49. #include <nuttx/fs/fs.h>
  50. #include <nuttx/mtd/mtd.h>
  51. #include "nxffs.h"
  52. /****************************************************************************
  53. * Public Functions
  54. ****************************************************************************/
  55. /****************************************************************************
  56. * Name: nxffs_statfs
  57. *
  58. * Description: Return filesystem statistics
  59. *
  60. ****************************************************************************/
  61. int nxffs_statfs(FAR struct inode *mountpt, FAR struct statfs *buf)
  62. {
  63. FAR struct nxffs_volume_s *volume;
  64. int ret;
  65. finfo("Entry\n");
  66. /* Sanity checks */
  67. DEBUGASSERT(mountpt && mountpt->i_private);
  68. /* Get the mountpoint private data from the NuttX inode structure */
  69. volume = mountpt->i_private;
  70. ret = nxsem_wait(&volume->exclsem);
  71. if (ret < 0)
  72. {
  73. goto errout;
  74. }
  75. /* Fill in the statfs info
  76. *
  77. * REVISIT: Need f_bfree, f_bavail, f_files, f_ffree calculation
  78. */
  79. memset(buf, 0, sizeof(struct statfs));
  80. buf->f_type = NXFFS_MAGIC;
  81. buf->f_bsize = volume->geo.blocksize;
  82. buf->f_blocks = volume->nblocks;
  83. buf->f_namelen = volume->geo.blocksize - SIZEOF_NXFFS_BLOCK_HDR -
  84. SIZEOF_NXFFS_INODE_HDR;
  85. ret = OK;
  86. nxsem_post(&volume->exclsem);
  87. errout:
  88. return ret;
  89. }
  90. /****************************************************************************
  91. * Name: nxffs_stat
  92. *
  93. * Description: Return information about a file or directory
  94. *
  95. ****************************************************************************/
  96. int nxffs_stat(FAR struct inode *mountpt, FAR const char *relpath,
  97. FAR struct stat *buf)
  98. {
  99. FAR struct nxffs_volume_s *volume;
  100. struct nxffs_entry_s entry;
  101. int ret;
  102. finfo("Entry\n");
  103. /* Sanity checks */
  104. DEBUGASSERT(mountpt && mountpt->i_private && buf);
  105. /* Get the mountpoint private data from the NuttX inode structure */
  106. volume = mountpt->i_private;
  107. ret = nxsem_wait(&volume->exclsem);
  108. if (ret != OK)
  109. {
  110. goto errout;
  111. }
  112. /* Initialize the return stat instance */
  113. memset(buf, 0, sizeof(struct stat));
  114. buf->st_blksize = volume->geo.blocksize;
  115. /* The requested directory must be the volume-relative "root" directory */
  116. if (relpath && relpath[0] != '\0')
  117. {
  118. /* Not the top directory.. find the NXFFS inode with this name */
  119. ret = nxffs_findinode(volume, relpath, &entry);
  120. if (ret < 0)
  121. {
  122. ferr("ERROR: Inode '%s' not found: %d\n", -ret);
  123. goto errout_with_semaphore;
  124. }
  125. /* Return status information based on the directory entry */
  126. buf->st_blocks = entry.datlen /
  127. (volume->geo.blocksize - SIZEOF_NXFFS_BLOCK_HDR);
  128. buf->st_mode = S_IFREG | S_IXOTH | S_IXGRP | S_IXUSR;
  129. buf->st_size = entry.datlen;
  130. buf->st_atime = entry.utc;
  131. buf->st_mtime = entry.utc;
  132. buf->st_ctime = entry.utc;
  133. /* Free inode resources */
  134. nxffs_freeentry(&entry);
  135. }
  136. else
  137. {
  138. /* It's a read/execute-only directory name */
  139. buf->st_mode = S_IFDIR | S_IROTH | S_IRGRP | S_IRUSR | S_IXOTH |
  140. S_IXGRP | S_IXUSR;
  141. }
  142. ret = OK;
  143. errout_with_semaphore:
  144. nxsem_post(&volume->exclsem);
  145. errout:
  146. return ret;
  147. }
  148. /****************************************************************************
  149. * Name: nxffs_fstat
  150. *
  151. * Description:
  152. * Obtain information about an open file associated with the file
  153. * descriptor 'fd', and will write it to the area pointed to by 'buf'.
  154. *
  155. ****************************************************************************/
  156. int nxffs_fstat(FAR const struct file *filep, FAR struct stat *buf)
  157. {
  158. FAR struct nxffs_volume_s *volume;
  159. FAR struct nxffs_ofile_s *ofile;
  160. int ret;
  161. finfo("Buf %s\n", buf);
  162. DEBUGASSERT(filep != NULL && buf != NULL);
  163. /* Recover the open file state from the struct file instance */
  164. DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL);
  165. ofile = (FAR struct nxffs_ofile_s *)filep->f_priv;
  166. /* Recover the volume state from the open file */
  167. volume = (FAR struct nxffs_volume_s *)filep->f_inode->i_private;
  168. DEBUGASSERT(volume != NULL);
  169. /* Get exclusive access to the volume. Note that the volume exclsem
  170. * protects the open file list.
  171. */
  172. ret = nxsem_wait(&volume->exclsem);
  173. if (ret != OK)
  174. {
  175. ferr("ERROR: nxsem_wait failed: %d\n", ret);
  176. return ret;
  177. }
  178. /* Return status information based on the directory entry */
  179. buf->st_blocks = ofile->entry.datlen /
  180. (volume->geo.blocksize - SIZEOF_NXFFS_BLOCK_HDR);
  181. buf->st_mode = S_IFREG | S_IXOTH | S_IXGRP | S_IXUSR;
  182. buf->st_size = ofile->entry.datlen;
  183. buf->st_atime = ofile->entry.utc;
  184. buf->st_mtime = ofile->entry.utc;
  185. buf->st_ctime = ofile->entry.utc;
  186. nxsem_post(&volume->exclsem);
  187. return OK;
  188. }