(fileHash string, isPartial bool)
| 167 | } |
| 168 | |
| 169 | func (this *BlocksFile) OpenFileReader(fileHash string, isPartial bool) (*FileReader, error) { |
| 170 | err := CheckHashErr(fileHash) |
| 171 | if err != nil { |
| 172 | return nil, err |
| 173 | } |
| 174 | |
| 175 | this.mu.RLock() |
| 176 | err = this.checkStatus() |
| 177 | this.mu.RUnlock() |
| 178 | if err != nil { |
| 179 | return nil, err |
| 180 | } |
| 181 | |
| 182 | // 是否存在 |
| 183 | header, ok := this.mFile.CloneFileHeader(fileHash) |
| 184 | if !ok { |
| 185 | return nil, os.ErrNotExist |
| 186 | } |
| 187 | |
| 188 | // TODO 对于partial content,需要传入ranges,用来判断是否有交集 |
| 189 | |
| 190 | if header.IsWriting { |
| 191 | return nil, ErrFileIsWriting |
| 192 | } |
| 193 | |
| 194 | if !isPartial && !header.IsCompleted { |
| 195 | return nil, os.ErrNotExist |
| 196 | } |
| 197 | |
| 198 | // 先尝试从Pool中获取 |
| 199 | select { |
| 200 | case reader := <-this.readerPool: |
| 201 | if reader == nil { |
| 202 | return nil, ErrClosed |
| 203 | } |
| 204 | reader.Reset(header) |
| 205 | atomic.AddInt32(&this.countRefs, 1) |
| 206 | return reader, nil |
| 207 | default: |
| 208 | } |
| 209 | |
| 210 | AckReadThread() |
| 211 | fp, err := os.Open(this.fp.Name()) |
| 212 | ReleaseReadThread() |
| 213 | if err != nil { |
| 214 | return nil, err |
| 215 | } |
| 216 | |
| 217 | atomic.AddInt32(&this.countRefs, 1) |
| 218 | return NewFileReader(this, fp, header), nil |
| 219 | } |
| 220 | |
| 221 | func (this *BlocksFile) CloseFileReader(reader *FileReader) error { |
| 222 | defer atomic.AddInt32(&this.countRefs, -1) |
nothing calls this directly
no test coverage detected