-
Notifications
You must be signed in to change notification settings - Fork 0
/
metarecord.go
146 lines (115 loc) · 3.8 KB
/
metarecord.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
// Copyright 2020 Adam Chalkley
//
// https://github.com/atc0005/check-path
//
// Licensed under the MIT License. See LICENSE file in the project root for
// full license information.
package paths
import (
"os"
"sort"
"time"
"github.com/atc0005/check-path/internal/units"
"github.com/phayes/permbits"
)
// ID is a collection of username and group name values, associated with a
// specific os.FileInfo value.
type ID struct {
Username string
UID int
UIDStr string
GroupName string
GID int
GIDStr string
}
// MetaRecord represents a superset of statistics for a file. This includes
// os.FileInfo, the fully-qualified path to the file and potentially
// user/group information.
type MetaRecord struct {
// Size, name and other common details for the associated path.
os.FileInfo
// Permissions for the associated path.
Permissions permbits.PermissionBits
// Username and group values.
ID
// FQPath is the original, fully-qualified path.
FQPath string
// ParentDir is the parent directory for the path.
ParentDir string
}
// MetaRecords is a slice of MetaRecord objects intended for bulk processing.
type MetaRecords []MetaRecord
// TotalFileSize returns the cumulative size of all MetaRecord objects in the
// slice in bytes. Since this would also apply to directories, those objects
// are filtered out in an effort to provide a more accurate value.
func (mr MetaRecords) TotalFileSize() int64 {
var totalSize int64
for _, file := range mr {
// Skip any directory (MetaRecord) entries that may have been added.
if file.IsDir() {
continue
}
totalSize += file.Size()
}
return totalSize
}
// TotalFileSizeHR returns a human-readable string of the cumulative size of
// all files in the slice of bytes.
func (mr MetaRecords) TotalFileSizeHR() string {
return units.ByteCountIEC(mr.TotalFileSize())
}
// SizeHR returns a human-readable string of the size of a MetaRecord object.
// Unless filtered later, this also applies to directories.
func (mr MetaRecord) SizeHR() string {
return units.ByteCountIEC(mr.Size())
}
// AgeExceeded indicates whether a path is older than the specified threshold
// in days. If the path age is younger or equal to the specified number of
// days then the threshold is considered uncrossed.
func AgeExceeded(file os.FileInfo, days int) bool {
var oldFile bool
now := time.Now()
fileModTime := file.ModTime()
// Flip user specified number of days negative so that we can wind
// back that many days from the file modification time. This gives
// us our threshold to compare file modification times against.
daysBack := -(days)
fileAgeThreshold := now.AddDate(0, 0, daysBack)
switch {
case fileModTime.Before(fileAgeThreshold):
oldFile = true
case fileModTime.Equal(fileAgeThreshold):
oldFile = false
case fileModTime.After(fileAgeThreshold):
oldFile = false
}
return oldFile
}
// SortByModTimeAsc sorts slice of MetaRecord objects in ascending order with
// older values listed first.
func (mr MetaRecords) SortByModTimeAsc() {
sort.Slice(mr, func(i, j int) bool {
return mr[i].ModTime().Before(mr[j].ModTime())
})
}
// SortByModTimeDesc sorts slice of MetaRecord objects in descending order with
// newer values listed first.
func (mr MetaRecords) SortByModTimeDesc() {
sort.Slice(mr, func(i, j int) bool {
return mr[i].ModTime().After(mr[j].ModTime())
})
}
// SortBySizeAsc sorts slice of MetaRecord objects in ascending order with
// smaller values listed first.
func (mr MetaRecords) SortBySizeAsc() {
sort.Slice(mr, func(i, j int) bool {
return mr[i].FileInfo.Size() > mr[j].FileInfo.Size()
})
}
// SortBySizeDesc sorts slice of MetaRecord objects in descending order with
// larger values listed first.
func (mr MetaRecords) SortBySizeDesc() {
sort.Slice(mr, func(i, j int) bool {
return mr[i].FileInfo.Size() < mr[j].FileInfo.Size()
})
}