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 #ifdef HAVE_LINUX_MAJOR_H
22 #include <linux/major.h>
24 #include <sys/types.h>
25 #ifdef HAVE_SYS_STAT_H
34 #include "plausible.h"
35 #include "ext2fs/ext2fs.h"
36 #include "nls-enable.h"
37 #include "blkid/blkid.h"
40 static magic_t (*dl_magic_open)(int);
41 static const char *(*dl_magic_file)(magic_t, const char *);
42 static int (*dl_magic_load)(magic_t, const char *);
43 static void (*dl_magic_close)(magic_t);
48 static void *magic_handle;
50 static int magic_library_available(void)
53 magic_handle = dlopen("libmagic.so.1", RTLD_NOW);
57 dl_magic_open = dlsym(magic_handle, "magic_open");
58 dl_magic_file = dlsym(magic_handle, "magic_file");
59 dl_magic_load = dlsym(magic_handle, "magic_load");
60 dl_magic_close = dlsym(magic_handle, "magic_close");
63 if (!dl_magic_open || !dl_magic_file ||
64 !dl_magic_load || !dl_magic_close)
69 static int magic_library_available(void)
71 dl_magic_open = magic_open;
72 dl_magic_file = magic_file;
73 dl_magic_load = magic_load;
74 dl_magic_close = magic_close;
81 static void print_ext2_info(const char *device)
84 struct ext2_super_block *sb;
90 retval = ext2fs_open2(device, 0, EXT2_FLAG_64BITS, 0, 0,
91 unix_io_manager, &fs);
98 if (sb->s_last_mounted[0]) {
99 memset(buf, 0, sizeof(buf));
100 strncpy(buf, sb->s_last_mounted,
101 sizeof(sb->s_last_mounted));
102 printf(_("\tlast mounted on %s on %s"), buf,
105 printf(_("\tlast mounted on %s"), ctime(&tm));
106 } else if (sb->s_mkfs_time) {
107 tm = sb->s_mkfs_time;
108 printf(_("\tcreated on %s"), ctime(&tm));
109 } else if (sb->s_wtime) {
111 printf(_("\tlast modified on %s"), ctime(&tm));
113 ext2fs_close_free(&fs);
117 * return 1 if there is no partition table, 0 if a partition table is
118 * detected, and -1 on an error.
120 static int check_partition_table(const char *device)
122 #ifdef HAVE_BLKID_PROBE_ENABLE_PARTITIONS
127 pr = blkid_new_probe_from_filename(device);
131 ret = blkid_probe_enable_partitions(pr, 1);
135 ret = blkid_probe_enable_superblocks(pr, 0);
139 ret = blkid_do_fullprobe(pr);
143 ret = blkid_probe_lookup_value(pr, "PTTYPE", &value, NULL);
145 fprintf(stderr, _("Found a %s partition table in %s\n"),
151 blkid_free_probe(pr);
159 * return 1 if the device looks plausible, creating the file if necessary
161 int check_plausibility(const char *device, int flags, int *ret_is_dev)
163 int fd, ret, is_dev = 0;
164 ext2fs_struct_stat s;
166 blkid_cache cache = NULL;
167 char *fs_type = NULL;
168 char *fs_label = NULL;
170 fd = ext2fs_open_file(device, fl, 0666);
171 if ((fd < 0) && (errno == ENOENT) && (flags & NO_SIZE)) {
172 fprintf(stderr, _("The file %s does not exist and no "
173 "size was specified.\n"), device);
176 if ((fd < 0) && (errno == ENOENT) && (flags & CREATE_FILE)) {
178 fd = ext2fs_open_file(device, fl, 0666);
179 if (fd >= 0 && (flags & VERBOSE_CREATE))
180 printf(_("Creating regular file %s\n"), device);
183 fprintf(stderr, _("Could not open %s: %s\n"),
184 device, error_message(errno));
186 fputs(_("\nThe device apparently does not exist; "
187 "did you specify it correctly?\n"), stderr);
191 if (ext2fs_fstat(fd, &s) < 0) {
197 if (S_ISBLK(s.st_mode))
199 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
200 /* On FreeBSD, all disk devices are character specials */
201 if (S_ISCHR(s.st_mode))
205 *ret_is_dev = is_dev;
207 if ((flags & CHECK_BLOCK_DEV) && !is_dev) {
208 printf(_("%s is not a block special device.\n"), device);
213 * Note: we use the older-style blkid API's here because we
214 * want as much functionality to be available when using the
215 * internal blkid library, when e2fsprogs is compiled for
216 * non-Linux systems that will probably not have the libraries
217 * from util-linux available. We only use the newer
218 * blkid-probe interfaces to access functionality not
219 * available in the original blkid library.
221 if ((flags & CHECK_FS_EXIST) && blkid_get_cache(&cache, NULL) >= 0) {
222 fs_type = blkid_get_tag_value(cache, "TYPE", device);
224 fs_label = blkid_get_tag_value(cache, "LABEL", device);
225 blkid_put_cache(cache);
230 printf(_("%s contains a %s file system "
231 "labelled '%s'\n"), device, fs_type, fs_label);
233 printf(_("%s contains a %s file system\n"), device,
235 if (strncmp(fs_type, "ext", 3) == 0)
236 print_ext2_info(device);
243 if ((flags & CHECK_FS_EXIST) && magic_library_available()) {
248 mag = dl_magic_open(MAGIC_RAW | MAGIC_SYMLINK | MAGIC_DEVICES |
249 MAGIC_ERROR | MAGIC_NO_CHECK_ELF |
250 MAGIC_NO_CHECK_COMPRESS);
251 dl_magic_load(mag, NULL);
253 msg = dl_magic_file(mag, device);
254 if (msg && strcmp(msg, "data") && strcmp(msg, "empty")) {
255 printf(_("%s contains `%s' data\n"), device, msg);
264 ret = check_partition_table(device);
268 #ifdef HAVE_LINUX_MAJOR_H
270 #define MAJOR(dev) ((dev)>>8)
271 #define MINOR(dev) ((dev) & 0xff)
273 #ifndef SCSI_BLK_MAJOR
274 #ifdef SCSI_DISK0_MAJOR
275 #ifdef SCSI_DISK8_MAJOR
276 #define SCSI_DISK_MAJOR(M) ((M) == SCSI_DISK0_MAJOR || \
277 ((M) >= SCSI_DISK1_MAJOR && (M) <= SCSI_DISK7_MAJOR) || \
278 ((M) >= SCSI_DISK8_MAJOR && (M) <= SCSI_DISK15_MAJOR))
280 #define SCSI_DISK_MAJOR(M) ((M) == SCSI_DISK0_MAJOR || \
281 ((M) >= SCSI_DISK1_MAJOR && (M) <= SCSI_DISK7_MAJOR))
282 #endif /* defined(SCSI_DISK8_MAJOR) */
283 #define SCSI_BLK_MAJOR(M) (SCSI_DISK_MAJOR((M)) || (M) == SCSI_CDROM_MAJOR)
285 #define SCSI_BLK_MAJOR(M) ((M) == SCSI_DISK_MAJOR || (M) == SCSI_CDROM_MAJOR)
286 #endif /* defined(SCSI_DISK0_MAJOR) */
287 #endif /* defined(SCSI_BLK_MAJOR) */
288 if (((MAJOR(s.st_rdev) == HD_MAJOR &&
289 MINOR(s.st_rdev)%64 == 0) ||
290 (SCSI_BLK_MAJOR(MAJOR(s.st_rdev)) &&
291 MINOR(s.st_rdev)%16 == 0))) {
292 printf(_("%s is entire device, not just one partition!\n"),