<commit_before>package fs

import (
	"fmt"
	"os"
	"os/user"
	"path"
	"path/filepath"
	"strings"
	"syscall"

	"github.com/op/go-logging"

	"github.com/subgraph/go-xdgdirs"
	"github.com/subgraph/oz"
)

type Filesystem struct {
	log     *logging.Logger
	base    string
	chroot  bool
	xdgDirs *xdgdirs.Dirs
	user    *user.User
	profile *oz.Profile
}

func NewFilesystem(config *oz.Config, log *logging.Logger, u *user.User, p *oz.Profile) *Filesystem {
	if log == nil {
		log = logging.MustGetLogger("oz")
	}

	dirs := new(xdgdirs.Dirs)
	if u != nil {
		dirs.Load(u.HomeDir)
	}
	return &Filesystem{
		base:    config.SandboxPath,
		log:     log,
		user:    u,
		xdgDirs: dirs,
		profile: p,
	}
}

func (fs *Filesystem) Root() string {
	return path.Join(fs.base, "rootfs")
}

func (fs *Filesystem) absPath(p string) string {
	if fs.chroot {
		return p
	}
	return path.Join(fs.Root(), p)
}

func (fs *Filesystem) CreateEmptyDir(target string) error {
	fi, err := os.Stat(target)
	if err != nil {
		return err
	}
	if err := os.MkdirAll(fs.absPath(target), fi.Mode().Perm()); err != nil {
		return err
	}

	return copyFileInfo(fi, fs.absPath(target))
}

func (fs *Filesystem) CreateDevice(devpath string, dev int, mode uint32, gid int) error {
	p := fs.absPath(devpath)
	um := syscall.Umask(0)
	if err := syscall.Mknod(p, mode, dev); err != nil {
		return fmt.Errorf("failed to mknod device '%s': %v", p, err)
	}
	if gid > 0 {
		if err := os.Chown(p, 0, gid); err != nil {
			return fmt.Errorf("failed to change group for device '%s': %v", p, err)
		}
	}
	syscall.Umask(um)
	return nil
}

func (fs *Filesystem) CreateSymlink(oldpath, newpath string) (string, error) {
	if err := syscall.Symlink(oldpath, fs.absPath(newpath)); err != nil {
		return "", fmt.Errorf("failed to symlink %s to %s: %v", fs.absPath(newpath), oldpath, err)
	}
	return fs.absPath(newpath), nil
}

func (fs *Filesystem) BindPath(from string, flags int, display int) error {
	return fs.bindResolve(from, "", flags, display)
}

func (fs *Filesystem) BindTo(from, to string, flags int, display int) error {
	return fs.bindResolve(from, to, flags, display)
}

const (
	BindReadOnly = 1 << iota
	BindCanCreate
	BindIgnore
	BindForce
	BindNoFollow
	BindAllowSetuid
)

func (fs *Filesystem) bindResolve(from string, to string, flags int, display int) error {
	if (to == "") || (from == to) {
		return fs.bindSame(from, flags, display)
	}
	if isGlobbed(to) {
		return fmt.Errorf("bind target (%s) cannot have globbed path", to)
	}
	t, err := resolveVars(to, display, fs.user, fs.xdgDirs, fs.profile)
	if err != nil {
		return err
	}
	if isGlobbed(from) {
		return fmt.Errorf("bind src (%s) cannot have globbed path with separate target path (%s)", from, to)
	}
	f, err := resolveVars(from, display, fs.user, fs.xdgDirs, fs.profile)
	if err != nil {
		return err
	}
	return fs.bind(f, t, flags)
}

func (fs *Filesystem) bindSame(p string, flags int, display int) error {
	ps, err := resolvePath(p, display, fs.user, fs.xdgDirs, fs.profile)
	if err != nil {
		return err
	}
	for _, p := range ps {
		if err := fs.bind(p, p, flags); err != nil {
			return err
		}
	}
	return nil
}

