Example #1
0
 def __save_session_state(self):
     """Saves the libtorrent session state"""
     try:
         session_state = deluge.configmanager.get_config_dir("session.state")
         open(session_state, "wb").write(lt.bencode(self.session.save_state()))
     except Exception, e:
         log.warning("Failed to save lt state: %s", e)
Example #2
0
    def save_resume_data_file(self, resume_data=None):
        """
        Saves the resume data file with the contents of self.resume_data.  If
        `resume_data` is None, then we grab the resume_data from the file on
        disk, else, we update `resume_data` with self.resume_data and save
        that to disk.

        :param resume_data: the current resume_data, this will be loaded from disk if not provided
        :type resume_data: dict

        """
        # Check to see if we're waiting on more resume data
        if self.num_resume_data or not self.resume_data:
            return

        path = os.path.join(get_config_dir(), "state", "torrents.fastresume")

        # First step is to load the existing file and update the dictionary
        if resume_data is None:
            resume_data = self.load_resume_data_file()

        resume_data.update(self.resume_data)
        self.resume_data = {}

        try:
            log.debug("Saving fastresume file: %s", path)
            fastresume_file = open(path, "wb")
            fastresume_file.write(lt.bencode(resume_data))
            fastresume_file.flush()
            os.fsync(fastresume_file.fileno())
            fastresume_file.close()
        except IOError:
            log.warning("Error trying to save fastresume file")
Example #3
0
    def _save_session_state(self):
        """Saves the libtorrent session state"""
        filename = 'session.state'
        filepath = get_config_dir(filename)
        filepath_bak = filepath + '.bak'
        filepath_tmp = filepath + '.tmp'

        try:
            if os.path.isfile(filepath):
                log.debug('Creating backup of %s at: %s', filename, filepath_bak)
                shutil.copy2(filepath, filepath_bak)
        except IOError as ex:
            log.error('Unable to backup %s to %s: %s', filepath, filepath_bak, ex)
        else:
            log.info('Saving the %s at: %s', filename, filepath)
            try:
                with open(filepath_tmp, 'wb') as _file:
                    _file.write(lt.bencode(self.session.save_state()))
                    _file.flush()
                    os.fsync(_file.fileno())
                shutil.move(filepath_tmp, filepath)
            except (IOError, EOFError) as ex:
                log.error('Unable to save %s: %s', filename, ex)
                if os.path.isfile(filepath_bak):
                    log.info('Restoring backup of %s from: %s', filename, filepath_bak)
                    shutil.move(filepath_bak, filepath)
Example #4
0
 def save_dht_state(self):
     """Saves the dht state to a file"""
     try:
         dht_data = open(deluge.configmanager.get_config_dir("dht.state"), "wb")
         dht_data.write(lt.bencode(self.session.dht_state()))
         dht_data.close()
     except Exception, e:
         log.warning("Failed to save dht state: %s", e)
Example #5
0
 def save_dht_state(self):
     """Saves the dht state to a file"""
     try:
         dht_data = open(deluge.configmanager.get_config_dir("dht.state"),
                         "wb")
         dht_data.write(lt.bencode(self.session.dht_state()))
         dht_data.close()
     except Exception, e:
         log.warning("Failed to save dht state: %s", e)
Example #6
0
 def __save_session_state(self):
     """Saves the libtorrent session state"""
     try:
         session_state = deluge.configmanager.get_config_dir(
             "session.state")
         open(session_state,
              "wb").write(lt.bencode(self.session.save_state()))
     except Exception, e:
         log.warning("Failed to save lt state: %s", e)
Example #7
0
    def on_alert_save_resume_data(self, alert):
        if log.isEnabledFor(logging.DEBUG):
            log.debug("on_alert_save_resume_data")
        torrent_id = str(alert.handle.info_hash())

        if torrent_id in self.torrents:
            # Libtorrent in add_torrent() expects resume_data to be bencoded
            self.resume_data[torrent_id] = lt.bencode(alert.resume_data)

        if torrent_id in self.waiting_on_resume_data:
            self.waiting_on_resume_data[torrent_id].callback(None)
