cached_file_descriptor.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //
  2. // Copyright (C) 2017 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. #ifndef UPDATE_ENGINE_PAYLOAD_CONSUMER_CACHED_FILE_DESCRIPTOR_H_
  17. #define UPDATE_ENGINE_PAYLOAD_CONSUMER_CACHED_FILE_DESCRIPTOR_H_
  18. #include <errno.h>
  19. #include <sys/types.h>
  20. #include <memory>
  21. #include <vector>
  22. #include <brillo/secure_blob.h>
  23. #include "update_engine/payload_consumer/file_descriptor.h"
  24. namespace chromeos_update_engine {
  25. class CachedFileDescriptor : public FileDescriptor {
  26. public:
  27. CachedFileDescriptor(FileDescriptorPtr fd, size_t cache_size) : fd_(fd) {
  28. cache_.resize(cache_size);
  29. }
  30. ~CachedFileDescriptor() override = default;
  31. bool Open(const char* path, int flags, mode_t mode) override {
  32. return fd_->Open(path, flags, mode);
  33. }
  34. bool Open(const char* path, int flags) override {
  35. return fd_->Open(path, flags);
  36. }
  37. ssize_t Read(void* buf, size_t count) override {
  38. return fd_->Read(buf, count);
  39. }
  40. ssize_t Write(const void* buf, size_t count) override;
  41. off64_t Seek(off64_t offset, int whence) override;
  42. uint64_t BlockDevSize() override { return fd_->BlockDevSize(); }
  43. bool BlkIoctl(int request,
  44. uint64_t start,
  45. uint64_t length,
  46. int* result) override {
  47. return fd_->BlkIoctl(request, start, length, result);
  48. }
  49. bool Flush() override;
  50. bool Close() override;
  51. bool IsSettingErrno() override { return fd_->IsSettingErrno(); }
  52. bool IsOpen() override { return fd_->IsOpen(); }
  53. private:
  54. // Internal flush without the need to call |fd_->Flush()|.
  55. bool FlushCache();
  56. FileDescriptorPtr fd_;
  57. brillo::Blob cache_;
  58. size_t bytes_cached_{0};
  59. off64_t offset_{0};
  60. DISALLOW_COPY_AND_ASSIGN(CachedFileDescriptor);
  61. };
  62. } // namespace chromeos_update_engine
  63. #endif // UPDATE_ENGINE_PAYLOAD_CONSUMER_CACHED_FILE_DESCRIPTOR_H_