• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /****************************************************************************
2  * fs/vfs/fs_stat.c
3  *
4  *   Copyright (C) 2007-2009, 2012, 2017 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 
42 #include "errno.h"
43 #include "sys/stat.h"
44 #include "string.h"
45 #include "stdlib.h"
46 #include "vnode.h"
47 /****************************************************************************
48  * Global Functions
49  ****************************************************************************/
50 
51 /****************************************************************************
52  * Name: stat
53  *
54  * Returned Value:
55  *   Zero on success; -1 on failure with errno set:
56  *
57  *   EACCES  Search permission is denied for one of the directories in the
58  *           path prefix of path.
59  *   EFAULT  Bad address.
60  *   ENOENT  A component of the path path does not exist, or the path is an
61  *           empty string.
62  *   ENOMEM  Out of memory
63  *   ENOTDIR A component of the path is not a directory.
64  *
65  ****************************************************************************/
66 
stat(const char * path,struct stat * buf)67 int stat(const char *path, struct stat *buf)
68 {
69   struct Vnode *vp = NULL;
70   int ret;
71 
72   /* Sanity checks */
73 
74   if (!path || !buf)
75     {
76       ret = -EFAULT;
77       goto errout;
78     }
79 
80   if (!path[0])
81     {
82       ret = -ENOENT;
83       goto errout;
84     }
85 
86   /* Get an vnode for this file */
87   VnodeHold();
88   ret = VnodeLookup(path, &vp, 0);
89   if (ret < 0)
90     {
91       VnodeDrop();
92       goto errout;
93     }
94 
95   /* The way we handle the stat depends on the type of vnode that we
96    * are dealing with.
97    */
98 
99   if (vp->vop != NULL && vp->vop->Getattr != NULL)
100     {
101       vp->useCount++;
102       VnodeDrop();
103       ret = vp->vop->Getattr(vp, buf);
104       VnodeHold();
105       vp->useCount--;
106       VnodeDrop();
107     }
108   else
109     {
110       VnodeDrop();
111       ret = -ENOSYS;
112       goto errout;
113     }
114 
115   if (ret < 0)
116     {
117       goto errout;
118     }
119 
120   return OK;
121 
122  /* Failure conditions always set the errno appropriately */
123 
124 errout:
125   set_errno(-ret);
126   return VFS_ERROR;
127 }
128