mirror of https://github.com/jetkvm/kvm.git
fix: symlink handling
This commit is contained in:
parent
ae77887ab2
commit
aee1a01cee
26
cmd/main.go
26
cmd/main.go
|
|
@ -117,31 +117,31 @@ func supervise() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func isSymlinkTo(dst, src string) bool {
|
func isSymlinkTo(oldName, newName string) bool {
|
||||||
file, err := os.Stat(dst)
|
file, err := os.Stat(newName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if file.Mode()&os.ModeSymlink != os.ModeSymlink {
|
if file.Mode()&os.ModeSymlink != os.ModeSymlink {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
target, err := os.Readlink(dst)
|
target, err := os.Readlink(newName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return target == src
|
return target == oldName
|
||||||
}
|
}
|
||||||
|
|
||||||
func ensureSymlink(dst, src string) error {
|
func ensureSymlink(oldName, newName string) error {
|
||||||
if isSymlinkTo(dst, src) {
|
if isSymlinkTo(oldName, newName) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
_ = os.Remove(dst)
|
_ = os.Remove(newName)
|
||||||
return os.Symlink(dst, src)
|
return os.Symlink(oldName, newName)
|
||||||
}
|
}
|
||||||
|
|
||||||
func renameFile(f *os.File, newName string) error {
|
func renameFile(f *os.File, newName string) error {
|
||||||
f.Close()
|
_ = f.Close()
|
||||||
|
|
||||||
// try to rename the file first
|
// try to rename the file first
|
||||||
if err := os.Rename(f.Name(), newName); err == nil {
|
if err := os.Rename(f.Name(), newName); err == nil {
|
||||||
|
|
@ -180,6 +180,8 @@ func renameFile(f *os.File, newName string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func createErrorDump(logFile *os.File) {
|
func createErrorDump(logFile *os.File) {
|
||||||
|
fmt.Println()
|
||||||
|
|
||||||
fileName := fmt.Sprintf(
|
fileName := fmt.Sprintf(
|
||||||
errorDumpTemplate,
|
errorDumpTemplate,
|
||||||
time.Now().Format("20060102-150405"),
|
time.Now().Format("20060102-150405"),
|
||||||
|
|
@ -191,9 +193,11 @@ func createErrorDump(logFile *os.File) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("error dump copied: %s\n", fileName)
|
fmt.Printf("error dump copied: %s\n", filePath)
|
||||||
|
|
||||||
if err := ensureSymlink(filePath, filepath.Join(errorDumpDir, errorDumpLastFile)); err != nil {
|
lastFilePath := filepath.Join(errorDumpDir, errorDumpLastFile)
|
||||||
|
|
||||||
|
if err := ensureSymlink(filePath, lastFilePath); err != nil {
|
||||||
fmt.Printf("failed to create symlink: %v\n", err)
|
fmt.Printf("failed to create symlink: %v\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue