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.


contents 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.
length gsize

The length of contents in bytes.

etag const char*

The old [entity-tag][gfile-etag] 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.
make_backup gboolean

TRUE if a backup should be created.

flags GFileCreateFlags

A set of GFileCreateFlags.

new_etag char**

A location to a new [entity tag][gfile-etag] 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.
 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.
cancellable GCancellable

Optional GCancellable object, NULL to ignore.

 The argument can be NULL.
 The data is owned by the caller of the function.
error GError **
  The return location for a GError*, or NULL.

Return value

Returns: gboolean

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