//go:build !windows

package main

import "syscall"

func freeSpace(path string) (uint64, error) {
	var s syscall.Statfs_t
	e := syscall.Statfs(path, &s)
	if e != nil {
		return 0, e
	}
	return s.Bavail * uint64(s.Bsize), nil
}
func rejectUnsafePath(root, p string) error { return rejectSymlinkPath(root, p) }
func rejectSymlinkPath(root, p string) error {
	cur := p
	for {
		st, e := osLstat(cur)
		if e == nil && st&uint32(syscall.S_IFMT) == uint32(syscall.S_IFLNK) {
			return syscall.ELOOP
		}
		if samePath(cur, root) {
			break
		}
		next := filepathDir(cur)
		if next == cur {
			break
		}
		cur = next
	}
	return nil
}
func osLstat(p string) (uint32, error) {
	var s syscall.Stat_t
	e := syscall.Lstat(p, &s)
	return s.Mode, e
}
func filepathDir(p string) string {
	for len(p) > 1 && (p[len(p)-1] == '/' || p[len(p)-1] == '\\') {
		p = p[:len(p)-1]
	}
	i := len(p) - 1
	for i >= 0 && p[i] != '/' && p[i] != '\\' {
		i--
	}
	if i <= 0 {
		return "/"
	}
	return p[:i]
}
