nxffs_stat.c 6.9 KB

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