1fb299fa2Sopenharmony_ci/* 2fb299fa2Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 3fb299fa2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4fb299fa2Sopenharmony_ci * you may not use this file except in compliance with the License. 5fb299fa2Sopenharmony_ci * You may obtain a copy of the License at 6fb299fa2Sopenharmony_ci * 7fb299fa2Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8fb299fa2Sopenharmony_ci * 9fb299fa2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10fb299fa2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11fb299fa2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12fb299fa2Sopenharmony_ci * See the License for the specific language governing permissions and 13fb299fa2Sopenharmony_ci * limitations under the License. 14fb299fa2Sopenharmony_ci */ 15fb299fa2Sopenharmony_ci 16fb299fa2Sopenharmony_ci#include "utils_common.h" 17fb299fa2Sopenharmony_ci#include <algorithm> 18fb299fa2Sopenharmony_ci#include <cerrno> 19fb299fa2Sopenharmony_ci#include <cstdint> 20fb299fa2Sopenharmony_ci#include <cstdlib> 21fb299fa2Sopenharmony_ci#include <dirent.h> 22fb299fa2Sopenharmony_ci#include <fcntl.h> 23fb299fa2Sopenharmony_ci#include <limits> 24fb299fa2Sopenharmony_ci#include <linux/reboot.h> 25fb299fa2Sopenharmony_ci#include <string> 26fb299fa2Sopenharmony_ci#include <sys/reboot.h> 27fb299fa2Sopenharmony_ci#include <sys/stat.h> 28fb299fa2Sopenharmony_ci#include <sys/syscall.h> 29fb299fa2Sopenharmony_ci#include <unistd.h> 30fb299fa2Sopenharmony_ci#include <vector> 31fb299fa2Sopenharmony_ci#include "log/log.h" 32fb299fa2Sopenharmony_ci 33fb299fa2Sopenharmony_cinamespace Updater { 34fb299fa2Sopenharmony_cinamespace Utils { 35fb299fa2Sopenharmony_ciconstexpr int USECONDS_PER_SECONDS = 1000000; // 1s = 1000000us 36fb299fa2Sopenharmony_ciconstexpr int NANOSECS_PER_USECONDS = 1000; // 1us = 1000ns 37fb299fa2Sopenharmony_ci 38fb299fa2Sopenharmony_cibool PathToRealPath(const std::string &path, std::string &realPath) 39fb299fa2Sopenharmony_ci{ 40fb299fa2Sopenharmony_ci if (path.empty()) { 41fb299fa2Sopenharmony_ci LOG(ERROR) << "path is empty!"; 42fb299fa2Sopenharmony_ci return false; 43fb299fa2Sopenharmony_ci } 44fb299fa2Sopenharmony_ci 45fb299fa2Sopenharmony_ci if ((path.length() >= PATH_MAX)) { 46fb299fa2Sopenharmony_ci LOG(ERROR) << "path len is error, the len is: " << path.length(); 47fb299fa2Sopenharmony_ci return false; 48fb299fa2Sopenharmony_ci } 49fb299fa2Sopenharmony_ci 50fb299fa2Sopenharmony_ci char tmpPath[PATH_MAX] = {0}; 51fb299fa2Sopenharmony_ci if (realpath(path.c_str(), tmpPath) == nullptr) { 52fb299fa2Sopenharmony_ci LOG(ERROR) << "path to realpath error " << path; 53fb299fa2Sopenharmony_ci return false; 54fb299fa2Sopenharmony_ci } 55fb299fa2Sopenharmony_ci 56fb299fa2Sopenharmony_ci realPath = tmpPath; 57fb299fa2Sopenharmony_ci return true; 58fb299fa2Sopenharmony_ci} 59fb299fa2Sopenharmony_ci 60fb299fa2Sopenharmony_civoid UsSleep(int usec) 61fb299fa2Sopenharmony_ci{ 62fb299fa2Sopenharmony_ci auto seconds = usec / USECONDS_PER_SECONDS; 63fb299fa2Sopenharmony_ci long nanoSeconds = static_cast<long>(usec) % USECONDS_PER_SECONDS * NANOSECS_PER_USECONDS; 64fb299fa2Sopenharmony_ci struct timespec ts = { static_cast<time_t>(seconds), nanoSeconds }; 65fb299fa2Sopenharmony_ci while (nanosleep(&ts, &ts) < 0 && errno == EINTR) { 66fb299fa2Sopenharmony_ci } 67fb299fa2Sopenharmony_ci} 68fb299fa2Sopenharmony_ci 69fb299fa2Sopenharmony_cibool IsUpdaterMode() 70fb299fa2Sopenharmony_ci{ 71fb299fa2Sopenharmony_ci struct stat st {}; 72fb299fa2Sopenharmony_ci if (stat("/bin/updater", &st) == 0 && S_ISREG(st.st_mode)) { 73fb299fa2Sopenharmony_ci LOG(INFO) << "updater mode"; 74fb299fa2Sopenharmony_ci return true; 75fb299fa2Sopenharmony_ci } 76fb299fa2Sopenharmony_ci LOG(INFO) << "normal mode"; 77fb299fa2Sopenharmony_ci return false; 78fb299fa2Sopenharmony_ci} 79fb299fa2Sopenharmony_ci} // Utils 80fb299fa2Sopenharmony_ci} // updater 81