diff --git a/include/cmd_parser.h b/include/cmd_parser.h index 299991b..61d729a 100644 --- a/include/cmd_parser.h +++ b/include/cmd_parser.h @@ -69,6 +69,7 @@ private: uint32_t AddConfig(const std::string& argValue); uint32_t AddStartId(const std::string& argValue); uint32_t AddCachePath(const std::string& argValue); + void AdaptResourcesDirForInput(); uint32_t CheckParam() const; uint32_t HandleProcess(int c, const std::string& argValue); uint32_t ParseFileList(const std::string& fileListPath); diff --git a/include/file_entry.h b/include/file_entry.h index 5419d9d..b73bbde 100644 --- a/include/file_entry.h +++ b/include/file_entry.h @@ -57,7 +57,7 @@ public: static bool CopyFileInner(const std::string &src, const std::string &dst); static bool IsDirectory(const std::string &path); static std::string RealPath(const std::string &path); - static std::string AdapateLongPath(const std::string &path); + static std::string AdaptLongPath(const std::string &path); private: bool IsIgnore(const std::string &filename) const; diff --git a/include/resource_util.h b/include/resource_util.h index d121f99..582bb19 100644 --- a/include/resource_util.h +++ b/include/resource_util.h @@ -16,9 +16,10 @@ #ifndef OHOS_RESTOOL_RESOURCE_UTIL_H #define OHOS_RESTOOL_RESOURCE_UTIL_H -#include -#include "resource_data.h" +#include +#include "file_entry.h" #include "json/json.h" +#include "resource_data.h" namespace OHOS { namespace Global { @@ -192,6 +193,21 @@ public: * @return All restype string */ static std::string GetAllRestypeString(); + + /** + * @brief get \base\element dir + * @param string inputpath + * @return resource\base\element dir + */ + static FileEntry::FilePath GetBaseElementPath(const std::string input); + + /** + * @brief get main dir + * @param string inputpath + * @return main dir + */ + static FileEntry::FilePath GetMainPath(const std::string input); + private: enum class IgnoreType { IGNORE_FILE, diff --git a/src/cmd_parser.cpp b/src/cmd_parser.cpp index 2872bda..4d0ed64 100644 --- a/src/cmd_parser.cpp +++ b/src/cmd_parser.cpp @@ -50,7 +50,11 @@ uint32_t PackageParser::Parse(int argc, char *argv[]) if (ParseCommand(argc, argv) != RESTOOL_SUCCESS) { return RESTOOL_ERROR; } - return CheckParam(); + if (CheckParam() != RESTOOL_SUCCESS) { + return RESTOOL_ERROR; + } + AdaptResourcesDirForInput(); + return RESTOOL_SUCCESS; } const vector &PackageParser::GetInputs() const @@ -220,6 +224,16 @@ uint32_t PackageParser::AddCachePath(const string& argValue) return RESTOOL_SUCCESS; } +// -i input directory, add the resource directory +void PackageParser::AdaptResourcesDirForInput() +{ + if (!isFileList_ && !combine_) { // -l and increment compile -i, no need to add resource directory + for (auto &path : inputs_) { + path = FileEntry::FilePath(path).Append(RESOURCES_DIR).GetPath(); + } + } +} + uint32_t PackageParser::CheckParam() const { if (inputs_.empty() && append_.empty()) { diff --git a/src/file_entry.cpp b/src/file_entry.cpp index 260b2bc..064feaf 100644 --- a/src/file_entry.cpp +++ b/src/file_entry.cpp @@ -64,7 +64,7 @@ const vector> FileEntry::GetChilds() const #ifdef _WIN32 WIN32_FIND_DATA findData; string temp(filePath + "\\*.*"); - HANDLE handle = FindFirstFile(AdapateLongPath(temp).c_str(), &findData); + HANDLE handle = FindFirstFile(AdaptLongPath(temp).c_str(), &findData); if (handle == INVALID_HANDLE_VALUE) { return children; } @@ -113,7 +113,7 @@ const FileEntry::FilePath &FileEntry::GetFilePath() const bool FileEntry::Exist(const string &path) { #ifdef _WIN32 - return PathFileExists(AdapateLongPath(path).c_str()); + return PathFileExists(AdaptLongPath(path).c_str()); #else struct stat s; if (stat(path.c_str(), &s) != 0) { @@ -145,7 +145,7 @@ bool FileEntry::CreateDirs(const string &path) bool FileEntry::CopyFileInner(const string &src, const string &dst) { #ifdef _WIN32 - if (!CopyFile(AdapateLongPath(src).c_str(), AdapateLongPath(dst).c_str(), false)) { + if (!CopyFile(AdaptLongPath(src).c_str(), AdaptLongPath(dst).c_str(), false)) { cerr << "Error: copy file fail from '" << src << "' to '" << dst << "'. reason:" << strerror(errno) << endl; return false; } @@ -164,7 +164,7 @@ bool FileEntry::CopyFileInner(const string &src, const string &dst) bool FileEntry::IsDirectory(const string &path) { #ifdef _WIN32 - if (!PathIsDirectory(AdapateLongPath(path).c_str())) { + if (!PathIsDirectory(AdaptLongPath(path).c_str())) { return false; } return true; @@ -179,7 +179,7 @@ string FileEntry::RealPath(const string &path) { #ifdef _WIN32 char buffer[MAX_PATH]; - if (!PathCanonicalize(buffer, AdapateLongPath(path).c_str())) { + if (!PathCanonicalize(buffer, AdaptLongPath(path).c_str())) { return ""; } @@ -283,7 +283,7 @@ bool FileEntry::RemoveAllDirInner(const FileEntry &entry) string path = entry.GetFilePath().GetPath(); if (entry.IsFile()) { #ifdef _WIN32 - bool result = remove(AdapateLongPath(path).c_str()) == 0; + bool result = remove(AdaptLongPath(path).c_str()) == 0; #else bool result = remove(path.c_str()) == 0; #endif @@ -300,7 +300,7 @@ bool FileEntry::RemoveAllDirInner(const FileEntry &entry) } } #ifdef _WIN32 - bool result = rmdir(AdapateLongPath(path).c_str()) == 0; + bool result = rmdir(AdaptLongPath(path).c_str()) == 0; #else bool result = rmdir(path.c_str()) == 0; #endif @@ -316,7 +316,7 @@ bool FileEntry::CreateDirsInner(const string &path, string::size_type offset) string::size_type pos = path.find_first_of(SEPARATE.front(), offset); if (pos == string::npos) { #ifdef _WIN32 - return CreateDirectory(AdapateLongPath(path).c_str(), nullptr) != 0; + return CreateDirectory(AdaptLongPath(path).c_str(), nullptr) != 0; #else return mkdir(path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0; #endif @@ -325,7 +325,7 @@ bool FileEntry::CreateDirsInner(const string &path, string::size_type offset) string subPath = path.substr(0, pos + 1); if (!Exist(subPath)) { #ifdef _WIN32 - if (!CreateDirectory(AdapateLongPath(subPath).c_str(), nullptr)) { + if (!CreateDirectory(AdaptLongPath(subPath).c_str(), nullptr)) { #else if (mkdir(subPath.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0) { #endif @@ -360,7 +360,7 @@ void FileEntry::FilePath::Init() } } -string FileEntry::AdapateLongPath(const string &path) +string FileEntry::AdaptLongPath(const string &path) { #ifdef _WIN32 if (path.size() >= MAX_PATH -12) { //the max file path can not exceed 260 - 12 diff --git a/src/id_worker.cpp b/src/id_worker.cpp index 741d076..de3c29c 100644 --- a/src/id_worker.cpp +++ b/src/id_worker.cpp @@ -206,8 +206,7 @@ uint32_t IdWorker::InitIdDefined() if (combine) { idDefinedPath = FileEntry::FilePath(inputPath).Append(ID_DEFINED_FILE).GetPath(); } else { - idDefinedPath = FileEntry::FilePath(inputPath).Append(RESOURCES_DIR) - .Append("base").Append("element").Append(ID_DEFINED_FILE).GetPath(); + idDefinedPath = ResourceUtil::GetBaseElementPath(inputPath).Append(ID_DEFINED_FILE).GetPath(); } if (ResourceUtil::FileExist(idDefinedPath) && startId > 0) { cerr << "Error: the set start_id and id_defined.json cannot be used together." << endl; diff --git a/src/resource_merge.cpp b/src/resource_merge.cpp index c96ea91..884e9fb 100644 --- a/src/resource_merge.cpp +++ b/src/resource_merge.cpp @@ -48,14 +48,14 @@ uint32_t ResourceMerge::Init() map> inputTypes; for (auto it = inputs.crbegin(); it != inputs.crend(); it++) { - string filePath = FileEntry::FilePath(*it).Append(ConfigParser::GetConfigName()).GetPath(); - string resourceDir = FileEntry::FilePath(*it).Append(RESOURCES_DIR).GetPath(); + string configPath = ResourceUtil::GetMainPath(*it).Append(ConfigParser::GetConfigName()).GetPath(); + string resourceDir = FileEntry::FilePath(*it).GetPath(); ConfigParser::ModuleType moduleType = ConfigParser::ModuleType::NONE; - if (!ResourceUtil::FileExist(filePath)) { + if (!ResourceUtil::FileExist(configPath)) { inputTypes[moduleType].push_back(resourceDir); continue; } - ConfigParser configParser(filePath); + ConfigParser configParser(configPath); if (configParser.Init() != RESTOOL_SUCCESS) { return RESTOOL_ERROR; } diff --git a/src/resource_pack.cpp b/src/resource_pack.cpp index 9e86d97..75d53b4 100644 --- a/src/resource_pack.cpp +++ b/src/resource_pack.cpp @@ -168,9 +168,9 @@ uint32_t ResourcePack::InitConfigJson() cerr << "Error: more input path, -j config.json empty" << endl; return RESTOOL_ERROR; } - config = FileEntry::FilePath(packageParser_.GetInputs()[0]).Append(CONFIG_JSON).GetPath(); + config = ResourceUtil::GetMainPath(packageParser_.GetInputs()[0]).Append(CONFIG_JSON).GetPath(); if (!ResourceUtil::FileExist(config)) { - config = FileEntry::FilePath(packageParser_.GetInputs()[0]).Append(MODULE_JSON).GetPath(); + config = ResourceUtil::GetMainPath(packageParser_.GetInputs()[0]).Append(MODULE_JSON).GetPath(); } } diff --git a/src/resource_util.cpp b/src/resource_util.cpp index 0bb8a7d..a8184a2 100644 --- a/src/resource_util.cpp +++ b/src/resource_util.cpp @@ -72,7 +72,7 @@ bool ResourceUtil::RmoveAllDir(const string &path) bool ResourceUtil::OpenJsonFile(const string &path, Json::Value &root) { - ifstream ifs(FileEntry::AdapateLongPath(path), ios::binary); + ifstream ifs(FileEntry::AdaptLongPath(path), ios::binary); if (!ifs.is_open()) { cerr << "Error: open json failed '" << path << "', reason: " << strerror(errno) << endl; return false; @@ -98,7 +98,7 @@ bool ResourceUtil::SaveToJsonFile(const string &path, const Json::Value &root) writerBuilder["indentation"] = " "; writerBuilder["emitUTF8"] = true; unique_ptr writer(writerBuilder.newStreamWriter()); - ofstream out(FileEntry::AdapateLongPath(path), ofstream::out | ofstream::binary); + ofstream out(FileEntry::AdaptLongPath(path), ofstream::out | ofstream::binary); if (!out.is_open()) { cerr << "Error: open failed '" << path <<"', reason: " << strerror(errno) << endl; return false; @@ -392,6 +392,16 @@ string ResourceUtil::GetAllRestypeString() return result; } +FileEntry::FilePath ResourceUtil::GetBaseElementPath(const string input) +{ + return FileEntry::FilePath(input).Append("base").Append("element"); +} + +FileEntry::FilePath ResourceUtil::GetMainPath(const string input) +{ + return FileEntry::FilePath(input).GetParent(); +} + } } }