Browse Source

!17661 add permission for save files

Merge pull request !17661 from zhangbuxue/add_permission_for_save_files
tags/v1.3.0
i-robot Gitee 4 years ago
parent
commit
50304d82fb
13 changed files with 158 additions and 153 deletions
  1. +8
    -20
      mindspore/ccsrc/backend/kernel_compiler/common_utils.cc
  2. +7
    -20
      mindspore/ccsrc/backend/session/session_basic.cc
  3. +8
    -8
      mindspore/ccsrc/debug/anf_ir_dump.cc
  4. +10
    -5
      mindspore/ccsrc/debug/anf_ir_utils.cc
  5. +36
    -33
      mindspore/ccsrc/debug/common.cc
  6. +17
    -9
      mindspore/ccsrc/debug/data_dump/dump_json_parser.cc
  7. +6
    -25
      mindspore/ccsrc/debug/debugger/proto_exporter.cc
  8. +16
    -5
      mindspore/ccsrc/debug/draw.cc
  9. +6
    -20
      mindspore/ccsrc/debug/dump_proto.cc
  10. +1
    -0
      mindspore/ccsrc/frontend/parallel/strategy_checkpoint/parallel_strategy_checkpoint.cc
  11. +3
    -5
      mindspore/ccsrc/utils/tensorprint_utils.cc
  12. +6
    -3
      mindspore/ccsrc/utils/utils.h
  13. +34
    -0
      mindspore/real_path.py

+ 8
- 20
mindspore/ccsrc/backend/kernel_compiler/common_utils.cc View File

@@ -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<const OpIn
}

void SaveJsonInfo(const std::string &json_name, const std::string &info, const std::string &base_path) {
char real_path[PATH_MAX] = {0};
std::string path = base_path + json_name + kInfoSuffix;
if (path.size() > 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) {


+ 7
- 20
mindspore/ccsrc/backend/session/session_basic.cc View File

@@ -2544,31 +2544,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";


+ 8
- 8
mindspore/ccsrc/debug/anf_ir_dump.cc View File

@@ -456,9 +456,9 @@ void DumpCNode(const CNodePtr &nd, const FuncGraphPtr &sub_graph, OrderedMap<Anf
auto primal_debug_infos = nd->primal_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, OrderedMap<Anf
auto primal_debug_infos = nd->primal_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()) {


+ 10
- 5
mindspore/ccsrc/debug/anf_ir_utils.cc View File

@@ -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<TaggedGraph> &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);


+ 36
- 33
mindspore/ccsrc/debug/common.cc View File

@@ -28,59 +28,62 @@

namespace mindspore {
std::optional<std::string> 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;
}



+ 17
- 9
mindspore/ccsrc/debug/data_dump/dump_json_parser.cc View File

@@ -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<std::string>(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<const char *>(data), SizeToLong(len));
fd.close();
ChangeFileMode(file_path, S_IRUSR);
return true;
}



+ 6
- 25
mindspore/ccsrc/debug/debugger/proto_exporter.cc View File

@@ -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;


+ 16
- 5
mindspore/ccsrc/debug/draw.cc View File

@@ -148,7 +148,7 @@ void DrawEdges(const std::vector<AnfNodePtr> &nodes, const std::shared_ptr<BaseD
}
}

void DrawByOpt(std::string filename, const FuncGraphPtr &func_graph, bool is_user) {
void DrawByOpt(const std::string &filename, const FuncGraphPtr &func_graph, bool is_user) {
if (func_graph == nullptr) {
return;
}
@@ -157,7 +157,7 @@ void DrawByOpt(std::string filename, const FuncGraphPtr &func_graph, bool is_use

std::shared_ptr<BaseDigraph> digraph;
OrderedMap<FuncGraphPtr, std::shared_ptr<BaseDigraph>> sub_graphs;
ChangeFileMode(filename, S_IRWXU);
ChangeFileMode(filename, S_IWUSR);
if (is_user) {
digraph = std::make_shared<ModelDigraph>("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 &) {


+ 6
- 20
mindspore/ccsrc/debug/dump_proto.cc View File

@@ -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);


+ 1
- 0
mindspore/ccsrc/frontend/parallel/strategy_checkpoint/parallel_strategy_checkpoint.cc View File

@@ -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;
}



+ 3
- 5
mindspore/ccsrc/utils/tensorprint_utils.cc View File

@@ -21,6 +21,7 @@
#include <vector>
#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


+ 6
- 3
mindspore/ccsrc/utils/utils.h View File

@@ -573,12 +573,15 @@ const std::set<std::string> 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.";
}
}



+ 34
- 0
mindspore/real_path.py View File

@@ -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

Loading…
Cancel
Save