grokify/mogo

View on GitHub
os/osutil/copy.go

Summary

Maintainability
A
40 mins
Test Coverage
package osutil

import (
    "io"
    "os"
)

func CopyFile(src, dst string) error {
    r, err := os.Open(src)
    if err != nil {
        return err
    }
    defer r.Close()

    w, err := os.Create(dst)
    if err != nil {
        return err
    }
    defer func() {
        if e := w.Close(); e != nil {
            err = e
        }
    }()

    _, err = io.Copy(w, r)
    if err != nil {
        return err
    }

    err = w.Sync()
    if err != nil {
        return err
    }

    si, err := os.Stat(src)
    if err != nil {
        return err
    }
    return os.Chmod(dst, si.Mode())
}