• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /****************************************************************************
2  * include/nuttx/fs/dirent_fs.h
3  *
4  *   Copyright (C) 2007, 2009, 2011-2013, 2015, 2018 Gregory Nutt. All
5  *     rights reserved.
6  *   Author: Gregory Nutt <gnutt@nuttx.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  * 3. Neither the name NuttX nor the names of its contributors may be
19  *    used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  ****************************************************************************/
36 
37 #ifndef __INCLUDE_NUTTX_FS_DIRENT_FS_H
38 #define __INCLUDE_NUTTX_FS_DIRENT_FS_H
39 
40 /****************************************************************************
41  * Included Files
42  ****************************************************************************/
43 
44 #include "vfs_config.h"
45 
46 #include "sys/types.h"
47 #include "stdint.h"
48 #include "dirent.h"
49 #include "vnode.h"
50 
51 #ifdef __cplusplus
52 #if __cplusplus
53 extern "C" {
54 #endif /* __cplusplus */
55 #endif /* __cplusplus */
56 
57 #define kmm_malloc(s)    malloc(s)
58 #define kmm_free(mem)    free(mem)
59 #define kmm_zalloc(s)    zalloc(s)
60 
61 /****************************************************************************
62  * Pre-processor Definitions
63  ****************************************************************************/
64 
65 #ifdef CONFIG_NFS
66 #  define DIRENT_NFS_MAXHANDLE 64        /* Maximum length of an NFSv3 file handle */
67 #  define DIRENT_NFS_VERFLEN    8        /* Length of the copy verifier */
68 #endif
69 
70 /****************************************************************************
71  * Public Types
72  ****************************************************************************/
73 
74 /* The internal representation of type DIR is just a container for an inode
75  * reference, a position, a dirent structure, and file-system-specific
76  * information.
77  *
78  * For the root pseudo-file system, we need retain only the 'next' inode
79  * need for the next readdir() operation.  We hold a reference on this
80  * inode so we know that it will persist until closedir is called.
81  */
82 
83 struct fs_pseudodir_s
84 {
85   struct inode *fd_next;             /* The inode for the next call to readdir() */
86 };
87 
88 typedef void *fs_dir_s;
89 
90 #define DIRENT_MAGIC     0x11CBA828       /* Magic number to express the status of a dirent */
91 
92 #ifdef CONFIG_FS_FAT
93 /* For fat, we need to return the start cluster, current cluster, current
94  * sector and current directory index.
95  */
96 
97 struct fs_fatdir_s
98 {
99   off_t        fd_startcluster;        /* Start cluster number of the directory */
100   off_t        fd_currcluster;         /* Current cluster number being read */
101   off_t        fd_currsector;          /* Current sector being read */
102   unsigned int fd_index;               /* Current index of the directory entry to read */
103 };
104 #endif /* CONFIG_FS_FAT */
105 
106 #ifdef CONFIG_FS_ROMFS
107 /* For ROMFS, we need to return the offset to the current and start positions
108  * of the directory entry being read
109  */
110 
111 struct fs_romfsdir_s
112 {
113   off_t        fr_firstoffset;         /* Offset to the first entry in the directory */
114   off_t        fr_curroffset;          /* Current offset into the directory contents */
115 };
116 #endif /* CONFIG_FS_ROMFS */
117 
118 #ifdef CONFIG_FS_CROMFS
119 /* For CROMFS, we need to return the next compressed node to be examined. */
120 
121 struct fs_cromfsdir_s
122 {
123   uint32_t     cr_firstoffset;         /* Offset to the first entry in the directory */
124   uint32_t     cr_curroffset;          /* Current offset into the directory contents */
125 };
126 #endif /* CONFIG_FS_CROMFS */
127 /* For TMPFS, we need the directory object and an index into the directory
128  * entries.
129  */
130 
131 struct tmpfs_directory_s;               /* Forward reference */
132 struct fs_tmpfsdir_s
133 {
134   struct tmpfs_directory_s *tf_tdo; /* Directory being enumerated */
135   unsigned int tf_index;                /* Directory index */
136 };
137 
138 #ifdef CONFIG_FS_BINFS
139 /* The apps/ pseudo bin/ directory.  The state value is simply an index */
140 
141 struct fs_binfsdir_s
142 {
143   unsigned int fb_index;               /* Index to the next named entry point */
144 };
145 #endif
146 
147 #ifdef CONFIG_FS_NXFFS
148 /* NXFFS is the tiny NuttX wear-leveling FLASH file system.  The state value is
149  * the offset in FLASH memory to the next vnode entry.
150  */
151 
152 struct fs_nxffsdir_s
153 {
154   off_t nx_offset;                     /* Offset to the next vnode */
155 };
156 #endif
157 
158 #ifdef CONFIG_NFS
159 /* The NFS client file system */
160 
161 struct nfsdir_s
162 {
163   uint8_t  nfs_fhsize;                        /* Length of the file handle */
164   uint8_t  nfs_fhandle[DIRENT_NFS_MAXHANDLE]; /* File handle (max size allocated) */
165   uint8_t  nfs_verifier[DIRENT_NFS_VERFLEN];  /* Cookie verifier */
166   uint32_t nfs_cookie[2];                     /* Cookie */
167 };
168 #endif
169 
170 #ifdef CONFIG_FS_SMARTFS
171 /* SMARTFS is the Sector Mapped Allocation for Really Tiny FLASH filesystem.
172  * it is designed to use small sectors on small serial FLASH devices, using
173  * minimal RAM footprint.
174  */
175 
176 struct fs_smartfsdir_s
177 {
178   uint16_t fs_firstsector;                    /* First sector of directory list */
179   uint16_t fs_currsector;                     /* Current sector of directory list */
180   uint16_t fs_curroffset;                     /* Current offset within current sector */
181 };
182 #endif
183 
184 #ifdef CONFIG_FS_SPIFFS
185 /* SPIFFS is an SPI-oriented FLASH file system originally by Peter Andersson */
186 
187 struct fs_spiffsdir_s
188 {
189   int16_t block;                               /* Current block */
190   int entry;                                   /* Current entry */
191 };
192 #endif
193 
194 #ifdef CONFIG_FS_UNIONFS
195 /* The Union File System can be used to merge to different mountpoints so
196  * that they appear as a single merged directory.
197  */
198 
199 struct fs_dirent_s;                           /* Forward reference */
200 struct fs_unionfsdir_s
201 {
202   uint8_t fu_ndx;                             /* Index of file system being enumerated */
203   bool fu_eod;                                /* True: At end of directory */
204   bool fu_prefix[2];                          /* True: Fake directory in prefix */
205   char *fu_relpath;                       /* Path being enumerated */
206   struct fs_dirent_s *fu_lower[2];        /* dirent struct used by contained file system */
207 };
208 #endif
209 
210 #ifdef CONFIG_FS_USERFS
211 /* The UserFS uses an opaque representation since the actual userspace representation
212  * of the directory state structure is unknowable.
213  */
214 
215 struct fs_userfsdir_s
216 {
217   void *fs_dir;                           /* Opaque pointer to UserFS DIR */
218 };
219 #endif
220 
221 #ifdef CONFIG_FS_HOSTFS
222 /* HOSTFS provides mapping to directories on the host machine in the
223  * sim environment.
224  */
225 
226 struct fs_hostfsdir_s
227 {
228   void *fs_dir;                           /* Opaque pointer to host DIR */
229 };
230 #endif
231 
232 struct fs_dirent_s
233 {
234   /* This is the node that was opened by opendir.  The type of the vnode
235    * determines the way that the readdir() operations are performed. For the
236    * pseudo root pseudo-file system, it is also used to support rewind.
237    *
238    * We hold a reference on this vnode so we know that it will persist until
239    * closedir() is called (although vnodes linked to this vnode may change).
240    */
241 
242   struct Vnode *fd_root;
243 
244   /* At present, only mountpoints require special handling flags */
245 
246   unsigned int fd_flags;
247 
248   /* This keeps track of the current directory position for telldir */
249 
250   off_t fd_position;
251 
252   /* This keeps track of the internal offset for some FSs */
253   off_t fd_int_offset;
254 
255 
256   /* Retained control information depends on the type of file system that
257    * provides is provides the mountpoint.  Ideally this information should
258    * be hidden behind an opaque, file-system-dependent void *, but we put
259    * the private definitions in line here for now to reduce allocations.
260    */
261 
262   struct
263     {
264       /* Private data used by the built-in pseudo-file system */
265 
266       struct fs_pseudodir_s pseudo;
267 
268       /* Private data used by other file systems */
269       fs_dir_s fs_dir;
270 #ifdef CONFIG_FS_FAT
271       struct fs_fatdir_s     fat;
272 #endif
273 #ifdef CONFIG_FS_ROMFS
274       struct fs_romfsdir_s   romfs;
275 #endif
276 #ifdef CONFIG_FS_CROMFS
277       struct fs_cromfsdir_s  cromfs;
278 #endif
279 #ifdef CONFIG_FS_TMPFS
280       struct fs_tmpfsdir_s   tmpfs;
281 #endif
282 #ifdef CONFIG_FS_BINFS
283       struct fs_binfsdir_s   binfs;
284 #endif
285 #ifdef CONFIG_FS_PROCFS
286       void              *procfs;
287 #endif
288 #ifdef CONFIG_FS_NXFFS
289       struct fs_nxffsdir_s   nxffs;
290 #endif
291 #ifdef CONFIG_NFS
292       struct nfsdir_s        nfs;
293 #endif
294 #ifdef CONFIG_FS_SMARTFS
295       struct fs_smartfsdir_s smartfs;
296 #endif
297 #ifdef CONFIG_FS_SPIFFS
298       struct fs_spiffsdir_s  spiffs;
299 #endif
300 #ifdef CONFIG_FS_LITTLEFS
301       void              *littlefs;
302 #endif
303 #ifdef CONFIG_FS_UNIONFS
304       struct fs_unionfsdir_s unionfs;
305 #endif
306 #ifdef CONFIG_FS_USERFS
307       struct fs_userfsdir_s  userfs;
308 #endif
309 #ifdef CONFIG_FS_HOSTFS
310       struct fs_hostfsdir_s  hostfs;
311 #endif
312 
313 #ifdef LOSCFG_FS_ZPFS
314       void                   *zpfs;
315 #endif
316     } u;
317 
318   /* In any event, this the actual struct dirent that is returned by readdir */
319 #ifdef LOSCFG_ENABLE_READ_BUFFER
320   struct dirent fd_dir[MAX_DIRENT_NUM];              /* Populated when readdir is called */
321 #else
322   struct dirent fd_dir[1];              /* Populated when readdir is called */
323 #endif
324   int16_t       cur_pos;
325   int16_t       end_pos;
326   int32_t       read_cnt;
327   int           fd_status;           /* Express the dirent is been opened or no */
328 };
329 
330 /****************************************************************************
331  * Public Data
332  ****************************************************************************/
333 
334 /****************************************************************************
335  * Public Function Prototypes
336  ****************************************************************************/
337 #ifdef __cplusplus
338 #if __cplusplus
339 }
340 #endif /* __cplusplus */
341 #endif /* __cplusplus */
342 #endif /* __INCLUDE_NUTTX_FS_DIRENT_FS_H */
343