Neil Fuller | 0891322 | 2015-03-31 18:24:29 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 17 | #include <ctype.h> |
Neil Fuller | 0891322 | 2015-03-31 18:24:29 +0100 | [diff] [blame] | 18 | #include <errno.h> |
| 19 | #include <ftw.h> |
| 20 | #include <libgen.h> |
| 21 | #include <stdarg.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <unistd.h> |
| 26 | #include <iostream> |
| 27 | #include <memory> |
| 28 | #include <string> |
| 29 | #include <vector> |
| 30 | |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 31 | #include "android-base/logging.h" |
Neil Fuller | 0891322 | 2015-03-31 18:24:29 +0100 | [diff] [blame] | 32 | |
Neil Fuller | c65c61b | 2017-02-09 13:47:03 +0000 | [diff] [blame] | 33 | // The name of the file containing the distro version information. |
Neil Fuller | c749c9a | 2017-03-20 10:21:46 +0000 | [diff] [blame] | 34 | // See also libcore.tzdata.shared2.TimeZoneDistro / libcore.tzdata.shared2.DistroVersion. |
Neil Fuller | 16e6487 | 2017-02-09 13:31:57 +0000 | [diff] [blame] | 35 | static const char* DISTRO_VERSION_FILENAME = "/distro_version"; |
Neil Fuller | c65c61b | 2017-02-09 13:47:03 +0000 | [diff] [blame] | 36 | |
Neil Fuller | 16e6487 | 2017-02-09 13:31:57 +0000 | [diff] [blame] | 37 | // distro_version is an ASCII file consisting of 17 bytes in the form: AAA.BBB|CCCCC|DDD |
| 38 | // AAA.BBB is the major/minor version of the distro format (e.g. 001.001), |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 39 | // CCCCC is the rules version (e.g. 2016g) |
Neil Fuller | 16e6487 | 2017-02-09 13:31:57 +0000 | [diff] [blame] | 40 | // DDD is the android revision for this rules version to allow for distro corrections (e.g. 001) |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 41 | // We only need the first 13 to determine if it is suitable for the device. |
Neil Fuller | 16e6487 | 2017-02-09 13:31:57 +0000 | [diff] [blame] | 42 | static const int DISTRO_VERSION_LENGTH = 13; |
Neil Fuller | c65c61b | 2017-02-09 13:47:03 +0000 | [diff] [blame] | 43 | |
Neil Fuller | 16e6487 | 2017-02-09 13:31:57 +0000 | [diff] [blame] | 44 | // The major version of the distro format supported by this code as a null-terminated char[]. |
Neil Fuller | c749c9a | 2017-03-20 10:21:46 +0000 | [diff] [blame] | 45 | // See also libcore.tzdata.shared2.TimeZoneDistro / libcore.tzdata.shared2.DistroVersion. |
Neil Fuller | 16e6487 | 2017-02-09 13:31:57 +0000 | [diff] [blame] | 46 | static const char SUPPORTED_DISTRO_MAJOR_VERSION[] = "001"; |
Neil Fuller | c65c61b | 2017-02-09 13:47:03 +0000 | [diff] [blame] | 47 | |
Neil Fuller | 16e6487 | 2017-02-09 13:31:57 +0000 | [diff] [blame] | 48 | // The length of the distro format major version excluding the \0 |
| 49 | static const size_t SUPPORTED_DISTRO_MAJOR_VERSION_LEN = sizeof(SUPPORTED_DISTRO_MAJOR_VERSION) - 1; |
Neil Fuller | c65c61b | 2017-02-09 13:47:03 +0000 | [diff] [blame] | 50 | |
Neil Fuller | 16e6487 | 2017-02-09 13:31:57 +0000 | [diff] [blame] | 51 | // The minor version of the distro format supported by this code as a null-terminated char[]. |
Neil Fuller | c749c9a | 2017-03-20 10:21:46 +0000 | [diff] [blame] | 52 | // See also libcore.tzdata.shared2.TimeZoneDistro / libcore.tzdata.shared2.DistroVersion. |
Neil Fuller | 16e6487 | 2017-02-09 13:31:57 +0000 | [diff] [blame] | 53 | static const char SUPPORTED_DISTRO_MINOR_VERSION[] = "001"; |
Neil Fuller | c65c61b | 2017-02-09 13:47:03 +0000 | [diff] [blame] | 54 | |
Neil Fuller | 16e6487 | 2017-02-09 13:31:57 +0000 | [diff] [blame] | 55 | // The length of the distro format minor version excluding the \0 |
| 56 | static const size_t SUPPORTED_DISTRO_MINOR_VERSION_LEN = sizeof(SUPPORTED_DISTRO_MINOR_VERSION) - 1; |
Neil Fuller | c65c61b | 2017-02-09 13:47:03 +0000 | [diff] [blame] | 57 | |
Neil Fuller | 16e6487 | 2017-02-09 13:31:57 +0000 | [diff] [blame] | 58 | // The length of the distro format version. e.g. 001.001 |
| 59 | static const size_t SUPPORTED_DISTRO_VERSION_LEN = |
| 60 | SUPPORTED_DISTRO_MAJOR_VERSION_LEN + SUPPORTED_DISTRO_MINOR_VERSION_LEN + 1; |
Neil Fuller | c65c61b | 2017-02-09 13:47:03 +0000 | [diff] [blame] | 61 | |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 62 | // The length of the IANA rules version bytes. e.g. 2016a |
| 63 | static const size_t RULES_VERSION_LEN = 5; |
Neil Fuller | c65c61b | 2017-02-09 13:47:03 +0000 | [diff] [blame] | 64 | |
Neil Fuller | 16e6487 | 2017-02-09 13:31:57 +0000 | [diff] [blame] | 65 | // Distro version bytes are: AAA.BBB|CCCCC - the rules version is CCCCC |
| 66 | static const size_t DISTRO_VERSION_RULES_IDX = 8; |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 67 | |
Neil Fuller | c749c9a | 2017-03-20 10:21:46 +0000 | [diff] [blame] | 68 | // See also libcore.tzdata.shared2.TimeZoneDistro. |
Neil Fuller | 0891322 | 2015-03-31 18:24:29 +0100 | [diff] [blame] | 69 | static const char* TZDATA_FILENAME = "/tzdata"; |
Neil Fuller | c65c61b | 2017-02-09 13:47:03 +0000 | [diff] [blame] | 70 | |
Neil Fuller | 0891322 | 2015-03-31 18:24:29 +0100 | [diff] [blame] | 71 | // tzdata file header (as much as we need for the version): |
| 72 | // byte[11] tzdata_version -- e.g. "tzdata2012f" |
| 73 | static const int TZ_HEADER_LENGTH = 11; |
Neil Fuller | c65c61b | 2017-02-09 13:47:03 +0000 | [diff] [blame] | 74 | |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 75 | static const char TZ_DATA_HEADER_PREFIX[] = "tzdata"; |
| 76 | static const size_t TZ_DATA_HEADER_PREFIX_LEN = sizeof(TZ_DATA_HEADER_PREFIX) - 1; // exclude \0 |
| 77 | |
Neil Fuller | 0891322 | 2015-03-31 18:24:29 +0100 | [diff] [blame] | 78 | |
| 79 | static void usage() { |
| 80 | std::cerr << "Usage: tzdatacheck SYSTEM_TZ_DIR DATA_TZ_DIR\n" |
| 81 | "\n" |
Neil Fuller | 16e6487 | 2017-02-09 13:31:57 +0000 | [diff] [blame] | 82 | "Checks whether any timezone update distro in DATA_TZ_DIR is compatible with the\n" |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 83 | "current Android release and better than or the same as base system timezone rules in\n" |
| 84 | "SYSTEM_TZ_DIR. If the timezone rules in SYSTEM_TZ_DIR are a higher version than the\n" |
| 85 | "one in DATA_TZ_DIR the DATA_TZ_DIR is renamed and then deleted.\n"; |
Neil Fuller | 0891322 | 2015-03-31 18:24:29 +0100 | [diff] [blame] | 86 | exit(1); |
| 87 | } |
| 88 | |
| 89 | /* |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 90 | * Opens a file and fills buffer with the first byteCount bytes from the file. |
| 91 | * If the file does not exist or cannot be opened or is too short then false is returned. |
Neil Fuller | 0891322 | 2015-03-31 18:24:29 +0100 | [diff] [blame] | 92 | * If the bytes were read successfully then true is returned. |
| 93 | */ |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 94 | static bool readBytes(const std::string& fileName, char* buffer, size_t byteCount) { |
| 95 | FILE* file = fopen(fileName.c_str(), "r"); |
| 96 | if (file == nullptr) { |
| 97 | if (errno != ENOENT) { |
| 98 | PLOG(WARNING) << "Error opening file " << fileName; |
Neil Fuller | 0891322 | 2015-03-31 18:24:29 +0100 | [diff] [blame] | 99 | } |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 100 | return false; |
Neil Fuller | 0891322 | 2015-03-31 18:24:29 +0100 | [diff] [blame] | 101 | } |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 102 | size_t bytesRead = fread(buffer, 1, byteCount, file); |
| 103 | fclose(file); |
Neil Fuller | 0891322 | 2015-03-31 18:24:29 +0100 | [diff] [blame] | 104 | if (bytesRead != byteCount) { |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 105 | LOG(WARNING) << fileName << " is too small. " << byteCount << " bytes required"; |
| 106 | return false; |
Neil Fuller | 0891322 | 2015-03-31 18:24:29 +0100 | [diff] [blame] | 107 | } |
Neil Fuller | 0891322 | 2015-03-31 18:24:29 +0100 | [diff] [blame] | 108 | return true; |
| 109 | } |
| 110 | |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 111 | /* |
| 112 | * Checks the contents of headerBytes. Returns true if it is valid (starts with "tzdata"), false |
| 113 | * otherwise. |
| 114 | */ |
| 115 | static bool checkValidTzDataHeader(const std::string& fileName, const char* headerBytes) { |
Neil Fuller | 0891322 | 2015-03-31 18:24:29 +0100 | [diff] [blame] | 116 | if (strncmp("tzdata", headerBytes, 6) != 0) { |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 117 | LOG(WARNING) << fileName << " does not start with the expected bytes (tzdata)"; |
| 118 | return false; |
Neil Fuller | 0891322 | 2015-03-31 18:24:29 +0100 | [diff] [blame] | 119 | } |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 120 | return true; |
| 121 | } |
| 122 | |
| 123 | static bool checkDigits(const char* buffer, const size_t count, size_t* i) { |
| 124 | for (size_t j = 0; j < count; j++) { |
| 125 | char toCheck = buffer[(*i)++]; |
| 126 | if (!isdigit(toCheck)) { |
| 127 | return false; |
| 128 | } |
| 129 | } |
| 130 | return true; |
| 131 | } |
| 132 | |
Neil Fuller | 16e6487 | 2017-02-09 13:31:57 +0000 | [diff] [blame] | 133 | static bool checkValidDistroVersion(const char* buffer) { |
| 134 | // See DISTRO_VERSION_LENGTH comments above for a description of the format. |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 135 | size_t i = 0; |
| 136 | if (!checkDigits(buffer, 3, &i)) { |
| 137 | return false; |
| 138 | } |
| 139 | if (buffer[i++] != '.') { |
| 140 | return false; |
| 141 | } |
| 142 | if (!checkDigits(buffer, 3, &i)) { |
| 143 | return false; |
| 144 | } |
| 145 | if (buffer[i++] != '|') { |
| 146 | return false; |
| 147 | } |
| 148 | if (!checkDigits(buffer, 4, &i)) { |
| 149 | return false; |
| 150 | } |
| 151 | // Ignore the last character. It is assumed to be a letter but we don't check because it's not |
| 152 | // obvious what would happen at 'z'. |
| 153 | return true; |
Neil Fuller | 0891322 | 2015-03-31 18:24:29 +0100 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /* Return the parent directory of dirName. */ |
| 157 | static std::string getParentDir(const std::string& dirName) { |
| 158 | std::unique_ptr<char> mutable_dirname(strdup(dirName.c_str())); |
| 159 | return dirname(mutable_dirname.get()); |
| 160 | } |
| 161 | |
| 162 | /* Deletes a single file, symlink or directory. Called from nftw(). */ |
| 163 | static int deleteFn(const char* fpath, const struct stat*, int typeflag, struct FTW*) { |
| 164 | LOG(DEBUG) << "Inspecting " << fpath; |
| 165 | switch (typeflag) { |
| 166 | case FTW_F: |
| 167 | case FTW_SL: |
| 168 | LOG(DEBUG) << "Unlinking " << fpath; |
| 169 | if (unlink(fpath)) { |
| 170 | PLOG(WARNING) << "Failed to unlink file/symlink " << fpath; |
| 171 | } |
| 172 | break; |
| 173 | case FTW_D: |
| 174 | case FTW_DP: |
| 175 | LOG(DEBUG) << "Removing dir " << fpath; |
| 176 | if (rmdir(fpath)) { |
| 177 | PLOG(WARNING) << "Failed to remove dir " << fpath; |
| 178 | } |
| 179 | break; |
| 180 | default: |
| 181 | LOG(WARNING) << "Unsupported file type " << fpath << ": " << typeflag; |
| 182 | break; |
| 183 | } |
| 184 | return 0; |
| 185 | } |
| 186 | |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 187 | enum PathStatus { ERR, NONE, IS_DIR, NOT_DIR }; |
| 188 | |
| 189 | static PathStatus checkPath(const std::string& path) { |
| 190 | struct stat buf; |
| 191 | if (stat(path.c_str(), &buf) != 0) { |
| 192 | if (errno != ENOENT) { |
| 193 | PLOG(WARNING) << "Unable to stat " << path; |
| 194 | return ERR; |
| 195 | } |
| 196 | return NONE; |
| 197 | } |
| 198 | return S_ISDIR(buf.st_mode) ? IS_DIR : NOT_DIR; |
| 199 | } |
| 200 | |
Neil Fuller | 0891322 | 2015-03-31 18:24:29 +0100 | [diff] [blame] | 201 | /* |
| 202 | * Deletes dirToDelete and returns true if it is successful in removing or moving the directory out |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 203 | * of the way. If dirToDelete does not exist this function does nothing and returns true. If |
| 204 | * dirToDelete is not a directory or cannot be accessed this method returns false. |
Neil Fuller | 0891322 | 2015-03-31 18:24:29 +0100 | [diff] [blame] | 205 | * |
| 206 | * During deletion, this function first renames the directory to a temporary name. If the temporary |
| 207 | * directory cannot be created, or the directory cannot be renamed, false is returned. After the |
| 208 | * rename, deletion of files and subdirs beneath the directory is performed on a "best effort" |
| 209 | * basis. Symlinks beneath the directory are not followed. |
| 210 | */ |
| 211 | static bool deleteDir(const std::string& dirToDelete) { |
| 212 | // Check whether the dir exists. |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 213 | int pathStatus = checkPath(dirToDelete); |
| 214 | if (pathStatus == NONE) { |
| 215 | LOG(INFO) << "Path " << dirToDelete << " does not exist"; |
| 216 | return true; |
| 217 | } |
| 218 | if (pathStatus != IS_DIR) { |
| 219 | LOG(WARNING) << "Path " << dirToDelete << " failed to stat() or is not a directory."; |
Neil Fuller | 0891322 | 2015-03-31 18:24:29 +0100 | [diff] [blame] | 220 | return false; |
Neil Fuller | 0891322 | 2015-03-31 18:24:29 +0100 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | // First, rename dirToDelete. |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 224 | |
Neil Fuller | 0891322 | 2015-03-31 18:24:29 +0100 | [diff] [blame] | 225 | std::string tempDirNameTemplate = getParentDir(dirToDelete); |
| 226 | tempDirNameTemplate += "/tempXXXXXX"; |
| 227 | |
| 228 | // Create an empty directory with the temporary name. For this we need a non-const char*. |
| 229 | std::vector<char> tempDirName(tempDirNameTemplate.length() + 1); |
| 230 | strcpy(&tempDirName[0], tempDirNameTemplate.c_str()); |
| 231 | if (mkdtemp(&tempDirName[0]) == nullptr) { |
| 232 | PLOG(WARNING) << "Unable to create a temporary directory: " << tempDirNameTemplate; |
| 233 | return false; |
| 234 | } |
| 235 | |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 236 | // Rename dirToDelete to tempDirName (replacing the empty tempDirName directory created above). |
Neil Fuller | 0891322 | 2015-03-31 18:24:29 +0100 | [diff] [blame] | 237 | int rc = rename(dirToDelete.c_str(), &tempDirName[0]); |
| 238 | if (rc == -1) { |
| 239 | PLOG(WARNING) << "Unable to rename directory from " << dirToDelete << " to " |
| 240 | << &tempDirName[0]; |
| 241 | return false; |
| 242 | } |
| 243 | |
| 244 | // Recursively delete contents of tempDirName. |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 245 | |
Neil Fuller | 0891322 | 2015-03-31 18:24:29 +0100 | [diff] [blame] | 246 | rc = nftw(&tempDirName[0], deleteFn, 10 /* openFiles */, |
| 247 | FTW_DEPTH | FTW_MOUNT | FTW_PHYS); |
| 248 | if (rc == -1) { |
| 249 | LOG(INFO) << "Could not delete directory: " << &tempDirName[0]; |
| 250 | } |
| 251 | return true; |
| 252 | } |
| 253 | |
| 254 | /* |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 255 | * Deletes the ConfigInstaller metadata directory. |
| 256 | * TODO(nfuller). http://b/31008728 Remove this when ConfigInstaller is no longer used. |
| 257 | */ |
| 258 | static void deleteConfigUpdaterMetadataDir(const char* dataZoneInfoDir) { |
| 259 | // Delete the update metadata |
| 260 | std::string dataUpdatesDirName(dataZoneInfoDir); |
| 261 | dataUpdatesDirName += "/updates"; |
| 262 | LOG(INFO) << "Removing: " << dataUpdatesDirName; |
| 263 | bool deleted = deleteDir(dataUpdatesDirName); |
| 264 | if (!deleted) { |
| 265 | LOG(WARNING) << "Deletion of install metadata " << dataUpdatesDirName |
| 266 | << " was not successful"; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | /* |
Neil Fuller | 16e6487 | 2017-02-09 13:31:57 +0000 | [diff] [blame] | 271 | * Deletes the timezone update distro directory. |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 272 | */ |
Neil Fuller | 16e6487 | 2017-02-09 13:31:57 +0000 | [diff] [blame] | 273 | static void deleteUpdateDistroDir(std::string& distroDirName) { |
| 274 | LOG(INFO) << "Removing: " << distroDirName; |
| 275 | bool deleted = deleteDir(distroDirName); |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 276 | if (!deleted) { |
Neil Fuller | 16e6487 | 2017-02-09 13:31:57 +0000 | [diff] [blame] | 277 | LOG(WARNING) << "Deletion of distro dir " << distroDirName << " was not successful"; |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
| 281 | /* |
Neil Fuller | 0891322 | 2015-03-31 18:24:29 +0100 | [diff] [blame] | 282 | * After a platform update it is likely that timezone data found on the system partition will be |
| 283 | * newer than the version found in the data partition. This tool detects this case and removes the |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 284 | * version in /data. |
Neil Fuller | 0891322 | 2015-03-31 18:24:29 +0100 | [diff] [blame] | 285 | * |
| 286 | * Note: This code is related to code in com.android.server.updates.TzDataInstallReceiver. The |
| 287 | * paths for the metadata and current timezone data must match. |
| 288 | * |
| 289 | * Typically on device the two args will be: |
| 290 | * /system/usr/share/zoneinfo /data/misc/zoneinfo |
| 291 | * |
| 292 | * See usage() for usage notes. |
| 293 | */ |
| 294 | int main(int argc, char* argv[]) { |
| 295 | if (argc != 3) { |
| 296 | usage(); |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 297 | return 1; |
Neil Fuller | 0891322 | 2015-03-31 18:24:29 +0100 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | const char* systemZoneInfoDir = argv[1]; |
| 301 | const char* dataZoneInfoDir = argv[2]; |
| 302 | |
Neil Fuller | 16e6487 | 2017-02-09 13:31:57 +0000 | [diff] [blame] | 303 | // Check the distro directory exists. If it does not, exit quickly: nothing to do. |
Neil Fuller | 0891322 | 2015-03-31 18:24:29 +0100 | [diff] [blame] | 304 | std::string dataCurrentDirName(dataZoneInfoDir); |
| 305 | dataCurrentDirName += "/current"; |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 306 | int dataCurrentDirStatus = checkPath(dataCurrentDirName); |
| 307 | if (dataCurrentDirStatus == NONE) { |
Neil Fuller | 16e6487 | 2017-02-09 13:31:57 +0000 | [diff] [blame] | 308 | LOG(INFO) << "timezone distro dir " << dataCurrentDirName |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 309 | << " does not exist. No action required."; |
Neil Fuller | 0891322 | 2015-03-31 18:24:29 +0100 | [diff] [blame] | 310 | return 0; |
| 311 | } |
Neil Fuller | 16e6487 | 2017-02-09 13:31:57 +0000 | [diff] [blame] | 312 | // If the distro directory path is not a directory or we can't stat() the path, exit with a |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 313 | // warning: either there's a problem accessing storage or the world is not as it should be; |
| 314 | // nothing to do. |
| 315 | if (dataCurrentDirStatus != IS_DIR) { |
Neil Fuller | 16e6487 | 2017-02-09 13:31:57 +0000 | [diff] [blame] | 316 | LOG(WARNING) << "Current distro dir " << dataCurrentDirName |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 317 | << " could not be accessed or is not a directory. result=" << dataCurrentDirStatus; |
| 318 | return 2; |
| 319 | } |
Neil Fuller | 0891322 | 2015-03-31 18:24:29 +0100 | [diff] [blame] | 320 | |
Neil Fuller | 16e6487 | 2017-02-09 13:31:57 +0000 | [diff] [blame] | 321 | // Check the installed distro version. |
| 322 | std::string distroVersionFileName(dataCurrentDirName); |
| 323 | distroVersionFileName += DISTRO_VERSION_FILENAME; |
| 324 | std::vector<char> distroVersion; |
| 325 | distroVersion.reserve(DISTRO_VERSION_LENGTH); |
| 326 | bool distroVersionReadOk = |
| 327 | readBytes(distroVersionFileName, distroVersion.data(), DISTRO_VERSION_LENGTH); |
| 328 | if (!distroVersionReadOk) { |
| 329 | LOG(WARNING) << "distro version file " << distroVersionFileName |
| 330 | << " does not exist or is too short. Deleting distro dir."; |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 331 | // Implies the contents of the data partition is corrupt in some way. Try to clean up. |
| 332 | deleteConfigUpdaterMetadataDir(dataZoneInfoDir); |
Neil Fuller | 16e6487 | 2017-02-09 13:31:57 +0000 | [diff] [blame] | 333 | deleteUpdateDistroDir(dataCurrentDirName); |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 334 | return 3; |
| 335 | } |
| 336 | |
Neil Fuller | 16e6487 | 2017-02-09 13:31:57 +0000 | [diff] [blame] | 337 | if (!checkValidDistroVersion(distroVersion.data())) { |
| 338 | LOG(WARNING) << "distro version file " << distroVersionFileName |
| 339 | << " is not valid. Deleting distro dir."; |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 340 | // Implies the contents of the data partition is corrupt in some way. Try to clean up. |
| 341 | deleteConfigUpdaterMetadataDir(dataZoneInfoDir); |
Neil Fuller | 16e6487 | 2017-02-09 13:31:57 +0000 | [diff] [blame] | 342 | deleteUpdateDistroDir(dataCurrentDirName); |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 343 | return 4; |
| 344 | } |
| 345 | |
Neil Fuller | 16e6487 | 2017-02-09 13:31:57 +0000 | [diff] [blame] | 346 | std::string actualDistroVersion = |
| 347 | std::string(distroVersion.data(), SUPPORTED_DISTRO_VERSION_LEN); |
| 348 | // Check the first 3 bytes of the distro version: these are the major version (e.g. 001). |
Neil Fuller | f54cadb | 2017-02-09 11:53:07 +0000 | [diff] [blame] | 349 | // It must match the one we support exactly to be ok. |
| 350 | if (strncmp( |
Neil Fuller | 16e6487 | 2017-02-09 13:31:57 +0000 | [diff] [blame] | 351 | &distroVersion[0], |
| 352 | SUPPORTED_DISTRO_MAJOR_VERSION, |
| 353 | SUPPORTED_DISTRO_MAJOR_VERSION_LEN) != 0) { |
Neil Fuller | f54cadb | 2017-02-09 11:53:07 +0000 | [diff] [blame] | 354 | |
Neil Fuller | 16e6487 | 2017-02-09 13:31:57 +0000 | [diff] [blame] | 355 | LOG(INFO) << "distro version file " << distroVersionFileName |
| 356 | << " major version is not the required version " << SUPPORTED_DISTRO_MAJOR_VERSION |
| 357 | << ", was \"" << actualDistroVersion << "\". Deleting distro dir."; |
| 358 | // This implies there has been an OTA and the installed distro is not compatible with the |
| 359 | // new version of Android. Remove the installed distro. |
Neil Fuller | f54cadb | 2017-02-09 11:53:07 +0000 | [diff] [blame] | 360 | deleteConfigUpdaterMetadataDir(dataZoneInfoDir); |
Neil Fuller | 16e6487 | 2017-02-09 13:31:57 +0000 | [diff] [blame] | 361 | deleteUpdateDistroDir(dataCurrentDirName); |
Neil Fuller | f54cadb | 2017-02-09 11:53:07 +0000 | [diff] [blame] | 362 | return 5; |
| 363 | } |
| 364 | |
Neil Fuller | 16e6487 | 2017-02-09 13:31:57 +0000 | [diff] [blame] | 365 | // Check the last 3 bytes of the distro version: these are the minor version (e.g. 001). |
| 366 | // If the version in the distro is < the minor version required by this device it cannot be |
Neil Fuller | f54cadb | 2017-02-09 11:53:07 +0000 | [diff] [blame] | 367 | // used. |
| 368 | if (strncmp( |
Neil Fuller | 16e6487 | 2017-02-09 13:31:57 +0000 | [diff] [blame] | 369 | &distroVersion[4], |
| 370 | SUPPORTED_DISTRO_MINOR_VERSION, |
| 371 | SUPPORTED_DISTRO_MINOR_VERSION_LEN) < 0) { |
Neil Fuller | f54cadb | 2017-02-09 11:53:07 +0000 | [diff] [blame] | 372 | |
Neil Fuller | 16e6487 | 2017-02-09 13:31:57 +0000 | [diff] [blame] | 373 | LOG(INFO) << "distro version file " << distroVersionFileName |
| 374 | << " minor version is not the required version " << SUPPORTED_DISTRO_MINOR_VERSION |
| 375 | << ", was \"" << actualDistroVersion << "\". Deleting distro dir."; |
| 376 | // This implies there has been an OTA and the installed distro is not compatible with the |
| 377 | // new version of Android. Remove the installed distro. |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 378 | deleteConfigUpdaterMetadataDir(dataZoneInfoDir); |
Neil Fuller | 16e6487 | 2017-02-09 13:31:57 +0000 | [diff] [blame] | 379 | deleteUpdateDistroDir(dataCurrentDirName); |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 380 | return 5; |
| 381 | } |
| 382 | |
| 383 | // Read the system rules version out of the /system tzdata file. |
Neil Fuller | 0891322 | 2015-03-31 18:24:29 +0100 | [diff] [blame] | 384 | std::string systemTzDataFileName(systemZoneInfoDir); |
| 385 | systemTzDataFileName += TZDATA_FILENAME; |
| 386 | std::vector<char> systemTzDataHeader; |
| 387 | systemTzDataHeader.reserve(TZ_HEADER_LENGTH); |
| 388 | bool systemFileExists = |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 389 | readBytes(systemTzDataFileName, systemTzDataHeader.data(), TZ_HEADER_LENGTH); |
Neil Fuller | 0891322 | 2015-03-31 18:24:29 +0100 | [diff] [blame] | 390 | if (!systemFileExists) { |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 391 | // Implies the contents of the system partition is corrupt in some way. Nothing we can do. |
| 392 | LOG(WARNING) << systemTzDataFileName << " does not exist or could not be opened"; |
| 393 | return 6; |
Neil Fuller | 0891322 | 2015-03-31 18:24:29 +0100 | [diff] [blame] | 394 | } |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 395 | if (!checkValidTzDataHeader(systemTzDataFileName, systemTzDataHeader.data())) { |
| 396 | // Implies the contents of the system partition is corrupt in some way. Nothing we can do. |
| 397 | LOG(WARNING) << systemTzDataFileName << " does not have a valid header."; |
| 398 | return 7; |
Neil Fuller | 0891322 | 2015-03-31 18:24:29 +0100 | [diff] [blame] | 399 | } |
| 400 | |
Neil Fuller | 16e6487 | 2017-02-09 13:31:57 +0000 | [diff] [blame] | 401 | // Compare the distro rules version against the system rules version. |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 402 | if (strncmp( |
| 403 | &systemTzDataHeader[TZ_DATA_HEADER_PREFIX_LEN], |
Neil Fuller | 16e6487 | 2017-02-09 13:31:57 +0000 | [diff] [blame] | 404 | &distroVersion[DISTRO_VERSION_RULES_IDX], |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 405 | RULES_VERSION_LEN) <= 0) { |
Neil Fuller | 16e6487 | 2017-02-09 13:31:57 +0000 | [diff] [blame] | 406 | LOG(INFO) << "Found an installed distro but it is valid. No action taken."; |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 407 | // Implies there is an installed update, but it is good. |
| 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | // Implies there has been an OTA and the system version of the timezone rules is now newer |
Neil Fuller | 16e6487 | 2017-02-09 13:31:57 +0000 | [diff] [blame] | 412 | // than the version installed in /data. Remove the installed distro. |
| 413 | LOG(INFO) << "timezone distro in " << dataCurrentDirName << " is older than data in " |
Neil Fuller | eec2bfb | 2016-12-13 16:26:02 +0000 | [diff] [blame] | 414 | << systemTzDataFileName << "; fixing..."; |
| 415 | |
| 416 | deleteConfigUpdaterMetadataDir(dataZoneInfoDir); |
Neil Fuller | 16e6487 | 2017-02-09 13:31:57 +0000 | [diff] [blame] | 417 | deleteUpdateDistroDir(dataCurrentDirName); |
Neil Fuller | 0891322 | 2015-03-31 18:24:29 +0100 | [diff] [blame] | 418 | return 0; |
| 419 | } |