• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /****************************************************************************
2  * fs/dirent/fs_seekdir.c
3  *
4  *   Copyright (C) 2007, 2008, 2011, 2014 Gregory Nutt. All rights reserved.
5  *   Author: Gregory Nutt <gnutt@nuttx.org>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name NuttX nor the names of its contributors may be
18  *    used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *
34  ****************************************************************************/
35 
36 /****************************************************************************
37  * Included Files
38  ****************************************************************************/
39 
40 #include "vfs_config.h"
41 #include "sys/types.h"
42 #include "dirent.h"
43 #include "errno.h"
44 #include "fs/dirent_fs.h"
45 #include "vnode.h"
46 
47 /****************************************************************************
48  * Private Functions
49  ****************************************************************************/
50 /****************************************************************************
51  * Name: seekmountptdir
52  ****************************************************************************/
53 
seekmountptdir(struct fs_dirent_s * idir,off_t offset)54 static inline void seekmountptdir(struct fs_dirent_s *idir, off_t offset)
55 {
56   struct Vnode *vnode;
57   off_t pos;
58 
59   /* Determine a starting point for the seek.  If the seek
60    * is "forward" from the current position, then we will
61    * start at the current poisition.  Otherwise, we will
62    * "rewind" to the root dir.
63    */
64 
65   vnode = idir->fd_root;
66   if (offset < idir->fd_position)
67     {
68       if (vnode->vop != NULL && vnode->vop->Rewinddir != NULL)
69         {
70           /* Perform the rewinddir() operation */
71 
72           vnode->vop->Rewinddir(vnode, idir);
73           pos = 0;
74         }
75       else
76         {
77           /* We can't do the seek and there is no way to return
78            * an error indication.
79            */
80 
81           return;
82         }
83     }
84   else
85     {
86       pos = idir->fd_position;
87     }
88 
89   /* This is a brute force approach... we will just read
90    * directory entries until we are at the desired position.
91    */
92 
93   idir->read_cnt = 1;
94 
95   while (pos < offset)
96     {
97       if (!vnode->vop || !vnode->vop->Readdir ||
98            vnode->vop->Readdir(vnode, idir) <= 0)
99         {
100           /* We can't read the next entry and there is no way to return
101            * an error indication.
102            */
103 
104            return;
105         }
106 
107       /* Increment the position on each successful read */
108 
109       pos++;
110     }
111 
112   /* If we get here the directory position has been successfully set */
113   idir->fd_position = pos;
114 }
115 
116 /****************************************************************************
117  * Public Functions
118  ****************************************************************************/
119 
120 /****************************************************************************
121  * Name: seekdir
122  *
123  * Description:
124  *   The seekdir() function sets the location in the directory stream from
125  *   which the next readdir() call will start.  seekdir() should be used with
126  *   an offset returned by telldir().
127  *
128  * Input Parameters:
129  *   dirp -- An instance of type DIR created by a previous
130  *     call to opendir();
131  *   offset -- offset to seek to
132  *
133  * Returned Value:
134  *   None
135  *
136  ****************************************************************************/
137 
seekdir(DIR * dirp,long offset)138 void seekdir(DIR *dirp, long offset)
139 {
140   struct fs_dirent_s *idir = (struct fs_dirent_s *)dirp;
141 
142   if (!idir || !idir->fd_root || idir->fd_status != DIRENT_MAGIC)
143     {
144       set_errno(EBADF);
145       return;
146     }
147 
148   if (offset < 0)
149     {
150       set_errno(EINVAL);
151       return;
152     }
153 
154   seekmountptdir(idir, offset);
155 }
156