2 * Copyright (C) 2018 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see
16 * <http://www.gnu.org/licenses/>.
24 #if HAVE_LINUX_MAGIC_H
25 # include <linux/magic.h>
29 #include "virstring.h"
32 #define VIR_FROM_THIS VIR_FROM_NONE
34 static FILE *(*real_setmntent
)(const char *filename
, const char *type
);
35 static int (*real_statfs
)(const char *path
, struct statfs
*buf
);
36 static char *(*real_canonicalize_file_name
)(const char *path
);
45 VIR_MOCK_REAL_INIT(setmntent
);
46 VIR_MOCK_REAL_INIT(statfs
);
47 VIR_MOCK_REAL_INIT(canonicalize_file_name
);
52 setmntent(const char *filename
, const char *type
)
58 if ((mtab
= getenv("LIBVIRT_MTAB")))
61 return real_setmntent(filename
, type
);
65 #ifndef NFS_SUPER_MAGIC
66 # define NFS_SUPER_MAGIC 0x6969
68 #ifndef OCFS2_SUPER_MAGIC
69 # define OCFS2_SUPER_MAGIC 0x7461636f
72 # define GFS2_MAGIC 0x01161970
75 # define AFS_FS_MAGIC 0x6B414653
77 #ifndef SMB_SUPER_MAGIC
78 # define SMB_SUPER_MAGIC 0x517B
80 #ifndef CIFS_SUPER_MAGIC
81 # define CIFS_SUPER_MAGIC 0xFF534D42
83 #ifndef HUGETLBFS_MAGIC
84 # define HUGETLBFS_MAGIC 0x958458f6
86 #ifndef FUSE_SUPER_MAGIC
87 # define FUSE_SUPER_MAGIC 0x65735546
89 #ifndef CEPH_SUPER_MAGIC
90 # define CEPH_SUPER_MAGIC 0x00c36400
92 #ifndef GPFS_SUPER_MAGIC
93 # define GPFS_SUPER_MAGIC 0x47504653
96 # define QB_MAGIC 0x51626d6e
101 statfs_mock(const char *mtab
,
108 char *canonPath
= NULL
;
111 if (!(f
= real_setmntent(mtab
, "r"))) {
112 fprintf(stderr
, "Unable to open %s", mtab
);
116 /* We don't need to do this in callers because real statfs(2)
117 * does that for us. However, in mocked implementation we
118 * need to do this. */
119 if (!(canonPath
= canonicalize_file_name(path
)))
122 while (getmntent_r(f
, &mb
, mntbuf
, sizeof(mntbuf
))) {
125 if (STRNEQ(mb
.mnt_dir
, canonPath
))
128 if (STREQ(mb
.mnt_type
, "nfs") ||
129 STREQ(mb
.mnt_type
, "nfs4")) {
130 ftype
= NFS_SUPER_MAGIC
;
131 } else if (STREQ(mb
.mnt_type
, "gfs2")||
132 STREQ(mb
.mnt_type
, "gfs2meta")) {
134 } else if (STREQ(mb
.mnt_type
, "ocfs2")) {
135 ftype
= OCFS2_SUPER_MAGIC
;
136 } else if (STREQ(mb
.mnt_type
, "afs")) {
137 ftype
= AFS_FS_MAGIC
;
138 } else if (STREQ(mb
.mnt_type
, "smb3")) {
139 ftype
= SMB_SUPER_MAGIC
;
140 } else if (STREQ(mb
.mnt_type
, "cifs")) {
141 ftype
= CIFS_SUPER_MAGIC
;
142 } else if (STRPREFIX(mb
.mnt_type
, "fuse")) {
143 ftype
= FUSE_SUPER_MAGIC
;
144 } else if (STRPREFIX(mb
.mnt_type
, "ceph")) {
145 ftype
= CEPH_SUPER_MAGIC
;
146 } else if (STRPREFIX(mb
.mnt_type
, "gpfs")) {
147 ftype
= GPFS_SUPER_MAGIC
;
149 /* Everything else is EXT4. We don't care really for other paths. */
150 ftype
= EXT4_SUPER_MAGIC
;
153 memset(buf
, 0, sizeof(*buf
));
154 /* We only care about f_type so far. */
167 statfs(const char *path
, struct statfs
*buf
)
173 if ((mtab
= getenv("LIBVIRT_MTAB")))
174 return statfs_mock(mtab
, path
, buf
);
176 return real_statfs(path
, buf
);
181 canonicalize_file_name(const char *path
)
186 if (getenv("LIBVIRT_MTAB")) {
190 if ((p
= STRSKIP(path
, "/some/symlink")))
191 ignore_value(virAsprintfQuiet(&ret
, "/gluster%s", p
));
193 ignore_value(VIR_STRDUP_QUIET(ret
, path
));
198 return real_canonicalize_file_name(path
);