blob: e55be4209a5c230854173cb37e0344190d68411c [file] [log] [blame]
Elliott Hughes701bec22013-02-25 13:14:31 -08001/*
2 * Copyright (C) 2013 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
17#include <dirent.h>
18
Elliott Hughes6331e802015-10-27 11:10:36 -070019#include <fcntl.h>
Elliott Hughes701bec22013-02-25 13:14:31 -080020#include <errno.h>
21#include <stdlib.h>
Elliott Hughes76f89162015-01-26 13:34:58 -080022#include <string.h>
Elliott Hughes6331e802015-10-27 11:10:36 -070023#include <unistd.h>
Elliott Hughes701bec22013-02-25 13:14:31 -080024
Elliott Hughes8eac9af2014-05-09 19:12:08 -070025#include "private/bionic_macros.h"
Elliott Hughes701bec22013-02-25 13:14:31 -080026#include "private/ScopedReaddir.h"
27
28// A smart pointer to the scandir dirent**.
29class ScandirResult {
30 public:
Elliott Hughes6331e802015-10-27 11:10:36 -070031 ScandirResult() : names_(nullptr), size_(0), capacity_(0) {
Elliott Hughes701bec22013-02-25 13:14:31 -080032 }
33
34 ~ScandirResult() {
35 while (size_ > 0) {
36 free(names_[--size_]);
37 }
38 free(names_);
39 }
40
41 size_t size() {
42 return size_;
43 }
44
45 dirent** release() {
46 dirent** result = names_;
Elliott Hughes6331e802015-10-27 11:10:36 -070047 names_ = nullptr;
Elliott Hughes701bec22013-02-25 13:14:31 -080048 size_ = capacity_ = 0;
49 return result;
50 }
51
52 bool Add(dirent* entry) {
53 if (size_ >= capacity_) {
54 size_t new_capacity = capacity_ + 32;
Elliott Hughes8b5df392015-01-21 16:19:07 -080055 dirent** new_names =
56 reinterpret_cast<dirent**>(realloc(names_, new_capacity * sizeof(dirent*)));
Elliott Hughes6331e802015-10-27 11:10:36 -070057 if (new_names == nullptr) {
Elliott Hughes701bec22013-02-25 13:14:31 -080058 return false;
59 }
60 names_ = new_names;
61 capacity_ = new_capacity;
62 }
63
64 dirent* copy = CopyDirent(entry);
Elliott Hughes6331e802015-10-27 11:10:36 -070065 if (copy == nullptr) {
Elliott Hughes701bec22013-02-25 13:14:31 -080066 return false;
67 }
68 names_[size_++] = copy;
69 return true;
70 }
71
72 void Sort(int (*comparator)(const dirent**, const dirent**)) {
73 // If we have entries and a comparator, sort them.
Elliott Hughes6331e802015-10-27 11:10:36 -070074 if (size_ > 0 && comparator != nullptr) {
Elliott Hughes8b5df392015-01-21 16:19:07 -080075 qsort(names_, size_, sizeof(dirent*),
76 reinterpret_cast<int (*)(const void*, const void*)>(comparator));
Elliott Hughes701bec22013-02-25 13:14:31 -080077 }
78 }
79
80 private:
81 dirent** names_;
82 size_t size_;
83 size_t capacity_;
84
85 static dirent* CopyDirent(dirent* original) {
86 // Allocate the minimum number of bytes necessary, rounded up to a 4-byte boundary.
87 size_t size = ((original->d_reclen + 3) & ~3);
Elliott Hughes8b5df392015-01-21 16:19:07 -080088 dirent* copy = reinterpret_cast<dirent*>(malloc(size));
Elliott Hughes701bec22013-02-25 13:14:31 -080089 memcpy(copy, original, original->d_reclen);
90 return copy;
91 }
92
Elliott Hughes8eac9af2014-05-09 19:12:08 -070093 DISALLOW_COPY_AND_ASSIGN(ScandirResult);
Elliott Hughes701bec22013-02-25 13:14:31 -080094};
95
Elliott Hughes6331e802015-10-27 11:10:36 -070096int scandirat(int parent_fd, const char* dir_name, dirent*** name_list,
97 int (*filter)(const dirent*),
98 int (*comparator)(const dirent**, const dirent**)) {
99 DIR* dir = nullptr;
100 if (parent_fd == AT_FDCWD) {
101 dir = opendir(dir_name);
102 } else {
103 int dir_fd = openat(parent_fd, dir_name, O_CLOEXEC | O_DIRECTORY | O_RDONLY);
104 if (dir_fd != -1) {
105 dir = fdopendir(dir_fd);
106 }
107 }
108
109 ScopedReaddir reader(dir);
Elliott Hughes701bec22013-02-25 13:14:31 -0800110 if (reader.IsBad()) {
111 return -1;
112 }
113
114 ScandirResult names;
115 dirent* entry;
Elliott Hughes6331e802015-10-27 11:10:36 -0700116 while ((entry = reader.ReadEntry()) != nullptr) {
Elliott Hughes701bec22013-02-25 13:14:31 -0800117 // If we have a filter, skip names that don't match.
Elliott Hughes6331e802015-10-27 11:10:36 -0700118 if (filter != nullptr && !(*filter)(entry)) {
Elliott Hughes701bec22013-02-25 13:14:31 -0800119 continue;
120 }
121 names.Add(entry);
122 }
123
124 names.Sort(comparator);
125
126 size_t size = names.size();
127 *name_list = names.release();
128 return size;
129}
Elliott Hughes6331e802015-10-27 11:10:36 -0700130__strong_alias(scandirat64, scandirat);
131
132int scandir(const char* dir_path, dirent*** name_list,
133 int (*filter)(const dirent*),
134 int (*comparator)(const dirent**, const dirent**)) {
135 return scandirat(AT_FDCWD, dir_path, name_list, filter, comparator);
136}
Elliott Hughesdb1ea342014-01-17 18:42:49 -0800137__strong_alias(scandir64, scandir);