2017-10-20 15:25:48 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2017-10-21 02:26:04 +03:00
|
|
|
"math"
|
2018-06-23 19:18:35 +03:00
|
|
|
math_rand "math/rand"
|
2018-04-22 15:16:16 +03:00
|
|
|
"net"
|
2017-10-20 15:25:48 +03:00
|
|
|
"os"
|
2017-10-21 02:26:04 +03:00
|
|
|
"strconv"
|
2018-06-23 19:18:35 +03:00
|
|
|
"time"
|
2017-10-21 23:34:59 +03:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2018-06-27 06:40:16 +03:00
|
|
|
"github.com/schollz/bytetoword"
|
2017-10-20 15:25:48 +03:00
|
|
|
)
|
|
|
|
|
2018-06-27 06:40:16 +03:00
|
|
|
// HashWords returns word after hashing
|
|
|
|
func HashWords(s string) string {
|
|
|
|
hasher := md5.New()
|
|
|
|
hasher.Write([]byte(s))
|
|
|
|
return bytetoword.EncodeToString(hasher.Sum(nil))
|
|
|
|
}
|
|
|
|
|
2017-10-24 12:12:33 +03:00
|
|
|
// CatFiles copies data from n files to a single one and removes source files
|
|
|
|
// if Debug mode is set to false
|
|
|
|
func CatFiles(files []string, outfile string, remove bool) error {
|
2017-10-21 03:49:19 +03:00
|
|
|
finished, err := os.Create(outfile)
|
|
|
|
if err != nil {
|
2017-10-21 23:34:59 +03:00
|
|
|
return errors.Wrap(err, "CatFiles create: ")
|
2017-10-21 03:49:19 +03:00
|
|
|
}
|
2017-10-24 12:12:33 +03:00
|
|
|
defer finished.Close()
|
|
|
|
for _, file := range files {
|
|
|
|
fh, err := os.Open(file)
|
2017-10-21 03:49:19 +03:00
|
|
|
if err != nil {
|
2017-10-24 12:12:33 +03:00
|
|
|
return errors.Wrap(err, fmt.Sprintf("CatFiles open %v: ", file))
|
2017-10-21 03:49:19 +03:00
|
|
|
}
|
|
|
|
_, err = io.Copy(finished, fh)
|
|
|
|
if err != nil {
|
2017-10-21 23:34:59 +03:00
|
|
|
return errors.Wrap(err, "CatFiles copy: ")
|
2017-10-21 03:49:19 +03:00
|
|
|
}
|
2017-10-24 16:57:32 +03:00
|
|
|
fh.Close()
|
2017-10-24 12:12:33 +03:00
|
|
|
if remove {
|
|
|
|
os.Remove(file)
|
2017-10-21 03:49:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-24 12:56:50 +03:00
|
|
|
// SplitFile creates a bunch of smaller files with the data from source splited into them
|
2017-10-21 02:26:04 +03:00
|
|
|
func SplitFile(fileName string, numPieces int) (err error) {
|
|
|
|
file, err := os.Open(fileName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
fi, err := file.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
bytesPerPiece := int(math.Ceil(float64(fi.Size()) / float64(numPieces)))
|
2018-02-07 11:24:44 +03:00
|
|
|
|
|
|
|
buf := make([]byte, bytesPerPiece)
|
|
|
|
for i := 0; i < numPieces; i++ {
|
|
|
|
|
|
|
|
out, err := os.Create(fileName + "." + strconv.Itoa(i))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-10-21 02:26:04 +03:00
|
|
|
n, err := file.Read(buf)
|
|
|
|
out.Write(buf[:n])
|
2018-02-07 11:24:44 +03:00
|
|
|
out.Close()
|
2017-10-24 12:56:50 +03:00
|
|
|
|
2018-02-07 11:24:44 +03:00
|
|
|
if err == io.EOF {
|
|
|
|
break
|
2017-10-21 02:26:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-20 15:25:48 +03:00
|
|
|
// CopyFile copies a file from src to dst. If src and dst files exist, and are
|
|
|
|
// the same, then return success. Otherise, attempt to create a hard link
|
|
|
|
// between the two files. If that fail, copy the file contents from src to dst.
|
|
|
|
func CopyFile(src, dst string) (err error) {
|
|
|
|
sfi, err := os.Stat(src)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !sfi.Mode().IsRegular() {
|
|
|
|
// cannot copy non-regular files (e.g., directories,
|
|
|
|
// symlinks, devices, etc.)
|
|
|
|
return fmt.Errorf("CopyFile: non-regular source file %s (%q)", sfi.Name(), sfi.Mode().String())
|
|
|
|
}
|
|
|
|
dfi, err := os.Stat(dst)
|
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !(dfi.Mode().IsRegular()) {
|
|
|
|
return fmt.Errorf("CopyFile: non-regular destination file %s (%q)", dfi.Name(), dfi.Mode().String())
|
|
|
|
}
|
|
|
|
if os.SameFile(sfi, dfi) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err = os.Link(src, dst); err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = copyFileContents(src, dst)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// copyFileContents 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.
|
|
|
|
func copyFileContents(src, dst string) (err error) {
|
|
|
|
in, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer in.Close()
|
|
|
|
out, err := os.Create(dst)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
cerr := out.Close()
|
|
|
|
if err == nil {
|
|
|
|
err = cerr
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if _, err = io.Copy(out, in); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = out.Sync()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// HashFile does a md5 hash on the file
|
|
|
|
// from https://golang.org/pkg/crypto/md5/#example_New_file
|
|
|
|
func HashFile(filename string) (hash string, err error) {
|
|
|
|
f, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
h := md5.New()
|
|
|
|
if _, err = io.Copy(h, f); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
hash = fmt.Sprintf("%x", h.Sum(nil))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// FileSize returns the size of a file
|
|
|
|
func FileSize(filename string) (int, error) {
|
2017-10-25 15:03:36 +03:00
|
|
|
fi, err := os.Stat(filename)
|
2017-10-20 15:25:48 +03:00
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
size := int(fi.Size())
|
|
|
|
return size, nil
|
|
|
|
}
|
2018-04-22 15:16:16 +03:00
|
|
|
|
|
|
|
// GetLocalIP returns the local ip address
|
|
|
|
func GetLocalIP() string {
|
|
|
|
addrs, err := net.InterfaceAddrs()
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
bestIP := ""
|
|
|
|
for _, address := range addrs {
|
|
|
|
// check the address type and if it is not a loopback the display it
|
|
|
|
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
|
|
|
|
if ipnet.IP.To4() != nil {
|
|
|
|
return ipnet.IP.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bestIP
|
|
|
|
}
|
2018-06-23 19:18:35 +03:00
|
|
|
|
|
|
|
// src is seeds the random generator for generating random strings
|
|
|
|
var src = math_rand.NewSource(time.Now().UnixNano())
|
|
|
|
|
|
|
|
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
const (
|
|
|
|
letterIdxBits = 6 // 6 bits to represent a letter index
|
|
|
|
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
|
|
|
|
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
|
|
|
|
)
|
|
|
|
|
|
|
|
// RandStringBytesMaskImprSrc prints a random string
|
|
|
|
func RandStringBytesMaskImprSrc(n int) string {
|
|
|
|
b := make([]byte, n)
|
|
|
|
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
|
|
|
|
for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
|
|
|
|
if remain == 0 {
|
|
|
|
cache, remain = src.Int63(), letterIdxMax
|
|
|
|
}
|
|
|
|
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
|
|
|
|
b[i] = letterBytes[idx]
|
|
|
|
i--
|
|
|
|
}
|
|
|
|
cache >>= letterIdxBits
|
|
|
|
remain--
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(b)
|
|
|
|
}
|