Tweaks to fastresume and state file saving
* Using move is not atomic on Windows so delete and rename instead. * Open the file with no buffering
This commit is contained in:
parent
bb16af3731
commit
7414737cbf
|
@ -693,25 +693,28 @@ class TorrentManager(component.Component):
|
||||||
filepath_tmp = filepath + ".tmp"
|
filepath_tmp = filepath + ".tmp"
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if os.path.isfile(filepath):
|
os.remove(filepath_bak)
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
try:
|
||||||
log.debug("Creating backup of %s at: %s", filename, filepath_bak)
|
log.debug("Creating backup of %s at: %s", filename, filepath_bak)
|
||||||
shutil.copy2(filepath, filepath_bak)
|
os.rename(filepath, filepath_bak)
|
||||||
except IOError as ex:
|
except OSError as ex:
|
||||||
log.error("Unable to backup %s to %s: %s", filepath, filepath_bak, ex)
|
log.error("Unable to backup %s to %s: %s", filepath, filepath_bak, ex)
|
||||||
else:
|
else:
|
||||||
log.info("Saving the %s at: %s", filename, filepath)
|
log.info("Saving the %s at: %s", filename, filepath)
|
||||||
try:
|
try:
|
||||||
with open(filepath_tmp, "wb") as _file:
|
with open(filepath_tmp, "wb", 0) as _file:
|
||||||
# Pickle the TorrentManagerState object
|
# Pickle the TorrentManagerState object
|
||||||
cPickle.dump(state, _file)
|
cPickle.dump(state, _file)
|
||||||
_file.flush()
|
_file.flush()
|
||||||
os.fsync(_file.fileno())
|
os.fsync(_file.fileno())
|
||||||
shutil.move(filepath_tmp, filepath)
|
os.rename(filepath_tmp, filepath)
|
||||||
except (IOError, cPickle.PicklingError) as ex:
|
except (OSError, cPickle.PicklingError) as ex:
|
||||||
log.error("Unable to save %s: %s", filename, ex)
|
log.error("Unable to save %s: %s", filename, ex)
|
||||||
if os.path.isfile(filepath_bak):
|
if os.path.isfile(filepath_bak):
|
||||||
log.info("Restoring backup of %s from: %s", filename, filepath_bak)
|
log.info("Restoring backup of %s from: %s", filename, filepath_bak)
|
||||||
shutil.move(filepath_bak, filepath)
|
os.rename(filepath_bak, filepath)
|
||||||
# We return True so that the timer thread will continue
|
# We return True so that the timer thread will continue
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@ -799,25 +802,32 @@ class TorrentManager(component.Component):
|
||||||
filepath_tmp = filepath + ".tmp"
|
filepath_tmp = filepath + ".tmp"
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if os.path.isfile(filepath):
|
os.remove(filepath_bak)
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
try:
|
||||||
log.debug("Creating backup of %s at: %s", filename, filepath_bak)
|
log.debug("Creating backup of %s at: %s", filename, filepath_bak)
|
||||||
shutil.copy2(filepath, filepath_bak)
|
os.rename(filepath, filepath_bak)
|
||||||
except IOError as ex:
|
except OSError as ex:
|
||||||
log.error("Unable to backup %s to %s: %s", filepath, filepath_bak, ex)
|
log.error("Unable to backup %s to %s: %s", filepath, filepath_bak, ex)
|
||||||
else:
|
else:
|
||||||
log.info("Saving the %s at: %s", filename, filepath)
|
log.info("Saving the %s at: %s", filename, filepath)
|
||||||
try:
|
try:
|
||||||
with open(filepath_tmp, "wb") as _file:
|
with open(filepath_tmp, "wb", 0) as _file:
|
||||||
_file.write(lt.bencode(self.resume_data))
|
_file.write(lt.bencode(self.resume_data))
|
||||||
_file.flush()
|
_file.flush()
|
||||||
os.fsync(_file.fileno())
|
os.fsync(_file.fileno())
|
||||||
shutil.move(filepath_tmp, filepath)
|
os.rename(filepath_tmp, filepath)
|
||||||
except (IOError, EOFError) as ex:
|
except (OSError, EOFError) as ex:
|
||||||
log.error("Unable to save %s: %s", filename, ex)
|
log.error("Unable to save %s: %s", filename, ex)
|
||||||
if os.path.isfile(filepath_bak):
|
if os.path.isfile(filepath_bak):
|
||||||
log.info("Restoring backup of %s from: %s", filename, filepath_bak)
|
log.info("Restoring backup of %s from: %s", filename, filepath_bak)
|
||||||
shutil.move(filepath_bak, filepath)
|
os.rename(filepath_bak, filepath)
|
||||||
else:
|
else:
|
||||||
|
# Sync the rename operations for the directory
|
||||||
|
dirfd = os.open(os.path.dirname(filepath), os.O_DIRECTORY)
|
||||||
|
os.fsync(dirfd)
|
||||||
|
os.close(dirfd)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def get_queue_position(self, torrent_id):
|
def get_queue_position(self, torrent_id):
|
||||||
|
|
Loading…
Reference in New Issue