Written by

Enterprise Application Development Consultant at The Ohio State University Wexner Medical Center
MOD
Question Scott Roth · Apr 6, 2023

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...

but as soon as a second call is made the permissions change to...

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

Product version: IRIS 2022.1
$ZV: IRIS for UNIX (Red Hat Enterprise Linux 8 for x86-64) 2022.1 (Build 209U) Tue May 31 2022 12:13:24 EDT

Comments

Scott Roth  Apr 6, 2023 to Alexander Pettitt

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.

0
Robert Cemper  Apr 6, 2023 to Scott Roth

instead of %File, I'd suggest to use %Stream.FileCharacter
it has a method MoveToEnd()  that's what might solve your append issue
more

0
Scott Roth  Apr 6, 2023 to Robert Cemper

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.

0
Robert Cemper  Apr 6, 2023 to Scott Roth

???  looks like Move to End only works once after Open   not sure after Rewind

0
Scott Roth  Apr 6, 2023 to Robert Cemper

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?

0
Scott Roth  Apr 6, 2023 to Alexander Pettitt

There wasn't any clear cut explanations on how I could use that. Do you have some examples?

0
Alexander Pettitt  Apr 6, 2023 to Scott Roth
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 file

This works for me

0