blob: c6b07b4e0e737bc84d86d30a82c541c4fdbc6bd6 [file] [log] [blame]
bigbiff6e0ca7d2021-02-06 19:15:16 -05001package twrp
2
3import (
4 "android/soong/android"
5 "fmt"
6 "io"
7 "io/ioutil"
8 "os"
9 "path"
10 "strings"
11)
12
13func getRecoveryAbsDir(ctx android.BaseContext) string {
14 return getBuildAbsDir(ctx) + "bootable/recovery/"
15}
16
17func getBuildAbsDir(ctx android.BaseContext) string {
18 outDir := ctx.Config().Getenv("OUT")
19 absIndex := strings.Index(outDir, "out")
20 return string(outDir[0:absIndex])
21}
22
23func copyDir(src string, dest string) error {
24 var err error
25 var fds []os.FileInfo
26 var srcinfo os.FileInfo
27
28 if srcinfo, err = os.Stat(src); err != nil {
29 return err
30 }
31
32 if err = os.MkdirAll(dest, srcinfo.Mode()); err != nil {
33 return err
34 }
35
36 if fds, err = ioutil.ReadDir(src); err != nil {
37 return err
38 }
39 for _, fd := range fds {
40 srcfp := path.Join(src, fd.Name())
41 dstfp := path.Join(dest, fd.Name())
42
43 if fd.IsDir() {
44 if err = copyDir(srcfp, dstfp); err != nil {
45 fmt.Println(err)
46 }
47 } else {
48 if err = copyFile(srcfp, dstfp); err != nil {
49 fmt.Println(err)
50 }
51 }
52 }
53 return nil
54}
55
56func copyFile(src string, dest string) error {
57 var err error
58 var srcfd *os.File
59 var dstfd *os.File
60 var srcinfo os.FileInfo
61
62 if srcfd, err = os.Open(src); err != nil {
63 return err
64 }
65 defer srcfd.Close()
66
67 if dstfd, err = os.Create(dest); err != nil {
68 return err
69 }
70 defer dstfd.Close()
71
72 if _, err = io.Copy(dstfd, srcfd); err != nil {
73 return err
74 }
75 if srcinfo, err = os.Stat(src); err != nil {
76 return err
77 }
78 return os.Chmod(dest, srcinfo.Mode())
79}