You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

path.cc 10 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /**
  2. * Copyright 2019 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. #include "minddata/dataset/util/path.h"
  17. #include <sys/stat.h>
  18. #include <fcntl.h>
  19. #include <unistd.h>
  20. #include <new>
  21. #include <sstream>
  22. #include "./securec.h"
  23. #include "utils/file_utils.h"
  24. #include "utils/ms_utils.h"
  25. #include "minddata/dataset/util/log_adapter.h"
  26. namespace mindspore {
  27. namespace dataset {
  28. #if defined(_WIN32) || defined(_WIN64)
  29. char Path::separator_ = '\\';
  30. #else
  31. char Path::separator_ = '/';
  32. #endif
  33. Path::Path(const std::string &s) {
  34. #if defined(_WIN32) || defined(_WIN64)
  35. path_ = FileUtils::UTF_8ToGB2312(s.data());
  36. #else
  37. path_ = s;
  38. #endif
  39. }
  40. Path::Path(const char *p) {
  41. #if defined(_WIN32) || defined(_WIN64)
  42. path_ = FileUtils::UTF_8ToGB2312(p);
  43. #else
  44. path_ = p;
  45. #endif
  46. }
  47. Path::Path(const Path &p) : path_(p.path_) {}
  48. Path &Path::operator=(const Path &p) {
  49. if (&p != this) {
  50. this->path_ = p.path_;
  51. }
  52. return *this;
  53. }
  54. Path &Path::operator=(Path &&p) noexcept {
  55. if (&p != this) {
  56. this->path_ = std::move(p.path_);
  57. }
  58. return *this;
  59. }
  60. Path::Path(Path &&p) noexcept { this->path_ = std::move(p.path_); }
  61. Path Path::operator+(const Path &p) {
  62. std::string q = path_ + p.ToString();
  63. return Path(q);
  64. }
  65. Path Path::operator+(const std::string &p) {
  66. std::string q = path_ + p;
  67. return Path(q);
  68. }
  69. Path Path::operator+(const char *p) {
  70. std::string q = path_ + p;
  71. return Path(q);
  72. }
  73. Path &Path::operator+=(const Path &rhs) {
  74. path_ += rhs.ToString();
  75. return *this;
  76. }
  77. Path &Path::operator+=(const std::string &p) {
  78. #if defined(_WIN32) || defined(_WIN64)
  79. path_ += FileUtils::UTF_8ToGB2312(p.data());
  80. #else
  81. path_ += p;
  82. #endif
  83. return *this;
  84. }
  85. Path &Path::operator+=(const char *p) {
  86. #if defined(_WIN32) || defined(_WIN64)
  87. path_ += FileUtils::UTF_8ToGB2312(p);
  88. #else
  89. path_ += p;
  90. #endif
  91. return *this;
  92. }
  93. Path Path::operator/(const Path &p) {
  94. std::string q = path_ + separator_ + p.ToString();
  95. return Path(q);
  96. }
  97. Path Path::operator/(const std::string &p) {
  98. std::string q = path_ + separator_ + p;
  99. return Path(q);
  100. }
  101. Path Path::operator/(const char *p) {
  102. std::string q = path_ + separator_ + p;
  103. return Path(q);
  104. }
  105. std::string Path::Extension() const {
  106. std::size_t found = path_.find_last_of('.');
  107. if (found != std::string::npos) {
  108. return path_.substr(found);
  109. } else {
  110. return std::string("");
  111. }
  112. }
  113. bool Path::Exists() {
  114. struct stat sb;
  115. int rc = stat(common::SafeCStr(path_), &sb);
  116. if (rc == -1 && errno != ENOENT) {
  117. MS_LOG(WARNING) << "Unable to query the status of " << path_ << ". Errno = " << errno << ".";
  118. }
  119. return (rc == 0);
  120. }
  121. bool Path::IsDirectory() {
  122. struct stat sb;
  123. int rc = stat(common::SafeCStr(path_), &sb);
  124. if (rc == 0) {
  125. return S_ISDIR(sb.st_mode);
  126. } else {
  127. return false;
  128. }
  129. }
  130. Status Path::CreateDirectory(bool is_common_dir) {
  131. if (!Exists()) {
  132. #if defined(_WIN32) || defined(_WIN64)
  133. int rc = mkdir(common::SafeCStr(path_));
  134. #else
  135. int rc = mkdir(common::SafeCStr(path_), S_IRUSR | S_IWUSR | S_IXUSR);
  136. if (rc == 0 && is_common_dir) {
  137. rc = chmod(common::SafeCStr(path_), S_IRWXU | S_IRWXG | S_IRWXO);
  138. }
  139. #endif
  140. if (rc) {
  141. std::ostringstream oss;
  142. oss << "Unable to create directory " << path_ << ". Errno = " << errno;
  143. RETURN_STATUS_UNEXPECTED(oss.str());
  144. }
  145. return Status::OK();
  146. } else {
  147. if (IsDirectory()) {
  148. return Status::OK();
  149. } else {
  150. std::ostringstream oss;
  151. oss << "Unable to create directory " << path_ << ". It exists but is not a directory";
  152. RETURN_STATUS_UNEXPECTED(oss.str());
  153. }
  154. }
  155. }
  156. std::string Path::ParentPath() {
  157. std::string r("");
  158. std::size_t found = path_.find_last_of(separator_);
  159. if (found != std::string::npos) {
  160. if (found == 0) {
  161. r += separator_;
  162. } else {
  163. r = std::string(path_.substr(0, found));
  164. }
  165. }
  166. return r;
  167. }
  168. Status Path::CreateDirectories(bool is_common_dir) {
  169. if (IsDirectory()) {
  170. MS_LOG(DEBUG) << "Directory " << ToString() << " already exists.";
  171. return Status::OK();
  172. } else {
  173. MS_LOG(DEBUG) << "Creating directory " << ToString() << ".";
  174. std::string parent = ParentPath();
  175. if (!parent.empty()) {
  176. if (Path(parent).CreateDirectories(is_common_dir)) {
  177. return CreateDirectory(is_common_dir);
  178. }
  179. } else {
  180. return CreateDirectory(is_common_dir);
  181. }
  182. }
  183. return Status::OK();
  184. }
  185. Status Path::CreateCommonDirectories() { return CreateDirectories(true); }
  186. Status Path::Remove() {
  187. if (Exists()) {
  188. if (IsDirectory()) {
  189. errno_t err = rmdir(common::SafeCStr(path_));
  190. if (err == -1) {
  191. std::ostringstream oss;
  192. oss << "Unable to delete directory " << path_ << ". Errno = " << errno;
  193. RETURN_STATUS_UNEXPECTED(oss.str());
  194. }
  195. } else {
  196. errno_t err = unlink(common::SafeCStr(path_));
  197. if (err == -1) {
  198. std::ostringstream oss;
  199. oss << "Unable to delete file " << path_ << ". Errno = " << errno;
  200. RETURN_STATUS_UNEXPECTED(oss.str());
  201. }
  202. }
  203. }
  204. return Status::OK();
  205. }
  206. Status Path::CreateFile(int *file_descriptor) { return OpenFile(file_descriptor, true); }
  207. Status Path::OpenFile(int *file_descriptor, bool create) {
  208. int fd;
  209. if (file_descriptor == nullptr) {
  210. RETURN_STATUS_UNEXPECTED("null pointer");
  211. }
  212. if (IsDirectory()) {
  213. std::ostringstream oss;
  214. oss << "Unable to create file " << path_ << " which is a directory.";
  215. RETURN_STATUS_UNEXPECTED(oss.str());
  216. }
  217. // Convert to canonical form.
  218. if (strlen(common::SafeCStr(path_)) >= PATH_MAX) {
  219. RETURN_STATUS_UNEXPECTED(strerror(errno));
  220. }
  221. char canonical_path[PATH_MAX] = {0x00};
  222. #if defined(_WIN32) || defined(_WIN64)
  223. auto err = _fullpath(canonical_path, common::SafeCStr(path_), PATH_MAX);
  224. #else
  225. auto err = realpath(common::SafeCStr(path_), canonical_path);
  226. #endif
  227. if (err == nullptr) {
  228. if (errno == ENOENT && create) {
  229. // File doesn't exist and we are to create it. Let's break it down.
  230. auto file_part = Basename();
  231. auto parent_part = ParentPath();
  232. #if defined(_WIN32) || defined(_WIN64)
  233. auto parent_err = _fullpath(canonical_path, common::SafeCStr(parent_part), PATH_MAX);
  234. #else
  235. auto parent_err = realpath(common::SafeCStr(parent_part), canonical_path);
  236. #endif
  237. if (parent_err == nullptr) {
  238. RETURN_STATUS_UNEXPECTED(strerror(errno));
  239. }
  240. auto cur_inx = strlen(canonical_path);
  241. if (cur_inx + file_part.length() >= PATH_MAX) {
  242. RETURN_STATUS_UNEXPECTED(strerror(errno));
  243. }
  244. canonical_path[cur_inx++] = separator_;
  245. if (strncpy_s(canonical_path + cur_inx, PATH_MAX - cur_inx, common::SafeCStr(file_part), file_part.length()) !=
  246. EOK) {
  247. RETURN_STATUS_UNEXPECTED(strerror(errno));
  248. }
  249. } else {
  250. RETURN_STATUS_UNEXPECTED(strerror(errno));
  251. }
  252. }
  253. if (create) {
  254. fd = open(canonical_path, O_CREAT | O_TRUNC | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP);
  255. } else {
  256. fd = open(canonical_path, O_RDWR);
  257. }
  258. if (fd == -1) {
  259. RETURN_STATUS_UNEXPECTED(strerror(errno));
  260. }
  261. *file_descriptor = fd;
  262. return Status::OK();
  263. }
  264. Status Path::CloseFile(int fd) const {
  265. if (close(fd) < 0) {
  266. RETURN_STATUS_UNEXPECTED(strerror(errno));
  267. }
  268. return Status::OK();
  269. }
  270. Status Path::TruncateFile(int fd) const {
  271. int rc = ftruncate(fd, 0);
  272. if (rc == 0) {
  273. return Status::OK();
  274. } else {
  275. RETURN_STATUS_UNEXPECTED(strerror(errno));
  276. }
  277. }
  278. std::string Path::Basename() {
  279. std::size_t found = path_.find_last_of(separator_);
  280. if (found != std::string::npos) {
  281. return path_.substr(found + 1);
  282. } else {
  283. return path_;
  284. }
  285. }
  286. std::shared_ptr<Path::DirIterator> Path::DirIterator::OpenDirectory(Path *f) {
  287. auto it = new (std::nothrow) DirIterator(f);
  288. if (it == nullptr) {
  289. return nullptr;
  290. }
  291. if (it->dp_) {
  292. return std::shared_ptr<DirIterator>(it);
  293. } else {
  294. delete it;
  295. return nullptr;
  296. }
  297. }
  298. Path::DirIterator::~DirIterator() {
  299. if (dp_) {
  300. (void)closedir(dp_);
  301. }
  302. dp_ = nullptr;
  303. dir_ = nullptr;
  304. entry_ = nullptr;
  305. }
  306. Path::DirIterator::DirIterator(Path *f) : dir_(f), dp_(nullptr), entry_(nullptr) {
  307. MS_LOG(DEBUG) << "Open directory " << f->ToString() << ".";
  308. dp_ = opendir(f->ToString().c_str());
  309. }
  310. bool Path::DirIterator::HasNext() {
  311. do {
  312. entry_ = readdir(dp_);
  313. if (entry_) {
  314. if (strcmp(entry_->d_name, ".") == 0 || strcmp(entry_->d_name, "..") == 0) {
  315. continue;
  316. }
  317. }
  318. break;
  319. } while (true);
  320. return (entry_ != nullptr);
  321. }
  322. Path Path::DirIterator::Next() { return (*(this->dir_) / Path(entry_->d_name)); }
  323. Status Path::RealPath(const std::string &path, std::string &realpath_str) {
  324. char real_path[PATH_MAX] = {0};
  325. // input_path is only file_name
  326. #if defined(_WIN32) || defined(_WIN64)
  327. CHECK_FAIL_RETURN_UNEXPECTED(path.length() < PATH_MAX,
  328. "The length of path: " + path + " exceeds limit: " + std::to_string(PATH_MAX));
  329. auto ret = _fullpath(real_path, common::SafeCStr(path), PATH_MAX);
  330. CHECK_FAIL_RETURN_UNEXPECTED(ret != nullptr, "The file " + path + " does not exist.");
  331. #else
  332. CHECK_FAIL_RETURN_UNEXPECTED(path.length() < NAME_MAX,
  333. "The length of path: " + path + " exceeds limit: " + std::to_string(NAME_MAX));
  334. auto ret = realpath(common::SafeCStr(path), real_path);
  335. CHECK_FAIL_RETURN_UNEXPECTED(ret != nullptr, "The file " + path + " does not exist.");
  336. #endif
  337. realpath_str = std::string(real_path);
  338. return Status::OK();
  339. }
  340. std::ostream &operator<<(std::ostream &os, const Path &s) {
  341. os << s.path_;
  342. return os;
  343. }
  344. } // namespace dataset
  345. } // namespace mindspore