Esempio n. 1
0
    def _update_free(self, k):
        # TODO evand 2009-08-28: Replace this mess with inotify watches.
        # Incorporate accounting for files we will delete.  Defer updates if
        # sufficient time has not passed since the last update.
        if not self.current_source:
            return False
        current_source = self.sources[self.current_source]
        changed = False
        target = self.targets[k]
        free = target['free']
        target['free'] = misc.fs_size(target['mountpoint'])[1]
        if free != target['free']:
            changed = True

        target = self.targets[k]
        status = target['status']
        target['status'] = misc.CAN_USE
        target['persist'] = 0
        if target['capacity'] < current_source['size']:
            target['status'] = misc.CANNOT_USE
        elif target['free'] < current_source['size']:
            target['status'] = misc.NEED_SPACE
        else:
            target['persist'] = (target['free'] - current_source['size'] -
                                 misc.PADDING * 1024 * 1024)
        if status != target['status']:
            changed = True
        # casper cannot handle files larger than MAX_PERSISTENCE (4GB)
        persist_max = misc.MAX_PERSISTENCE * 1024 * 1024 - 1
        if target['persist'] > persist_max:
            target['persist'] = persist_max
        return changed
Esempio n. 2
0
    def _add_cd(self, device):
        logging.debug('cd added: %s' % device)
        dk = self.bus.get_object(DISKS_IFACE, device)

        def get(prop):
            return dk.Get(device, prop, dbus_interface=PROPS_IFACE)

        label = get('id-label')
        if not get('device-is-mounted'):
            try:
                dk.FilesystemMount('', [], dbus_interface=DEVICE_IFACE)
            except dbus.DBusException as e:
                logging.exception('Could not mount the device: %s' % e)
                return
        mount = get('device-mount-paths')[0]
        device_file = get('device-file')
        total, free = misc.fs_size(mount)
        self.sources[device] = {
            'device': device_file,
            'size': total,
            'label': label,
            'type': misc.SOURCE_CD,
        }
        if misc.callable(self.source_added_cb):
            self.source_added_cb(device)
Esempio n. 3
0
 def _udisks_cdrom_added(self, obj, block, drive, path):
     logging.debug('cd added: %s' % path)
     fs = obj.get_filesystem()
     if not fs:
         logging.debug('cd %s has no filesystem.' % path)
         return
     mount_points = fs.get_cached_property(
         'MountPoints').get_bytestring_array()
     if len(mount_points) == 0:
         try:
             mount = fs.call_mount_sync(no_options, None)
         except GLib.GError as e:
             logging.exception('Could not mount the device: %s' % e.message)
     else:
         mount = mount_points[0]
     total, free = misc.fs_size(mount)
     self.sources[path] = {
         'device':
         block.get_cached_property('Device').get_bytestring().decode(
             'utf-8'),
         'size':
         total,
         'label':
         block.get_cached_property('IdLabel').get_string(),
         'type':
         misc.SOURCE_CD,
         'mount':
         mount,
     }
     if misc.callable(self.source_added_cb):
         self.source_added_cb(path)
Esempio n. 4
0
    def _device_added(self, drive):
        drive_buf = ctypes.create_unicode_buffer(512)
        fs_buf = ctypes.create_unicode_buffer(512)
        vol_i = ctypes.windll.kernel32.GetVolumeInformationW
        drive_type = ctypes.windll.kernel32.GetDriveTypeW(drive)
        if vol_i(drive, drive_buf, 512, None, None, None, fs_buf, 512):
            volume = drive_buf.value
            fs = fs_buf.value
        else:
            logging.error('Could not get volume information for %s' % drive)
            return

        if drive_type > len(DRIVE_TYPES):
            drive_type = 0
        drive_type = DRIVE_TYPES[drive_type]

        if drive_type == 'cdrom':
            if os.path.exists(os.path.join(drive, '.disk/info')):
                self.sources[drive] = {
                    'device': drive,
                    'size': misc.fs_size(drive)[0],
                    'label': volume,
                    'type': misc.SOURCE_CD,
                }
                if misc.callable(self.source_added_cb):
                    self.source_added_cb(drive)
        elif drive_type == 'removeable':  # and fs == u'fat32':
            tot, free = misc.fs_size(drive)
            self.targets[drive] = {
                'capacity': tot,
                'free': free,
                'device': drive,
                # FIXME evand 2009-07-16: How does Windows get the volume name?
                # GetVolumeInformation returns NULL here.
                'label': '',
                'mountpoint': drive,
                'status': misc.CAN_USE,
            }
            if misc.callable(self.target_added_cb):
                self.target_added_cb(drive)
        else:
            tot, free = misc.fs_size(drive)
            logging.debug('Not adding %s (%s %s %s volume, %d total bytes, '
                          '%d bytes free)' %
                          (drive, volume, drive_type, fs, tot, free))
