g_file_replace_contents (
  GFile* file,
  const char* contents,
  gsize length,
  const char* etag,
  gboolean make_backup,
  GFileCreateFlags flags,
  char** new_etag,
  GCancellable* cancellable,
  GError** error


Replaces the contents of file with contents of length bytes.

If etag is specified (not NULL), any existing file must have that etag, or the error G_IO_ERROR_WRONG_ETAG will be returned.

If make_backup is TRUE, this function will attempt to make a backup of file. Internally, it uses g_file_replace(), so will try to replace the file contents in the safest way possible. For example, atomic renames are used when replacing local files’ contents.

If cancellable is not NULL, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the error G_IO_ERROR_CANCELLED will be returned.

The returned new_etag can be used to verify that the file hasn’t changed the next time it is saved over.



Type: An array of guint8

A string containing the new contents for file.

The length of the array is specified in the length argument.
The data is owned by the caller of the function.

Type: gsize

The length of contents in bytes.


Type: const char*

The old entity-tag for the document, or NULL.

The argument can be NULL.
The data is owned by the caller of the function.
The value is a NUL terminated UTF-8 string.

Type: gboolean

TRUE if a backup should be created.


Type: GFileCreateFlags

A set of GFileCreateFlags.


Type: char**

A location to a new entity tag for the document. This should be freed with g_free() when no longer needed, or NULL.

The argument will be set by the function.
The argument can be set to NULL by the method.
The argument can be NULL.
The instance takes ownership of the data, and is responsible for freeing it.
The value is a NUL terminated UTF-8 string.

Type: GCancellable

Optional GCancellable object, NULL to ignore.

The argument can be NULL.
The data is owned by the caller of the function.

Type: GError **

The return location for a recoverable error.

The argument can be NULL.
If the return location is not NULL, then you must initialize it to a NULL GError*.
The argument will left initialized to NULL by the method if there are no errors.
In case of error, the argument will be set to a newly allocated GError; the caller will take ownership of the data, and be responsible for freeing it.

Return value

Type: gboolean

TRUE if successful. If an error has occurred, this function will return FALSE and set error appropriately if present.