๐Ÿ“ฆ apache / nuttx

๐Ÿ“„ fs_fstat.c ยท 284 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284/****************************************************************************
 * fs/vfs/fs_fstat.c
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.  The
 * ASF licenses this file to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the
 * License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations
 * under the License.
 *
 ****************************************************************************/

/****************************************************************************
 * Included Files
 ****************************************************************************/

#include <nuttx/config.h>

#include <sys/stat.h>
#include <unistd.h>
#include <sched.h>
#include <assert.h>
#include <errno.h>

#include <nuttx/fs/fs.h>
#include <nuttx/mtd/mtd.h>
#include <nuttx/net/net.h>
#include "inode/inode.h"

/****************************************************************************
 * Private Functions
 ****************************************************************************/

/****************************************************************************
 * Name: proxy_fstat
 *
 * Description:
 *   Check for special cases where the character driver is really just a
 *   proxy for the real, underlying MTD or block driver.
 *
 *   NOTE:  This must be done here rather than in the the common
 *   inode_stat() function because the filep reference must be available
 *   in order to call the character driver ioctl method.
 *
 * Input Parameters:
 *   filep  - File structure instance
 *   inode  - The inode associated with the file descriptor
 *   buf    - The caller provide location in which to return information
 *            about the open file.
 *
 * Returned Value:
 *   Upon successful completion, 0 is returned. Otherwise, a negated errno
 *   value is returned.
 *
 ****************************************************************************/

static int proxy_fstat(FAR struct file *filep, FAR struct inode *inode,
                       FAR struct stat *buf)
{
#ifdef CONFIG_MTD
  struct mtd_geometry_s mtdgeo;
#endif
#ifndef CONFIG_DISABLE_MOUNTPOINT
  struct geometry blkgeo;
#endif
  int ret = -ENOENT;

  /* Check if this is a valid character driver */

  if (INODE_IS_DRIVER(inode) &&
      inode->u.i_ops != NULL &&
      inode->u.i_ops->ioctl != NULL)
    {
#ifdef CONFIG_MTD
      /* Check if this is a proxy for an MTD driver.  In this case, both the
       * MTDIOC_GEOMETRY ioctl and the BIOC_GEOMTRY will be supported by
       * character driver.
       */

      if (inode->u.i_ops->ioctl(filep, MTDIOC_GEOMETRY,
                                (unsigned long)((uintptr_t)&mtdgeo)) >= 0)
        {
          memset(buf, 0, sizeof(struct stat));
          buf->st_mode  = S_IFMTD;
          buf->st_mode |= S_IROTH | S_IRGRP | S_IRUSR;
          buf->st_mode |= S_IWOTH | S_IWGRP | S_IWUSR;
          buf->st_size  = mtdgeo.neraseblocks * mtdgeo.erasesize;
          ret           = OK;
        }
#ifndef CONFIG_DISABLE_MOUNTPOINT
      else
#endif
#endif

#ifndef CONFIG_DISABLE_MOUNTPOINT
      /* Check if this is a proxy for a block driver.  In this case, only
       * the BIOC_GEOMETRY ioctl will be supported.
       */

      if (inode->u.i_ops->ioctl(filep, BIOC_GEOMETRY,
                                (unsigned long)((uintptr_t)&blkgeo)) >= 0)
        {
          memset(buf, 0, sizeof(struct stat));
          buf->st_mode = S_IFBLK;
          if (inode->u.i_ops->readv || inode->u.i_ops->read)
            {
              buf->st_mode |= S_IROTH | S_IRGRP | S_IRUSR;
            }

          if (inode->u.i_ops->writev || inode->u.i_ops->read)
            {
              buf->st_mode |= S_IWOTH | S_IWGRP | S_IWUSR;
            }

          if (blkgeo.geo_available)
            {
              buf->st_size = blkgeo.geo_nsectors * blkgeo.geo_sectorsize;
            }

          ret = OK;
        }
#endif
    }

  return ret;
}

/****************************************************************************
 * Public Functions
 ****************************************************************************/

/****************************************************************************
 * Name: file_fstat
 *
 * Description:
 *   file_fstat() is an internal OS interface.  It is functionally similar
 *   to the standard fstat() interface except:
 *
 *    - It does not modify the errno variable,
 *    - It is not a cancellation point,
 *    - It does not handle socket descriptors, and
 *    - It accepts a file structure instance instead of file descriptor.
 *
 * Input Parameters:
 *   filep  - File structure instance
 *   buf    - The caller provide location in which to return information
 *            about the open file.
 *
 * Returned Value:
 *   Upon successful completion, 0 shall be returned. Otherwise, -1 shall be
 *   returned and errno set to indicate the error.
 *
 ****************************************************************************/

int file_fstat(FAR struct file *filep, FAR struct stat *buf)
{
  FAR struct inode *inode;
  int ret;

  DEBUGASSERT(filep != NULL);

  /* Get the inode from the file structure */

  inode = filep->f_inode;

  /* Was this file opened ? */

  if (!inode)
    {
      return -EBADF;
    }

  /* The way we handle the stat depends on the type of inode that we
   * are dealing with.
   */

#ifndef CONFIG_DISABLE_MOUNTPOINT
  if (INODE_IS_MOUNTPT(inode))
    {
      /* The inode is a file system mountpoint. Verify that the mountpoint
       * supports the fstat() method
       */

      ret = -ENOSYS;
      if (inode->u.i_mops && inode->u.i_mops->fstat)
        {
          /* Perform the fstat() operation */

          ret = inode->u.i_mops->fstat(filep, buf);
        }
    }
  else
#endif
#ifdef CONFIG_NET
  if (INODE_IS_SOCKET(inode))
    {
      /* Let the networking logic handle the fstat() */

      ret = psock_fstat(file_socket(filep), buf);
    }
  else
#endif
    {
      /* Check if the inode is a proxy for a block or MTD driver */

      ret = proxy_fstat(filep, inode, buf);
      if (ret < 0)
        {
          /* The inode is part of the root pseudo file system. */

          ret = inode_stat(inode, buf, 0);
        }
    }

  return ret;
}

int nx_fstat(int fd, FAR struct stat *buf)
{
  FAR struct file *filep;
  int ret;

  /* First, get the file structure.  Note that on failure,
   * file_get() will return the errno.
   */

  ret = file_get(fd, &filep);
  if (ret >= 0)
    {
      /* Perform the fstat operation */

      ret = file_fstat(filep, buf);
      file_put(filep);
    }

  return ret;
}

/****************************************************************************
 * Name: fstat
 *
 * Description:
 *   The fstat() function will obtain information about an open file
 *   associated with the file descriptor 'fd', and will write it to the area
 *   pointed to by 'buf'.
 *
 *   The 'buf' argument is a pointer to a stat structure, as defined in
 *   <sys/stat.h>, into which information is placed concerning the file.
 *
 * Input Parameters:
 *   fd  - The file descriptor associated with the open file of interest
 *   buf - The caller provide location in which to return information about
 *         the open file.
 *
 * Returned Value:
 *   Upon successful completion, 0 shall be returned. Otherwise, -1 shall be
 *   returned and errno set to indicate the error.
 *
 ****************************************************************************/

int fstat(int fd, FAR struct stat *buf)
{
  int ret;

  ret = nx_fstat(fd, buf);
  if (ret < 0)
    {
      set_errno(-ret);
      ret = ERROR;
    }

  return ret;
}