2012-10-26 05:55:23 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
|
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <dirent.h>
|
2013-02-25 22:14:31 +01:00
|
|
|
|
2012-10-26 05:55:23 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2015-01-29 03:02:33 +01:00
|
|
|
#include <malloc.h>
|
|
|
|
#include <string.h>
|
2012-10-26 05:55:23 +02:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2018-06-02 00:30:54 +02:00
|
|
|
#include <android/fdsan.h>
|
|
|
|
|
2017-10-27 00:38:06 +02:00
|
|
|
#include "private/bionic_fortify.h"
|
2013-02-12 17:40:24 +01:00
|
|
|
#include "private/ErrnoRestorer.h"
|
|
|
|
#include "private/ScopedPthreadMutexLocker.h"
|
2012-10-26 05:55:23 +02:00
|
|
|
|
2014-06-07 00:24:20 +02:00
|
|
|
extern "C" int __getdents64(unsigned int, dirent*, unsigned int);
|
2014-06-06 20:44:55 +02:00
|
|
|
|
2015-06-04 22:30:45 +02:00
|
|
|
// Apportable decided to copy the data structure from this file
|
|
|
|
// and use it in their own code, but they also call into readdir.
|
|
|
|
// In order to avoid a lockup, the structure must be maintained in
|
|
|
|
// the exact same order as in L and below. New structure members
|
|
|
|
// need to be added to the end of this structure.
|
|
|
|
// See b/21037208 for more details.
|
2012-10-26 05:55:23 +02:00
|
|
|
struct DIR {
|
|
|
|
int fd_;
|
|
|
|
size_t available_bytes_;
|
|
|
|
dirent* next_;
|
|
|
|
pthread_mutex_t mutex_;
|
|
|
|
dirent buff_[15];
|
2015-06-04 22:30:45 +02:00
|
|
|
long current_pos_;
|
2012-10-26 05:55:23 +02:00
|
|
|
};
|
|
|
|
|
2017-10-27 00:38:06 +02:00
|
|
|
#define CHECK_DIR(d) if (d == nullptr) __fortify_fatal("%s: null DIR*", __FUNCTION__)
|
|
|
|
|
2018-06-02 00:30:54 +02:00
|
|
|
static uint64_t __get_dir_tag(DIR* dir) {
|
|
|
|
return android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_DIR,
|
|
|
|
reinterpret_cast<uint64_t>(dir));
|
|
|
|
}
|
|
|
|
|
2012-10-26 05:55:23 +02:00
|
|
|
static DIR* __allocate_DIR(int fd) {
|
|
|
|
DIR* d = reinterpret_cast<DIR*>(malloc(sizeof(DIR)));
|
2018-08-03 02:31:13 +02:00
|
|
|
if (d == nullptr) {
|
|
|
|
return nullptr;
|
2012-10-26 05:55:23 +02:00
|
|
|
}
|
|
|
|
d->fd_ = fd;
|
2018-06-02 00:30:54 +02:00
|
|
|
android_fdsan_exchange_owner_tag(fd, 0, __get_dir_tag(d));
|
2012-10-26 05:55:23 +02:00
|
|
|
d->available_bytes_ = 0;
|
2018-08-03 02:31:13 +02:00
|
|
|
d->next_ = nullptr;
|
2014-11-07 04:55:09 +01:00
|
|
|
d->current_pos_ = 0L;
|
2018-08-03 02:31:13 +02:00
|
|
|
pthread_mutex_init(&d->mutex_, nullptr);
|
2012-10-26 05:55:23 +02:00
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2017-10-27 00:38:06 +02:00
|
|
|
int dirfd(DIR* d) {
|
|
|
|
CHECK_DIR(d);
|
|
|
|
return d->fd_;
|
2012-10-26 05:55:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DIR* fdopendir(int fd) {
|
|
|
|
// Is 'fd' actually a directory?
|
|
|
|
struct stat sb;
|
|
|
|
if (fstat(fd, &sb) == -1) {
|
2018-08-03 02:31:13 +02:00
|
|
|
return nullptr;
|
2012-10-26 05:55:23 +02:00
|
|
|
}
|
|
|
|
if (!S_ISDIR(sb.st_mode)) {
|
|
|
|
errno = ENOTDIR;
|
2018-08-03 02:31:13 +02:00
|
|
|
return nullptr;
|
2012-10-26 05:55:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return __allocate_DIR(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
DIR* opendir(const char* path) {
|
2014-08-27 01:20:59 +02:00
|
|
|
int fd = open(path, O_CLOEXEC | O_DIRECTORY | O_RDONLY);
|
2018-08-03 02:31:13 +02:00
|
|
|
return (fd != -1) ? __allocate_DIR(fd) : nullptr;
|
2012-10-26 05:55:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool __fill_DIR(DIR* d) {
|
2017-10-27 00:38:06 +02:00
|
|
|
CHECK_DIR(d);
|
2014-06-06 20:44:55 +02:00
|
|
|
int rc = TEMP_FAILURE_RETRY(__getdents64(d->fd_, d->buff_, sizeof(d->buff_)));
|
2012-10-26 05:55:23 +02:00
|
|
|
if (rc <= 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
d->available_bytes_ = rc;
|
|
|
|
d->next_ = d->buff_;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static dirent* __readdir_locked(DIR* d) {
|
|
|
|
if (d->available_bytes_ == 0 && !__fill_DIR(d)) {
|
2018-08-03 02:31:13 +02:00
|
|
|
return nullptr;
|
2012-10-26 05:55:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
dirent* entry = d->next_;
|
|
|
|
d->next_ = reinterpret_cast<dirent*>(reinterpret_cast<char*>(entry) + entry->d_reclen);
|
|
|
|
d->available_bytes_ -= entry->d_reclen;
|
2014-11-07 04:55:09 +01:00
|
|
|
// The directory entry offset uses 0, 1, 2 instead of real file offset,
|
|
|
|
// so the value range of long type is enough.
|
|
|
|
d->current_pos_ = static_cast<long>(entry->d_off);
|
2012-10-26 05:55:23 +02:00
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
dirent* readdir(DIR* d) {
|
2017-10-27 00:38:06 +02:00
|
|
|
CHECK_DIR(d);
|
2012-10-26 05:55:23 +02:00
|
|
|
ScopedPthreadMutexLocker locker(&d->mutex_);
|
|
|
|
return __readdir_locked(d);
|
|
|
|
}
|
Implement some of the missing LFS64 support.
This gives us:
* <dirent.h>
struct dirent64
readdir64, readdir64_r, alphasort64, scandir64
* <fcntl.h>
creat64, openat64, open64.
* <sys/stat.h>
struct stat64
fstat64, fstatat64, lstat64, stat64.
* <sys/statvfs.h>
struct statvfs64
statvfs64, fstatvfs64.
* <sys/vfs.h>
struct statfs64
statfs64, fstatfs64.
This also removes some of the incorrect #define hacks we've had in the
past (for stat64, for example, which we promised to clean up way back
in bug 8472078).
Bug: 11865851
Bug: 8472078
Change-Id: Ia46443521918519f2dfa64d4621027dfd13ac566
2014-01-18 03:42:49 +01:00
|
|
|
__strong_alias(readdir64, readdir);
|
2012-10-26 05:55:23 +02:00
|
|
|
|
|
|
|
int readdir_r(DIR* d, dirent* entry, dirent** result) {
|
2017-10-27 00:38:06 +02:00
|
|
|
CHECK_DIR(d);
|
|
|
|
|
2013-02-12 17:40:24 +01:00
|
|
|
ErrnoRestorer errno_restorer;
|
2012-10-26 05:55:23 +02:00
|
|
|
|
2018-08-03 02:31:13 +02:00
|
|
|
*result = nullptr;
|
2012-10-26 05:55:23 +02:00
|
|
|
errno = 0;
|
|
|
|
|
|
|
|
ScopedPthreadMutexLocker locker(&d->mutex_);
|
|
|
|
|
|
|
|
dirent* next = __readdir_locked(d);
|
2018-08-03 02:31:13 +02:00
|
|
|
if (errno != 0 && next == nullptr) {
|
2012-10-26 05:55:23 +02:00
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
|
2018-08-03 02:31:13 +02:00
|
|
|
if (next != nullptr) {
|
2012-10-26 05:55:23 +02:00
|
|
|
memcpy(entry, next, next->d_reclen);
|
|
|
|
*result = entry;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
Implement some of the missing LFS64 support.
This gives us:
* <dirent.h>
struct dirent64
readdir64, readdir64_r, alphasort64, scandir64
* <fcntl.h>
creat64, openat64, open64.
* <sys/stat.h>
struct stat64
fstat64, fstatat64, lstat64, stat64.
* <sys/statvfs.h>
struct statvfs64
statvfs64, fstatvfs64.
* <sys/vfs.h>
struct statfs64
statfs64, fstatfs64.
This also removes some of the incorrect #define hacks we've had in the
past (for stat64, for example, which we promised to clean up way back
in bug 8472078).
Bug: 11865851
Bug: 8472078
Change-Id: Ia46443521918519f2dfa64d4621027dfd13ac566
2014-01-18 03:42:49 +01:00
|
|
|
__strong_alias(readdir64_r, readdir_r);
|
2012-10-26 05:55:23 +02:00
|
|
|
|
|
|
|
int closedir(DIR* d) {
|
2018-08-03 02:31:13 +02:00
|
|
|
if (d == nullptr) {
|
2012-10-26 05:55:23 +02:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fd = d->fd_;
|
|
|
|
pthread_mutex_destroy(&d->mutex_);
|
2018-06-02 00:30:54 +02:00
|
|
|
int rc = android_fdsan_close_with_tag(fd, __get_dir_tag(d));
|
2012-10-26 05:55:23 +02:00
|
|
|
free(d);
|
2018-06-02 00:30:54 +02:00
|
|
|
return rc;
|
2012-10-26 05:55:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void rewinddir(DIR* d) {
|
2017-10-27 00:38:06 +02:00
|
|
|
CHECK_DIR(d);
|
|
|
|
|
2012-10-26 05:55:23 +02:00
|
|
|
ScopedPthreadMutexLocker locker(&d->mutex_);
|
|
|
|
lseek(d->fd_, 0, SEEK_SET);
|
|
|
|
d->available_bytes_ = 0;
|
2014-11-07 04:55:09 +01:00
|
|
|
d->current_pos_ = 0L;
|
|
|
|
}
|
|
|
|
|
|
|
|
void seekdir(DIR* d, long offset) {
|
2017-10-27 00:38:06 +02:00
|
|
|
CHECK_DIR(d);
|
|
|
|
|
2014-11-07 04:55:09 +01:00
|
|
|
ScopedPthreadMutexLocker locker(&d->mutex_);
|
|
|
|
off_t ret = lseek(d->fd_, offset, SEEK_SET);
|
|
|
|
if (ret != -1L) {
|
|
|
|
d->available_bytes_ = 0;
|
|
|
|
d->current_pos_ = ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
long telldir(DIR* d) {
|
2017-10-27 00:38:06 +02:00
|
|
|
CHECK_DIR(d);
|
|
|
|
|
2014-11-07 04:55:09 +01:00
|
|
|
return d->current_pos_;
|
2012-10-26 05:55:23 +02:00
|
|
|
}
|
|
|
|
|
2013-02-25 22:14:31 +01:00
|
|
|
int alphasort(const dirent** a, const dirent** b) {
|
2012-10-29 15:32:54 +01:00
|
|
|
return strcoll((*a)->d_name, (*b)->d_name);
|
|
|
|
}
|
Implement some of the missing LFS64 support.
This gives us:
* <dirent.h>
struct dirent64
readdir64, readdir64_r, alphasort64, scandir64
* <fcntl.h>
creat64, openat64, open64.
* <sys/stat.h>
struct stat64
fstat64, fstatat64, lstat64, stat64.
* <sys/statvfs.h>
struct statvfs64
statvfs64, fstatvfs64.
* <sys/vfs.h>
struct statfs64
statfs64, fstatfs64.
This also removes some of the incorrect #define hacks we've had in the
past (for stat64, for example, which we promised to clean up way back
in bug 8472078).
Bug: 11865851
Bug: 8472078
Change-Id: Ia46443521918519f2dfa64d4621027dfd13ac566
2014-01-18 03:42:49 +01:00
|
|
|
__strong_alias(alphasort64, alphasort);
|