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

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