Skip to content
This repository has been archived by the owner on Jan 11, 2021. It is now read-only.

Commit

Permalink
folder 정보 식별 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
심승용 committed May 17, 2020
1 parent 9624eb6 commit 81d99cf
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions beater/lsbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package beater
import (
"fmt"
"io/ioutil"
"os"
s "strings"
"time"

Expand Down Expand Up @@ -60,7 +61,7 @@ func (bt *Lsbeat) Run(b *beat.Beat) error {
continue
}
path = s.Replace(path, "\\", "/", -1)
listDir(path, bt, b)
listDir(path, bt, b, 1)
}

select {
Expand All @@ -77,8 +78,8 @@ func (bt *Lsbeat) Stop() {
close(bt.done)
}

func listDir(dirName string, bt *Lsbeat, b *beat.Beat) (int64, int, int) {
files, err := ioutil.ReadDir(dirName)
func listDir(dirName string, bt *Lsbeat, b *beat.Beat, depth int) (int64, int, int) {
curDir, err := os.Stat(dirName)

if err != nil {
event := beat.Event{
Expand All @@ -93,6 +94,18 @@ func listDir(dirName string, bt *Lsbeat, b *beat.Beat) (int64, int, int) {
return 0, 0, 0
}

if depth == 0 {
return curDir.Size(), 0, 0
}

folderinfo := common.MapStr{
"name": curDir.Name(),
"size": curDir.Size(),
"modTime": curDir.ModTime(),
}

files, _ := ioutil.ReadDir(dirName)

fileinfos := []common.MapStr{}

dirSize := int64(0)
Expand All @@ -105,17 +118,17 @@ func listDir(dirName string, bt *Lsbeat, b *beat.Beat) (int64, int, int) {
for _, f := range files {
if f.IsDir() {
subfoldercount++
da, fa, sfa := listDir(dirName+"/"+f.Name(), bt, b)
da, fa, sfa := listDir(dirName+"/"+f.Name(), bt, b, depth-1)
dirSizeAcc += da
filecountAcc += fa
subfoldercountAcc += sfa
} else {
dirSize += f.Size()

fileinfos = append(fileinfos, common.MapStr{
"fileName": f.Name(),
"fileSize": f.Size(),
"modTime": f.ModTime(),
"name": f.Name(),
"size": f.Size(),
"modTime": f.ModTime(),
})
filecount++
}
Expand All @@ -128,11 +141,12 @@ func listDir(dirName string, bt *Lsbeat, b *beat.Beat) (int64, int, int) {
eventDir := beat.Event{
Timestamp: time.Now(),
Fields: common.MapStr{
"folder": folderinfo,
"dirName": dirName,
"dirSizeOld": dirSize,
"dirSizeAccumulate": dirSizeAcc,
"filesCount": filecount,
"filesCountAccumulate": filecountAcc,
"dirSize": dirSize,
"dirSizeAccumulate": dirSizeAcc,
"subFolderCount": subfoldercount,
"subFolderCountAccumulate": subfoldercountAcc,
"isExist": true,
Expand Down

0 comments on commit 81d99cf

Please sign in to comment.