From e0df5554f064250c63f82a473a1439f3830caa8f Mon Sep 17 00:00:00 2001 From: ma_nan Date: Thu, 29 Jul 2021 19:02:55 +0800 Subject: [PATCH 1/4] delete sparse image Signed-off-by: ma_nan --- ohos.build | 1 - services/applypatch/BUILD.gn | 2 - services/applypatch/data_writer.cpp | 6 - services/applypatch/sparse_writer.cpp | 59 ---- services/applypatch/sparse_writer.h | 40 --- services/script/yacc/FlexLexer.h | 220 +++++++++++++++ services/sparse_image/BUILD.gn | 34 --- services/sparse_image/sparse_block.cpp | 166 ----------- services/sparse_image/sparse_block.h | 63 ----- services/sparse_image/sparse_chunk.cpp | 125 --------- services/sparse_image/sparse_chunk.h | 28 -- services/sparse_image/sparse_image.cpp | 162 ----------- services/sparse_image/sparse_image.h | 68 ----- services/sparse_image/sparse_image_handler.h | 80 ------ services/sparse_image/sparse_image_writer.cpp | 262 ------------------ 15 files changed, 220 insertions(+), 1096 deletions(-) delete mode 100644 services/applypatch/sparse_writer.cpp delete mode 100644 services/applypatch/sparse_writer.h create mode 100644 services/script/yacc/FlexLexer.h delete mode 100644 services/sparse_image/BUILD.gn delete mode 100644 services/sparse_image/sparse_block.cpp delete mode 100644 services/sparse_image/sparse_block.h delete mode 100644 services/sparse_image/sparse_chunk.cpp delete mode 100644 services/sparse_image/sparse_chunk.h delete mode 100644 services/sparse_image/sparse_image.cpp delete mode 100644 services/sparse_image/sparse_image.h delete mode 100644 services/sparse_image/sparse_image_handler.h delete mode 100644 services/sparse_image/sparse_image_writer.cpp diff --git a/ohos.build b/ohos.build index 123b489a..bfdc7c2a 100644 --- a/ohos.build +++ b/ohos.build @@ -11,7 +11,6 @@ "//base/update/updater/services/updater_binary:updater_binary", "//base/update/updater/services:updater", "//base/update/updater/services/applypatch:libapplypatch", - "//base/update/updater/services/sparse_image:libsparseimage", "//base/update/updater/services/fs_manager:libfsmanager", "//base/update/updater/utils:libutils", "//base/update/updater/utils:updater_reboot", diff --git a/services/applypatch/BUILD.gn b/services/applypatch/BUILD.gn index 085b8ad2..8d8d8725 100644 --- a/services/applypatch/BUILD.gn +++ b/services/applypatch/BUILD.gn @@ -23,7 +23,6 @@ ohos_static_library("libapplypatch") { "data_writer.cpp", "partition_record.cpp", "raw_writer.cpp", - "sparse_writer.cpp", "store.cpp", "transfer_manager.cpp", ] @@ -45,7 +44,6 @@ ohos_static_library("libapplypatch") { deps = [ "//base/update/updater/services/diffpatch/patch:libpatch", "//base/update/updater/services/fs_manager:libfsmanager", - "//base/update/updater/services/sparse_image:libsparseimage", "//third_party/bounds_checking_function:libsec_static", "//third_party/bzip2:libbz2", ] diff --git a/services/applypatch/data_writer.cpp b/services/applypatch/data_writer.cpp index 8618aeb0..602fdd74 100644 --- a/services/applypatch/data_writer.cpp +++ b/services/applypatch/data_writer.cpp @@ -23,7 +23,6 @@ #include "fs_manager/mount.h" #include "log/log.h" #include "raw_writer.h" -#include "sparse_writer.h" namespace updater { int DataWriter::OpenPartition(const std::string &partitionName) @@ -53,11 +52,6 @@ int DataWriter::OpenPartition(const std::string &partitionName) std::unique_ptr DataWriter::CreateDataWriter(WriteMode mode, const std::string &partitionName) { switch (mode) { - case WRITE_SPARSE: - { - std::unique_ptr writer(std::make_unique(partitionName)); - return std::move(writer); - } case WRITE_RAW: { std::unique_ptr writer(std::make_unique(partitionName)); diff --git a/services/applypatch/sparse_writer.cpp b/services/applypatch/sparse_writer.cpp deleted file mode 100644 index b6358cdf..00000000 --- a/services/applypatch/sparse_writer.cpp +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#include "sparse_writer.h" -#include -#include -#include -#include -#include "log/log.h" -#include "sparse_image/sparse_image_api.h" - -namespace updater { -constexpr uint32_t SPARSE_HEADER_MAGIC = 0XED26FF3A; -SparseWriter::~SparseWriter() -{ - if (sf_ != nullptr) { - DeallocateSparseImage(sf_); - } -} - -bool SparseWriter::Write(const uint8_t *addr, size_t len, WriteMode mode, const std::string &partitionName) -{ - if (addr == nullptr) { - LOG(ERROR) << "SparseWrite: invalid address."; - return false; - } - - if (len == 0) { - LOG(WARNING) << "SparseWrite: write length is 0, skip."; - return false; - } - if ((len <= sizeof(SPARSE_HEADER_MAGIC)) || (*reinterpret_cast(addr) != SPARSE_HEADER_MAGIC)) { - LOG(ERROR) << "SparseWrite: Invalid sparse image."; - return false; - } - - auto p = const_cast(addr); - sf_ = CreateSparseImageFromBuffer(reinterpret_cast(p)); - UPDATER_ERROR_CHECK(sf_ != nullptr, "Sparsewrite: cannot get sparse file descriptor.", return false); - int fd = OpenPartition(partitionName_); - UPDATER_CHECK_ONLY_RETURN(fd >= 0, return false); - int ret = SparseImageRestore(fd, sf_); - UPDATER_ERROR_CHECK(ret >= 0, "Sparsewrite: write sparse image failed.", close(fd); return false); - LOG(INFO) << "Sparsewrite: write sparse image successfully."; - close(fd); - return true; -} -} // updater diff --git a/services/applypatch/sparse_writer.h b/services/applypatch/sparse_writer.h deleted file mode 100644 index 2ef2ea04..00000000 --- a/services/applypatch/sparse_writer.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef UPDATE_SPARSE_WRITER_H -#define UPDATE_SPARSE_WRITER_H - -#include -#include -#include "applypatch/data_writer.h" -#include "sparse_image/sparse_image_api.h" - -namespace updater { -class SparseWriter : public DataWriter { -public: - virtual bool Write(const uint8_t *addr, size_t len, WriteMode mode, const std::string &partitionName); - - explicit SparseWriter(const std::string &partitionName) : sf_(nullptr), partitionName_(partitionName) {} - - virtual ~SparseWriter(); -private: - struct SparseImage *sf_; - - SparseWriter(const SparseWriter&) = delete; - - const SparseWriter& operator=(const SparseWriter&) = delete; - std::string partitionName_; -}; -} // updater -#endif // UPDATE_SPARSE_WRITER_H diff --git a/services/script/yacc/FlexLexer.h b/services/script/yacc/FlexLexer.h new file mode 100644 index 00000000..c4dad2b1 --- /dev/null +++ b/services/script/yacc/FlexLexer.h @@ -0,0 +1,220 @@ +// -*-C++-*- +// FlexLexer.h -- define interfaces for lexical analyzer classes generated +// by flex + +// Copyright (c) 1993 The Regents of the University of California. +// All rights reserved. +// +// This code is derived from software contributed to Berkeley by +// Kent Williams and Tom Epperly. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions +// are met: + +// 1. Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. + +// Neither the name of the University nor the names of its contributors +// may be used to endorse or promote products derived from this software +// without specific prior written permission. + +// THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR +// IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED +// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +// PURPOSE. + +// This file defines FlexLexer, an abstract class which specifies the +// external interface provided to flex C++ lexer objects, and yyFlexLexer, +// which defines a particular lexer class. +// +// If you want to create multiple lexer classes, you use the -P flag +// to rename each yyFlexLexer to some other xxFlexLexer. You then +// include in your other sources once per lexer class: +// +// #undef yyFlexLexer +// #define yyFlexLexer xxFlexLexer +// #include +// +// #undef yyFlexLexer +// #define yyFlexLexer zzFlexLexer +// #include +// ... + +#ifndef __FLEX_LEXER_H +// Never included before - need to define base class. +#define __FLEX_LEXER_H + +#include + +extern "C++" { + +struct yy_buffer_state; +typedef int yy_state_type; + +class FlexLexer +{ +public: + virtual ~FlexLexer() { } + + const char* YYText() const { return yytext; } + int YYLeng() const { return yyleng; } + + virtual void + yy_switch_to_buffer( yy_buffer_state* new_buffer ) = 0; + virtual yy_buffer_state* yy_create_buffer( std::istream* s, int size ) = 0; + virtual yy_buffer_state* yy_create_buffer( std::istream& s, int size ) = 0; + virtual void yy_delete_buffer( yy_buffer_state* b ) = 0; + virtual void yyrestart( std::istream* s ) = 0; + virtual void yyrestart( std::istream& s ) = 0; + + virtual int yylex() = 0; + + // Call yylex with new input/output sources. + int yylex( std::istream& new_in, std::ostream& new_out ) + { + switch_streams( new_in, new_out ); + return yylex(); + } + + int yylex( std::istream* new_in, std::ostream* new_out = 0) + { + switch_streams( new_in, new_out ); + return yylex(); + } + + // Switch to new input/output streams. A nil stream pointer + // indicates "keep the current one". + virtual void switch_streams( std::istream* new_in, + std::ostream* new_out ) = 0; + virtual void switch_streams( std::istream& new_in, + std::ostream& new_out ) = 0; + + int lineno() const { return yylineno; } + + int debug() const { return yy_flex_debug; } + void set_debug( int flag ) { yy_flex_debug = flag; } + +protected: + char* yytext; + int yyleng; + int yylineno; // only maintained if you use %option yylineno + int yy_flex_debug; // only has effect with -d or "%option debug" +}; + +} +#endif // FLEXLEXER_H + +#if defined(yyFlexLexer) || ! defined(yyFlexLexerOnce) +// Either this is the first time through (yyFlexLexerOnce not defined), +// or this is a repeated include to define a different flavor of +// yyFlexLexer, as discussed in the flex manual. +# define yyFlexLexerOnce + +extern "C++" { + +class yyFlexLexer : public FlexLexer { +public: + // arg_yyin and arg_yyout default to the cin and cout, but we + // only make that assignment when initializing in yylex(). + yyFlexLexer( std::istream& arg_yyin, std::ostream& arg_yyout ); + yyFlexLexer( std::istream* arg_yyin = 0, std::ostream* arg_yyout = 0 ); +private: + void ctor_common(); + +public: + + virtual ~yyFlexLexer(); + + void yy_switch_to_buffer( yy_buffer_state* new_buffer ); + yy_buffer_state* yy_create_buffer( std::istream* s, int size ); + yy_buffer_state* yy_create_buffer( std::istream& s, int size ); + void yy_delete_buffer( yy_buffer_state* b ); + void yyrestart( std::istream* s ); + void yyrestart( std::istream& s ); + + void yypush_buffer_state( yy_buffer_state* new_buffer ); + void yypop_buffer_state(); + + virtual int yylex(); + virtual void switch_streams( std::istream& new_in, std::ostream& new_out ); + virtual void switch_streams( std::istream* new_in = 0, std::ostream* new_out = 0 ); + virtual int yywrap(); + +protected: + virtual int LexerInput( char* buf, int max_size ); + virtual void LexerOutput( const char* buf, int size ); + virtual void LexerError( const char* msg ); + + void yyunput( int c, char* buf_ptr ); + int yyinput(); + + void yy_load_buffer_state(); + void yy_init_buffer( yy_buffer_state* b, std::istream& s ); + void yy_flush_buffer( yy_buffer_state* b ); + + int yy_start_stack_ptr; + int yy_start_stack_depth; + int* yy_start_stack; + + void yy_push_state( int new_state ); + void yy_pop_state(); + int yy_top_state(); + + yy_state_type yy_get_previous_state(); + yy_state_type yy_try_NUL_trans( yy_state_type current_state ); + int yy_get_next_buffer(); + + std::istream yyin; // input source for default LexerInput + std::ostream yyout; // output sink for default LexerOutput + + // yy_hold_char holds the character lost when yytext is formed. + char yy_hold_char; + + // Number of characters read into yy_ch_buf. + int yy_n_chars; + + // Points to current character in buffer. + char* yy_c_buf_p; + + int yy_init; // whether we need to initialize + int yy_start; // start state number + + // Flag which is used to allow yywrap()'s to do buffer switches + // instead of setting up a fresh yyin. A bit of a hack ... + int yy_did_buffer_switch_on_eof; + + + size_t yy_buffer_stack_top; /**< index of top of stack. */ + size_t yy_buffer_stack_max; /**< capacity of stack. */ + yy_buffer_state ** yy_buffer_stack; /**< Stack as an array. */ + void yyensure_buffer_stack(void); + + // The following are not always needed, but may be depending + // on use of certain flex features (like REJECT or yymore()). + + yy_state_type yy_last_accepting_state; + char* yy_last_accepting_cpos; + + yy_state_type* yy_state_buf; + yy_state_type* yy_state_ptr; + + char* yy_full_match; + int* yy_full_state; + int yy_full_lp; + + int yy_lp; + int yy_looking_for_trail_begin; + + int yy_more_flag; + int yy_more_len; + int yy_more_offset; + int yy_prev_more_offset; +}; + +} + +#endif // yyFlexLexer || ! yyFlexLexerOnce diff --git a/services/sparse_image/BUILD.gn b/services/sparse_image/BUILD.gn deleted file mode 100644 index 252c79bc..00000000 --- a/services/sparse_image/BUILD.gn +++ /dev/null @@ -1,34 +0,0 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import("//build/ohos.gni") - -ohos_static_library("libsparseimage") { - sources = [ - "sparse_block.cpp", - "sparse_chunk.cpp", - "sparse_image.cpp", - "sparse_image_writer.cpp", - ] - - include_dirs = [ - "//base/update/updater/services/include", - "//base/update/updater/utils/include", - "//third_party/bounds_checking_function/include", - ] - - deps = [ - "//base/update/updater/services/log:libupdaterlog", - "//third_party/bounds_checking_function:libsec_static", - ] -} diff --git a/services/sparse_image/sparse_block.cpp b/services/sparse_image/sparse_block.cpp deleted file mode 100644 index a0dcc14f..00000000 --- a/services/sparse_image/sparse_block.cpp +++ /dev/null @@ -1,166 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "sparse_block.h" - -#include -#include "log/log.h" -#include "securec.h" - -namespace updater { -void DeallocateBlockList(struct BlockList *bl) -{ - SparseImageBlock *imageBlocks = bl->imageBlocks; - while (imageBlocks != nullptr) { - SparseImageBlock *next = imageBlocks->next; - if (imageBlocks->type == BLOCK_TYPE_FILE) { - free(imageBlocks->file.fileName); - } - free(imageBlocks); - imageBlocks = next; - } - free(bl); - bl = nullptr; -} - -static void MergeBlocks(unsigned int blockSize, SparseImageBlock *prev, SparseImageBlock *next) -{ - // sanity checks - if (prev == nullptr || next == nullptr) { // Do not need to merge - return; - } - - if (prev->type != next->type) { // block with different type - LOG(WARNING) << "SparseBlock: try to merge blocks with different type: " << - " previous block with type: " << prev->type << - " while next block with type: " << next->type; - return; - } - - // should never happen - if (blockSize == 0) { - LOG(ERROR) << "SparseBlock: block size is zero"; - return; - } - // check if these two blocks adjacent - unsigned int blockCounts = prev->size / blockSize; - if (prev->blockIndex + blockCounts != next->blockIndex) { - LOG(WARNING) << "SparseBlock: try to merge two block nonadjacent: " << - " previous block index: " << prev->blockIndex << " size: " << prev->size << - " while next block index: " << next->blockIndex; - return; - } - - if (prev->type == BLOCK_TYPE_FILE) { - if (strcmp(prev->file.fileName, next->file.fileName) != 0 || - prev->file.offset + prev->size != next->file.offset) { - return; - } - } else if (prev->type == BLOCK_TYPE_FD) { - if (prev->fd.fd != next->fd.fd || prev->fd.offset + prev->size != next->fd.offset) { - return; - } - } else if (prev->type == BLOCK_TYPE_FILL) { - if (prev->fill.value != next->fill.value) { - return; - } - } else { // BLOCK_TYPE_DATA, do not need to merge - return; - } - - // OK, we've done all checks, now merge next to prev. - prev->size += next->size; - prev->next = next->next; - // free next; - if (next->type == BLOCK_TYPE_FILE) { - free(next->file.fileName); - } - free(next); - return; -} - -static int InsertToBlockList(struct BlockList *bl, SparseImageBlock *sib) -{ - if (bl->imageBlocks == nullptr) { - bl->imageBlocks = sib; - bl->imageBlocks->next = nullptr; - return 0; - } - - // block list is not empty, insert @sib to block list in order. - if (bl->imageBlocks->blockIndex > sib->blockIndex) { - // Insert @sib at front of bl - sib->next = bl->imageBlocks; - bl->imageBlocks = sib; - return 0; - } - - // @sib->blockIndex is larger. find a appropriate place - SparseImageBlock *tmp = nullptr; - for (tmp = bl->imageBlocks; tmp->next != nullptr; tmp = tmp->next) { - if (tmp->next->blockIndex > sib->blockIndex) { - break; - } - } - - // Insert @sib to the appropriate place. - if (tmp->next == nullptr) { - tmp->next = sib; - } else { - sib->next = tmp->next; - tmp->next = sib; - } - - // adjacent block may have the same property, merge them. - // 1. merge sib with next one - MergeBlocks(bl->blockSize, sib, sib->next); - MergeBlocks(bl->blockSize, tmp, sib); - return 0; -} - -int AddRawDataToSparseImage(struct BlockList *bl, void *buffer, int64_t size, uint32_t blockIndex) -{ - SparseImageBlock *sib = static_cast(calloc(1, sizeof(SparseImageBlock))); - if (sib == nullptr) { - LOG(ERROR) << "SparseBlock: Failed to allocate memory for sparse block"; - return -ENOMEM; - } - sib->blockIndex = blockIndex; - sib->size = static_cast(size); // maybe the size should be unsigned int - sib->type = BLOCK_TYPE_DATA; - sib->data.data = buffer; - sib->next = nullptr; - - // insert into block list. - return InsertToBlockList(bl, sib); -} - -int AddFillValueToSparseImage(struct BlockList *bl, uint32_t value, int64_t size, uint32_t blockIndex) -{ - SparseImageBlock *sib = static_cast(calloc(1, sizeof(SparseImageBlock))); - if (sib == nullptr) { - LOG(ERROR) << "SparseBlock: Failed to allocate memory for sparse block"; - return -ENOMEM; - } - - sib->blockIndex = blockIndex; - sib->size = static_cast(size); - sib->type = BLOCK_TYPE_FILL; - sib->fill.value = value; - sib->next = nullptr; - - return InsertToBlockList(bl, sib); -} -} // namespace updater diff --git a/services/sparse_image/sparse_block.h b/services/sparse_image/sparse_block.h deleted file mode 100644 index 3d32630f..00000000 --- a/services/sparse_image/sparse_block.h +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef UPDATER_SPARSE_BLOCK_H -#define UPDATER_SPARSE_BLOCK_H -#include - -namespace updater { -enum BLOCKTYPE { - BLOCK_TYPE_DATA, - BLOCK_TYPE_FILE, - BLOCK_TYPE_FD, - BLOCK_TYPE_FILL, -}; - -// Sparse image combined by many -// blocks. each block has different type -struct SparseImageBlock { - unsigned int blockIndex; - unsigned int size; - BLOCKTYPE type; - union { - struct { - void *data; - } data; - struct { - char *fileName; - int64_t offset; - } file; - struct { - int fd; - int64_t offset; - } fd; - struct { - uint32_t value; - } fill; - }; - SparseImageBlock *next; -}; - -struct BlockList { - SparseImageBlock *imageBlocks; - unsigned int blockSize; -}; - -void DeallocateBlockList(struct BlockList *bl); -int AddFillValueToSparseImage(struct BlockList *bl, uint32_t value, int64_t size, uint32_t blockIndex); -int AddRawDataToSparseImage(struct BlockList *bl, void *buffer, int64_t size, uint32_t blockIndex); -} // namespace updater -#endif - diff --git a/services/sparse_image/sparse_chunk.cpp b/services/sparse_image/sparse_chunk.cpp deleted file mode 100644 index 54d7589b..00000000 --- a/services/sparse_image/sparse_chunk.cpp +++ /dev/null @@ -1,125 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "sparse_chunk.h" - -#include - -#include "log/log.h" - -namespace updater { -static bool IsValidChunk(uint32_t chunkContentSize, uint32_t chunkSize, uint32_t blockSize) -{ - // should never happen - if (blockSize == 0) { - LOG(ERROR) << "SparseImage: block size is zero"; - return false; - } - // check if chunk content size align with block size. - if (chunkContentSize % blockSize != 0) { - LOG(ERROR) << "SparseImage: invalid chunk " << - " chunk content is not aligned with " << blockSize; - return false; - } - - if (chunkContentSize / blockSize != chunkSize) { - LOG(ERROR) << "SparseImage: invalid chunk " << - " chunk content size is " << chunkContentSize << - " while chunk size record in chunk header is " << chunkSize * blockSize; - return false; - } - return true; -} - -static int HandleRawChunk(struct SparseImage *si, SparseImageHandlerFromBuffer &imageBuffer, - const SparseImageChunkHeader &chunkHeader, const SparseImageHeader &imageHeader, uint32_t currentBlock) -{ - int64_t outputContentSize = chunkHeader.chunkSize * imageHeader.blockSize; - uint32_t chunkContentSize = chunkHeader.totalSize - static_cast(imageHeader.chunkHeaderSize); - - if (!IsValidChunk(chunkContentSize, chunkHeader.chunkSize, imageHeader.blockSize)) { - return -EINVAL; - } - - int ret = imageBuffer.CopyToSparseImage(si, outputContentSize, currentBlock); - if (ret == 0) { - imageBuffer.Seek(outputContentSize); - } - return (ret < 0) ? -EINVAL : 0; -} - -static int HandleFillChunk(struct SparseImage *si, SparseImageHandlerFromBuffer &imageBuffer, - const SparseImageChunkHeader &chunkHeader, const SparseImageHeader &imageHeader, uint32_t currentBlock) -{ - int64_t outputContentSize = chunkHeader.chunkSize * imageHeader.blockSize; - uint32_t chunkContentSize = chunkHeader.totalSize - static_cast(imageHeader.chunkHeaderSize); - uint32_t value; - - if (chunkContentSize != sizeof(value)) { - LOG(ERROR) << "SparseImage: invalid fill chunk"; - return -EINVAL; - } - - int ret = imageBuffer.ReadContent(&value, sizeof(value)); - if (ret < 0) { - LOG(ERROR) << "SparseImage: failed to read value from fill chunk"; - return -EINVAL; - } - - ret = AddFillValueToSparseImage(si->bl, value, outputContentSize, currentBlock); - return (ret < 0) ? -EINVAL : 0; -} - -int HandleChunks(struct SparseImage *si, SparseImageHandlerFromBuffer &imageBuffer, - const SparseImageChunkHeader &chunkHeader, const SparseImageHeader &imageHeader, uint32_t ¤tBlock) -{ - int ret = 0; - switch (chunkHeader.type) { - case CHUNK_TYPE_RAW: - ret = HandleRawChunk(si, imageBuffer, chunkHeader, imageHeader, currentBlock); - if (ret < 0) { - LOG(ERROR) << "SparseImage: handle raw chunk failed"; - return -EINVAL; - } - currentBlock += chunkHeader.chunkSize; - break; - case CHUNK_TYPE_FILL: - ret = HandleFillChunk(si, imageBuffer, chunkHeader, imageHeader, currentBlock); - if (ret < 0) { - LOG(ERROR) << "SparseImage: handle fill chunk failed"; - return -EINVAL; - } - currentBlock += chunkHeader.chunkSize; - break; - case CHUNK_TYPE_DONT_CARE: - { - int64_t chunkContentSize = chunkHeader.totalSize - sizeof(SparseImageChunkHeader); - if (chunkContentSize != 0) { // For don't care chunk, chunk content size should be zero. - LOG(ERROR) << "SparseImage: dont care chunk with non-zero chunk size"; - return -EINVAL; - } - currentBlock += chunkHeader.chunkSize; - break; - } - case CHUNK_TYPE_CRC32: - LOG(WARNING) << "SparseImage: crc32 chunk is unsupported it for now"; - return -EINVAL; - default: - LOG(ERROR) << "SparseImage: unexpected chunk type: " << std::hex << chunkHeader.type << std::dec; - return -EINVAL; - } - return ret; -} -} // namespace updater diff --git a/services/sparse_image/sparse_chunk.h b/services/sparse_image/sparse_chunk.h deleted file mode 100644 index ceb4afc9..00000000 --- a/services/sparse_image/sparse_chunk.h +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef UPDATER_SPARSE_CHUNK_H -#define UPDATER_SPARSE_CHUNK_H -#include - -#include "log/log.h" -#include "sparse_image.h" -#include "sparse_image_handler.h" - -namespace updater { -int HandleChunks(struct SparseImage *si, SparseImageHandlerFromBuffer &imageBuffer, - const SparseImageChunkHeader &chunkHeader, const SparseImageHeader &imageHeader, uint32_t ¤tBlock); -} // namespace updater -#endif // UPDATER_SPARSE_CHUNK_H diff --git a/services/sparse_image/sparse_image.cpp b/services/sparse_image/sparse_image.cpp deleted file mode 100644 index 0c3f89fd..00000000 --- a/services/sparse_image/sparse_image.cpp +++ /dev/null @@ -1,162 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#include "sparse_image.h" -#include -#include "log/log.h" -#include "securec.h" -#include "sparse_chunk.h" -#include "sparse_image_handler.h" - -namespace updater { -// Check if sparse image header is valid. -// returns true is sparse image header is valid, -// otherwise, returns false. -static bool IsValidSparseImageHeader(const SparseImageHeader &header) -{ - if (header.magicNumber != SPARSE_IMAGE_MAGIC) { - LOG(ERROR) << "SparseImage: invalid sparse image magic number"; - return false; - } - - // Only check if major version is expected. - if (header.majorVersion != SPARSE_IMAGE_MAJOR_VERSIOIN) { - LOG(ERROR) << "SparseImage: sparse image with incorrect version"; - return false; - } - - // Check if sparse image size is sufficient - if (header.fileHeaderSize < sizeof(SparseImageHeader)) { - LOG(ERROR) << "SparseImage: sparse image header size is invalid"; - return false; - } - - if (header.chunkHeaderSize < sizeof(SparseImageChunkHeader)) { - LOG(ERROR) << "SparseImage: sparse image chunk header size is invalid"; - return false; - } - - // Everything seems OK. - return true; -} - -static struct SparseImage *AllocateSparseImage(int64_t totalSize, uint32_t blockSize) -{ - struct SparseImage *si = static_cast(calloc(1, sizeof(struct SparseImage))); - if (si == nullptr) { - LOG(ERROR) << "SparseImage: allocate memory for sparse image failed: " << errno; - return nullptr; - } - - si->bl = static_cast(calloc(1, sizeof(struct BlockList))); - if (si->bl == nullptr) { - LOG(ERROR) << "SparseImage: allocate memory for block list failed: " << errno; - free(si); - } - - si->blockSize = blockSize; - si->totalSize = totalSize; - si->bl->blockSize = blockSize; - return si; -} - -void DeallocateSparseImage(struct SparseImage *si) -{ - if (si == nullptr) { - return; - } - DeallocateBlockList(si->bl); - free(si); - si = nullptr; -} - -static int ReadSparseImageFromBuffer(struct SparseImage *si, SparseImageHandlerFromBuffer &imageBuffer) -{ - SparseImageHeader imageHeader {}; - SparseImageChunkHeader chunkHeader {}; - uint32_t currentBlock = 0; - - int ret = imageBuffer.ReadContent(&imageHeader, sizeof(SparseImageHeader)); - if (ret < 0) { - LOG(ERROR) << "SparseImage: Read sparse image header failed"; - return ret; - } - - if (!IsValidSparseImageHeader(imageHeader)) { - return -EINVAL; - } - // check if there is a gab between SparseImageHeader::fileHeaderSize and length of SparseImageHeader - if (imageHeader.fileHeaderSize > sizeof(SparseImageHeader)) { - imageBuffer.Seek(imageHeader.fileHeaderSize - sizeof(SparseImageHeader)); - } - - for (uint32_t i = 0; i < imageHeader.totalChunks; i++) { - ret = imageBuffer.ReadContent(&chunkHeader, sizeof(SparseImageChunkHeader)); - if (ret < 0) { - LOG(ERROR) << "SparseImage: Read sparse image chunk header failed"; - return ret; - } - - if (imageHeader.chunkHeaderSize > sizeof(SparseImageChunkHeader)) { - imageBuffer.Seek(imageHeader.chunkHeaderSize - sizeof(SparseImageChunkHeader)); - } - ret = HandleChunks(si, imageBuffer, chunkHeader, imageHeader, currentBlock); - if (ret < 0) { - return -EINVAL; - } - } - - // Check if all chunks handled - if (imageHeader.totalBlocks != currentBlock) { - LOG(ERROR) << "SparseImage: read sparse image failed: " << " expected " << imageHeader.totalBlocks << - " actual handled blocks: " << currentBlock; - return -EINVAL; - } - return 0; -} - -struct SparseImage *CreateSparseImageFromBuffer(char *buff) -{ - SparseImageHandlerFromBuffer imageBuffer(buff); - SparseImageHeader imageHeader {}; - struct SparseImage *si = nullptr; - - // Read sparse image header from buffer. - int ret = imageBuffer.ReadContent(&imageHeader, sizeof(SparseImageHeader)); - if (ret < 0) { - LOG(ERROR) << "SparseImage: Read sparse image header failed"; - return nullptr; - } - - if (!IsValidSparseImageHeader(imageHeader)) { - return nullptr; - } - - // Well, the sparse image header looks fine. - // Get total size of sparse image. - auto sparseImageSize = static_cast(imageHeader.totalBlocks * imageHeader.blockSize); - si = AllocateSparseImage(sparseImageSize, imageHeader.blockSize); - if (si == nullptr) { - return nullptr; - } - - imageBuffer.SetOffset(0); - ret = ReadSparseImageFromBuffer(si, imageBuffer); - if (ret < 0) { - DeallocateSparseImage(si); - return nullptr; - } - return si; -} -} // namespace updater diff --git a/services/sparse_image/sparse_image.h b/services/sparse_image/sparse_image.h deleted file mode 100644 index f311bbe1..00000000 --- a/services/sparse_image/sparse_image.h +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef UPDATER_SPARSE_IMAGE_H -#define UPDATER_SPARSE_IMAGE_H -#include -#include "sparse_block.h" - -namespace updater { -constexpr uint32_t SPARSE_IMAGE_MAGIC = 0xED26FF3A; -constexpr uint16_t SPARSE_IMAGE_MAJOR_VERSIOIN = 0x1; -constexpr uint16_t SPARSE_IMAGE_MINOR_VERSIOIN = 0x0; - -struct SparseImageHeader { - // Magic number of sparse image is 0xed26ff3a - uint32_t magicNumber; - // major versoin of sparse image is 0x1 - uint16_t majorVersion; - // minor versoin of sparse image is 0x0 - uint16_t minorVersion; - // file header size, 28 bytes - uint16_t fileHeaderSize; - // chunk header size, 12 bytes - uint16_t chunkHeaderSize; - // block size in bytes, default is 4096 bytes - uint32_t blockSize; - // total blocks of non-sparse image. - uint32_t totalBlocks; - // total chunks of sparse image. - uint32_t totalChunks; - // CRC32 checksum of original data. - uint32_t checkSum; -}; - -// chunk type definition -constexpr uint16_t CHUNK_TYPE_RAW = 0xCAC1; -constexpr uint16_t CHUNK_TYPE_FILL = 0xCAC2; -constexpr uint16_t CHUNK_TYPE_DONT_CARE = 0xCAC3; -constexpr uint16_t CHUNK_TYPE_CRC32 = 0xCAC4; - -struct SparseImageChunkHeader { - uint16_t type; // chunk type: raw, fill or don't care - uint16_t reserved1; - uint32_t chunkSize; // size in blocks of output image. - uint32_t totalSize; // size in bytes, include chunk header and data -}; - -struct SparseImage { - // size in bock - unsigned int blockSize; - // total size of sparse image - int64_t totalSize; - // block list - struct BlockList *bl; -}; -} // namespace updater -#endif // UPDATER_SPARSE_IMAGE_H diff --git a/services/sparse_image/sparse_image_handler.h b/services/sparse_image/sparse_image_handler.h deleted file mode 100644 index dd537f68..00000000 --- a/services/sparse_image/sparse_image_handler.h +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef UPDATER_SPARSE_IMAGE_HANDLER_H -#define UPDATER_SPARSE_IMAGE_HANDLER_H -#include - -#include "log/log.h" -#include "securec.h" -#include "sparse_image.h" - -namespace updater { -class ISparseImageHandler { -public: - // Seek sparse image to specific offset. - virtual void Seek(int64_t offset) = 0; - virtual int64_t GetOffset() const = 0; - virtual void SetOffset(int64_t offset) = 0; - virtual int CopyToSparseImage(struct SparseImage *si, int64_t size, uint32_t blockIndex) = 0; - virtual int ReadContent(void *buff, int64_t len) = 0; - virtual ~ISparseImageHandler() {} -}; - -class SparseImageHandlerFromBuffer : public ISparseImageHandler { -public: - explicit SparseImageHandlerFromBuffer(char *buff) : buff_(buff), offset_(0) {} - ~SparseImageHandlerFromBuffer() override {} - void Seek(int64_t offset) override - { - buff_ += offset; - offset_ += offset; - } - - int64_t GetOffset() const override - { - return offset_; - } - - void SetOffset(int64_t offset) override - { - buff_ -= offset_; // Move buff_ back to beginning. - buff_ += offset; - offset_ = offset; - } - - int CopyToSparseImage(struct SparseImage *si, int64_t size, uint32_t blockIndex) override - { - // block list should not be null. - struct BlockList *bl = si->bl; - char *buffer = buff_; - return AddRawDataToSparseImage(bl, buffer, size, blockIndex); - } - - int ReadContent(void *buff, int64_t size) override - { - if (memcpy_s(buff, static_cast(size), buff_, static_cast(size)) != EOK) { - return -EINVAL; - } - Seek(size); - return 0; - } - -private: - char *buff_; - int64_t offset_; -}; -} // namespace updater -#endif // UPDATER_SPARSE_IMAGE_HANDLER_H diff --git a/services/sparse_image/sparse_image_writer.cpp b/services/sparse_image/sparse_image_writer.cpp deleted file mode 100644 index a542af66..00000000 --- a/services/sparse_image/sparse_image_writer.cpp +++ /dev/null @@ -1,262 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#include -#include -#include -#include -#include -#include "log/log.h" -#include "sparse_block.h" -#include "sparse_image.h" - -namespace updater { -#define ROUNDUPWITH(a, b) (((a) + (b) - 1) / (b)) -#define ALIGNWITH(a, b) ((b) * ROUNDUPWITH((a), (b))) -#define MIN(a, b) (((a) < (b)) ? (a) : (b)) - -// When write file block, will map file to memory, -// the mapped memory size should be aligned. -constexpr unsigned int MMAP_ALIGNED_MASK = (4096 - 1); - -static int WriteToFd(int fd, void *data, size_t size) -{ - ssize_t written = 0; - size_t rest = size; - - while (rest > 0) { - written = write(fd, data, rest); - if (written < 0) { - if (errno == EINTR) { - continue; - } - LOG(ERROR) << "SparseImage: write to fd failed: " << errno; - return -1; - } - data = static_cast(data) + written; - rest -= written; - } - return 0; -} - -static int SkipChunks(int fd, int64_t offset) -{ - off64_t rc = 0; - rc = lseek64(fd, offset, SEEK_CUR); - if (rc < 0) { - LOG(ERROR) << "SparseImage: seek file to " << offset << " failed: " << errno; - return -1; - } - return 0; -} - -static int WriteFdBlockToFd(int out, int in, int64_t offset, unsigned int size, unsigned int blockSize) -{ - int64_t alignedOffset = offset & MMAP_ALIGNED_MASK; - int alignedGap = static_cast(offset - alignedOffset); - int64_t mapSize = static_cast(size) + static_cast(alignedGap); - - if (mapSize < 0) { // overflow - LOG(ERROR) << "SparseImage: file size too big. overflow."; - return -1; - } - - char *ptr = static_cast(mmap64(nullptr, mapSize, PROT_READ, MAP_SHARED, in, alignedOffset)); - if (ptr == MAP_FAILED) { - LOG(ERROR) << "SparseImage: map file failed: " << errno; - return -1; - } - - char *p = ptr + alignedGap; - int ret = WriteToFd(out, p, size); - if (ret < 0) { - munmap(ptr, mapSize); - return ret; - } - - // Written data should be aligned with @blockSize. - // if not so, skip reset size. - unsigned int alignedSize = ALIGNWITH(size, blockSize); - if (alignedSize > size) { - ret = SkipChunks(out, alignedSize - size); - if (ret < 0) { - munmap(ptr, mapSize); - return ret; - } - } - munmap(ptr, mapSize); - return 0; -} - -static int WriteFileBlockToFd(int out, const char *fileName, int64_t offset, unsigned int size, unsigned int blockSize) -{ - int in = open(fileName, O_RDONLY); - if (in < 0) { - LOG(ERROR) << "SparseImage: open " << fileName << " failed: " << errno; - return -1; - } - int ret = WriteFdBlockToFd(out, in, offset, size, blockSize); - close(in); - in = -1; - return ret; -} - -static int WriteDataBlockToFd(int fd, void *data, unsigned int size, unsigned int blockSize) -{ - unsigned int alignedSize = ALIGNWITH(size, blockSize); - - int ret = WriteToFd(fd, data, size); - if (ret < 0) { - return ret; - } - // Written data should be aligned with @blockSize. - // if not so, skip reset size. - if (alignedSize > size) { - ret = SkipChunks(fd, alignedSize - size); - if (ret < 0) { - return -1; - } - } - return 0; -} - -static int WriteFillBlockToFd(int fd, uint32_t value, unsigned int size, unsigned int blockSize) -{ - // should never happen - if (blockSize == 0) { - LOG(ERROR) << "SparseImage: block size is zero"; - return -1; - } - auto buffer = static_cast(calloc(blockSize, 1)); - uint32_t toWrite = 0; - if (buffer == nullptr) { - LOG(ERROR) << "SparseImage: allocate memory for fill block failed: " << errno; - return -1; - } - - for (int i = 0; i < static_cast(blockSize / sizeof(uint32_t)); i++) { - buffer[i] = value; - } - - while (size) { - toWrite = MIN(size, blockSize); - int ret = WriteToFd(fd, buffer, toWrite); - if (ret < 0) { - free(buffer); - buffer = nullptr; - return ret; - } - size -= toWrite; - } - free(buffer); - buffer = nullptr; - return 0; -} - -static int WriteBlockToFd(int fd, SparseImageBlock *sib, unsigned int blockSize) -{ - BLOCKTYPE type = sib->type; - int ret = -EINVAL; - switch (type) { - case BLOCK_TYPE_DATA: - ret = WriteDataBlockToFd(fd, sib->data.data, sib->size, blockSize); - if (ret) { - LOG(ERROR) << "SparseImage: write data block to file failed"; - } - break; - case BLOCK_TYPE_FILE: - ret = WriteFileBlockToFd(fd, sib->file.fileName, sib->file.offset, sib->size, blockSize); - if (ret) { - LOG(ERROR) << "SparseImage: write file block to file failed"; - } - break; - case BLOCK_TYPE_FD: - ret = WriteFdBlockToFd(fd, sib->fd.fd, sib->fd.offset, sib->size, blockSize); - if (ret) { - LOG(ERROR) << "SparseImage: write fd block to file failed"; - } - break; - case BLOCK_TYPE_FILL: - ret = WriteFillBlockToFd(fd, sib->fill.value, sib->size, blockSize); - if (ret) { - LOG(ERROR) << "SparseImage: write fill block to file failed"; - } - break; - default: - LOG(ERROR) << "SparseImage: Unsupported block type " << std::hex << type << std::dec; - break; - } - return ret; -} - -// Write Block to fd. -// Write all blocks in sparse image to the -// file bind to fd. -static int WriteBlocksToFd(int fd, struct BlockList *bl, int64_t totalSize) -{ - SparseImageBlock *sib = nullptr; - uint32_t proceedBlocks = 0; - int ret = 0; - - for (sib = bl->imageBlocks; sib != nullptr; sib = sib->next) { - // There is a gap between blocks. - // maybe there are don't care chunks - // Just skip it. - if (sib->blockIndex > proceedBlocks) { - uint32_t skippedBlocks = sib->blockIndex - proceedBlocks; - ret = SkipChunks(fd, skippedBlocks * bl->blockSize); - if (ret < 0) { - LOG(ERROR) << "SparseImage: Skip chunks failed"; - return ret; - } - } - ret = WriteBlockToFd(fd, sib, bl->blockSize); - if (ret < 0) { - return ret; - } - proceedBlocks = sib->blockIndex + ROUNDUPWITH(sib->size, bl->blockSize); - } - - // We've written all blocks - // check if there is a gap between totalSize and proceed blocks - int64_t gap = totalSize - proceedBlocks * bl->blockSize; - ret = SkipChunks(fd, gap); - if (ret < 0) { - LOG(ERROR) << "SparseImage: Skip chunks failed"; - } - return ret; -} - -int SparseImageRestore(int fd, struct SparseImage *si) -{ - if (si == nullptr || fd < 0) { - LOG(ERROR) << "SparseImage: invalid arguments"; - return -1; - } - - // We don't need to check if SparseImage is valid here. - struct BlockList *bl = si->bl; - int ret = WriteBlocksToFd(fd, bl, si->totalSize); - if (ret < 0) { - LOG(ERROR) << "SparseImage: failed to write blocks to fd"; - return -1; - } - // truncate the file to sparse image size. - ret = ftruncate64(fd, si->totalSize); - if (ret < 0) { - LOG(WARNING) << "SparseImage: failed to truncate file: " << errno; - } - return 0; -} -} // namespace updater -- Gitee From a94d5fe3e667f0e6b8ab644501a9d457e0078ea0 Mon Sep 17 00:00:00 2001 From: ma_nan Date: Thu, 29 Jul 2021 19:04:38 +0800 Subject: [PATCH 2/4] delete sparse image Signed-off-by: ma_nan --- services/script/yacc/FlexLexer.h | 220 ------------------------------- 1 file changed, 220 deletions(-) delete mode 100644 services/script/yacc/FlexLexer.h diff --git a/services/script/yacc/FlexLexer.h b/services/script/yacc/FlexLexer.h deleted file mode 100644 index c4dad2b1..00000000 --- a/services/script/yacc/FlexLexer.h +++ /dev/null @@ -1,220 +0,0 @@ -// -*-C++-*- -// FlexLexer.h -- define interfaces for lexical analyzer classes generated -// by flex - -// Copyright (c) 1993 The Regents of the University of California. -// All rights reserved. -// -// This code is derived from software contributed to Berkeley by -// Kent Williams and Tom Epperly. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions -// are met: - -// 1. Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// 2. Redistributions in binary form must reproduce the above copyright -// notice, this list of conditions and the following disclaimer in the -// documentation and/or other materials provided with the distribution. - -// Neither the name of the University nor the names of its contributors -// may be used to endorse or promote products derived from this software -// without specific prior written permission. - -// THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR -// IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED -// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -// PURPOSE. - -// This file defines FlexLexer, an abstract class which specifies the -// external interface provided to flex C++ lexer objects, and yyFlexLexer, -// which defines a particular lexer class. -// -// If you want to create multiple lexer classes, you use the -P flag -// to rename each yyFlexLexer to some other xxFlexLexer. You then -// include in your other sources once per lexer class: -// -// #undef yyFlexLexer -// #define yyFlexLexer xxFlexLexer -// #include -// -// #undef yyFlexLexer -// #define yyFlexLexer zzFlexLexer -// #include -// ... - -#ifndef __FLEX_LEXER_H -// Never included before - need to define base class. -#define __FLEX_LEXER_H - -#include - -extern "C++" { - -struct yy_buffer_state; -typedef int yy_state_type; - -class FlexLexer -{ -public: - virtual ~FlexLexer() { } - - const char* YYText() const { return yytext; } - int YYLeng() const { return yyleng; } - - virtual void - yy_switch_to_buffer( yy_buffer_state* new_buffer ) = 0; - virtual yy_buffer_state* yy_create_buffer( std::istream* s, int size ) = 0; - virtual yy_buffer_state* yy_create_buffer( std::istream& s, int size ) = 0; - virtual void yy_delete_buffer( yy_buffer_state* b ) = 0; - virtual void yyrestart( std::istream* s ) = 0; - virtual void yyrestart( std::istream& s ) = 0; - - virtual int yylex() = 0; - - // Call yylex with new input/output sources. - int yylex( std::istream& new_in, std::ostream& new_out ) - { - switch_streams( new_in, new_out ); - return yylex(); - } - - int yylex( std::istream* new_in, std::ostream* new_out = 0) - { - switch_streams( new_in, new_out ); - return yylex(); - } - - // Switch to new input/output streams. A nil stream pointer - // indicates "keep the current one". - virtual void switch_streams( std::istream* new_in, - std::ostream* new_out ) = 0; - virtual void switch_streams( std::istream& new_in, - std::ostream& new_out ) = 0; - - int lineno() const { return yylineno; } - - int debug() const { return yy_flex_debug; } - void set_debug( int flag ) { yy_flex_debug = flag; } - -protected: - char* yytext; - int yyleng; - int yylineno; // only maintained if you use %option yylineno - int yy_flex_debug; // only has effect with -d or "%option debug" -}; - -} -#endif // FLEXLEXER_H - -#if defined(yyFlexLexer) || ! defined(yyFlexLexerOnce) -// Either this is the first time through (yyFlexLexerOnce not defined), -// or this is a repeated include to define a different flavor of -// yyFlexLexer, as discussed in the flex manual. -# define yyFlexLexerOnce - -extern "C++" { - -class yyFlexLexer : public FlexLexer { -public: - // arg_yyin and arg_yyout default to the cin and cout, but we - // only make that assignment when initializing in yylex(). - yyFlexLexer( std::istream& arg_yyin, std::ostream& arg_yyout ); - yyFlexLexer( std::istream* arg_yyin = 0, std::ostream* arg_yyout = 0 ); -private: - void ctor_common(); - -public: - - virtual ~yyFlexLexer(); - - void yy_switch_to_buffer( yy_buffer_state* new_buffer ); - yy_buffer_state* yy_create_buffer( std::istream* s, int size ); - yy_buffer_state* yy_create_buffer( std::istream& s, int size ); - void yy_delete_buffer( yy_buffer_state* b ); - void yyrestart( std::istream* s ); - void yyrestart( std::istream& s ); - - void yypush_buffer_state( yy_buffer_state* new_buffer ); - void yypop_buffer_state(); - - virtual int yylex(); - virtual void switch_streams( std::istream& new_in, std::ostream& new_out ); - virtual void switch_streams( std::istream* new_in = 0, std::ostream* new_out = 0 ); - virtual int yywrap(); - -protected: - virtual int LexerInput( char* buf, int max_size ); - virtual void LexerOutput( const char* buf, int size ); - virtual void LexerError( const char* msg ); - - void yyunput( int c, char* buf_ptr ); - int yyinput(); - - void yy_load_buffer_state(); - void yy_init_buffer( yy_buffer_state* b, std::istream& s ); - void yy_flush_buffer( yy_buffer_state* b ); - - int yy_start_stack_ptr; - int yy_start_stack_depth; - int* yy_start_stack; - - void yy_push_state( int new_state ); - void yy_pop_state(); - int yy_top_state(); - - yy_state_type yy_get_previous_state(); - yy_state_type yy_try_NUL_trans( yy_state_type current_state ); - int yy_get_next_buffer(); - - std::istream yyin; // input source for default LexerInput - std::ostream yyout; // output sink for default LexerOutput - - // yy_hold_char holds the character lost when yytext is formed. - char yy_hold_char; - - // Number of characters read into yy_ch_buf. - int yy_n_chars; - - // Points to current character in buffer. - char* yy_c_buf_p; - - int yy_init; // whether we need to initialize - int yy_start; // start state number - - // Flag which is used to allow yywrap()'s to do buffer switches - // instead of setting up a fresh yyin. A bit of a hack ... - int yy_did_buffer_switch_on_eof; - - - size_t yy_buffer_stack_top; /**< index of top of stack. */ - size_t yy_buffer_stack_max; /**< capacity of stack. */ - yy_buffer_state ** yy_buffer_stack; /**< Stack as an array. */ - void yyensure_buffer_stack(void); - - // The following are not always needed, but may be depending - // on use of certain flex features (like REJECT or yymore()). - - yy_state_type yy_last_accepting_state; - char* yy_last_accepting_cpos; - - yy_state_type* yy_state_buf; - yy_state_type* yy_state_ptr; - - char* yy_full_match; - int* yy_full_state; - int yy_full_lp; - - int yy_lp; - int yy_looking_for_trail_begin; - - int yy_more_flag; - int yy_more_len; - int yy_more_offset; - int yy_prev_more_offset; -}; - -} - -#endif // yyFlexLexer || ! yyFlexLexerOnce -- Gitee From 3442a4f1f1181a096a9815daaf422e1fe26860b5 Mon Sep 17 00:00:00 2001 From: ma_nan Date: Thu, 29 Jul 2021 19:06:35 +0800 Subject: [PATCH 3/4] delete sparse image Signed-off-by: ma_nan --- services/applypatch/block_writer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/applypatch/block_writer.cpp b/services/applypatch/block_writer.cpp index 8a1326f2..86fec96a 100644 --- a/services/applypatch/block_writer.cpp +++ b/services/applypatch/block_writer.cpp @@ -86,7 +86,7 @@ bool BlockWriter::Write(const uint8_t *addr, size_t len, WriteMode mode, const s if (fsync(fd_) == -1) { LOG(ERROR) << "Failed to fsync "; - return -1; + return false; } return true; -- Gitee From 4deaa7388979bdabfa4431183eda6ef3b8e004c2 Mon Sep 17 00:00:00 2001 From: ma_nan Date: Thu, 29 Jul 2021 19:08:48 +0800 Subject: [PATCH 4/4] delete sparse image Signed-off-by: ma_nan --- services/applypatch/block_writer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/applypatch/block_writer.cpp b/services/applypatch/block_writer.cpp index 86fec96a..88e86e44 100644 --- a/services/applypatch/block_writer.cpp +++ b/services/applypatch/block_writer.cpp @@ -85,7 +85,7 @@ bool BlockWriter::Write(const uint8_t *addr, size_t len, WriteMode mode, const s } if (fsync(fd_) == -1) { - LOG(ERROR) << "Failed to fsync "; + LOG(ERROR) << "Failed to fsync"; return false; } -- Gitee