Module: Bootsnap::LoadPathCache::Native

Defined in:
ext/bootsnap/bootsnap.c

Class Method Summary collapse

Class Method Details

.scan_dir(abspath) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'ext/bootsnap/bootsnap.c', line 155

static VALUE
bs_rb_scan_dir(VALUE self, VALUE abspath)
{
    Check_Type(abspath, T_STRING);

    DIR *dirp = opendir(RSTRING_PTR(abspath));

    VALUE dirs = rb_ary_new();
    VALUE requirables = rb_ary_new();
    VALUE result = rb_ary_new_from_args(2, requirables, dirs);

    if (dirp == NULL) {
        if (errno == ENOTDIR || errno == ENOENT) {
            return result;
        }
        rb_sys_fail("opendir");
        return Qundef;
    }

    struct dirent *entry;
    struct stat st;
    int dfd = -1;

    errno = 0;
    while ((entry = readdir(dirp))) {
        if (entry->d_name[0] == '.') continue;

        if (RB_UNLIKELY(entry->d_type == DT_UNKNOWN || entry->d_type == DT_LNK)) {
            // Note: the original implementation of LoadPathCache did follow symlink.
            // So this is replicated here, but I'm not sure it's a good idea.
            if (dfd < 0) {
                dfd = dirfd(dirp);
                if (dfd < 0) {
                    rb_sys_fail("dirfd");
                    return Qundef;
                }
            }

            if (fstatat(dfd, entry->d_name, &st, 0)) {
                if (errno == ENOENT) {
                    // Broken symlinK
                    continue;
                }
                rb_sys_fail("fstatat");
                return Qundef;
            }

            if (S_ISREG(st.st_mode)) {
                entry->d_type = DT_REG;
            } else if (S_ISDIR(st.st_mode)) {
                entry->d_type = DT_DIR;
            }
        }

        if (entry->d_type == DT_DIR) {
            rb_ary_push(dirs, rb_utf8_str_new_cstr(entry->d_name));
            continue;
        } else if (entry->d_type == DT_REG) {
            size_t len = strlen(entry->d_name);
            bool is_requirable = (
                // Comparing 4B allows compiler to optimize this into a single 32b integer comparison.
                (len > 3 && memcmp(entry->d_name + (len - 3), ".rb", 4) == 0)
                || (len > DLEXT_MAXLEN && memcmp(entry->d_name + (len - DLEXT_MAXLEN), DLEXT, DLEXT_MAXLEN + 1) == 0)
#ifdef DLEXT2
                || (len > DLEXT2_MAXLEN && memcmp(entry->d_name + (len - DLEXT2_MAXLEN), DLEXT2, DLEXT2_MAXLEN + 1) == 0)
#endif
            );
            if (is_requirable) {
                rb_ary_push(requirables, rb_utf8_str_new(entry->d_name, len));
            }
        }
    }

    if (closedir(dirp)) {
        rb_sys_fail("closedir");
        return Qundef;
    }
    return result;
}