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.

file_system.cc 2.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 "utils/system/file_system.h"
  17. #include <sys/stat.h>
  18. #include <unistd.h>
  19. #include <algorithm>
  20. #include <deque>
  21. namespace mindspore {
  22. namespace system {
  23. #if defined(SYSTEM_ENV_POSIX)
  24. // Implement the Posix file systen
  25. WriteFilePtr PosixFileSystem::CreateWriteFile(const string &file_name) {
  26. if (file_name.empty()) {
  27. MS_LOG(ERROR) << "Create write file failed because the file name is null.";
  28. return nullptr;
  29. }
  30. auto fp = std::make_shared<PosixWriteFile>(file_name);
  31. if (fp == nullptr) {
  32. MS_LOG(ERROR) << "Create write file(" << file_name << ") failed.";
  33. return nullptr;
  34. }
  35. bool result = fp->Open();
  36. if (!result) {
  37. MS_LOG(ERROR) << "Open the write file(" << file_name << ") failed.";
  38. return nullptr;
  39. }
  40. return fp;
  41. }
  42. bool PosixFileSystem::FileExist(const string &file_name) {
  43. if (file_name.empty()) {
  44. MS_LOG(WARNING) << "The file name is null.";
  45. return false;
  46. }
  47. auto result = access(file_name.c_str(), F_OK);
  48. if (result != 0) {
  49. MS_LOG(INFO) << "The file(" << file_name << ") not exist.";
  50. return false;
  51. }
  52. return true;
  53. }
  54. bool PosixFileSystem::DeleteFile(const string &file_name) {
  55. if (file_name.empty()) {
  56. MS_LOG(WARNING) << "The file name is null.";
  57. return false;
  58. }
  59. auto result = unlink(file_name.c_str());
  60. if (result != 0) {
  61. MS_LOG(ERROR) << "Delete the file(" << file_name << ") is falire, error(" << errno << ").";
  62. return false;
  63. }
  64. return true;
  65. }
  66. static const int DEFAULT_MKDIR_MODE = 0700;
  67. bool PosixFileSystem::CreateDir(const string &dir_name) {
  68. if (dir_name.empty()) {
  69. MS_LOG(WARNING) << "The directory name is null.";
  70. return false;
  71. }
  72. auto result = mkdir(dir_name.c_str(), DEFAULT_MKDIR_MODE);
  73. if (result != 0) {
  74. MS_LOG(ERROR) << "Create the dir(" << dir_name << ") is falire, error(" << errno << ").";
  75. return false;
  76. }
  77. return true;
  78. }
  79. bool PosixFileSystem::DeleteDir(const string &dir_name) {
  80. if (dir_name.empty()) {
  81. MS_LOG(WARNING) << "The directory name is null.";
  82. return false;
  83. }
  84. auto result = rmdir(dir_name.c_str());
  85. if (result != 0) {
  86. MS_LOG(ERROR) << "Delete the dir(" << dir_name << ") is falire, error(" << errno << ").";
  87. return false;
  88. }
  89. return true;
  90. }
  91. #endif
  92. } // namespace system
  93. } // namespace mindspore