Internal API Reference - File utilities
Desktop Apps Training - Policy Kit

File utilities

File utilities — File utilities

 

Synopsis



kit_bool_t kit_file_get_contents (const char *path,
char **out_contents,
size_t *out_contents_size);
kit_bool_t kit_file_set_contents (const char *path,
mode_t mode,
const char *contents,
size_t contents_size);

Description

Various file utilities.

 

Details

kit_file_get_contents ()

kit_bool_t          kit_file_get_contents               (const char *path,
char **out_contents,
size_t *out_contents_size);

Reads an entire file into allocated memory.

 

path :

path to file

out_contents :

Return location for allocated memory. Free with kit_free().

out_contents_size :

Return location for size of the file.

Returns :

TRUE if the file was read into memory; FALSE if an error occured and errno will be set. On OOM, errno will be set to ENOMEM. If the file doesn't exist, errno will be set to ENOENT.

kit_file_set_contents ()

kit_bool_t          kit_file_set_contents               (const char *path,
mode_t mode,
const char *contents,
size_t contents_size);

Writes all of contents to a file named path, with good error checking. If a file called path already exists it will be overwritten. This write is atomic in the sense that it is first written to a temporary file which is then renamed to the final name.

If the file already exists hard links to path will break. Also since the file is recreated, existing permissions, access control lists, metadata etc. may be lost. If path is a symbolic link, the link itself will be replaced, not the linked file.

 

path :

path to file

mode :

mode for file

contents :

contents to set

contents_size :

size of contents

Returns :

TRUE if contents were set; FALSE if an error occured and errno will be set