func()

in pkg/files/store/store.go [86:123]


func (s *store) Open(c pcontext.Context) (File, error) {

	chunkKey := c.GetString(pcontext.FileChunkCtxKey)
	tokens := strings.Split(chunkKey, files.FileChunkKeySep)
	name := tokens[0]
	alignedOff, _ := strconv.ParseInt(tokens[1], 10, 64)

	log := pcontext.Logger(c)
	if pcontext.IsRequestFromAPeer(c) {
		// This request came from a peer. Don't serve it unless we have the requested range cached.
		if ok := s.cache.Exists(name, alignedOff); !ok {
			log.Info().Str("name", name).Msg("peer request not cached")
			return nil, os.ErrNotExist
		}
	}

	f := &file{
		Name:   name,
		store:  s,
		cur:    0,
		size:   0,
		reader: reader.NewReader(c, s.router, s.resolveRetries, s.resolveTimeout, s.metricsRecorder),
	}

	if pcontext.IsRequestFromAPeer(c) {
		// Ensure this file can only serve the requested chunk.
		// This is to prevent infinite loops when a peer requests a file that is not cached.
		f.chunkOffset = alignedOff
	}

	fileSize, err := f.Fstat() // Fstat sets up the file size appropriately.

	if s.prefetchable {
		f.prefetch(0, fileSize)
	}

	return f, err
}