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.5 kB

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