Example #8
0
    def on_alert_save_resume_data(self, alert):
        if log.isEnabledFor(logging.DEBUG):
            log.debug("on_alert_save_resume_data")
        torrent_id = str(alert.handle.info_hash())

        if torrent_id in self.torrents:
            # Libtorrent in add_torrent() expects resume_data to be bencoded
            self.resume_data[torrent_id] = lt.bencode(alert.resume_data)

        if torrent_id in self.waiting_on_resume_data:
            self.waiting_on_resume_data[torrent_id].callback(None)
Example #9
0
 def write_torrentfile(self):
     """Writes the torrent file"""
     path = "%s/%s.torrent" % (os.path.join(get_config_dir(), "state"), self.torrent_id)
     log.debug("Writing torrent file: %s", path)
     try:
         self.torrent_info = self.handle.get_torrent_info()
         # Regenerate the file priorities
         self.set_file_priorities([])
         md = lt.bdecode(self.torrent_info.metadata())
         torrent_file = {}
         torrent_file["info"] = md
         open(path, "wb").write(lt.bencode(torrent_file))
     except Exception, e:
         log.warning("Unable to save torrent file: %s", e)
Example #10
0
 def write_torrentfile(self):
     """Writes the torrent file"""
     path = "%s/%s.torrent" % (os.path.join(get_config_dir(),
                                            "state"), self.torrent_id)
     log.debug("Writing torrent file: %s", path)
     try:
         # Regenerate the file priorities
         self.set_file_priorities([])
         md = lt.bdecode(self.torrent_info.metadata())
         torrent_file = {}
         torrent_file["info"] = md
         open(path, "wb").write(lt.bencode(torrent_file))
     except Exception, e:
         log.warning("Unable to save torrent file: %s", e)
Example #11
0
    def save_resume_data_file(self):
        """
        Saves the resume data file with the contents of self.resume_data.
        """
        path = os.path.join(get_config_dir(), "state", "torrents.fastresume")

        try:
            log.debug("Saving fastresume file: %s", path)
            fastresume_file = open(path, "wb")
            fastresume_file.write(lt.bencode(self.resume_data))
            fastresume_file.flush()
            os.fsync(fastresume_file.fileno())
            fastresume_file.close()
        except IOError:
            log.warning("Error trying to save fastresume file")
Example #12
0
    def save_resume_data_file(self):
        """
        Saves the resume data file with the contents of self.resume_data.
        """
        path = os.path.join(get_config_dir(), "state", "torrents.fastresume")

        try:
            log.debug("Saving fastresume file: %s", path)
            fastresume_file = open(path, "wb")
            fastresume_file.write(lt.bencode(self.resume_data))
            fastresume_file.flush()
            os.fsync(fastresume_file.fileno())
            fastresume_file.close()
        except IOError:
            log.warning("Error trying to save fastresume file")
Example #13
0
    def on_alert_save_resume_data(self, alert):
        log.debug("on_alert_save_resume_data")
        try:
            torrent_id = str(alert.handle.info_hash())
            torrent = self.torrents[torrent_id]
        except:
            return

        # Libtorrent in add_torrent() expects resume_data to be bencoded
        self.resume_data[torrent_id] = lt.bencode(alert.resume_data)
        self.num_resume_data -= 1

        torrent.waiting_on_resume_data = False

        self.save_resume_data_file()
Example #14
0
 def __write(self, torrent_ids, state_filename, resume_filename):
     state_file = open(state_filename + ".new", "wb")
     resume_file = open(resume_filename + ".new", "wb")
     state_data = TorrentManagerState()
     state_data.torrents = [
         self.torrents[torrent_id]["state"] for torrent_id in torrent_ids
     ]
     resume_data = {
         torrent_id: self.torrents[torrent_id]["resume"]
         for torrent_id in torrent_ids
     }
     cPickle.dump(state_data, state_file, cPickle.HIGHEST_PROTOCOL)
     resume_file.write(lt.bencode(resume_data))
     state_file.flush()
     resume_file.flush()
     os.fsync(state_file.fileno())
     os.fsync(resume_file.fileno())
     state_file.close()
     resume_file.close()
     os.rename(state_filename + ".new", state_filename)
     os.rename(resume_filename + ".new", resume_filename)
