Using %Stream.FileCharacter to create and append to files - Permissions Issue
We have a function that was written for us that allows us to create text files for logging certain aspects to the OS using %Stream.FileCharacter. We had no issues with this until we moved from AIX to Red Hat.
Now it seems the 1st time the function is called, and the file is created the permissions seem to be correct on the file. But as soon as we attempt to write another line to the file using MoveToEnd() it seems the permissions are changed on the file. I have been able to narrow the issue down to the MoveToEnd() by calling the function from different users.
When I initaly create the file via the function call I get the following permissions...
.png)
but as soon as a second call is made the permissions change to...
.png)
Here is the function code...
ClassMethod LogIt(pComponent As %String, pMsgIn As %String)
{
set vDIR="/archive/logs/"
set fs=##class(%Stream.FileCharacter).%New()
set fs.Filename=vDIR_pComponent_".log"
do fs.MoveToEnd()
set vTM=$PIECE($ZDATETIME($HOROLOG)," ",2)
//$ZTIME($PIECE($H,",",2),1)
do fs.WriteLine(vTM_" : "_pMsgIn)
do fs.%Save()
set fs = "" // Close file
}
I have tried adding code to see if the file exists, then try an Open to open the file handle like we do in Perl but the permissions still seem to change.
Why when MoveToEnd() and Save is called a second time the permissions change? I can change the functionality just to use %File but it is not as efficent.
Thanks
Comments
Are you sure this is not a umask issue?
https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/…
We have gone through all the permission settings at the directory and user level. We do not believe it is an unmask issue as the file is ok when it is first created, but the subsequent writes cause the permissions to change. If it was an unmask issue wouldn't the file have the wrong permissions in the first place? And I confirmed its not a user issue as its been duplicated with the owner of the directory and others within the same group.
Instead of MoveToOpen is there a way to use Open the file and go to the end of the file to insert the data? I tried looking at %File but could not figure out now to append data to the end of the file I just kept overwriting data.
instead of %File, I'd suggest to use %Stream.FileCharacter
it has a method MoveToEnd() that's what might solve your append issue
more
the Function is already using %Stream.FileCharacter and MoveToEnd(). After MoveToEnd() is called the second time we see the permissions on the file get updated.
??? looks like Move to End only works once after Open not sure after Rewind
I tried the open also. But I did find a work around by resetting the ##class(%File).SetAttributes which is basically doing a chmod but at the ObjectScript level when creating/appending to the file.
So what is %Stream really doing, is it just creating a new file based on what is in memory? Is it just reading the entire file into Memory so the IO is cut down and creating a new file from what is in Memory?
You could hard link the file as a way to detect the delete.
Would open the file append work?
https://docs.intersystems.com/iris20223/csp/documatic/%25CSP.Documatic…
There wasn't any clear cut explanations on how I could use that. Do you have some examples?
Set file=##class(%File).%New("[path/filename]")
Write file.Size
Do file.Open("AWS")
Do file.WriteLine("This is a second line of text")
Write file.Size
do file.%Save()
set file = ""// Close fileThis works for me