r231 - in /debtorrent/branches/unique/DebTorrent/BT1: AptListener.py btformats.py

camrdale-guest at users.alioth.debian.org camrdale-guest at users.alioth.debian.org
Mon Aug 13 22:08:25 UTC 2007


Author: camrdale-guest
Date: Mon Aug 13 22:08:25 2007
New Revision: 231

URL: http://svn.debian.org/wsvn/debtorrent/?sc=1&rev=231
Log:
Add better error checking when creating torrents.

Modified:
    debtorrent/branches/unique/DebTorrent/BT1/AptListener.py
    debtorrent/branches/unique/DebTorrent/BT1/btformats.py

Modified: debtorrent/branches/unique/DebTorrent/BT1/AptListener.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/branches/unique/DebTorrent/BT1/AptListener.py?rev=231&op=diff
==============================================================================
--- debtorrent/branches/unique/DebTorrent/BT1/AptListener.py (original)
+++ debtorrent/branches/unique/DebTorrent/BT1/AptListener.py Mon Aug 13 22:08:25 2007
@@ -30,6 +30,7 @@
 from binascii import a2b_hex, b2a_hex
 from makemetafile import getpieces, getsubpieces, getordering, uniconvert, convert_all
 from DebTorrent.HTTPCache import HTTPCache
+from btformats import check_message
 import os, logging
 from DebTorrent.__init__ import version, product_name,version_short
 
@@ -503,21 +504,25 @@
             logger.warning('Packages file is not in the correct format')
             return 
 
-        sub_pieces = getsubpieces('_'.join(path))
-        
-        (piece_ordering, ordering_headers) = getordering('_'.join(path))
-        if self.config['separate_all']:
-            (piece_ordering_all, ordering_all_headers) = getordering('_'.join(path), all = True)
-        else:
-            piece_ordering_all = {}
-            ordering_all_headers = {}
-    
-        (info, info_all) = getpieces(h, separate_all = self.config['separate_all'],
-                                     sub_pieces = sub_pieces,
-                                     piece_ordering = piece_ordering,
-                                     piece_ordering_all = piece_ordering_all,
-                                     num_pieces = int(ordering_headers.get('NextPiece', 0)),
-                                     num_all_pieces = int(ordering_all_headers.get('NextPiece', 0)))
+        try:
+            sub_pieces = getsubpieces('_'.join(path))
+            
+            (piece_ordering, ordering_headers) = getordering('_'.join(path))
+            if self.config['separate_all']:
+                (piece_ordering_all, ordering_all_headers) = getordering('_'.join(path), all = True)
+            else:
+                piece_ordering_all = {}
+                ordering_all_headers = {}
+        
+            (info, info_all) = getpieces(h, separate_all = self.config['separate_all'],
+                                         sub_pieces = sub_pieces,
+                                         piece_ordering = piece_ordering,
+                                         piece_ordering_all = piece_ordering_all,
+                                         num_pieces = int(ordering_headers.get('NextPiece', 0)),
+                                         num_all_pieces = int(ordering_all_headers.get('NextPiece', 0)))
+        except:
+            logger.exception('Failed to create torrent for: %s', name)
+            return
         
         if info and self.config['separate_all'] in (0, 2, 3):
             self.start_torrent(info, ordering_headers, name, path)
@@ -558,6 +563,12 @@
         if path.count('dists'):
             mirror = 'http://' + '/'.join(path[:path.index('dists')]) + '/'
             response.setdefault('deb_mirrors', []).append(mirror)
+        
+        try:
+            check_message(response)
+        except:
+            logger.exception('Poorly formatted torrent, not starting')
+            return
         
         infohash = sha(bencode(response['info'])).digest()
         

Modified: debtorrent/branches/unique/DebTorrent/BT1/btformats.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/branches/unique/DebTorrent/BT1/btformats.py?rev=231&op=diff
==============================================================================
--- debtorrent/branches/unique/DebTorrent/BT1/btformats.py (original)
+++ debtorrent/branches/unique/DebTorrent/BT1/btformats.py Mon Aug 13 22:08:25 2007
@@ -44,41 +44,45 @@
     piecelengths = info.get('piece lengths')
     if type(piecelengths) != ListType:
         raise ValueError, 'bad metainfo - bad piece lengths list'
-    total_length = 0L
-    piece_bounds = [0L]
-    for length in piecelengths:
-        if type(length) not in ints or length < 0:
-            raise ValueError, 'bad metainfo - bad piece length'
-        total_length += length
-        piece_bounds.append(total_length)
-    if info.has_key('files') == info.has_key('length'):
-        raise ValueError, 'single/multiple file mix'
     files = info.get('files')
     if type(files) != ListType:
         raise ValueError, 'bad metainfo - bad files list'
-    total_length = 0L
-    for f in files:
-        if type(f) != DictType:
-            raise ValueError, 'bad metainfo - bad file value'
-        length = f.get('length')
-        if type(length) not in ints or length < 0:
-            raise ValueError, 'bad metainfo - bad length'
-        total_length += length
-        if piece_bounds[bisect(piece_bounds,total_length)-1] != total_length:
+    total_piece_length = 0L
+    total_file_length = 0L
+    file_length = -1L
+    cur_file = -1
+    for piece_length in piecelengths:
+        if type(piece_length) not in ints or piece_length < 0:
+            raise ValueError, 'bad metainfo - bad piece length'
+        
+        if total_piece_length == total_file_length and (piece_length != 0 or file_length != 0):
+            cur_file += 1
+            f = files[cur_file]
+            if type(f) != DictType:
+                raise ValueError, 'bad metainfo - bad file value'
+            old_file_length = file_length
+            file_length = f.get('length')
+            if type(file_length) not in ints or file_length < 0:
+                raise ValueError, 'bad metainfo - bad length'
+            if old_file_length == 0 and file_length == 0:
+                raise ValueError, 'bad metainfo - cannot have sequential files of length 0'
+            total_file_length += file_length
+            path = f.get('path')
+            if type(path) != ListType:
+                raise ValueError, 'bad metainfo - bad path'
+            for p in path:
+                if type(p) != StringType:
+                    raise ValueError, 'bad metainfo - bad path dir'
+                if not reg.match(p):
+                    raise ValueError, 'path %s disallowed for security reasons' % p
+
+        total_piece_length += piece_length
+        if piece_length > 0 and file_length == 0:
+            raise ValueError, 'bad metainfo - piece length should be 0 for file of length 0'
+        if file_length > 0 and piece_length == 0:
+            raise ValueError, 'bad metainfo - file of length greater than 0 can not contain pieces of length 0'
+        if total_piece_length > total_file_length:
             raise ValueError, 'bad metainfo - file does not end on piece boundary'
-        path = f.get('path')
-        if type(path) != ListType:
-            raise ValueError, 'bad metainfo - bad path'
-        for p in path:
-            if type(p) != StringType:
-                raise ValueError, 'bad metainfo - bad path dir'
-            if not reg.match(p):
-                raise ValueError, 'path %s disallowed for security reasons' % p
-# Removed to speed up checking of large files
-#        for i in xrange(len(files)):
-#            for j in xrange(i):
-#                if files[i]['path'] == files[j]['path']:
-#                    raise ValueError, 'bad metainfo - duplicate path'
 
 def check_message(message):
     """Checks the metainfo dictionary for conformance.




More information about the Debtorrent-commits mailing list