Example #15
0
    def test_torrent_error_resume_data_unaltered(self):
        resume_data = {'active_time': 13399, 'num_incomplete': 16777215, 'announce_to_lsd': 1, 'seed_mode': 0,
                       'pieces': '\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01', 'paused': 0,
                       'seeding_time': 13399, 'last_scrape': 13399,
                       'info-hash': '-\xc5\xd0\xe7\x1af\xfeid\x9ad\r9\xcb\x00\xa2YpIs', 'max_uploads': 16777215,
                       'max_connections': 16777215, 'num_downloaders': 16777215, 'total_downloaded': 0,
                       'file-format': 'libtorrent resume file', 'peers6': '', 'added_time': 1411826665,
                       'banned_peers6': '', 'file_priority': [1], 'last_seen_complete': 0, 'total_uploaded': 0,
                       'piece_priority': '\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01',
                       'file-version': 1, 'announce_to_dht': 1, 'auto_managed': 1, 'upload_rate_limit': 0,
                       'completed_time': 1411826665, 'allocation': 'sparse', 'blocks per piece': 2,
                       'download_rate_limit': 0, 'libtorrent-version': '0.16.17.0', 'banned_peers': '',
                       'num_seeds': 16777215, 'sequential_download': 0, 'announce_to_trackers': 1,
                       'peers': '\n\x00\x02\x0f=\xc6SC\x17]\xd8}\x7f\x00\x00\x01=\xc6', 'finished_time': 13399,
                       'last_upload': 13399, 'trackers': [[]], 'super_seeding': 0,
                       'file sizes': [[512000, 1411826586]], 'last_download': 13399}
        torrent_state = TorrentState(
            torrent_id='2dc5d0e71a66fe69649a640d39cb00a259704973',
            filename='test_torrent.file.torrent',
            name='',
            save_path='/home/ubuntu/Downloads',
            file_priorities=[1],
            is_finished=True,
        )

        filename = common.get_test_data_file('test_torrent.file.torrent')
        with open(filename) as _file:
            filedump = _file.read()
        resume_data = utf8_encode_structure(resume_data)
        torrent_id = yield self.core.torrentmanager.add(state=torrent_state, filedump=filedump,
                                                        resume_data=lt.bencode(resume_data))
        torrent = self.core.torrentmanager.torrents[torrent_id]

        def assert_resume_data():
            self.assert_state(torrent, 'Error')
            tm_resume_data = lt.bdecode(self.core.torrentmanager.resume_data[torrent.torrent_id])
            self.assertEqual(tm_resume_data, resume_data)

        yield deferLater(reactor, 0.5, assert_resume_data)
        return
