Package DebTorrent :: Package BT1 :: Module Storage :: Class Storage
[hide private]
[frames] | no frames]

Class Storage

source code

Low-level writing of files.

Control the low-level management of files in the download. Contains functions to open and close, read and write, enable and disable, flush and delete, all the files in the download. Also serves as an abstraction layer, as the reading and writing is called with no knowledge of file boundaries.

Instance Methods [hide private]
 
__init__(self, files, piece_lengths, doneflag, config, enabled_files=None)
Initializes the Storage.
source code
 
_lock_file(self, name, f)
Lock a file on Linux.
source code
 
_unlock_file(self, name, f)
Unlock a file on Linux.
source code
boolean
was_preallocated(self, pos, length)
Check if a download location was pre-allocated.
source code
 
_sync(self, file)
Synchronize a file to disk.
source code
 
sync(self)
Synchronize all read/write files to disk.
source code
 
set_readonly(self, f=None)
Set a file (or all files) to be read-only.
source code
long
get_total_length(self)
Get the total length of the download.
source code
file handle
_open(self, file, mode)
Open a file with the given mode.
source code
 
_close(self, file)
Close the file
source code
 
_close_file(self, file)
Close the file and release the handle.
source code
file handle
_get_file_handle(self, file, for_write)
Get a new or existing flie handle for the file.
source code
 
_reset_ranges(self)
Re-initialize the ranges from the working copies.
source code
(long, long, long, string)
get_file_range(self, index)
Get the file name and range that corresponds to this piece.
source code
list of tuple of (string, long, long)
_intervals(self, pos, amount)
Get the files that are within the range.
source code
DebTorrent.piecebuffer.SingleBuffer
read(self, pos, amount, flush_first=True)
Read data from the download.
source code
 
write(self, pos, s)
Write data to the download.
source code
 
make_directories(self, file)
Create missing parent directories for a file.
source code
 
top_off(self)
Extend all files to their appropriate length.
source code
 
flush(self)
Flush all files to disk.
source code
 
close(self)
Close all open files.
source code
tuple
_get_disabled_ranges(self, f)
Calculate the file ranges for the disabled file.
source code
 
set_bufferdir(self, dir)
Sets the buffer directory.
source code
 
enable_file(self, f)
Enable a file for writing.
source code
 
disable_file(self, f)
Disable a file from writing.
source code
list of int
get_piece_update_list(self, f)
Get the list of pieces the file shares with other files.
source code
 
delete_file(self, f)
Delete the file.
source code
dictionary
pickle(self)
Create a dictionary representing the current state of the download.
source code
list of int
unpickle(self, data)
Extract the current status of the download from a pickled dictionary.
source code
Instance Variables [hide private]
 
reset_file_status(self)
a shortcut to the _reset_ranges method
source code
list of long begins
the offset within the download that each non-disabled file begins at
string bufferdir
the buffer directory
list of boolean disabled
list of true for the files that are disabled
list of tuple disabled_ranges
for each file, a tuple containing the working range, shared pieces, and disabled range (see _get_disabled_ranges for their meaning)
threading.Event doneflag
the flag that indicates when the program is to be shutdown
list of (int, int) file_pieces
for each file, the starting and ending piece of the file
list of (long, long, long, string) file_ranges
for each file, the start offset within the download, end offset, offset within the file, and file name
list of tuple of (string, long) files
the files list from the info of the metainfo dictionary
list handlebuffer
the list of open files, in the order of most recently accessed, with the most recently accessed at the end of the list (only if there is a limit on the number of open files, otherwise None)
dictionary of {string, file handle} handles
the file handles that are open, keys are file names and values are the file handles
lock lock
a threading lock object for synchorizing threads (semaphore)
method lock_file
locks a file (if file locking is enabled, otherwise does nothing)
boolean lock_while_reading
whether to lock files while reading them
int max_files_open
the maximum number of files to have open at a time (0 means no maximum)
dictionary of {string, long} mtimes
the last modified time of each file (by name)
dictionary piece_files
for each piece, the starting and ending offset of the piece in the file, the length of the file, and the file name
list of long piece_lengths
the list of piece lengths
list of list of (long, long, long, string) ranges
for each file, the list of files to be written when writing to that file (may not be the actual file, i.e if it is disabled)
dictionary of {string, long} sizes
the desired length of each file (by name)
dictionary of {string, long} tops
the current length of each file (by name)
long total_length
the total length in bytes of the download
method unlock_file
unlocks a file (if file locking is enabled, otherwise does nothing)
dictionary of {string, int} whandles
the files that are open for writing, keys are the file names and values are all 1
list of list of (long, long, long, string) working_ranges
For each file, the list of files to be written when writing to that file (may not be the actual file, i.e if it is disabled).
Method Details [hide private]

__init__(self, files, piece_lengths, doneflag, config, enabled_files=None)
(Constructor)

source code 

Initializes the Storage.

Initializes some variables, and calculates defaults for others, such as which pieces are contained by which file.
Parameters:
  • files (list of tuple of (string, long)) - the files list from the info of the metainfo dictionary
  • piece_lengths (list of long) - the list of piece lengths
  • doneflag (threading.Event) - the flag that indicates when the program is to be shutdown
  • config (dictionary) - the configuration information
  • enabled_files (list of boolean) - list of true for the files that are enabled (optional, default is all files disabled)

_lock_file(self, name, f)

