Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multifile Torrents #12

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions fileinfo/fileinfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package fileinfo

import (
"fmt"
"os"
"path/filepath"
)

type FileInfo struct {
Length int `bencode:"length"`
Path []string `bencode:"path"`
}

func (file *FileInfo) WriteToDisk(buf []byte, files []FileInfo, path, torrentName string) error {
outputPath := createPath(path, torrentName, file.Path[0], files)

err := os.MkdirAll(filepath.Dir(outputPath), os.ModePerm)
if err != nil {
return err
}

fileBuf := make([]byte, file.Length)
copy(fileBuf, buf[getOffset(*file, files):getOffset(*file, files)+file.Length])

err = os.WriteFile(outputPath, fileBuf, os.ModePerm)
if err != nil {
return err
}

fmt.Printf("saved file %s\n", file.Path[0])

return nil
}

func getOffset(file FileInfo, files []FileInfo) int {
offset := 0
for _, f := range files {
if f.Length == file.Length && f.Path[0] == file.Path[0] {
return offset
}
offset += f.Length
}
return -1
}

func createPath(path, torrentName, filename string, files []FileInfo) (outputPath string) {
if len(files) < 2 {
return filepath.Join(path, torrentName)
}

return filepath.Join(path, torrentName, filename)
}
66 changes: 66 additions & 0 deletions fileinfo/fileinfo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package fileinfo

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func TestCreatePathSingleFileTorrent(t *testing.T) {
files := []FileInfo{
{
Path: []string{"archlinux-2019.12.01-x86_64.iso"},
Length: 1,
},
}

want := "test/archlinux-2019.12.01-x86_64.iso"
got := createPath("test", files[0].Path[0], "archlinux-2019.12.01-x86_64.iso", files)

assert.Equal(t, want, got)
}

func TestCreatePathMultiFileTorrent(t *testing.T) {
files := []FileInfo{
{
Path: []string{"archlinux-2019.12.01-x86_64.iso"},
Length: 1,
},
{
Path: []string{"sali.txt"},
Length: 1,
},
}

givenPath := "dir1"
torrentName := "dir2"

for _, file := range files {
want := fmt.Sprintf("%s/%s/%s", givenPath, torrentName, file.Path[0])
got := createPath(givenPath, torrentName, file.Path[0], files)
assert.Equal(t, want, got)
}
}

// good idea to test writes to the system?
// func TestWriteToDisk(t *testing.T) {
// buf := []byte("helloworld")
// files := []FileInfo{
// {
// Path: []string{"file1.txt"},
// Length: 5,
// },
// {
// Path: []string{"file2.txt"},
// Length: 5,
// },
// }
// path := "dir1"
// torrentName := "alexandria"

// for _, file := range files {
// err := file.WriteToDisk(buf, files, path, torrentName)
// require.Nil(t, err)
// }
// }
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ github.com/jackpal/bencode-go v0.0.0-20180813173944-227668e840fa h1:ym9I4Q1lJG8n
github.com/jackpal/bencode-go v0.0.0-20180813173944-227668e840fa/go.mod h1:5FSBQ74yhCl5oQ+QxRPYzWMONFnxbL68/23eezsBI5c=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
Expand Down
2 changes: 1 addition & 1 deletion p2p/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func attemptDownloadPiece(c *client.Client, pw *pieceWork) ([]byte, error) {
func checkIntegrity(pw *pieceWork, buf []byte) error {
hash := sha1.Sum(buf)
if !bytes.Equal(hash[:], pw.hash[:]) {
return fmt.Errorf("Index %d failed integrity check", pw.index)
return fmt.Errorf("index %d failed integrity check", pw.index)
}
return nil
}
Expand Down
Loading