[RUBY] The permission specification of the FileUtils method is an octal number.

When creating a directory with mkdir_p, I also want to specify the permission ...

About mkdir_p

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

Try to create by specifying permission

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 ...

I read the source code of mkdir_p

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

It was an octal number, not a decimal number, that should be included in the argument

'-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!

In FileUtils, the permission setting was octal, regardless of mkdir_p

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

The permission specification of the FileUtils method is an octal number.
The order of Java method modifiers is fixed
[Ruby] Count an even number in an array using the even? Method
What is the pluck method?
Is the method of the primitive specialized IntFunction apply or applyAsInt?
[Ruby] Questions and verification about the number of method arguments
What is the difference between an action and an instance method?
What is the initialize method?
Method to add the number of years and get the end of the month
Investigation method when the CPU of the server running java is heavy
'% 02d' What is the percentage of% 2?
About the role of the initialize method
What kind of method is define_method?
Introduction of heartbeat-logger that periodically logs the method / line number being executed
Get the type of an array element to determine if it is an array
What is testing? ・ About the importance of testing
Compare the elements of an array (Java)
How to determine the number of parallels
What is the main method in Java?
What is the data structure of ActionText?
Deepened my understanding of the merge method
About the number of threads of Completable Future
[Java] Check the number of occurrences of characters
[Swift] How to get the number of elements in an array (super basic)
Upcast (Java) that can reduce the amount of change when the specification is changed
[Java] Is it unnecessary to check "identity" in the implementation of the equals () method?
Whether the total product of the first n prime numbers plus 1 is a prime number