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 #include "update_engine/payload_consumer/cached_file_descriptor.h"
18 
19 #include <unistd.h>
20 
21 #include <algorithm>
22 
23 #include <base/logging.h>
24 
25 #include "update_engine/common/utils.h"
26 
27 namespace chromeos_update_engine {
28 
29 off64_t CachedFileDescriptor::Seek(off64_t offset, int whence) {
30   // Only support SEEK_SET and SEEK_CUR. I think these two would be enough. If
31   // we want to support SEEK_END then we have to figure out the size of the
32   // underlying file descriptor each time and it may not be a very good idea.
33   CHECK(whence == SEEK_SET || whence == SEEK_CUR);
34   off64_t next_offset = whence == SEEK_SET ? offset : offset_ + offset;
35 
36   if (next_offset != offset_) {
37     // We sought somewhere other than what we are now. So we have to flush and
38     // move to the new offset.
39     if (!FlushCache()) {
40       return -1;
41     }
42     // Then we have to seek there.
43     if (fd_->Seek(next_offset, SEEK_SET) < 0) {
44       return -1;
45     }
46     offset_ = next_offset;
47   }
48   return offset_;
49 }
50 
51 ssize_t CachedFileDescriptor::Write(const void* buf, size_t count) {
52   auto bytes = static_cast<const uint8_t*>(buf);
53   size_t total_bytes_wrote = 0;
54   while (total_bytes_wrote < count) {
55     auto bytes_to_cache =
56         std::min(count - total_bytes_wrote, cache_.size() - bytes_cached_);
57     if (bytes_to_cache > 0) {  // Which means |cache_| is still have some space.
58       memcpy(cache_.data() + bytes_cached_,
59              bytes + total_bytes_wrote,
60              bytes_to_cache);
61       total_bytes_wrote += bytes_to_cache;
62       bytes_cached_ += bytes_to_cache;
63     }
64     if (bytes_cached_ == cache_.size()) {
65       // Cache is full; write it to the |fd_| as long as you can.
66       if (!FlushCache()) {
67         return -1;
68       }
69     }
70   }
71   offset_ += total_bytes_wrote;
72   return total_bytes_wrote;
73 }
74 
75 bool CachedFileDescriptor::Flush() {
76   return FlushCache() && fd_->Flush();
77 }
78 
79 bool CachedFileDescriptor::Close() {
80   offset_ = 0;
81   return FlushCache() && fd_->Close();
82 }
83 
84 bool CachedFileDescriptor::FlushCache() {
85   size_t begin = 0;
86   while (begin < bytes_cached_) {
87     auto bytes_wrote = fd_->Write(cache_.data() + begin, bytes_cached_ - begin);
88     if (bytes_wrote < 0) {
89       PLOG(ERROR) << "Failed to flush cached data!";
90       return false;
91     }
92     begin += bytes_wrote;
93   }
94   bytes_cached_ = 0;
95   return true;
96 }
97 
98 }  // namespace chromeos_update_engine
99