All pastes #15146 Raw Edit

Anonymous

public text v1 · immutable
#15146 ·published 2005-06-20 21:21 UTC
rendered paste body
int
device_removable( const char* device )
{
    struct sysfs_device *dev;
    static char* hotplug_buses[] = { "usb", "ieee1394", NULL };
    int removable;
    char blockdevpath[PATH_MAX];

    dev = find_sysfs_device( device, blockdevpath, sizeof( blockdevpath ) );
    if( !dev ) {
        debug( "device_removable: could not find a sysfs device for %s\n", device );
        return 0;
    }

    debug( "device_removable: corresponding block device for %s is %s\n",
                device, blockdevpath );

    /* check whether device has "removable" attribute with value '1' */
    removable = get_blockdev_attr( blockdevpath, "removable" );

    /* if not, fall back to bus scanning (regard USB and FireWire as removable) */
    if( !removable )
        removable = find_bus_ancestry( dev, hotplug_buses );
    sysfs_close_device( dev );

    if( !removable )
        fprintf( stderr, _("Error: device %s is not removable\n"), device );

    return removable;