blkid_probe
blkid_free_probe
blkid_new_probe
+blkid_new_probe_from_filename
blkid_probe_get_devno
blkid_probe_get_sectorsize
blkid_probe_get_size
int main(int argc, char *argv[])
{
- int fd, rc;
+ int rc;
char *devname;
blkid_probe pr;
blkid_topology tp;
}
devname = argv[1];
-
- fd = open(devname, O_RDONLY);
- if (fd < 0)
- errx(EXIT_FAILURE, "%s: open() failed", devname);
-
- pr = blkid_new_probe();
+ pr = blkid_new_probe_from_filename(devname);
if (!pr)
- errx(EXIT_FAILURE, "faild to allocate a new libblkid probe");
-
- if (blkid_probe_set_device(pr, fd, 0, 0) != 0)
- errx(EXIT_FAILURE, "failed to assign device to libblkid probe");
+ err(EXIT_FAILURE, "%s: faild to create a new libblkid probe",
+ devname);
/*
* check Filesystems / Partitions overwrite
int main(int argc, char *argv[])
{
- int fd, i, nparts;
+ int i, nparts;
char *devname;
blkid_probe pr;
blkid_partlist ls;
}
devname = argv[1];
-
- fd = open(devname, O_RDONLY);
- if (fd < 0)
- err(EXIT_FAILURE, "%s: open() failed", devname);
-
- pr = blkid_new_probe();
+ pr = blkid_new_probe_from_filename(devname);
if (!pr)
- errx(EXIT_FAILURE, "failed to allocate a new libblkid probe");
-
- if (blkid_probe_set_device(pr, fd, 0, 0) != 0)
- errx(EXIT_FAILURE, "failed to assign device to libblkid probe");
-
+ err(EXIT_FAILURE, "%s: faild to create a new libblkid probe",
+ devname);
/* Binary interface */
ls = blkid_probe_get_partitions(pr);
if (!ls)
int main(int argc, char *argv[])
{
- int fd, rc;
+ int rc;
char *devname;
blkid_probe pr;
blkid_topology tp;
}
devname = argv[1];
-
- fd = open(devname, O_RDONLY);
- if (fd < 0)
- err(EXIT_FAILURE, "%s: open() failed", devname);
-
- pr = blkid_new_probe();
+ pr = blkid_new_probe_from_filename(devname);
if (!pr)
- errx(EXIT_FAILURE, "faild to allocate a new libblkid probe");
-
- if (blkid_probe_set_device(pr, fd, 0, 0) != 0)
- errx(EXIT_FAILURE, "failed to assign device to libblkid probe");
-
+ err(EXIT_FAILURE, "%s: faild to create a new libblkid probe",
+ devname);
/*
* Binary interface
*/
/* probe.c */
extern blkid_probe blkid_new_probe(void);
+extern blkid_probe blkid_new_probe_from_filename(const char *filename);
extern void blkid_free_probe(blkid_probe pr);
extern void blkid_reset_probe(blkid_probe pr);
blkid_devno_to_wholedisk;
blkid_do_fullprobe;
blkid_known_pttype;
+ blkid_new_probe_from_filename;
blkid_partition_get_name;
blkid_partition_get_partno;
blkid_partition_get_size;
int fd; /* device file descriptor */
blkid_loff_t off; /* begin of data on the device */
blkid_loff_t size; /* end of data on the device */
+
dev_t devno; /* device number (st.st_rdev) */
unsigned int blkssz; /* sector size (BLKSSZGET ioctl) */
mode_t mode; /* struct stat.sb_mode */
+ int flags; /* private libray flags */
+
unsigned char *sbbuf; /* superblok buffer */
size_t sbbuf_len; /* size of data in superblock buffer */
struct blkid_prval vals[BLKID_NVALS]; /* results */
int nvals; /* number of assigned vals */
-
- /* obsolete */
- int probreq; /* BLKID_PROBREQ_* flags */
- int idx; /* index of the last prober */
- unsigned long *fltr; /* filter */
};
+/* flags */
+#define BLKID_PRIVATE_FD (1 << 1) /* see blkid_new_probe_from_filename() */
+
/*
* Evaluation methods (for blkid_eval_* API)
*/
return pr;
}
+/**
+ * blkid_new_probe_from_filename:
+ * @filename: device or regular file
+ *
+ * This function is same as call open(filename), blkid_new_probe() and
+ * blkid_probe_set_device(pr, fd, 0, 0).
+ *
+ * The @filename is closed by blkid_free_probe() or by the
+ * blkid_probe_set_device() call.
+ *
+ * Returns: a pointer to the newly allocated probe struct or NULL in case of
+ * error.
+ */
+blkid_probe blkid_new_probe_from_filename(const char *filename)
+{
+ int fd = -1;
+ blkid_probe pr = NULL;
+
+ if (!filename)
+ return NULL;
+
+ fd = open(filename, O_RDONLY);
+ if (fd < 0)
+ return NULL;
+
+ pr = blkid_new_probe();
+ if (!pr)
+ goto err;
+
+ if (blkid_probe_set_device(pr, fd, 0, 0))
+ goto err;
+
+ pr->flags |= BLKID_PRIVATE_FD;
+ return pr;
+err:
+ if (fd >= 0)
+ close(fd);
+ blkid_free_probe(pr);
+ return NULL;
+}
+
/**
* blkid_free_probe:
* @pr: probe
}
free(pr->buf);
free(pr->sbbuf);
+
+ if ((pr->flags & BLKID_PRIVATE_FD) && pr->fd >= 0)
+ close(pr->fd);
free(pr);
}
* @pr: probe
* @fd: device file descriptor
* @off: begin of probing area
- * @size: size of probing area
+ * @size: size of probing area (zero means whole device/file)
*
* Assigns the device to probe control struct, resets internal buffers and
* reads 512 bytes from device to the buffers.
blkid_reset_probe(pr);
+ if ((pr->flags & BLKID_PRIVATE_FD) && pr->fd >= 0)
+ close(pr->fd);
+
+ pr->flags &= ~BLKID_PRIVATE_FD;
pr->fd = fd;
pr->off = off;
pr->size = 0;