<commit_before>package proc

import (
	"fmt"
	"net"
	"os/exec"
	"strconv"
	"strings"
)

type reader struct{}

// NewReader returns a Darwin (lsof-based) '/proc' reader
func NewReader(proc Dir) Reader {
	return &reader{}
}

const (
	lsofBinary    = "lsof"
	lsofFields    = "cn" // parseLSOF() depends on the order
	netstatBinary = "netstat"
	lsofBinary    = "lsof"
)

func (reader) Processes(f func(Process)) error {
	output, err := exec.Command(
		lsofBinary,
		"-i",       // only Internet files
		"-n", "-P", // no number resolving
		"-w",             // no warnings
		"-F", lsofFields, // \n based output of only the fields we want.
	).CombinedOutput()
	if err != nil {
		return err
	}

	processes, err := parseLSOF(string(output))
	if err != nil {
		return err
	}

	for _, process := range processes {
		f(process)
	}
	return nil
}

func (r *reader) Connections(withProcs bool, f func(Connection)) error {
	out, err := exec.Command(
		netstatBinary,
		"-n", // no number resolving
		"-W", // Wide output
		// "-l", // full IPv6 addresses // What does this do?
		"-p", "tcp", // only TCP
	).CombinedOutput()
	if err != nil {
		return err
	}
	connections := parseDarwinNetstat(string(out))

	if withProcs {
		out, err := exec.Command(
			lsofBinary,
			"-i",       // only Internet files
			"-n", "-P", // no number resolving
			"-w",             // no warnings
			"-F", lsofFields, // \n based output of only the fields we want.
		).CombinedOutput()
		if err != nil {
			return err
		}

		procs, err := parseLSOF(string(out))
		if err != nil {
			return err
		}
		for local, proc := range procs {
			for i, c := range connections {
				localAddr := net.JoinHostPort(
					c.LocalAddress.String(),
					strconv.Itoa(int(c.LocalPort)),
				)
				if localAddr == local {
					connections[i].Proc = proc
				}
			}
		}
	}

	for _, c := range connections {
		f(c)
	}
	return nil
}

// Close closes the Darwin "/proc" reader
func (reader) Close() error {
	return nil<commit_msg>Fix bad merge on Darwin<commit_after>package proc

import (
	"fmt"
	"net"
	"os/exec"
	"strconv"
	"strings"
)

type reader struct{}

// NewReader returns a Darwin (lsof-based) '/proc' reader
func NewReader(proc Dir) Reader {
	return &reader{}
}

const (
	lsofBinary    = "lsof"
	lsofFields    = "cn" // parseLSOF() depends on the order
	netstatBinary = "netstat"
)

func (reader) Processes(f func(Process)) error {
	output, err := exec.Command(
		lsofBinary,
		"-i",       // only Internet files
		"-n", "-P", // no number resolving
		"-w",             // no warnings
		"-F", lsofFields, // \n based output of only the fields we want.
	).CombinedOutput()
	if err != nil {
		return err
	}

	processes, err := parseLSOF(string(output))
	if err != nil {
		return err
	}

	for _, process := range processes {
		f(process)
	}
	return nil
}

func (r *reader) Connections(withProcs bool, f func(Connection)) error {
	out, err := exec.Command(
		netstatBinary,
		"-n", // no number resolving
		"-W", // Wide output
		// "-l", // full IPv6 addresses // What does this do?
		"-p", "tcp", // only TCP
	).CombinedOutput()
	if err != nil {
		return err
	}
	connections := parseDarwinNetstat(string(out))

	if withProcs {
		out, err := exec.Command(
			lsofBinary,
			"-i",       // only Internet files
			"-n", "-P", // no number resolving
			"-w",             // no warnings
			"-F", lsofFields, // \n based output of only the fields we want.
		).CombinedOutput()
		if err != nil {
			return err
		}

		procs, err := parseLSOF(string(out))
		if err != nil {
			return err
		}
		for local, proc := range procs {
			for i, c := range connections {
				localAddr := net.JoinHostPort(
					c.LocalAddress.String(),
					strconv.Itoa(int(c.LocalPort)),
				)
				if localAddr == local {
					connections[i].Process = proc
				}
			}
		}
	}

	for _, c := range connections {
		f(c)
	}
	return nil
}

// Close closes the Darwin "/proc" reader
func (reader) Close() error {
	return nil
{'fraction_non_alphanumeric': 0.1485148514851485, 'fraction_numerical': 0.0005077430820005078, 'mean_word_length': 2.608058608058608, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5771959', 'n_tokens_mistral': 1566, 'n_tokens_neox': 1375, 'n_words': 423}