// Fetch compares two patches from a global and a local branch and updates the local branch to match the global one.
// It only replaces or adds files, but does not delete any.
// It may return an error if the origin handling fails.
551079
func Fetch(dir, target string, pool int, dynamic bool) error {
	local, err := GetOrigin(dir)
	if err != nil {
		log.Error("bad local origin:", err)
		return err
	}
	localHashes, localSource, err := local.Scan()
	if target != "" {
		localSource = target
	}
	global, err := GetOrigin(localSource)
	if err != nil {
		log.Error("bad global origin:", err)
		return err
	}
	globalHashes, globalSource, err := global.Scan()
	if globalSource != localSource {
		log.Warning("unverified origin:", globalSource)
	} else {
		log.Notice("verified origin:", globalSource)
	}

	missingHashes := Compare(localHashes, globalHashes)
	if pool < 2 {
		err = FetchSpecific(dir, global, missingHashes)
	} else {
		if dynamic {
			pool *= runtime.NumCPU()
		}
		err = FetchSpecificAsync(dir, global, missingHashes, pool)
	}
	if err != nil {
		log.Error("failed to fetch files:", err)
		return err
	}
	log.Notice("fetched", len(missingHashes), "files from origin")

	return Write(dir, globalSource, globalHashes)
}
1148578
func (sto *unionStorage) Fetch(ctx context.Context, b blob.Ref) (file io.ReadCloser, size uint32, err error) {
	type result struct {
		file io.ReadCloser
		size uint32
		err  error
	}
	results := make(chan result, len(sto.subsets))
	var wg sync.WaitGroup
	for _, bs := range sto.subsets {
		bs := bs
		wg.Add(1)
		go func() {
			defer wg.Done()
			var res result
			res.file, res.size, res.err = bs.Fetch(ctx, b)
			results <- res
		}()
	}
	go func() {
		wg.Wait()
		close(results)
	}()
	var firstErr error
	var firstRes result
	for r := range results {
		if r.err != nil {
			if firstErr == nil {
				firstErr = r.err
			}
			continue
		}
		if firstRes.file != nil {
			if r.file != nil {
				r.file.Close() // don't need, we already have a successful Fetch
			}
			continue
		}

		firstRes = r
	}
	if firstRes.file != nil {
		return firstRes.file, firstRes.size, nil
	}
	return nil, 0, firstErr
}
582494
func (c HTTPClient) Fetch(instanceID string) (AgentSettings, error) {
	endpoint := fmt.Sprintf("%s/instances/%s/settings", c.options.EndpointWithCredentials(), instanceID)
	c.logger.Debug(httpClientLogTag, "Fetching agent settings from registry endpoint '%s'", endpoint)

	request, err := http.NewRequest("GET", endpoint, nil)
	if err != nil {
		return AgentSettings{}, bosherr.WrapErrorf(err, "Creating GET request for registry endpoint '%s'", endpoint)
	}

	httpResponse, err := c.doRequest(request)
	if err != nil {
		return AgentSettings{}, bosherr.WrapErrorf(err, "Fetching agent settings from registry endpoint '%s'", endpoint)
	}

	defer httpResponse.Body.Close()

	if httpResponse.StatusCode != http.StatusOK {
		return AgentSettings{}, bosherr.Errorf("Received status code '%d' when fetching agent settings from registry endpoint '%s'", httpResponse.StatusCode, endpoint)
	}

	httpBody, err := ioutil.ReadAll(httpResponse.Body)
	if err != nil {
		return AgentSettings{}, bosherr.WrapErrorf(err, "Reading agent settings response from registry endpoint '%s'", endpoint)
	}

	var settingsResponse agentSettingsResponse
	if err = json.Unmarshal(httpBody, &settingsResponse); err != nil {
		return AgentSettings{}, bosherr.WrapErrorf(err, "Unmarshalling agent settings response from registry endpoint '%s', contents: '%s'", endpoint, httpBody)
	}

	var agentSettings AgentSettings
	if err = json.Unmarshal([]byte(settingsResponse.Settings), &agentSettings); err != nil {
		return AgentSettings{}, bosherr.WrapErrorf(err, "Unmarshalling agent settings response from registry endpoint '%s', contents: '%s'", endpoint, httpBody)
	}

	c.logger.Debug(httpClientLogTag, "Received agent settings from registry endpoint '%s', contents: '%s'", endpoint, httpBody)
	return agentSettings, nil
}
54444
func Fetch(baseURL string, resourceType string, id string) (*jsh.Document, *http.Response, error) {
	request, err := FetchRequest(baseURL, resourceType, id)
	if err != nil {
		return nil, nil, err
	}

	return Do(request, jsh.ObjectMode)
}
314821
func (s *simpleCache) Fetch(id uint64) (*Row, bool) {
	m, ok := s.cache[id]
	return m, ok
}
129660
func fetchRef(ref string, filter *filepathfilter.Filter) bool {
	pointers, err := pointersToFetchForRef(ref, filter)
	if err != nil {
		Panic(err, "Could not scan for Git LFS files")
	}
	return fetchAndReportToChan(pointers, filter, nil)
}
900055
func Fetch(ctx context.Context, client *containerd.Client, ref string, config *FetchConfig) (images.Image, error) {
	ongoing := newJobs(ref)

	pctx, stopProgress := context.WithCancel(ctx)
	progress := make(chan struct{})

	go func() {
		if config.ProgressOutput != nil {
			// no progress bar, because it hides some debug logs
			showProgress(pctx, ongoing, client.ContentStore(), config.ProgressOutput)
		}
		close(progress)
	}()

	h := images.HandlerFunc(func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
		if desc.MediaType != images.MediaTypeDockerSchema1Manifest {
			ongoing.add(desc)
		}
		return nil, nil
	})

	log.G(pctx).WithField("image", ref).Debug("fetching")
	labels := commands.LabelArgs(config.Labels)
	opts := []containerd.RemoteOpt{
		containerd.WithPullLabels(labels),
		containerd.WithResolver(config.Resolver),
		containerd.WithImageHandler(h),
		containerd.WithSchema1Conversion,
		containerd.WithAppendDistributionSourceLabel(),
	}

	for _, platform := range config.Platforms {
		opts = append(opts, containerd.WithPlatform(platform))
	}

	img, err := client.Fetch(pctx, ref, opts...)
	stopProgress()
	if err != nil {
		return images.Image{}, err
	}

	<-progress
	return img, nil
}
293417
func (c *connection) Fetch(req *proto.FetchReq) (*proto.FetchResp, error) {
	b, err := c.sendRequest(req)
	if err != nil {
		return nil, err
	}

	resp, err := proto.ReadVersionedFetchResp(bytes.NewReader(b), req.GetVersion())
	if err != nil {
		return nil, err
	}

	// Compressed messages are returned in full batches for efficiency
	// (the broker doesn't need to decompress).
	// This means that it's possible to get some leading messages
	// with a smaller offset than requested. Trim those.
	for ti := range resp.Topics {
		topic := &resp.Topics[ti]
		reqTopic := &req.Topics[ti]
		for pi := range topic.Partitions {
			partition := &topic.Partitions[pi]
			requestedOffset := reqTopic.Partitions[pi].FetchOffset
			i := 0
			if partition.MessageVersion < 2 {
				for _, msg := range partition.Messages {
					if msg.Offset >= requestedOffset {
						break
					}
					i++
				}
				partition.Messages = partition.Messages[i:]
			} else {
				firstOffset := partition.RecordBatch.FirstOffset
				for _, rec := range partition.RecordBatch.Records {
					if firstOffset+rec.OffsetDelta >= requestedOffset {
						break
					}
					i++
				}
				partition.RecordBatch.Records = partition.RecordBatch.Records[i:]
			}
		}
	}
	return resp, nil
}
1316830
func (l *Log) fetch(nextToken *string, start int64, ch chan<- *Event) (*string, int64, error) {
	res, err := l.Service.FilterLogEvents(&cloudwatchlogs.FilterLogEventsInput{
		LogGroupName:  &l.GroupName,
		FilterPattern: &l.FilterPattern,
		StartTime:     &start,
		NextToken:     nextToken,
	})

	if e, ok := err.(awserr.Error); ok {
		if e.Code() == "ResourceNotFoundException" {
			l.Log.Debug("not found")
			return nil, 0, nil
		}
	}

	if err != nil {
		return nil, 0, err
	}

	for _, event := range res.Events {
		start = *event.Timestamp + 1
		ch <- &Event{
			GroupName: l.GroupName,
			Message:   *event.Message,
		}
	}

	return res.NextToken, start, nil
}
460208
func (t *SwitchesTable) fetch(c context.Context) error {
	db := database.Get(c)
	rows, err := db.QueryContext(c, `
		SELECT id, name, description, ports, state, rack_id
		FROM switches
	`)
	if err != nil {
		return errors.Annotate(err, "failed to select switches").Err()
	}
	defer rows.Close()
	for rows.Next() {
		s := &Switch{}
		if err := rows.Scan(&s.Id, &s.Name, &s.Description, &s.Ports, &s.State, &s.RackId); err != nil {
			return errors.Annotate(err, "failed to scan switch").Err()
		}
		t.current = append(t.current, s)
	}
	return nil
}
82929
func (r *report) fetch(force bool, secs time.Duration) error {
	r.mu.Lock()
	defer r.mu.Unlock()
	if r.p != nil && !force {
		return nil
	}
	if secs == 0 {
		secs = 60 * time.Second
	}
	url := fmt.Sprintf("%s/debug/_gom?view=profile&name=%s", *target, r.name)
	p, err := fetch.FetchProfile(url, secs)
	if err != nil {
		return err
	}
	if err := symbolz.Symbolize(fmt.Sprintf("%s/debug/_gom?view=symbol", *target), fetch.PostURL, p); err != nil {
		return err
	}
	r.p = p
	return nil
}
1074529
func (f *Fetcher) fetch(c context.Context) {
	defer close(f.logC)

	lb := logBuffer{}
	nextFetchIndex := f.o.Index
	lastSentIndex := types.MessageIndex(-1)
	tidx := types.MessageIndex(-1)

	c, cancelFunc := context.WithCancel(c)
	bytes := int64(0)
	logFetchBaseC := make(chan *fetchResponse)
	var logFetchC chan *fetchResponse
	defer func() {
		// Reap our fetch goroutine, if still fetching.
		if logFetchC != nil {
			cancelFunc()
			<-logFetchC
		}
		close(logFetchBaseC)
	}()

	errOut := func(err error) {
		f.logC <- &logResponse{err: err}
	}

	count := int64(0)
	for tidx < 0 || lastSentIndex < tidx {
		// If we're configured with an upper delivery bound and we've delivered
		// that many entries, we're done.
		remaining := int64(-1)
		if f.o.Count > 0 {
			remaining = f.o.Count - count
			if remaining <= 0 {
				log.Fields{
					"count": count,
				}.Debugf(c, "Exceeded maximum log count.")
				break
			}
		}

		// If we have a buffered log, prepare it for sending.
		var sendC chan<- *logResponse
		var lr *logResponse
		if le := lb.current(); le != nil {
			lr = &logResponse{
				log:   le,
				size:  f.sizeOf(le),
				index: types.MessageIndex(le.StreamIndex),
			}
			sendC = f.logC
		}

		// If we're not currently fetching logs, and we are below our thresholds,
		// request a new batch of logs.
		if logFetchC == nil && (tidx < 0 || nextFetchIndex <= tidx) {

			// We always have a byte constraint. Are we below it?
			fetchCount := f.applyConstraint(int64(f.o.BufferCount), int64(lb.size()))
			fetchBytes := f.applyConstraint(f.o.BufferBytes, bytes)

			// Apply maximum log count constraint, if one is specified.
			if remaining >= 0 {
				// Factor in our currently-buffered logs.
				remaining -= int64(lb.size())

				switch {
				case remaining <= 0:
					// No more to fetch, we've buffered all the logs we're going to need.
					fetchCount = -1
				case fetchCount == 0, remaining < fetchCount:
					fetchCount = remaining
				}
			}

			// Fetch logs if neither constraint has vetoed.
			if fetchCount >= 0 && fetchBytes >= 0 {
				logFetchC = logFetchBaseC
				req := fetchRequest{
					index: nextFetchIndex,
					count: int(fetchCount),
					bytes: fetchBytes,
					respC: logFetchC,
				}

				log.Fields{
					"index": req.index,
					"count": req.count,
					"bytes": req.bytes,
				}.Debugf(c, "Buffering next round of logs...")
				go f.fetchLogs(c, &req)
			}
		}

		select {
		case <-c.Done():
			// Our Context has been cancelled. Terminate and propagate that error.
			log.WithError(c.Err()).Debugf(c, "Context has been cancelled.")
			errOut(c.Err())
			return

		case resp := <-logFetchC:
			logFetchC = nil
			if resp.err != nil {
				log.WithError(resp.err).Errorf(c, "Error fetching logs.")
				errOut(resp.err)
				return
			}

			if f.o.RequireCompleteStream && resp.tidx == -1 {
				errOut(ErrIncompleteStream)
				return
			}

			// Account for each log and add it to our buffer.
			if len(resp.logs) > 0 {
				for _, le := range resp.logs {
					bytes += int64(f.sizeOf(le))
				}
				nextFetchIndex = types.MessageIndex(resp.logs[len(resp.logs)-1].StreamIndex) + 1
				lb.append(resp.logs...)
			}
			if resp.tidx >= 0 {
				tidx = resp.tidx
			}

		case sendC <- lr:
			bytes -= lr.size
			lastSentIndex = lr.index

			count++
			lb.next()
		}
	}

	// We've punted the full log stream.
	errOut(io.EOF)
}
343353
func (c *LayeredCache) Fetch(primary, secondary string, duration time.Duration, fetch func() (interface{}, error)) (*Item, error) {
	item := c.Get(primary, secondary)
	if item != nil {
		return item, nil
	}
	value, err := fetch()
	if err != nil {
		return nil, err
	}
	return c.set(primary, secondary, value, duration), nil
}
1163577
func (ds *DiskSource) Fetch(loc BlockLocation, data []byte) (int, error) {
	addr := ds.BlockAddresses.Get(loc)
	if addr == "" {
		return 0, errors.WithStack(fmt.Errorf("no address for block %+v", loc))
	}
	path := filepath.Join(ds.BasePath, addr)

	fr, err := os.Open(path)
	if err != nil {
		return 0, errors.WithStack(err)
	}

	defer fr.Close()

	if ds.Decompressor == nil {
		bytesRead, err := io.ReadFull(fr, data)
		if err != nil {
			if err == io.ErrUnexpectedEOF {
				// all good
			} else {
				return 0, errors.WithStack(err)
			}
		}

		return bytesRead, nil
	}

	return ds.Decompressor.Decompress(data, fr)
}
714114
func fetch(params string, c *Conn, m mailstore.Message) (string, error) {
	paramList := util.SplitParams(params)

	// Prepare the list of responses
	responseParams := make([]string, 0, len(paramList))

	for _, param := range paramList {
		paramResponse, err := fetchParam(param, c, m)
		if err != nil {
			return "", err
		}
		responseParams = append(responseParams, paramResponse)
	}
	return strings.Join(responseParams, " "), nil
}
827996
func (mtt *mttNode) Fetch(ns []string) ([]*metricType, error) {
	children := mtt.fetch(ns)
	var mts []*metricType
	for _, child := range children {
		for _, mt := range child.mts {
			mts = append(mts, mt)
		}
	}
	if len(mts) == 0 && len(ns) > 0 {
		return nil, errorMetricsNotFound("/" + strings.Join(ns, "/"))
	}
	return mts, nil
}
1052178
func (t *RacksTable) fetch(c context.Context) error {
	db := database.Get(c)
	rows, err := db.QueryContext(c, `
		SELECT id, name, description, state, datacenter_id
		FROM racks
	`)
	if err != nil {
		return errors.Annotate(err, "failed to select racks").Err()
	}
	defer rows.Close()
	for rows.Next() {
		rack := &Rack{}
		if err := rows.Scan(&rack.Id, &rack.Name, &rack.Description, &rack.State, &rack.DatacenterId); err != nil {
			return errors.Annotate(err, "failed to scan rack").Err()
		}
		t.current = append(t.current, rack)
	}
	return nil
}