aboutsummaryrefslogtreecommitdiffstats
path: root/file.c
diff options
context:
space:
mode:
authorsorah <sorah@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-08-31 11:14:36 +0000
committersorah <sorah@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-08-31 11:14:36 +0000
commit5214596a34e3eae16320d4590f3a7a8b352970f4 (patch)
tree64a4779a153f45831864a390ef84eb025fe1d4e4 /file.c
parentf1271a5b56a1e5a86a08438e186113f0269ec878 (diff)
downloadruby-5214596a34e3eae16320d4590f3a7a8b352970f4.tar.gz
File#path: Raise IOError when a file is O_TMPFILE
File#path for a file opened with O_TMPFILE has no meaning. A filepath returned by this method isn't guarranteed about its accuracy, but files opened with O_TMPFILE are known its recorded path has no meaning. So let them not to return any pathname. After a discussion in ruby-core, just returning Qnil makes guessing the root cause difficult. Instead, this patch makes the method to raise an error. Other consideration is calling fnctl(2) on rb_file_path, but it adds a overhead, and it's difficult to determine O_TMPFILE status after fd has been closed. [Feature #13568] * io.c(rb_file_open_generic): Set Qnil to fptr->pathv when opening a file using O_TMPFILE * file.c(rb_file_path): Raise IOError when fptr->pathv is Qnil * file.c(rb_file_path): [DOC] Update for the new behavior git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59704 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'file.c')
-rw-r--r--file.c11
1 files changed, 9 insertions, 2 deletions
diff --git a/file.c b/file.c
index 46593e0110..02ec141fb4 100644
--- a/file.c
+++ b/file.c
@@ -379,7 +379,10 @@ apply2files(void (*func)(const char *, VALUE, void *), int argc, VALUE *argv, vo
* not normalize the name.
*
* The pathname may not point the file corresponding to <i>file</i>.
- * e.g. file has been moved, deleted, or created with <code>File::TMPFILE</code> option.
+ * For instance, pathname becomes inaccurate when file has been moved or deleted.
+ *
+ * This method raises <code>IOError</code> for a <i>file</i> created using
+ * <code>File::Constants::TMPFILE</code> because they don't have a pathname.
*
* File.new("testfile").path #=> "testfile"
* File.new("/tmp/../tmp/xxx", "w").path #=> "/tmp/../tmp/xxx"
@@ -393,7 +396,11 @@ rb_file_path(VALUE obj)
fptr = RFILE(rb_io_taint_check(obj))->fptr;
rb_io_check_initialized(fptr);
- if (NIL_P(fptr->pathv)) return Qnil;
+
+ if (NIL_P(fptr->pathv)) {
+ rb_raise(rb_eIOError, "File is unnamed (TMPFILE?)");
+ }
+
return rb_obj_taint(rb_str_dup(fptr->pathv));
}