Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect whether a directory is writeable in Ruby

I am using Ruby and need to detect whether a directory is writeable before trying to create new files.

I have tried the following code, which correctly returns true/false depending on whether @path is a directory. However, it still returns true when there is no write permission to the @path folder.

  if File.directory?(@path) && File.writable?(@path)
    #is a writeable directory
    true
  else
    #is not a writeable directory
    false
  end

I have looked at the help for the File and Dir classes and cannot see any method that allows me to check directory write permissions. Is there a way?

I only need it work on Windows, using Ruby 1.9.3.

like image 517
SimonMayer Avatar asked Jan 26 '12 10:01

SimonMayer


1 Answers

For Windows you want the File.attributes or File.readonly? methods from the win32-file gem.

And you should consider contributing to Daniel Berger, as without his win32- gems Ruby on Windows would be a far more hostile place.

like image 63
Phrogz Avatar answered Sep 21 '22 08:09

Phrogz