Esempio n. 5
0
 def run(self):
     try:
         while not self._stopevent.isSet():
             free = fs_size(self.device)[1]
             written = self.start_free - free
             v = int((written / float(self.to_write)) * 100)
             est = self.remtime.estimate(written, self.to_write)
             if callable(self.progress):
                 self.progress(v, est[0], est[1])
             self._stopevent.wait(2)
     except StandardError:
         logging.exception('Could not update progress:')
Esempio n. 6
0
 def run(self):
     try:
         while not self._stopevent.isSet():
             free = fs_size(self.device)[1]
             written = self.start_free - free
             v = int((written / float(self.to_write)) * 100)
             est = self.remtime.estimate(written, self.to_write)
             if callable(self.progress):
                 self.progress(v, est[0], est[1])
             self._stopevent.wait(2)
     except Exception:
         logging.exception('Could not update progress:')
Esempio n. 7
0
 def initialize_progress_thread(self):
     logging.debug('initialize_progress_thread')
     if os.path.isfile(self.source):
         s_total = os.path.getsize(self.source)
     else:
         s_total, s_free = fs_size(self.source)
     t_total, t_free = fs_size(self.target)
     # We don't really care if we can't write the entire persistence
     # file.
     if s_total > t_total:
         s_total = s_total / 1024 / 1024
         t_total = t_total / 1024 / 1024
         self._failure(_('Insufficient free space to write the image:\n'
                         '%s\n\n(%d MB) > %s (%d MB)') %
                       (self.source, s_total, self.target, t_total))
     # TODO evand 2009-07-24: Make sure dd.exe doesn't do something
     # stupid, like write past the end of the device.
     damage = s_total + (self.persist * 1024 * 1024)
     self.progress_thread = progress(t_free, damage, self.target)
     self.progress_thread.progress = self.progress
     self.progress_thread.start()
     self.check()
Esempio n. 8
0
 def initialize_progress_thread(self):
     logging.debug('initialize_progress_thread')
     if os.path.isfile(self.source):
         s_total = os.path.getsize(self.source)
     else:
         s_total, s_free = fs_size(self.source)
     t_total, t_free = fs_size(self.target)
     # We don't really care if we can't write the entire persistence
     # file.
     if s_total > t_total:
         s_total = s_total / 1024 / 1024
         t_total = t_total / 1024 / 1024
         self._failure(
             _('Insufficient free space to write the image:\n'
               '%s\n\n(%d MB) > %s (%d MB)') %
             (self.source, s_total, self.target, t_total))
     # TODO evand 2009-07-24: Make sure dd.exe doesn't do something
     # stupid, like write past the end of the device.
     damage = s_total + (self.persist * 1024 * 1024)
     self.progress_thread = progress(t_free, damage, self.target)
     self.progress_thread.progress = self.progress
     self.progress_thread.start()
     self.check()
