From: Matt Joiner Date: Mon, 12 Sep 2016 07:01:00 +0000 (+1000) Subject: Create zero-length files in the file storage when the torrent storage is opened X-Git-Tag: v1.0.0~588 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=b5ed171ac526856dda367d8bcdb5b8940421acd6;p=btrtrc.git Create zero-length files in the file storage when the torrent storage is opened Fixes #111. --- diff --git a/storage/file.go b/storage/file.go index 3e686aa2..cee96e61 100644 --- a/storage/file.go +++ b/storage/file.go @@ -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