// AnnotateAddToZip is an extended Zip writer that accepts an annotation function
// to customize the FileHeader values written into the archive
339983
func AnnotateAddToZip(zipWriter *zip.Writer,
	source string,
	rootSource string,
	annotator FileHeaderAnnotator,
	logger *logrus.Logger) error {

	linuxZipName := func(platformValue string) string {
		return strings.Replace(platformValue, "\\", "/", -1)
	}

	fullPathSource, err := filepath.Abs(source)
	if nil != err {
		return errors.Wrapf(err, "Failed to get absolute filepath")
	}

	appendFile := func(info os.FileInfo) error {
		zipEntryName := source
		if rootSource != "" {
			zipEntryName = fmt.Sprintf("%s/%s", linuxZipName(rootSource), info.Name())
		}
		// Create a header for this zipFile, basically let's see
		// if we can get the executable bits to travel along..
		fileHeader, fileHeaderErr := zip.FileInfoHeader(info)
		if fileHeaderErr != nil {
			return fileHeaderErr
		}
		// Update the name to the proper thing...
		fileHeader.Name = zipEntryName
		if annotator != nil {
			annotatedHeader, annotatedHeaderErr := annotator(fileHeader)
			if annotatedHeaderErr != nil {
				return errors.Wrapf(annotatedHeaderErr, "Failed to annotate Zip entry file header")
			}
			fileHeader = annotatedHeader
		}

		// File info for the binary executable
		binaryWriter, binaryWriterErr := zipWriter.CreateHeader(fileHeader)
		if binaryWriterErr != nil {
			return binaryWriterErr
		}
		/* #nosec */
		reader, readerErr := os.Open(fullPathSource)
		if readerErr != nil {
			return readerErr
		}
		written, copyErr := io.Copy(binaryWriter, reader)
		errClose := reader.Close()
		if errClose != nil {
			logger.WithFields(logrus.Fields{
				"Error": errClose,
			}).Warn("Failed to close Zip input stream")
		}
		logger.WithFields(logrus.Fields{
			"WrittenBytes": written,
			"SourcePath":   fullPathSource,
			"ZipName":      zipEntryName,
		}).Debug("Archiving file")
		return copyErr
	}

	directoryWalker := func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		header, err := zip.FileInfoHeader(info)
		if err != nil {
			return errors.Wrapf(err, "Failed to create FileInfoHeader")
		}
		// Normalize the Name
		platformName := strings.TrimPrefix(strings.TrimPrefix(path, rootSource), string(os.PathSeparator))
		header.Name = linuxZipName(platformName)

		if info.IsDir() {
			header.Name += "/"
		} else {
			header.Method = zip.Deflate
		}
		writer, err := zipWriter.CreateHeader(header)
		if err != nil {
			return err
		}
		if info.IsDir() {
			return nil
		}
		/* #nosec */
		file, err := os.Open(path)
		if err != nil {
			return errors.Wrapf(err, "Failed to open file: %s", path)
		}
		defer file.Close()
		_, err = io.Copy(writer, file)
		if err != nil {
			return errors.Wrapf(err, "Failed to copy file contents")
		}
		return nil
	}

	fileInfo, err := os.Stat(fullPathSource)
	if nil != err {
		return errors.Wrapf(err, "Failed to get file information")
	}
	switch mode := fileInfo.Mode(); {
	case mode.IsDir():
		err = filepath.Walk(fullPathSource, directoryWalker)
	case mode.IsRegular():
		err = appendFile(fileInfo)
	default:
		err = errors.New("Inavlid source type")
	}
	if err != nil {
		return errors.Wrapf(err, "Failed to determine file mode")
	}
	return nil
}
418071
func setIdleAnnotations(annotations map[string]string, scaleRefs []unidlingapi.RecordedScaleReference, nowTime time.Time) error {
	var scaleRefsBytes []byte
	var err error
	if scaleRefsBytes, err = json.Marshal(scaleRefs); err != nil {
		return err
	}

	annotations[unidlingapi.UnidleTargetAnnotation] = string(scaleRefsBytes)
	annotations[unidlingapi.IdledAtAnnotation] = nowTime.Format(time.RFC3339)

	return nil
}
1325805
func (s *TrafficPolicyInstance) SetHostedZoneId(v string) *TrafficPolicyInstance {
	s.HostedZoneId = &v
	return s
}
228601
func (z *Zipkin) HeadersToLog() []string {
	if !z.zipkinEnabled {
		return z.headersToLog
	}
	headersToLog := z.headersToLog
	if !contains(headersToLog, B3TraceIdHeader) {
		headersToLog = append(headersToLog, B3TraceIdHeader)
	}

	if !contains(headersToLog, B3SpanIdHeader) {
		headersToLog = append(headersToLog, B3SpanIdHeader)
	}

	if !contains(headersToLog, B3ParentSpanIdHeader) {
		headersToLog = append(headersToLog, B3ParentSpanIdHeader)
	}

	if !contains(headersToLog, B3Header) {
		headersToLog = append(headersToLog, B3Header)
	}

	return headersToLog
}
1068094
func (s *Segment) addAnnotation(key string, value interface{}) {
	s.Lock()
	defer s.Unlock()

	if s.Annotations == nil {
		s.Annotations = make(map[string]interface{})
	}
	s.Annotations[key] = value
}
244294
func (p *Packet) UDPDataDetach() {
	// Create constants for IP header + UDP header. copy ?
	p.ipHdr.Buffer = p.ipHdr.Buffer[:p.ipHdr.ipHeaderLen+UDPDataPos]
}
1124375
func (s *CreateQueryLoggingConfigInput) SetHostedZoneId(v string) *CreateQueryLoggingConfigInput {
	s.HostedZoneId = &v
	return s
}
892906
func (r *Reader) AnonymousIP(ipAddress net.IP) (*AnonymousIP, error) {
	if isAnonymousIP&r.databaseType == 0 {
		return nil, InvalidMethodError{"AnonymousIP", r.Metadata().DatabaseType}
	}
	var anonIP AnonymousIP
	err := r.mmdbReader.Lookup(ipAddress, &anonIP)
	return &anonIP, err
}
1237926
func (c *Client) LinodeIPAddPrivate(linodeID int) (ipID int, ipAddr string, err error) {
	args := make(map[string]interface{})
	args["LinodeID"] = linodeID

	data, err := c.apiCall("linode.ip.addprivate", args)
	if err != nil {
		return 0, "", err
	}

	out := struct {
		IPAddrID int    `json:"IPAddressID"`
		IPAddr   string `json:"IPAddress"`
	}{}

	err = json.Unmarshal(data, &out)
	if err != nil {
		return 0, "", err
	}

	return out.IPAddrID, out.IPAddr, nil
}
742871
func (s *ListTrafficPolicyInstancesOutput) SetHostedZoneIdMarker(v string) *ListTrafficPolicyInstancesOutput {
	s.HostedZoneIdMarker = &v
	return s
}
1260120
func (p *PUPolicy) Annotations() *TagStore {
	p.Lock()
	defer p.Unlock()

	return p.annotations.Copy()
}
1270560
func (s *ListTrafficPolicyInstancesInput) SetHostedZoneIdMarker(v string) *ListTrafficPolicyInstancesInput {
	s.HostedZoneIdMarker = &v
	return s
}
1365092
func (ef *File) ParseFileHeader() {
	ef.Lock()
	defer ef.Unlock()
	GoToSeeker(ef.file, 0)
	err := encoding.Unmarshal(ef.file, &ef.Header, Endianness)
	if err != nil {
		panic(err)
	}
}
1266262
func (s *Service) Annotations(w http.ResponseWriter, r *http.Request) {
	id, err := paramID("id", r)
	if err != nil {
		Error(w, http.StatusUnprocessableEntity, err.Error(), s.Logger)
		return
	}

	start, stop, err := validAnnotationQuery(r.URL.Query())
	if err != nil {
		Error(w, http.StatusUnprocessableEntity, err.Error(), s.Logger)
		return
	}

	ctx := r.Context()
	src, err := s.Store.Sources(ctx).Get(ctx, id)
	if err != nil {
		notFound(w, id, s.Logger)
		return
	}

	ts, err := s.TimeSeries(src)
	if err != nil {
		msg := fmt.Sprintf("unable to connect to source %d: %v", id, err)
		Error(w, http.StatusBadRequest, msg, s.Logger)
		return
	}

	if err = ts.Connect(ctx, &src); err != nil {
		msg := fmt.Sprintf("unable to connect to source %d: %v", id, err)
		Error(w, http.StatusBadRequest, msg, s.Logger)
		return
	}

	store := influx.NewAnnotationStore(ts)
	annotations, err := store.All(ctx, start, stop)
	if err != nil {
		msg := fmt.Errorf("error loading annotations: %v", err)
		unknownErrorWithMessage(w, msg, s.Logger)
		return
	}

	res := newAnnotationsResponse(src, annotations)
	encodeJSON(w, http.StatusOK, res, s.Logger)
}
606152
func (self *RawDispatcher) AddResourceSeparateUdid(node *RestNode, name string, wireExample interface{}, index RestIndex,
	find RestFindUdid, post RestPost, put RestPutUdid, del RestDeleteUdid) {
	t := self.validateType(wireExample)
	obj := &restObjUdid{
		restShared: restShared{
			typ:   t,
			name:  name,
			index: index,
			post:  post,
		},
		find: find,
		del:  del,
		put:  put,
	}
	node.ResUdid[strings.ToLower(name)] = obj
}
447715
func Zipit(source, target, prefix string) error {
	// Thanks to Svett Ralchev
	// http://blog.ralch.com/tutorial/golang-working-with-zip/

	zipfile, err := os.Create(target)
	if err != nil {
		return err
	}
	defer zipfile.Close()

	if prefix != "" {
		_, err = io.WriteString(zipfile, prefix)
		if err != nil {
			return err
		}
	}

	archive := zip.NewWriter(zipfile)
	defer archive.Close()

	err = filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}

		if path == source {
			return nil
		}

		header, err := zip.FileInfoHeader(info)
		if err != nil {
			return err
		}
		header.Name, err = filepath.Rel(source, path)
		if err != nil {
			return err
		}

		header.Name = filepath.ToSlash(header.Name)

		if info.IsDir() {
			header.Name += "/"
			header.SetMode(0755)
		} else {
			header.Method = zip.Deflate
			header.SetMode(0744)
		}

		writer, err := archive.CreateHeader(header)
		if err != nil {
			return err
		}

		if info.IsDir() {
			return nil
		}

		file, err := os.Open(path)
		if err != nil {
			return err
		}
		defer file.Close()

		_, err = io.Copy(writer, file)
		return err
	})

	return err
}
932370
func (s *QueryLoggingConfig) SetHostedZoneId(v string) *QueryLoggingConfig {
	s.HostedZoneId = &v
	return s
}