File.writable? false for DOCUMENTS folder
-
Ruby ALLWAYS reports "not writable" for the documents folder on my system, Windows 8.1.
Even if i am administrator. And even if i can write in the directory.
I suppose it is for security reasons (?)puts ENV["HOME"] #output ; # C;/Users/USERNAME File.writable? ENV["HOME"] #output ; # true File.exist? ENV["HOME"]+"/Documents" #output ; # true File.writable? ENV["HOME"]+"/Documents" #output ; # false ## THIS IS NOT LOGIC because i can write ; File.exist? ENV["HOME"]+"/Documents/test" #output ; # false Dir.mkdir ENV["HOME"]+"/Documents/test" #output ; # 0 File.exist? ENV["HOME"]+"/Documents/test" #output ; # true File.writable? ENV["HOME"]+"/Documents/test" #output ; # true
It seems that plugins must ignore this false information ?
Can someone confirm ?The properties of my documents folder are.
-
what does it return if you add an additional
/
puts File.writable? ENV["HOME"]+"/Documents/"
both variants return true on a mac...
-
As you have discovered...
File.writable?{file_or_folder_path)
only works consistently/effectively on MACs.
On PCs you can add a final '/
' and it might [sometimes] work... but I recommend that you really need to try to briefly write a temporary file to that folder in your own 'trapped' method, to get a consistent and 'correct' result... e.g.def self.writable?(directory_path=nil) return nil unless d=directory_path return nil unless File.directory?(d) begin r=rand.to_s r.next! while File.exist?(File.join(d, r)) #unique temp file trap t=File.join(d, r) f=File.new(t, 'w') f.puts("0") #it's made, can we modify it ? f.close if File.exist?(t) begin File.delete(t) w=true rescue #can write it BUT not change it !? w=false end else w=false end rescue #can't write it at all ! w=false end return w end
This test is used thus
self.writable?(some_directory_path)
, which returnstrue
orfalse
if a temporary file can be written to the folder, or it returnsnil
if no 'directory_path' was passed or the folder doesn't exist and/or it is not a directory !]... -
@pgarmyn said:
@driven said:
what does it return if you add an additional
/
puts File.writable? ENV["HOME"]+"/Documents/"
both variants return true on a mac...
Both false on my Win 8.1
My experience is that 'Documents' is the exception on Windows.
For other folders, the value is consistenly.Could this be related to the "shims" that recent versions of Windows use to fake some folders?
-
@driven said:
what does it return if you add an additional
/
puts File.writable? ENV["HOME"]+"/Documents/"
both variants return true on a mac...
Both false on my Win 8.1
My experience is that 'Documents' is the exception on Windows 8.1.
(I don't have non ASCII cars in the path)For other folders, the value is consistenly.
-
Ruby's file handling under Windows is frustratingly flaky. We have reported a number of issues to the Ruby developers and I'd encourage people to file whatever you find.
I usually wrap file handling in a begin, rescue block instead of testing if a while is writable - simply because Windows file handling is unpredictable.
Advertisement