won't start a download if isn't enough space

This commit is contained in:
Zach Tibbitts 2007-03-08 22:14:40 +00:00
parent d0b053fca4
commit 61e48e249b
2 changed files with 54 additions and 5 deletions

View File

@ -37,7 +37,7 @@
# 3. supp_torrent_state - supplementary torrent data, from Deluge # 3. supp_torrent_state - supplementary torrent data, from Deluge
import deluge_core import deluge_core
import os, shutil import os, shutil, statvfs
import pickle import pickle
import time import time
import gettext import gettext
@ -110,6 +110,13 @@ class InvalidTorrentError(DelugeError):
class InvalidUniqueIDError(DelugeError): class InvalidUniqueIDError(DelugeError):
pass pass
class InsufficientFreeSpaceError(DelugeError):
def __init__(self, free_space, needed_space):
self.free_space = free_space
self.needed_space = needed_space
def __str__(self):
return "%d %d bytes needed"%(self.free_space, self.needed_space)
# A cached data item # A cached data item
class cached_data: class cached_data:
@ -590,14 +597,22 @@ class Manager:
return self.saved_core_torrent_file_infos[unique_ID].get(efficiently) return self.saved_core_torrent_file_infos[unique_ID].get(efficiently)
# Functions for checking if enough space is available
def calc_free_space(self, directory):
dir_stats = os.statvfs(directory)
block_size = dir_stats[statvfs.F_BSIZE]
avail_blocks = dir_stats[statvfs.F_BAVAIL]
return long(block_size * avail_blocks)
# Non-syncing functions. Used when we loop over such events, and sync manually at the end # Non-syncing functions. Used when we loop over such events, and sync manually at the end
def add_torrent_ns(self, filename, save_dir, compact): def add_torrent_ns(self, filename, save_dir, compact):
# Cache torrent file # Cache torrent file
(temp, filename_short) = os.path.split(filename) (temp, filename_short) = os.path.split(filename)
if filename_short in os.listdir(self.base_dir + "/" + TORRENTS_SUBDIR): # if filename_short in os.listdir(self.base_dir + "/" + TORRENTS_SUBDIR):
raise DuplicateTorrentError("Duplicate Torrent, it appears: " + filename_short) # raise DuplicateTorrentError("Duplicate Torrent, it appears: " + filename_short)
full_new_name = self.base_dir + "/" + TORRENTS_SUBDIR + "/" + filename_short full_new_name = self.base_dir + "/" + TORRENTS_SUBDIR + "/" + filename_short
@ -633,9 +648,27 @@ class Manager:
torrent.save_dir, torrent.save_dir,
torrent.compact) torrent.compact)
# print "Got unique ID:", unique_ID # print "Got unique ID:", unique_ID
# Now to check and see if there is enough free space for the download
size = deluge_core.get_torrent_state(unique_ID)["total_size"]
avail = self.calc_free_space(torrent.save_dir)
print "Torrent Size", size
print "Available Space", avail
size = avail + 1 #debug!
if size > avail: # Not enough free space
deluge_core.remove_torrent(unique_ID) #Remove the torrent
self.state.torrents.remove(torrent)
#self.state.queue.remove(unique_ID)
os.remove(torrent.filename)
try:
# Must be after removal of the torrent, because that saves a new .fastresume
os.remove(torrent.filename + ".fastresume")
except OSError:
pass # Perhaps there never was one to begin with
raise InsufficientFreeSpaceError(avail, size)
ret = unique_ID ret = unique_ID
self.unique_IDs[unique_ID] = torrent self.unique_IDs[unique_ID] = torrent
# print torrents_with_unique_ID # print torrents_with_unique_ID
# Remove torrents from core, unique_IDs and queue # Remove torrents from core, unique_IDs and queue
to_delete = [] to_delete = []
@ -703,3 +736,5 @@ class Manager:
ret = -1 ret = -1
return ret return ret

View File

@ -879,7 +879,21 @@ class DelugeGTK:
path = dgtk.show_directory_chooser_dialog(self.window) path = dgtk.show_directory_chooser_dialog(self.window)
if path is None: if path is None:
return return
unique_id = self.manager.add_torrent(torrent, path, self.config.get('use_compact_storage', bool, default=False)) try:
unique_id = self.manager.add_torrent(torrent, path, False)
# unique_id = self.manager.add_torrent(torrent, path, self.config.get('use_compact_storage', bool, default=False))
except deluge.InsufficientFreeSpaceError, err:
print "Got an Error,", err
if self.is_running: # make sure the gui is running and alert the user.
nice_need = dcommon.fsize(err.needed_space)
nice_free = dcommon.fsize(err.free_space)
message = _("There is not enough free disk space to complete your download.") + "\n" \
+ _("Space available on disk:") + " " + nice_free + "\n" \
+ _("Total size of download:") + " " + nice_need
dgtk.show_popup_warning(self.window, message)
return
if append: if append:
self.torrent_model.append(self.get_list_from_unique_id(unique_id)) self.torrent_model.append(self.get_list_from_unique_id(unique_id))