Skip to content

Commit

Permalink
update:优化运行时内存占用(由1g到10m)
Browse files Browse the repository at this point in the history
  • Loading branch information
0990 committed Feb 29, 2020
1 parent 434752d commit eeda47b
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 124 deletions.
56 changes: 16 additions & 40 deletions cleaner/cleaner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@ import (
"github.com/sirupsen/logrus"
"io/ioutil"
"os"
"path"
"strings"
"sync/atomic"
"syscall"
"time"
"unicode/utf16"
"unsafe"
)

const BACKUP_DIR_NAME = "被删除的文件"

var allDelCount int32
var allFileCount int32
var allDirCount int32
var ignoreCount int32

func DoClean() {
t := time.Now()
Expand All @@ -27,20 +26,20 @@ func DoClean() {
logrus.Infof("总耗时%v", time.Since(t))
}

func dirWalk(path string) {
if strings.Contains(path, BACKUP_DIR_NAME) {
func dirWalk(dirPath string) {
if strings.Contains(dirPath, BACKUP_DIR_NAME) {
return
}
log := logrus.WithField("目录", path)
hidden, err := isFileHidden(path)
log := logrus.WithField("目录", dirPath)
hidden, err := isFileHidden(dirPath)
if err != nil {
log.WithError(err).Info("isFileHidden")
}
if hidden {
return
}

fs, err := ioutil.ReadDir(path)
fs, err := ioutil.ReadDir(dirPath)
if err != nil {
logrus.Panic(err)
}
Expand All @@ -49,10 +48,14 @@ func dirWalk(path string) {
hash2files := make(map[string][]os.FileInfo, 0)
for _, file := range fs {
if file.IsDir() {
dirWalk(path + file.Name() + "/")
dirWalk(dirPath + file.Name() + "/")
} else {
name := path + file.Name()
md5, err := MD5File(name)
if path.Ext(file.Name()) == ".log" {
atomic.AddInt32(&ignoreCount, 1)
continue
}
name := dirPath + file.Name()
md5, err := md5File(name)
if err != nil {
logrus.Panic(err)
}
Expand Down Expand Up @@ -81,9 +84,9 @@ func dirWalk(path string) {
log.WithField("filename", file.Name()).Info("保留")
continue
}
backupDir := "./" + BACKUP_DIR_NAME + "/" + path[2:]
backupDir := "./" + BACKUP_DIR_NAME + "/" + dirPath[2:]
createDirIfNoExist(backupDir)
err = os.Rename(path+file.Name(), backupDir+file.Name())
err = os.Rename(dirPath+file.Name(), backupDir+file.Name())
if err != nil {
logrus.Panic(err)
}
Expand All @@ -97,30 +100,3 @@ func dirWalk(path string) {
log.Infof("%d个文件被移除", delCount)
}
}

func createDirIfNoExist(path string) {
_, err := os.Stat(path) //os.Stat获取文件信息
if err != nil {
if os.IsNotExist(err) {
os.MkdirAll(path, os.ModePerm) // Everyone can read write and execute
return
}
return
}
}

func isFileHidden(path string) (bool, error) {

name := utf16.Encode([]rune(path + "\x00"))

attributes, err := syscall.GetFileAttributes((*uint16)(unsafe.Pointer(&name[0])))

if err != nil {

return false, err

}

return attributes&syscall.FILE_ATTRIBUTE_HIDDEN != 0, nil

}
84 changes: 0 additions & 84 deletions cleaner/md5.go

This file was deleted.

49 changes: 49 additions & 0 deletions cleaner/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package cleaner

import (
"crypto/md5"
"fmt"
"io"
"os"
"syscall"
"unicode/utf16"
"unsafe"
)

func createDirIfNoExist(path string) {
_, err := os.Stat(path) //os.Stat获取文件信息
if err != nil {
if os.IsNotExist(err) {
os.MkdirAll(path, os.ModePerm) // Everyone can read write and execute
return
}
return
}
}

func isFileHidden(path string) (bool, error) {

name := utf16.Encode([]rune(path + "\x00"))

attributes, err := syscall.GetFileAttributes((*uint16)(unsafe.Pointer(&name[0])))

if err != nil {

return false, err

}

return attributes&syscall.FILE_ATTRIBUTE_HIDDEN != 0, nil

}

func md5File(filename string) (string, error) {
file, err := os.Open(filename)
defer file.Close()
if err != nil {
return "", err
}
md5h := md5.New()
io.Copy(md5h, file)
return fmt.Sprintf("%x", md5h.Sum([]byte(""))), nil
}
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ package main
import (
"github.com/0990/momentscleaner/cleaner"
"github.com/0990/momentscleaner/logconfig"
_ "net/http/pprof"
)

func main() {
//go func() {
// fmt.Println(http.ListenAndServe("localhost:8888", nil))
//}()

logconfig.InitLogrus("cleaner", 10)
cleaner.DoClean()
}

0 comments on commit eeda47b

Please # to comment.