Gio.File.replace
Description: Reference for Gio.File.replace

### Virtual Method

# Gio Filereplace

#### Declaration

    GFileOutputStream*
    replace (
      GFile* file,
      const char* etag,
      gboolean make_backup,
      GFileCreateFlags flags,
      GCancellable* cancellable,
      GError** error
    )

#### Description

Returns an output stream for overwriting the file, possibly creating a backup copy of the file first. If the file doesn’t exist, it will be created.

This will try to replace the file in the safest way possible so that any errors during the writing will not affect an already existing copy of the file. For instance, for local files it may write to a temporary file and then atomically rename over the destination when the stream is closed.

By default files created are generally readable by everyone, but if you pass `G_FILE_CREATE_PRIVATE` in `flags` the file will be made readable only to the current user, to the level that is supported on the target filesystem.

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.

If you pass in a non-`NULL` `etag` value and `file` already exists, then this value is compared to the current entity tag of the file, and if they differ an `G_IO_ERROR_WRONG_ETAG` error is returned. This generally means that the file has been changed since you last read it. You can get the new etag from `g_file_output_stream_get_etag()` after you’ve finished writing and closed the `GFileOutputStream`. When you load a new file you can use `g_file_input_stream_query_info()` to get the etag of the file.

If `make_backup` is `TRUE`, this function will attempt to make a backup of the current file before overwriting it. If this fails a `G_IO_ERROR_CANT_CREATE_BACKUP` error will be returned. If you want to replace anyway, try again with `make_backup` set to `FALSE`.

If the file is a directory the `G_IO_ERROR_IS_DIRECTORY` error will be returned, and if the file is some other form of non-regular file then a `G_IO_ERROR_NOT_REGULAR_FILE` error will be returned. Some file systems don’t allow all file names, and may return an `G_IO_ERROR_INVALID_FILENAME` error, and if the name is to long `G_IO_ERROR_FILENAME_TOO_LONG` will be returned. Other errors are possible too, and depend on what kind of filesystem the file is on.

#### Parameters

`etag`

  Type: `const char*`

  An optional entity tag for the current `GFile`, or `NULL` to ignore.

  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`

  Type: `gboolean`

  `TRUE` if a backup should be created.

`flags`

  Type: `GFileCreateFlags`

  A set of `GFileCreateFlags`.

`cancellable`

  Type: `GCancellable`

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

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

`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 virtual function 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: `GFileOutputStream`

A `GFileOutputStream` or `NULL` on error. Free the returned object with g_object_unref().

The caller of the function takes ownership of the data, and is responsible for freeing it.