2013-02-20 23:43:01 -05:00
|
|
|
package util
|
2013-02-18 20:44:00 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
2013-08-13 23:12:08 -04:00
|
|
|
"syscall"
|
2013-02-18 20:44:00 -05:00
|
|
|
)
|
|
|
|
|
2013-02-20 23:43:01 -05:00
|
|
|
func EnsureDirExists(dir string) error {
|
2013-02-18 20:44:00 -05:00
|
|
|
_, err := os.Lstat(dir)
|
|
|
|
if err != nil {
|
|
|
|
fi, err := os.Lstat(path.Dir(dir))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = os.Mkdir(dir, fi.Mode().Perm())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-02-20 23:43:01 -05:00
|
|
|
func CopyToTmp(src string, tmpdir string) (string, error) {
|
2013-02-18 20:44:00 -05:00
|
|
|
infile, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer infile.Close()
|
|
|
|
|
|
|
|
outfile, err := ioutil.TempFile(tmpdir, "asink")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer outfile.Close()
|
|
|
|
|
|
|
|
_, err = io.Copy(outfile, infile)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return outfile.Name(), nil
|
|
|
|
}
|
2013-08-13 23:12:08 -04:00
|
|
|
|
|
|
|
func ErrorFileNotFound(err error) bool {
|
|
|
|
if e, ok := err.(*os.PathError); ok && e.Err == syscall.ENOENT {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|