Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep file permissions when copy directory #188

Merged
merged 4 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.20

require (
github.com/BurntSushi/toml v1.3.2
github.com/CycloneDX/cyclonedx-go v0.7.1
github.com/CycloneDX/cyclonedx-go v0.7.2
github.com/buger/jsonparser v1.1.1
github.com/jfrog/gofrog v1.3.0
github.com/minio/sha256-simd v1.0.1
Expand Down
5 changes: 3 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/CycloneDX/cyclonedx-go v0.7.1 h1:5w1SxjGm9MTMNTuRbEPyw21ObdbaagTWF/KfF0qHTRE=
github.com/CycloneDX/cyclonedx-go v0.7.1/go.mod h1:N/nrdWQI2SIjaACyyDs/u7+ddCkyl/zkNs8xFsHF2Ps=
github.com/CycloneDX/cyclonedx-go v0.7.2 h1:kKQ0t1dPOlugSIYVOMiMtFqeXI2wp/f5DBIdfux8gnQ=
github.com/CycloneDX/cyclonedx-go v0.7.2/go.mod h1:K2bA+324+Og0X84fA8HhN2X066K7Bxz4rpMQ4ZhjtSk=
github.com/bradleyjkemp/cupaloy/v2 v2.8.0 h1:any4BmKE+jGIaMpnU8YgH/I2LPiLBufr6oMMlVBbn9M=
github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs=
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
Expand All @@ -24,6 +24,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/terminalstatic/go-xsd-validate v0.1.5 h1:RqpJnf6HGE2CB/lZB1A8BYguk8uRtcvYAPLCF15qguo=
github.com/urfave/cli/v2 v2.25.7 h1:VAzn5oq403l5pHjc4OhD54+XGO9cdKVL/7lDjF+iKUs=
github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c=
Expand Down
48 changes: 22 additions & 26 deletions utils/fileutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"golang.org/x/exp/slices"
"io"
Expand Down Expand Up @@ -410,61 +411,56 @@ func CopyDir(fromPath, toPath string, includeDirs bool, excludeNames []string) e
return err
}

for _, v := range files {
for _, file := range files {
fileName := filepath.Base(file)
// Skip if excluded
if slices.Contains(excludeNames, filepath.Base(v)) {
if slices.Contains(excludeNames, fileName) {
continue
}

dir, err := IsDirExists(v, false)
var isDir bool
isDir, err = IsDirExists(file, false)
if err != nil {
return err
}

if dir {
toPath := toPath + GetFileSeparator() + filepath.Base(v)
err := CopyDir(v, toPath, true, nil)
if err != nil {
return err
}
continue
if isDir {
err = CopyDir(file, filepath.Join(toPath, fileName), true, nil)
} else {
err = CopyFile(toPath, file)
}
err = CopyFile(toPath, v)
if err != nil {
return err
}
}
return err
return nil
}

func CopyFile(dst, src string) (err error) {
srcFile, err := os.Open(src)
if err != nil {
return err
return
}
defer func() {
e := srcFile.Close()
if err == nil {
err = e
}
err = errors.Join(err, srcFile.Close())
}()
srcInfo, err := srcFile.Stat()
if err != nil {
return
}
fileName, _ := GetFileAndDirFromPath(src)
dstPath, err := CreateFilePath(dst, fileName)
if err != nil {
return err
return
}
dstFile, err := os.Create(dstPath)
dstFile, err := os.OpenFile(dstPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, srcInfo.Mode())
if err != nil {
return err
return
}
defer func() {
e := dstFile.Close()
if err == nil {
err = e
}
err = errors.Join(err, dstFile.Close())
}()
_, err = io.Copy(dstFile, srcFile)
return err
return
}

func GetFileSeparator() string {
Expand Down
Loading