1 /* 2 * Copyright (c) 2025-2025 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 OHOS_FORM_FWK_FILE_UTILS_H 17 #define OHOS_FORM_FWK_FILE_UTILS_H 18 19 #include <fstream> 20 #include <iostream> 21 #include <string> 22 #include <cstdlib> 23 #include <climits> 24 #include "fms_log_wrapper.h" 25 26 namespace OHOS { 27 namespace AppExecFwk { 28 class FileUtils { 29 public: IsFileExists(const std::string & fileName)30 static bool IsFileExists(const std::string &fileName) 31 { 32 char canonicalPath[PATH_MAX] = { '\0' }; 33 if (realpath(fileName.c_str(), canonicalPath) == nullptr) { 34 HILOG_ERROR("canonicalPath is null"); 35 return false; 36 } 37 canonicalPath[PATH_MAX - 1] = '\0'; 38 std::ifstream file(canonicalPath); 39 if (!file.good()) { 40 HILOG_WARN("file not exists: %{public}s", fileName.c_str()); 41 return false; 42 } 43 return true; 44 }; 45 }; 46 } // namespace AppExecFwk 47 } // namespace OHOS 48 #endif // OHOS_FORM_FWK_FILE_UTILS_H