<commit_before>package migmem

import (
	"bufio"
	"io/ioutil"
	"log"
	"os"
	"path/filepath"
	"regexp"
	"strconv"
	"strings"
)

// HasLibrary checks if a process with a given pid has a certain library.
// To do this we use the /proc/<pid>/maps to know which files are mapped.
// the file format is described in `man proc`
func HasLibrary(pid uint, r *regexp.Regexp) (bool, error) {
	path := filepath.Join("/proc", strconv.Itoa(pid), "maps")
	f, err := os.Open(path)
	if err != nil {
		return false, err
	}
	defer f.Close()

	scanner := bufio.NewScanner(f)
	for scanner.Scan() {
		line := scanner.Text()

		// Just keep the last part of the mapped filename
		// TODO(mvanotti): Probably now that we are using regexp,
		// we may want to do the regexp over the whole filename.
		fields := strings.Split(line, "/")
		if len(fields) <= 1 {
			continue
		}
		library := fields[len(fields)-1]

		if r.MatchString(library) {
			return true, nil
		}
	}

	if err := scanner.Err(); err != nil {
		return false, err
	}

	return false, nil
}

// FindProcWithLib returns a list of process ids that have the given library loaded in memory.
// It works looking at all the pids listed in /proc folder, and for each of them, checking its maps file.
func FindProcWithLib(r *regexp.Regexp) ([]uint, error) {
	files, _ := ioutil.ReadDir("/proc/")
	res := make([]int, 0)<commit_msg>Fix build errors in linux<commit_after>package migmem

import (
	"bufio"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"path/filepath"
	"regexp"
	"strconv"
	"strings"
)

// HasLibrary checks if a process with a given pid has a certain library.
// To do this we use the /proc/<pid>/maps to know which files are mapped.
// the file format is described in `man proc`
func HasLibrary(pid uint, r *regexp.Regexp) (bool, error) {
	path := filepath.Join("/proc", fmt.Sprintf("%d", pid), "maps")
	f, err := os.Open(path)
	if err != nil {
		return false, err
	}
	defer f.Close()

	scanner := bufio.NewScanner(f)
	for scanner.Scan() {
		line := scanner.Text()

		// Just keep the last part of the mapped filename
		// TODO(mvanotti): Probably now that we are using regexp,
		// we may want to do the regexp over the whole filename.
		fields := strings.Split(line, "/")
		if len(fields) <= 1 {
			continue
		}
		library := fields[len(fields)-1]

		if r.MatchString(library) {
			return true, nil
		}
	}

	if err := scanner.Err(); err != nil {
		return false, err
	}

	return false, nil
}

// FindProcWithLib returns a list of process ids that have the given library loaded in memory.
// It works looking at all the pids listed in /proc folder, and for each of them, checking its maps file.
func FindProcWithLib(r *regexp.Regexp) ([]uint, error) {
	files, _ := ioutil.ReadDir("/proc/")
	res := make([]uint, 0)
{'fraction_non_alphanumeric': 0.13260473588342442, 'fraction_numerical': 0.002185792349726776, 'mean_word_length': 3.372611464968153, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '2836456', 'n_tokens_mistral': 992, 'n_tokens_neox': 937, 'n_words': 345}