1 /****************************************************************************
2 * fs/vfs/fs_stat.c
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership. The
7 * ASF licenses this file to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance with the
9 * License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16 * License for the specific language governing permissions and limitations
17 * under the License.
18 *
19 ****************************************************************************/
20
21 /****************************************************************************
22 * Included Files
23 ****************************************************************************/
24
25 #include "vfs_config.h"
26
27 #include "errno.h"
28 #include "sys/stat.h"
29 #include "string.h"
30 #include "stdlib.h"
31 #include "vnode.h"
32 /****************************************************************************
33 * Global Functions
34 ****************************************************************************/
35
36 /****************************************************************************
37 * Name: stat
38 *
39 * Returned Value:
40 * Zero on success; -1 on failure with errno set:
41 *
42 * EACCES Search permission is denied for one of the directories in the
43 * path prefix of path.
44 * EFAULT Bad address.
45 * ENOENT A component of the path path does not exist, or the path is an
46 * empty string.
47 * ENOMEM Out of memory
48 * ENOTDIR A component of the path is not a directory.
49 *
50 ****************************************************************************/
51
stat(const char * path,struct stat * buf)52 int stat(const char *path, struct stat *buf)
53 {
54 struct Vnode *vp = NULL;
55 int ret;
56
57 /* Sanity checks */
58
59 if (!path || !buf)
60 {
61 ret = -EFAULT;
62 goto errout;
63 }
64
65 if (!path[0])
66 {
67 ret = -ENOENT;
68 goto errout;
69 }
70
71 /* Get an vnode for this file */
72 VnodeHold();
73 ret = VnodeLookup(path, &vp, 0);
74 if (ret < 0)
75 {
76 VnodeDrop();
77 goto errout;
78 }
79
80 /* The way we handle the stat depends on the type of vnode that we
81 * are dealing with.
82 */
83
84 if (vp->vop != NULL && vp->vop->Getattr != NULL)
85 {
86 vp->useCount++;
87 VnodeDrop();
88 ret = vp->vop->Getattr(vp, buf);
89 VnodeHold();
90 vp->useCount--;
91 VnodeDrop();
92 }
93 else
94 {
95 VnodeDrop();
96 ret = -ENOSYS;
97 goto errout;
98 }
99
100 if (ret < 0)
101 {
102 goto errout;
103 }
104
105 return OK;
106
107 /* Failure conditions always set the errno appropriately */
108
109 errout:
110 set_errno(-ret);
111 return VFS_ERROR;
112 }
113