Some helper functions common for various libraries and programs.
More...
- Copyright
- GNU Lesser General Public License 2.1 or later
- Author
- Jürgen Borleis
- Warning
- Use as experimental
- Note
- The global API you can find here: libcapsbase: API description
◆ COPY_BUFFER_SIZE
#define COPY_BUFFER_SIZE 4096 |
◆ 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] | string | String to check |
[in] | max_len | Accepted 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_name | The 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_path | The compiled-in directory path |
[in] | environment_var | The environment variable to provide the directory path (in case of debugging is enabled) |
[in] | file_name | The 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_path | The compiled-in directory path |
[in] | environment_var | The 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] | file | Where 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?