-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from sudo-bmitch/pr-mem-init
[WIP] Refactoring to enable memory store to be backed by dir
- Loading branch information
Showing
7 changed files
with
853 additions
and
302 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Package copy is used internally to recursively copy filesystem content. | ||
package copy | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
// Copy copies the content of src to dst. | ||
func Copy(dest, src string) error { | ||
dest = filepath.Clean(dest) | ||
src = filepath.Clean(src) | ||
return filepath.Walk(src, func(srcCur string, fi fs.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
destCur := filepath.Join(dest, strings.TrimPrefix(srcCur, src)) | ||
// handle directory | ||
if fi.IsDir() { | ||
err = os.MkdirAll(destCur, fi.Mode()) | ||
return err | ||
} | ||
// handle links and other files | ||
if !fi.Mode().IsRegular() { | ||
switch fi.Mode().Type() & os.ModeType { | ||
case os.ModeSymlink: | ||
link, err := os.Readlink(srcCur) | ||
if err != nil { | ||
return err | ||
} | ||
return os.Symlink(link, destCur) | ||
default: | ||
return fmt.Errorf("unsupported file to copy: %s, type = %d", srcCur, fi.Mode().Type()) | ||
} | ||
} | ||
// copy file | ||
//#nosec G304 copy is only used for internal (test) code. | ||
fhSrc, err := os.Open(srcCur) | ||
if err != nil { | ||
return err | ||
} | ||
defer fhSrc.Close() | ||
//#nosec G304 copy is only used for internal (test) code. | ||
fhDest, err := os.Create(destCur) | ||
if err != nil { | ||
return err | ||
} | ||
defer fhDest.Close() | ||
err = fhDest.Chmod(fi.Mode()) | ||
if err != nil { | ||
return err | ||
} | ||
_, err = io.Copy(fhDest, fhSrc) | ||
return err | ||
}) | ||
} |
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
Oops, something went wrong.