CAPS Universe documentation  1.0.4
All you need to know to be successful
Macros | Functions
libcapshelper.c File Reference

Some helper functions common for various libraries and programs. More...

Macros

#define COPY_BUFFER_SIZE   4096
 

Functions

static int caps_helper_string_validate (const char *string, size_t max_len)
 
static int caps_helper_file_name_validate (const char *file_name)
 
static char * _caps_helper_create_full_path (const char *default_path, const char *environment_var, const char *file_name)
 
static int _caps_helper_return_directory_fd (const char *default_path, const char *environment_var)
 
char * caps_helper_driver_db_path_create (const char *file_name)
 
int caps_helper_driver_db_dir_fd (void)
 
char * caps_helper_driver_config_path_create (const char *file_name)
 
int caps_helper_driver_config_dir_fd (void)
 
char * caps_helper_driver_path_create (const char *file_name)
 
int caps_helper_driver_dir_fd (void)
 
char * caps_helper_device_db_path_create (const char *file_name)
 
int caps_helper_device_db_dir_fd (void)
 
char * caps_helper_config_path_create (const char *file_name)
 
int caps_helper_config_dir_fd (void)
 
int caps_helper_number_convert (const char *string)
 
int caps_helper_boolean_check (const char *value, int set)
 
static void create_invisible_temp_file_standard (int *file)
 
int caps_helper_invisible_file_create (int *file)
 
int caps_helper_file_content_copy (int fd_to, int fd_from)
 
int caps_helper_verbosity_setup (const char *keyword, bool overwrite)
 

Detailed Description

Author
Jürgen Borleis
Warning
Use as experimental
Note
The global API you can find here: libcapsbase: API description

Macro Definition Documentation

◆ COPY_BUFFER_SIZE

#define COPY_BUFFER_SIZE   4096

Byte count copied at once, refer caps_helper_file_content_copy

Function Documentation

◆ caps_helper_string_validate()

static int caps_helper_string_validate ( const char *  string,
size_t  max_len 
)
static

Ensure a string is a valid C-String (duck test)

Parameters
[in]stringString to check
[in]max_lenAccepted max length of this string
Returns
0 if the string is valid (e.g. has a terminating '\0' in the given range)
Note
Only done in debug mode
Attention
If no '\0' is found within the range of max_len, this function might touch invalid memory!

◆ caps_helper_file_name_validate()

static int caps_helper_file_name_validate ( const char *  file_name)
static

Be paranoid and check for a sane filename (no path)

Parameters
[in]file_nameThe file name to check
Returns
0 if file_name contains a valid file name
Note
Only done in debug mode
Todo:

Could be a global function for other modules to validate filenames at their "border"

The max length of a filename depends on the underlying filesystem

◆ _caps_helper_create_full_path()

static char * _caps_helper_create_full_path ( const char *  default_path,
const char *  environment_var,
const char *  file_name 
)
static

Create a full qualified path to a directory or a file

Parameters
[in]default_pathThe compiled-in directory path
[in]environment_varThe environment variable to provide the directory path (in case of debugging is enabled)
[in]file_nameThe file name in the directory (can be NULL)
Returns
Pointer to a full qualified path to a directory or a file
Precondition
file_name must be NULL or a valid single name (no path!)

In the case file_name is NULL, it returns default_path or (if debugging is enabled) the content of the environment variable named in environment_var in a newly allocated buffer. If file_name isn't NULL, the returned buffer additionally contains the file name as its trailing component.

Note
If debugging is enabled the environment variable (if set) has always precedence over the default_path.
Postcondition
The allocated buffer must be freed after use.

◆ _caps_helper_return_directory_fd()

static int _caps_helper_return_directory_fd ( const char *  default_path,
const char *  environment_var 
)
static

Return a file descriptor for a directory

Parameters
[in]default_pathThe compiled-in directory path
[in]environment_varThe environment variable to provide the directory path (in case of debugging is enabled)
Returns
The directory's file descriptor or negative errno in case of failure

The file descriptor is intended for use with the various *at() functions (openat() for example).

Returns always the file descriptor of directory default_path or (if debugging is enabled) of the directory defined by the environment variable named in environment_var (if defined at run-time).

Note
If debugging is enabled the environment variable (if set) has always precedence over the default_path.
The file descriptor must be closed after use.
Todo:
The file descriptor is opened for read only, which might be not helpful.

◆ create_invisible_temp_file_standard()

static void create_invisible_temp_file_standard ( int *  file)
static

Create an invisible and self-removing temp. file in a standard way

Parameters
[out]fileWhere to store the filedescriptor
Note
This way should work on every platform (unlink a still open file).

This is the standard way to create a (mostly) invisible and self-removing file when there are other ways missing. If someone monitors the directory it is still possible to catch the file.

Todo:
If the unlink (e.g. hiding) fails: should it be an error?