Project

General

Profile

Feature #16249 ยป file_dir_i_empty.patch

zverok (Victor Shepelev), 10/12/2019 09:47 AM

View differences:

dir.c
3381 3381
    return result;
3382 3382
}
3383 3383

  
3384
/*
3385
 * call-seq:
3386
 *   dir.empty?  ->  true or false
3387
 *
3388
 * Returns <code>true</code> if the directory is empty.
3389
 */
3390
static VALUE
3391
dir_empty_p(VALUE dir)
3392
{
3393
    struct dir_data *dirp;
3394
    TypedData_Get_Struct(dir, struct dir_data, &dir_data_type, dirp);
3395

  
3396
    return rb_dir_s_empty_p(rb_cDir, rb_str_dup(dirp->path));
3397
}
3398

  
3384 3399
/*
3385 3400
 *  Objects of class Dir are directory streams representing
3386 3401
 *  directories in the underlying file system. They provide a variety
......
3420 3435
    rb_define_method(rb_cDir,"pos", dir_tell, 0);
3421 3436
    rb_define_method(rb_cDir,"pos=", dir_set_pos, 1);
3422 3437
    rb_define_method(rb_cDir,"close", dir_close, 0);
3438
    rb_define_method(rb_cDir,"empty?", dir_empty_p, 0);
3423 3439

  
3424 3440
    rb_define_singleton_method(rb_cDir,"chdir", dir_s_chdir, -1);
3425 3441
    rb_define_singleton_method(rb_cDir,"getwd", dir_s_getwd, 0);
file.c
2518 2518
    return OFFT2NUM(st.st_size);
2519 2519
}
2520 2520

  
2521
/*
2522
 *  call-seq:
2523
 *     file.empty?  ->  true or false
2524
 *
2525
 *  Returns +true+ if file has a zero size.
2526
 *
2527
 */
2528

  
2529
static VALUE
2530
rb_file_empty_p(VALUE obj)
2531
{
2532
    rb_io_t *fptr;
2533
    struct stat st;
2534

  
2535
    GetOpenFile(obj, fptr);
2536
    if (fptr->mode & FMODE_WRITABLE) {
2537
        rb_io_flush_raw(obj, 0);
2538
    }
2539
    if (fstat(fptr->fd, &st) == -1) {
2540
        return Qfalse;
2541
    }
2542
    return st.st_size == 0 ? Qtrue : Qfalse;
2543
}
2544

  
2521 2545
static int
2522 2546
chmod_internal(const char *path, void *mode)
2523 2547
{
......
6556 6580
    rb_define_method(rb_cFile, "ctime", rb_file_ctime, 0);
6557 6581
    rb_define_method(rb_cFile, "birthtime", rb_file_birthtime, 0);
6558 6582
    rb_define_method(rb_cFile, "size", rb_file_size, 0);
6583
    rb_define_method(rb_cFile, "empty?", rb_file_empty_p, 0);
6559 6584

  
6560 6585
    rb_define_method(rb_cFile, "chmod", rb_file_chmod, 1);
6561 6586
    rb_define_method(rb_cFile, "chown", rb_file_chown, 2);
test/ruby/test_dir.rb
476 476
      assert_equal([*"a".."z"], list)
477 477
    end;
478 478
  end if defined?(Process::RLIMIT_NOFILE)
479

  
480
  def test_instance_empty?
481
    Dir.open(@root) { |dir| assert_not_send([dir, :empty?]) }
482
    a = Dir.open(File.join(@root, "a"))
483
    assert_send([a, :empty?])
484
    %w[A .dot].each do |tmp|
485
      tmp = File.join(a, tmp)
486
      open(tmp, "w") {}
487
      assert_not_send([a, :empty?])
488
      File.delete(tmp)
489
      assert_send([a, :empty?])
490
      Dir.mkdir(tmp)
491
      assert_not_send([a, :empty?])
492
      Dir.rmdir(tmp)
493
      assert_send([a, :empty?])
494
    end
495
  ensure
496
    a&.close
497
  end
479 498
end
test/ruby/test_file.rb
537 537
      assert_file.absolute_path?("/foo/bar\\baz")
538 538
    end
539 539
  end
540

  
541
  def test_empty?
542
    Tempfile.create("test-empty") {|f|
543
      assert_send([f, :empty?])
544
      f.print "abc"
545
      assert_not_send([f, :empty?])
546
    }
547
  end
540 548
end