// FetchSpecific downloads the files in the set from the origin and stores it in the target directory.
// It may fail if either can't fetch the file or can't create the directory / file.
348806
func FetchSpecific(dir string, source Origin, set HashSet) error {
	for _, hash := range set {
		data, err := source.Get(hash.Name)
		if err != nil {
			return errors.New("failed to fetch file: " + hash.Name)
		}
		file := filepath.Join(dir, hash.Name)
		fileDir := filepath.Dir(file)
		err = os.MkdirAll(fileDir, DefaultDirMode)
		if err != nil {
			return errors.New("failed to create folder: " + fileDir)
		}
		err = ioutil.WriteFile(file, data, DefaultFileMode)
		if err != nil {
			return errors.New("failed to write file: " + file)
		}
	}
	return nil
}
783598
func fetchIndex(source DataSource, indexPath string, mirrorsPath string, cloudSpec CloudSpec,
	signed bool, params ValueParams) (indexRef *IndexReference, indexURL string, _ error) {
	indexURL, err := source.URL(indexPath)
	if err != nil {
		// Some providers return an error if asked for the URL of a non-existent file.
		// So the best we can do is use the relative path for the URL when logging messages.
		indexURL = indexPath
	}
	indexRef, err = GetIndexWithFormat(
		source, indexPath, IndexFormat, mirrorsPath, signed, cloudSpec, params,
	)
	return indexRef, indexURL, err
}
782748
func (f *Fetcher) FetchFromDataURL(u url.URL, dest *os.File, opts FetchOptions) error {
	if opts.Compression != "" {
		return ErrCompressionUnsupported
	}
	url, err := dataurl.DecodeString(u.String())
	if err != nil {
		return err
	}

	return f.decompressCopyHashAndVerify(dest, bytes.NewBuffer(url.Data), opts)
}
1232118
func (c *Cache) fetchDirect(t string, r Request, minIndex uint64) (interface{}, ResultMeta, error) {
	// Get the type that we're fetching
	c.typesLock.RLock()
	tEntry, ok := c.types[t]
	c.typesLock.RUnlock()
	if !ok {
		return nil, ResultMeta{}, fmt.Errorf("unknown type in cache: %s", t)
	}

	// Fetch it with the min index specified directly by the request.
	result, err := tEntry.Type.Fetch(FetchOptions{
		MinIndex: minIndex,
	}, r)
	if err != nil {
		return nil, ResultMeta{}, err
	}

	// Return the result and ignore the rest
	return result.Value, ResultMeta{}, nil
}
995367
func (t *OSesTable) fetch(c context.Context) error {
	db := database.Get(c)
	rows, err := db.QueryContext(c, `
		SELECT id, name, description
		FROM oses
	`)
	if err != nil {
		return errors.Annotate(err, "failed to select operating systems").Err()
	}
	defer rows.Close()
	for rows.Next() {
		os := &OS{}
		if err := rows.Scan(&os.Id, &os.Name, &os.Description); err != nil {
			return errors.Annotate(err, "failed to scan operating system").Err()
		}
		t.current = append(t.current, os)
	}
	return nil
}
1266435
func (m *MemAccResolver) Fetch(name string) (string, error) {
	if j, ok := m.sm.Load(name); ok {
		return j.(string), nil
	}
	return _EMPTY_, ErrMissingAccount
}
314722
func fetchConfigFile(c context.Context, path string) (string, *config.Meta, error) {
	configSet := cfgclient.CurrentServiceConfigSet(c)
	logging.Infof(c, "Reading %q from config set %q", path, configSet)
	c, cancelFunc := context.WithTimeout(c, 29*time.Second) // URL fetch deadline
	defer cancelFunc()

	var (
		content string
		meta    config.Meta
	)
	if err := cfgclient.Get(c, cfgclient.AsService, configSet, path, cfgclient.String(&content), &meta); err != nil {
		return "", nil, err
	}
	return content, &meta, nil
}
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
}
1329143
func ComputeSpecificity(selector string) int {
	result := 0

	if selector == inlineFakeSelector {
		result += 1000
	}

	result += 100 * strings.Count(selector, "#")
	result += 10 * len(nonIDAttrAndPseudoClassesRegexp.FindAllStringSubmatch(selector, -1))
	result += len(elementsAndPseudoElementsRegexp.FindAllStringSubmatch(selector, -1))

	return result
}
841360
func (o *Remote) Fetch(refspecs []string, opts *FetchOptions, msg string) error {
	var cmsg *C.char = nil
	if msg != "" {
		cmsg = C.CString(msg)
		defer C.free(unsafe.Pointer(cmsg))
	}

	crefspecs := C.git_strarray{}
	crefspecs.count = C.size_t(len(refspecs))
	crefspecs.strings = makeCStringsFromStrings(refspecs)
	defer freeStrarray(&crefspecs)

	coptions := (*C.git_fetch_options)(C.calloc(1, C.size_t(unsafe.Sizeof(C.git_fetch_options{}))))
	defer C.free(unsafe.Pointer(coptions))

	populateFetchOptions(coptions, opts)
	defer untrackCalbacksPayload(&coptions.callbacks)
	defer freeStrarray(&coptions.custom_headers)

	runtime.LockOSThread()
	defer runtime.UnlockOSThread()

	ret := C.git_remote_fetch(o.ptr, &crefspecs, coptions, cmsg)
	runtime.KeepAlive(o)
	if ret < 0 {
		return MakeGitError(ret)
	}
	return nil
}
600267
func (dc *DefaultConnector) Fetch(topic string, partition int32, offset int64) (*FetchResponse, error) {
	response, err := dc.tryFetch(topic, partition, offset)
	if err != nil {
		return response, err
	}

	if response.Error(topic, partition) == ErrNotLeaderForPartition {
		Infof(dc, "Sent a fetch reqest to a non-leader broker. Refleshing metadata for topic %s and retrying the request", topic)
		err = dc.metadata.Refresh([]string{topic})
		if err != nil {
			return nil, err
		}
		response, err = dc.tryFetch(topic, partition, offset)
	}

	return response, err
}
1261120
func (d *Downloader) fetchReceipts(from uint64) error {
	log.Debug("Downloading transaction receipts", "origin", from)

	var (
		deliver = func(packet dataPack) (int, error) {
			pack := packet.(*receiptPack)
			return d.queue.DeliverReceipts(pack.peerID, pack.receipts)
		}
		expire   = func() map[string]int { return d.queue.ExpireReceipts(d.requestTTL()) }
		fetch    = func(p *peerConnection, req *fetchRequest) error { return p.FetchReceipts(req) }
		capacity = func(p *peerConnection) int { return p.ReceiptCapacity(d.requestRTT()) }
		setIdle  = func(p *peerConnection, accepted int) { p.SetReceiptsIdle(accepted) }
	)
	err := d.fetchParts(errCancelReceiptFetch, d.receiptCh, deliver, d.receiptWakeCh, expire,
		d.queue.PendingReceipts, d.queue.InFlightReceipts, d.queue.ShouldThrottleReceipts, d.queue.ReserveReceipts,
		d.receiptFetchHook, fetch, d.queue.CancelReceipts, capacity, d.peers.ReceiptIdlePeers, setIdle, "receipts")

	log.Debug("Transaction receipt download terminated", "err", err)
	return err
}
905030
func (f *StatsFetcher) fetch(server *serverParts, replyCh chan *autopilot.ServerStats) {
	var args struct{}
	var reply autopilot.ServerStats
	err := f.pool.RPC(f.region, server.Addr, server.MajorVersion, "Status.RaftStats", &args, &reply)
	if err != nil {
		f.logger.Warn("failed retrieving server health", "server", server.Name, "error", err)
	} else {
		replyCh <- &reply
	}

	f.inflightLock.Lock()
	delete(f.inflight, server.ID)
	f.inflightLock.Unlock()
}
70817
func (this *SiestaClient) Fetch(topic string, partition int32, offset int64) ([]*Message, error) {
	Tracef(this, "Fetching %s %d from %d", topic, partition, offset)
	response, err := this.connector.Fetch(topic, partition, offset)
	if err != nil {
		return nil, err
	}

	messages := make([]*Message, 0)

	timestamp := time.Now().UnixNano() / int64(time.Millisecond)
	collector := func(topic string, partition int32, offset int64, key []byte, value []byte) error {
		decodedKey, err := this.config.KeyDecoder.Decode(key)
		if err != nil {
			//TODO: what if we fail to decode the key: fail-fast or fail-safe strategy?
			Error(this, err.Error())
			return err
		}
		decodedValue, err := this.config.ValueDecoder.Decode(value)
		if err != nil {
			//TODO: what if we fail to decode the value: fail-fast or fail-safe strategy?
			Error(this, err.Error())
			return err
		}

		if this.config.Debug {
			decodedKey = []int64{timestamp}
		}

		messages = append(messages, &Message{
			Key:                 key,
			Value:               value,
			DecodedKey:          decodedKey,
			DecodedValue:        decodedValue,
			Topic:               topic,
			Partition:           partition,
			Offset:              offset,
			HighwaterMarkOffset: response.Data[topic][partition].HighwaterMarkOffset,
		})
		return nil
	}

	return messages, response.CollectMessages(collector)
}
1171125
func fetch(c context.Context) (*Config, error) {
	cli := getInterface(c)
	set := cfgclient.CurrentServiceConfigSet(c)

	// If VMs and kinds are both non-empty, then their revisions must match
	// because VMs may depend on kinds. If VMs is empty but kinds is not,
	// then kinds are declared but unused (which is fine). If kinds is empty
	// but VMs is not, this may or may not be fine. Validation will tell us.
	rev := ""

	vms := &gce.Configs{}
	switch vmsCfg, err := cli.GetConfig(c, set, vmsFile, false); {
	case err == config.ErrNoConfig:
		logging.Debugf(c, "%q not found", vmsFile)
	case err != nil:
		return nil, errors.Annotate(err, "failed to fetch %q", vmsFile).Err()
	default:
		rev = vmsCfg.Revision
		logging.Debugf(c, "found %q revision %s", vmsFile, vmsCfg.Revision)
		if err := proto.UnmarshalText(vmsCfg.Content, vms); err != nil {
			return nil, errors.Annotate(err, "failed to load %q", vmsFile).Err()
		}
	}

	kinds := &gce.Kinds{}
	switch kindsCfg, err := cli.GetConfig(c, set, kindsFile, false); {
	case err == config.ErrNoConfig:
		logging.Debugf(c, "%q not found", kindsFile)
	case err != nil:
		return nil, errors.Annotate(err, "failed to fetch %q", kindsFile).Err()
	default:
		logging.Debugf(c, "found %q revision %s", kindsFile, kindsCfg.Revision)
		if rev != "" && kindsCfg.Revision != rev {
			return nil, errors.Reason("config revision mismatch").Err()
		}
		if err := proto.UnmarshalText(kindsCfg.Content, kinds); err != nil {
			return nil, errors.Annotate(err, "failed to load %q", kindsFile).Err()
		}
	}

	prjs := &projects.Configs{}
	switch prjsCfg, err := cli.GetConfig(c, set, projectsFile, false); {
	case err == config.ErrNoConfig:
		logging.Debugf(c, "%q not found", projectsFile)
	case err != nil:
		return nil, errors.Annotate(err, "failed to fetch %q", projectsFile).Err()
	default:
		logging.Debugf(c, "found %q revision %s", projectsFile, prjsCfg.Revision)
		if rev != "" && prjsCfg.Revision != rev {
			return nil, errors.Reason("config revision mismatch").Err()
		}
		if err := proto.UnmarshalText(prjsCfg.Content, prjs); err != nil {
			return nil, errors.Annotate(err, "failed to load %q", projectsFile).Err()
		}
	}
	return &Config{
		revision: rev,
		Kinds:    kinds,
		Projects: prjs,
		VMs:      vms,
	}, nil
}
739091
func (f *fetcher) Fetch() error {
	resp, err := f.client.Do(f.req)
	if err != nil {
		return errors.Trace(err)
	}

	defer func() {
		err = resp.Body.Close()
		if err != nil {
			logger.Debugf("failed defer %q", errors.Trace(err))
		}
	}()

	if resp.StatusCode != 200 {
		f.image.cleanup()
		return errors.NotFoundf(
			"got %d fetching image %q", resp.StatusCode, path.Base(
				f.req.URL.String()))
	}
	err = f.image.write(resp.Body, f.metadata)
	if err != nil {
		return errors.Trace(err)
	}
	return nil
}
726948
func (m *Migrator) fetchMigrations() error {
	pathGlob := append([]byte(m.MigrationsPath), []byte("*")...)

	matches, err := filepath.Glob(string(pathGlob))
	if err != nil {
		m.logger.Fatalf("Error while globbing migrations: %v", err)
	}

	for _, match := range matches {
		num, migrationType, name, err := parseMigrationPath(match)
		if err != nil {
			m.logger.Printf("Invalid migration file found: %s", match)
			continue
		}

		m.logger.Printf("Migration file found: %s", match)

		migration, ok := m.migrations[num]
		if !ok {
			migration = &Migration{Id: num, Name: name, Status: Inactive}
			m.migrations[num] = migration
		}
		if migrationType == upMigration {
			migration.UpPath = match
		} else {
			migration.DownPath = match
		}
	}

	// Validate each migration.
	for _, migration := range m.migrations {
		if !migration.valid() {
			path := migration.UpPath
			if path == "" {
				path = migration.DownPath
			}
			m.logger.Printf("Invalid migration pair for path: %s", path)
			return InvalidMigrationPair
		}
	}

	m.logger.Printf("Migrations file pairs found: %v", len(m.migrations))

	return nil
}