From 0049109d117af35d0d02df8bb563b77733976eb0 Mon Sep 17 00:00:00 2001 From: buxue Date: Wed, 2 Jun 2021 17:28:56 +0800 Subject: [PATCH] add permission for save files --- .../backend/kernel_compiler/common_utils.cc | 28 +++----- .../ccsrc/backend/session/session_basic.cc | 27 ++------ mindspore/ccsrc/debug/anf_ir_dump.cc | 16 ++--- mindspore/ccsrc/debug/anf_ir_utils.cc | 15 ++-- mindspore/ccsrc/debug/common.cc | 69 ++++++++++--------- .../ccsrc/debug/data_dump/dump_json_parser.cc | 26 ++++--- .../ccsrc/debug/debugger/proto_exporter.cc | 31 ++------- mindspore/ccsrc/debug/draw.cc | 21 ++++-- mindspore/ccsrc/debug/dump_proto.cc | 26 ++----- .../parallel_strategy_checkpoint.cc | 1 + mindspore/ccsrc/utils/tensorprint_utils.cc | 8 +-- mindspore/ccsrc/utils/utils.h | 9 ++- mindspore/real_path.py | 34 +++++++++ 13 files changed, 158 insertions(+), 153 deletions(-) create mode 100644 mindspore/real_path.py diff --git a/mindspore/ccsrc/backend/kernel_compiler/common_utils.cc b/mindspore/ccsrc/backend/kernel_compiler/common_utils.cc index a5d4dcf7993..69c3bbec874 100644 --- a/mindspore/ccsrc/backend/kernel_compiler/common_utils.cc +++ b/mindspore/ccsrc/backend/kernel_compiler/common_utils.cc @@ -30,6 +30,7 @@ #include "base/core_ops.h" #include "ir/graph_utils.h" #include "utils/ms_context.h" +#include "mindspore/ccsrc/debug/common.h" namespace mindspore { namespace kernel { @@ -432,34 +433,21 @@ bool ParseMetadata(const CNodePtr &kernel_node, const std::shared_ptr PATH_MAX) { - MS_LOG(DEBUG) << "file path " << path << " is too long."; + auto realpath = Common::GetRealPath(path); + if (!realpath.has_value()) { + MS_LOG(ERROR) << "Get real path failed, path=" << path; return; } - std::ofstream filewrite; - filewrite.open(path); + ChangeFileMode(realpath.value(), S_IWUSR); + std::ofstream filewrite(realpath.value()); if (!filewrite.is_open()) { + MS_LOG(ERROR) << "Open file '" << realpath.value() << "' failed!"; return; } filewrite << info << std::endl; filewrite.close(); -#if defined(_WIN32) || defined(_WIN64) - if (nullptr == _fullpath(real_path, path.c_str(), PATH_MAX)) { - MS_LOG(DEBUG) << "dir " << path << " does not exit."; - return; - } -#else - if (nullptr == realpath(path.c_str(), real_path)) { - MS_LOG(DEBUG) << "dir " << path << " does not exit."; - return; - } -#endif - MS_LOG(INFO) << "real path is :" << real_path; - if (chmod(real_path, S_IRUSR) == -1) { - MS_LOG(DEBUG) << "modify file:" << real_path << " to read only fail."; - } + ChangeFileMode(realpath.value(), S_IRUSR); } Processor GetProcessor(const string &processor) { diff --git a/mindspore/ccsrc/backend/session/session_basic.cc b/mindspore/ccsrc/backend/session/session_basic.cc index c4a9eb17539..07f5014f305 100644 --- a/mindspore/ccsrc/backend/session/session_basic.cc +++ b/mindspore/ccsrc/backend/session/session_basic.cc @@ -2546,31 +2546,18 @@ void DumpGraphExeOrder(const std::string &file_name, const std::string &target_d MS_LOG(ERROR) << "Failed at CreateNotExistDirs in DumpGraphExeOrder"; return; } - if (file_path.size() > PATH_MAX) { - MS_LOG(ERROR) << "File path " << file_path << " is too long."; + + auto realpath = Common::GetRealPath(file_path); + if (!realpath.has_value()) { + MS_LOG(ERROR) << "Get real path failed, path=" << file_path; return; } - char real_path[PATH_MAX] = {0}; - char *real_path_ret = nullptr; -#if defined(_WIN32) || defined(_WIN64) - real_path_ret = _fullpath(real_path, file_path.c_str(), PATH_MAX); -#else - real_path_ret = realpath(file_path.c_str(), real_path); -#endif - if (real_path_ret == nullptr) { - MS_LOG(DEBUG) << "dir " << file_path << " does not exit."; - } else { - std::string path_string = real_path; - if (chmod(common::SafeCStr(path_string), S_IRUSR | S_IWUSR) == -1) { - MS_LOG(ERROR) << "Modify file:" << real_path << " to rw fail."; - return; - } - } + ChangeFileMode(realpath.value(), S_IWUSR); // write to csv file - std::ofstream ofs(real_path); + std::ofstream ofs(realpath.value()); if (!ofs.is_open()) { - MS_LOG(ERROR) << "Open file '" << real_path << "' failed!"; + MS_LOG(ERROR) << "Open file '" << realpath.value() << "' failed!"; return; } ofs << "NodeExecutionOrder-FullNameWithScope\n"; diff --git a/mindspore/ccsrc/debug/anf_ir_dump.cc b/mindspore/ccsrc/debug/anf_ir_dump.cc index 2d58f36d5d9..621223eea52 100644 --- a/mindspore/ccsrc/debug/anf_ir_dump.cc +++ b/mindspore/ccsrc/debug/anf_ir_dump.cc @@ -456,9 +456,9 @@ void DumpCNode(const CNodePtr &nd, const FuncGraphPtr &sub_graph, OrderedMapprimal_debug_infos(); if (!primal_debug_infos.empty()) { gsub->buffer << " # Corresponding forward node candidate:\n"; - for (auto iter = primal_debug_infos.begin(); iter != primal_debug_infos.end(); iter++) { - gsub->buffer << trace::GetDebugInfo(*iter, " # ", kSourceLineTipDiscard) << "#" - << label_manage::Label(*iter) << "\n"; + for (auto &primal_debug_info : primal_debug_infos) { + gsub->buffer << trace::GetDebugInfo(primal_debug_info, " # ", kSourceLineTipDiscard) << "#" + << label_manage::Label(primal_debug_info) << "\n"; } } } else { @@ -466,8 +466,8 @@ void DumpCNode(const CNodePtr &nd, const FuncGraphPtr &sub_graph, OrderedMapprimal_debug_infos(); if (!primal_debug_infos.empty()) { gsub->buffer << " # Corresponding forward node candidate:\n"; - for (auto iter = primal_debug_infos.begin(); iter != primal_debug_infos.end(); iter++) { - gsub->buffer << trace::GetDebugInfo(*iter, " # ", kSourceLineTipDiscard) << "\n"; + for (auto &primal_debug_info : primal_debug_infos) { + gsub->buffer << trace::GetDebugInfo(primal_debug_info, " # ", kSourceLineTipDiscard) << "\n"; } } } @@ -588,11 +588,11 @@ void DumpIR(const std::string &filename, const FuncGraphPtr &graph, bool dump_fu } auto realpath = Common::GetRealPath(path); if (!realpath.has_value()) { - MS_LOG(ERROR) << "Get real path failed. path=" << path; + MS_LOG(ERROR) << "Get real path failed, path=" << path; return; } - ChangeFileMode(realpath.value(), S_IRWXU); + ChangeFileMode(realpath.value(), S_IWUSR); std::ofstream fout(realpath.value()); std::ostringstream buffer; if (!fout.is_open()) { @@ -634,7 +634,7 @@ void DumpIRForRDR(const std::string &filename, const FuncGraphPtr &graph, bool d return; } - ChangeFileMode(realpath.value(), S_IRWXU); + ChangeFileMode(realpath.value(), S_IWUSR); std::ofstream fout(realpath.value()); std::ostringstream buffer; if (!fout.is_open()) { diff --git a/mindspore/ccsrc/debug/anf_ir_utils.cc b/mindspore/ccsrc/debug/anf_ir_utils.cc index 77d3b0d4266..226e0939bc6 100644 --- a/mindspore/ccsrc/debug/anf_ir_utils.cc +++ b/mindspore/ccsrc/debug/anf_ir_utils.cc @@ -634,18 +634,23 @@ void ExportIR(const std::string &filename, const FuncGraphPtr &func_graph) { return; } - auto real_filename = pipeline::GetSaveGraphsPathName(Common::AddId(filename, ".dat")); + auto filepath = pipeline::GetSaveGraphsPathName(Common::AddId(filename, ".dat")); + auto real_filepath = Common::GetRealPath(filepath); + if (!real_filepath.has_value()) { + MS_LOG(ERROR) << "The export ir path: " << filepath << " is not illegal."; + return; + } + ChangeFileMode(real_filepath.value(), S_IWUSR); AnfExporter exporter; - ChangeFileMode(real_filename, S_IRWXU); - exporter.ExportFuncGraph(real_filename, func_graph); + exporter.ExportFuncGraph(real_filepath.value(), func_graph); // set file mode to read only by user - ChangeFileMode(real_filename, S_IRUSR); + ChangeFileMode(real_filepath.value(), S_IRUSR); } void ExportIR(const std::string &filename, const std::vector &graphs) { auto real_filename = pipeline::GetSaveGraphsPathName(Common::AddId(filename, ".dat")); + ChangeFileMode(real_filename, S_IWUSR); AnfExporter exporter("", false); - ChangeFileMode(real_filename, S_IRWXU); exporter.ExportFuncGraph(real_filename, graphs); // set file mode to read only by user ChangeFileMode(real_filename, S_IRUSR); diff --git a/mindspore/ccsrc/debug/common.cc b/mindspore/ccsrc/debug/common.cc index 3a0034dd07b..42945be9dad 100644 --- a/mindspore/ccsrc/debug/common.cc +++ b/mindspore/ccsrc/debug/common.cc @@ -28,59 +28,62 @@ namespace mindspore { std::optional Common::GetRealPath(const std::string &input_path) { - std::string out_path; - auto path_split_pos = input_path.find_last_of('/'); - if (path_split_pos == std::string::npos) { - path_split_pos = input_path.find_last_of('\\'); + if (input_path.length() > PATH_MAX) { + MS_LOG(EXCEPTION) << "The length of path: " << input_path << " exceeds limit: " << PATH_MAX; } +#if defined(SYSTEM_ENV_POSIX) + size_t path_split_pos = input_path.find_last_of('/'); +#elif defined(SYSTEM_ENV_WINDOWS) + size_t path_split_pos = input_path.find_last_of('\\'); +#else + MS_LOG(EXCEPTION) << "Unsupported platform."; +#endif // get real path - char real_path[PATH_MAX] = {0}; + std::string out_path; + char real_path[PATH_MAX + 1] = {0}; + // input_path is dir + file_name if (path_split_pos != std::string::npos) { std::string prefix_path = input_path.substr(0, path_split_pos); - if (prefix_path.length() >= PATH_MAX) { - MS_LOG(ERROR) << "Prefix path is too longer!"; - return std::nullopt; - } - std::string last_path = input_path.substr(path_split_pos, input_path.length() - path_split_pos); - auto ret = CreateNotExistDirs(prefix_path); - if (!ret) { - MS_LOG(ERROR) << "CreateNotExistDirs Failed!"; + std::string file_name = input_path.substr(path_split_pos); + if (!CreateNotExistDirs(prefix_path)) { + MS_LOG(ERROR) << "Create dir " << prefix_path << " Failed!"; return std::nullopt; } #if defined(SYSTEM_ENV_POSIX) - if (realpath(prefix_path.c_str(), real_path) == nullptr) { - MS_LOG(ERROR) << "dir " << prefix_path << " does not exist."; + if (file_name.length() > NAME_MAX) { + MS_LOG(EXCEPTION) << "The length of file name : " << file_name.length() << " exceeds limit: " << NAME_MAX; + } + if (realpath(common::SafeCStr(prefix_path), real_path) == nullptr) { + MS_LOG(ERROR) << "The dir " << prefix_path << " does not exist."; return std::nullopt; } #elif defined(SYSTEM_ENV_WINDOWS) - if (_fullpath(real_path, prefix_path.c_str(), PATH_MAX) == nullptr) { - MS_LOG(ERROR) << "dir " << prefix_path << " does not exist."; + if (_fullpath(real_path, common::SafeCStr(prefix_path), PATH_MAX) == nullptr) { + MS_LOG(ERROR) << "The dir " << prefix_path << " does not exist."; return std::nullopt; } -#else - MS_LOG(EXCEPTION) << "Unsupported platform."; #endif - out_path = std::string(real_path) + last_path; - } - - if (path_split_pos == std::string::npos) { - if (input_path.length() >= PATH_MAX) { - MS_LOG(ERROR) << "Prefix path is too longer!"; - return std::nullopt; - } + out_path = std::string(real_path) + file_name; + } else { + // input_path is only file_name #if defined(SYSTEM_ENV_POSIX) - if (realpath(input_path.c_str(), real_path) == nullptr) { - MS_LOG(ERROR) << "File " << input_path << " does not exist, it will be created."; + if (input_path.length() > NAME_MAX) { + MS_LOG(EXCEPTION) << "The length of file name : " << input_path.length() << " exceeds limit: " << NAME_MAX; + } + if (realpath(common::SafeCStr(input_path), real_path) == nullptr) { + MS_LOG(INFO) << "The file " << input_path << " does not exist, it will be created."; } #elif defined(SYSTEM_ENV_WINDOWS) - if (_fullpath(real_path, input_path.c_str(), PATH_MAX) == nullptr) { - MS_LOG(ERROR) << "File " << input_path << " does not exist, it will be created."; + if (_fullpath(real_path, common::SafeCStr(input_path), PATH_MAX) == nullptr) { + MS_LOG(INFO) << "The file " << input_path << " does not exist, it will be created."; } -#else - MS_LOG(EXCEPTION) << "Unsupported platform."; #endif out_path = std::string(real_path); } + + if (out_path.length() > PATH_MAX) { + MS_LOG(EXCEPTION) << "The file real path: " << out_path << " exceeds limit: " << PATH_MAX; + } return out_path; } diff --git a/mindspore/ccsrc/debug/data_dump/dump_json_parser.cc b/mindspore/ccsrc/debug/data_dump/dump_json_parser.cc index ae732dadec5..061490a9988 100644 --- a/mindspore/ccsrc/debug/data_dump/dump_json_parser.cc +++ b/mindspore/ccsrc/debug/data_dump/dump_json_parser.cc @@ -127,10 +127,12 @@ void DumpJsonParser::CopyJsonToDir(uint32_t rank_id) { if (!realpath.has_value()) { MS_LOG(ERROR) << "Get real path failed in CopyJsonDir."; } - std::ofstream json_copy(realpath.value()); + const std::string file_path = realpath.value(); + ChangeFileMode(file_path, S_IWUSR); + std::ofstream json_copy(file_path); json_copy << json_file.rdbuf(); json_copy.close(); - ChangeFileMode(realpath.value(), S_IRUSR); + ChangeFileMode(file_path, S_IRUSR); } } @@ -151,10 +153,12 @@ void DumpJsonParser::CopyHcclJsonToDir(uint32_t rank_id) { if (!realpath.has_value()) { MS_LOG(ERROR) << "Get real path failed in CopyHcclJsonToDir."; } else { - std::ofstream json_copy(realpath.value()); + const std::string file_path = realpath.value(); + ChangeFileMode(file_path, S_IWUSR); + std::ofstream json_copy(file_path); json_copy << json_file.rdbuf(); json_copy.close(); - ChangeFileMode(realpath.value(), S_IRUSR); + ChangeFileMode(file_path, S_IRUSR); } } @@ -171,10 +175,12 @@ void DumpJsonParser::CopyMSCfgJsonToDir(uint32_t rank_id) { MS_EXCEPTION_IF_NULL(context); ms_info["device_target"] = context->get_param(MS_CTX_DEVICE_TARGET); ms_info["ms_version"] = "1.2.0"; - std::ofstream json_create(realpath.value()); + const std::string file_path = realpath.value(); + ChangeFileMode(file_path, S_IWUSR); + std::ofstream json_create(file_path); json_create << ms_info; json_create.close(); - ChangeFileMode(realpath.value(), S_IRUSR); + ChangeFileMode(file_path, S_IRUSR); } } @@ -192,16 +198,18 @@ bool DumpJsonParser::DumpToFile(const std::string &filename, const void *data, s MS_LOG(ERROR) << "Get real path failed."; return false; } - std::ofstream fd; - fd.open(realpath.value(), std::ios::binary | std::ios::out); + const std::string file_path = realpath.value(); + ChangeFileMode(file_path, S_IWUSR); + std::ofstream fd(file_path, std::ios::out | std::ios::trunc | std::ios::binary); if (!fd.is_open()) { - MS_LOG(ERROR) << "Open file " << realpath.value() << " fail."; + MS_LOG(ERROR) << "Open file " << file_path << " failed."; return false; } std::string npy_header = GenerateNpyHeader(shape, type); fd << npy_header; (void)fd.write(reinterpret_cast(data), SizeToLong(len)); fd.close(); + ChangeFileMode(file_path, S_IRUSR); return true; } diff --git a/mindspore/ccsrc/debug/debugger/proto_exporter.cc b/mindspore/ccsrc/debug/debugger/proto_exporter.cc index 0eed9b5a6ad..15cb59373a8 100644 --- a/mindspore/ccsrc/debug/debugger/proto_exporter.cc +++ b/mindspore/ccsrc/debug/debugger/proto_exporter.cc @@ -553,36 +553,17 @@ void DumpIRProtoWithSrcInfo(const FuncGraphPtr &func_graph, const std::string &s return; } std::string file_path = target_dir + "/" + "ms_output_" + suffix + ".pb"; - bool status = Common::CreateNotExistDirs(target_dir); - if (!status) { - MS_LOG(ERROR) << "Failed at CreateNotExistDirs in dump_proto"; + auto realpath = Common::GetRealPath(file_path); + if (!realpath.has_value()) { + MS_LOG(ERROR) << "Get real path failed, path=" << file_path; return; } - if (file_path.size() > PATH_MAX) { - MS_LOG(ERROR) << "File path " << file_path << " is too long."; - return; - } - char real_path[PATH_MAX] = {0}; - char *real_path_ret = nullptr; -#if defined(_WIN32) || defined(_WIN64) - real_path_ret = _fullpath(real_path, file_path.c_str(), PATH_MAX); -#else - real_path_ret = realpath(file_path.c_str(), real_path); -#endif - if (real_path_ret == nullptr) { - MS_LOG(DEBUG) << "dir " << file_path << " does not exit."; - } else { - std::string path_string = real_path; - if (chmod(common::SafeCStr(path_string), S_IRUSR | S_IWUSR) == -1) { - MS_LOG(ERROR) << "Modify file:" << real_path << " to rw fail."; - return; - } - } + ChangeFileMode(realpath.value(), S_IWUSR); // write to pb file - std::ofstream ofs(real_path); + std::ofstream ofs(realpath.value()); if (!ofs.is_open()) { - MS_LOG(ERROR) << "Open file '" << real_path << "' failed!"; + MS_LOG(ERROR) << "Open file '" << realpath.value() << "' failed!"; return; } ofs << graph_proto; diff --git a/mindspore/ccsrc/debug/draw.cc b/mindspore/ccsrc/debug/draw.cc index 3cf7c52832c..66c939c5959 100644 --- a/mindspore/ccsrc/debug/draw.cc +++ b/mindspore/ccsrc/debug/draw.cc @@ -148,7 +148,7 @@ void DrawEdges(const std::vector &nodes, const std::shared_ptr digraph; OrderedMap> sub_graphs; - ChangeFileMode(filename, S_IRWXU); + ChangeFileMode(filename, S_IWUSR); if (is_user) { digraph = std::make_shared("mindspore", filename); } else { @@ -189,13 +189,24 @@ void DrawByOpt(std::string filename, const FuncGraphPtr &func_graph, bool is_use #ifdef ENABLE_DUMP_IR void Draw(const std::string &filename, const FuncGraphPtr &func_graph) { const std::string dot_suffix = ".dot"; - std::string filename_with_suffix = + const std::string filename_with_suffix = (filename.rfind(dot_suffix) != (filename.size() - dot_suffix.size())) ? (filename + dot_suffix) : filename; - DrawByOpt(pipeline::GetSaveGraphsPathName(Common::AddId(filename_with_suffix, dot_suffix)), func_graph, false); + const std::string filepath = pipeline::GetSaveGraphsPathName(Common::AddId(filename_with_suffix, dot_suffix)); + auto real_filepath = Common::GetRealPath(filepath); + if (!real_filepath.has_value()) { + MS_LOG(EXCEPTION) << "The export ir path: " << filepath << " is not illegal."; + } + DrawByOpt(real_filepath.value(), func_graph, false); } void DrawUserFuncGraph(const std::string &filename, const FuncGraphPtr &func_graph) { - DrawByOpt(pipeline::GetSaveGraphsPathName(Common::AddId(filename, ".dot")), func_graph, true); + const std::string dot_suffix = ".dot"; + const std::string filepath = pipeline::GetSaveGraphsPathName(Common::AddId(filename, dot_suffix)); + auto real_filepath = Common::GetRealPath(filepath); + if (!real_filepath.has_value()) { + MS_LOG(EXCEPTION) << "The export ir path: " << filepath << " is not illegal."; + } + DrawByOpt(real_filepath.value(), func_graph, true); } #else void Draw(const std::string &, const FuncGraphPtr &) { diff --git a/mindspore/ccsrc/debug/dump_proto.cc b/mindspore/ccsrc/debug/dump_proto.cc index e49653874ef..6fb0c26e08d 100644 --- a/mindspore/ccsrc/debug/dump_proto.cc +++ b/mindspore/ccsrc/debug/dump_proto.cc @@ -543,31 +543,17 @@ void DumpIRProto(const FuncGraphPtr &func_graph, const std::string &suffix) { return; } std::string file_path = pipeline::GetSaveGraphsPathName("ms_output_" + suffix + ".pb"); - if (file_path.size() > PATH_MAX) { - MS_LOG(ERROR) << "File path " << file_path << " is too long."; + auto realpath = Common::GetRealPath(file_path); + if (!realpath.has_value()) { + MS_LOG(ERROR) << "Get real path failed, path=" << file_path; return; } - char real_path[PATH_MAX] = {0}; - char *real_path_ret = nullptr; -#if defined(_WIN32) || defined(_WIN64) - real_path_ret = _fullpath(real_path, file_path.c_str(), PATH_MAX); -#else - real_path_ret = realpath(file_path.c_str(), real_path); -#endif - if (real_path_ret == nullptr) { - MS_LOG(DEBUG) << "dir " << file_path << " does not exit."; - } else { - std::string path_string = real_path; - if (chmod(common::SafeCStr(path_string), S_IRUSR | S_IWUSR) == -1) { - MS_LOG(ERROR) << "Modify file:" << real_path << " to rw fail."; - return; - } - } + ChangeFileMode(realpath.value(), S_IWUSR); // write to pb file - std::ofstream ofs(real_path); + std::ofstream ofs(file_path); if (!ofs.is_open()) { - MS_LOG(ERROR) << "Open file '" << real_path << "' failed!"; + MS_LOG(ERROR) << "Open file '" << file_path << "' failed!"; return; } ofs << GetFuncGraphProtoString(func_graph); diff --git a/mindspore/ccsrc/frontend/parallel/strategy_checkpoint/parallel_strategy_checkpoint.cc b/mindspore/ccsrc/frontend/parallel/strategy_checkpoint/parallel_strategy_checkpoint.cc index 8aab6301289..caec61b4f06 100644 --- a/mindspore/ccsrc/frontend/parallel/strategy_checkpoint/parallel_strategy_checkpoint.cc +++ b/mindspore/ccsrc/frontend/parallel/strategy_checkpoint/parallel_strategy_checkpoint.cc @@ -174,6 +174,7 @@ Status StrategyCheckpoint::Save(const StrategyMap &strategy_map, const TensorInf return FAILED; } output.close(); + ChangeFileMode(save_file_, S_IRUSR | S_IWUSR); return SUCCESS; } diff --git a/mindspore/ccsrc/utils/tensorprint_utils.cc b/mindspore/ccsrc/utils/tensorprint_utils.cc index 28043941782..f642d0301c0 100644 --- a/mindspore/ccsrc/utils/tensorprint_utils.cc +++ b/mindspore/ccsrc/utils/tensorprint_utils.cc @@ -21,6 +21,7 @@ #include #include "ir/tensor.h" #include "pybind11/pybind11.h" +#include "utils/utils.h" #include "utils/ms_utils.h" #include "utils/shape_utils.h" @@ -288,6 +289,7 @@ void TensorPrint::operator()() { } } } else { + ChangeFileMode(print_file_path_, S_IWUSR); std::fstream output(print_file_path_, std::ios::out | std::ios::trunc | std::ios::binary); while (true) { acltdtDataset *acl_dataset = acltdtCreateDataset(); @@ -303,11 +305,7 @@ void TensorPrint::operator()() { } } output.close(); - std::string path_string = print_file_path_; - if (chmod(common::SafeCStr(path_string), S_IRUSR) == -1) { - MS_LOG(ERROR) << "Modify file:" << print_file_path_ << " fail."; - return; - } + ChangeFileMode(print_file_path_, S_IRUSR); } } #endif diff --git a/mindspore/ccsrc/utils/utils.h b/mindspore/ccsrc/utils/utils.h index c9f5ff1a47b..8de8f783d64 100644 --- a/mindspore/ccsrc/utils/utils.h +++ b/mindspore/ccsrc/utils/utils.h @@ -573,12 +573,15 @@ const std::set DynamicShapeConstInputToAttr = { kReduceMinOpName, kReduceMeanOpName, kReduceMaxOpName, kReduceAllOpName, kReduceAnyOpName, kConcatOpName}; static inline void ChangeFileMode(const std::string &file_name, mode_t mode) { + if (access(file_name.c_str(), F_OK) == -1) { + return; + } try { - if (chmod(file_name.c_str(), mode) != 0) { - MS_LOG(DEBUG) << "Change file `" << file_name << "` to mode " << std::oct << mode << " fail."; + if (chmod(common::SafeCStr(file_name), mode) != 0) { + MS_LOG(WARNING) << "Change file `" << file_name << "` to mode " << std::oct << mode << " fail."; } } catch (std::exception &e) { - MS_LOG(DEBUG) << "File `" << file_name << "` change mode failed! May be not exist."; + MS_LOG(WARNING) << "File `" << file_name << "` change mode failed! May be not exist."; } } diff --git a/mindspore/real_path.py b/mindspore/real_path.py new file mode 100644 index 00000000000..3b0214e8402 --- /dev/null +++ b/mindspore/real_path.py @@ -0,0 +1,34 @@ +# Copyright 2021 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ +"""file absolute path""" +import os + +_NAME_MAX = 255 +_PATH_MAX = 4096 + + +def get_file_real_path(file_path: str): + """get file absolute path""" + real_path = os.path.realpath(file_path) + if len(real_path) > _PATH_MAX: + raise ValueError(f"The length of dir path: {real_path} exceeds limit: {_PATH_MAX}") + file_pos = real_path.rfind('/') + 1 + dir_path = real_path[0: file_pos] + file_name = real_path[file_pos:] + if len(file_name) > _NAME_MAX: + raise ValueError(f"The length of file name: {file_name} exceeds limit: {_NAME_MAX}") + if not os.path.exists(dir_path): + os.makedirs(real_path[0: file_pos]) + return real_path