Loading include/cutils/fs.h +15 −0 Original line number Original line Diff line number Diff line Loading @@ -19,6 +19,21 @@ #include <sys/types.h> #include <sys/types.h> /* * TEMP_FAILURE_RETRY is defined by some, but not all, versions of * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's * not already defined, then define it here. */ #ifndef TEMP_FAILURE_RETRY /* Used to retry syscalls that can return EINTR. */ #define TEMP_FAILURE_RETRY(exp) ({ \ typeof (exp) _rc; \ do { \ _rc = (exp); \ } while (_rc == -1 && errno == EINTR); \ _rc; }) #endif #ifdef __cplusplus #ifdef __cplusplus extern "C" { extern "C" { #endif #endif Loading libcutils/fs.c +8 −6 Original line number Original line Diff line number Diff line Loading @@ -14,6 +14,8 @@ * limitations under the License. * limitations under the License. */ */ #define LOG_TAG "cutils" #include <cutils/fs.h> #include <cutils/fs.h> #include <cutils/log.h> #include <cutils/log.h> Loading @@ -31,11 +33,11 @@ int fs_prepare_dir(const char* path, mode_t mode, uid_t uid, gid_t gid) { int fs_prepare_dir(const char* path, mode_t mode, uid_t uid, gid_t gid) { // Check if path needs to be created // Check if path needs to be created struct stat sb; struct stat sb; if (lstat(path, &sb) == -1) { if (TEMP_FAILURE_RETRY(lstat(path, &sb)) == -1) { if (errno == ENOENT) { if (errno == ENOENT) { goto create; goto create; } else { } else { ALOGE("Failed to stat(%s): %s", path, strerror(errno)); ALOGE("Failed to lstat(%s): %s", path, strerror(errno)); return -1; return -1; } } } } Loading @@ -52,17 +54,17 @@ int fs_prepare_dir(const char* path, mode_t mode, uid_t uid, gid_t gid) { } } create: create: if (mkdir(path, mode) == -1) { if (TEMP_FAILURE_RETRY(mkdir(path, mode)) == -1) { ALOGE("Failed to mkdir(%s): %s", path, strerror(errno)); ALOGE("Failed to mkdir(%s): %s", path, strerror(errno)); return -1; return -1; } } fixup: fixup: if (chmod(path, mode) == -1) { if (TEMP_FAILURE_RETRY(chmod(path, mode)) == -1) { ALOGE("Failed to chown(%s, %d): %s", path, mode, strerror(errno)); ALOGE("Failed to chmod(%s, %d): %s", path, mode, strerror(errno)); return -1; return -1; } } if (chown(path, uid, gid) == -1) { if (TEMP_FAILURE_RETRY(chown(path, uid, gid)) == -1) { ALOGE("Failed to chown(%s, %d, %d): %s", path, uid, gid, strerror(errno)); ALOGE("Failed to chown(%s, %d, %d): %s", path, uid, gid, strerror(errno)); return -1; return -1; } } Loading Loading
include/cutils/fs.h +15 −0 Original line number Original line Diff line number Diff line Loading @@ -19,6 +19,21 @@ #include <sys/types.h> #include <sys/types.h> /* * TEMP_FAILURE_RETRY is defined by some, but not all, versions of * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's * not already defined, then define it here. */ #ifndef TEMP_FAILURE_RETRY /* Used to retry syscalls that can return EINTR. */ #define TEMP_FAILURE_RETRY(exp) ({ \ typeof (exp) _rc; \ do { \ _rc = (exp); \ } while (_rc == -1 && errno == EINTR); \ _rc; }) #endif #ifdef __cplusplus #ifdef __cplusplus extern "C" { extern "C" { #endif #endif Loading
libcutils/fs.c +8 −6 Original line number Original line Diff line number Diff line Loading @@ -14,6 +14,8 @@ * limitations under the License. * limitations under the License. */ */ #define LOG_TAG "cutils" #include <cutils/fs.h> #include <cutils/fs.h> #include <cutils/log.h> #include <cutils/log.h> Loading @@ -31,11 +33,11 @@ int fs_prepare_dir(const char* path, mode_t mode, uid_t uid, gid_t gid) { int fs_prepare_dir(const char* path, mode_t mode, uid_t uid, gid_t gid) { // Check if path needs to be created // Check if path needs to be created struct stat sb; struct stat sb; if (lstat(path, &sb) == -1) { if (TEMP_FAILURE_RETRY(lstat(path, &sb)) == -1) { if (errno == ENOENT) { if (errno == ENOENT) { goto create; goto create; } else { } else { ALOGE("Failed to stat(%s): %s", path, strerror(errno)); ALOGE("Failed to lstat(%s): %s", path, strerror(errno)); return -1; return -1; } } } } Loading @@ -52,17 +54,17 @@ int fs_prepare_dir(const char* path, mode_t mode, uid_t uid, gid_t gid) { } } create: create: if (mkdir(path, mode) == -1) { if (TEMP_FAILURE_RETRY(mkdir(path, mode)) == -1) { ALOGE("Failed to mkdir(%s): %s", path, strerror(errno)); ALOGE("Failed to mkdir(%s): %s", path, strerror(errno)); return -1; return -1; } } fixup: fixup: if (chmod(path, mode) == -1) { if (TEMP_FAILURE_RETRY(chmod(path, mode)) == -1) { ALOGE("Failed to chown(%s, %d): %s", path, mode, strerror(errno)); ALOGE("Failed to chmod(%s, %d): %s", path, mode, strerror(errno)); return -1; return -1; } } if (chown(path, uid, gid) == -1) { if (TEMP_FAILURE_RETRY(chown(path, uid, gid)) == -1) { ALOGE("Failed to chown(%s, %d, %d): %s", path, uid, gid, strerror(errno)); ALOGE("Failed to chown(%s, %d, %d): %s", path, uid, gid, strerror(errno)); return -1; return -1; } } Loading