empty?(p1)
public
Returns true if the named file is an empty directory, false if it is not a
directory or non-empty.
Show source
static VALUE
rb_dir_s_empty_p(VALUE obj, VALUE dirname)
{
DIR *dir;
struct dirent *dp;
VALUE result = Qtrue, orig;
const char *path;
enum {false_on_notdir = 1};
FilePathValue(dirname);
orig = rb_str_dup_frozen(dirname);
dirname = rb_str_encode_ospath(dirname);
dirname = rb_str_dup_frozen(dirname);
path = RSTRING_PTR(dirname);
{
u_int32_t attrbuf[SIZEUP32(fsobj_tag_t)];
struct attrlist al = {ATTR_BIT_MAP_COUNT, 0, ATTR_CMN_OBJTAG,};
if (getattrlist(path, &al, attrbuf, sizeof(attrbuf), 0) != 0)
rb_sys_fail_path(orig);
if (*(const fsobj_tag_t *)(attrbuf+1) == VT_HFS) {
al.commonattr = 0;
al.dirattr = ATTR_DIR_ENTRYCOUNT;
if (getattrlist(path, &al, attrbuf, sizeof(attrbuf), 0) == 0) {
if (attrbuf[0] >= 2 * sizeof(u_int32_t))
return attrbuf[1] ? Qfalse : Qtrue;
if (false_on_notdir) return Qfalse;
}
rb_sys_fail_path(orig);
}
}
dir = opendir(path);
if (!dir) {
int e = errno;
switch (rb_gc_for_fd(e)) {
default:
dir = opendir(path);
if (dir) break;
e = errno;
/* fall through */
case 0:
if (false_on_notdir && e == ENOTDIR) return Qfalse;
rb_syserr_fail_path(e, orig);
}
}
errno = 0;
while ((dp = READDIR(dir, NULL)) != NULL) {
if (!to_be_skipped(dp)) {
result = Qfalse;
break;
}
}
closedir(dir);
return result;
}