blob: 6b3a7667c17f11c499069845b76aad5ac8866b7f [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"
bigbiff9dbd1fd2021-09-01 19:38:54 -040010 "runtime"
11 "path/filepath"
bigbiff6e0ca7d2021-02-06 19:15:16 -050012 "strings"
13)
14
15func getRecoveryAbsDir(ctx android.BaseContext) string {
16 return getBuildAbsDir(ctx) + "bootable/recovery/"
17}
18
19func getBuildAbsDir(ctx android.BaseContext) string {
bigbiff9dbd1fd2021-09-01 19:38:54 -040020 var b string
21 _, b, _, _ = runtime.Caller(0)
22 absIndex := strings.Index(filepath.Dir(b), "bootable")
23 return string(b[0:absIndex])
bigbiff6e0ca7d2021-02-06 19:15:16 -050024}
25
26func copyDir(src string, dest string) error {
27 var err error
28 var fds []os.FileInfo
29 var srcinfo os.FileInfo
30
31 if srcinfo, err = os.Stat(src); err != nil {
32 return err
33 }
34
35 if err = os.MkdirAll(dest, srcinfo.Mode()); err != nil {
36 return err
37 }
38
39 if fds, err = ioutil.ReadDir(src); err != nil {
40 return err
41 }
42 for _, fd := range fds {
43 srcfp := path.Join(src, fd.Name())
44 dstfp := path.Join(dest, fd.Name())
45
46 if fd.IsDir() {
47 if err = copyDir(srcfp, dstfp); err != nil {
48 fmt.Println(err)
49 }
50 } else {
51 if err = copyFile(srcfp, dstfp); err != nil {
52 fmt.Println(err)
53 }
54 }
55 }
56 return nil
57}
58
59func copyFile(src string, dest string) error {
60 var err error
61 var srcfd *os.File
62 var dstfd *os.File
63 var srcinfo os.FileInfo
64
65 if srcfd, err = os.Open(src); err != nil {
66 return err
67 }
68 defer srcfd.Close()
69
70 if dstfd, err = os.Create(dest); err != nil {
71 return err
72 }
73 defer dstfd.Close()
74
75 if _, err = io.Copy(dstfd, srcfd); err != nil {
76 return err
77 }
78 if srcinfo, err = os.Stat(src); err != nil {
79 return err
80 }
81 return os.Chmod(dest, srcinfo.Mode())
82}