2023-09-16 15:25:25 +00:00
|
|
|
//
|
|
|
|
// Copyright ⓒ 2023 Chakib Ben Ziane <contact@blob42.xyz> and [`GoSuki` contributors]
|
|
|
|
// (https://github.com/blob42/gosuki/graphs/contributors).
|
|
|
|
//
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
//
|
|
|
|
// This file is part of GoSuki.
|
|
|
|
//
|
|
|
|
// GoSuki is free software: you can redistribute it and/or modify it under the terms of
|
|
|
|
// the GNU Affero General Public License as published by the Free Software Foundation,
|
|
|
|
// either version 3 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// GoSuki 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 Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License along with
|
|
|
|
// gosuki. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2018-12-04 03:34:30 +00:00
|
|
|
package utils
|
2020-09-09 13:07:29 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2022-10-27 22:33:20 +00:00
|
|
|
|
2023-10-08 12:41:04 +00:00
|
|
|
"github.com/blob42/gosuki/internal/logging"
|
2020-09-09 13:07:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
TMPDIR = ""
|
2022-10-27 22:33:20 +00:00
|
|
|
log = logging.GetLogger("")
|
2020-09-09 13:07:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func copyFileToDst(src string, dst string) error {
|
|
|
|
srcFile, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
dstFile, err := os.OpenFile(dst, os.O_RDWR|os.O_CREATE, 0755)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.Copy(dstFile, srcFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-27 22:33:20 +00:00
|
|
|
err = dstFile.Sync()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-28 20:08:21 +00:00
|
|
|
|
2020-09-09 13:07:29 +00:00
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy files from src glob to dst folder
|
2022-12-27 12:24:41 +00:00
|
|
|
func CopyFilesToTmpFolder(srcglob string, dst string) error {
|
2020-09-09 13:07:29 +00:00
|
|
|
matches, err := filepath.Glob(os.ExpandEnv(srcglob))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range matches {
|
2022-12-27 12:24:41 +00:00
|
|
|
dstFile := path.Join(dst, path.Base(v))
|
2020-09-09 13:07:29 +00:00
|
|
|
err = copyFileToDst(v, dstFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-09-15 01:17:12 +00:00
|
|
|
//FIX: this is not always working as expected
|
|
|
|
//TEST:
|
2020-09-09 13:07:29 +00:00
|
|
|
func CleanFiles() {
|
|
|
|
log.Debugf("Cleaning files <%s>", TMPDIR)
|
|
|
|
err := os.RemoveAll(TMPDIR)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
var err error
|
2023-02-18 23:13:23 +00:00
|
|
|
TMPDIR, err = ioutil.TempDir("", "gosuki*")
|
2020-09-09 13:07:29 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|