Files
dolt/tools/file/file.go
Erik Arvidsson 5edf89cf3d Replace d.Chk.True with d.PanicIfFalse (#2563)
And same for d.Chk.False
2016-09-14 13:11:28 -07:00

40 lines
1.0 KiB
Go

// Copyright 2016 Attic Labs, Inc. All rights reserved.
// Licensed under the Apache License, version 2.0:
// http://www.apache.org/licenses/LICENSE-2.0
package file
import (
"io"
"os"
"path/filepath"
"runtime"
"github.com/attic-labs/noms/go/d"
)
// DumbCopy copies the contents of a regular file at srcPath (following symlinks) to a new regular file at dstPath. New file is created with same mode.
func DumbCopy(srcPath, dstPath string) {
chkClose := func(c io.Closer) { d.PanicIfError(c.Close()) }
info, err := os.Stat(srcPath)
d.PanicIfError(err)
src, err := os.Open(srcPath)
d.PanicIfError(err)
defer chkClose(src)
dst, err := os.OpenFile(dstPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, info.Mode())
d.PanicIfError(err)
defer chkClose(dst)
_, err = io.Copy(dst, src)
d.PanicIfError(err)
}
// MyDir returns the directory in which the file containing the calling source code resides.
func MyDir() string {
_, path, _, ok := runtime.Caller(1)
d.PanicIfFalse(ok, "Should have been able to get Caller.")
return filepath.Dir(path)
}