name of a file to write contents to, in the GLib file name encoding
string to write to the file
flags controlling the safety vs speed of the operation
file mode, as passed to open(); typically this will be 0666
true on success, false if an error occurred
Writes all of
contentsto a file namedfilename, with good error checking. If a file calledfilenamealready exists it will be overwritten.flagscontrol the properties of the write operation: whether it’s atomic, and what the tradeoff is between returning quickly or being resilient to system crashes.As this function performs file I/O, it is recommended to not call it anywhere where blocking would cause problems, such as in the main loop of a graphical application. In particular, if
flagshas any value other than GLib.FileSetContentsFlags.NONE then this function may callfsync().If GLib.FileSetContentsFlags.CONSISTENT is set in
flags, the operation is atomic in the sense that it is first written to a temporary file which is then renamed to the final name.Notes:
On UNIX, if
filenamealready exists hard links tofilenamewill break. Also since the file is recreated, existing permissions, access control lists, metadata etc. may be lost. Iffilenameis a symbolic link, the link itself will be replaced, not the linked file.On UNIX, if
filenamealready exists and is non-empty, and if the system supports it (via a journalling filesystem or equivalent), and if GLib.FileSetContentsFlags.CONSISTENT is set inflags, thefsync()call (or equivalent) will be used to ensure atomic replacement:filenamewill contain either its old contents orcontents, even in the face of system power loss, the disk being unsafely removed, etc.On UNIX, if
filenamedoes not already exist or is empty, there is a possibility that system power loss etc. after calling this function will leavefilenameempty or full of NUL bytes, depending on the underlying filesystem, unless GLib.FileSetContentsFlags.DURABLE and GLib.FileSetContentsFlags.CONSISTENT are set inflags.On Windows renaming a file will not remove an existing file with the new name, so on Windows there is a race condition between the existing file being removed and the temporary file being renamed.
On Windows there is no way to remove a file that is open to some process, or mapped into memory. Thus, this function will fail if
filenamealready exists and is open.If the call was successful, it returns
true. If the call was not successful, it returnsfalseand setserror. The error domain isG_FILE_ERROR. Possible error codes are those in the GLib.FileError enumeration.Note that the name for the temporary file is constructed by appending up to 7 characters to
filename.If the file didn’t exist before and is created, it will be given the permissions from
mode. Otherwise, the permissions of the existing file will remain unchanged.