<commit_before>// A FUSE filesystem that shunts all request to an underlying file
// system.  Its main purpose is to provide test coverage without
// having to build an actual synthetic filesystem.

package fuse

import (
	"fmt"
	"io"
	"log"
	"os"
	"path/filepath"
	"syscall"
	"time"
)

var _ = fmt.Println
var _ = log.Println

type LoopbackFileSystem struct {
	Root string

	DefaultFileSystem
}

func NewLoopbackFileSystem(root string) (out *LoopbackFileSystem) {
	out = new(LoopbackFileSystem)
	out.Root = root

	return out
}

func (me *LoopbackFileSystem) GetPath(relPath string) string {
	return filepath.Join(me.Root, relPath)
}

func (me *LoopbackFileSystem) GetAttr(name string, context *Context) (a *Attr, code Status) {
	fullPath := me.GetPath(name)
	var err error = nil
	st := syscall.Stat_t{}
	if name == "" {
		// When GetAttr is called for the toplevel directory, we always want
		// to look through symlinks.
		err = syscall.Stat(fullPath, &st)
	} else {
		err = syscall.Lstat(fullPath, &st)
	}
	if err != nil {
		return nil, ToStatus(err)
	}
	a = &Attr{}
	a.FromStat(&st)
	return a, OK
}

func (me *LoopbackFileSystem) OpenDir(name string, context *Context) (stream chan DirEntry, status Status) {
	// What other ways beyond O_RDONLY are there to open
	// directories?
	f, err := os.Open(me.GetPath(name))
	if err != nil {
		return nil, ToStatus(err)
	}
	want := 500
	output := make(chan DirEntry, want)
	go func() {
		for {
			infos, err := f.Readdir(want)
			for i := range infos {
				s := ToStatT(infos[i])
				output <- DirEntry{
					Name: infos[i].Name(),
					Mode: s.Mode,
				}
			}
			if len(infos) < want || err == io.EOF {
				break
			}
			if err != nil {
				log.Println("Readdir() returned err:", err)
				break
			}
		}
		close(output)
		f.Close()
	}()

	return output, OK
}

func (me *LoopbackFileSystem) Open(name string, flags uint32, context *Context) (fuseFile File, status Status) {
	f, err := os.OpenFile(me.GetPath(name), int(flags), 0)
	if err != nil {
		return nil, ToStatus(err)
	}<commit_msg>Survive if ioutil.ReadDir returns an entry without stat data.<commit_after>// A FUSE filesystem that shunts all request to an underlying file
// system.  Its main purpose is to provide test coverage without
// having to build an actual synthetic filesystem.

package fuse

import (
	"fmt"
	"io"
	"log"
	"os"
	"path/filepath"
	"syscall"
	"time"
)

var _ = fmt.Println
var _ = log.Println

type LoopbackFileSystem struct {
	Root string

	DefaultFileSystem
}

func NewLoopbackFileSystem(root string) (out *LoopbackFileSystem) {
	out = new(LoopbackFileSystem)
	out.Root = root

	return out
}

func (me *LoopbackFileSystem) GetPath(relPath string) string {
	return filepath.Join(me.Root, relPath)
}

func (me *LoopbackFileSystem) GetAttr(name string, context *Context) (a *Attr, code Status) {
	fullPath := me.GetPath(name)
	var err error = nil
	st := syscall.Stat_t{}
	if name == "" {
		// When GetAttr is called for the toplevel directory, we always want
		// to look through symlinks.
		err = syscall.Stat(fullPath, &st)
	} else {
		err = syscall.Lstat(fullPath, &st)
	}
	if err != nil {
		return nil, ToStatus(err)
	}
	a = &Attr{}
	a.FromStat(&st)
	return a, OK
}

func (me *LoopbackFileSystem) OpenDir(name string, context *Context) (stream chan DirEntry, status Status) {
	// What other ways beyond O_RDONLY are there to open
	// directories?
	f, err := os.Open(me.GetPath(name))
	if err != nil {
		return nil, ToStatus(err)
	}
	want := 500
	output := make(chan DirEntry, want)
	go func() {
		for {
			infos, err := f.Readdir(want)
			for i := range infos {
				d := DirEntry{
					Name: infos[i].Name(),
				}
				if s := ToStatT(infos[i]); s != nil {
					d.Mode = s.Mode
				}
				output <- d
			}
			if len(infos) < want || err == io.EOF {
				break
			}
			if err != nil {
				log.Println("Readdir() returned err:", err)
				break
			}
		}
		close(output)
		f.Close()
	}()

	return output, OK
}

func (me *LoopbackFileSystem) Open(name string, flags uint32, context *Context) (fuseFile File, status Status) {
	f, err := os.OpenFile(me.GetPath(name), int(flags), 0)
	if err != nil {
		return nil, ToStatus(err)
	}
{'fraction_non_alphanumeric': 0.1287915262397689, 'fraction_numerical': 0.002888781896966779, 'mean_word_length': 3.410828025477707, 'pattern_counts': {'":': 0, '<': 7, '<?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': '7611797', 'n_tokens_mistral': 1640, 'n_tokens_neox': 1482, 'n_words': 457}