Took me 1 hours to figure out Dir.mkdir’s permission handling is broken ?
micha@ubuntu:/home/data/test$ ruby -e "Dir.mkdir 'wtf',0777" micha@ubuntu:/home/data/test$ ls -al drwxr-xr-x 2 micha micha 4096 2008-02-24 13:07 wtf micha@ubuntu:/home/data/test$ ruby -e "File.chmod 0777,'wtf'" micha@ubuntu:/home/data/test$ ls -al drwxrwxrwx 2 micha micha 4096 2008-02-24 13:07 wtf
Dir: 0777 = drwxr-xr-x
File: 0777 = drwxrwxrwx
WTF?
And to not make it a short post, i add some recursive directory creating 😀
#simple recursive dir creation FileUtils.mkdir_p dir #create a directory recursive and set rights/group def self.mkdir_r path, permission=0770,group_id=false path.split('/').inject(path =~ /^\// ? '/':'') do |root,part| path = root+'/'+part unless File.exist? path Dir.mkdir(path, permission) #hack, mkdir does not set the permisson right !? File.chmod permission,path File.chown(-1,group_id,path) if group_id end path end end
This is because the umask is used, as per the documentation: https://ruby-doc.org/core-2.2.3/Dir.html#method-c-mkdir