コード例 #1
0
ファイル: device-info.c プロジェクト: paulcbetts/gformat
gchar*
get_friendly_drive_name(LibHalDrive* drive)
{
	const char* ret;

	/* Getting the name is tricky because we never know what info we have
	 * so we have to fall-back all the time */
	ret = libhal_drive_get_model(drive);
	if(ret && ret[0] != 0)	goto out;

	ret = libhal_drive_get_vendor(drive);
	if(ret && ret[0] != 0)	goto out;

	ret = libhal_drive_get_device_file(drive);
	if(ret && ret[0] != 0)	goto out;

	ret = _("(Unknown Device)");

out:
	return g_strdup(ret);
}
コード例 #2
0
ファイル: device-info.c プロジェクト: paulcbetts/gformat
gboolean
set_partition_type(LibHalDrive* drive, int partition, int msdos_type)
{
	const char* dev = libhal_drive_get_device_file(drive);
	if(!dev) 	return FALSE;

	PartitionTable* table = part_table_load_from_disk(dev);
	if(!table) 	return FALSE;
	int entries = part_table_get_num_entries(table);
	if(entries < partition) 	return FALSE;
	PartitionScheme scheme = part_table_get_scheme(table);

	gboolean ret;
	guint64 start, size, dontcare;
	char* type;
	start = part_table_entry_get_offset(table, partition);
	size = part_table_entry_get_size(table, partition);
	type = get_parted_type_string(msdos_type, scheme);
	
	ret = part_change_partition(dev, start, start, size, &dontcare, &dontcare, 
				type, NULL, NULL, 0, 0);
	g_free(type);
	return ret;
}
コード例 #3
0
ファイル: device-info.c プロジェクト: paulcbetts/gformat
gboolean
write_partition_table_for_device(LibHalDrive* drive, PartitionScheme scheme, GError** error)
{
	char* name;
	const char* msg;
	g_assert(drive);

	char* dev = libhal_drive_get_device_file(drive);
	g_assert(dev);
	if(!dev)	return FALSE;

	/* Create a new table first */
	if(!part_create_partition_table(dev, scheme)) {
		msg =  _("Cannot create partition table on %s");
		goto error_out;
	}

	/* Create one partition in this table; we start the FS at sector
	 * 63 */
	const int start = 512*63;
	guint64 size = (guint64)libhal_drive_get_size(drive);
	if(size == 0) {
		/* Try to get the media size - even for USB disks we have to do this */
		size = (guint64)libhal_drive_get_media_size(drive);
	}
	if(size < start) {
		msg = _("Cannot create partition table on %s");
		goto error_out;
	}

	/* This doesn't matter, we're going to reset it later;
	 * we just need something to give to part_add_partition */
	const char* type;
	switch(scheme) {
	case PART_TYPE_GPT:
		type = "{EBD0A0A2-B9E5-4433-87C0-68B6B72699C7}"; /* Linux Data */
		break;
	case PART_TYPE_APPLE:
		type = "DOS_FAT_32";
		break;
	default:
		type = "0x83";
	}

	guint64 dontcare;
	if(!part_add_partition(dev, start, size - start, &dontcare, &dontcare, 
			       (char*)type, NULL, NULL, 0, 0)) {
		msg = _("Cannot add new partition on %s");
		goto error_out;
	}

	if(!repoll_partition_table(dev)) {
		msg = _("The kernel cannot repoll the partition table on %s. "
			"Please reboot the computer or reinsert this disk if it "
			"is removable and try again.");
		goto error_out;
	}

	return TRUE;

error_out:
	name = get_friendly_drive_name(drive);
	g_set_error(error, 0, 0, _("Cannot create partition table on %s"), name);
	g_free(name);
	return FALSE;
}
static void
thunar_vfs_volume_hal_update (ThunarVfsVolumeHal *volume_hal,
                              LibHalContext      *context,
                              LibHalVolume       *hv,
                              LibHalDrive        *hd)
{
  gchar *desired_mount_point;
  gchar *mount_root;
  gchar *basename;
  gchar *filename;

  _thunar_vfs_return_if_fail (THUNAR_VFS_IS_VOLUME_HAL (volume_hal));
  _thunar_vfs_return_if_fail (hd != NULL);

  /* reset the volume status */
  volume_hal->status = 0;

  /* determine the new device file */
  g_free (volume_hal->device_file);
  volume_hal->device_file = g_strdup ((hv != NULL) ? libhal_volume_get_device_file (hv) : libhal_drive_get_device_file (hd));

  /* compute a usable display name for the volume/drive */
  g_free (volume_hal->device_label);
  volume_hal->device_label = (hv == NULL)
    ? exo_hal_drive_compute_display_name (context, hd)
    : exo_hal_volume_compute_display_name (context, hv, hd);
  if (G_UNLIKELY (volume_hal->device_label == NULL))
    {
      /* use the basename of the device file as label */
      volume_hal->device_label = g_path_get_basename (volume_hal->device_file);
    }

  /* compute a usable list of icon names for the volume/drive */
  g_list_foreach (volume_hal->icon_list, (GFunc) g_free, NULL);
  g_list_free (volume_hal->icon_list);
  volume_hal->icon_list = (hv == NULL)
    ? exo_hal_drive_compute_icon_list (context, hd)
    : exo_hal_volume_compute_icon_list (context, hv, hd);

  /* release the previous mount point (if any) */
  if (G_LIKELY (volume_hal->mount_point != NULL))
    {
      thunar_vfs_path_unref (volume_hal->mount_point);
      volume_hal->mount_point = NULL;
    }

  /* determine the type of the volume */
  switch (libhal_drive_get_type (hd))
    {
    case LIBHAL_DRIVE_TYPE_CDROM:
      /* check if we have a pure audio CD without any data track */
      if (libhal_volume_disc_has_audio (hv) && !libhal_volume_disc_has_data (hv))
        {
          /* special treatment for pure audio CDs */
          volume_hal->kind = THUNAR_VFS_VOLUME_KIND_AUDIO_CD;
        }
      else
        {
          /* check which kind of CD-ROM/DVD we have */
          switch (libhal_volume_get_disc_type (hv))
            {
            case LIBHAL_VOLUME_DISC_TYPE_CDROM:
              volume_hal->kind = THUNAR_VFS_VOLUME_KIND_CDROM;
              break;

            case LIBHAL_VOLUME_DISC_TYPE_CDR:
              volume_hal->kind = THUNAR_VFS_VOLUME_KIND_CDR;
              break;

            case LIBHAL_VOLUME_DISC_TYPE_CDRW:
              volume_hal->kind = THUNAR_VFS_VOLUME_KIND_CDRW;
              break;

            case LIBHAL_VOLUME_DISC_TYPE_DVDROM:
              volume_hal->kind = THUNAR_VFS_VOLUME_KIND_DVDROM;
              break;

            case LIBHAL_VOLUME_DISC_TYPE_DVDRAM:
              volume_hal->kind = THUNAR_VFS_VOLUME_KIND_DVDRAM;
              break;

            case LIBHAL_VOLUME_DISC_TYPE_DVDR:
              volume_hal->kind = THUNAR_VFS_VOLUME_KIND_DVDR;
              break;

            case LIBHAL_VOLUME_DISC_TYPE_DVDRW:
              volume_hal->kind = THUNAR_VFS_VOLUME_KIND_DVDRW;
              break;

            case LIBHAL_VOLUME_DISC_TYPE_DVDPLUSR:
              volume_hal->kind = THUNAR_VFS_VOLUME_KIND_DVDPLUSR;
              break;

            case LIBHAL_VOLUME_DISC_TYPE_DVDPLUSRW:
              volume_hal->kind = THUNAR_VFS_VOLUME_KIND_DVDPLUSRW;
              break;

            default:
              /* unsupported disc type */
              volume_hal->kind = THUNAR_VFS_VOLUME_KIND_UNKNOWN;
              break;
            }
        }
      break;

    case LIBHAL_DRIVE_TYPE_FLOPPY:
      volume_hal->kind = THUNAR_VFS_VOLUME_KIND_FLOPPY;
      break;

    case LIBHAL_DRIVE_TYPE_PORTABLE_AUDIO_PLAYER:
      volume_hal->kind = THUNAR_VFS_VOLUME_KIND_AUDIO_PLAYER;
      break;

    case LIBHAL_DRIVE_TYPE_SMART_MEDIA:
    case LIBHAL_DRIVE_TYPE_SD_MMC:
      volume_hal->kind = THUNAR_VFS_VOLUME_KIND_MEMORY_CARD;
      break;

    default:
      /* check if the drive is connected to the USB bus */
      if (libhal_drive_get_bus (hd) == LIBHAL_DRIVE_BUS_USB)
        {
          /* we consider the drive to be an USB stick */
          volume_hal->kind = THUNAR_VFS_VOLUME_KIND_USBSTICK;
        }
      else if (libhal_drive_uses_removable_media (hd)
            || libhal_drive_is_hotpluggable (hd))
        {
          /* fallback to generic removable disk */
          volume_hal->kind = THUNAR_VFS_VOLUME_KIND_REMOVABLE_DISK;
        }
      else
        {
          /* fallback to harddisk drive */
          volume_hal->kind = THUNAR_VFS_VOLUME_KIND_HARDDISK;
        }
      break;
    }

  /* either we have a volume, which means we have media, or
   * a drive, which means non-pollable then, so it's present
   */
  volume_hal->status |= THUNAR_VFS_VOLUME_STATUS_PRESENT;
  
  /* figure out if the volume is mountable */
  if(hv != NULL && libhal_volume_get_fsusage (hv) == LIBHAL_VOLUME_USAGE_MOUNTABLE_FILESYSTEM)
    volume_hal->status |= THUNAR_VFS_VOLUME_STATUS_MOUNTABLE;

  /* check if the drive requires eject */
  volume_hal->requires_eject = libhal_drive_requires_eject (hd);

  /* check if the volume is currently mounted */
  if (hv != NULL && libhal_volume_is_mounted (hv))
    {
      /* try to determine the new mount point */
      volume_hal->mount_point = thunar_vfs_path_new (libhal_volume_get_mount_point (hv), NULL);

      /* we only mark the volume as mounted if we have a valid mount point */
      if (G_LIKELY (volume_hal->mount_point != NULL))
        volume_hal->status |= THUNAR_VFS_VOLUME_STATUS_MOUNTED | THUNAR_VFS_VOLUME_STATUS_PRESENT;
    }
  else
    {
      /* we don't trust HAL, so let's see what the kernel says about the volume */
      volume_hal->mount_point = thunar_vfs_volume_hal_find_active_mount_point (volume_hal);

      /* we must have been mounted successfully if we have a mount point */
      if (G_LIKELY (volume_hal->mount_point != NULL))
        volume_hal->status |= THUNAR_VFS_VOLUME_STATUS_MOUNTED | THUNAR_VFS_VOLUME_STATUS_PRESENT;
    }

  /* check if we have to figure out the mount point ourself */
  if (G_UNLIKELY (volume_hal->mount_point == NULL))
    {
      /* ask HAL for the default mount root (falling back to /media otherwise) */
      mount_root = libhal_device_get_property_string (context, "/org/freedesktop/Hal/devices/computer", "storage.policy.default.mount_root", NULL);
      if (G_UNLIKELY (mount_root == NULL || !g_path_is_absolute (mount_root)))
        {
          /* fallback to /media (seems to be sane) */
          g_free (mount_root);
          mount_root = g_strdup ("/media");
        }

      /* lets see, maybe /etc/fstab knows where to mount */
      volume_hal->mount_point = thunar_vfs_volume_hal_find_fstab_mount_point (volume_hal);

      /* if we still don't have a mount point, ask HAL */
      if (G_UNLIKELY (volume_hal->mount_point == NULL))
        {
          /* determine the desired mount point and prepend the mount root */
          desired_mount_point = libhal_device_get_property_string (context, volume_hal->udi, "volume.policy.desired_mount_point", NULL);
          if (G_LIKELY (desired_mount_point != NULL && *desired_mount_point != '\0'))
            {
              filename = g_build_filename (mount_root, desired_mount_point, NULL);
              volume_hal->mount_point = thunar_vfs_path_new (filename, NULL);
              g_free (filename);
            }
          libhal_free_string (desired_mount_point);
        }

      /* ok, last fallback, just use <mount-root>/<device> */
      if (G_UNLIKELY (volume_hal->mount_point == NULL))
        {
          /* <mount-root>/<device> looks like a good idea */
          basename = g_path_get_basename (volume_hal->device_file);
          filename = g_build_filename (mount_root, basename, NULL);
          volume_hal->mount_point = thunar_vfs_path_new (filename, NULL);
          g_free (filename);
          g_free (basename);
        }

      /* release the mount root */
      g_free (mount_root);
    }

  /* if we get here, we must have a valid mount point */
  g_assert (volume_hal->mount_point != NULL);

  /* emit the "changed" signal */
  thunar_vfs_volume_changed (THUNAR_VFS_VOLUME (volume_hal));
}