Changeset f500d7


Ignore:
Timestamp:
11/03/2015 12:39:50 PM (9 years ago)
Author:
Calum Lind <calumlind+deluge@gmail.com>
Branches:
2.0.x, develop, master
Children:
f1e708
Parents:
ed48c4
git-author:
Calum Lind <calumlind+deluge@gmail.com> (10/30/2015 02:34:58 PM)
git-committer:
Calum Lind <calumlind+deluge@gmail.com> (11/03/2015 12:39:50 PM)
Message:

#2775 Update state and fastresume save methods

  • Issue introducted in a previous commit meant the state file is never saved when starting with a fresh config.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • deluge/core/torrentmanager.py

    red48c4 rf500d7  
    650650        """Save the state of the TorrentManager to the torrents.state file."""
    651651        state = self.create_state()
     652        if not state.torrents:
     653            log.debug("Skipping saving state with no torrents loaded")
     654            return
     655
    652656        filename = "torrents.state"
    653657        filepath = os.path.join(self.state_dir, filename)
     
    656660
    657661        try:
    658             os.remove(filepath_bak)
    659         except OSError:
    660             pass
     662            log.debug("Creating the temporary file: %s", filepath_tmp)
     663            with open(filepath_tmp, "wb", 0) as _file:
     664                cPickle.dump(state, _file)
     665                _file.flush()
     666                os.fsync(_file.fileno())
     667        except (OSError, cPickle.PicklingError) as ex:
     668            log.error("Unable to save %s: %s", filename, ex)
     669            return
     670
    661671        try:
    662672            log.debug("Creating backup of %s at: %s", filename, filepath_bak)
    663             os.rename(filepath, filepath_bak)
     673            if os.path.isfile(filepath_bak):
     674                os.remove(filepath_bak)
     675            if os.path.isfile(filepath):
     676                os.rename(filepath, filepath_bak)
    664677        except OSError as ex:
    665678            log.error("Unable to backup %s to %s: %s", filepath, filepath_bak, ex)
    666         else:
    667             log.info("Saving the %s at: %s", filename, filepath)
    668             try:
    669                 with open(filepath_tmp, "wb", 0) as _file:
    670                     # Pickle the TorrentManagerState object
    671                     cPickle.dump(state, _file)
    672                     _file.flush()
    673                     os.fsync(_file.fileno())
    674                 os.rename(filepath_tmp, filepath)
    675             except (OSError, cPickle.PicklingError) as ex:
    676                 log.error("Unable to save %s: %s", filename, ex)
    677                 if os.path.isfile(filepath_bak):
    678                     log.info("Restoring backup of %s from: %s", filename, filepath_bak)
    679                     os.rename(filepath_bak, filepath)
     679            return
     680
     681        try:
     682            log.debug("Saving %s to: %s", filename, filepath)
     683            os.rename(filepath_tmp, filepath)
     684        except OSError, ex:
     685            log.error("Failed to set new state file %s: %s", filepath, ex)
     686            if os.path.isfile(filepath_bak):
     687                log.info("Restoring backup of state from: %s", filepath_bak)
     688                os.rename(filepath_bak, filepath)
    680689
    681690    def save_resume_data(self, torrent_ids=None, flush_disk_cache=False):
     
    766775
    767776        try:
    768             os.remove(filepath_bak)
    769         except OSError:
    770             pass
     777            log.debug("Creating the temporary file: %s", filepath_tmp)
     778            with open(filepath_tmp, "wb", 0) as _file:
     779                _file.write(lt.bencode(self.resume_data))
     780                _file.flush()
     781                os.fsync(_file.fileno())
     782        except (OSError, EOFError) as ex:
     783            log.error("Unable to save %s: %s", filename, ex)
     784            return False
     785
    771786        try:
    772787            log.debug("Creating backup of %s at: %s", filename, filepath_bak)
    773             os.rename(filepath, filepath_bak)
     788            if os.path.isfile(filepath_bak):
     789                os.remove(filepath_bak)
     790            if os.path.isfile(filepath):
     791                os.rename(filepath, filepath_bak)
    774792        except OSError as ex:
    775793            log.error("Unable to backup %s to %s: %s", filepath, filepath_bak, ex)
     794            return False
     795
     796        try:
     797            log.debug("Saving %s to: %s", filename, filepath)
     798            os.rename(filepath_tmp, filepath)
     799        except OSError, ex:
     800            log.error("Failed to set new file %s: %s", filepath, ex)
     801            if os.path.isfile(filepath_bak):
     802                log.info("Restoring backup from: %s", filepath_bak)
     803                os.rename(filepath_bak, filepath)
    776804        else:
    777             log.info("Saving the %s at: %s", filename, filepath)
    778             try:
    779                 with open(filepath_tmp, "wb", 0) as _file:
    780                     _file.write(lt.bencode(self.resume_data))
    781                     _file.flush()
    782                     os.fsync(_file.fileno())
    783                 os.rename(filepath_tmp, filepath)
    784             except (OSError, EOFError) as ex:
    785                 log.error("Unable to save %s: %s", filename, ex)
    786                 if os.path.isfile(filepath_bak):
    787                     log.info("Restoring backup of %s from: %s", filename, filepath_bak)
    788                     os.rename(filepath_bak, filepath)
    789             else:
    790                 # Sync the rename operations for the directory
    791                 if hasattr(os, 'O_DIRECTORY'):
    792                     dirfd = os.open(os.path.dirname(filepath), os.O_DIRECTORY)
    793                     os.fsync(dirfd)
    794                     os.close(dirfd)
    795                 return True
     805            # Sync the rename operations for the directory
     806            if hasattr(os, 'O_DIRECTORY'):
     807                dirfd = os.open(os.path.dirname(filepath), os.O_DIRECTORY)
     808                os.fsync(dirfd)
     809                os.close(dirfd)
     810            return True
    796811
    797812    def get_queue_position(self, torrent_id):
Note: See TracChangeset for help on using the changeset viewer.