]>
git.saurik.com Git - apple/libc.git/blob - sys/statx_np.c
2 * Copyright (c) 2004, 2006 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 #include <sys/types.h>
34 #define ACL_MIN_SIZE_HEURISTIC (KAUTH_FILESEC_SIZE(16))
36 static int statx_syscall(void *obj
, void *sbptr
, void *fsacl
, size_t *fsacl_size
);
37 static int fstatx_syscall(void *obj
, void *sbptr
, void *fsacl
, size_t *fsacl_size
);
38 static int lstatx_syscall(void *obj
, void *sbptr
, void *fsacl
, size_t *fsacl_size
);
40 static int statx1(void *obj
,
41 int (* stat_syscall
)(void *obj
, void *sbptr
, void *fsacl
, size_t *fsacl_size
),
42 void *sbptr
, filesec_t fsec
);
48 statx_np(const char *path
, struct stat
*sb
, filesec_t fsec
)
51 return(stat(path
, sb
));
52 return(statx1((void *)&path
, statx_syscall
, (void *)sb
, fsec
));
56 fstatx_np(int fd
, struct stat
*sb
, filesec_t fsec
)
59 return(fstat(fd
, sb
));
60 return(statx1((void *)&fd
, fstatx_syscall
, (void *)sb
, fsec
));
64 lstatx_np(const char *path
, struct stat
*sb
, filesec_t fsec
)
67 return(lstat(path
, sb
));
68 return(statx1((void *)&path
, lstatx_syscall
, (void *)sb
, fsec
));
72 #if __DARWIN_64_BIT_INO_T && !__DARWIN_ONLY_64_BIT_INO_T
74 statx64_np(const char *path
, struct stat64
*sb
, filesec_t fsec
)
76 return(statx_np(path
, (struct stat
*)sb
, fsec
));
80 fstatx64_np(int fd
, struct stat64
*sb
, filesec_t fsec
)
82 return(fstatx_np(fd
, (struct stat
*)sb
, fsec
));
86 lstatx64_np(const char *path
, struct stat64
*sb
, filesec_t fsec
)
88 return(lstatx_np(path
, (struct stat
*)sb
, fsec
));
90 #endif /* __DARWIN_64_BIT_INO_T && !__DARWIN_ONLY_64_BIT_INO_T */
95 #if __DARWIN_64_BIT_INO_T
96 extern int __fstat64_extended(int, struct stat
*, void *, size_t *);
97 extern int __lstat64_extended(const char *, struct stat
*, void *, size_t *);
98 extern int __stat64_extended(const char *, struct stat
*, void *, size_t *);
99 #else /* !__DARWIN_64_BIT_INO_T */
100 extern int __fstat_extended(int, struct stat
*, void *, size_t *);
101 extern int __lstat_extended(const char *, struct stat
*, void *, size_t *);
102 extern int __stat_extended(const char *, struct stat
*, void *, size_t *);
103 #endif /* __DARWIN_64_BIT_INO_T */
106 statx_syscall(void *obj
, void *sb
, void *fsacl
, size_t *fsacl_size
)
108 const char *path
= *(const char **)obj
;
109 #if __DARWIN_64_BIT_INO_T
110 return(__stat64_extended(path
, (struct stat
*)sb
, fsacl
, fsacl_size
));
111 #else /* !__DARWIN_64_BIT_INO_T */
112 return(__stat_extended(path
, (struct stat
*)sb
, fsacl
, fsacl_size
));
113 #endif /* __DARWIN_64_BIT_INO_T */
117 fstatx_syscall(void *obj
, void *sb
, void *fsacl
, size_t *fsacl_size
)
119 int fd
= *(int *)obj
;
120 #if __DARWIN_64_BIT_INO_T
121 return(__fstat64_extended(fd
, (struct stat
*)sb
, fsacl
, fsacl_size
));
122 #else /* !__DARWIN_64_BIT_INO_T */
123 return(__fstat_extended(fd
, (struct stat
*)sb
, fsacl
, fsacl_size
));
124 #endif /* __DARWIN_64_BIT_INO_T */
128 lstatx_syscall(void *obj
, void *sb
, void *fsacl
, size_t *fsacl_size
)
130 const char *path
= *(const char **)obj
;
131 #if __DARWIN_64_BIT_INO_T
132 return(__lstat64_extended(path
, (struct stat
*)sb
, fsacl
, fsacl_size
));
133 #else /* !__DARWIN_64_BIT_INO_T */
134 return(__lstat_extended(path
, (struct stat
*)sb
, fsacl
, fsacl_size
));
135 #endif /* __DARWIN_64_BIT_INO_T */
143 int (* stat_syscall
)(void *obj
, void *sbptr
, void *fsacl
, size_t *fsacl_size
),
144 void *sbptr
, filesec_t fsec
)
146 kauth_filesec_t fsacl
, ofsacl
;
147 size_t fsacl_size
, buffer_size
;
149 struct stat
* sb
= (struct stat
*)0;
154 sb
= (struct stat
*)sbptr
;
157 * Allocate an initial buffer.
159 if ((fsacl
= malloc(ACL_MIN_SIZE_HEURISTIC
)) == NULL
) {
163 buffer_size
= ACL_MIN_SIZE_HEURISTIC
;
166 * Loop until we have the ACL.
169 fsacl_size
= buffer_size
;
171 if ((error
= stat_syscall(obj
, sbptr
, fsacl
, &fsacl_size
)) != 0)
175 * No error, did we get the ACL?
177 if (fsacl_size
<= buffer_size
)
180 /* no, use supplied buffer size plus some padding */
182 fsacl
= realloc(fsacl
, fsacl_size
+ sizeof(struct kauth_ace
) * 2);
188 buffer_size
= fsacl_size
;
191 /* populate filesec with values from stat */
192 filesec_set_property(fsec
, FILESEC_OWNER
, &(sb
->st_uid
));
193 filesec_set_property(fsec
, FILESEC_GROUP
, &(sb
->st_gid
));
194 filesec_set_property(fsec
, FILESEC_MODE
, &(sb
->st_mode
));
196 /* if we got a kauth_filesec, take values from there too */
197 if (fsacl_size
>= sizeof(struct kauth_filesec
)) {
198 filesec_set_property(fsec
, FILESEC_UUID
, &fsacl
->fsec_owner
);
199 filesec_set_property(fsec
, FILESEC_GRPUUID
, &fsacl
->fsec_group
);
201 /* check to see whether there's actually an ACL here */
202 if (fsacl
->fsec_acl
.acl_entrycount
!= KAUTH_FILESEC_NOACL
) {
203 filesec_set_property(fsec
, FILESEC_ACL_ALLOCSIZE
, &fsacl_size
);
204 filesec_set_property(fsec
, FILESEC_ACL_RAW
, &fsacl
);
205 fsacl
= NULL
; /* avoid freeing it below */
207 filesec_set_property(fsec
, FILESEC_ACL_ALLOCSIZE
, NULL
);
208 filesec_set_property(fsec
, FILESEC_ACL_RAW
, NULL
);
211 filesec_set_property(fsec
, FILESEC_UUID
, NULL
);
212 filesec_set_property(fsec
, FILESEC_GRPUUID
, NULL
);
213 filesec_set_property(fsec
, FILESEC_ACL_ALLOCSIZE
, NULL
);
214 filesec_set_property(fsec
, FILESEC_ACL_RAW
, NULL
);