github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/util/legacy.go (about) 1 // Copyright 2023 The GitBundle Inc. All rights reserved. 2 // Copyright 2017 The Gitea Authors. All rights reserved. 3 // Use of this source code is governed by a MIT-style 4 // license that can be found in the LICENSE file. 5 6 package util 7 8 import ( 9 "io" 10 "os" 11 ) 12 13 // CopyFile copies file from source to target path. 14 func CopyFile(src, dest string) error { 15 si, err := os.Lstat(src) 16 if err != nil { 17 return err 18 } 19 20 sr, err := os.Open(src) 21 if err != nil { 22 return err 23 } 24 defer sr.Close() 25 26 dw, err := os.Create(dest) 27 if err != nil { 28 return err 29 } 30 defer dw.Close() 31 32 if _, err = io.Copy(dw, sr); err != nil { 33 return err 34 } 35 36 if err = os.Chtimes(dest, si.ModTime(), si.ModTime()); err != nil { 37 return err 38 } 39 return os.Chmod(dest, si.Mode()) 40 }