fix: failed to bak file cause by linux file system case sensitivity #20

This commit is contained in:
BaoXuebin 2022-07-23 12:19:35 +08:00
parent 81738c09d9
commit 517cb25e73
1 changed files with 17 additions and 8 deletions

View File

@ -3,6 +3,7 @@ package script
import ( import (
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath"
) )
func FileIfExist(filePath string) bool { func FileIfExist(filePath string) bool {
@ -39,7 +40,10 @@ func WriteFile(filePath string, content string) error {
func AppendFileInNewLine(filePath string, content string) error { func AppendFileInNewLine(filePath string, content string) error {
content = "\r\n" + content content = "\r\n" + content
file, err := os.OpenFile(filePath, os.O_APPEND|os.O_WRONLY, os.ModeAppend) file, err := os.OpenFile(filePath, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
if err == nil { if err != nil {
LogSystemError("Failed to open file (" + filePath + ")")
return err
} else {
_, err = file.WriteString(content) _, err = file.WriteString(content)
if err != nil { if err != nil {
LogSystemError("Failed to append file (" + filePath + ")") LogSystemError("Failed to append file (" + filePath + ")")
@ -48,17 +52,22 @@ func AppendFileInNewLine(filePath string, content string) error {
} }
defer file.Close() defer file.Close()
LogSystemInfo("Success append file (" + filePath + ")") LogSystemInfo("Success append file (" + filePath + ")")
return nil return err
} }
func CreateFile(filePath string) error { func CreateFile(filePath string) error {
f, err := os.Create(filePath) if _, e := os.Stat(filePath); os.IsNotExist(e) {
if nil != err { _ = os.MkdirAll(filepath.Dir(filePath), os.ModePerm)
LogSystemError(filePath + " create failed") f, err := os.Create(filePath)
return err if nil != err {
LogSystemError(filePath + " create failed")
return err
}
defer f.Close()
LogSystemInfo("Success create file " + filePath)
} else {
LogSystemInfo("File is exist " + filePath)
} }
defer f.Close()
LogSystemInfo("Success create file " + filePath)
return nil return nil
} }