@@ -105,7 +105,11 @@ func (u *DeviceService) Clean(req []dto.Clean) {
105105 case "tmp_backup" :
106106 dropFileOrDir (path .Join (global .Dir .LocalBackupDir , "tmp" ))
107107 case "unknown_backup" :
108- dropFileOrDir (item .Name )
108+ if strings .HasPrefix (item .Name , path .Join (global .Dir .LocalBackupDir , "log/website" )) {
109+ dropFileOrDir (item .Name )
110+ } else {
111+ dropFile (item .Name )
112+ }
109113
110114 case "rollback" :
111115 dropFileOrDir (path .Join (global .Dir .BaseDir , rollbackPath , "app" ))
@@ -182,6 +186,7 @@ func (u *DeviceService) Clean(req []dto.Clean) {
182186 }
183187 }
184188
189+ _ = cleanEmptyDirs (global .Dir .LocalBackupDir )
185190 _ = settingRepo .Update ("LastCleanTime" , time .Now ().Format (constant .DateTimeLayout ))
186191 _ = settingRepo .Update ("LastCleanSize" , fmt .Sprintf ("%v" , size ))
187192 _ = settingRepo .Update ("LastCleanData" , fmt .Sprintf ("%v" , len (req )))
@@ -317,6 +322,9 @@ func loadAgentPackage(fileOp fileUtils.FileOp) dto.CleanTree {
317322 itemSize , _ := file .Info ()
318323 itemName := file .Name ()
319324 isCurrentVersion := strings .HasPrefix (itemName , fmt .Sprintf ("1panel-agent_%s_" , global .CONF .Base .Version ))
325+ if isCurrentVersion {
326+ continue
327+ }
320328 itemTree .Size += uint64 (itemSize .Size ())
321329 itemTree .Children = append (itemTree .Children , dto.CleanTree {
322330 ID : uuid .NewString (),
@@ -326,7 +334,6 @@ func loadAgentPackage(fileOp fileUtils.FileOp) dto.CleanTree {
326334 IsCheck : ! isCurrentVersion ,
327335 IsRecommend : true ,
328336 Type : "agent" ,
329- IsDisabled : isCurrentVersion ,
330337 })
331338 }
332339 }
@@ -363,7 +370,7 @@ func loadBackupTree(fileOp fileUtils.FileOp) []dto.CleanTree {
363370 treeData = append (treeData , loadUnknownDbs (fileOp , recordMap ))
364371 treeData = append (treeData , loadUnknownWebsites (fileOp , recordMap ))
365372 treeData = append (treeData , loadUnknownSnapshot (fileOp ))
366- treeData = append (treeData , loadUnknownWebsiteLog (fileOp , recordMap ))
373+ treeData = append (treeData , loadUnknownWebsiteLog (fileOp ))
367374 return treeData
368375}
369376
@@ -390,7 +397,6 @@ func loadUnknownApps(fileOp fileUtils.FileOp, recordMap map[string][]string) dto
390397 Type : "unknown_backup" ,
391398 }
392399 _ = loadFileOrDirWithExclude (fileOp , 0 , backupPath , & treeData , excludePaths )
393- loadBackupIsCheck (& treeData )
394400 return treeData
395401}
396402func loadUnknownDbs (fileOp fileUtils.FileOp , recordMap map [string ][]string ) dto.CleanTree {
@@ -418,8 +424,6 @@ func loadUnknownDbs(fileOp fileUtils.FileOp, recordMap map[string][]string) dto.
418424 Type : "unknown_backup" ,
419425 }
420426 _ = loadFileOrDirWithExclude (fileOp , 0 , backupPath , & treeData , excludePaths )
421-
422- loadBackupIsCheck (& treeData )
423427 return treeData
424428}
425429func loadUnknownWebsites (fileOp fileUtils.FileOp , recordMap map [string ][]string ) dto.CleanTree {
@@ -445,7 +449,6 @@ func loadUnknownWebsites(fileOp fileUtils.FileOp, recordMap map[string][]string)
445449 Type : "unknown_backup" ,
446450 }
447451 _ = loadFileOrDirWithExclude (fileOp , 0 , backupPath , & treeData , excludePaths )
448- loadBackupIsCheck (& treeData )
449452 return treeData
450453}
451454func loadUnknownSnapshot (fileOp fileUtils.FileOp ) dto.CleanTree {
@@ -466,14 +469,16 @@ func loadUnknownSnapshot(fileOp fileUtils.FileOp) dto.CleanTree {
466469 entries , _ := os .ReadDir (backupPath )
467470 for _ , entry := range entries {
468471 childPath := filepath .Join (backupPath , entry .Name ())
472+ if isExactPathMatch (childPath , excludePaths ) {
473+ continue
474+ }
469475 childNode := dto.CleanTree {
470476 ID : uuid .NewString (),
471477 Label : entry .Name (),
472478 IsCheck : false ,
473479 IsRecommend : false ,
474480 Name : childPath ,
475481 Type : "unknown_backup" ,
476- IsDisabled : isExactPathMatch (childPath , excludePaths ),
477482 }
478483 if entry .IsDir () {
479484 itemSize , _ := fileOp .GetDirSize (childPath )
@@ -492,7 +497,7 @@ func loadUnknownSnapshot(fileOp fileUtils.FileOp) dto.CleanTree {
492497 return treeData
493498}
494499
495- func loadUnknownWebsiteLog (fileOp fileUtils.FileOp , recordMap map [ string ][] string ) dto.CleanTree {
500+ func loadUnknownWebsiteLog (fileOp fileUtils.FileOp ) dto.CleanTree {
496501 treeData := dto.CleanTree {
497502 ID : uuid .NewString (),
498503 Label : "unknown_website_log" ,
@@ -543,14 +548,16 @@ func loadFileOrDirWithExclude(fileOp fileUtils.FileOp, index uint, dir string, r
543548 }
544549 for _ , entry := range entries {
545550 childPath := filepath .Join (dir , entry .Name ())
551+ if isExactPathMatch (childPath , excludes ) {
552+ continue
553+ }
546554 childNode := dto.CleanTree {
547555 ID : uuid .NewString (),
548556 Label : entry .Name (),
549557 IsCheck : false ,
550558 IsRecommend : false ,
551559 Name : childPath ,
552560 Type : "unknown_backup" ,
553- IsDisabled : ! entry .IsDir () && isExactPathMatch (childPath , excludes ),
554561 }
555562 if entry .IsDir () {
556563 if index < 4 {
@@ -578,26 +585,6 @@ func loadFileOrDirWithExclude(fileOp fileUtils.FileOp, index uint, dir string, r
578585 return nil
579586}
580587
581- func loadBackupIsCheck (treeData * dto.CleanTree ) {
582- for i := 0 ; i < len (treeData .Children ); i ++ {
583- if len (treeData .Children [i ].Children ) == 0 {
584- treeData .Children [i ].IsCheck = true
585- treeData .Children [i ].IsRecommend = true
586- continue
587- }
588- if treeData .Label != "unknown_database" && treeData .Label != "unknown_app" {
589- continue
590- }
591- for j := 0 ; j < len (treeData .Children [i ].Children ); j ++ {
592- if len (treeData .Children [i ].Children [j ].Children ) == 0 {
593- treeData .Children [i ].Children [j ].IsCheck = true
594- treeData .Children [i ].Children [j ].IsRecommend = true
595- continue
596- }
597- }
598- }
599- }
600-
601588func isExactPathMatch (path string , excludePaths []string ) bool {
602589 cleanPath := filepath .Clean (path )
603590
@@ -909,6 +896,18 @@ func dropFileOrDir(itemPath string) {
909896 global .LOG .Errorf ("drop file %s failed, err %v" , itemPath , err )
910897 }
911898}
899+ func dropFile (itemPath string ) {
900+ info , err := os .Stat (itemPath )
901+ if err != nil {
902+ return
903+ }
904+ if info .IsDir () {
905+ return
906+ }
907+ if err := os .Remove (itemPath ); err != nil {
908+ global .LOG .Errorf ("drop file %s failed, err %v" , itemPath , err )
909+ }
910+ }
912911
913912func dropBuildCache () (int , int ) {
914913 client , err := docker .NewDockerClient ()
@@ -1107,3 +1106,36 @@ func scanFile(pathItem string, size *int64, count *int) {
11071106 }
11081107 }
11091108}
1109+
1110+ func cleanEmptyDirs (root string ) error {
1111+ dirsToCheck := make ([]string , 0 )
1112+ err := filepath .WalkDir (root , func (path string , d os.DirEntry , err error ) error {
1113+ if err != nil {
1114+ return nil
1115+ }
1116+ if d .IsDir () {
1117+ dirsToCheck = append (dirsToCheck , path )
1118+ }
1119+ return nil
1120+ })
1121+ if err != nil {
1122+ return err
1123+ }
1124+
1125+ for i := len (dirsToCheck ) - 1 ; i >= 0 ; i -- {
1126+ dir := dirsToCheck [i ]
1127+ if dir == root {
1128+ continue
1129+ }
1130+
1131+ entries , err := os .ReadDir (dir )
1132+ if err != nil {
1133+ continue
1134+ }
1135+
1136+ if len (entries ) == 0 {
1137+ _ = os .Remove (dir )
1138+ }
1139+ }
1140+ return nil
1141+ }
0 commit comments