bigbiff | 6e0ca7d | 2021-02-06 19:15:16 -0500 | [diff] [blame] | 1 | package twrp |
| 2 | |
| 3 | import ( |
| 4 | "android/soong/android" |
| 5 | "fmt" |
| 6 | "io" |
| 7 | "io/ioutil" |
| 8 | "os" |
| 9 | "path" |
bigbiff | 9dbd1fd | 2021-09-01 19:38:54 -0400 | [diff] [blame] | 10 | "runtime" |
| 11 | "path/filepath" |
bigbiff | 6e0ca7d | 2021-02-06 19:15:16 -0500 | [diff] [blame] | 12 | "strings" |
| 13 | ) |
| 14 | |
| 15 | func getRecoveryAbsDir(ctx android.BaseContext) string { |
| 16 | return getBuildAbsDir(ctx) + "bootable/recovery/" |
| 17 | } |
| 18 | |
| 19 | func getBuildAbsDir(ctx android.BaseContext) string { |
bigbiff | 9dbd1fd | 2021-09-01 19:38:54 -0400 | [diff] [blame] | 20 | var b string |
| 21 | _, b, _, _ = runtime.Caller(0) |
| 22 | absIndex := strings.Index(filepath.Dir(b), "bootable") |
| 23 | return string(b[0:absIndex]) |
bigbiff | 6e0ca7d | 2021-02-06 19:15:16 -0500 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | func 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 | |
| 59 | func 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 | } |