No Description

fuse_sdcard_provider.cpp 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (C) 2014 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "fuse_sdcard_provider.h"
  17. #include <errno.h>
  18. #include <fcntl.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <pthread.h>
  23. #include <sys/mount.h>
  24. #include <sys/stat.h>
  25. #include <unistd.h>
  26. #include <functional>
  27. #include <android-base/file.h>
  28. #include "fuse_sideload.h"
  29. struct file_data {
  30. int fd; // the underlying sdcard file
  31. uint64_t file_size;
  32. uint32_t block_size;
  33. };
  34. static int read_block_file(const file_data& fd, uint32_t block, uint8_t* buffer,
  35. uint32_t fetch_size) {
  36. off64_t offset = static_cast<off64_t>(block) * fd.block_size;
  37. if (TEMP_FAILURE_RETRY(lseek64(fd.fd, offset, SEEK_SET)) == -1) {
  38. fprintf(stderr, "seek on sdcard failed: %s\n", strerror(errno));
  39. return -EIO;
  40. }
  41. if (!android::base::ReadFully(fd.fd, buffer, fetch_size)) {
  42. fprintf(stderr, "read on sdcard failed: %s\n", strerror(errno));
  43. return -EIO;
  44. }
  45. return 0;
  46. }
  47. struct token {
  48. pthread_t th;
  49. const char* path;
  50. int result;
  51. };
  52. static void* run_sdcard_fuse(void* cookie) {
  53. token* t = reinterpret_cast<token*>(cookie);
  54. struct stat sb;
  55. if (stat(t->path, &sb) < 0) {
  56. fprintf(stderr, "failed to stat %s: %s\n", t->path, strerror(errno));
  57. t->result = -1;
  58. return nullptr;
  59. }
  60. struct file_data fd;
  61. struct provider_vtab vtab;
  62. fd.fd = open(t->path, O_RDONLY);
  63. if (fd.fd < 0) {
  64. fprintf(stderr, "failed to open %s: %s\n", t->path, strerror(errno));
  65. t->result = -1;
  66. return nullptr;
  67. }
  68. fd.file_size = sb.st_size;
  69. fd.block_size = 65536;
  70. vtab.read_block = std::bind(&read_block_file, fd, std::placeholders::_1, std::placeholders::_2,
  71. std::placeholders::_3);
  72. vtab.close = [&fd]() { close(fd.fd); };
  73. t->result = run_fuse_sideload(vtab, fd.file_size, fd.block_size);
  74. return nullptr;
  75. }
  76. // How long (in seconds) we wait for the fuse-provided package file to
  77. // appear, before timing out.
  78. #define SDCARD_INSTALL_TIMEOUT 10
  79. void* start_sdcard_fuse(const char* path) {
  80. token* t = new token;
  81. t->path = path;
  82. pthread_create(&(t->th), NULL, run_sdcard_fuse, t);
  83. struct stat st;
  84. int i;
  85. for (i = 0; i < SDCARD_INSTALL_TIMEOUT; ++i) {
  86. if (stat(FUSE_SIDELOAD_HOST_PATHNAME, &st) != 0) {
  87. if (errno == ENOENT && i < SDCARD_INSTALL_TIMEOUT-1) {
  88. sleep(1);
  89. continue;
  90. } else {
  91. return nullptr;
  92. }
  93. }
  94. }
  95. // The installation process expects to find the sdcard unmounted. Unmount it with MNT_DETACH so
  96. // that our open file continues to work but new references see it as unmounted.
  97. umount2("/sdcard", MNT_DETACH);
  98. return t;
  99. }
  100. void finish_sdcard_fuse(void* cookie) {
  101. if (cookie == NULL) return;
  102. token* t = reinterpret_cast<token*>(cookie);
  103. // Calling stat() on this magic filename signals the fuse
  104. // filesystem to shut down.
  105. struct stat st;
  106. stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
  107. pthread_join(t->th, nullptr);
  108. delete t;
  109. }