/
basedirs.go
151 lines (123 loc) · 4.39 KB
/
basedirs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*******************************************************************************
* Copyright (c) 2022, 2023 Genome Research Ltd.
*
* Authors:
* Sendu Bala <sb10@sanger.ac.uk>
* Michael Woolnough <mw31@sanger.ac.uk>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************/
// package basedirs is used to summarise disk usage information by base
// directory, storing and retrieving the information from an embedded database.
package basedirs
import (
"regexp"
"strings"
"github.com/ugorji/go/codec"
"github.com/wtsi-ssg/wrstat/v4/dgut"
)
const (
basedirSplits = 4
basedirMinDirs = 4
basedirMinDirsMDT = 5
)
var basedirMDTRegexp = regexp.MustCompile(`\/mdt\d(\/|\z)`)
// BaseDirs is used to summarise disk usage information by base directory and
// group or user.
type BaseDirs struct {
dbPath string
tree *dgut.Tree
quotas *Quotas
ch codec.Handle
mountPoints mountPoints
}
// NewCreator returns a BaseDirs that lets you create a database summarising
// usage information by base directory, taken from the given tree and quotas.
func NewCreator(dbPath string, tree *dgut.Tree, quotas *Quotas) (*BaseDirs, error) {
mp, err := getMountPoints()
if err != nil {
return nil, err
}
return &BaseDirs{
dbPath: dbPath,
tree: tree,
quotas: quotas,
ch: new(codec.BincHandle),
mountPoints: mp,
}, nil
}
// SetMountPoints can be used to manually set your mountpoints, if the automatic
// discovery of mountpoints on your system doesn't work.
func (b *BaseDirs) SetMountPoints(mountpoints []string) {
b.mountPoints = mountpoints
}
// CalculateForGroup calculates all the base directories for the given group.
func (b *BaseDirs) CalculateForGroup(gid uint32) (dgut.DCSs, error) {
var dcss dgut.DCSs
if err := b.filterWhereResults(&dgut.Filter{GIDs: []uint32{gid}}, func(ds *dgut.DirSummary) {
dcss = append(dcss, ds)
}); err != nil {
return nil, err
}
return dcss, nil
}
func (b *BaseDirs) filterWhereResults(filter *dgut.Filter, cb func(ds *dgut.DirSummary)) error {
dcss, err := b.tree.Where("/", filter, basedirSplits)
if err != nil {
return err
}
dcss.SortByDir()
var previous string
for _, ds := range dcss {
if notEnoughDirs(ds.Dir) || childOfPreviousResult(ds.Dir, previous) {
continue
}
cb(ds)
// used to be `dirs = append(dirs, ds.Dir)`
// then for each dir, `outFile.WriteString(fmt.Sprintf("%d\t%s\n", gid, dir))`
previous = ds.Dir
}
return nil
}
// notEnoughDirs returns true if the given path has fewer than 4 directories.
// If path has an mdt directory in it, then it becomes 5 directories.
func notEnoughDirs(path string) bool {
numDirs := strings.Count(path, "/")
min := basedirMinDirs
if basedirMDTRegexp.MatchString(path) {
min = basedirMinDirsMDT
}
return numDirs < min
}
// childOfPreviousResult returns true if previous is not blank, and dir starts
// with it.
func childOfPreviousResult(dir, previous string) bool {
return previous != "" && strings.HasPrefix(dir, previous)
}
// CalculateForUser calculates all the base directories for the given user.
func (b *BaseDirs) CalculateForUser(uid uint32) (dgut.DCSs, error) {
var dcss dgut.DCSs
if err := b.filterWhereResults(&dgut.Filter{UIDs: []uint32{uid}}, func(ds *dgut.DirSummary) {
dcss = append(dcss, ds)
}); err != nil {
return nil, err
}
return dcss, nil
}