]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Create zero-length files in the file storage when the torrent storage is opened
authorMatt Joiner <anacrolix@gmail.com>
Mon, 12 Sep 2016 07:01:00 +0000 (17:01 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Mon, 12 Sep 2016 07:01:00 +0000 (17:01 +1000)
Fixes #111.

storage/file.go

index 3e686aa26f58634a48832f9b323d5191f1b76743..cee96e61852d838292f589a5e64f6994c90071bd 100644 (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