git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/filex/filex.go (about) 1 package filex 2 3 import ( 4 "errors" 5 "fmt" 6 "io" 7 "os" 8 ) 9 10 // Exists returns true if the file exists or false otherwise 11 func Exists(file string) (ret bool, err error) { 12 _, err = os.Stat(file) 13 14 if err == nil { 15 ret = true 16 } else if errors.Is(err, os.ErrNotExist) { 17 ret = false 18 err = nil 19 } 20 21 return 22 } 23 24 // Copy the src file to dst 25 func Copy(dst, src string) (err error) { 26 sourceFileInfo, err := os.Stat(src) 27 if err != nil { 28 return 29 } 30 31 if !sourceFileInfo.Mode().IsRegular() { 32 err = fmt.Errorf("%s is not a regular file", src) 33 return 34 } 35 36 source, err := os.Open(src) 37 if err != nil { 38 return 39 } 40 defer source.Close() 41 42 destination, err := os.Create(dst) 43 if err != nil { 44 return 45 } 46 defer destination.Close() 47 48 _, err = io.Copy(destination, source) 49 return 50 }