// Link specific
160999
func handleLink(update netlink.LinkUpdate, callback func(NetlinkUpdate)) {
	if update.IfInfomsg.Flags&syscall.IFF_UP == 1 {
		fmt.Printf("[Link device up]\tupdateLink is:%+v, flag is:0x%x\n", update.Link.Attrs(), update.IfInfomsg.Flags)
	} else {
		if update.Link.Attrs().ParentIndex == 0 {
			fmt.Printf("[Link device !up][Deleted]\tupdateLink is:%+v, flag is:0x%x\n", update.Link.Attrs(), update.IfInfomsg.Flags)
		} else {
			fmt.Printf("[Link device !up]\tupdateLink is:%+v, flag is:0x%x\n", update.Link.Attrs(), update.IfInfomsg.Flags)
		}
	}

	netlinkUpdate := NetlinkUpdate{}
	netlinkUpdate.UpdateType = UpdateTypeLink

	// We would like to only handle the veth pair link at present.
	if veth, ok := (update.Link).(*netlink.Veth); ok {
		netlinkUpdate.Veth = veth
		callback(netlinkUpdate)
	}
}
845605
func (w *Writer) Link(oldname, newname string) error {
	if err := w.finishInode(); err != nil {
		return err
	}
	newdir, existing, newchildname, err := w.lookup(newname, false)
	if err != nil {
		return err
	}
	if existing != nil && (existing.IsDir() || existing.LinkCount < 2) {
		return fmt.Errorf("%s: cannot orphan existing file or directory", newname)
	}

	_, oldfile, _, err := w.lookup(oldname, true)
	if err != nil {
		return err
	}
	switch oldfile.Mode & format.TypeMask {
	case format.S_IFDIR, format.S_IFLNK:
		return fmt.Errorf("%s: link target cannot be a directory or symlink: %s", newname, oldname)
	}

	if existing != oldfile && oldfile.LinkCount >= format.MaxLinks {
		return fmt.Errorf("%s: link target would exceed maximum link count: %s", newname, oldname)
	}

	if existing != nil {
		existing.LinkCount--
	}
	oldfile.LinkCount++
	newdir.Children[newchildname] = oldfile
	return nil
}
1090618
func (e *Error) SetLink(errorLink string) {
	errorLink = strings.Replace(errorLink, "{{Path}}", e.Path, -1)
	errorLink = strings.Replace(errorLink, "{{Line}}", strconv.Itoa(e.Line), -1)

	e.Link = "<a href=" + errorLink + ">" + e.Path + ":" + strconv.Itoa(e.Line) + "</a>"
}
464297
func (l Links) Href(rel string) (string, error) {
	return l.HrefParams(rel, nil)
}
144030
func (r renderer) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) {
	out.Write(content)
}
1210100
func (l *linkIndex) link(parent, child *container.Container, alias string) {
	l.mu.Lock()

	if l.idx[parent] == nil {
		l.idx[parent] = make(map[string]*container.Container)
	}
	l.idx[parent][alias] = child
	if l.childIdx[child] == nil {
		l.childIdx[child] = make(map[*container.Container]map[string]struct{})
	}
	if l.childIdx[child][parent] == nil {
		l.childIdx[child][parent] = make(map[string]struct{})
	}
	l.childIdx[child][parent][alias] = struct{}{}

	l.mu.Unlock()
}
939804
func (i *HTTPInstaller) link(from string) error {
	debug("symlinking %s to %s", from, i.Path())
	return os.Symlink(from, i.Path())
}
1335428
func (f *FieldSpec) Link(scope Scope) (err error) {
	if f.Type, err = f.Type.Link(scope); err != nil {
		return err
	}
	if f.Default != nil {
		f.Default, err = f.Default.Link(scope, f.Type)
	}
	return err
}
410491
func (m *linkMechanismManager) UpdateLink(context *LinkManagerContext) (err error) {
	link := new(LinkManagerContext)

	update := func(fn func() error) error {
		return m.BaseMechanismManager.Update(
			LinkModel, link, fn,
		)
	}

	pre := func(port LinkPort) error {
		linkContext, err := m.linkContext(port)
		if err != nil {
			return err
		}

		err = m.do(func(llmech LinkMechanism) error {
			return llmech.UpdateLinkPreCommit(linkContext)
		})

		if err != nil {
			log.ErrorLog("link/UPDATE_LINK_PRECOMMIT",
				"Failed to update link configuration: ", err)
		}

		return err
	}

	post := func(port LinkPort) error {
		linkContext, err := m.linkContext(port)
		if err != nil {
			return err
		}

		err = m.do(func(llmech LinkMechanism) error {
			return llmech.UpdateLinkPostCommit(linkContext)
		})

		if err != nil {
			log.ErrorLog("link/UPDATE_LINK_POSTCOMMIT",
				"Failed to update link configuration: ", err)
		}

		return err
	}

	m.lock.RLock()
	defer m.lock.RUnlock()

	return update(func() error {
		_, err := m.Driver()
		if err != nil && len(link.Ports) != 0 {
			log.ErrorLog("link/UPDATE_LINK",
				"Driver not found, but ports need to be updated")
			return ErrLinkNotInitialized
		}

		for _, port := range context.Ports {
			port = link.Port(port.Port)
			blank := LinkPort{}

			// Skip blank ports
			if port == blank {
				continue
			}

			// Update pre-commit
			if err := pre(port); err != nil {
				log.ErrorLog("link/UPDATE_LINK",
					"Link layer delete pre-commit failed: ", err)
				return err
			}
		}

		// Set new link layer driver
		err = m.SetDriver(context.Driver)
		if err != nil {
			return err
		}

		// Update driver configuration
		link.Driver = context.Driver

		for _, port := range context.Ports {
			// Update  post-commit
			if err := post(port); err != nil {
				log.ErrorLog("link/UPDATE_LINK",
					"Link layer update pre-commit failed: ", err)
				return err
			}

			// Update link layer port configuration
			link.SetPort(port)
		}

		return nil
	})
}
1235311
func (c ConstantSet) Link(scope Scope, t TypeSpec) (ConstantValue, error) {
	s, ok := RootTypeSpec(t).(*SetSpec)
	if !ok {
		return nil, constantValueCastError{Value: c, Type: t}
	}

	// TODO(abg): Track whether things are linked so that we don't re-link here
	// TODO(abg): Fail for duplicates
	values := make([]ConstantValue, len(c))
	for i, v := range c {
		value, err := v.Link(scope, s.ValueSpec)
		if err != nil {
			return nil, err
		}
		values[i] = value
	}

	return ConstantSet(values), nil
}
919322
func (r constantReference) Link(scope Scope, t TypeSpec) (ConstantValue, error) {
	src := ast.ConstantReference(r)

	c, err := scope.LookupConstant(src.Name)
	if err == nil {
		if err := c.Link(scope); err != nil {
			return nil, err
		}
		return ConstReference{Target: c}.Link(scope, t)
	}

	mname, iname := splitInclude(src.Name)
	if len(mname) == 0 {
		return nil, referenceError{
			Target:    src.Name,
			Line:      src.Line,
			ScopeName: scope.GetName(),
			Reason:    err,
		}
	}

	if enum, ok := lookupEnum(scope, mname); ok {
		if item, ok := enum.LookupItem(iname); ok {
			return EnumItemReference{
				Enum: enum,
				Item: item,
			}, nil
		}

		return nil, referenceError{
			Target:    src.Name,
			Line:      src.Line,
			ScopeName: scope.GetName(),
			Reason: unrecognizedEnumItemError{
				EnumName: mname,
				ItemName: iname,
			},
		}
	}

	includedScope, err := getIncludedScope(scope, mname)
	if err != nil {
		return nil, referenceError{
			Target:    src.Name,
			Line:      src.Line,
			ScopeName: scope.GetName(),
			Reason:    err,
		}
	}

	value, err := constantReference{Name: iname}.Link(includedScope, t)
	if err != nil {
		return nil, referenceError{
			Target:    src.Name,
			Line:      src.Line,
			ScopeName: scope.GetName(),
			Reason:    err,
		}
	}

	return value, nil
}
536409
func (b *Build) BuilderLink() *Link {
	if b.Builder == nil {
		panic("Invalid build")
	}
	builder := b.Builder
	return NewLink(
		builder.Builder,
		fmt.Sprintf("/p/%s/builders/%s/%s", builder.Project, builder.Bucket, builder.Builder),
		fmt.Sprintf("Builder %s in bucket %s", builder.Builder, builder.Bucket))
}
577498
func (s *Switch) AddLink(link ChannelLink) error {
	s.indexMtx.Lock()
	defer s.indexMtx.Unlock()

	chanID := link.ChanID()

	// First, ensure that this link is not already active in the switch.
	_, err := s.getLink(chanID)
	if err == nil {
		return fmt.Errorf("unable to add ChannelLink(%v), already "+
			"active", chanID)
	}

	// Get and attach the mailbox for this link, which buffers packets in
	// case there packets that we tried to deliver while this link was
	// offline.
	mailbox := s.mailOrchestrator.GetOrCreateMailBox(chanID)
	link.AttachMailBox(mailbox)

	if err := link.Start(); err != nil {
		s.removeLink(chanID)
		return err
	}

	shortChanID := link.ShortChanID()
	if shortChanID == sourceHop {
		log.Infof("Adding pending link chan_id=%v, short_chan_id=%v",
			chanID, shortChanID)

		s.pendingLinkIndex[chanID] = link
	} else {
		log.Infof("Adding live link chan_id=%v, short_chan_id=%v",
			chanID, shortChanID)

		s.addLiveLink(link)
		s.mailOrchestrator.BindLiveShortChanID(
			mailbox, chanID, shortChanID,
		)
	}

	return nil
}
399660
func (f *Food) Link(pkg *Package) error {
	barrelDir := filepath.Join(home.Home(home.HomePath).Barrel(), f.Name, f.Version)
	for _, r := range pkg.Resources {
		// TODO: run this in parallel
		destPath := filepath.Join(home.HomePrefix, r.InstallPath)
		if err := os.MkdirAll(filepath.Dir(destPath), 0755); err != nil && !os.IsExist(err) {
			return err
		}
		if r.Executable {
			if err := os.Chmod(filepath.Join(barrelDir, r.Path), 0755); err != nil {
				return err
			}
		}
		if err := os.Symlink(filepath.Join(barrelDir, r.Path), destPath); err != nil {
			return err
		}
	}
	return nil
}
696205
func (l Links) Next(href string, args ...interface{}) Links {
	return l.Link("next", href, args...)
}
267055
func (c Context) AddLink(link string) string {
	if c.responseHeader == nil {
		return ""
	}
	c.responseHeader.Add("Link", link)
	return ""
}
456192
func (r typeSpecReference) Link(scope Scope) (TypeSpec, error) {
	src := ast.TypeReference(r)
	t, err := scope.LookupType(src.Name)
	if err == nil {
		return t.Link(scope)
	}

	mname, iname := splitInclude(src.Name)
	if len(mname) == 0 {
		return nil, referenceError{
			Target:    src.Name,
			Line:      src.Line,
			ScopeName: scope.GetName(),
			Reason:    err,
		}
	}

	includedScope, err := getIncludedScope(scope, mname)
	if err != nil {
		return nil, referenceError{
			Target:    src.Name,
			Line:      src.Line,
			ScopeName: scope.GetName(),
			Reason:    err,
		}
	}

	t, err = typeSpecReference{Name: iname}.Link(includedScope)
	if err != nil {
		return nil, referenceError{
			Target:    src.Name,
			Line:      src.Line,
			ScopeName: scope.GetName(),
			Reason:    err,
		}
	}

	return t, nil
}