2019-11-04 16:16:13 +00:00
|
|
|
// Copyright © 2019 Ettore Di Giacinto <mudler@gentoo.org>
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation; either version 2 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package helpers
|
|
|
|
|
2019-11-05 16:36:22 +00:00
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2019-11-10 09:48:07 +00:00
|
|
|
"path/filepath"
|
2020-04-13 17:28:52 +00:00
|
|
|
"time"
|
2019-11-23 18:13:32 +00:00
|
|
|
|
|
|
|
copy "github.com/otiai10/copy"
|
2019-11-05 16:36:22 +00:00
|
|
|
)
|
2019-11-04 16:16:13 +00:00
|
|
|
|
2020-03-28 15:55:16 +00:00
|
|
|
func ListDir(dir string) ([]string, error) {
|
|
|
|
content := []string{}
|
|
|
|
|
|
|
|
err := filepath.Walk(dir,
|
|
|
|
func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
content = append(content, path)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return content, err
|
|
|
|
}
|
|
|
|
|
2020-04-13 17:28:52 +00:00
|
|
|
// Touch creates an empty file
|
|
|
|
func Touch(f string) error {
|
|
|
|
_, err := os.Stat(f)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
file, err := os.Create(f)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
} else {
|
|
|
|
currentTime := time.Now().Local()
|
|
|
|
err = os.Chtimes(f, currentTime, currentTime)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-04 16:16:13 +00:00
|
|
|
// Exists reports whether the named file or directory exists.
|
|
|
|
func Exists(name string) bool {
|
|
|
|
if _, err := os.Stat(name); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2019-11-05 16:36:22 +00:00
|
|
|
|
|
|
|
func Read(file string) (string, error) {
|
|
|
|
dat, err := ioutil.ReadFile(file)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(dat), nil
|
|
|
|
}
|
2019-11-10 09:48:07 +00:00
|
|
|
|
2019-11-13 16:07:09 +00:00
|
|
|
func ensureDir(fileName string) {
|
|
|
|
dirName := filepath.Dir(fileName)
|
|
|
|
if _, serr := os.Stat(dirName); serr != nil {
|
|
|
|
merr := os.MkdirAll(dirName, os.ModePerm) // FIXME: It should preserve permissions from src to dst instead
|
|
|
|
if merr != nil {
|
|
|
|
panic(merr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-10 09:48:07 +00:00
|
|
|
// CopyFile copies the contents of the file named src to the file named
|
|
|
|
// by dst. The file will be created if it does not already exist. If the
|
|
|
|
// destination file exists, all it's contents will be replaced by the contents
|
|
|
|
// of the source file. The file mode will be copied from the source and
|
|
|
|
// the copied data is synced/flushed to stable storage.
|
|
|
|
func CopyFile(src, dst string) (err error) {
|
2020-10-19 15:58:43 +00:00
|
|
|
return copy.Copy(src, dst, copy.Options{OnSymlink: func(string) copy.SymlinkAction { return copy.Shallow }})
|
2019-11-10 09:48:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func IsDirectory(path string) (bool, error) {
|
|
|
|
fileInfo, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return fileInfo.IsDir(), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// CopyDir recursively copies a directory tree, attempting to preserve permissions.
|
|
|
|
// Source directory must exist, destination directory must *not* exist.
|
|
|
|
// Symlinks are ignored and skipped.
|
|
|
|
func CopyDir(src string, dst string) (err error) {
|
|
|
|
src = filepath.Clean(src)
|
|
|
|
dst = filepath.Clean(dst)
|
2020-10-19 15:58:43 +00:00
|
|
|
return copy.Copy(src, dst, copy.Options{OnSymlink: func(string) copy.SymlinkAction { return copy.Shallow }})
|
2019-11-10 09:48:07 +00:00
|
|
|
}
|