Exemple #1
0
// snmpTranslate resolves the given OID.
// The contents of the oid parameter will be replaced with the numeric oid value.
// If name is empty, the textual OID value is stored in it. If the textual OID cannot be translated, the numeric OID is stored instead.
// If mibPrefix is non-nil, the MIB in which the OID was found is stored, with a suffix of "::".
func snmpTranslate(mibPrefix *string, oid *string, name *string) error {
	if strings.ContainsAny(*oid, ":abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") {
		out, err := execCmd("snmptranslate", "-m", "all", "-On", *oid)
		if err != nil {
			return Errorf(err, "translating %s", *oid)
		}
		*oid = string(bytes.TrimSuffix(out, []byte{'\n'}))
	}

	if *name == "" {
		out, err := execCmd("snmptranslate", "-m", "all", *oid)
		if err != nil {
			//TODO debug message
			*name = *oid
		} else {
			if i := bytes.Index(out, []byte("::")); i != -1 {
				if mibPrefix != nil {
					*mibPrefix = string(out[:i+2])
				}
				out = out[i+2:]
			}
			*name = string(bytes.TrimSuffix(out, []byte{'\n'}))
		}
	}

	return nil
}
Exemple #2
0
// statsPerNUMA gathers hugepages statistics from each NUMA node
func statsPerNUMA(path string) (map[string]HugepagesNUMAStats, error) {
	var hugepagesStats = make(map[string]HugepagesNUMAStats)
	dirs, err := ioutil.ReadDir(path)
	if err != nil {
		return hugepagesStats, err
	}

	for _, d := range dirs {
		if !(d.IsDir() && strings.HasPrefix(d.Name(), "node")) {
			continue
		}

		hugepagesFree := fmt.Sprintf("%s/%s/hugepages/hugepages-2048kB/free_hugepages", path, d.Name())
		hugepagesNR := fmt.Sprintf("%s/%s/hugepages/hugepages-2048kB/nr_hugepages", path, d.Name())

		free, err := ioutil.ReadFile(hugepagesFree)
		if err != nil {
			return hugepagesStats, err
		}

		nr, err := ioutil.ReadFile(hugepagesNR)
		if err != nil {
			return hugepagesStats, err
		}

		f, _ := strconv.Atoi(string(bytes.TrimSuffix(free, newlineByte)))
		n, _ := strconv.Atoi(string(bytes.TrimSuffix(nr, newlineByte)))

		hugepagesStats[d.Name()] = HugepagesNUMAStats{Free: f, NR: n}

	}
	return hugepagesStats, nil
}
Exemple #3
0
func (r *Repository) execAndParseCols(subcmd string) ([][2]string, error) {
	cmd := exec.Command("hg", "-v", "--debug", subcmd)
	cmd.Dir = r.Dir
	out, err := cmd.CombinedOutput()
	if err != nil {
		return nil, fmt.Errorf("exec `hg -v --debug %s` failed: %s. Output was:\n\n%s", subcmd, err, out)
	}

	out = bytes.TrimSuffix(out, []byte("\n")) // remove trailing newline
	lines := bytes.Split(out, []byte("\n"))
	sort.Sort(byteSlices(lines)) // sort for consistency
	refs := make([][2]string, len(lines))
	for i, line := range lines {
		line = bytes.TrimSuffix(line, []byte(" (inactive)"))

		// format: "NAME      SEQUENCE:ID" (arbitrary amount of whitespace between NAME and SEQUENCE)
		if len(line) <= 41 {
			return nil, fmt.Errorf("unexpectedly short (<=41 bytes) line in `hg -v --debug %s` output", subcmd)
		}
		id := line[len(line)-40:]

		// find where the SEQUENCE begins
		seqIdx := bytes.LastIndex(line, []byte(" "))
		if seqIdx == -1 {
			return nil, fmt.Errorf("unexpectedly no whitespace in line in `hg -v --debug %s` output", subcmd)
		}
		name := bytes.TrimRight(line[:seqIdx], " ")
		refs[i] = [2]string{string(id), string(name)}
	}
	return refs, nil
}
Exemple #4
0
func (p *docPrinter) adjustedOutputPosition() int64 {
	b := p.buf.Bytes()
	b = bytes.TrimSuffix(b, []byte{'*'})
	b = bytes.TrimSuffix(b, []byte{'[', ']'})
	b = bytes.TrimSuffix(b, []byte{'*'})
	b = bytes.TrimSuffix(b, []byte{'&'})
	return p.outputPosition() - int64(p.buf.Len()-len(b))
}
Exemple #5
0
func ExampleTrimSuffix() {
	var b = []byte("Hello, goodbye, etc!")
	b = bytes.TrimSuffix(b, []byte("goodbye, etc!"))
	b = bytes.TrimSuffix(b, []byte("gopher"))
	b = append(b, bytes.TrimSuffix([]byte("world!"), []byte("x!"))...)
	os.Stdout.Write(b)
	// Output: Hello, world!
}
Exemple #6
0
func (l *lexer) toEOL() ([]byte, error) {
	line, err := l.buf.ReadBytes('\n')
	// ignore EOF here since it's roughly equivalent to EOL
	if err != nil && err != io.EOF {
		return nil, err
	}

	line = bytes.TrimSuffix(line, []byte{'\r'})
	line = bytes.TrimSuffix(line, []byte{'\n'})

	return line, nil
}
Exemple #7
0
// Parses output of `qmake -v` like
//
//     Using Qt version 5.2.1 in /usr/lib/x86_64-linux-gnu
//
// And `qtpaths --plugin-dir` like
//
//     /usr/local/Cellar/qt5/5.3.0/plugins
func getQtInfo() (info qtInfo, err error) {
	errFmt := fmt.Errorf("qt info: qmake unexpected output")

	buf, err := exec.Command("qmake", "-v").Output()
	if err != nil {
		err = fmt.Errorf("qt info: qmake: %v", err)
		return
	}
	idx := bytes.Index(buf, []byte("Qt version"))
	if idx < 0 {
		err = errFmt
		return
	}
	buf = buf[idx:]
	n, err := fmt.Sscanf(string(buf), "Qt version %s in %s", &info.Version, &info.LibPath)
	if err != nil || n != 2 {
		err = errFmt
	}

	// qtpaths
	buf, err = exec.Command("qtpaths", "--plugin-dir").Output()
	if err != nil {
		err = fmt.Errorf("qt info: qtpaths: %v", err)
		return
	}
	buf = bytes.TrimSpace(buf)
	if len(buf) < 1 || !bytes.HasSuffix(buf, []byte("plugins")) {
		err = fmt.Errorf("qt info: qtpaths unexpected output")
		return
	}
	buf = bytes.TrimSuffix(buf, []byte("plugins"))
	info.BasePath = string(buf[:len(buf)-1]) // drop sep
	return
}
Exemple #8
0
func (r *Reader) Read() ([]byte, error) {
	buf := []byte{}
	var isErr bool
	for {
		line, err := r.ReadBytes('\n')
		if err != nil {
			return nil, err
		}
		if bytes.HasPrefix(line, []byte("event: error")) {
			isErr = true
		}
		if bytes.HasPrefix(line, []byte("data: ")) {
			data := bytes.TrimSuffix(bytes.TrimPrefix(line, []byte("data: ")), []byte("\n"))
			buf = append(buf, data...)
		}
		// peek ahead one byte to see if we have a double newline (terminator)
		if peek, err := r.Peek(1); err == nil && string(peek) == "\n" {
			break
		}
	}
	if isErr {
		return nil, Error(string(buf))
	}
	return buf, nil
}
Exemple #9
0
func (dbp *Process) loadProcessInformation(wg *sync.WaitGroup) {
	defer wg.Done()

	comm, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/comm", dbp.Pid))
	if err == nil {
		// removes newline character
		comm = bytes.TrimSuffix(comm, []byte("\n"))
	}

	if comm == nil || len(comm) <= 0 {
		stat, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/stat", dbp.Pid))
		if err != nil {
			fmt.Printf("Could not read proc stat: %v\n", err)
			os.Exit(1)
		}
		expr := fmt.Sprintf("%d\\s*\\((.*)\\)", dbp.Pid)
		rexp, err := regexp.Compile(expr)
		if err != nil {
			fmt.Printf("Regexp compile error: %v\n", err)
			os.Exit(1)
		}
		match := rexp.FindSubmatch(stat)
		if match == nil {
			fmt.Printf("No match found using regexp '%s' in /proc/%d/stat\n", expr, dbp.Pid)
			os.Exit(1)
		}
		comm = match[1]
	}
	dbp.os.comm = strings.Replace(string(comm), "%", "%%", -1)
}
Exemple #10
0
func matchRoute(path []byte, itype int) *RouteMatched {
	var route *Route
	var items []*Route
	switch itype {
	case 3:
		items = wsRoutes
	default:
		items = routes
	}

	for _, route = range items {
		if route.Pattern == nil {
			// fix route
			if bytes.HasSuffix(path, B_SLASH) {
				path = bytes.TrimSuffix(path, B_SLASH)
			}
			if bytes.Equal(path, route.Rule) {
				return &RouteMatched{ClassType: route.ClassType, Params: nil}
			}
		} else {
			// regexp route
			if matched := route.Pattern.FindAllSubmatch(path, -1); matched != nil {
				params := make(map[string]string)
				i := 0
				for _, value := range route.Keys {
					i++
					params[value] = html.EscapeString(string(matched[0][i]))
				}
				return &RouteMatched{ClassType: route.ClassType, Params: params}
			}
		}
	}

	return nil
}
Exemple #11
0
// indentVal writes the given YAML-formatted value x to w and prefixing
// the second and subsequent lines with the given ident.
func indentVal(w io.Writer, x interface{}, indentStr string) {
	data, err := yaml.Marshal(x)
	if err != nil {
		panic(fmt.Errorf("cannot marshal YAML", err))
	}
	if len(data) == 0 {
		panic("YAML cannot marshal to empty string")
	}
	indent := []byte(indentStr + "  ")
	if canUseSameLine(x) {
		w.Write(space)
	} else {
		w.Write(nl)
		w.Write(indent)
	}
	data = bytes.TrimSuffix(data, nl)
	lines := bytes.Split(data, nl)
	for i, line := range lines {
		if i > 0 {
			w.Write(indent)
		}
		w.Write(line)
		w.Write(nl)
	}
}
Exemple #12
0
func (c *Copier) copySrc(name string, src io.Reader) {
	defer c.copyJobs.Done()
	reader := bufio.NewReader(src)

	for {
		line, err := reader.ReadBytes('\n')
		line = bytes.TrimSuffix(line, []byte{'\n'})

		// ReadBytes can return full or partial output even when it failed.
		// e.g. it can return a full entry and EOF.
		if err == nil || len(line) > 0 {
			if logErr := c.dst.Log(&Message{ContainerID: c.cid, Line: line, Source: name, Timestamp: time.Now().UTC()}); logErr != nil {
				logrus.Errorf("Failed to log msg %q for logger %s: %s", line, c.dst.Name(), logErr)
			}
		}

		if err != nil {
			if err != io.EOF {
				logrus.Errorf("Error scanning log stream: %s", err)
			}
			return
		}

	}
}
Exemple #13
0
func fmtToJsonArr(s []byte) []byte {
	s = bytes.Replace(s, []byte("{"), []byte("[{"), 1)
	s = bytes.Replace(s, []byte("}"), []byte("},"), -1)
	s = bytes.TrimSuffix(s, []byte(","))
	s = append(s, []byte("]")...)
	return s
}
Exemple #14
0
func (r *Repository) showRef(arg string) ([][2]string, error) {
	cmd := exec.Command("git", "show-ref", arg)
	cmd.Dir = r.Dir
	out, err := cmd.CombinedOutput()
	if err != nil {
		// Exit status of 1 and no output means there were no
		// results. This is not a fatal error.
		if exitStatus(err) == 1 && len(out) == 0 {
			return nil, nil
		}
		return nil, fmt.Errorf("exec `git show-ref %s` in %s failed: %s. Output was:\n\n%s", arg, r.Dir, err, out)
	}

	out = bytes.TrimSuffix(out, []byte("\n")) // remove trailing newline
	lines := bytes.Split(out, []byte("\n"))
	sort.Sort(byteSlices(lines)) // sort for consistency
	refs := make([][2]string, len(lines))
	for i, line := range lines {
		if len(line) <= 41 {
			return nil, errors.New("unexpectedly short (<=41 bytes) line in `git show-ref ...` output")
		}
		id := line[:40]
		name := line[41:]
		refs[i] = [2]string{string(id), string(name)}
	}
	return refs, nil
}
Exemple #15
0
func readAscii(buf []byte) (string, error) {
	br := bufio.NewReader(bytes.NewReader(buf))
	bytez, e := br.ReadBytes(byte(0x00))
	check(e)
	bytez = bytes.TrimSuffix(bytez, []byte{0x00})
	return string(bytez), nil
}
// markdownify renders a given string from Markdown to HTML.
func markdownify(in interface{}) template.HTML {
	text := cast.ToString(in)
	m := helpers.RenderBytes(&helpers.RenderingContext{Content: []byte(text), PageFmt: "markdown"})
	m = bytes.TrimPrefix(m, markdownTrimPrefix)
	m = bytes.TrimSuffix(m, markdownTrimSuffix)
	return template.HTML(m)
}
Exemple #17
0
func listxattrAll(path string, listFunc listxattrFunc) ([]string, error) {
	var p []byte // nil on first execution

	for {
		n, err := listFunc(path, p) // first call gets buffer size.
		if err != nil {
			return nil, err
		}

		if n > len(p) {
			p = make([]byte, n)
			continue
		}

		p = p[:n]

		ps := bytes.Split(bytes.TrimSuffix(p, []byte{0}), []byte{0})
		var entries []string
		for _, p := range ps {
			s := string(p)
			if s != "" {
				entries = append(entries, s)
			}
		}

		return entries, nil
	}
}
Exemple #18
0
// The refs are either tips (obj-id SP refname) or a peeled (obj-id SP refname^{}).
// If there are no refs, then there might be a shallow or flush-ptk.
func decodeOtherRefs(p *Decoder) decoderStateFn {
	if ok := p.nextLine(); !ok {
		return nil
	}

	if bytes.HasPrefix(p.line, shallow) {
		return decodeShallow
	}

	if len(p.line) == 0 {
		return nil
	}

	saveTo := p.data.References
	if bytes.HasSuffix(p.line, peeled) {
		p.line = bytes.TrimSuffix(p.line, peeled)
		saveTo = p.data.Peeled
	}

	ref, hash, err := readRef(p.line)
	if err != nil {
		p.error("%s", err)
		return nil
	}
	saveTo[ref] = hash

	return decodeOtherRefs
}
Exemple #19
0
// The request should be of format
// ADD|ElementToAdd\n
// TEST|AmIHere
// writes back 200|true if the element is probably in the bloom filter
// writes back 200|false is the element is definitely not in the bloom filter
func (c *ClientConn) writeResponse(request []byte) {
	message := bytes.Split(request, []byte{'|'})
	var response []byte
	if len(message) != 2 {
		response = []byte("400|Malformed request\n Usage: ADD|ElementToAdd or TEST|ElementToTest\n")
		c.conn.Write(response)
		return
	}

	verb := string(message[0])
	item := bytes.TrimSuffix(message[1], []byte("\n"))

	if verb == "TEST" {
		if c.f.Test(item) {
			response = []byte("200|true\n")
		} else {
			response = []byte("200|false\n")
		}
	} else if verb == "ADD" {
		c.f.Add(item)
		response = []byte("201\n")
	} else {
		response = []byte("400|Unknown Verb\n")
	}
	c.conn.Write(response)
	fmt.Println("Request:", string(request), "Response: ", string(response))
}
Exemple #20
0
func substPatternBytes(pat, repl, str []byte) (pre, subst, post []byte) {
	i := bytes.IndexByte(pat, '%')
	if i < 0 {
		if bytes.Equal(str, pat) {
			return repl, nil, nil
		}
		return str, nil, nil
	}
	in := str
	trimed := str
	if i > 0 {
		trimed = bytes.TrimPrefix(in, pat[:i])
		if bytes.Equal(trimed, in) {
			return str, nil, nil
		}
	}
	in = trimed
	if i < len(pat)-1 {
		trimed = bytes.TrimSuffix(in, pat[i+1:])
		if bytes.Equal(trimed, in) {
			return str, nil, nil
		}
	}

	i = bytes.IndexByte(repl, '%')
	if i < 0 {
		return repl, nil, nil
	}

	return repl[:i], trimed, repl[i+1:]
}
func (w *bufWriter) Write(in []byte) (n int, err error) {
	w.mu.Lock()
	defer w.mu.Unlock()

	n = len(in)
	in = bytes.TrimSuffix(in, w.suffix)

	if debug {
		inTxt := strings.Replace(string(in), "\n", "\\n", -1)
		findTxt := strings.Replace(string(w.findTxt), "\n", "\\n", -1)
		fmt.Printf("debug --> %s <-- debug (findTxt='%s')\n", inTxt, findTxt)
	}

	w.buf = append(w.buf, in...)

	if len(w.findTxt) > 0 {
		if i := bytes.Index(w.buf, w.findTxt); i >= 0 {
			w.findCh <- i
			close(w.findCh)
			w.findTxt = nil
			w.findCh = nil
			if w.findAfter != nil {
				w.findAfter.Stop()
				w.findAfter = nil
			}
		}
	}
	return n, nil
}
Exemple #22
0
// authReadLine reads a line and separates it into its fields.
func authReadLine(in *bufio.Reader) ([][]byte, error) {
	data, err := in.ReadBytes('\n')
	if err != nil {
		return nil, err
	}
	data = bytes.TrimSuffix(data, []byte("\r\n"))
	return bytes.Split(data, []byte{' '}), nil
}
Exemple #23
0
func NewReplayerTransport(reader io.Reader) (*replayerTransport, error) {
	t := &replayerTransport{mutex: sync.Mutex{}}
	t.mutex.Lock()
	defer t.mutex.Unlock()
	conversation, err := ioutil.ReadAll(reader)
	if err != nil {
		return nil, fmt.Errorf("vcr: failed to read vcr file: %s", err)
	}
	r := bufio.NewReader(bytes.NewReader(conversation))

	i := 0
	for true {
		i++
		//		fmt.Printf("Reading Request %d\n", i)
		req, err := http.ReadRequest(r)
		if err == io.EOF {
			return t, nil
		} else if err != nil {
			fmt.Printf("error on request read: %s", err)
			return t, nil
		} else {
			t.requests = append(t.requests, req)
		}

		res, err := http.ReadResponse(r, req) // nil?
		if err == io.EOF {
			return t, nil
		} else if err != nil {
			fmt.Printf("error on response read: %s", err)
			return t, nil
		} else {
			res.Request.URL.Scheme = "http"
			res.Request.URL.Host = req.Host
			t.responses = append(t.responses, res)
		}

		bodyBytes := []byte{}
		for true {
			line, err := r.ReadBytes('\n')
			separatorReached := strings.HasSuffix(string(line), recordSeparator)
			if separatorReached {
				line = bytes.TrimSuffix(line, []byte(recordSeparator))
			}
			bodyBytes = append(bodyBytes, line...)

			if err == io.EOF || separatorReached {
				bodyReader := bytes.NewReader(bodyBytes)
				res.Body = ioutil.NopCloser(bodyReader)
				break
			} else if err == nil {
			} else {
				return t, err
			}
		}
	}

	return t, nil
}
Exemple #24
0
func detectLicense(filepath string) (LicenseType, error) {
	fh, err := os.Open(filepath)
	if err != nil {
		return UNKNOWN, err
	}
	defer fh.Close()

	var buf bytes.Buffer
	scanner := bufio.NewScanner(fh)
	for scanner.Scan() {
		if bytes.HasPrefix(scanner.Bytes(), []byte("package ")) {
			break
		}
		line := bytes.TrimSuffix(bytes.TrimPrefix(bytes.TrimPrefix(scanner.Bytes(),
			[]byte("//")), []byte("/*")), []byte("*/"))
		if len(line) > 0 && (line[0] == '+' || bytes.HasPrefix(bytes.TrimSpace(line), []byte("Copyright"))) {
			continue
		}
		buf.Write(bytes.TrimSpace(line))
		buf.WriteByte('\n')
	}
	//fmt.Fprintf(os.Stderr, "DETECT %q\n", strings.TrimSpace(buf.String()))
	l := license.New("", strings.TrimSpace(buf.String()))
	l.File = filepath
	if err = l.GuessType(); err != nil {
		if err.Error() == license.ErrUnrecognizedLicense {
			return UNKNOWN, scanner.Err()
		}
		return UNKNOWN, err
	}

	err = scanner.Err()
	switch l.Type {
	case license.LicenseMIT:
		return MIT, err
	case license.LicenseNewBSD:
		return NewBSD, err
	case license.LicenseFreeBSD:
		return Freebsd, err
	case license.LicenseApache20:
		return Apache2, err
	case license.LicenseMPL20:
		return MPL2, err
	case license.LicenseGPL20:
		return GPL2, err
	case license.LicenseGPL30:
		return GPL3, err
	case license.LicenseLGPL21:
		return LGPL2, err
	case license.LicenseLGPL30:
		return LGPL2, err
	case license.LicenseCDDL10:
		return CDDL, err
	case license.LicenseEPL10:
		return EPL, err
	}
	return UNKNOWN, err
}
Exemple #25
0
func (self Stack) String() string {
	var buf bytes.Buffer
	for _, token := range self.tokens {
		buf.WriteString(tokenToString(token))
		buf.WriteRune(' ')
	}

	return string(bytes.TrimSuffix(buf.Bytes(), []byte(` `)))
}
Exemple #26
0
func run(command string, args ...string) ([]byte, error) {
	log.Println(strings.Join(append([]string{command}, args...), " "))
	cmd := exec.Command(command, args...)
	out, err := cmd.CombinedOutput()
	if err != nil {
		return nil, fmt.Errorf("error: %v\n%s\n", err, out)
	}
	return bytes.TrimSuffix(out, []byte{'\n'}), nil
}
Exemple #27
0
func RunTemplate(templateName string, obj interface{}) ([]byte, error) {
	buf := new(bytes.Buffer)

	tmpl := GetTemplate(templateName)
	if err := tmpl.Execute(buf, obj); err != nil {
		return nil, err
	}

	return bytes.TrimSuffix(buf.Bytes(), []byte("\n  ")), nil
}
Exemple #28
0
func updateTokenValue(t *token, value string) {
	oldvalue := t.value
	t.value = value
	newraw := make([]byte, len(t.rawvalue))
	copy(newraw, t.rawvalue)
	t.rawvalue = append(
		bytes.TrimSuffix(newraw, []byte(oldvalue)),
		[]byte(value)...,
	)
}
Exemple #29
0
func verifyAndUnfold(line []byte) ([]byte, error) {
	line = bytes.Replace(line, []byte{'\r', '\n', ' '}, []byte{}, -1)
	line = bytes.TrimSuffix(line, []byte{'\r', '\n'})
	for _, c := range []byte{'\r', '\n'} {
		if bytes.IndexByte(line, c) != -1 {
			return nil, crlfError
		}
	}
	return line, nil
}
Exemple #30
0
// RawGetMessageId return Message-ID or empty string if to found
func RawGetMessageId(raw *[]byte) []byte {
	bHeader := []byte("message-id")
	for _, line := range bytes.Split(RawGetHeaders(raw), []byte{13, 10}) {
		if bytes.HasPrefix(bytes.ToLower(line), bHeader) {
			// strip <>
			return bytes.TrimPrefix(bytes.TrimSuffix(line[12:], []byte{62}), []byte{60})
		}
	}
	return []byte{}
}