func (fs *Filesystem) bind(from string, to string, flags int) error {
	cc := flags&BindCanCreate != 0
	ii := flags&BindIgnore != 0
	ff := flags&BindForce != 0
	nf := flags&BindNoFollow != 0
	var src string
	var err error
	if !nf {
		src, err = filepath.EvalSymlinks(from)
		if err != nil && !cc && !ii {
			return fmt.Errorf("error resolving symlinks for path (%s): %v", from, err)
		}
	}
	if src == "" {
		src = from
	}
	sinfo, err := readSourceInfo(src, cc, fs)
	if err != nil {
		if !ii {
			return fmt.Errorf("failed to bind path (%s): %v", src, err)
		} else {
			fs.log.Warning("bind target (%s) missing and ignored!", src)
			return nil
		}
	}
	if sinfo == nil {
		fs.log.Warning("bind target (%s) does not exist and has been ignored!", src)
		return nil
	}

	if to == "" {
		to = from
	}
	oto := to
	to = path.Join(fs.Root(), to)

	s, err := os.Stat(to)
	if !ff && (err == nil || !os.IsNotExist(err)) {
		fs.log.Warning("Target (%s > %s) already exists, ignoring: %v %v", src, to, err, s)
		return nil
	}
<commit_msg>Fixed warning msg...<commit_after>package fs

import (
	"fmt"
	"os"
	"os/user"
	"path"
	"path/filepath"
	"strings"
	"syscall"

	"github.com/op/go-logging"

	"github.com/subgraph/go-xdgdirs"
	"github.com/subgraph/oz"
)

type Filesystem struct {
	log     *logging.Logger
	base    string
	chroot  bool
	xdgDirs *xdgdirs.Dirs
	user    *user.User
	profile *oz.Profile
}

func NewFilesystem(config *oz.Config, log *logging.Logger, u *user.User, p *oz.Profile) *Filesystem {
	if log == nil {
		log = logging.MustGetLogger("oz")
	}

	dirs := new(xdgdirs.Dirs)
	if u != nil {
		dirs.Load(u.HomeDir)
	}
	return &Filesystem{
		base:    config.SandboxPath,
		log:     log,
		user:    u,
		xdgDirs: dirs,
		profile: p,
	}
}

func (fs *Filesystem) Root() string {
	return path.Join(fs.base, "rootfs")
}

func (fs *Filesystem) absPath(p string) string {
	if fs.chroot {
		return p
	}
	return path.Join(fs.Root(), p)
}

func (fs *Filesystem) CreateEmptyDir(target string) error {
	fi, err := os.Stat(target)
	if err != nil {
		return err
	}
	if err := os.MkdirAll(fs.absPath(target), fi.Mode().Perm()); err != nil {
		return err
	}

	return copyFileInfo(fi, fs.absPath(target))
}

func (fs *Filesystem) CreateDevice(devpath string, dev int, mode uint32, gid int) error {
	p := fs.absPath(devpath)
	um := syscall.Umask(0)
	if err := syscall.Mknod(p, mode, dev); err != nil {
		return fmt.Errorf("failed to mknod device '%s': %v", p, err)
	}
	if gid > 0 {
		if err := os.Chown(p, 0, gid); err != nil {
			return fmt.Errorf("failed to change group for device '%s': %v", p, err)
		}
	}
	syscall.Umask(um)
	return nil
}

func (fs *Filesystem) CreateSymlink(oldpath, newpath string) (string, error) {
	if err := syscall.Symlink(oldpath, fs.absPath(newpath)); err != nil {
		return "", fmt.Errorf("failed to symlink %s to %s: %v", fs.absPath(newpath), oldpath, err)
	}
	return fs.absPath(newpath), nil
}

func (fs *Filesystem) BindPath(from string, flags int, display int) error {
	return fs.bindResolve(from, "", flags, display)
}

func (fs *Filesystem) BindTo(from, to string, flags int, display int) error {
	return fs.bindResolve(from, to, flags, display)
}

const (
	BindReadOnly = 1 << iota
	BindCanCreate
	BindIgnore
	BindForce
	BindNoFollow
	BindAllowSetuid
)

func (fs *Filesystem) bindResolve(from string, to string, flags int, display int) error {
	if (to == "") || (from == to) {
		return fs.bindSame(from, flags, display)
	}
	if isGlobbed(to) {
		return fmt.Errorf("bind target (%s) cannot have globbed path", to)
	}
	t, err := resolveVars(to, display, fs.user, fs.xdgDirs, fs.profile)
	if err != nil {
		return err
	}
	if isGlobbed(from) {
		return fmt.Errorf("bind src (%s) cannot have globbed path with separate target path (%s)", from, to)
	}
	f, err := resolveVars(from, display, fs.user, fs.xdgDirs, fs.profile)
	if err != nil {
		return err
	}
	return fs.bind(f, t, flags)
}

func (fs *Filesystem) bindSame(p string, flags int, display int) error {
	ps, err := resolvePath(p, display, fs.user, fs.xdgDirs, fs.profile)
	if err != nil {
		return err
	}
	for _, p := range ps {
		if err := fs.bind(p, p, flags); err != nil {
			return err
		}
	}
	return nil
}

func (fs *Filesystem) bind(from string, to string, flags int) error {
	cc := flags&BindCanCreate != 0
	ii := flags&BindIgnore != 0
	ff := flags&BindForce != 0
	nf := flags&BindNoFollow != 0
	var src string
	var err error
	if !nf {
		src, err = filepath.EvalSymlinks(from)
		if err != nil && !cc && !ii {
			return fmt.Errorf("error resolving symlinks for path (%s): %v", from, err)
		}
	}
	if src == "" {
		src = from
	}
	sinfo, err := readSourceInfo(src, cc, fs)
	if err != nil {
		if !ii {
			return fmt.Errorf("failed to bind path (%s): %v", src, err)
		} else {
			fs.log.Warning("bind target (%s) missing and ignored!", src)
			return nil
		}
	}
	if sinfo == nil {
		fs.log.Warning("bind target (%s) does not exist and has been ignored!", src)
		return nil
	}

	if to == "" {
		to = from
	}
	oto := to
	to = path.Join(fs.Root(), to)

	_, err = os.Stat(to)
	if !ff && (err == nil || !os.IsNotExist(err)) {
		fs.log.Warning("Target (%s > %s) already exists, ignoring!", src, to)
		return nil
	}

{'fraction_non_alphanumeric': 0.1556410873225884, 'fraction_numerical': 0.0024055809477988932, 'mean_word_length': 3.5536692223439212, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '4340221', 'n_tokens_mistral': 3343, 'n_tokens_neox': 3214, 'n_words': 943}