mapped_file_unix.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright 2015 The Bazel Authors. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <errno.h>
  15. #include <fcntl.h>
  16. #include <stdio.h>
  17. #include <sys/mman.h>
  18. #include <unistd.h>
  19. #include <algorithm>
  20. #include <limits>
  21. #include "third_party/ijar/mapped_file.h"
  22. #define MAX_ERROR 2048
  23. namespace devtools_ijar {
  24. static char errmsg[MAX_ERROR];
  25. struct MappedInputFileImpl {
  26. size_t discarded_;
  27. int fd_;
  28. };
  29. MappedInputFile::MappedInputFile(const char* name) {
  30. impl_ = NULL;
  31. opened_ = false;
  32. int fd = open(name, O_RDONLY);
  33. if (fd < 0) {
  34. snprintf(errmsg, MAX_ERROR, "open(): %s", strerror(errno));
  35. errmsg_ = errmsg;
  36. return;
  37. }
  38. off_t length = lseek(fd, 0, SEEK_END);
  39. if (length < 0) {
  40. snprintf(errmsg, MAX_ERROR, "lseek(): %s", strerror(errno));
  41. errmsg_ = errmsg;
  42. return;
  43. }
  44. void* buffer = mmap(NULL, length, PROT_READ, MAP_PRIVATE, fd, 0);
  45. if (buffer == MAP_FAILED) {
  46. snprintf(errmsg, MAX_ERROR, "mmap(): %s", strerror(errno));
  47. errmsg_ = errmsg;
  48. return;
  49. }
  50. impl_ = new MappedInputFileImpl();
  51. impl_->fd_ = fd;
  52. impl_->discarded_ = 0;
  53. buffer_ = reinterpret_cast<u1*>(buffer);
  54. length_ = length;
  55. opened_ = true;
  56. }
  57. MappedInputFile::~MappedInputFile() {
  58. delete impl_;
  59. }
  60. void MappedInputFile::Discard(size_t bytes) {
  61. munmap(buffer_ + impl_->discarded_, bytes);
  62. impl_->discarded_ += bytes;
  63. }
  64. int MappedInputFile::Close() {
  65. if (close(impl_->fd_) < 0) {
  66. snprintf(errmsg, MAX_ERROR, "close(): %s", strerror(errno));
  67. errmsg_ = errmsg;
  68. return -1;
  69. }
  70. return 0;
  71. }
  72. struct MappedOutputFileImpl {
  73. int fd_;
  74. int mmap_length_;
  75. };
  76. MappedOutputFile::MappedOutputFile(const char* name, size_t estimated_size)
  77. : estimated_size_(estimated_size) {
  78. impl_ = NULL;
  79. opened_ = false;
  80. int fd = open(name, O_CREAT|O_RDWR|O_TRUNC, 0644);
  81. if (fd < 0) {
  82. snprintf(errmsg, MAX_ERROR, "open(): %s", strerror(errno));
  83. errmsg_ = errmsg;
  84. return;
  85. }
  86. // Create mmap-able sparse file
  87. if (ftruncate(fd, estimated_size) < 0) {
  88. snprintf(errmsg, MAX_ERROR, "ftruncate(): %s", strerror(errno));
  89. errmsg_ = errmsg;
  90. return;
  91. }
  92. // Ensure that any buffer overflow in JarStripper will result in
  93. // SIGSEGV or SIGBUS by over-allocating beyond the end of the file.
  94. size_t mmap_length =
  95. std::min(static_cast<size_t>(estimated_size + sysconf(_SC_PAGESIZE)),
  96. std::numeric_limits<size_t>::max());
  97. void* mapped =
  98. mmap(NULL, mmap_length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  99. if (mapped == MAP_FAILED) {
  100. snprintf(errmsg, MAX_ERROR, "mmap(): %s", strerror(errno));
  101. errmsg_ = errmsg;
  102. return;
  103. }
  104. impl_ = new MappedOutputFileImpl();
  105. impl_->fd_ = fd;
  106. impl_->mmap_length_ = mmap_length;
  107. buffer_ = reinterpret_cast<u1*>(mapped);
  108. opened_ = true;
  109. }
  110. MappedOutputFile::~MappedOutputFile() {
  111. delete impl_;
  112. }
  113. int MappedOutputFile::Close(size_t size) {
  114. if (size > estimated_size_) {
  115. snprintf(errmsg, MAX_ERROR, "size %zu > estimated size %zu", size,
  116. estimated_size_);
  117. errmsg_ = errmsg;
  118. return -1;
  119. }
  120. munmap(buffer_, impl_->mmap_length_);
  121. if (ftruncate(impl_->fd_, size) < 0) {
  122. snprintf(errmsg, MAX_ERROR, "ftruncate(): %s", strerror(errno));
  123. errmsg_ = errmsg;
  124. return -1;
  125. }
  126. if (close(impl_->fd_) < 0) {
  127. snprintf(errmsg, MAX_ERROR, "close(): %s", strerror(errno));
  128. errmsg_ = errmsg;
  129. return -1;
  130. }
  131. return 0;
  132. }
  133. } // namespace devtools_ijar