1. When Is A Drive Really Read Only?
- Posted by euphoric (admin) Oct 04, 2018
- 1162 views
I have a user who says my app is saying his access to a folder is blocked (read only). However, he can use Notepad to create files in that same folder.
Am I determining read/write status improperly? Here's my code:
public function write_to_path(sequence p) integer res = 0 dp("write access to '" & p & "'") object d = dir(p) if sequence(d) then -- dp(sprintf("ATTRIBUTES: %s", { d[1][D_ATTRIBUTES] })) res = find('d', d[1][D_ATTRIBUTES]) and not find('r',d[1][D_ATTRIBUTES]) end if if res then dp("...Allowed!",{}) else dp("...Denied!",{}) end if return res end function
dp() is just a debug printing function.
This is for Windows OS-based PCs.
Any help appreciated!
2. Re: When Is A Drive Really Read Only?
- Posted by SDPringle Oct 04, 2018
- 1110 views
On mine, it is telling me that '/' write is allowed and my /tmp directory (which is writable) is denied.
I made some thing to complete it a bit:
include std/filesys.e include std/console.e with trace trace(1) procedure dp(sequence s, sequence args={}) printf(1, s & 10, args) end procedure public function write_to_path(sequence p) integer res = 0 dp("write access to '" & p & "'") object d = dir(p) if sequence(d) then -- dp(sprintf("ATTRIBUTES: %s", { d[1][D_ATTRIBUTES] })) res = find('d', d[1][D_ATTRIBUTES]) and not find('r',d[1][D_ATTRIBUTES]) end if if res then dp("...Allowed!",{}) else dp("...Denied!",{}) end if return res end function sequence dir_name = prompt_string("Enter dir:") ? write_to_path(dir_name)
One gotcha is that when you test a directory, dir doesn't only give you the information for the passed directory, but all of the children of the directory!
3. Re: When Is A Drive Really Read Only?
- Posted by ghaberek (admin) Oct 04, 2018
- 1159 views
The "Read only" attribute of a file system object has nothing to do with the user's permission to that object. Where are you trying to write these files?
-Greg
4. Re: When Is A Drive Really Read Only?
- Posted by euphoric (admin) Oct 04, 2018
- 1103 views
The "Read only" attribute of a file system object has nothing to do with the user's permission to that object. Where are you trying to write these files?
Oh. I'm trying to write to a subfolder of "Documents" called "Custom Office Templates."
5. Re: When Is A Drive Really Read Only?
- Posted by SDPringle Oct 04, 2018
- 1129 views
Try this one:
include std/filesys.e include std/console.e procedure dp(sequence s, sequence args={}) printf(1, s & 10, args) end procedure public function write_to_path(sequence p) integer res = 0 dp("write access to '" & p & "'") object d = dir(p) if sequence(d) then if length(d) > 1 then -- it's a directory for i = 1 to length(d) do if equal(d[i][D_NAME],".") then -- forget about the directory's children d[i][D_NAME] = p d = {d[i]} exit end if end for end if dp(sprintf("%s ATTRIBUTES: %s", { d[1][D_NAME], d[1][D_ATTRIBUTES] })) res = find('r', d[1][D_ATTRIBUTES]) = 0 end if if res then dp("...Allowed!",{}) else dp("...Denied!",{}) end if return res end function sequence dir_name = prompt_string("Enter file or directory:") write_to_path(dir_name)
6. Re: When Is A Drive Really Read Only?
- Posted by ghaberek (admin) Oct 04, 2018
- 1132 views
Oh. I'm trying to write to a subfolder of "Documents" called "Custom Office Templates."
Strange. It could be related to the filesystem virtualization in Windows (circa Vista through 10) but the user's profile folders (Documents, Downloads, etc.) should always be writable by the user. So it's hard to say why without getting eyes on it.
This may be of some help to you. I can work it into Euphoria code tonight.
http://blog.aaronballman.com/2011/08/how-to-check-access-rights/
Although, determining if you can or cannot write a file may not be helpful if the answer is "no."
-Greg
7. Re: When Is A Drive Really Read Only?
- Posted by euphoric (admin) Oct 04, 2018
- 1106 views
Would it be helpful to simply attempt to create a file to the location, then check if it exists? Or maybe the writing of the file itself will produce an error?
if not create_file("the_new_file") then crash("Filesystem problem - could not create the new file") end if
8. Re: When Is A Drive Really Read Only?
- Posted by SDPringle Oct 05, 2018
- 1083 views
I've noticed that the dir() routine in Linux only sets the 'd' attribute where appropriate and it is so documented. You ought to write a unit test like:
include std/filesys.e include std/unittest.e -- I don't even use Windows right now. Does system.dll even exist anymore? sequence system_dll = dir("C:\\WINDOWS\\SYSTEM.DLL") test_true("system.dll is marked as read-only", find('r', system_dll[1][D_ATTRIBUTES])) -- more examples of using places of Windows we know exist but we shouldn't be able to write to, -- maybe using test_false, for things that should be read-write. test_report()
9. Re: When Is A Drive Really Read Only?
- Posted by ghaberek (admin) Oct 05, 2018
- 1090 views
Would it be helpful to simply attempt to create a file to the location, then check if it exists? Or maybe the writing of the file itself will produce an error?
From what I'm reading around the internet, yes. Actually testing is the only reliable method.
-Greg
10. Re: When Is A Drive Really Read Only?
- Posted by euphoric (admin) Oct 05, 2018
- 1103 views
Would it be helpful to simply attempt to create a file to the location, then check if it exists? Or maybe the writing of the file itself will produce an error?
From what I'm reading around the internet, yes. Actually testing is the only reliable method.
Here's what I'm doing for now:
public constant LOWERCASE_LETTERS = "abcdefghijklmnopqrstuvwxyz", UPPERCASE_LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ", LETTERS = LOWERCASE_LETTERS & UPPERCASE_LETTERS, NUMBERS = "0123456789" public enum USE_LOWER, USE_UPPER, USE_NUMBERS public function get_random_string(integer i, sequence options = {USE_LOWER,USE_UPPER,USE_NUMBERS}) sequence result, pool = {} integer count if length(options) = 0 then pool = LETTERS & NUMBERS else if find(USE_LOWER,options) then pool &= LOWERCASE_LETTERS end if if find(USE_UPPER,options) then pool &= UPPERCASE_LETTERS end if if find(USE_NUMBERS,options) then pool &= NUMBERS end if end if result = "" count = length(pool) for t=1 to i do result &= pool[rand(count)] end for return result end function public function write_to_path(sequence p) -- does user have permission to write a file to path 'p' integer res sequence fname = get_random_string(8) & ".txt" if p[$] != '\\' then p &= '\\' end if fname = p & fname res = create_file(fname) if res then delete_file(fname) end if return res end function
11. Re: When Is A Drive Really Read Only?
- Posted by ghaberek (admin) Oct 05, 2018
- 1111 views
That looks fine. But again, it's hard to say exactly why you're having this issue.
-Greg