From b1d77d2d891f04b710d610b47388e431fc519a07 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Mon, 24 Mar 2025 17:56:23 +0200 Subject: initial dup --- internal/file/file.go | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'internal/file') diff --git a/internal/file/file.go b/internal/file/file.go index 513212b..d43a79e 100644 --- a/internal/file/file.go +++ b/internal/file/file.go @@ -18,7 +18,7 @@ type File interface { type FdFile struct { fd int32 name string - Flags int32 + Flags *int32 // TODO: This must be a pointer as via dup(2) - means, we need to synchronise access as well .. use atomicint flagsFromFdInfo bool } @@ -26,7 +26,7 @@ func NewFd(fd int32, name []byte, flags int32) FdFile { return FdFile{ fd: fd, name: stringValue(name), - Flags: flags, + Flags: &flags, } } @@ -36,7 +36,7 @@ func NewFdWithPid(fd int32, pid uint32) FdFile { return FdFile{ fd: fd, name: "?", - Flags: -1, + Flags: nil, // Unknown flags at this point flagsFromFdInfo: true, } } @@ -45,11 +45,18 @@ func NewFdWithPid(fd int32, pid uint32) FdFile { return FdFile{ fd: fd, name: linkName, - Flags: flags, + Flags: &flags, flagsFromFdInfo: true, } } +func (f FdFile) Dup(fd int32) FdFile { + duppedFd := f + duppedFd.fd = fd + duppedFd.flagsFromFdInfo = false + return duppedFd +} + func readFlagsFromFdInfo(fd int32, pid uint32) (int32, error) { data, err := os.ReadFile(fmt.Sprintf("/proc/%d/fdinfo/%d", pid, fd)) if err != nil { @@ -72,7 +79,11 @@ func (f FdFile) Name() string { } func (f FdFile) FlagsString() string { - return flagsToStr(f.Flags) + var flags int32 = -1 + if f.Flags != nil { + flags = *f.Flags + } + return flagsToStr(flags) } func (f FdFile) String() string { -- cgit v1.2.3