adriengibrat / torrent-rw

php5 class to read and write .torrent files
GNU General Public License v3.0
325 stars 103 forks source link

New torrents from certain directory structures are accidentally flattened and files are discarded #22

Closed fabacab closed 8 years ago

fabacab commented 9 years ago

When a root directory that contains more than one descendant directory who, themselves, both share a common parent directory but whose parent has no files, the Torrent::files method incorrectly identifies the root directory as one of the descendant directories rather than their shared parent directory.

Specifically, given a directory structure like this:

$ tree
.
└── child-directory
    ├── descendant-dir1
    │   └── file1.txt
    └── descendant-dir2
        └── file2.txt

creating a new torrent from either . or ./child-directory like so:

<?php
require_once 'Torrent-RW/Torrent.php';
$torrent = new Torrent('/path/to/child-directory', 'udp://tracker.publicbt.com:80');
$torrent->save('test.torrent');

will create a torrent file whose info dictionary only lists the contents of descendant-dir2:

$ lstor --raw test.torrent 
{'announce': 'http://tracker.openbt.com/',
 'created by': 'Torrent RW PHP Class - http://github.com/************/**********',
 'creation date': 1421322276,
 'info': {'files': [{'length': 0, 'path': ['file2.txt']}],
          'name': 'descendant-dir2',
          'piece length': 262144,
          'pieces': '<0 piece hashes>'}}
INFO:pyrocore.scripts.lstor.MetafileLister:Total time: 0.047 seconds.

This is unexpected behavior. Instead, both the contents of descendant-dir1 and the contents of descendant-dir2 should have been included, with child-directory (their common root) being correctly identified as the torrent root.

However, if we then add a file inside of child-directory, so that our directory tree looks like this:

$ tree
.
└── child-directory
    ├── descendant-dir1
    │   └── file1.txt
    ├── descendant-dir2
    │   └── file2.txt
    └── fileX.txt

Then the correct root (child-directory) is identified:

MacBookPro:torrent-test meitar$ php ./test.php 
MacBookPro:torrent-test meitar$ lstor --raw test.torrent 
{'announce': 'http://tracker.openbt.com/',
 'created by': 'Torrent RW PHP Class - http://github.com/************/**********',
 'creation date': 1421322693,
 'info': {'files': [{'length': 0, 'path': ['fileX.txt']},
                    {'length': 0, 'path': ['descendant-dir2', 'file2.txt']},
                    {'length': 0, 'path': ['descendant-dir1', 'file1.txt']}],
          'name': 'child-directory',
          'piece length': 262144,
          'pieces': '<0 piece hashes>'}}
INFO:pyrocore.scripts.lstor.MetafileLister:Total time: 0.029 seconds.
adriengibrat commented 8 years ago

far too late, i'm sorry, this seem to be an actual bug, thanks to report it so accuratly. I'm not maintaining this project as you may have guessed... but i'll accept merge request.

fabacab commented 8 years ago

Not sure if I'll have time to write a patch since the easy workaround is to supply directories with files but if I ever come back to that project with enough time I will upstream whatever improvements/fixes I make.