I am using a GNU/Linux system.
Firstly, I have:
Moved to the
/tmp/
directory.Created a file called
ruby.rb
as a non-root user.Opened
irb
as su user.
Now in IRB:
┌┄┄[root::archlinux]┈[/tmp]
└──╼⮚ irb
irb(main):001:0> File.writable?('ruby.rb')
=> true
irb(main):002:0> File.stat('ruby.rb')
=> #<File::Stat dev=0x2d, ino=819138, mode=0100644, nlink=1, uid=1000, gid=1000, rdev=0x0, size=0, blksize=4096, blocks=0, atime=2019-07-14 04:44:13 +0530, mtime=2019-07-14 04:44:13 +0530, ctime=2019-07-14 04:44:13 +0530>
irb(main):003:0> File.write('ruby.rb', '#!/usr/bin/ruby -w')
Traceback (most recent call last):
3: from /root/.irb:351:in `<main>'
2: from (irb):3
1: from (irb):3:in `write'
Errno::EACCES (Permission denied @ rb_sysopen - ruby.rb)
irb(main):004:0>
Apart from this, actually I am trying to do is writing a log file. I am checking if the file is writable. If not, it sends notification to the user.
I have faced the problem previously where File#writable?(str)
returned true in such case (and it works on the /tmp/
directory). And just used a begin <...> rescue Errno::EACCES
block to fix the problem. But in the current project, I don't want to use a rescue block.
Why does the File#writable?(str)
return true in the first place?
EDIT: First of all this should be moved to unix.stackexchange.
Secondly I understood
You can't modify a file on a tmp filesystem: I have mounted a mere 4 MiB partition as tmpfs on /mnt. Created a file as local user. Then changed the permission to omnipotent 777! Then I changed my account to root. I tried to edit the contents with nano. It's not possible.
Ruby's File#writable? probably doesn't detect the mounted filesystem type. Whether it's XFS or EXT4 or procfs or tmpfs. If all it does is checking the mode, in such scenario, the File#writable? returns true while in reality, it's not actually writable!
Edit 2: I have uploaded 2 screenshots at imgur: