fix: don't copy elements of OpenFilesStat slice again

pull/1866/head
NitroCao 4 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) { func (p *Process) OpenFilesWithContext(ctx context.Context) ([]OpenFilesStat, error) {
_, ofs, err := p.fillFromfdWithContext(ctx) _, ofs, err := p.fillFromfdWithContext(ctx)
if err != nil { return ofs, err
return nil, err
}
ret := make([]OpenFilesStat, len(ofs))
for i, o := range ofs {
ret[i] = *o
}
return ret, nil
} }
func (p *Process) ConnectionsWithContext(ctx context.Context) ([]net.ConnectionStat, error) { 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 // 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) statPath, fnames, err := p.fillFromfdListWithContext(ctx)
if err != nil { if err != nil {
return 0, nil, err return 0, nil, err
} }
numFDs := int32(len(fnames)) numFDs := int32(len(fnames))
openfiles := make([]*OpenFilesStat, 0, numFDs) openfiles := make([]OpenFilesStat, 0, numFDs)
for _, fd := range fnames { for _, fd := range fnames {
fpath := filepath.Join(statPath, fd) fpath := filepath.Join(statPath, fd)
path, err := common.Readlink(fpath) path, err := common.Readlink(fpath)
@ -647,7 +639,7 @@ func (p *Process) fillFromfdWithContext(ctx context.Context) (int32, []*OpenFile
if err != nil { if err != nil {
return numFDs, openfiles, err return numFDs, openfiles, err
} }
o := &OpenFilesStat{ o := OpenFilesStat{
Path: path, Path: path,
Fd: t, Fd: t,
} }

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

Loading…
Cancel
Save