13f4cbf05Sopenharmony_ci/* 23f4cbf05Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 33f4cbf05Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 43f4cbf05Sopenharmony_ci * you may not use this file except in compliance with the License. 53f4cbf05Sopenharmony_ci * You may obtain a copy of the License at 63f4cbf05Sopenharmony_ci * 73f4cbf05Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 83f4cbf05Sopenharmony_ci * 93f4cbf05Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 103f4cbf05Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 113f4cbf05Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 123f4cbf05Sopenharmony_ci * See the License for the specific language governing permissions and 133f4cbf05Sopenharmony_ci * limitations under the License. 143f4cbf05Sopenharmony_ci */ 153f4cbf05Sopenharmony_ci 163f4cbf05Sopenharmony_ci#ifndef DIRECTORY_EX_H 173f4cbf05Sopenharmony_ci#define DIRECTORY_EX_H 183f4cbf05Sopenharmony_ci 193f4cbf05Sopenharmony_ci#include <string> 203f4cbf05Sopenharmony_ci#include <vector> 213f4cbf05Sopenharmony_ci#include <sys/stat.h> 223f4cbf05Sopenharmony_ci#ifdef UTILS_CXX_RUST 233f4cbf05Sopenharmony_ci#include "cxx.h" 243f4cbf05Sopenharmony_ci#endif 253f4cbf05Sopenharmony_ci 263f4cbf05Sopenharmony_cinamespace OHOS { 273f4cbf05Sopenharmony_ci 283f4cbf05Sopenharmony_ci#ifdef UTILS_CXX_RUST 293f4cbf05Sopenharmony_cirust::String RustGetCurrentProcFullFileName(); 303f4cbf05Sopenharmony_cirust::String RustGetCurrentProcPath(); 313f4cbf05Sopenharmony_cirust::String RustExtractFilePath(const rust::String& fileFullName); 323f4cbf05Sopenharmony_cirust::String RustExtractFileName(const rust::String& fileFullName); 333f4cbf05Sopenharmony_cirust::String RustExtractFileExt(const rust::String& fileName); 343f4cbf05Sopenharmony_cirust::String RustExcludeTrailingPathDelimiter(const rust::String& path); 353f4cbf05Sopenharmony_cirust::String RustIncludeTrailingPathDelimiter(const rust::String& path); 363f4cbf05Sopenharmony_cibool RustPathToRealPath(const rust::String& path, rust::String& realPath); 373f4cbf05Sopenharmony_civoid RustGetDirFiles(const rust::String& path, rust::vec<rust::String>& files); 383f4cbf05Sopenharmony_ci#endif 393f4cbf05Sopenharmony_ci 403f4cbf05Sopenharmony_ci/** 413f4cbf05Sopenharmony_ci * @brief Obtains the full absolute path of this program. 423f4cbf05Sopenharmony_ci * 433f4cbf05Sopenharmony_ci * <b>/proc/self/exe</b> indicates the program, and you can obtain its absolute 443f4cbf05Sopenharmony_ci * path by using readlink(). 453f4cbf05Sopenharmony_ci */ 463f4cbf05Sopenharmony_cistd::string GetCurrentProcFullFileName(); 473f4cbf05Sopenharmony_ci 483f4cbf05Sopenharmony_ci/** 493f4cbf05Sopenharmony_ci * @brief Obtains the absolute path of this program. 503f4cbf05Sopenharmony_ci */ 513f4cbf05Sopenharmony_cistd::string GetCurrentProcPath(); 523f4cbf05Sopenharmony_ci 533f4cbf05Sopenharmony_ci/** 543f4cbf05Sopenharmony_ci * @brief Obtains the path of a file based on the full path. 553f4cbf05Sopenharmony_ci */ 563f4cbf05Sopenharmony_cistd::string ExtractFilePath(const std::string& fileFullName); 573f4cbf05Sopenharmony_ci 583f4cbf05Sopenharmony_ci/** 593f4cbf05Sopenharmony_ci * @brief Obtains the name of a file based on the full path. 603f4cbf05Sopenharmony_ci */ 613f4cbf05Sopenharmony_cistd::string ExtractFileName(const std::string& fileFullName); 623f4cbf05Sopenharmony_ci 633f4cbf05Sopenharmony_ci/** 643f4cbf05Sopenharmony_ci * @brief Obtains the filename extension based on the full path. 653f4cbf05Sopenharmony_ci * 663f4cbf05Sopenharmony_ci */ 673f4cbf05Sopenharmony_cistd::string ExtractFileExt(const std::string& fileName); 683f4cbf05Sopenharmony_ci 693f4cbf05Sopenharmony_ci/** 703f4cbf05Sopenharmony_ci * @brief Excludes the trailing path delimiter '/' from the <b>strPath</b>. 713f4cbf05Sopenharmony_ci * 723f4cbf05Sopenharmony_ci * If the path ends with '/', returns the path after removing '/'. 733f4cbf05Sopenharmony_ci * Otherwise, returns the path directly. 743f4cbf05Sopenharmony_ci */ 753f4cbf05Sopenharmony_cistd::string ExcludeTrailingPathDelimiter(const std::string& path); 763f4cbf05Sopenharmony_ci 773f4cbf05Sopenharmony_ci/** 783f4cbf05Sopenharmony_ci * @brief Includes the trailing path delimiter '/' in the <b>strPath</b>. 793f4cbf05Sopenharmony_ci * 803f4cbf05Sopenharmony_ci * If the path ends with "/", returns the path. 813f4cbf05Sopenharmony_ci * Otherwise, returns the path with an appended delimiter. 823f4cbf05Sopenharmony_ci */ 833f4cbf05Sopenharmony_cistd::string IncludeTrailingPathDelimiter(const std::string& path); 843f4cbf05Sopenharmony_ci 853f4cbf05Sopenharmony_ci/** 863f4cbf05Sopenharmony_ci * @brief Obtains the names of all files in the specified directory recursively. 873f4cbf05Sopenharmony_ci * 883f4cbf05Sopenharmony_ci * @param path Indicates the target directory. 893f4cbf05Sopenharmony_ci * @param files Indicates the <b>std::vector</b> to store the file names. 903f4cbf05Sopenharmony_ci */ 913f4cbf05Sopenharmony_civoid GetDirFiles(const std::string& path, std::vector<std::string>& files); 923f4cbf05Sopenharmony_ci 933f4cbf05Sopenharmony_ci/** 943f4cbf05Sopenharmony_ci * @brief Checks whether a folder is empty. 953f4cbf05Sopenharmony_ci * 963f4cbf05Sopenharmony_ci * @return Returns <b>true</b> if the folder is empty; 973f4cbf05Sopenharmony_ci * returns <b>false</b> otherwise. 983f4cbf05Sopenharmony_ci */ 993f4cbf05Sopenharmony_cibool IsEmptyFolder(const std::string& path); 1003f4cbf05Sopenharmony_ci 1013f4cbf05Sopenharmony_ci/** 1023f4cbf05Sopenharmony_ci * @brief Creates a directory recursively. 1033f4cbf05Sopenharmony_ci * 1043f4cbf05Sopenharmony_ci * The parent directory can be created at the same time when it does not exist. 1053f4cbf05Sopenharmony_ci * 1063f4cbf05Sopenharmony_ci * @note If there are errors such as 'Permission Denied', the creation may 1073f4cbf05Sopenharmony_ci * also fail. 1083f4cbf05Sopenharmony_ci * @return Returns <b>true</b> if the directory is created; 1093f4cbf05Sopenharmony_ci * returns <b>false</b> otherwise. 1103f4cbf05Sopenharmony_ci */ 1113f4cbf05Sopenharmony_cibool ForceCreateDirectory(const std::string& path); 1123f4cbf05Sopenharmony_ci 1133f4cbf05Sopenharmony_ci/** 1143f4cbf05Sopenharmony_ci * @brief Deletes a directory. 1153f4cbf05Sopenharmony_ci * 1163f4cbf05Sopenharmony_ci * All subdirectories and files in the specified directory will also be deleted. 1173f4cbf05Sopenharmony_ci * 1183f4cbf05Sopenharmony_ci * @note It is not necessarily successful to delete. 1193f4cbf05Sopenharmony_ci * @note If there are errors such as 'Permission Denied', the deletion may 1203f4cbf05Sopenharmony_ci * also fail. 1213f4cbf05Sopenharmony_ci * @return Returns <b>true</b> if the directory is deleted; 1223f4cbf05Sopenharmony_ci * returns <b>false</b> otherwise. 1233f4cbf05Sopenharmony_ci */ 1243f4cbf05Sopenharmony_cibool ForceRemoveDirectory(const std::string& path); 1253f4cbf05Sopenharmony_ci 1263f4cbf05Sopenharmony_ci/** 1273f4cbf05Sopenharmony_ci * @brief Removes the file specified by <b>fileName</b>. 1283f4cbf05Sopenharmony_ci * 1293f4cbf05Sopenharmony_ci * @return Returns <b>true</b> if the file is removed; 1303f4cbf05Sopenharmony_ci * returns <b>false</b> otherwise. 1313f4cbf05Sopenharmony_ci */ 1323f4cbf05Sopenharmony_cibool RemoveFile(const std::string& fileName); 1333f4cbf05Sopenharmony_ci 1343f4cbf05Sopenharmony_ci/** 1353f4cbf05Sopenharmony_ci * @brief Obtains the folder size, in bytes. 1363f4cbf05Sopenharmony_ci */ 1373f4cbf05Sopenharmony_ciuint64_t GetFolderSize(const std::string& path); 1383f4cbf05Sopenharmony_ci 1393f4cbf05Sopenharmony_ci/** 1403f4cbf05Sopenharmony_ci * @brief Changes the access permissions on a file. 1413f4cbf05Sopenharmony_ci * 1423f4cbf05Sopenharmony_ci * @param mode Indicates the permissions on the file. 1433f4cbf05Sopenharmony_ci * For details, see <b>chmod()</b>. 1443f4cbf05Sopenharmony_ci * @return Returns <b>true</b> if the permissions are changed; 1453f4cbf05Sopenharmony_ci * returns <b>false</b> otherwise. 1463f4cbf05Sopenharmony_ci */ 1473f4cbf05Sopenharmony_cibool ChangeModeFile(const std::string& fileName, const mode_t& mode); 1483f4cbf05Sopenharmony_ci 1493f4cbf05Sopenharmony_ci/** 1503f4cbf05Sopenharmony_ci * @brief Changes the access permissions on a directory and all its 1513f4cbf05Sopenharmony_ci * subdirectories. 1523f4cbf05Sopenharmony_ci * 1533f4cbf05Sopenharmony_ci * @param mode Indicates the permissions. For details, see <b>chmod()</b>. 1543f4cbf05Sopenharmony_ci * @return Returns <b>true</b> if the permissions are changed; 1553f4cbf05Sopenharmony_ci * returns <b>false</b> otherwise. 1563f4cbf05Sopenharmony_ci */ 1573f4cbf05Sopenharmony_cibool ChangeModeDirectory(const std::string& path, const mode_t& mode); 1583f4cbf05Sopenharmony_ci 1593f4cbf05Sopenharmony_ci/** 1603f4cbf05Sopenharmony_ci * @brief Obtains the real path from a relative path. 1613f4cbf05Sopenharmony_ci * 1623f4cbf05Sopenharmony_ci * @return Returns <b>true</b> if the real path is obtained; 1633f4cbf05Sopenharmony_ci * returns <b>false</b> otherwise. 1643f4cbf05Sopenharmony_ci */ 1653f4cbf05Sopenharmony_cibool PathToRealPath(const std::string& path, std::string& realPath); 1663f4cbf05Sopenharmony_ci 1673f4cbf05Sopenharmony_ci#if defined(IOS_PLATFORM) || defined(_WIN32) 1683f4cbf05Sopenharmony_ci/** 1693f4cbf05Sopenharmony_ci * @brief Transforms a file name to that for Windows or macOS. 1703f4cbf05Sopenharmony_ci */ 1713f4cbf05Sopenharmony_cistd::string TransformFileName(const std::string& fileName); 1723f4cbf05Sopenharmony_ci#endif 1733f4cbf05Sopenharmony_ci} // OHOS 1743f4cbf05Sopenharmony_ci#endif