Create zero-length files in the file storage when the torrent storage is opened

Fixes #111.
This commit is contained in:
Matt Joiner 2016-09-12 17:01:00 +10:00
parent 4d10fd3496
commit b5ed171ac5
1 changed files with 24 additions and 0 deletions

View File

@ -24,6 +24,10 @@ func NewFile(baseDir string) ClientImpl {
}
func (fs *fileStorage) OpenTorrent(info *metainfo.Info, infoHash metainfo.Hash) (TorrentImpl, error) {
err := CreateNativeZeroLengthFiles(info, fs.baseDir)
if err != nil {
return nil, err
}
return &fileTorrentStorage{
fs,
info,
@ -57,6 +61,26 @@ func (fs *fileTorrentStorage) Close() error {
return nil
}
// Creates natives files for any zero-length file entries in the info. This is
// a helper for file-based storages, which don't address or write to zero-
// length files because they have no corresponding pieces.
func CreateNativeZeroLengthFiles(info *metainfo.Info, baseDir string) (err error) {
for _, fi := range info.UpvertedFiles() {
if fi.Length != 0 {
continue
}
name := filepath.Join(append([]string{baseDir, info.Name}, fi.Path...)...)
os.MkdirAll(filepath.Dir(name), 0750)
var f io.Closer
f, err = os.Create(name)
if err != nil {
break
}
f.Close()
}
return
}
// Exposes file-based storage of a torrent, as one big ReadWriterAt.
type fileStorageTorrent struct {
fts *fileTorrentStorage