package procfs
import (
"bufio"
"bytes"
"fmt"
"regexp"
"github.com/prometheus/procfs/internal/util"
)
var (
rPos = regexp .MustCompile (`^pos:\s+(\d+)$` )
rFlags = regexp .MustCompile (`^flags:\s+(\d+)$` )
rMntID = regexp .MustCompile (`^mnt_id:\s+(\d+)$` )
rIno = regexp .MustCompile (`^ino:\s+(\d+)$` )
rInotify = regexp .MustCompile (`^inotify` )
rInotifyParts = regexp .MustCompile (`^inotify\s+wd:([0-9a-f]+)\s+ino:([0-9a-f]+)\s+sdev:([0-9a-f]+)(?:\s+mask:([0-9a-f]+))?` )
)
type ProcFDInfo struct {
FD string
Pos string
Flags string
MntID string
Ino string
InotifyInfos []InotifyInfo
}
func (p Proc ) FDInfo (fd string ) (*ProcFDInfo , error ) {
data , err := util .ReadFileNoStat (p .path ("fdinfo" , fd ))
if err != nil {
return nil , err
}
var text , pos , flags , mntid , ino string
var inotify []InotifyInfo
scanner := bufio .NewScanner (bytes .NewReader (data ))
for scanner .Scan () {
text = scanner .Text ()
if rPos .MatchString (text ) {
pos = rPos .FindStringSubmatch (text )[1 ]
} else if rFlags .MatchString (text ) {
flags = rFlags .FindStringSubmatch (text )[1 ]
} else if rMntID .MatchString (text ) {
mntid = rMntID .FindStringSubmatch (text )[1 ]
} else if rIno .MatchString (text ) {
ino = rIno .FindStringSubmatch (text )[1 ]
} else if rInotify .MatchString (text ) {
newInotify , err := parseInotifyInfo (text )
if err != nil {
return nil , err
}
inotify = append (inotify , *newInotify )
}
}
i := &ProcFDInfo {
FD : fd ,
Pos : pos ,
Flags : flags ,
MntID : mntid ,
Ino : ino ,
InotifyInfos : inotify ,
}
return i , nil
}
type InotifyInfo struct {
WD string
Ino string
Sdev string
Mask string
}
func parseInotifyInfo(line string ) (*InotifyInfo , error ) {
m := rInotifyParts .FindStringSubmatch (line )
if len (m ) >= 4 {
var mask string
if len (m ) == 5 {
mask = m [4 ]
}
i := &InotifyInfo {
WD : m [1 ],
Ino : m [2 ],
Sdev : m [3 ],
Mask : mask ,
}
return i , nil
}
return nil , fmt .Errorf ("%w: invalid inode entry: %q" , ErrFileParse , line )
}
type ProcFDInfos []ProcFDInfo
func (p ProcFDInfos ) Len () int { return len (p ) }
func (p ProcFDInfos ) Swap (i , j int ) { p [i ], p [j ] = p [j ], p [i ] }
func (p ProcFDInfos ) Less (i , j int ) bool { return p [i ].FD < p [j ].FD }
func (p ProcFDInfos ) InotifyWatchLen () (int , error ) {
length := 0
for _ , f := range p {
length += len (f .InotifyInfos )
}
return length , nil
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .