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 
17 #ifndef UPDATE_ENGINE_PAYLOAD_CONSUMER_CACHED_FILE_DESCRIPTOR_H_
18 #define UPDATE_ENGINE_PAYLOAD_CONSUMER_CACHED_FILE_DESCRIPTOR_H_
19 
20 #include <errno.h>
21 #include <sys/types.h>
22 
23 #include <memory>
24 #include <vector>
25 
26 #include <brillo/secure_blob.h>
27 
28 #include "update_engine/payload_consumer/file_descriptor.h"
29 
30 namespace chromeos_update_engine {
31 
32 class CachedFileDescriptorBase : public FileDescriptor {
33  public:
CachedFileDescriptorBase(size_t cache_size)34   explicit CachedFileDescriptorBase(size_t cache_size) : cache_(cache_size) {}
35   ~CachedFileDescriptorBase() override = default;
36 
Open(const char * path,int flags,mode_t mode)37   bool Open(const char* path, int flags, mode_t mode) override {
38     return GetFd()->Open(path, flags, mode);
39   }
Open(const char * path,int flags)40   bool Open(const char* path, int flags) override {
41     return GetFd()->Open(path, flags);
42   }
Read(void * buf,size_t count)43   ssize_t Read(void* buf, size_t count) override {
44     return GetFd()->Read(buf, count);
45   }
46   ssize_t Write(const void* buf, size_t count) override;
47   off64_t Seek(off64_t offset, int whence) override;
BlockDevSize()48   uint64_t BlockDevSize() override { return GetFd()->BlockDevSize(); }
BlkIoctl(int request,uint64_t start,uint64_t length,int * result)49   bool BlkIoctl(int request,
50                 uint64_t start,
51                 uint64_t length,
52                 int* result) override {
53     return GetFd()->BlkIoctl(request, start, length, result);
54   }
55   bool Flush() override;
56   bool Close() override;
IsSettingErrno()57   bool IsSettingErrno() override { return GetFd()->IsSettingErrno(); }
IsOpen()58   bool IsOpen() override { return GetFd()->IsOpen(); }
59 
60  protected:
61   virtual FileDescriptor* GetFd() = 0;
62 
63  private:
64   // Internal flush without the need to call |fd_->Flush()|.
65   bool FlushCache();
66 
67   brillo::Blob cache_;
68   size_t bytes_cached_{0};
69   off64_t offset_{0};
70 
71   DISALLOW_COPY_AND_ASSIGN(CachedFileDescriptorBase);
72 };
73 
74 class CachedFileDescriptor final : public CachedFileDescriptorBase {
75  public:
CachedFileDescriptor(FileDescriptorPtr fd,size_t cache_size)76   CachedFileDescriptor(FileDescriptorPtr fd, size_t cache_size)
77       : CachedFileDescriptorBase(cache_size), fd_(fd) {}
78 
79  protected:
GetFd()80   virtual FileDescriptor* GetFd() { return fd_.get(); }
81   FileDescriptorPtr fd_;
82 };
83 
84 class UnownedCachedFileDescriptor final : public CachedFileDescriptorBase {
85  public:
UnownedCachedFileDescriptor(FileDescriptor * fd,size_t cache_size)86   UnownedCachedFileDescriptor(FileDescriptor* fd, size_t cache_size)
87       : CachedFileDescriptorBase(cache_size), fd_(fd) {}
88   // used for EnocdeFEC
89   void SetFD(FileDescriptor* fd);
90 
91  protected:
GetFd()92   virtual FileDescriptor* GetFd() { return fd_; }
93   FileDescriptor* fd_;
94 };
95 
96 }  // namespace chromeos_update_engine
97 
98 #endif  // UPDATE_ENGINE_PAYLOAD_CONSUMER_CACHED_FILE_DESCRIPTOR_H_
99