Feature #16249 ยป file_dir_i_empty.patch
dir.c | ||
---|---|---|
return result;
|
||
}
|
||
/*
|
||
* call-seq:
|
||
* dir.empty? -> true or false
|
||
*
|
||
* Returns <code>true</code> if the directory is empty.
|
||
*/
|
||
static VALUE
|
||
dir_empty_p(VALUE dir)
|
||
{
|
||
struct dir_data *dirp;
|
||
TypedData_Get_Struct(dir, struct dir_data, &dir_data_type, dirp);
|
||
return rb_dir_s_empty_p(rb_cDir, rb_str_dup(dirp->path));
|
||
}
|
||
/*
|
||
* Objects of class Dir are directory streams representing
|
||
* directories in the underlying file system. They provide a variety
|
||
... | ... | |
rb_define_method(rb_cDir,"pos", dir_tell, 0);
|
||
rb_define_method(rb_cDir,"pos=", dir_set_pos, 1);
|
||
rb_define_method(rb_cDir,"close", dir_close, 0);
|
||
rb_define_method(rb_cDir,"empty?", dir_empty_p, 0);
|
||
rb_define_singleton_method(rb_cDir,"chdir", dir_s_chdir, -1);
|
||
rb_define_singleton_method(rb_cDir,"getwd", dir_s_getwd, 0);
|
file.c | ||
---|---|---|
return OFFT2NUM(st.st_size);
|
||
}
|
||
/*
|
||
* call-seq:
|
||
* file.empty? -> true or false
|
||
*
|
||
* Returns +true+ if file has a zero size.
|
||
*
|
||
*/
|
||
static VALUE
|
||
rb_file_empty_p(VALUE obj)
|
||
{
|
||
rb_io_t *fptr;
|
||
struct stat st;
|
||
GetOpenFile(obj, fptr);
|
||
if (fptr->mode & FMODE_WRITABLE) {
|
||
rb_io_flush_raw(obj, 0);
|
||
}
|
||
if (fstat(fptr->fd, &st) == -1) {
|
||
return Qfalse;
|
||
}
|
||
return st.st_size == 0 ? Qtrue : Qfalse;
|
||
}
|
||
static int
|
||
chmod_internal(const char *path, void *mode)
|
||
{
|
||
... | ... | |
rb_define_method(rb_cFile, "ctime", rb_file_ctime, 0);
|
||
rb_define_method(rb_cFile, "birthtime", rb_file_birthtime, 0);
|
||
rb_define_method(rb_cFile, "size", rb_file_size, 0);
|
||
rb_define_method(rb_cFile, "empty?", rb_file_empty_p, 0);
|
||
rb_define_method(rb_cFile, "chmod", rb_file_chmod, 1);
|
||
rb_define_method(rb_cFile, "chown", rb_file_chown, 2);
|
test/ruby/test_dir.rb | ||
---|---|---|
assert_equal([*"a".."z"], list)
|
||
end;
|
||
end if defined?(Process::RLIMIT_NOFILE)
|
||
def test_instance_empty?
|
||
Dir.open(@root) { |dir| assert_not_send([dir, :empty?]) }
|
||
a = Dir.open(File.join(@root, "a"))
|
||
assert_send([a, :empty?])
|
||
%w[A .dot].each do |tmp|
|
||
tmp = File.join(a, tmp)
|
||
open(tmp, "w") {}
|
||
assert_not_send([a, :empty?])
|
||
File.delete(tmp)
|
||
assert_send([a, :empty?])
|
||
Dir.mkdir(tmp)
|
||
assert_not_send([a, :empty?])
|
||
Dir.rmdir(tmp)
|
||
assert_send([a, :empty?])
|
||
end
|
||
ensure
|
||
a&.close
|
||
end
|
||
end
|
test/ruby/test_file.rb | ||
---|---|---|
assert_file.absolute_path?("/foo/bar\\baz")
|
||
end
|
||
end
|
||
def test_empty?
|
||
Tempfile.create("test-empty") {|f|
|
||
assert_send([f, :empty?])
|
||
f.print "abc"
|
||
assert_not_send([f, :empty?])
|
||
}
|
||
end
|
||
end
|