Functions
FUTIL: POSIX file handling

This is a wrapper for the POSIX file handling stuff. It mainly does some things for convenience and is not a real abstraction layer. More...

Functions

int fu_check_path (const char *path)
 Check path. More...
 
int fu_create_path (const char *path, posix_mode_t perm)
 Create path. More...
 
int fu_check_file (const char *pathname, struct_posix_stat *state)
 Check whether file exist. More...
 
int fu_open_file (const char *pathname, int *filedesc, int mode, posix_mode_t perm)
 Open file. More...
 
void fu_close_file (int *filedesc, FILE **stream)
 Close file (and potentially associated I/O stream) More...
 
int fu_lock_file (int filedesc)
 Lock file for writing. More...
 
int fu_unlink_file (const char *pathname)
 Unlink file. More...
 
int fu_assign_stream (int filedesc, FILE **stream, const char *mode)
 Assign I/O stream to open file. More...
 
int fu_sync (int filedesc, FILE *stream)
 Flush buffers of file. More...
 
int fu_read_whole_file (int filedesc, char **buffer, size_t *len)
 Read text file content and store it into memory buffer. More...
 
int fu_read_from_filedesc (int filedesc, char *buffer, size_t *len)
 Read data block to filedescriptor. More...
 
int fu_write_to_filedesc (int filedesc, const char *buffer, size_t len)
 Write data block to filedescriptor. More...
 
int fu_delete_tree (const char *dir)
 Delete directory tree. More...
 

Detailed Description

This is a wrapper for the POSIX file handling stuff. It mainly does some things for convenience and is not a real abstraction layer.

Note
It can't be used from C++ code because the used POSIX API doesn't has a C++ binding.

Function Documentation

◆ fu_assign_stream()

int fu_assign_stream ( int  filedesc,
FILE **  stream,
const char *  mode 
)

Assign I/O stream to open file.

Parameters
[in]filedescFiledescriptor to which the stream should be assigned
[out]streamFile stream that was assigned to file descriptor
[in]modeAccess mode of the new stream
Returns
  • Zero on success
  • Negative value on error

Definition at line 373 of file fileutils.c.

References PRINT_ERROR.

Referenced by core_save_to_file(), and log_open_logfile().

◆ fu_check_file()

int fu_check_file ( const char *  pathname,
struct_posix_stat *  state 
)

Check whether file exist.

Parameters
[in]pathnamePathname of file to open
[out]statePointer to status buffer

This is a wrapper for the POSIX stat() system call.

This function check for a file from pathname and try to get its status. If stat is NULL the status is thrown away.

On success, status is written to location pointed to by stat

Returns
  • Zero on success
  • Negative value on error

Definition at line 211 of file fileutils.c.

Referenced by core_check_file_exist(), core_get_signature(), ext_inews(), ext_pp_filter(), and secure_cl_secret().

◆ fu_check_path()

int fu_check_path ( const char *  path)

Check path.

Parameters
[in]pathString with path or pathname to verify

Accept only characters from the POSIX portable filename character set and '/' for security reasons. Especially accept no space and '' in the path.

Returns
  • Zero on success
  • Negative value on error

Definition at line 73 of file fileutils.c.

References PRINT_ERROR.

Referenced by core_get_homedir(), core_get_signature(), and xdg_get_confdir().

◆ fu_close_file()

void fu_close_file ( int *  filedesc,
FILE **  stream 
)

Close file (and potentially associated I/O stream)

Parameters
[in]filedescPointer to filedescriptor of file to close
[in]streamPointer to stream associated with filedesc

This function first close the stream pointed to by stream and then the filedescriptor pointed to by filedesc.

Note
Both, filedesc and stream may be NULL and are ignored in this case. If filedesc points to a location with value -1 it is ignored. If stream points to a location containing a NULL pointer, it is ignored.

Definition at line 290 of file fileutils.c.

Referenced by core_get_cancel_key(), core_get_signature(), core_save_to_file(), log_close_logfile(), log_open_logfile(), and secure_cl_secret().

◆ fu_create_path()

int fu_create_path ( const char *  path,
posix_mode_t  perm 
)

Create path.

Parameters
[in]pathString with path to create
[in]permPermissions for directories to create
Attention
path must not be NULL and must be an absolute path.

mkdir() is called for every component of the path. If path does not end with a slash, the last component is not treated as a directory to create.

Returns
  • Zero on success
  • Negative value on error

Definition at line 119 of file fileutils.c.

