1 /**
2 * Copyright 2021 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "utils/file_utils.h"
18
19 #include <limits.h>
20 #include <string.h>
21 #include <string>
22 #include <optional>
23 #include <memory>
24 #include "utils/system/file_system.h"
25 #include "utils/system/env.h"
26 #include "utils/utils.h"
27
28 namespace mindspore {
GetRealPath(const char * path)29 std::optional<std::string> FileUtils::GetRealPath(const char *path) {
30 if (path == nullptr) {
31 MS_LOG(ERROR) << "Input path is nullptr";
32 return std::nullopt;
33 }
34
35 char real_path[PATH_MAX] = {0};
36 #if defined(_WIN32) || defined(_WIN64)
37 if (strlen(path) >= PATH_MAX || _fullpath(real_path, path, PATH_MAX) == nullptr) {
38 MS_LOG(ERROR) << "Get _fullpath failed";
39 return std::nullopt;
40 }
41 #else
42 if (strlen(path) >= PATH_MAX || realpath(path, real_path) == nullptr) {
43 MS_LOG(ERROR) << "Get realpath failed, path[" << path << "]";
44 return std::nullopt;
45 }
46 #endif
47 return std::string(real_path);
48 }
49
SplitDirAndFileName(const std::string & path,std::optional<std::string> * prefix_path,std::optional<std::string> * file_name)50 void FileUtils::SplitDirAndFileName(const std::string &path, std::optional<std::string> *prefix_path,
51 std::optional<std::string> *file_name) {
52 auto path_split_pos = path.find_last_of('/');
53 if (path_split_pos == std::string::npos) {
54 path_split_pos = path.find_last_of('\\');
55 }
56
57 MS_EXCEPTION_IF_NULL(prefix_path);
58 MS_EXCEPTION_IF_NULL(file_name);
59
60 if (path_split_pos != std::string::npos) {
61 *prefix_path = path.substr(0, path_split_pos);
62 *file_name = path.substr(path_split_pos + 1);
63 } else {
64 *prefix_path = std::nullopt;
65 *file_name = path;
66 }
67 }
68
ConcatDirAndFileName(const std::optional<std::string> * dir,const std::optional<std::string> * file_name,std::optional<std::string> * path)69 void FileUtils::ConcatDirAndFileName(const std::optional<std::string> *dir, const std::optional<std::string> *file_name,
70 std::optional<std::string> *path) {
71 MS_EXCEPTION_IF_NULL(dir);
72 MS_EXCEPTION_IF_NULL(file_name);
73 MS_EXCEPTION_IF_NULL(path);
74 #if defined(_WIN32) || defined(_WIN64)
75 *path = dir->value() + "\\" + file_name->value();
76 #else
77 *path = dir->value() + "/" + file_name->value();
78 #endif
79 }
80
CreateNotExistDirs(const std::string & path,const bool support_relative_path)81 std::optional<std::string> FileUtils::CreateNotExistDirs(const std::string &path, const bool support_relative_path) {
82 if (path.size() >= PATH_MAX) {
83 MS_LOG(ERROR) << "The length of the path is greater than or equal to:" << PATH_MAX;
84 return std::nullopt;
85 }
86 if (!support_relative_path) {
87 auto dot_pos = path.find("..");
88 if (dot_pos != std::string::npos) {
89 MS_LOG(ERROR) << "Do not support relative path";
90 return std::nullopt;
91 }
92 }
93
94 std::shared_ptr<system::FileSystem> fs = system::Env::GetFileSystem();
95 MS_EXCEPTION_IF_NULL(fs);
96 char temp_path[PATH_MAX] = {0};
97 for (uint32_t i = 0; i < path.length(); i++) {
98 temp_path[i] = path[i];
99 if (temp_path[i] == '\\' || temp_path[i] == '/') {
100 if (i != 0) {
101 char tmp_char = temp_path[i];
102 temp_path[i] = '\0';
103 std::string path_handle(temp_path);
104 if (!fs->FileExist(path_handle)) {
105 if (!fs->CreateDir(path_handle)) {
106 MS_LOG(ERROR) << "Create " << path_handle << " dir error";
107 return std::nullopt;
108 }
109 }
110 temp_path[i] = tmp_char;
111 }
112 }
113 }
114
115 if (!fs->FileExist(path)) {
116 if (!fs->CreateDir(path)) {
117 MS_LOG(ERROR) << "Create " << path << " dir error";
118 return std::nullopt;
119 }
120 }
121 return GetRealPath(path.c_str());
122 }
123 } // namespace mindspore
124