MCPcopy Index your code
hub / github.com/GoEdgeLab/EdgeNode / OpenFileReader

Method OpenFileReader

internal/utils/bfs/blocks_file.go:169–219  ·  view source on GitHub ↗
(fileHash string, isPartial bool)

Source from the content-addressed store, hash-verified

167}
168
169func (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
221func (this *BlocksFile) CloseFileReader(reader *FileReader) error {
222 defer atomic.AddInt32(&this.countRefs, -1)

Callers

nothing calls this directly

Calls 11

checkStatusMethod · 0.95
CheckHashErrFunction · 0.85
AckReadThreadFunction · 0.85
ReleaseReadThreadFunction · 0.85
RLockMethod · 0.80
RUnlockMethod · 0.80
CloneFileHeaderMethod · 0.80
NewFileReaderFunction · 0.70
ResetMethod · 0.65
NameMethod · 0.65
OpenMethod · 0.45

Tested by

no test coverage detected