http://coldfusion.com logo
#cfml-general
Title
# cfml-general
d

Daniel Mejia

02/04/2022, 11:25 PM
I'm probably misunderstanding how the getFileInfo()
canWrite
property works, or maybe this is a bug.
b

bdw429s

02/04/2022, 11:29 PM
The permissions check is likely different from the file being locked by another process
d

Daniel Mejia

02/04/2022, 11:30 PM
Docs say
canWrite: whether the file has write permission
.
b

bdw429s

02/04/2022, 11:30 PM
Do you have some java code that opens an input stream on that file and doesn't close it?
Right, and the error doesn't say "permissions denied", it says "the file is in use by another process"
d

Daniel Mejia

02/04/2022, 11:30 PM
Oh I'm actually opening the file myself to test the
canWrite
property. I know its open - but thats exactly what I want to find out.
I guess I have no other option but to catch the error
d

David Buck

02/05/2022, 9:26 PM
You mean you opened it using
fileOpen()
? You don't need to do that to use
getFileInfo()
. In fact, you should probably check the
canWrite
property before you open it (assuming you do intend to write to it). Unfortunately, if it didn't get closed for some reason (via
fileClose()
), then it may now be stuck in an open state, making it impossible to interact with until you reboot the server.
d

Daniel Mejia

02/07/2022, 4:10 PM
@David Buck no, I'm opening the files via file explorer, leaving the file open on purpose so I can figure out how to determine that it is open. canWrite is just a permissions check and I don't see a built-in way in cfml to check if the file is locked by another process SO I'm resorting to catching the error. At this point I wish cfml errors had specific error codes - instead I have to regex find kewords.
d

David Buck

02/07/2022, 5:53 PM
Aaah, ok now I see the problem.
a

Adam Cameron

02/07/2022, 7:49 PM
At this point I wish cfml errors had specific error codes
Or - wild wild idea here - actually had exception types. Now there's a crazy idea that no-one has ever thought of before. 😐
💯 1
😂 1
That said, I'd've thought the
canWrite
value would be agnostic to the underlying reason why a file might not be writable, be it perms or it being already locked open? If you can write to it, you can write to it. Isn't that what you care about? Or is it returning
true
even if the file is in use by another process, or something?
That said, I would perhaps approach your dilemma along these lines:
Copy code
try {
    // open it for writing
    // write to it
} catch () {
    // deal with it
} finally {
    try {
        // close it
    } catch () {
        // deal with _that_
    }
}
Job done?
d

David Buck

02/07/2022, 8:03 PM
@Adam Cameron I did some testing earlier, and it appears
getFileInfo()
does just check for write permission like Daniel said (actually, I'm not even sure it checks permissions-- it may just look at the readonly property).
a

Adam Cameron

02/07/2022, 8:04 PM
Yeah I was about to ask if you meant "perms" or just that attribute
Still. What I think you want is to write to the file right? Yer less fussed about the value some function call returns about whether it's writable or not?
So just... try to write to it. If you can? Sweet. If not... "I kinda expected that..." is fine.
1
I mean what were you gonna do if you were accurately detect that you couldn't write to it?
d

Daniel Mejia

02/08/2022, 3:08 AM
@Adam Cameron @David Buck canWrite returns true/false based on perms, and yes it doesn't matter that the file is being used by another process. So you are right that I want to be able to write to the file. So check this. After I started this post I went with catching the error. Which is what you suggested Adam. But earlier today I changed that to avoid all of it. I now create unique named files. So my users can run the process, open the generated files straight from the network share, and rerun the process again without worrrying about "open" files.
d

David Buck

02/08/2022, 3:54 PM
It's always nice when a roadblock forces a detour that turns out to be better than the original route.
3 Views