source code 
Lock a file on Linux.
Parameters:
  • name (string) - The file name to lock (only used to get the file size)
  • f (file) - a file handle for the file to lock

_unlock_file(self, name, f)

source code 
Unlock a file on Linux.
Parameters:
  • name (string) - The file name to unlock (only used to get the file size)
  • f (file) - a file handle for the file to unlock

was_preallocated(self, pos, length)

source code 
Check if a download location was pre-allocated.
Parameters:
  • pos (long) - the location to start checking
  • length (long) - the amount of the download to check
Returns: boolean
whether the download location was pre-allocated

_sync(self, file)

source code 

Synchronize a file to disk.

Closes the open file so that it is synchronize to disk, the next time it is referenced it will be reopened.
Parameters:
  • file (string) - the name of the file to sync

set_readonly(self, f=None)

source code 

Set a file (or all files) to be read-only.

Synchronizes the file (or all read/write files if none is given) to disk. The next time the file is accessed it will be reopened.
Parameters:
  • f (int) - the number of the file to set read-only (optional, default is to set all read/write files read-only)

get_total_length(self)

source code 
Get the total length of the download.
Returns: long
the total length of the download

_open(self, file, mode)

source code 
Open a file with the given mode.
Parameters:
  • file (string) - the file name to open
  • mode (string) - the mode to open the file in (r = read, w = write, a = append, add b for binary)
Returns: file handle
the file handle to access the file with
Raises:
  • IOError - if the file has been modified since it was last read

_close(self, file)

source code 
Close the file
Parameters:
  • file (string) - the name of the file to close

_close_file(self, file)

source code 
Close the file and release the handle.
Parameters:
  • file (string) - the name of the file to close

_get_file_handle(self, file, for_write)

source code 
Get a new or existing flie handle for the file.
Parameters:
  • file (string) - the name of the file to get a handle for
  • for_write (boolean) - whether to open the file for writing
Returns: file handle
the file handle that can be used for the file
Raises:
  • IOError - if the file can not be opened

get_file_range(self, index)

source code 
Get the file name and range that corresponds to this piece.
Parameters:
  • index (int) - the piece index to get a file range for
Returns: (long, long, long, string)
the start and end offsets of the piece in the file, the length of the file, and the name of the file

_intervals(self, pos, amount)

source code 

Get the files that are within the range.

Finds all the files that occur within a given range in the download, and return a list of them. Includes the range of the file that is inside the range, which will be the start (0) and end (length) of the file unless it goes past the beginning or end of the range.
Parameters:
  • pos (long) - the start of the range within the download
  • amount (long) - the length of the range
Returns: list of tuple of (string, long, long)
the list of files and their start and end offsets in the range

read(self, pos, amount, flush_first=True)

source code 
Read data from the download.
Parameters:
  • pos (long) - the offset in the download to start reading from
  • amount (long) - the length of the data to read
  • flush_first (boolean) - whether to flush the files before reading the data (optional, default is not to flush)
Returns: DebTorrent.piecebuffer.SingleBuffer
the data that was read
Raises:
  • IOError - if the read data is not complete

write(self, pos, s)

source code 
Write data to the download.
Parameters:
  • pos (long) - the offset in the download to start writing at
  • s (string) - data to write

make_directories(self, file)

source code 
Create missing parent directories for a file.
Parameters:
  • file (string) - the file name to create directories for

_get_disabled_ranges(self, f)

source code 

Calculate the file ranges for the disabled file.

Calculates, based on the file lengths and piece lengths, the ranges to write for the file. There are three lists calculated.

The working range is the list of files and file offsets to write if the file is enabled.

The shared pieces is a list of piece numbers that the file shares with other files.

The disabled range is the list of files and file offsets to write if the file is disabled.
Parameters:
  • f (int) - the index of the file
Returns: tuple
a tuple containing the working range, shared pieces, and disabled range

set_bufferdir(self, dir)

source code 
Sets the buffer directory.
Parameters:
  • dir (string) - the new buffer directory

enable_file(self, f)

source code 
Enable a file for writing.
Parameters:
  • f (int) - the index of the file to enable

disable_file(self, f)

source code 
Disable a file from writing.
Parameters:
  • f (int) - the index of the file to disable

get_piece_update_list(self, f)

source code 
Get the list of pieces the file shares with other files.
Parameters:
  • f (int) - the index of the file to disable
Returns: list of int
the list of piece indexes

delete_file(self, f)

source code 
Delete the file.
Parameters:
  • f (int) - the index of the file to delete

pickle(self)

source code 

Create a dictionary representing the current state of the download.

Pickled data format:
   d['files'] = [ file name, size, mtime {, file name, size, mtime...} ]
               file name in torrent, and the size and last modification
               time for those files.  Missing files are either empty
               or disabled.
Returns: dictionary
the pickled current status of the download

unpickle(self, data)

source code 

Extract the current status of the download from a pickled dictionary.

Assumes all previously-disabled files have already been disabled.
Parameters:
  • data (dictionary) - the pickled current status of the download
Returns: list of int
a list of the currently enabled pieces

Instance Variable Details [hide private]

working_ranges

For each file, the list of files to be written when writing to that file (may not be the actual file, i.e if it is disabled). Ranges are temporarily stored here, before eventually being written to self.ranges by _reset_ranges to be used.
Type:
list of list of (long, long, long, string)