int mountdevice(const char *devnode){ /*Using device's MBR information to retrieve partitions and its types * to mount it correctly*/ blkid_probe pr; /*BLKID probe struct*/ blkid_partlist pl; /*BLKID partitions list*/ int nparts, i; char *pname = (char*)malloc(32 * sizeof(char)); char *mountpoint = (char*)malloc(128 * sizeof(char)); pr = blkid_new_probe_from_filename(devnode); /*Starts blkid probe*/ if (!pr){ fprintf(stderr, "Error on opening device: %s - %s\n", devnode, strerror(errno)); return -1; } if (!pname || !mountpoint){ fprintf(stderr, "Not able to allocate memory, will not mount anything\n"); return -1; } pl = blkid_probe_get_partitions(pr); /*Getting partition list*/ nparts = blkid_partlist_numof_partitions(pl); /*Getting the number of partitions*/ for (i = 0; i < nparts; i++){ char *pnametmp; /*temp variable to be tokenized by udev_get_partition*/ char *ptype, *cmd; int t_part, partno, mstatus; struct stat st = {0}; blkid_partition part = blkid_partlist_get_partition(pl, i); t_part = blkid_partition_get_type(part); partno = blkid_partition_get_partno(part); fprintf(stdout, "Partition: %d, type: 0x%x\n", partno, t_part); sprintf(pname, "%s%d", devnode, partno); pnametmp = (char*)malloc(strlen(pname) * sizeof(char)); if (!pnametmp){ fprintf(stderr, "Could not allocate memory, will not mount anything\n"); return -1; } strcpy(pnametmp, pname); sprintf(mountpoint, "/media/USBSTICK%s", udev_get_partition(pnametmp)); /*Freeing temporary variable*/ free(pnametmp); fprintf(stdout, "Preparing to mount: %s\n", pname); if (stat(mountpoint, &st) == -1){ fprintf(stdout, "Creating dir: %s\n", mountpoint); /*create a new dir*/ if(mkdir(mountpoint, 0777) == -1){ fprintf(stderr, "Unable to create: %s. Error: %s\n", mountpoint, strerror(errno)); return -1; }else{ fprintf(stdout, "%s create successfully.\n", mountpoint); } } fprintf(stdout, "Mounting %s on %s\n", pname, mountpoint); ptype = getpartitiontype(t_part); mstatus = mount(pname, mountpoint, ptype, MS_SYNCHRONOUS, ""); if (mstatus == -1){ cmd = (char*)malloc(SLENGHT * sizeof(char)); fprintf(stderr, "Error mounting [%s]: %s\n", pname, strerror(errno)); sprintf(cmd, "DISPLAY=:0 zenity --notification --text\"Não foi possivel montar\ %s\" --timeout=2", pname); if(fork() == 0) system(cmd); else wait(NULL); free(cmd); return -1; }else{
int main(int argc, char *argv[]) { int i, nparts; char *devname; blkid_probe pr; blkid_partlist ls; blkid_parttable root_tab; if (argc < 2) { fprintf(stderr, "usage: %s <device|file> " "-- prints partitions\n", program_invocation_short_name); return EXIT_FAILURE; } devname = argv[1]; pr = blkid_new_probe_from_filename(devname); if (!pr) err(EXIT_FAILURE, "%s: faild to create a new libblkid probe", devname); /* Binary interface */ ls = blkid_probe_get_partitions(pr); if (!ls) errx(EXIT_FAILURE, "%s: failed to read partitions\n", devname); /* * Print info about the primary (root) partition table */ root_tab = blkid_partlist_get_table(ls); if (!root_tab) errx(EXIT_FAILURE, "%s: does not contains any " "known partition table\n", devname); printf("size: %jd, sector size: %u, PT: %s, offset: %jd\n---\n", blkid_probe_get_size(pr), blkid_probe_get_sectorsize(pr), blkid_parttable_get_type(root_tab), blkid_parttable_get_offset(root_tab)); /* * List partitions */ nparts = blkid_partlist_numof_partitions(ls); if (!nparts) goto done; for (i = 0; i < nparts; i++) { const char *p; blkid_partition par = blkid_partlist_get_partition(ls, i); blkid_parttable tab = blkid_partition_get_table(par); printf("#%d: %10llu %10llu 0x%x", blkid_partition_get_partno(par), (unsigned long long) blkid_partition_get_start(par), (unsigned long long) blkid_partition_get_size(par), blkid_partition_get_type(par)); if (root_tab != tab) /* subpartition (BSD, Minix, ...) */ printf(" (%s)", blkid_parttable_get_type(tab)); p = blkid_partition_get_name(par); if (p) printf(" name='%s'", p); p = blkid_partition_get_uuid(par); if (p) printf(" uuid='%s'", p); p = blkid_partition_get_type_string(par); if (p) printf(" type='%s'", p); putc('\n', stdout); } done: blkid_free_probe(pr); return EXIT_SUCCESS; }
static int probe_bsd_pt(blkid_probe pr, const struct blkid_idmag *mag) { struct bsd_disklabel *l; struct bsd_partition *p; const char *name = "bsd" ; blkid_parttable tab = NULL; blkid_partition parent; blkid_partlist ls; int i, nparts = BSD_MAXPARTITIONS; unsigned char *data; int rc = BLKID_PROBE_NONE; if (blkid_partitions_need_typeonly(pr)) /* caller does not ask for details about partitions */ return rc; data = blkid_probe_get_sector(pr, BLKID_MAG_SECTOR(mag)); if (!data) { if (errno) rc = -errno; goto nothing; } l = (struct bsd_disklabel *) data + BLKID_MAG_LASTOFFSET(mag); ls = blkid_probe_get_partlist(pr); if (!ls) goto nothing; /* try to determine the real type of BSD system according to * (parental) primary partition */ parent = blkid_partlist_get_parent(ls); if (parent) { switch(blkid_partition_get_type(parent)) { case MBR_FREEBSD_PARTITION: name = "freebsd"; break; case MBR_NETBSD_PARTITION: name = "netbsd"; break; case MBR_OPENBSD_PARTITION: name = "openbsd"; break; default: DBG(LOWPROBE, ul_debug( "WARNING: BSD label detected on unknown (0x%x) " "primary partition", blkid_partition_get_type(parent))); break; } } tab = blkid_partlist_new_parttable(ls, name, BLKID_MAG_OFFSET(mag)); if (!tab) { rc = -ENOMEM; goto nothing; } if (le16_to_cpu(l->d_npartitions) < BSD_MAXPARTITIONS) nparts = le16_to_cpu(l->d_npartitions); else if (le16_to_cpu(l->d_npartitions) > BSD_MAXPARTITIONS) DBG(LOWPROBE, ul_debug( "WARNING: ignore %d more BSD partitions", le16_to_cpu(l->d_npartitions) - BSD_MAXPARTITIONS)); for (i = 0, p = l->d_partitions; i < nparts; i++, p++) { blkid_partition par; uint32_t start, size; /* TODO: in fdisk-mode returns all non-zero (p_size) partitions */ if (p->p_fstype == BSD_FS_UNUSED) continue; start = le32_to_cpu(p->p_offset); size = le32_to_cpu(p->p_size); if (parent && blkid_partition_get_start(parent) == start && blkid_partition_get_size(parent) == size) { DBG(LOWPROBE, ul_debug( "WARNING: BSD partition (%d) same like parent, " "ignore", i)); continue; } if (parent && !blkid_is_nested_dimension(parent, start, size)) { DBG(LOWPROBE, ul_debug( "WARNING: BSD partition (%d) overflow " "detected, ignore", i)); continue; } par = blkid_partlist_add_partition(ls, tab, start, size); if (!par) { rc = -ENOMEM; goto nothing; } blkid_partition_set_type(par, p->p_fstype); } return BLKID_PROBE_OK; nothing: return rc; }