References PRINT_ERROR.

Referenced by core_get_cancel_key(), log_get_logpathname(), and xdg_get_confdir().

◆ fu_delete_tree()

int fu_delete_tree ( const char *  dir)

Delete directory tree.

Parameters
[in]dirPathname of toplevel directory (to delete with all content)

The parameter dir must point to a string with the absolute path. A trailing slash is allowed and ignored.

Attention
If the directory tree dir contains symbolic links, this function only works if compiled to use the X/Open XSI extension of the operating system.
Returns
  • Zero on success
  • Negative value on error

Definition at line 578 of file fileutils.c.

References PRINT_ERROR.

◆ fu_lock_file()

int fu_lock_file ( int  filedesc)

Lock file for writing.

Parameters
[in]filedescDescriptor of file to lock

The file must be open for writing for this function to work.

Returns
  • Zero on success
  • Negative value on error

Definition at line 328 of file fileutils.c.

References PRINT_ERROR.

◆ fu_open_file()

int fu_open_file ( const char *  pathname,
int *  filedesc,
int  mode,
posix_mode_t  perm 
)

Open file.

Parameters
[in]pathnamePathname of file to open
[out]filedescPointer to file descriptor
[in]modeMode for file to open
[in]permPermissions for file to open (if it must be created)

This is a wrapper for the POSIX open() system call that automatically retry if the system call was interrupted by a signal.

This function opens a file from pathname with the mode mode . If a new file is created, the permissions from perm are used, otherwise perm is ignored.

Returns
  • Zero on success
  • Negative value on error

Definition at line 243 of file fileutils.c.

References PRINT_ERROR.

Referenced by core_get_cancel_key(), core_get_signature(), core_save_to_file(), log_open_logfile(), and secure_cl_secret().

◆ fu_read_from_filedesc()

int fu_read_from_filedesc ( int  filedesc,
char *  buffer,
size_t *  len 
)

Read data block to filedescriptor.

Parameters
[in]filedescFiledescriptor of file
[out]bufferPointer to data buffer
[in,out]lenPointer to data length in octets

The filedescriptor filedesc must be valid.

Attention
The caller must set the value len points to to the size of the buffer. On success the value is overwritten with the number of octets read. A buffer size of zero is treated as error.
Returns
  • 0 on success (at least one octet was written to buffer)
  • Nonzero on error (or EOF before the first octet was read)

Definition at line 504 of file fileutils.c.

References PRINT_ERROR.

Referenced by core_get_cancel_key().

◆ fu_read_whole_file()

int fu_read_whole_file ( int  filedesc,
char **  buffer,
size_t *  len 
)

Read text file content and store it into memory buffer.

Parameters
[in]filedescFiledescriptor of file
[out]bufferPointer to data buffer
[out]lenPointer to buffer size

The filedescriptor filedesc must be valid. The data is terminated with a NUL character, therefore the file must be a text file without NUL characters in the content.

Attention
The value written to len is the buffer size, not the data length!

On success that caller is responsible to free the allocated buffer.

Returns
  • Zero on success
  • Negative value on error

Definition at line 445 of file fileutils.c.

Referenced by core_get_signature().

◆ fu_sync()

int fu_sync ( int  filedesc,
FILE *  stream 
)

Flush buffers of file.

Parameters
[in]filedescFiledescriptor of file
[out]streamFile stream assigned to file descriptor

The filedescriptor filedesc must be valid, stream may be NULL if there is no stream assigned to the filedescriptor.

Returns
  • Zero on success
  • Negative value on error

Definition at line 402 of file fileutils.c.

◆ fu_unlink_file()

int fu_unlink_file ( const char *  pathname)

Unlink file.

Parameters
[in]pathnamePathname of file to unlink
Returns
  • Zero on success
  • Negative value on error

Definition at line 355 of file fileutils.c.

Referenced by core_tmpfile_delete(), log_delete_logfile(), and secure_cl_secret().

◆ fu_write_to_filedesc()

int fu_write_to_filedesc ( int  filedesc,
const char *  buffer,
size_t  len 
)

Write data block to filedescriptor.

Parameters
[in]filedescFiledescriptor of file
[in]bufferPointer to data buffer
[in]lenData length in octets

The filedescriptor filedesc must be valid.

Returns
  • Zero on success
  • Negative value on error

Definition at line 542 of file fileutils.c.

References PRINT_ERROR.

Referenced by secure_cl_secret().


Generated at 2024-04-27 using  doxygen