blob: 0170c4f10eb5482fadfa84f6a7f592ca38f2d6e5 [file] [log] [blame]
// Copyright 2024 The Dawn & Tint Authors
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package fileutils
import (
"fmt"
"io"
"os"
"path/filepath"
)
// CopyFile copies the file from 'src' to 'dst' replacing the existing file at 'dst' if it already
// exists.
func CopyFile(dst, src string) error {
if !IsFile(src) {
return fmt.Errorf("'%v' is not a file", src)
}
dstDir := filepath.Dir(dst)
if !IsDir(dstDir) {
if err := os.MkdirAll(dstDir, 0777); err != nil {
return err
}
}
s, err := os.Open(src)
if err != nil {
return err
}
defer s.Close()
info, err := s.Stat()
if err != nil {
return err
}
d, err := os.OpenFile(dst, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666|info.Mode()&0777)
if err != nil {
return err
}
defer d.Close()
_, err = io.Copy(d, s)
return err
}
// CopyDir copies the directory and all its content from 'src' to 'dst' replacing the existing
// directory at 'dst' if it already exists.
func CopyDir(dst, src string) error {
if !IsDir(src) {
return fmt.Errorf("'%v' is not a directory", src)
}
if IsFile(dst) {
return fmt.Errorf("'%v' is a file", dst)
}
if IsDir(dst) {
if err := os.RemoveAll(dst); err != nil {
return err
}
}
return filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
rel, err := filepath.Rel(src, path)
if err != nil {
return err
}
if !info.IsDir() {
if err := CopyFile(filepath.Join(dst, rel), path); err != nil {
return err
}
}
return nil
})
}