Beware of Dir.mkdir Permission Handling

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

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

One thought on “Beware of Dir.mkdir Permission Handling

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s