1 // Copyright (C) 2023 Google LLC 2 // 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 #ifndef ICING_FILE_VERSION_UTIL_H_ 16 #define ICING_FILE_VERSION_UTIL_H_ 17 18 #include <cstdint> 19 #include <string> 20 21 #include "icing/text_classifier/lib3/utils/base/status.h" 22 #include "icing/text_classifier/lib3/utils/base/statusor.h" 23 #include "icing/file/filesystem.h" 24 25 namespace icing { 26 namespace lib { 27 28 namespace version_util { 29 30 // - Version 0: Android T. Can be identified only by flash index magic. 31 // - Version 1: mainline release 2023-06. 32 inline static constexpr int32_t kVersion = 1; 33 inline static constexpr int32_t kVersionOne = 1; 34 35 inline static constexpr int kVersionZeroFlashIndexMagic = 0x6dfba6ae; 36 37 struct VersionInfo { 38 int32_t version; 39 int32_t max_version; 40 VersionInfoVersionInfo41 explicit VersionInfo(int32_t version_in, int32_t max_version_in) 42 : version(version_in), max_version(max_version_in) {} 43 IsValidVersionInfo44 bool IsValid() const { return version >= 0 && max_version >= 0; } 45 46 bool operator==(const VersionInfo& other) const { 47 return version == other.version && max_version == other.max_version; 48 } 49 } __attribute__((packed)); 50 static_assert(sizeof(VersionInfo) == 8, ""); 51 52 enum class StateChange { 53 kUndetermined, 54 kCompatible, 55 kRollForward, 56 kRollBack, 57 kUpgrade, 58 kVersionZeroUpgrade, 59 kVersionZeroRollForward, 60 }; 61 62 // Helper method to read version info (using version file and flash index header 63 // magic) from the existing data. If the state is invalid (e.g. flash index 64 // header file is missing), then return an invalid VersionInfo. 65 // 66 // RETURNS: 67 // - Existing data's VersionInfo on success 68 // - INTERNAL_ERROR on I/O errors 69 libtextclassifier3::StatusOr<VersionInfo> ReadVersion( 70 const Filesystem& filesystem, const std::string& version_file_path, 71 const std::string& index_base_dir); 72 73 // Helper method to write version file. 74 // 75 // RETURNS: 76 // - OK on success 77 // - INTERNAL_ERROR on I/O errors 78 libtextclassifier3::Status WriteVersion(const Filesystem& filesystem, 79 const std::string& version_file_path, 80 const VersionInfo& version_info); 81 82 // Helper method to determine the change state between the existing data version 83 // and the current code version. 84 // 85 // REQUIRES: curr_version > 0. We implement version checking in version 1, so 86 // the callers (except unit tests) will always use a version # greater than 0. 87 // 88 // RETURNS: StateChange 89 StateChange GetVersionStateChange(const VersionInfo& existing_version_info, 90 int32_t curr_version = kVersion); 91 92 } // namespace version_util 93 94 } // namespace lib 95 } // namespace icing 96 97 #endif // ICING_FILE_VERSION_UTIL_H_ 98