심볼링크 로그 파일은 빼고 삭제

This commit is contained in:
2023-11-24 01:52:52 +09:00
parent 1a404e5361
commit 84543f9a31

View File

@ -15,6 +15,7 @@ import (
"os/exec"
"path"
"path/filepath"
"sort"
"strings"
"syscall"
"time"
@ -77,7 +78,7 @@ func (hc *houstonClient) uploadZipLogFile(zipFile string, name string, version s
return nil
}
func zipLogFiles(storageRoot string, req *shared.UploadRequest, start, except string) (string, []string, error) {
func zipLogFiles(storageRoot string, req *shared.UploadRequest) (string, []string, error) {
root := path.Join(storageRoot, req.Name, req.Version)
matches, err := filepath.Glob(path.Join(root, req.Filter))
if err != nil {
@ -95,6 +96,7 @@ func zipLogFiles(storageRoot string, req *shared.UploadRequest, start, except st
root = path.Join(root, path.Dir(req.Filter))
zipFileName := path.Join(os.TempDir(), path.Base(matches[0])) + ".zip"
os.Remove(zipFileName)
f, err := os.OpenFile(zipFileName, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
if err != nil {
@ -110,13 +112,14 @@ func zipLogFiles(storageRoot string, req *shared.UploadRequest, start, except st
if file == root {
continue
}
if len(except) > 0 && file >= except {
matches = matches[:i]
break
}
if len(start) > 0 && file < start {
continue
if fi, err := os.Lstat(file); err == nil {
if (fi.Mode() & os.ModeSymlink) == os.ModeSymlink {
matches[i] = ""
continue
}
}
if len(oldestFile) == 0 {
oldestFile = path.Base(file)
}
@ -124,19 +127,16 @@ func zipLogFiles(storageRoot string, req *shared.UploadRequest, start, except st
relative := file[len(root)+1:]
fw, err := w.Create(relative)
if err != nil {
logger.Println(err)
return "", nil, err
}
src, err := os.Open(file)
if err != nil {
logger.Println(err)
return "", nil, err
}
defer src.Close()
if _, err = io.Copy(fw, src); err != nil {
logger.Println(err)
return "", nil, err
}
}
@ -496,12 +496,15 @@ func (hc *houstonClient) uploadFiles(req *shared.UploadRequest) error {
logger.Println("uploadFiles found :", child.version, child.name)
go func() {
zipFile, srcFiles, err := zipLogFiles(hc.config.StorageRoot, req, "", "")
zipFile, srcFiles, err := zipLogFiles(hc.config.StorageRoot, req)
if err == nil && len(zipFile) > 0 && len(srcFiles) > 0 {
if err = hc.uploadZipLogFile(zipFile, child.name, child.version); err == nil {
// 마지막거 빼고 삭제
sort.StringSlice(srcFiles).Sort()
for i := 0; i < len(srcFiles)-1; i++ {
os.Remove(srcFiles[i])
if len(srcFiles[i]) > 0 {
os.Remove(srcFiles[i])
}
}
} else {
logger.Println("uploadZipLogFile failed :", err)
@ -517,9 +520,17 @@ func (hc *houstonClient) uploadFiles(req *shared.UploadRequest) error {
// 실행 중이 아닌 폴더에서도 대상을 찾는다
// 전체 파일을 대상으로
zipFile, srcFiles, err := zipLogFiles(hc.config.StorageRoot, req, "", "")
zipFile, srcFiles, err := zipLogFiles(hc.config.StorageRoot, req)
if err == nil && len(zipFile) > 0 && len(srcFiles) > 0 {
if err = hc.uploadZipLogFile(zipFile, req.Name, req.Version); err != nil {
if err = hc.uploadZipLogFile(zipFile, req.Name, req.Version); err == nil {
// 마지막거 빼고 삭제
sort.StringSlice(srcFiles).Sort()
for i := 0; i < len(srcFiles)-1; i++ {
if len(srcFiles[i]) > 0 {
os.Remove(srcFiles[i])
}
}
} else {
logger.Println("uploadZipLogFile failed :", err)
}
} else if err != nil {