| 64 | } |
| 65 | |
| 66 | func newDecoder(fileIO fileIO, delegate DecoderDelegate, indexFile string) (*Decoder, error) { |
| 67 | indexVolume, err := func() (volume, error) { |
| 68 | bytes, err := fileIO.ReadFile(indexFile) |
| 69 | if err != nil { |
| 70 | return volume{}, err |
| 71 | } |
| 72 | |
| 73 | indexVolume, err := readVolume(bytes) |
| 74 | if err != nil { |
| 75 | return volume{}, err |
| 76 | } |
| 77 | |
| 78 | if indexVolume.header.VolumeNumber != 0 { |
| 79 | // TODO: Relax this check. |
| 80 | return volume{}, errors.New("expected volume number 0 for index volume") |
| 81 | } |
| 82 | return indexVolume, nil |
| 83 | }() |
| 84 | delegate.OnVolumeFileLoad(0, indexFile, indexVolume.header.SetHash, indexVolume.setHash, len(indexVolume.data), err) |
| 85 | if err != nil { |
| 86 | return nil, err |
| 87 | } |
| 88 | |
| 89 | delegate.OnHeaderLoad(indexVolume.header.String()) |
| 90 | for i, entry := range indexVolume.entries { |
| 91 | delegate.OnFileEntryLoad(i+1, len(indexVolume.entries), entry.filename, entry.header.String()) |
| 92 | } |
| 93 | // The comment could be in any encoding, so just pass it |
| 94 | // through as bytes. |
| 95 | delegate.OnCommentLoad(indexVolume.data) |
| 96 | |
| 97 | return &Decoder{ |
| 98 | fileIO, delegate, |
| 99 | indexFile, indexVolume, |
| 100 | nil, |
| 101 | 0, nil, |
| 102 | }, nil |
| 103 | } |
| 104 | |
| 105 | // NewDecoder reads the given index file, which usually has a .PAR |
| 106 | // extension. |