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 8.6 kB

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