Example #16
0
        resume_data.update(self.resume_data)
        self.resume_data = {}

        try:
            os.remove(filepath_bak)
        except OSError:
            pass
        try:
            log.debug("Creating backup of fastresume at: %s", filepath_bak)
            os.rename(filepath, filepath_bak)
        except OSError, ex:
            log.error("Unable to backup %s to %s: %s", filepath, filepath_bak, ex)
        try:
            log.info("Saving the fastresume at: %s", filepath)
            fastresume_file = open(filepath_tmp, "wb", 0)
            fastresume_file.write(lt.bencode(resume_data))
            fastresume_file.flush()
            os.fsync(fastresume_file.fileno())
            fastresume_file.close()
            os.rename(filepath_tmp, filepath)
        except IOError:
            log.error("Unable to save %s: %s", filepath, ex)
            if os.path.isfile(filepath_bak):
                log.info("Restoring backup of fastresume from: %s", filepath_bak)
                os.rename(filepath_bak, filepath)

    def remove_empty_folders(self, torrent_id, folder):
        """
        Recursively removes folders but only if they are empty.
        Cleans up after libtorrent folder renames.
Example #17
0
    def test_torrent_error_resume_data_unaltered(self):
        if windows_check():
            raise unittest.SkipTest(
                'unexpected end of file in bencoded string')
        resume_data = {
            'active_time': 13399,
            'num_incomplete': 16777215,
            'announce_to_lsd': 1,
            'seed_mode': 0,
            'pieces':
            '\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01',
            'paused': 0,
            'seeding_time': 13399,
            'last_scrape': 13399,
            'info-hash': '-\xc5\xd0\xe7\x1af\xfeid\x9ad\r9\xcb\x00\xa2YpIs',
            'max_uploads': 16777215,
            'max_connections': 16777215,
            'num_downloaders': 16777215,
            'total_downloaded': 0,
            'file-format': 'libtorrent resume file',
            'peers6': '',
            'added_time': 1411826665,
            'banned_peers6': '',
            'file_priority': [1],
            'last_seen_complete': 0,
            'total_uploaded': 0,
            'piece_priority':
            '\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01',
            'file-version': 1,
            'announce_to_dht': 1,
            'auto_managed': 1,
            'upload_rate_limit': 0,
            'completed_time': 1411826665,
            'allocation': 'sparse',
            'blocks per piece': 2,
            'download_rate_limit': 0,
            'libtorrent-version': '0.16.17.0',
            'banned_peers': '',
            'num_seeds': 16777215,
            'sequential_download': 0,
            'announce_to_trackers': 1,
            'peers': '\n\x00\x02\x0f=\xc6SC\x17]\xd8}\x7f\x00\x00\x01=\xc6',
            'finished_time': 13399,
            'last_upload': 13399,
            'trackers': [[]],
            'super_seeding': 0,
            'file sizes': [[512000, 1411826586]],
            'last_download': 13399,
        }
        torrent_state = TorrentState(
            torrent_id='2dc5d0e71a66fe69649a640d39cb00a259704973',
            filename='test_torrent.file.torrent',
            name='',
            save_path='/home/ubuntu/Downloads',
            file_priorities=[1],
            is_finished=True,
        )

        filename = common.get_test_data_file('test_torrent.file.torrent')
        with open(filename, 'rb') as _file:
            filedump = _file.read()
        resume_data = utf8_encode_structure(resume_data)
        torrent_id = self.core.torrentmanager.add(
            state=torrent_state,
            filedump=filedump,
            resume_data=lt.bencode(resume_data))
        torrent = self.core.torrentmanager.torrents[torrent_id]

        def assert_resume_data():
            self.assert_state(torrent, 'Error')
            tm_resume_data = lt.bdecode(
                self.core.torrentmanager.resume_data[torrent.torrent_id])
            self.assertEqual(tm_resume_data, resume_data)

        return deferLater(reactor, 0.5, assert_resume_data)
                    #has unclean suffix?
                    for suffix in UNCLEANSUFFIXES:
                        if abspath.endswith(suffix):
                            abspath = abspath[:-len(suffix)]
                    target_path = abspath.replace(file.path, "")

                    #print into statefile
                    t.save_path = target_path;
                    t.move_completed = False;
                    t.move_completed_path = target_path;

                    #print into fastresume file
                    tf = lt.bdecode(resume_file.get(t.torrent_id))
                    tf.update( {"save_path":target_path} )
                    resume_file.update( {t.torrent_id:lt.bencode(tf)})
                    
                    change = True
                    print "--- FOUND and printed "+t.filename+", "+ t.save_path
                    break
        else: print "NOT-FOUND:$$$$$$$ "+t.filename;
    #if changed save everything
    if change:
        #bencode resume_file
        resume_file = lt.bencode(resume_file);
        try:
            with open(configPath+"torrents.state", 'wb') as statefile_handler:    
                cPickle.dump(statefile, statefile_handler);
                statefile_handler.flush();
                statefile_handler.close();
            with open(configPath+"torrents.fastresume", 'wb') as resumefile_handler: