Files
gosqldump/internal/util/files.go

59 lines
1.1 KiB
Go
Raw Normal View History

package util
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
func MoveFile(src, dst string) error {
in, err := os.Open(src)
if err != nil {
return fmt.Errorf("couldn't open source file: %s", err)
}
out, err := os.Create(dst)
if err != nil {
in.Close()
return fmt.Errorf("couldn't open dest file: %s", err)
}
defer out.Close()
_, err = io.Copy(out, in)
in.Close()
if err != nil {
return fmt.Errorf("writing to output file failed: %s", err)
}
err = out.Sync()
if err != nil {
return fmt.Errorf("sync error: %s", err)
}
si, err := os.Stat(src)
if err != nil {
return fmt.Errorf("stat error: %s", err)
}
err = os.Chmod(dst, si.Mode())
if err != nil {
return fmt.Errorf("chmod error: %s", err)
}
//err = in.Close()
//if err != nil {
// return fmt.Errorf("closing file failed: %s", err)
//}
//time.Sleep(time.Second * 10)
err = os.Remove(src)
if err != nil {
return fmt.Errorf("failed removing original file: %s", err)
}
return nil
}
func FileNameWithoutExt(fileName string) string {
return strings.TrimSuffix(fileName, filepath.Ext(fileName))
}