2 * plausible.c --- Figure out if a pathname is ext* or something else.
4 * Copyright 2014, Oracle, Inc.
7 * Copyright 1995, 1996, 1997, 1998, 1999, 2000 by Theodore Ts'o.
10 * This file may be redistributed under the terms of the GNU Public
15 #define _LARGEFILE_SOURCE
16 #define _LARGEFILE64_SOURCE
21 #include <sys/types.h>
22 #ifdef HAVE_SYS_STAT_H
31 #include "plausible.h"
32 #include "ext2fs/ext2fs.h"
33 #include "nls-enable.h"
34 #include "blkid/blkid.h"
37 static magic_t (*dl_magic_open)(int);
38 static const char *(*dl_magic_file)(magic_t, const char *);
39 static int (*dl_magic_load)(magic_t, const char *);
40 static void (*dl_magic_close)(magic_t);
45 static void *magic_handle;
47 static int magic_library_available(void)
50 magic_handle = dlopen("libmagic.so.1", RTLD_NOW);
54 dl_magic_open = dlsym(magic_handle, "magic_open");
55 dl_magic_file = dlsym(magic_handle, "magic_file");
56 dl_magic_load = dlsym(magic_handle, "magic_load");
57 dl_magic_close = dlsym(magic_handle, "magic_close");
60 if (!dl_magic_open || !dl_magic_file ||
61 !dl_magic_load || !dl_magic_close)
66 static int magic_library_available(void)
68 dl_magic_open = magic_open;
69 dl_magic_file = magic_file;
70 dl_magic_load = magic_load;
71 dl_magic_close = magic_close;
78 static void print_ext2_info(const char *device)
81 struct ext2_super_block *sb;
87 retval = ext2fs_open2(device, 0, EXT2_FLAG_64BITS, 0, 0,
88 unix_io_manager, &fs);
95 if (sb->s_last_mounted[0]) {
96 memset(buf, 0, sizeof(buf));
97 strncpy(buf, sb->s_last_mounted,
98 sizeof(sb->s_last_mounted));
99 printf(_("\tlast mounted on %s on %s"), buf,
102 printf(_("\tlast mounted on %s"), ctime(&tm));
103 } else if (sb->s_mkfs_time) {
104 tm = sb->s_mkfs_time;
105 printf(_("\tcreated on %s"), ctime(&tm));
106 } else if (sb->s_wtime) {
108 printf(_("\tlast modified on %s"), ctime(&tm));
110 ext2fs_close_free(&fs);
114 * return 1 if there is no partition table, 0 if a partition table is
115 * detected, and -1 on an error.
117 static int check_partition_table(const char *device)
119 #ifdef HAVE_BLKID_PROBE_ENABLE_PARTITIONS
124 pr = blkid_new_probe_from_filename(device);
128 ret = blkid_probe_enable_partitions(pr, 1);
132 ret = blkid_probe_enable_superblocks(pr, 0);
136 ret = blkid_do_fullprobe(pr);
140 ret = blkid_probe_lookup_value(pr, "PTTYPE", &value, NULL);
142 fprintf(stderr, _("Found a %s partition table in %s\n"),
148 blkid_free_probe(pr);
156 * return 1 if the device looks plausible, creating the file if necessary
158 int check_plausibility(const char *device, int flags, int *ret_is_dev)
160 int fd, ret, is_dev = 0;
161 ext2fs_struct_stat s;
163 blkid_cache cache = NULL;
164 char *fs_type = NULL;
165 char *fs_label = NULL;
167 fd = ext2fs_open_file(device, fl, 0666);
168 if ((fd < 0) && (errno == ENOENT) && (flags & NO_SIZE)) {
169 fprintf(stderr, _("The file %s does not exist and no "
170 "size was specified.\n"), device);
173 if ((fd < 0) && (errno == ENOENT) && (flags & CREATE_FILE)) {
175 fd = ext2fs_open_file(device, fl, 0666);
176 if (fd >= 0 && (flags & VERBOSE_CREATE))
177 printf(_("Creating regular file %s\n"), device);
180 fprintf(stderr, _("Could not open %s: %s\n"),
181 device, error_message(errno));
183 fputs(_("\nThe device apparently does not exist; "
184 "did you specify it correctly?\n"), stderr);
188 if (ext2fs_fstat(fd, &s) < 0) {
194 if (S_ISBLK(s.st_mode))
196 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
197 /* On FreeBSD, all disk devices are character specials */
198 if (S_ISCHR(s.st_mode))
202 *ret_is_dev = is_dev;
204 if ((flags & CHECK_BLOCK_DEV) && !is_dev) {
205 printf(_("%s is not a block special device.\n"), device);
210 * Note: we use the older-style blkid API's here because we
211 * want as much functionality to be available when using the
212 * internal blkid library, when e2fsprogs is compiled for
213 * non-Linux systems that will probably not have the libraries
214 * from util-linux available. We only use the newer
215 * blkid-probe interfaces to access functionality not
216 * available in the original blkid library.
218 if ((flags & CHECK_FS_EXIST) && blkid_get_cache(&cache, NULL) >= 0) {
219 fs_type = blkid_get_tag_value(cache, "TYPE", device);
221 fs_label = blkid_get_tag_value(cache, "LABEL", device);
222 blkid_put_cache(cache);
227 printf(_("%s contains a %s file system "
228 "labelled '%s'\n"), device, fs_type, fs_label);
230 printf(_("%s contains a %s file system\n"), device,
232 if (strncmp(fs_type, "ext", 3) == 0)
233 print_ext2_info(device);
240 if ((flags & CHECK_FS_EXIST) && magic_library_available()) {
245 mag = dl_magic_open(MAGIC_RAW | MAGIC_SYMLINK | MAGIC_DEVICES |
246 MAGIC_ERROR | MAGIC_NO_CHECK_ELF |
247 MAGIC_NO_CHECK_COMPRESS);
248 dl_magic_load(mag, NULL);
250 msg = dl_magic_file(mag, device);
251 if (msg && strcmp(msg, "data") && strcmp(msg, "empty")) {
252 printf(_("%s contains `%s' data\n"), device, msg);
261 ret = check_partition_table(device);