1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 #ifndef _LIBSPL_SYS_STAT_H
27 #define	_LIBSPL_SYS_STAT_H
28 
29 #include_next <sys/stat.h>
30 
31 /* Note: this file can be used on linux/macOS when bootstrapping tools. */
32 
33 #if defined(__FreeBSD__)
34 #include <sys/mount.h> /* for BLKGETSIZE64 */
35 
36 #define	stat64	stat
37 
38 #define	MAXOFFSET_T	OFF_MAX
39 
40 #ifndef _KERNEL
41 #include <sys/disk.h>
42 
43 static __inline int
44 fstat64(int fd, struct stat *sb)
45 {
46 	int ret;
47 
48 	ret = fstat(fd, sb);
49 	if (ret == 0) {
50 		if (S_ISCHR(sb->st_mode))
51 			(void) ioctl(fd, DIOCGMEDIASIZE, &sb->st_size);
52 	}
53 	return (ret);
54 }
55 #endif
56 
57 /*
58  * Emulate Solaris' behavior of returning the block device size in fstat64().
59  */
60 static inline int
61 fstat64_blk(int fd, struct stat64 *st)
62 {
63 	if (fstat64(fd, st) == -1)
64 		return (-1);
65 
66 	/* In Linux we need to use an ioctl to get the size of a block device */
67 	if (S_ISBLK(st->st_mode)) {
68 		if (ioctl(fd, BLKGETSIZE64, &st->st_size) != 0)
69 			return (-1);
70 	}
71 
72 	return (0);
73 }
74 #endif /* defined(__FreeBSD__) */
75 
76 /*
77  * Only Intel-based Macs have a separate stat64; Arm-based Macs are like
78  * FreeBSD and have a full 64-bit stat from the start.
79  */
80 #if defined(__APPLE__) && !(defined(__i386__) || defined(__x86_64__))
81 #define	stat64	stat
82 #define	fstat64	fstat
83 #endif
84 
85 #endif /* _LIBSPL_SYS_STAT_H */
86