-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Foysal Iqbal
authored
Apr 9, 2020
1 parent
d94454b
commit 7cf2b00
Showing
4 changed files
with
90 additions
and
107 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 |
---|---|---|
@@ -1,111 +1,8 @@ | ||
package api | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"syscall" | ||
) | ||
import "github.com/otiai10/copy" | ||
|
||
func CopyDirectory(scrDir, dest string) error { | ||
entries, err := ioutil.ReadDir(scrDir) | ||
if err != nil { | ||
return err | ||
} | ||
for _, entry := range entries { | ||
sourcePath := filepath.Join(scrDir, entry.Name()) | ||
destPath := filepath.Join(dest, entry.Name()) | ||
|
||
fileInfo, err := os.Stat(sourcePath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
stat, ok := fileInfo.Sys().(*syscall.Stat_t) | ||
if !ok { | ||
return fmt.Errorf("failed to get raw syscall.Stat_t data for '%s'", sourcePath) | ||
} | ||
|
||
switch fileInfo.Mode() & os.ModeType { | ||
case os.ModeDir: | ||
if err := CreateIfNotExists(destPath, 0755); err != nil { | ||
return err | ||
} | ||
if err := CopyDirectory(sourcePath, destPath); err != nil { | ||
return err | ||
} | ||
case os.ModeSymlink: | ||
if err := CopySymLink(sourcePath, destPath); err != nil { | ||
return err | ||
} | ||
default: | ||
if err := Copy(sourcePath, destPath); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
if err := os.Lchown(destPath, int(stat.Uid), int(stat.Gid)); err != nil { | ||
return err | ||
} | ||
|
||
isSymlink := entry.Mode()&os.ModeSymlink != 0 | ||
if !isSymlink { | ||
if err := os.Chmod(destPath, entry.Mode()); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func Copy(srcFile, dstFile string) error { | ||
out, err := os.Create(dstFile) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer out.Close() | ||
|
||
in, err := os.Open(srcFile) | ||
defer in.Close() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = io.Copy(out, in) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func Exists(filePath string) bool { | ||
if _, err := os.Stat(filePath); os.IsNotExist(err) { | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
func CreateIfNotExists(dir string, perm os.FileMode) error { | ||
if Exists(dir) { | ||
return nil | ||
} | ||
|
||
if err := os.MkdirAll(dir, perm); err != nil { | ||
return fmt.Errorf("failed to create directory: '%s', error: '%s'", dir, err.Error()) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func CopySymLink(source, dest string) error { | ||
link, err := os.Readlink(source) | ||
if err != nil { | ||
return err | ||
} | ||
return os.Symlink(link, dest) | ||
//copy source directory to destination | ||
func CopyDirectory(source string, dest string) error { | ||
return copy.Copy(source, dest) | ||
} |
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