replace_contents


Description:

public bool replace_contents (uint8[] contents, string? etag, bool make_backup, FileCreateFlags flags, out string? new_etag, Cancellable? cancellable = null) throws Error

Replaces the contents of this with contents of contents.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 this. Internally, it uses 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.

Example: Replace file-content (uint8[]-based), sync:

public static int main (string[] args) {
if (args.length != 2) {
print ("%s FILE\n", args[0]);
return 0;
}

try {
File file = File.new_for_commandline_arg (args[1]);
file.replace_contents ("My info\n".data, null, false, FileCreateFlags.NONE, null);
} catch (Error e) {
print ("Error: %s\n", e.message);
}
return 0;
}

valac --pkg gio-2.0 GLib.File.replace_contents.vala

Example: Replace content:

public static int main (string[] args) {
GLib.File file = GLib.File.new_for_commandline_arg (args[1]);
try {
file.replace_contents (args[0].data, null, true, GLib.FileCreateFlags.NONE, null, null);
} catch ( GLib.Error e ) {
GLib.error (e.message);
}

return 0;
}

valac --pkg gio-2.0 GLib.replace_contents.vala

Parameters:

this

input File

contents

a string containing the new contents for this

etag

the old [entity-tag](entity-tags) for the document, or null

make_backup

true if a backup should be created

flags

a set of FileCreateFlags

new_etag

a location to a new [entity tag](entity-tags) for the document. This should be freed with g_free when no longer needed, or null

cancellable

optional Cancellable object, null to ignore

length

the length of contents in bytes

Returns:

true if successful. If an error has occurred, this function will return false and set throws appropriately if present.