1 | //===-- Linux implementation of pathconf_utils ----------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | // This header must go before limits_macros.h otherwise libc header may choose |
10 | // to undefine LINK_MAX. |
11 | #include <linux/limits.h> // For LINK_MAX and other limits |
12 | |
13 | #include "hdr/limits_macros.h" |
14 | #include "hdr/unistd_macros.h" |
15 | #include "src/__support/OSUtil/syscall.h" // For internal syscall function. |
16 | #include "src/__support/common.h" |
17 | #include "src/__support/libc_errno.h" |
18 | #include "src/__support/macros/config.h" |
19 | #include "src/sys/statvfs/linux/statfs_utils.h" |
20 | |
21 | // other linux specific includes |
22 | #include <linux/bfs_fs.h> |
23 | #if __has_include(<linux/ufs_fs.h>) |
24 | #include <linux/ufs_fs.h> |
25 | #else |
26 | // from https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/ |
27 | #define UFS_MAGIC 0x00011954 |
28 | #endif |
29 | #include <linux/magic.h> // For common FS magics |
30 | |
31 | namespace LIBC_NAMESPACE_DECL { |
32 | |
33 | long filesizebits(const statfs_utils::LinuxStatFs &s) { |
34 | switch (s.f_type) { |
35 | case JFFS2_SUPER_MAGIC: |
36 | case MSDOS_SUPER_MAGIC: |
37 | case NCP_SUPER_MAGIC: |
38 | return 32; |
39 | } |
40 | return 64; |
41 | } |
42 | |
43 | long link_max(const statfs_utils::LinuxStatFs &s) { |
44 | switch (s.f_type) { |
45 | case EXT2_SUPER_MAGIC: |
46 | return 32000; |
47 | case MINIX_SUPER_MAGIC: |
48 | return 250; |
49 | case MINIX2_SUPER_MAGIC: |
50 | return 65530; |
51 | case REISERFS_SUPER_MAGIC: |
52 | return 0xffff - 1000; |
53 | case UFS_MAGIC: |
54 | return 32000; |
55 | } |
56 | return LINK_MAX; |
57 | } |
58 | |
59 | long symlinks(const statfs_utils::LinuxStatFs &s) { |
60 | switch (s.f_type) { |
61 | case ADFS_SUPER_MAGIC: |
62 | case BFS_MAGIC: |
63 | case CRAMFS_MAGIC: |
64 | case EFS_SUPER_MAGIC: |
65 | case MSDOS_SUPER_MAGIC: |
66 | case QNX4_SUPER_MAGIC: |
67 | return 0; |
68 | } |
69 | return 1; |
70 | } |
71 | |
72 | long pathconfig(const statfs_utils::LinuxStatFs &s, int name) { |
73 | switch (name) { |
74 | case _PC_LINK_MAX: |
75 | return link_max(s); |
76 | |
77 | case _PC_FILESIZEBITS: |
78 | return filesizebits(s); |
79 | |
80 | case _PC_2_SYMLINKS: |
81 | return symlinks(s); |
82 | |
83 | case _PC_REC_MIN_XFER_SIZE: |
84 | return s.f_bsize; |
85 | |
86 | case _PC_ALLOC_SIZE_MIN: |
87 | case _PC_REC_XFER_ALIGN: |
88 | return s.f_frsize; |
89 | |
90 | case _PC_MAX_CANON: |
91 | return _POSIX_MAX_CANON; |
92 | |
93 | case _PC_MAX_INPUT: |
94 | return _POSIX_MAX_INPUT; |
95 | |
96 | case _PC_NAME_MAX: |
97 | return s.f_namelen; |
98 | |
99 | case _PC_PATH_MAX: |
100 | return _POSIX_PATH_MAX; |
101 | |
102 | case _PC_PIPE_BUF: |
103 | return _POSIX_PIPE_BUF; |
104 | |
105 | case _PC_CHOWN_RESTRICTED: |
106 | return _POSIX_CHOWN_RESTRICTED; |
107 | |
108 | case _PC_NO_TRUNC: |
109 | return _POSIX_NO_TRUNC; |
110 | |
111 | case _PC_VDISABLE: |
112 | return _POSIX_VDISABLE; |
113 | |
114 | case _PC_ASYNC_IO: |
115 | case _PC_PRIO_IO: |
116 | case _PC_REC_INCR_XFER_SIZE: |
117 | case _PC_REC_MAX_XFER_SIZE: |
118 | case _PC_SYMLINK_MAX: |
119 | case _PC_SYNC_IO: |
120 | return -1; |
121 | |
122 | default: |
123 | libc_errno = EINVAL; |
124 | return -1; |
125 | } |
126 | } |
127 | |
128 | } // namespace LIBC_NAMESPACE_DECL |
129 | |