### Method

# Gio File replace_contents

#### Declaration [src]

    gboolean
    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
    )

#### Description [src]

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.

#### Parameters

`contents`

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

`length`

  Type: `gsize`

  The length of `contents` in bytes.

`etag`

  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 method.
  The value is a NUL terminated UTF-8 string.

`make_backup`

  Type: `gboolean`

  `TRUE` if a backup should be created.

`flags`

  Type: `GFileCreateFlags`

  A set of `GFileCreateFlags`.

`new_etag`

  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 caller of the method takes ownership of the returned data, and is responsible for freeing it.
  The value is a NUL terminated UTF-8 string.

`cancellable`

  Type: `GCancellable`

  Optional `GCancellable` object, `NULL` to ignore.

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

`error`

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