fix: don't copy elements of OpenFilesStat slice again

pull/1866/head
NitroCao 3 weeks ago
parent 81edea074a
commit 9dcddfedc7

@ -372,15 +372,7 @@ func (p *Process) ChildrenWithContext(ctx context.Context) ([]*Process, error) {
func (p *Process) OpenFilesWithContext(ctx context.Context) ([]OpenFilesStat, error) {
_, ofs, err := p.fillFromfdWithContext(ctx)
if err != nil {
return nil, err
}
ret := make([]OpenFilesStat, len(ofs))
for i, o := range ofs {
ret[i] = *o
}
return ret, nil
return ofs, err
}
func (p *Process) ConnectionsWithContext(ctx context.Context) ([]net.ConnectionStat, error) {
@ -629,14 +621,14 @@ func (p *Process) fillFromfdListWithContext(ctx context.Context) (string, []stri
}
// Get num_fds from /proc/(pid)/fd
func (p *Process) fillFromfdWithContext(ctx context.Context) (int32, []*OpenFilesStat, error) {
func (p *Process) fillFromfdWithContext(ctx context.Context) (int32, []OpenFilesStat, error) {
statPath, fnames, err := p.fillFromfdListWithContext(ctx)
if err != nil {
return 0, nil, err
}
numFDs := int32(len(fnames))
openfiles := make([]*OpenFilesStat, 0, numFDs)
openfiles := make([]OpenFilesStat, 0, numFDs)
for _, fd := range fnames {
fpath := filepath.Join(statPath, fd)
path, err := common.Readlink(fpath)
@ -647,7 +639,7 @@ func (p *Process) fillFromfdWithContext(ctx context.Context) (int32, []*OpenFile
if err != nil {
return numFDs, openfiles, err
}
o := &OpenFilesStat{
o := OpenFilesStat{
Path: path,
Fd: t,
}

@ -18,7 +18,7 @@ import (
func TestFillFromfdWithContext(t *testing.T) {
type expect struct {
numFDs int32
openFiles []*OpenFilesStat
openFiles []OpenFilesStat
err error
}
type testCase struct {
@ -33,7 +33,7 @@ func TestFillFromfdWithContext(t *testing.T) {
pid: 1,
expected: &expect{
numFDs: 3,
openFiles: []*OpenFilesStat{
openFiles: []OpenFilesStat{
{
Path: "/foo",
Fd: 0,

Loading…
Cancel
Save