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) {} 31 ~RawWriter()32 virtual ~RawWriter() 33 { 34 offset_ = 0; 35 if (fd_ > 0) { 36 fsync(fd_); 37 close(fd_); 38 } 39 fd_ = -1; 40 } 41 private: 42 int WriteInternal(int fd, const uint8_t *data, size_t len); 43 44 RawWriter(const RawWriter&) = delete; 45 46 const RawWriter& operator=(const RawWriter&) = delete; 47 int fd_; 48 std::string path_; 49 off64_t offset_; 50 }; 51 } // namespace Updater 52 #endif /* UPDATER_RAW_WRITER_H */ 53