Create the directory dir and all its parent directories.
For example
require 'fileutils'
FileUtils.mkdir_p('/usr/local/lib/ruby')
Will check all the directories below(If not)Create.
/usr
/usr/local
/usr/local/bin
/usr/local/bin/ruby
Quote https://docs.ruby-lang.org/ja/latest/class/FileUtils.html#M_MAKEDIRS
Linux commands ...
mkdir -p -m 755 ./hoge1/hoge2/hoge3/ruby
ruby
test.rb
require 'fileutils'
FileUtils.mkdir_p('./hoge1/hoge2/hoge3/ruby', mode: 755)
Wonder?
Run immediately
$ ruby test.rb
$ ls -la
d-wxrw--wt 3 casix staff 96 Jun 12 01:06 hoge1/
No, obviously the authority is not 755 ...
fileutils.rb
# File fileutils.rb, line 194
def mkdir_p(list, mode: nil, noop: nil, verbose: nil)
list = fu_list(list)
fu_output_message "mkdir -p #{mode ? ('-m %03o ' % mode) : ''}#{list.join ' '}" if verbose
return *list if noop
...
end
If you read carefully the processing of the arguments put in mode
mkdir -p #{mode ? ('-m %03o ' % mode) : ''}
a
#{mode ? ('-m %03o ' % mode) : ''}
Should be 755,
'-m %03o ' % mode
If you put 755 in mode
irb(main):001:0> mode = 755
=> 755
irb(main):002:0> '-m %03o ' % mode
=> "-m 1363 "
1363 is returned
That means ...
FileUtils.mkdir_p('./hoge1/hoge2/hoge3/ruby', mode: 755)
# ↓
# $ mkdir -p -m 1363 ./hoge1/hoge2/hoge3/ruby
That it was
'-m %03o ' % mode
# %Since 03o is output as a 0-packed 3-digit octal number
#Mode is a number converted from 755 to decimal.(493)Put in
irb(main):001:0> mode = 0o755
=> 493
irb(main):002:0> '-m %03o ' % mode
=> "-m 755 "
Looks good
Try to fix
FileUtils.mkdir_p('./hoge1/hoge2/hoge3/ruby', mode: 0o755)
# FileUtils.mkdir_p('./hoge1/hoge2/hoge3/ruby', mode: 0755)But k
$ ls -la
drwxr-xr-x 3 casix staff 96 Jun 12 01:43 hoge1/
It seems that it can be set with 755!
It wasn't limited to mkdir_p in the first place All the methods that can specify permission with FileUtils seem to have the same specifications
https://docs.ruby-lang.org/ja/latest/class/FileUtils.html#options
(By the way, it's easy to forget the knowledge that when it starts from 0, it becomes an octal number ...)
Recommended Posts