FileSystem.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (C) 2018 The Android Open Source Project
  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 <vintf/FileSystem.h>
  17. #include <dirent.h>
  18. #include <fstream>
  19. #include <iostream>
  20. #include <sstream>
  21. namespace android {
  22. namespace vintf {
  23. namespace details {
  24. status_t FileSystemImpl::fetch(const std::string& path, std::string* fetched,
  25. std::string* error) const {
  26. std::ifstream in;
  27. errno = 0;
  28. in.open(path);
  29. if (!in || errno != 0) {
  30. if (error) {
  31. *error = "Cannot open " + path + ": " + strerror(errno);
  32. }
  33. return -errno;
  34. }
  35. std::stringstream ss;
  36. ss << in.rdbuf();
  37. *fetched = ss.str();
  38. return -errno;
  39. }
  40. status_t FileSystemImpl::listFiles(const std::string& path, std::vector<std::string>* out,
  41. std::string* error) const {
  42. errno = 0;
  43. DIR* dirp = opendir(path.c_str());
  44. if (!dirp || errno != 0) {
  45. if (error) {
  46. *error = "Cannot open " + path + ": " + strerror(errno);
  47. }
  48. return -errno;
  49. }
  50. std::unique_ptr<DIR, decltype(&closedir)> dir(dirp, closedir);
  51. dirent* dp;
  52. while ((dp = readdir(dir.get())) != nullptr) {
  53. if (dp->d_type != DT_DIR) {
  54. out->push_back(dp->d_name);
  55. }
  56. }
  57. return -errno;
  58. }
  59. status_t FileSystemNoOp::fetch(const std::string&, std::string*, std::string*) const {
  60. return NAME_NOT_FOUND;
  61. }
  62. status_t FileSystemNoOp::listFiles(const std::string&, std::vector<std::string>*,
  63. std::string*) const {
  64. return NAME_NOT_FOUND;
  65. }
  66. FileSystemUnderPath::FileSystemUnderPath(const std::string& rootdir) {
  67. mRootDir = rootdir;
  68. if (!mRootDir.empty() && mRootDir.back() != '/') {
  69. mRootDir.push_back('/');
  70. }
  71. }
  72. status_t FileSystemUnderPath::fetch(const std::string& path, std::string* fetched,
  73. std::string* error) const {
  74. return mImpl.fetch(mRootDir + path, fetched, error);
  75. }
  76. status_t FileSystemUnderPath::listFiles(const std::string& path, std::vector<std::string>* out,
  77. std::string* error) const {
  78. return mImpl.listFiles(mRootDir + path, out, error);
  79. }
  80. const std::string& FileSystemUnderPath::getRootDir() const {
  81. return mRootDir;
  82. }
  83. } // namespace details
  84. } // namespace vintf
  85. } // namespace android