Add gocage list snapshot myjail

This commit is contained in:
yo
2022-04-03 14:27:26 +02:00
parent 285229009f
commit 910be4ea31
4 changed files with 511 additions and 335 deletions

View File

@ -503,9 +503,10 @@ func displayStructFields(jails []Jail, valsToDisplay []string) {
*
*****************************************************************************/
// already defined in cmd/struct.go
//type lessFunc func(j1 *Jail, j2 *Jail) bool
func initSortStruct() JailSort {
// This struct hold "sort by jail fields" functions
type jailLessFunc func(j1 *Jail, j2 *Jail) bool
func initJailSortStruct() JailSort {
jcs := JailConfigSort{
Allow_chflagsInc: func(j1, j2 *Jail) bool {
return j1.Config.Allow_chflags < j2.Config.Allow_chflags
@ -1374,34 +1375,34 @@ func initSortStruct() JailSort {
return js
}
// multiSorter implements the Sort interface, sorting the jails within.
type multiSorter struct {
// jailSorter implements the Sort interface, sorting the jails within.
type jailSorter struct {
jails []Jail
less []lessFunc
less []jailLessFunc
}
// Sort sorts the argument slice according to the less functions passed to OrderedBy.
func (ms *multiSorter) Sort(jails []Jail) {
ms.jails = jails
sort.Sort(ms)
// Sort sorts the argument slice according to the less functions passed to JailsOrderedBy.
func (js *jailSorter) Sort(jails []Jail) {
js.jails = jails
sort.Sort(js)
}
// OrderedBy returns a Sorter that sorts using the less functions, in order.
// JailsOrderedBy returns a Sorter that sorts using the less functions, in order.
// Call its Sort method to sort the data.
func OrderedBy(less ...lessFunc) *multiSorter {
return &multiSorter{
func JailsOrderedBy(less ...jailLessFunc) *jailSorter {
return &jailSorter{
less: less,
}
}
// Len is part of sort.Interface.
func (ms *multiSorter) Len() int {
return len(ms.jails)
func (js *jailSorter) Len() int {
return len(js.jails)
}
// Swap is part of sort.Interface.
func (ms *multiSorter) Swap(i, j int) {
ms.jails[i], ms.jails[j] = ms.jails[j], ms.jails[i]
func (js *jailSorter) Swap(i, j int) {
js.jails[i], js.jails[j] = js.jails[j], js.jails[i]
}
// Less is part of sort.Interface. It is implemented by looping along the
@ -1410,12 +1411,12 @@ func (ms *multiSorter) Swap(i, j int) {
// less functions twice per call. We could change the functions to return
// -1, 0, 1 and reduce the number of calls for greater efficiency: an
// exercise for the reader.
func (ms *multiSorter) Less(i, j int) bool {
p, q := &ms.jails[i], &ms.jails[j]
func (js *jailSorter) Less(i, j int) bool {
p, q := &js.jails[i], &js.jails[j]
// Try all but the last comparison.
var k int
for k = 0; k < len(ms.less)-1; k++ {
less := ms.less[k]
for k = 0; k < len(js.less)-1; k++ {
less := js.less[k]
switch {
case less(p, q):
// p < q, so we have a decision.
@ -1428,9 +1429,119 @@ func (ms *multiSorter) Less(i, j int) bool {
}
// All comparisons to here said "equal", so just return whatever
// the final comparison reports.
return ms.less[k](p, q)
return js.less[k](p, q)
}
/*****************************************************************************
*
* Sorting snapshots
*
*****************************************************************************/
// This struct hold "sort by jail fields" functions
type snapshotLessFunc func(s1 *Snapshot, s2 *Snapshot) bool
func initSnapshotSortStruct() SnapshotSort {
ss := SnapshotSort{
NameInc: func(s1, s2 *Snapshot) bool {
return s1.Name < s2.Name
},
NameDec: func(s1, s2 *Snapshot) bool {
return s1.Name > s2.Name
},
DsNameInc: func(s1, s2 *Snapshot) bool {
return s1.Dsname < s2.Dsname
},
DsNameDec: func(s1, s2 *Snapshot) bool {
return s1.Dsname > s2.Dsname
},
MountpointInc: func(s1, s2 *Snapshot) bool {
return s1.Mountpoint < s2.Mountpoint
},
MountpointDec: func(s1, s2 *Snapshot) bool {
return s1.Mountpoint > s2.Mountpoint
},
UsedInc: func(s1, s2 *Snapshot) bool {
return s1.Used < s2.Used
},
UsedDec: func(s1, s2 *Snapshot) bool {
return s1.Used > s2.Used
},
ReferencedInc: func(s1, s2 *Snapshot) bool {
return s1.Referenced < s2.Referenced
},
ReferencedDec: func(s1, s2 *Snapshot) bool {
return s1.Referenced > s2.Referenced
},
CreationInc: func(s1, s2 *Snapshot) bool {
return s1.Creation.Unix() < s2.Creation.Unix()
},
CreationDec: func(s1, s2 *Snapshot) bool {
return s1.Creation.Unix() > s2.Creation.Unix()
},
}
return ss
}
// snapshotSorter implements the Sort interface, sorting the jails within.
type snapshotSorter struct {
snapshots []Snapshot
less []snapshotLessFunc
}
// Sort sorts the argument slice according to the less functions passed to OrderedBy.
func (ss *snapshotSorter) Sort(snapshots []Snapshot) {
ss.snapshots = snapshots
sort.Sort(ss)
}
// OrderedBy returns a Sorter that sorts using the less functions, in order.
// Call its Sort method to sort the data.
func SnapshotsOrderedBy(less ...snapshotLessFunc) *snapshotSorter {
return &snapshotSorter{
less: less,
}
}
// Len is part of sort.Interface.
func (ss *snapshotSorter) Len() int {
return len(ss.snapshots)
}
// Swap is part of sort.Interface.
func (ss *snapshotSorter) Swap(i, j int) {
ss.snapshots[i], ss.snapshots[j] = ss.snapshots[j], ss.snapshots[i]
}
// Less is part of sort.Interface. It is implemented by looping along the
// less functions until it finds a comparison that discriminates between
// the two items (one is less than the other). Note that it can call the
// less functions twice per call. We could change the functions to return
// -1, 0, 1 and reduce the number of calls for greater efficiency: an
// exercise for the reader.
func (ss *snapshotSorter) Less(i, j int) bool {
p, q := &ss.snapshots[i], &ss.snapshots[j]
// Try all but the last comparison.
var k int
for k = 0; k < len(ss.less)-1; k++ {
less := ss.less[k]
switch {
case less(p, q):
// p < q, so we have a decision.
return true
case less(q, p):
// p > q, so we have a decision.
return false
}
// p == q; try the next comparison.
}
// All comparisons to here said "equal", so just return whatever
// the final comparison reports.
return ss.less[k](p, q)
}
/*****************************************************************************
*
* Generic utilities