out-of-tree kernel {module, exploit} development tool
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

images.go 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2019 Mikhail Klementev. All rights reserved.
  2. // Use of this source code is governed by a AGPLv3 license
  3. // (or later) that can be found in the LICENSE file.
  4. package main
  5. import (
  6. "fmt"
  7. "io"
  8. "io/ioutil"
  9. "net/http"
  10. "os"
  11. "os/exec"
  12. )
  13. // inspired by Edd Turtle code
  14. func downloadFile(filepath string, url string) (err error) {
  15. out, err := os.Create(filepath)
  16. if err != nil {
  17. return
  18. }
  19. defer out.Close()
  20. resp, err := http.Get(url)
  21. if err != nil {
  22. return
  23. }
  24. defer resp.Body.Close()
  25. switch resp.StatusCode {
  26. case http.StatusOK:
  27. break
  28. case http.StatusForbidden, http.StatusNotFound:
  29. err = fmt.Errorf("Cannot download %s. It looks like you need "+
  30. "to generate it manually and place it "+
  31. "to ~/.out-of-tree/images/. "+
  32. "Check documentation for additional information.", url)
  33. return
  34. default:
  35. err = fmt.Errorf("Something weird happens while "+
  36. "download file: %d", resp.StatusCode)
  37. return
  38. }
  39. _, err = io.Copy(out, resp.Body)
  40. return
  41. }
  42. func unpackTar(archive, destination string) (err error) {
  43. // NOTE: If you're change anything in tar command please check also
  44. // BSD tar (or if you're using macOS, do not forget to check GNU Tar)
  45. // Also make sure that sparse files are extracting correctly
  46. cmd := exec.Command("tar", "-Sxf", archive)
  47. cmd.Dir = destination + "/"
  48. rawOutput, err := cmd.CombinedOutput()
  49. if err != nil {
  50. err = fmt.Errorf("%v: %s", err, rawOutput)
  51. return
  52. }
  53. return
  54. }
  55. func downloadImage(path, file string) (err error) {
  56. tmp, err := ioutil.TempDir("/tmp/", "out-of-tree_")
  57. if err != nil {
  58. return
  59. }
  60. defer os.RemoveAll(tmp)
  61. archive := tmp + "/" + file + ".tar.gz"
  62. url := imagesBaseURL + file + ".tar.gz"
  63. err = downloadFile(archive, url)
  64. if err != nil {
  65. return
  66. }
  67. err = unpackTar(archive, path)
  68. return
  69. }