Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby FileUtils mkdir_p mode - unexpected result

Tags:

ruby

I am trying to use the :mode option in FileUtils.mkdir_p. However, I am getting unexpected results using Ruby 2.1.0.

require 'fileutils'
FileUtils.mkdir_p '/this/is/my/full/path/tmp', :mode => 2750

Result:

d-wSrwxrwT  2 myuid users   4096 Mar 24 10:14 tmp

However, if I just call the shell commands with backticks I get the desired result:

`mkdir /this/is/my/full/path/tmp && chmod 2750 /this/is/my/full/path/tmp`

Result:

drwxr-s---  2 myuid users   4096 Mar 24 10:16 tmp

How can I create the directory with the desired permissions without using shell commands?

like image 819
Greg Ruhl Avatar asked Mar 24 '15 17:03

Greg Ruhl


1 Answers

Ruby is interpreting the permissions as an integer rather than an octal number. The chmod command (and the options passed to mkdir_p) takes an octal (or the equivalent as an integer). If you prepend 0 to the number, Ruby will use it as an octal.

FileUtils.mkdir_p '/this/is/my/full/path/tmp', :mode => 02750

Or, you could use an integer (ruby -e 'puts 02750.to_i' displays 1512).

FileUtils.mkdir_p '/this/is/my/full/path/tmp', :mode => 1512

like image 174
xrd Avatar answered Oct 12 '22 12:10

xrd