Whamcloud - gitweb
import older libsysio snapshot.
[fs/lustre-release.git] / libsysio / src / stat.c
1 /*
2  *    This Cplant(TM) source code is the property of Sandia National
3  *    Laboratories.
4  *
5  *    This Cplant(TM) source code is copyrighted by Sandia National
6  *    Laboratories.
7  *
8  *    The redistribution of this Cplant(TM) source code is subject to the
9  *    terms of the GNU Lesser General Public License
10  *    (see cit/LGPL or http://www.gnu.org/licenses/lgpl.html)
11  *
12  *    Cplant(TM) Copyright 1998-2003 Sandia Corporation. 
13  *    Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
14  *    license for use of this work by or on behalf of the US Government.
15  *    Export of this program may require a license from the United States
16  *    Government.
17  */
18
19 /*
20  * This library is free software; you can redistribute it and/or
21  * modify it under the terms of the GNU Lesser General Public
22  * License as published by the Free Software Foundation; either
23  * version 2.1 of the License, or (at your option) any later version.
24  * 
25  * This library is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28  * Lesser General Public License for more details.
29  * 
30  * You should have received a copy of the GNU Lesser General Public
31  * License along with this library; if not, write to the Free Software
32  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33  *
34  * Questions or comments about this library should be sent to:
35  *
36  * Lee Ward
37  * Sandia National Laboratories, New Mexico
38  * P.O. Box 5800
39  * Albuquerque, NM 87185-1110
40  *
41  * lee@sandia.gov
42  */
43
44 #include <errno.h>
45 #include <assert.h>
46 #include <sys/types.h>
47 #include <sys/stat.h>
48 #include <unistd.h>
49 #include <sys/queue.h>
50
51 #include "sysio.h"
52 #include "inode.h"
53 #include "file.h"
54
55 #include "sysio-symbols.h"
56
57 #ifndef REDSTORM
58 #undef fstat
59 #undef stat
60 #undef lstat
61 #endif
62
63 #undef __fxstat
64 #undef __xstat
65 #undef __lxstat
66
67 #if !defined(_STAT_VER)
68 #define _STAT_VER               0
69 #endif
70
71 #if _LARGEFILE64_SOURCE
72 static void
73 convstat(struct stat64 *st64_buf, struct stat *st_buf)
74 {
75
76         st_buf->st_dev = st64_buf->st_dev;
77         st_buf->st_ino = st64_buf->st_ino;
78         st_buf->st_mode = st64_buf->st_mode;
79         st_buf->st_nlink = st64_buf->st_nlink;
80         st_buf->st_uid = st64_buf->st_uid;
81         st_buf->st_gid = st64_buf->st_gid;
82         st_buf->st_rdev = st64_buf->st_rdev;
83         st_buf->st_size = st64_buf->st_size;
84         st_buf->st_blksize = st64_buf->st_blksize;
85         st_buf->st_blocks = st64_buf->st_blocks;
86         st_buf->st_atime = st64_buf->st_atime;
87         st_buf->st_mtime = st64_buf->st_mtime;
88         st_buf->st_ctime = st64_buf->st_ctime;
89 }
90 #endif
91
92 int
93 PREPEND(__, SYSIO_INTERFACE_NAME(fxstat))(int __ver, 
94                                           int __fildes, 
95                                           struct stat *__stat_buf)
96 {
97         struct file *fil;
98         int     err;
99         struct intnl_stat *buf;
100 #if _LARGEFILE64_SOURCE
101         struct stat64 st64;
102 #endif
103         SYSIO_INTERFACE_DISPLAY_BLOCK;
104
105         SYSIO_INTERFACE_ENTER;
106         if (__ver != _STAT_VER) {
107                 err = -ENOSYS;
108                 goto out;
109         }
110
111         err = 0;
112         fil = _sysio_fd_find(__fildes);
113         if (!fil) {
114                 err = -EBADF;
115                 goto out;
116         }
117 #if _LARGEFILE64_SOURCE
118         buf = &st64;
119 #else
120         buf = __stat_buf;
121 #endif
122         err =
123             fil->f_ino->i_ops.inop_getattr(NULL, fil->f_ino, buf);
124 #if _LARGEFILE64_SOURCE
125         if (!err)
126                 convstat(buf, __stat_buf);
127 #endif
128 out:
129         SYSIO_INTERFACE_RETURN(err ? -1 : 0, err);
130 }
131
132 #ifdef REDSTORM
133 #undef _fxstat
134 sysio_sym_weak_alias(PREPEND(__, SYSIO_INTERFACE_NAME(fxstat)), 
135                      PREPEND(_, SYSIO_INTERFACE_NAME(fxstat)))
136 #endif
137
138 #ifndef REDSTORM
139 static int
140 PREPEND(__, SYSIO_INTERFACE_NAME(fstat))(int fd, struct stat *buf)
141 {
142
143         return PREPEND(__, SYSIO_INTERFACE_NAME(fxstat))(_STAT_VER, 
144                                                          fd, 
145                                                          buf);
146 }
147
148 sysio_sym_weak_alias(PREPEND(__, SYSIO_INTERFACE_NAME(fstat)), 
149                      SYSIO_INTERFACE_NAME(fstat))
150
151 #ifdef BSD
152 #undef _fstat
153 sysio_sym_weak_alias(PREPEND(__, SYSIO_INTERFACE_NAME(fstat)),
154                      PREPEND(_, SYSIO_INTERFACE_NAME(fstat)))
155 #endif
156 #endif
157
158 int
159 PREPEND(__, SYSIO_INTERFACE_NAME(xstat))(int __ver, 
160                                          const char *__filename, 
161                                          struct stat *__stat_buf)
162 {
163         struct intent intent;
164         int     err;
165         struct pnode *pno;
166         struct inode *ino;
167         struct intnl_stat *buf;
168 #if _LARGEFILE64_SOURCE
169         struct stat64 st64;
170 #endif
171         SYSIO_INTERFACE_DISPLAY_BLOCK;
172
173         SYSIO_INTERFACE_ENTER;
174         if (__ver != _STAT_VER) {
175                 err = -ENOSYS;
176                 goto out;
177         }
178
179         INTENT_INIT(&intent, INT_GETATTR, NULL, NULL);
180         err = _sysio_namei(_sysio_cwd, __filename, 0, &intent, &pno);
181         if (err)
182                 goto out;
183         ino = pno->p_base->pb_ino;
184 #if _LARGEFILE64_SOURCE
185         buf = &st64;
186 #else
187         buf = __stat_buf;
188 #endif
189         err =
190             ino->i_ops.inop_getattr(pno,
191                                     pno->p_base->pb_ino,
192                                     buf);
193
194         P_RELE(pno);
195 #if _LARGEFILE64_SOURCE
196         if (!err)
197                 convstat(buf, __stat_buf);
198 #endif
199 out:
200         SYSIO_INTERFACE_RETURN(err ? -1 : 0, err);
201 }
202
203 #ifdef REDSTORM
204 #undef _xstat
205 sysio_sym_weak_alias(PREPEND(__, SYSIO_INTERFACE_NAME(xstat)),
206                      PREPEND(_, SYSIO_INTERFACE_NAME(xstat)))
207 #endif
208
209 #ifndef REDSTORM
210 static int
211 PREPEND(__, SYSIO_INTERFACE_NAME(stat))(const char *filename, 
212                                         struct stat *buf)
213 {
214
215         return PREPEND(__, SYSIO_INTERFACE_NAME(xstat))(_STAT_VER, 
216                                                         filename,
217                                                         buf);
218 }
219
220 sysio_sym_weak_alias(PREPEND(__, SYSIO_INTERFACE_NAME(stat)),
221                      SYSIO_INTERFACE_NAME(stat))
222
223 #ifdef BSD
224 #undef _stat
225 sysio_sym_weak_alias(PREPEND(__, SYSIO_INTERFACE_NAME(stat)),
226                      PREPEND(_, SYSIO_INTERFACE_NAME(stat)))
227 #endif
228 #endif
229
230 int
231 PREPEND(__, SYSIO_INTERFACE_NAME(lxstat))(int __ver, 
232                                           const char *__filename, 
233                                           struct stat *__stat_buf)
234 {
235         struct intent intent;
236         int     err;
237         struct pnode *pno;
238         struct inode *ino;
239         struct intnl_stat *buf;
240 #if _LARGEFILE64_SOURCE
241         struct stat64 st64;
242 #endif
243         SYSIO_INTERFACE_DISPLAY_BLOCK;
244
245         SYSIO_INTERFACE_ENTER;
246         if (__ver != _STAT_VER) {
247                 err = -ENOSYS;
248                 goto out;
249         }
250
251         INTENT_INIT(&intent, INT_GETATTR, NULL, NULL);
252         err = _sysio_namei(_sysio_cwd, __filename, ND_NOFOLLOW, &intent, &pno);
253         if (err)
254                 goto out;
255 #if _LARGEFILE64_SOURCE
256         buf = &st64;
257 #else
258         buf = __stat_buf;
259 #endif
260         ino = pno->p_base->pb_ino;
261         err =
262             ino->i_ops.inop_getattr(pno,
263                                     pno->p_base->pb_ino,
264                                     buf);
265
266         P_RELE(pno);
267 #if _LARGEFILE64_SOURCE
268         if (!err)
269                 convstat(buf, __stat_buf);
270 #endif
271 out:
272         SYSIO_INTERFACE_RETURN(err ? -1 : 0, err);
273 }
274
275 #ifdef REDSTORM
276 #undef _lxstat
277 sysio_sym_weak_alias(PREPEND(__, SYSIO_INTERFACE_NAME(lxstat)),
278                      PREPEND(_, SYSIO_INTERFACE_NAME(lxstat)))
279 #endif
280
281 #ifndef REDSTORM
282 static int
283 PREPEND(__, SYSIO_INTERFACE_NAME(lstat))(const char *filename, struct stat *buf)
284 {
285         return PREPEND(__, SYSIO_INTERFACE_NAME(lxstat))(_STAT_VER, 
286                                                          filename,
287                                                          buf);
288 }
289
290 sysio_sym_weak_alias(PREPEND(__, SYSIO_INTERFACE_NAME(lstat)),
291                      SYSIO_INTERFACE_NAME(lstat))
292
293 #ifdef BSD
294 #undef _lstat
295 sysio_sym_weak_alias(PREPEND(__, SYSIO_INTERFACE_NAME(lstat)),
296                      PREPEND(_, SYSIO_INTERFACE_NAME(lstat)))
297 #endif
298 #endif