Merge from vendor branch OPENSSH:
[dragonfly.git] / sys / emulation / dragonfly12 / dfbsd12_stat.c
1 /*-
2  * Copyright (c) 2005 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Joerg Sonnenberger <joerg@bec.de>.
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 of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software 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 HOLDERS 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;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * $DragonFly: src/sys/emulation/dragonfly12/dfbsd12_stat.c,v 1.4 2008/09/17 21:44:16 dillon Exp $
35  */
36
37 #include "opt_compatdf12.h"
38
39 #include <sys/param.h>
40 #include <sys/kern_syscall.h>
41 #include <sys/mount.h>
42 #include <sys/nlookup.h>
43 #include <sys/proc.h>
44 #include <sys/stat.h>
45 #include <sys/sysproto.h>
46 #include <sys/systm.h>
47 #include <sys/thread.h>
48 #include <sys/vnode.h>
49 #include <emulation/dragonfly12/stat.h>
50
51 static void
52 cvtstat(struct dfbsd12_stat *oldstat, struct stat *newstat)
53 {
54         bzero(oldstat, sizeof(oldstat));
55
56         oldstat->st_dev = newstat->st_dev;
57         oldstat->st_ino = newstat->st_ino;      /* truncation */
58         oldstat->st_mode = newstat->st_mode;
59         oldstat->st_nlink = newstat->st_nlink;  /* truncation */
60         oldstat->st_uid = newstat->st_uid;
61         oldstat->st_gid = newstat->st_gid;
62         oldstat->st_rdev = newstat->st_rdev;
63         oldstat->st_atimespec = newstat->st_atimespec;
64         oldstat->st_mtimespec = newstat->st_mtimespec;
65         oldstat->st_ctimespec = newstat->st_ctimespec;
66         oldstat->st_size = newstat->st_size;
67         oldstat->st_blocks = newstat->st_blocks;
68         oldstat->st_blksize = newstat->st_blksize;
69         oldstat->st_flags = newstat->st_flags;
70         oldstat->st_gen = newstat->st_gen;
71 }
72
73 /*
74  * stat_args(char *path, struct dfbsd12_stat *ub)
75  *
76  * Get file status; this version follows links.
77  */
78 int
79 sys_dfbsd12_stat(struct dfbsd12_stat_args *uap)
80 {
81         struct nlookupdata nd;
82         struct dfbsd12_stat ost;
83         struct stat st;
84         int error;
85
86         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
87         if (error == 0) {
88                 error = kern_stat(&nd, &st);
89                 if (error == 0) {
90                         cvtstat(&ost, &st);
91                         error = copyout(&ost, uap->ub, sizeof(ost));
92                 }
93         }
94         nlookup_done(&nd);
95         return (error);
96 }
97
98 /*
99  * lstat_args(char *path, struct dfbsd12_stat *ub)
100  *
101  * Get file status; this version does not follow links.
102  */
103 int
104 sys_dfbsd12_lstat(struct dfbsd12_lstat_args *uap)
105 {
106         struct nlookupdata nd;
107         struct dfbsd12_stat ost;
108         struct stat st;
109         int error;
110
111         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
112         if (error == 0) {
113                 error = kern_stat(&nd, &st);
114                 if (error == 0) {
115                         cvtstat(&ost, &st);
116                         error = copyout(&ost, uap->ub, sizeof(ost));
117                 }
118         }
119         nlookup_done(&nd);
120         return (error);
121 }
122
123 /*
124  * fhstat_args(struct fhandle *u_fhp, struct dfbsd12_stat *sb)
125  */
126 int
127 sys_dfbsd12_fhstat(struct dfbsd12_fhstat_args *uap)
128 {
129         struct thread *td = curthread;
130         struct dfbsd12_stat osb;
131         struct stat sb;
132         fhandle_t fh;
133         struct mount *mp;
134         struct vnode *vp;
135         int error;
136
137         /*
138          * Must be super user
139          */
140         error = suser(td);
141         if (error)
142                 return (error);
143         
144         error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
145         if (error)
146                 return (error);
147
148         if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
149                 return (ESTALE);
150         if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
151                 return (error);
152         error = vn_stat(vp, &sb, td->td_proc->p_ucred);
153         vput(vp);
154         if (error)
155                 return (error);
156         cvtstat(&osb, &sb);
157         error = copyout(&osb, uap->sb, sizeof(osb));
158         return (error);
159 }
160
161 /*
162  * dfbsd12_fstat_args(int fd, struct dfbsd12_stat *sb)
163  */
164 int
165 sys_dfbsd12_fstat(struct dfbsd12_fstat_args *uap)
166 {
167         struct dfbsd12_stat ost;
168         struct stat st;
169         int error;
170
171         error = kern_fstat(uap->fd, &st);
172
173         if (error == 0) {
174                 cvtstat(&ost, &st);
175                 error = copyout(&ost, uap->sb, sizeof(ost));
176         }
177         return (error);
178 }