fs.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (C) 2012 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. #define LOG_TAG "cutils"
  17. #include <cutils/fs.h>
  18. #include <cutils/log.h>
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #include <fcntl.h>
  22. #include <unistd.h>
  23. #include <errno.h>
  24. #include <string.h>
  25. #include <limits.h>
  26. #define ALL_PERMS (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
  27. #define BUF_SIZE 64
  28. int fs_prepare_dir(const char* path, mode_t mode, uid_t uid, gid_t gid) {
  29. // Check if path needs to be created
  30. struct stat sb;
  31. if (TEMP_FAILURE_RETRY(lstat(path, &sb)) == -1) {
  32. if (errno == ENOENT) {
  33. goto create;
  34. } else {
  35. ALOGE("Failed to lstat(%s): %s", path, strerror(errno));
  36. return -1;
  37. }
  38. }
  39. // Exists, verify status
  40. if (!S_ISDIR(sb.st_mode)) {
  41. ALOGE("Not a directory: %s", path);
  42. return -1;
  43. }
  44. if (((sb.st_mode & ALL_PERMS) == mode) && (sb.st_uid == uid) && (sb.st_gid == gid)) {
  45. return 0;
  46. } else {
  47. goto fixup;
  48. }
  49. create:
  50. if (TEMP_FAILURE_RETRY(mkdir(path, mode)) == -1) {
  51. if (errno != EEXIST) {
  52. ALOGE("Failed to mkdir(%s): %s", path, strerror(errno));
  53. return -1;
  54. }
  55. }
  56. fixup:
  57. if (TEMP_FAILURE_RETRY(chmod(path, mode)) == -1) {
  58. ALOGE("Failed to chmod(%s, %d): %s", path, mode, strerror(errno));
  59. return -1;
  60. }
  61. if (TEMP_FAILURE_RETRY(chown(path, uid, gid)) == -1) {
  62. ALOGE("Failed to chown(%s, %d, %d): %s", path, uid, gid, strerror(errno));
  63. return -1;
  64. }
  65. return 0;
  66. }
  67. int fs_read_atomic_int(const char* path, int* out_value) {
  68. int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY));
  69. if (fd == -1) {
  70. ALOGE("Failed to read %s: %s", path, strerror(errno));
  71. return -1;
  72. }
  73. char buf[BUF_SIZE];
  74. if (TEMP_FAILURE_RETRY(read(fd, buf, BUF_SIZE)) == -1) {
  75. ALOGE("Failed to read %s: %s", path, strerror(errno));
  76. goto fail;
  77. }
  78. if (sscanf(buf, "%d", out_value) != 1) {
  79. ALOGE("Failed to parse %s: %s", path, strerror(errno));
  80. goto fail;
  81. }
  82. close(fd);
  83. return 0;
  84. fail:
  85. close(fd);
  86. *out_value = -1;
  87. return -1;
  88. }
  89. int fs_write_atomic_int(const char* path, int value) {
  90. char temp[PATH_MAX];
  91. if (snprintf(temp, PATH_MAX, "%s.XXXXXX", path) >= PATH_MAX) {
  92. ALOGE("Path too long");
  93. return -1;
  94. }
  95. int fd = TEMP_FAILURE_RETRY(mkstemp(temp));
  96. if (fd == -1) {
  97. ALOGE("Failed to open %s: %s", temp, strerror(errno));
  98. return -1;
  99. }
  100. char buf[BUF_SIZE];
  101. int len = snprintf(buf, BUF_SIZE, "%d", value) + 1;
  102. if (len > BUF_SIZE) {
  103. ALOGE("Value %d too large: %s", value, strerror(errno));
  104. goto fail;
  105. }
  106. if (TEMP_FAILURE_RETRY(write(fd, buf, len)) < len) {
  107. ALOGE("Failed to write %s: %s", temp, strerror(errno));
  108. goto fail;
  109. }
  110. if (close(fd) == -1) {
  111. ALOGE("Failed to close %s: %s", temp, strerror(errno));
  112. goto fail_closed;
  113. }
  114. if (rename(temp, path) == -1) {
  115. ALOGE("Failed to rename %s to %s: %s", temp, path, strerror(errno));
  116. goto fail_closed;
  117. }
  118. return 0;
  119. fail:
  120. close(fd);
  121. fail_closed:
  122. unlink(temp);
  123. return -1;
  124. }