Esempio n. 1
0
DiskPartitionInfo *
disk_partitions()
{
  FILE *file = NULL;
  struct mntent *entry;
  int nparts = 5;
  DiskPartition *partitions = (DiskPartition *)calloc(nparts, sizeof(DiskPartition));
  DiskPartitionInfo *ret = (DiskPartitionInfo *)calloc(1, sizeof(DiskPartitionInfo));
  DiskPartition *d = partitions;
  check_mem(partitions);
  check_mem(ret);

  ret->nitems = 0;
  ret->partitions = partitions;

  file = setmntent(MOUNTED, "r");
  check(file, "Couldn't open %s", MOUNTED);

  while ((entry = getmntent(file))) {
    d->device  = strdup(entry->mnt_fsname);
    d->mountpoint = strdup(entry->mnt_dir);
    d->fstype = strdup(entry->mnt_type);
    d->opts = strdup(entry->mnt_opts);

    ret->nitems ++;
    d++;

    if (ret->nitems == nparts) {
      nparts *= 2;
      partitions = realloc(partitions, sizeof(DiskPartition) * nparts);
      check_mem(partitions);
      ret->partitions = partitions;
      d = ret->partitions + ret->nitems; /* Move the cursor to the correct
                                            value in case the realloc moved
                                            the memory */
    }
  }
  endmntent(file);
  return ret;

 error:
  if (file)
    endmntent(file);
  free_disk_partition_info(ret);
  return NULL;
}
Esempio n. 2
0
DiskPartitionInfo*
disk_partitions_phys()
{
  FILE *fs = fopen("/proc/filesystems", "r");
  check(fs, "Couldn't open /proc/filesystems");
  char line[50];
  int i, j;

  int nparts = 5;
  DiskPartition *partitions = calloc(nparts, sizeof(DiskPartition));
  DiskPartitionInfo *ret = malloc(sizeof(DiskPartitionInfo));
  DiskPartition *d = partitions;
  check_mem(partitions);
  check_mem(ret);

  ret->nitems = 0;
  ret->partitions = partitions;

  int devs = 0;
  char *phydevs[100];
  while (fgets(line, 50, fs) != NULL) {
    if (strncmp(line, "nodev", 5) != 0) {
      phydevs[devs] = calloc(100, sizeof(char));
      int start = 0, end = strlen(line)-1;
      while (isspace(line[start])) start++;
      while (isspace(line[end])) end--;
      strncpy(phydevs[devs], line+start, (end+1)-start);

      devs++;
    }
  }
  fclose(fs);

  DiskPartitionInfo *tmp = disk_partitions();
  check(tmp, "disk_partitions failed");

  for (i = 0;i < tmp->nitems; i++) {
    DiskPartition *p = tmp->partitions + i;

    bool nodev = true;
    for(j = 0;j < devs; j++) {
      if(strcmp(phydevs[j], p->fstype) == 0)
	nodev = false;
    }
    if(strlen(p->device) == 0 || nodev) {
      continue;
    }

    *d = *p;
    d->device = strdup(p->device);
    d->mountpoint = strdup(p->mountpoint);
    d->fstype = strdup(p->fstype);
    d->opts = strdup(p->opts);

    ret->nitems ++;
    d++;

    if (ret->nitems == nparts) {
      nparts *= 2;
      partitions = realloc(partitions, sizeof(DiskPartition) * nparts);
      check_mem(partitions);
      ret->partitions = partitions;
      d = ret->partitions + ret->nitems;
    }
  }
  free_disk_partition_info(tmp);
  return ret;

error:
    free_disk_partition_info(tmp);
    return NULL;
}
Esempio n. 3
0
DiskPartitionInfo *disk_partitions(bool physical) {
  FILE *file = NULL;
  struct mntent *entry;
  uint32_t nparts = 5;
  char **phys_devices = NULL;
  size_t nphys_devices;
  unsigned int i;

  DiskPartition *partitions =
      (DiskPartition *)calloc(nparts, sizeof(DiskPartition));
  DiskPartitionInfo *ret =
      (DiskPartitionInfo *)calloc(1, sizeof(DiskPartitionInfo));
  DiskPartition *d = partitions;
  check_mem(partitions);
  check_mem(ret);

  ret->nitems = 0;
  ret->partitions = partitions;

  file = setmntent(MOUNTED, "r");
  check(file, "Couldn't open %s", MOUNTED);

  phys_devices = get_physical_devices(&nphys_devices);

  while ((entry = getmntent(file))) {
    if (physical &&
        !lfind(&entry->mnt_type, phys_devices, &nphys_devices, sizeof(char *),
               str_comp)) {
      /* Skip this partition since we only need physical partitions */
      continue;
    }
    d->device = strdup(entry->mnt_fsname);
    d->mountpoint = strdup(entry->mnt_dir);
    d->fstype = strdup(entry->mnt_type);
    d->opts = strdup(entry->mnt_opts);

    ret->nitems++;
    d++;

    if (ret->nitems == nparts) {
      nparts *= 2;
      partitions =
          (DiskPartition *)realloc(partitions, sizeof(DiskPartition) * nparts);
      check_mem(partitions);
      ret->partitions = partitions;
      d = ret->partitions + ret->nitems; /* Move the cursor to the correct
                                            value in case the realloc moved
                                            the memory */
    }
  }
  endmntent(file);

  for (i = 0; i < nphys_devices; i++) {
    free(phys_devices[i]);
  }
  free(phys_devices);

  return ret;

error:
  if (file)
    endmntent(file);
  free_disk_partition_info(ret);
  return NULL;
}