Skip to content

Commit

Permalink
[CWS] optimize os.Stat in hash resolver because of heavy usage (#29120
Browse files Browse the repository at this point in the history
)
  • Loading branch information
paulcacheux authored Sep 9, 2024
1 parent 9d11690 commit e7306ac
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
6 changes: 2 additions & 4 deletions pkg/security/resolvers/hash/resolver_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"io/fs"
"os"
"slices"
"syscall"

"github.com/DataDog/datadog-go/v5/statsd"
"github.com/glaslos/ssdeep"
Expand Down Expand Up @@ -215,18 +214,17 @@ type fileUniqKey struct {
}

func getFileInfo(path string) (fs.FileMode, int64, fileUniqKey, error) {
fileInfo, err := os.Stat(path)
stat, err := utils.UnixStat(path)
if err != nil {
return 0, 0, fileUniqKey{}, err
}

stat := fileInfo.Sys().(*syscall.Stat_t)
fkey := fileUniqKey{
dev: stat.Dev,
inode: stat.Ino,
}

return fileInfo.Mode(), fileInfo.Size(), fkey, nil
return utils.UnixStatModeToGoFileMode(stat.Mode), stat.Size, fkey, nil
}

// hash hashes the provided file event
Expand Down
59 changes: 59 additions & 0 deletions pkg/security/utils/stat_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// 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 2016-present Datadog, Inc.

//go:build unix

// Package utils holds utils related files
package utils

import (
"io/fs"
"syscall"
)

// UnixStat is an unix only equivalent to os.Stat, but alloc-free,
// and returning directly the platform-specific syscall.Stat_t structure.
func UnixStat(path string) (syscall.Stat_t, error) {
var stat syscall.Stat_t
var err error
for {
err := syscall.Stat(path, &stat)
if err != syscall.EINTR {
break
}
}
return stat, err
}

// UnixStatModeToGoFileMode converts a Unix mode to a Go fs.FileMode.
func UnixStatModeToGoFileMode(mode uint32) fs.FileMode {
fsmode := fs.FileMode(mode & 0777)
switch mode & syscall.S_IFMT {
case syscall.S_IFBLK:
fsmode |= fs.ModeDevice
case syscall.S_IFCHR:
fsmode |= fs.ModeDevice | fs.ModeCharDevice
case syscall.S_IFDIR:
fsmode |= fs.ModeDir
case syscall.S_IFIFO:
fsmode |= fs.ModeNamedPipe
case syscall.S_IFLNK:
fsmode |= fs.ModeSymlink
case syscall.S_IFREG:
// nothing to do
case syscall.S_IFSOCK:
fsmode |= fs.ModeSocket
}
if mode&syscall.S_ISGID != 0 {
fsmode |= fs.ModeSetgid
}
if mode&syscall.S_ISUID != 0 {
fsmode |= fs.ModeSetuid
}
if mode&syscall.S_ISVTX != 0 {
fsmode |= fs.ModeSticky
}
return fsmode
}

0 comments on commit e7306ac

Please sign in to comment.