1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 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 */ 15 16 #ifndef UPDATER_RAW_WRITER_H 17 #define UPDATER_RAW_WRITER_H 18 19 #include <cstdio> 20 #include <string> 21 #include <sys/types.h> 22 #include <unistd.h> 23 #include "applypatch/data_writer.h" 24 25 namespace Updater { 26 class RawWriter : public DataWriter { 27 public: 28 bool Write(const uint8_t *addr, size_t len, const void *context) override; 29 RawWriter(const std::string path,uint64_t offset)30 RawWriter(const std::string path, uint64_t offset) : fd_(-1), path_(path), offset_(offset) {} RawWriter(const std::string path,uint64_t startAddr,uint64_t offset)31 RawWriter(const std::string path, uint64_t startAddr, uint64_t offset) : fd_(-1), 32 path_(path), offset_(startAddr + offset) {} 33 ~RawWriter()34 virtual ~RawWriter() 35 { 36 offset_ = 0; 37 if (fd_ > 0) { 38 fsync(fd_); 39 close(fd_); 40 } 41 fd_ = -1; 42 } 43 private: 44 int WriteInternal(int fd, const uint8_t *data, size_t len); 45 46 RawWriter(const RawWriter&) = delete; 47 48 const RawWriter& operator=(const RawWriter&) = delete; 49 int fd_; 50 std::string path_; 51 off64_t offset_; 52 }; 53 } // namespace Updater 54 #endif /* UPDATER_RAW_WRITER_H */ 55