]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - lib/support/plausible.c
Eliminate unused parameter warnings from Android build
[thirdparty/e2fsprogs.git] / lib / support / plausible.c
1 /*
2 * plausible.c --- Figure out if a pathname is ext* or something else.
3 *
4 * Copyright 2014, Oracle, Inc.
5 *
6 * Some parts are:
7 * Copyright 1995, 1996, 1997, 1998, 1999, 2000 by Theodore Ts'o.
8 *
9 * %Begin-Header%
10 * This file may be redistributed under the terms of the GNU Public
11 * License.
12 * %End-Header%
13 */
14
15 #ifndef _LARGEFILE_SOURCE
16 #define _LARGEFILE_SOURCE
17 #endif
18 #ifndef _LARGEFILE64_SOURCE
19 #define _LARGEFILE64_SOURCE
20 #endif
21
22 #include "config.h"
23 #include <fcntl.h>
24 #include <time.h>
25 #include <sys/types.h>
26 #ifdef HAVE_SYS_STAT_H
27 #include <sys/stat.h>
28 #endif
29 #ifdef HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32 #ifdef HAVE_MAGIC_H
33 #include <magic.h>
34 #endif
35 #include "plausible.h"
36 #include "ext2fs/ext2fs.h"
37 #include "nls-enable.h"
38 #include "blkid/blkid.h"
39
40 #ifdef HAVE_MAGIC_H
41 static magic_t (*dl_magic_open)(int);
42 static const char *(*dl_magic_file)(magic_t, const char *);
43 static int (*dl_magic_load)(magic_t, const char *);
44 static void (*dl_magic_close)(magic_t);
45
46 #ifdef HAVE_DLOPEN
47 #include <dlfcn.h>
48
49 static void *magic_handle;
50
51 static int magic_library_available(void)
52 {
53 if (!magic_handle) {
54 magic_handle = dlopen("libmagic.so.1", RTLD_NOW);
55 if (!magic_handle)
56 return 0;
57
58 dl_magic_open = dlsym(magic_handle, "magic_open");
59 dl_magic_file = dlsym(magic_handle, "magic_file");
60 dl_magic_load = dlsym(magic_handle, "magic_load");
61 dl_magic_close = dlsym(magic_handle, "magic_close");
62 }
63
64 if (!dl_magic_open || !dl_magic_file ||
65 !dl_magic_load || !dl_magic_close)
66 return 0;
67 return 1;
68 }
69 #else
70 static int magic_library_available(void)
71 {
72 dl_magic_open = magic_open;
73 dl_magic_file = magic_file;
74 dl_magic_load = magic_load;
75 dl_magic_close = magic_close;
76
77 return 1;
78 }
79 #endif
80 #endif
81
82 static void print_ext2_info(const char *device)
83
84 {
85 struct ext2_super_block *sb;
86 ext2_filsys fs;
87 errcode_t retval;
88 time_t tm;
89 char buf[80];
90
91 retval = ext2fs_open2(device, 0, EXT2_FLAG_64BITS, 0, 0,
92 unix_io_manager, &fs);
93 if (retval)
94 return;
95 sb = fs->super;
96
97 if (sb->s_mtime) {
98 tm = sb->s_mtime;
99 if (sb->s_last_mounted[0]) {
100 memset(buf, 0, sizeof(buf));
101 strncpy(buf, sb->s_last_mounted,
102 sizeof(sb->s_last_mounted));
103 printf(_("\tlast mounted on %s on %s"), buf,
104 ctime(&tm));
105 } else
106 printf(_("\tlast mounted on %s"), ctime(&tm));
107 } else if (sb->s_mkfs_time) {
108 tm = sb->s_mkfs_time;
109 printf(_("\tcreated on %s"), ctime(&tm));
110 } else if (sb->s_wtime) {
111 tm = sb->s_wtime;
112 printf(_("\tlast modified on %s"), ctime(&tm));
113 }
114 ext2fs_close_free(&fs);
115 }
116
117 /*
118 * return 1 if there is no partition table, 0 if a partition table is
119 * detected, and -1 on an error.
120 */
121 #ifdef HAVE_BLKID_PROBE_ENABLE_PARTITIONS
122 static int check_partition_table(const char *device)
123 {
124 blkid_probe pr;
125 const char *value;
126 int ret;
127
128 pr = blkid_new_probe_from_filename(device);
129 if (!pr)
130 return -1;
131
132 ret = blkid_probe_enable_partitions(pr, 1);
133 if (ret < 0)
134 goto errout;
135
136 ret = blkid_probe_enable_superblocks(pr, 0);
137 if (ret < 0)
138 goto errout;
139
140 ret = blkid_do_fullprobe(pr);
141 if (ret < 0)
142 goto errout;
143
144 ret = blkid_probe_lookup_value(pr, "PTTYPE", &value, NULL);
145 if (ret == 0)
146 fprintf(stderr, _("Found a %s partition table in %s\n"),
147 value, device);
148 else
149 ret = 1;
150
151 errout:
152 blkid_free_probe(pr);
153 return ret;
154 }
155 #else
156 static int check_partition_table(const char *device EXT2FS_ATTR((unused)))
157 {
158 return -1;
159 }
160 #endif
161
162 /*
163 * return 1 if the device looks plausible, creating the file if necessary
164 */
165 int check_plausibility(const char *device, int flags, int *ret_is_dev)
166 {
167 int fd, ret, is_dev = 0;
168 ext2fs_struct_stat s;
169 int fl = O_RDONLY;
170 blkid_cache cache = NULL;
171 char *fs_type = NULL;
172 char *fs_label = NULL;
173
174 fd = ext2fs_open_file(device, fl, 0666);
175 if ((fd < 0) && (errno == ENOENT) && (flags & NO_SIZE)) {
176 fprintf(stderr, _("The file %s does not exist and no "
177 "size was specified.\n"), device);
178 exit(1);
179 }
180 if ((fd < 0) && (errno == ENOENT) && (flags & CREATE_FILE)) {
181 fl |= O_CREAT;
182 fd = ext2fs_open_file(device, fl, 0666);
183 if (fd >= 0 && (flags & VERBOSE_CREATE))
184 printf(_("Creating regular file %s\n"), device);
185 }
186 if (fd < 0) {
187 fprintf(stderr, _("Could not open %s: %s\n"),
188 device, error_message(errno));
189 if (errno == ENOENT)
190 fputs(_("\nThe device apparently does not exist; "
191 "did you specify it correctly?\n"), stderr);
192 exit(1);
193 }
194
195 if (ext2fs_fstat(fd, &s) < 0) {
196 perror("stat");
197 exit(1);
198 }
199 close(fd);
200
201 if (S_ISBLK(s.st_mode))
202 is_dev = 1;
203 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
204 /* On FreeBSD, all disk devices are character specials */
205 if (S_ISCHR(s.st_mode))
206 is_dev = 1;
207 #endif
208 if (ret_is_dev)
209 *ret_is_dev = is_dev;
210
211 if ((flags & CHECK_BLOCK_DEV) && !is_dev) {
212 printf(_("%s is not a block special device.\n"), device);
213 return 0;
214 }
215
216 /*
217 * Note: we use the older-style blkid API's here because we
218 * want as much functionality to be available when using the
219 * internal blkid library, when e2fsprogs is compiled for
220 * non-Linux systems that will probably not have the libraries
221 * from util-linux available. We only use the newer
222 * blkid-probe interfaces to access functionality not
223 * available in the original blkid library.
224 */
225 if ((flags & CHECK_FS_EXIST) && blkid_get_cache(&cache, NULL) >= 0) {
226 fs_type = blkid_get_tag_value(cache, "TYPE", device);
227 if (fs_type)
228 fs_label = blkid_get_tag_value(cache, "LABEL", device);
229 blkid_put_cache(cache);
230 }
231
232 if (fs_type) {
233 if (fs_label)
234 printf(_("%s contains a %s file system "
235 "labelled '%s'\n"), device, fs_type, fs_label);
236 else
237 printf(_("%s contains a %s file system\n"), device,
238 fs_type);
239 if (strncmp(fs_type, "ext", 3) == 0)
240 print_ext2_info(device);
241 free(fs_type);
242 free(fs_label);
243 return 0;
244 }
245
246 #ifdef HAVE_MAGIC_H
247 if ((flags & CHECK_FS_EXIST) && magic_library_available()) {
248 const char *msg;
249 magic_t mag;
250 int has_magic = 0;
251
252 mag = dl_magic_open(MAGIC_RAW | MAGIC_SYMLINK | MAGIC_DEVICES |
253 MAGIC_ERROR | MAGIC_NO_CHECK_ELF |
254 MAGIC_NO_CHECK_COMPRESS);
255 dl_magic_load(mag, NULL);
256
257 msg = dl_magic_file(mag, device);
258 if (msg && strcmp(msg, "data") && strcmp(msg, "empty")) {
259 printf(_("%s contains `%s' data\n"), device, msg);
260 has_magic = 1;
261 }
262
263 dl_magic_close(mag);
264 return !has_magic;
265 }
266 #endif
267
268 ret = check_partition_table(device);
269 if (ret >= 0)
270 return ret;
271
272 return 1;
273 }
274