// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2026-present Datadog, Inc.

//go:build !windows

package ls

import (
	"context"
	"fmt"
	iofs "io/fs"
	"syscall"

	"github.com/DataDog/rshell/builtins"
)

// fileOwner returns the numeric UID, GID, and hard link count for the given
// FileInfo. Names are not resolved to avoid reading /etc/passwd or triggering
// NSS/LDAP lookups outside the sandbox.
// The ctx, callCtx, and path parameters are used on Windows to open files
// through the sandbox for GetFileInformationByHandle; on Unix they are ignored.
func fileOwner(_ context.Context, _ *builtins.CallContext, _ string, info iofs.FileInfo) (owner, group, nlink string) {
	st, ok := info.Sys().(*syscall.Stat_t)
	if !ok {
		return "?", "?", "?"
	}

	owner = fmt.Sprintf("%d", st.Uid)
	group = fmt.Sprintf("%d", st.Gid)
	nlink = fmt.Sprintf("%d", st.Nlink)
	return owner, group, nlink
}

// fileBlocks returns the number of 512-byte blocks allocated for the file
// and whether the value is available. The ctx, callCtx, and path parameters
// are used on Windows; on Unix they are ignored (Stat_t has everything).
func fileBlocks(_ context.Context, _ *builtins.CallContext, _ string, info iofs.FileInfo) (int64, bool) {
	st, ok := info.Sys().(*syscall.Stat_t)
	if !ok {
		return 0, false
	}
	return st.Blocks, true
}
