From 61e48e249bb0744cf703da348e0ce02d8a7fab03 Mon Sep 17 00:00:00 2001 From: Zach Tibbitts Date: Thu, 8 Mar 2007 22:14:40 +0000 Subject: [PATCH] won't start a download if isn't enough space --- src/deluge.py | 43 +++++++++++++++++++++++++++++++++++++++---- src/delugegtk.py | 16 +++++++++++++++- 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/src/deluge.py b/src/deluge.py index bfa7b17b2..5e5a4483c 100644 --- a/src/deluge.py +++ b/src/deluge.py @@ -37,7 +37,7 @@ # 3. supp_torrent_state - supplementary torrent data, from Deluge import deluge_core -import os, shutil +import os, shutil, statvfs import pickle import time import gettext @@ -110,6 +110,13 @@ class InvalidTorrentError(DelugeError): class InvalidUniqueIDError(DelugeError): 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 class cached_data: @@ -299,7 +306,7 @@ class Manager: # Apply the pref, if applicable if PREF_FUNCTIONS[key] is not None: PREF_FUNCTIONS[key](value) - + # Torrent addition and removal functions def add_torrent(self, filename, save_dir, compact): @@ -590,14 +597,22 @@ class Manager: 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 def add_torrent_ns(self, filename, save_dir, compact): # Cache torrent file (temp, filename_short) = os.path.split(filename) - if filename_short in os.listdir(self.base_dir + "/" + TORRENTS_SUBDIR): - raise DuplicateTorrentError("Duplicate Torrent, it appears: " + filename_short) + # if filename_short in os.listdir(self.base_dir + "/" + TORRENTS_SUBDIR): + # raise DuplicateTorrentError("Duplicate Torrent, it appears: " + filename_short) full_new_name = self.base_dir + "/" + TORRENTS_SUBDIR + "/" + filename_short @@ -633,8 +648,26 @@ class Manager: torrent.save_dir, torrent.compact) # 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 self.unique_IDs[unique_ID] = torrent + # print torrents_with_unique_ID # Remove torrents from core, unique_IDs and queue @@ -703,3 +736,5 @@ class Manager: ret = -1 return ret + + diff --git a/src/delugegtk.py b/src/delugegtk.py index 6054c10fc..c3eaeffc9 100644 --- a/src/delugegtk.py +++ b/src/delugegtk.py @@ -879,7 +879,21 @@ class DelugeGTK: path = dgtk.show_directory_chooser_dialog(self.window) if path is None: 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: self.torrent_model.append(self.get_list_from_unique_id(unique_id))