-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added io & fix dependabot issue - CVE-2022-32149
- Loading branch information
Showing
3 changed files
with
83 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package io | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"io" | ||
) | ||
|
||
|
||
func CopyDir(src string, dest string) error { | ||
// Get properties of source directory | ||
srcInfo, err := os.Stat(src) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Create the destination directory | ||
err = os.MkdirAll(dest, srcInfo.Mode()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
directory, _ := os.Open(src) | ||
objects, err := directory.Readdir(-1) | ||
|
||
for _, obj := range objects { | ||
srcFilePointer := filepath.Join(src, obj.Name()) | ||
destFilePointer := filepath.Join(dest, obj.Name()) | ||
|
||
if obj.IsDir() { | ||
// Create sub-directories, recursively. | ||
err = CopyDir(srcFilePointer, destFilePointer) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
// Perform the file copy. | ||
err = CopyFile(srcFilePointer, destFilePointer) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func CopyFile(src, dst string) error { | ||
var err error | ||
var srcfd *os.File | ||
var dstfd *os.File | ||
var srcinfo os.FileInfo | ||
|
||
if srcfd, err = os.Open(src); err != nil { | ||
return err | ||
} | ||
defer srcfd.Close() | ||
|
||
if dstfd, err = os.Create(dst); err != nil { | ||
return err | ||
} | ||
defer dstfd.Close() | ||
|
||
if _, err = io.Copy(dstfd, srcfd); err != nil { | ||
return err | ||
} | ||
if srcinfo, err = os.Stat(src); err != nil { | ||
return err | ||
} | ||
return os.Chmod(dst, srcinfo.Mode()) | ||
} |