Skip to content

Commit c540167

Browse files
committed
Merge branch 'master' of github.com:sohaha/zlsgo
2 parents 0034642 + 18467f1 commit c540167

File tree

4 files changed

+115
-6
lines changed

4 files changed

+115
-6
lines changed

zfile/file.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import (
1111
"net/http"
1212
"os"
1313
"path/filepath"
14+
"sort"
1415
"strings"
16+
"time"
1517
)
1618

1719
var ProjectPath = "./"
@@ -263,3 +265,89 @@ func HasPermission(path string, perm os.FileMode, noUp ...bool) bool {
263265
func HasReadWritePermission(path string) bool {
264266
return HasPermission(path, fs.FileMode(0o600))
265267
}
268+
269+
type fileInfo struct {
270+
path string
271+
size uint64
272+
modTime time.Time
273+
}
274+
275+
type fileInfos []fileInfo
276+
277+
func (f fileInfos) Len() int { return len(f) }
278+
func (f fileInfos) Less(i, j int) bool { return f[i].modTime.Before(f[j].modTime) }
279+
func (f fileInfos) Swap(i, j int) { f[i], f[j] = f[j], f[i] }
280+
281+
type DirStatOptions struct {
282+
MaxSize uint64 // max size
283+
MaxTotal uint64 // max total files
284+
}
285+
286+
// StatDir get directory size and total files
287+
func StatDir(path string, options ...DirStatOptions) (size, total uint64, err error) {
288+
var (
289+
totalSize uint64
290+
totalFiles uint64
291+
files = make([]fileInfo, 0, 1024)
292+
needCollect = len(options) > 0 && (options[0].MaxSize > 0 || options[0].MaxTotal > 0)
293+
)
294+
295+
err = filepath.WalkDir(path, func(filePath string, d fs.DirEntry, err error) error {
296+
if err != nil {
297+
return err
298+
}
299+
300+
if d.Type()&os.ModeSymlink != 0 {
301+
return nil
302+
}
303+
304+
if !d.IsDir() {
305+
info, err := d.Info()
306+
if err != nil {
307+
return err
308+
}
309+
310+
fileSize := uint64(info.Size())
311+
totalSize += fileSize
312+
totalFiles++
313+
314+
if needCollect {
315+
files = append(files, fileInfo{
316+
path: filePath,
317+
size: fileSize,
318+
modTime: info.ModTime(),
319+
})
320+
}
321+
}
322+
return nil
323+
})
324+
325+
if err != nil {
326+
return totalSize, totalFiles, err
327+
}
328+
329+
if needCollect {
330+
needCleanup := totalSize > options[0].MaxSize
331+
if options[0].MaxTotal > 0 && totalFiles > options[0].MaxTotal {
332+
needCleanup = true
333+
}
334+
335+
if needCleanup {
336+
sort.Sort(fileInfos(files))
337+
for i := 0; i < len(files); i++ {
338+
if (options[0].MaxSize == 0 || totalSize <= options[0].MaxSize) &&
339+
(options[0].MaxTotal == 0 || totalFiles <= options[0].MaxTotal) {
340+
break
341+
}
342+
343+
if err := os.Remove(files[i].path); err != nil {
344+
return totalSize, totalFiles, fmt.Errorf("failed to delete file %s: %v", files[i].path, err)
345+
}
346+
totalSize -= files[i].size
347+
totalFiles--
348+
}
349+
}
350+
}
351+
352+
return totalSize, totalFiles, nil
353+
}

zfile/file_test.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package zfile
22

33
import (
4+
"fmt"
45
"os"
56
"path/filepath"
67
"strings"
@@ -130,10 +131,7 @@ func TestPermissionDenied(t *testing.T) {
130131
dir := "./permission_denied"
131132

132133
os.Mkdir(dir, 0o000)
133-
defer func() {
134-
os.Chmod(dir, 777)
135-
Rmdir(dir)
136-
}()
134+
defer Rmdir(dir)
137135

138136
tt.Run("NotPermission", func(tt *TestUtil) {
139137
tt.EqualTrue(!HasReadWritePermission(dir))
@@ -156,3 +154,26 @@ func TestPermissionDenied(t *testing.T) {
156154
tt.Log(HasPermission("/", 0o664))
157155
})
158156
}
157+
158+
func TestGetDirSize(t *testing.T) {
159+
tt := NewTest(t)
160+
161+
tmp := RealPathMkdir("./tmp-size")
162+
defer Rmdir(tmp)
163+
164+
for i := 0; i < 20; i++ {
165+
WriteFile(filepath.Join(tmp, fmt.Sprintf("file-%d.txt", i)), []byte(strings.Repeat("a", (i+1)*8*KB)))
166+
}
167+
168+
size, total, err := StatDir(tmp)
169+
tt.NoError(err)
170+
tt.EqualTrue(size > 1*MB)
171+
tt.EqualTrue(total == 20)
172+
tt.Log(SizeFormat(size), size)
173+
174+
size, total, err = StatDir(tmp, DirStatOptions{MaxSize: 1 * MB, MaxTotal: 3})
175+
tt.NoError(err)
176+
tt.EqualTrue(size < 1*MB)
177+
tt.EqualTrue(total == 3)
178+
tt.Log(SizeFormat(size), size)
179+
}

znet/valid.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ func (c *Context) Valid(defRule zvalid.Engine, key string, name ...string) zvali
4343
return valid(defRule, value, key, name...)
4444
}
4545

46-
func valid(defRule zvalid.Engine, value, key string, name ...string) (valid zvalid.Engine) {
46+
func valid(defRule zvalid.Engine, value, _ string, name ...string) (valid zvalid.Engine) {
4747
return defRule.Verifi(value, name...)
4848
}

znet/web_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,7 @@ func TestMethodAndName(t *testing.T) {
872872
r := New("TestMethodAndName")
873873
r.SetMode(DebugMode)
874874
g := r.Group("/TestMethodAndName")
875-
h := func(s string) func(c *Context) {
875+
h := func(_ string) func(c *Context) {
876876
return func(c *Context) {
877877
c.String(200, c.GetParam("id"))
878878
}

0 commit comments

Comments
 (0)