Julian Matthews · Mar 17, 2021

Saving a file character stream to a UNC path not reporting a failure to save the file

Hey everyone.

I have a process where I create a new %Stream.FileCharacter object, specify the filename (including it's path), write data to the stream, and then save. However for some reason, if the user account does not have write access to the directory, the %Save method is still returning true even though it was unable to actually write the file to the folder.

The location is a UNC path, and I'm wondering if this is tripping me up?

0 188
Discussion (3)1
Log in or sign up to continue

Checking status codes is a good starting point...

set str=##class(%Stream.FileCharacter).%New()
write str  --> 4@%Stream.FileCharacter
write $system.OBJ.DisplayError(str.LinkToFile("/root/my_file.txt")) --> 1
write $system.OBJ.DisplayError(str.WriteLine("line-1"))  --> ERROR #5005: Cannot open file '/root/my_file.txt'1

Your %Save() returns with 1 (OK), because there is nothing to save...

Note: on linux,  for a standard user (like me now) is forbidden to write to '/root' directory

Ahh that explains where I went wrong, thanks!

In my mind, the write action was writing the data to the object while it was still server-side, and then it was the save that actually commits it to the destination, which is why I was only looking at the status of the save method.

Of course, if you don't want to check each and every write() for error or success, you can do the check just one time at the beginning

set str=##class(%Stream.FileCharacter).%New()
do str.LinkToFile("/root/my_file.txt")
set sts=str.Write("")
if 'sts { write "We have a problem",! quit }

writing a nullstring to stream does not change the stream but the file opening sequence will be executed