Esempio n. 9
0
    def _add_partition(self, device):
        logging.debug('partition added: %s' % device)
        dk = self.bus.get_object(DISKS_IFACE, device)

        def get(prop):
            return dk.Get(device, prop, dbus_interface=PROPS_IFACE)

        model = get('DriveModel')
        vendor = get('DriveVendor')
        fstype = get('id-type')
        logging.debug('id-type: %s' % fstype)
        if fstype == 'vfat':
            status = misc.CAN_USE
        else:
            status = misc.NEED_FORMAT
        label = get('id-label')
        logging.debug('id-label: %s' % label)
        parent = get('partition-slave')
        if fstype == 'vfat' and not get('device-is-mounted'):
            parent_i = self.bus.get_object(DISKS_IFACE, parent)
            parent_i.Get(parent, 'device-file', dbus_interface=PROPS_IFACE)
            if device not in self.formatting and parent not in self.formatting:
                try:
                    self.retry_mount(device)
                except:
                    logging.exception('Could not mount the device:')
                    return
        mount = get('device-mount-paths') or ''
        if mount:
            mount = mount[0]
            total, free = misc.fs_size(mount)
        else:
            # FIXME evand 2009-09-11: This is going to have weird side effects.
            # If the device cannot be mounted, but is a vfat filesystem, that
            # is.  Is this really the right approach?
            total = get('partition-size')
            free = -1
        logging.debug('mount: %s' % mount)
        device_file = get('device-file')
        if total > 0:
            self.targets[misc.text_type(device)] = {
                'vendor': vendor,
                'model': model,
                'label': misc.text_type(label),
                'free': free,
                'device': misc.text_type(device_file),
                'capacity': total,
                'status': status,
                'mountpoint': mount,
                'persist': 0,
                'parent': misc.text_type(parent),
                'formatting': False,
            }
            self._update_free(misc.text_type(device))
            if self.show_all:
                if misc.callable(self.target_added_cb):
                    self.target_added_cb(device)
            else:
                if status != misc.NEED_FORMAT:
                    if misc.text_type(parent) in self.targets:
                        if misc.callable(self.target_removed_cb):
                            self.target_removed_cb(parent)
                    if misc.callable(self.target_added_cb):
                        self.target_added_cb(device)
        else:
            logging.debug('not adding device: 0 byte partition.')
Esempio n. 10
0
    def _udisks_partition_added(self, obj, block, drive, path):
        logging.debug('partition added: %s' % path)
        fstype = block.get_cached_property('IdType').get_string()
        logging.debug('id-type: %s' % fstype)
        if fstype == 'vfat':
            status = misc.CAN_USE
        else:
            status = misc.NEED_FORMAT

        if drive:
            vendor = drive.get_cached_property('Vendor').get_string()
            model = drive.get_cached_property('Model').get_string()
            size = drive.get_cached_property('Size').get_uint64()
        else:
            vendor = ''
            model = ''
            size = block.get_cached_property('Size').get_uint64()

        partition = obj.get_partition()
        parent = partition.get_cached_property('Table').get_string()
        fs = obj.get_filesystem()
        if fs:
            mount_points = fs.get_cached_property(
                'MountPoints').get_bytestring_array()
            if (fstype == 'vfat' and len(mount_points) == 0
                    and path not in self.formatting
                    and parent not in self.formatting):
                try:
                    mount = self.retry_mount(fs)
                except:
                    logging.exception('Could not mount the device: %s' % path)
                    return
            else:
                mount = mount_points and mount_points[0]
        else:
            mount = None

        if mount:
            total, free = misc.fs_size(mount)
        else:
            # FIXME evand 2009-09-11: This is going to have weird side effects.
            # If the device cannot be mounted, but is a vfat filesystem, that
            # is.  Is this really the right approach?
            total = size
            free = -1
            mount = ''
        logging.debug('mount: %s' % mount)
        if total > 1:
            self.targets[path] = {
                'vendor':
                vendor,
                'model':
                model,
                'label':
                block.get_cached_property('IdLabel').get_string(),
                'free':
                free,
                'device':
                block.get_cached_property('Device').get_bytestring().decode(
                    'utf-8'),
                'capacity':
                total,
                'status':
                status,
                'mountpoint':
                mount,
                'persist':
                0,
                'parent':
                misc.text_type(parent),
                'formatting':
                False,
            }
            self._update_free(path)
            if self.show_all:
                if misc.callable(self.target_added_cb):
                    self.target_added_cb(device)
            else:
                if status != misc.NEED_FORMAT:
                    if parent in self.targets:
                        if misc.callable(self.target_removed_cb):
                            self.target_removed_cb(parent)
                    if misc.callable(self.target_added_cb):
                        self.target_added_cb(path)
        else:
            logging.debug('not adding device: 0 byte partition.')