def _set_files_disabled(self, old_priority, new_priority): old_disabled = [p == -1 for p in old_priority] new_disabled = [p == -1 for p in new_priority] data_to_update = [] for f in xrange(self.numfiles): if new_disabled[f] != old_disabled[f]: data_to_update.extend(self.storage.get_piece_update_list(f)) buffer = [] for piece, start, length in data_to_update: if self.storagewrapper.has_data(piece): data = self.storagewrapper.read_raw(piece, start, length) if data is None: return False buffer.append((piece, start, data)) files_updated = False try: for f in xrange(self.numfiles): if new_disabled[f] and not old_disabled[f]: self.storage.disable_file(f) files_updated = True if old_disabled[f] and not new_disabled[f]: self.storage.enable_file(f) files_updated = True except (IOError, OSError), e: if new_disabled[f]: msg = "can't open partial file for " else: msg = "unable to open " self.failfunc(msg + self.files[f][0] + ": " + exceptionArgsToString(e)) return False
def _get_file_handle(self, file, for_write): if self.handles.has_key(file): if for_write and not self.whandles.has_key(file): self._close(file) try: f = self._open(file, 'rb+') self.handles[file] = f self.whandles[file] = 1 self.lock_file(file, f) except (IOError, OSError), e: if DEBUG: print_exc() raise IOError('unable to reopen '+file+': '+exceptionArgsToString(e)) if self.handlebuffer: if self.handlebuffer[-1] != file: self.handlebuffer.remove(file) self.handlebuffer.append(file) elif self.handlebuffer is not None: self.handlebuffer.append(file)
def init_priority(self, new_priority): try: assert len(new_priority) == self.numfiles for v in new_priority: assert type(v) in (type(0), type(0L)) assert v >= -1 assert v <= 2 except: # print_exc() return False try: files_updated = False for f in xrange(self.numfiles): if new_priority[f] < 0: self.storage.disable_file(f) files_updated = True if files_updated: self.storage.reset_file_status() self.new_priority = new_priority except (IOError, OSError), e: self.failfunc("can't open partial file for " + self.files[f][0] + ": " + exceptionArgsToString(e)) return False
else: try: if for_write: f = self._open(file, 'rb+') self.handles[file] = f self.whandles[file] = 1 self.lock_file(file, f) else: f = self._open(file, 'rb') self.handles[file] = f if self.lock_while_reading: self.lock_file(file, f) except (IOError, OSError), e: if DEBUG: print_exc() raise IOError('unable to open '+file+': '+exceptionArgsToString(e)) if self.handlebuffer is not None: self.handlebuffer.append(file) if len(self.handlebuffer) > self.max_files_open: self._close(self.handlebuffer.pop(0)) return self.handles[file] def _reset_ranges(self): self.ranges = [] for l in self.working_ranges: self.ranges.extend(l) self.begins = [i[0] for i in self.ranges]