]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/freebsd.c
xfs: create structure verifier function for short form symlinks
[thirdparty/xfsprogs-dev.git] / libxfs / freebsd.c
1 /*
2 * Copyright (c) 2003,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include "libxfs.h"
20 #include <sys/stat.h>
21 #include <sys/disk.h>
22 #include <sys/mount.h>
23 #include <sys/ioctl.h>
24 #include <sys/sysctl.h>
25
26 int platform_has_uuid = 1;
27 extern char *progname;
28
29 int
30 platform_check_ismounted(char *name, char *block, struct stat *s, int verbose)
31 {
32 struct stat st;
33 int cnt, i;
34 struct statfs *fsinfo;
35
36 if (!s) {
37 if (stat(block, &st) < 0)
38 return 0;
39 s = &st;
40 }
41
42 /* Remember, FreeBSD can now mount char devices! -- adrian */
43 if (((st.st_mode & S_IFMT) != S_IFBLK) &&
44 ((st.st_mode & S_IFMT) != S_IFCHR))
45 return 0;
46
47 if ((cnt = getmntinfo(&fsinfo, MNT_NOWAIT)) == 0) {
48 fprintf(stderr,
49 _("%s: %s possibly contains a mounted filesystem\n"),
50 progname, name);
51 return 1;
52 }
53
54 for (i = 0; i < cnt; i++) {
55 if (strcmp (name, fsinfo[i].f_mntfromname) != 0)
56 continue;
57
58 if (verbose)
59 fprintf(stderr,
60 _("%s: %s contains a mounted filesystem\n"),
61 progname, name);
62 break;
63 }
64
65 return i < cnt;
66 }
67
68 int
69 platform_check_iswritable(char *name, char *block, struct stat *s)
70 {
71 int cnt, i;
72 struct statfs *fsinfo;
73
74 if ((cnt = getmntinfo(&fsinfo, MNT_NOWAIT)) == 0) {
75 fprintf(stderr, _("%s: %s contains a possibly writable, "
76 "mounted filesystem\n"), progname, name);
77 return 1;
78 }
79
80 for (i = 0; i < cnt; i++) {
81 if (strcmp (name, fsinfo[i].f_mntfromname) != 0)
82 continue;
83
84 if (fsinfo[i].f_flags &= MNT_RDONLY)
85 break;
86 }
87
88 if (i == cnt) {
89 fprintf(stderr, _("%s: %s contains a mounted and writable "
90 "filesystem\n"), progname, name);
91 return 1;
92 }
93 return 0;
94 }
95
96 int
97 platform_set_blocksize(int fd, char *path, dev_t device, int blocksize, int fatal)
98 {
99 return fatal;
100 }
101
102 void
103 platform_flush_device(int fd, dev_t device)
104 {
105 return;
106 }
107
108 void
109 platform_findsizes(char *path, int fd, long long *sz, int *bsz)
110 {
111 struct stat st;
112 int64_t size;
113 uint ssize;
114
115 if (fstat(fd, &st) < 0) {
116 fprintf(stderr, _("%s: "
117 "cannot stat the device file \"%s\": %s\n"),
118 progname, path, strerror(errno));
119 exit(1);
120 }
121
122 if ((st.st_mode & S_IFMT) == S_IFREG) {
123 *sz = (long long)(st.st_size >> 9);
124 *bsz = 512;
125 return;
126 }
127
128 if ((st.st_mode & S_IFMT) != S_IFCHR) {
129 fprintf(stderr, _("%s: Not a device or file: \"%s\"\n"),
130 progname, path);
131 exit(1);
132 }
133
134 if (ioctl(fd, DIOCGMEDIASIZE, &size) != 0) {
135 fprintf(stderr, _("%s: DIOCGMEDIASIZE failed on \"%s\": %s\n"),
136 progname, path, strerror(errno));
137 exit(1);
138 }
139
140 if (ioctl(fd, DIOCGSECTORSIZE, &ssize) != 0) {
141 fprintf(stderr, _("%s: "
142 "DIOCGSECTORSIZE failed on \"%s\": %s\n"),
143 progname, path, strerror(errno));
144 exit(1);
145 }
146
147 *sz = (long long) (size / ssize);
148 *bsz = (int)ssize;
149 }
150
151 char *
152 platform_findrawpath(char *path)
153 {
154 return path;
155 }
156
157 char *
158 platform_findblockpath(char *path)
159 {
160 return path;
161 }
162
163 int
164 platform_direct_blockdev(void)
165 {
166 return 0;
167 }
168
169 int
170 platform_align_blockdev(void)
171 {
172 return sizeof(void *);
173 }
174
175 int
176 platform_nproc(void)
177 {
178 int ncpu;
179 size_t len = sizeof(ncpu);
180 static int mib[2] = {CTL_HW, HW_NCPU};
181
182 if (sysctl(mib, 2, &ncpu, &len, NULL, 0) < 0)
183 ncpu = 1;
184
185 return ncpu;
186 }
187
188 unsigned long
189 platform_physmem(void)
190 {
191 unsigned long physmem;
192 size_t len = sizeof(physmem);
193 static int mib[2] = {CTL_HW, HW_PHYSMEM};
194
195 if (sysctl(mib, 2, &physmem, &len, NULL, 0) < 0) {
196 fprintf(stderr, _("%s: can't determine memory size\n"),
197 progname);
198 exit(1);
199 }
200 return physmem >> 10;
201 }