Skip to content

Commit

Permalink
fix the error when newing a Info from a directory
Browse files Browse the repository at this point in the history
  • Loading branch information
xgfone committed Feb 8, 2021
1 parent b983dc1 commit fd3825a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 29 deletions.
76 changes: 47 additions & 29 deletions metainfo/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,49 +52,67 @@ type Info struct {
Files []File `json:"files,omitempty" bencode:"files,omitempty"` // BEP 3
}

// NewInfoFromFilePath returns a new Info from a file or directory.
func NewInfoFromFilePath(root string, pieceLength int64) (info Info, err error) {
info.Name = filepath.Base(root)
info.PieceLength = pieceLength
err = filepath.Walk(root, func(path string, fi os.FileInfo, err error) error {
if err != nil {
// getAllInfoFiles returns the list of the all files in a certain directory
// recursively.
func getAllInfoFiles(rootDir string) (files []File, err error) {
err = filepath.Walk(rootDir, func(path string, fi os.FileInfo, err error) error {
if err != nil || fi.IsDir() {
return err
}

if path == root && !fi.IsDir() { // The root is a file.
info.Length = fi.Size()
return nil
}

relPath, err := filepath.Rel(root, path)
relPath, err := filepath.Rel(rootDir, path)
if err != nil {
return fmt.Errorf("error getting relative path: %s", err)
}

info.Files = append(info.Files, File{
Paths: strings.Split(relPath, string(filepath.Separator)),
paths := strings.Split(relPath, string(filepath.Separator))
for _, name := range paths {
if name == ".git" {
return nil
}
}

files = append(files, File{
Paths: paths,
Length: fi.Size(),
})

return nil
})

if err == nil {
sort.Sort(files(info.Files))
info.Pieces, err = GeneratePiecesFromFiles(info.AllFiles(), info.PieceLength,
func(file File) (io.ReadCloser, error) {
if _len := len(file.Paths); _len > 0 {
paths := make([]string, 0, _len+1)
paths = append(paths, root)
paths = append(paths, file.Paths...)
return os.Open(filepath.Join(paths...))
}
return os.Open(root)
})
return
}

if err != nil {
err = fmt.Errorf("error generating pieces: %s", err)
}
// NewInfoFromFilePath returns a new Info from a file or directory.
func NewInfoFromFilePath(root string, pieceLength int64) (info Info, err error) {
root = filepath.Clean(root)

fi, err := os.Stat(root)
if err != nil {
return
} else if !fi.IsDir() {
info.Length = fi.Size()
} else if info.Files, err = getAllInfoFiles(root); err != nil {
return
}

sort.Sort(files(info.Files))
info.Pieces, err = GeneratePiecesFromFiles(info.AllFiles(), pieceLength,
func(file File) (io.ReadCloser, error) {
if _len := len(file.Paths); _len > 0 {
paths := make([]string, 0, _len+1)
paths = append(paths, root)
paths = append(paths, file.Paths...)
return os.Open(filepath.Join(paths...))
}
return os.Open(root)
})

if err == nil {
info.Name = filepath.Base(root)
info.PieceLength = pieceLength
} else {
err = fmt.Errorf("error generating pieces: %s", err)
}

return
Expand Down
23 changes: 23 additions & 0 deletions metainfo/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,26 @@ func TestInfo_GetFileByOffset(t *testing.T) {
t.Errorf("expect fileOffset='%d', but got '%d'", 300, fileOffset)
}
}

func TestNewInfoFromFilePath(t *testing.T) {
info, err := NewInfoFromFilePath("info.go", PieceSize256KB)
if err != nil {
t.Error(err)
} else if info.Name != "info.go" || info.Files != nil {
t.Errorf("invalid info %+v\n", info)
}

info, err = NewInfoFromFilePath("../metainfo", PieceSize256KB)
if err != nil {
t.Error(err)
} else if info.Name != "metainfo" || info.Files == nil || info.Length > 0 {
t.Errorf("invalid info %+v\n", info)
}

info, err = NewInfoFromFilePath("../../bt", PieceSize256KB)
if err != nil {
t.Error(err)
} else if info.Name != "bt" || info.Files == nil || info.Length > 0 {
t.Errorf("invalid info %+v\n", info)
}
}

0 comments on commit fd3825a

Please sign in to comment.