<commit_before>// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package os

import (
	"io"
	"syscall"
	"unsafe"
)

//extern opendir
func libc_opendir(*byte) *syscall.DIR

//extern closedir
func libc_closedir(*syscall.DIR) int

// FIXME: pathconf returns long, not int.
//extern pathconf
func libc_pathconf(*byte, int) int

func clen(n []byte) int {
	for i := 0; i < len(n); i++ {
		if n[i] == 0 {
			return i
		}
	}
	return len(n)
}

var elen int

func (file *File) readdirnames(n int) (names []string, err error) {
	if elen == 0 {
		var dummy syscall.Dirent
		elen = (int(unsafe.Offsetof(dummy.Name)) +
			libc_pathconf(syscall.StringBytePtr(file.name), syscall.PC_NAME_MAX) +
			1)
	}

	if file.dirinfo == nil {
		file.dirinfo = new(dirInfo)
		file.dirinfo.buf = make([]byte, elen)
		file.dirinfo.dir = libc_opendir(syscall.StringBytePtr(file.name))
	}

	entry_dirent := unsafe.Pointer(&file.dirinfo.buf[0]).(*syscall.Dirent)

	size := n
	if size < 0 {
		size = 100
		n = -1
	}

	names = make([]string, 0, size) // Empty with room to grow.

	dir := file.dirinfo.dir
	if dir == nil {
		return names, NewSyscallError("opendir", syscall.GetErrno())
	}

	for n != 0 {
		var result *syscall.Dirent
		i := libc_readdir_r(dir, entry_dirent, &result)
		if i != 0 {
			return names, NewSyscallError("readdir_r", i)
		}
		if result == nil {
			break // EOF
		}
		var name = string(result.Name[0:clen(result.Name[0:])])
		if name == "." || name == ".." { // Useless names
			continue
		}
		names = append(names, name)
		n--
	}
	if n >= 0 && len(names) == 0 {
		return names, io.EOF
	}
	return names, nil
}
<commit_msg>os: Use Entersyscall when reading directories.<commit_after>// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package os

import (
	"io"
	"syscall"
	"unsafe"
)

//extern opendir
func libc_opendir(*byte) *syscall.DIR

//extern closedir
func libc_closedir(*syscall.DIR) int

// FIXME: pathconf returns long, not int.
//extern pathconf
func libc_pathconf(*byte, int) int

func clen(n []byte) int {
	for i := 0; i < len(n); i++ {
		if n[i] == 0 {
			return i
		}
	}
	return len(n)
}

var elen int

func (file *File) readdirnames(n int) (names []string, err error) {
	if elen == 0 {
		var dummy syscall.Dirent
		elen = (int(unsafe.Offsetof(dummy.Name)) +
			libc_pathconf(syscall.StringBytePtr(file.name), syscall.PC_NAME_MAX) +
			1)
	}

	if file.dirinfo == nil {
		file.dirinfo = new(dirInfo)
		file.dirinfo.buf = make([]byte, elen)
		p := syscall.StringBytePtr(file.name)
		syscall.Entersyscall()
		r := libc_opendir(p)
		syscall.Exitsyscall()
		file.dirinfo.dir = r
	}

	entry_dirent := unsafe.Pointer(&file.dirinfo.buf[0]).(*syscall.Dirent)

	size := n
	if size < 0 {
		size = 100
		n = -1
	}

	names = make([]string, 0, size) // Empty with room to grow.

	dir := file.dirinfo.dir
	if dir == nil {
		return names, NewSyscallError("opendir", syscall.GetErrno())
	}

	for n != 0 {
		var result *syscall.Dirent
		pr := &result
		syscall.Entersyscall()
		i := libc_readdir_r(dir, entry_dirent, pr)
		syscall.Exitsyscall()
		if i != 0 {
			return names, NewSyscallError("readdir_r", i)
		}
		if result == nil {
			break // EOF
		}
		var name = string(result.Name[0:clen(result.Name[0:])])
		if name == "." || name == ".." { // Useless names
			continue
		}
		names = append(names, name)
		n--
	}
	if n >= 0 && len(names) == 0 {
		return names, io.EOF
	}
	return names, nil
}

{'fraction_non_alphanumeric': 0.15, 'fraction_numerical': 0.011666666666666667, 'mean_word_length': 3.226525821596244, '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': '306146', 'n_tokens_mistral': 1569, 'n_tokens_neox': 1346, 'n_words': 377}