Project

General

Profile

Feature #13568 ยป 0001-File-path-Raise-IOError-when-a-file-is-O_TMPFILE.patch

sorah (Sorah Fukumori), 08/31/2017 07:02 AM

View differences:

file.c
379 379
 *  not normalize the name.
380 380
 *
381 381
 *  The pathname may not point the file corresponding to <i>file</i>.
382
 *  e.g. file has been moved, deleted, or created with <code>File::TMPFILE</code> option.
382
 *  For instance, pathname becomes inaccurate when file has been moved or deleted.
383
 *
384
 *  This method raises <code>IOError</code> for a <i>file</i> created using
385
 *  <code>File::Constants::TMPFILE</code> because they don't have a pathname.
383 386
 *
384 387
 *     File.new("testfile").path               #=> "testfile"
385 388
 *     File.new("/tmp/../tmp/xxx", "w").path   #=> "/tmp/../tmp/xxx"
......
393 396

  
394 397
    fptr = RFILE(rb_io_taint_check(obj))->fptr;
395 398
    rb_io_check_initialized(fptr);
396
    if (NIL_P(fptr->pathv)) return Qnil;
399

  
400
    if (NIL_P(fptr->pathv)) {
401
        rb_raise(rb_eIOError, "File is unnamed (TMPFILE?)");
402
    }
403

  
397 404
    return rb_obj_taint(rb_str_dup(fptr->pathv));
398 405
}
399 406

  
io.c
5813 5813
rb_file_open_generic(VALUE io, VALUE filename, int oflags, int fmode,
5814 5814
		     const convconfig_t *convconfig, mode_t perm)
5815 5815
{
5816
    VALUE pathv;
5816 5817
    rb_io_t *fptr;
5817 5818
    convconfig_t cc;
5818 5819
    if (!convconfig) {
......
5828 5829
    MakeOpenFile(io, fptr);
5829 5830
    fptr->mode = fmode;
5830 5831
    fptr->encs = *convconfig;
5831
    fptr->pathv = rb_str_new_frozen(filename);
5832
    fptr->fd = rb_sysopen(fptr->pathv, oflags, perm);
5832
    pathv = rb_str_new_frozen(filename);
5833
#ifdef O_TMPFILE
5834
    if (!(oflags & O_TMPFILE)) {
5835
        fptr->pathv = pathv;
5836
    }
5837
#else
5838
    fptr->pathv = pathv;
5839
#endif
5840
    fptr->fd = rb_sysopen(pathv, oflags, perm);
5833 5841
    io_check_tty(fptr);
5834 5842
    if (fmode & FMODE_SETENC_BY_BOM) io_set_encoding_by_bom(io);
5835 5843

  
test/ruby/test_file.rb
468 468
      assert_file.not_exist?(path)
469 469
    end
470 470
  end
471

  
472
  def test_open_tempfile_path
473
    Dir.mktmpdir(__method__.to_s) do |tmpdir|
474
      File.open(tmpdir, File::RDWR | File::TMPFILE) do |io|
475
        io.write "foo"
476
        io.flush
477
        assert_equal 3, io.size
478
        assert_raise(IOError) { io.path }
479
      end
480
    end
481
  end if File::Constants.const_defined?(:TMPFILE)
482

  
471 483
end
472
-