myMPD
Internal API documentation
Loading...
Searching...
No Matches
filehandler.h File Reference

File handling. More...

#include "dist/sds/sds.h"
#include <stdbool.h>
#include <stdio.h>
Include dependency graph for filehandler.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Enumerations

enum  testdir_status { DIR_EXISTS = 0 , DIR_CREATED = 1 , DIR_CREATE_FAILED = 2 , DIR_NOT_EXISTS = 3 }
 
enum  try_rm_file_status { RM_FILE_OK = 0 , RM_FILE_ENOENT = 1 , RM_FILE_ERROR = 2 }
 

Functions

bool update_mtime (const char *filename)
 
bool do_chown (const char *file_path, const char *username)
 
time_t get_mtime (const char *filepath)
 
sds sds_getline (sds s, FILE *fp, size_t max, int *nread)
 
sds sds_getfile (sds s, const char *file_path, size_t max, bool remove_newline, bool warn, int *nread)
 
sds sds_getfile_from_fp (sds s, FILE *fp, size_t max, bool remove_newline, int *nread)
 
bool create_tmp_file (const char *filepath)
 
FILE * open_tmp_file (sds filepath)
 
bool rename_tmp_file (FILE *fp, sds tmp_file, bool write_rc)
 
bool write_data_to_file (const char *filepath, const char *data, size_t data_len)
 
bool rm_file (const char *filepath)
 
int try_rm_file (const char *filepath)
 
bool rename_file (const char *src, const char *dst)
 
bool testfile_read (const char *filename)
 
int testdir (const char *desc, const char *dir_name, bool create, bool silent)
 
bool is_dir (const char *dir_name)
 
bool clean_directory (const char *dir_name)
 
bool rm_directory (const char *dir_name)
 
bool clean_rm_directory (const char *dir_name)
 

Detailed Description

File handling.

Enumeration Type Documentation

◆ testdir_status

Status for a directory

◆ try_rm_file_status

Status of file remove function

Function Documentation

◆ clean_directory()

bool clean_directory ( const char *  dir_name)

Removes all regular files from a directory

Parameters
dir_namedirectory to cleanup
Returns
true on success, else false

◆ clean_rm_directory()

bool clean_rm_directory ( const char *  dir_name)

Shortcut for clean_directory and rm_directory

Parameters
dir_namedirectory to cleanup and remove
Returns
true on success, else false

◆ create_tmp_file()

bool create_tmp_file ( const char *  filepath)

Creates a file in specified path if it does not exists.

Parameters
filepathfilepath to create
Returns
bool true on success, else false

◆ do_chown()

bool do_chown ( const char *  file_path,
const char *  username 
)

Sets the owner of a file and group to the primary group of the user

Parameters
file_pathfile to change ownership
usernamenew owner username
Returns
true on success else false

◆ get_mtime()

time_t get_mtime ( const char *  filepath)

Returns the modification time of a file

Parameters
filepathfilepath
Returns
time_t modification time

◆ is_dir()

bool is_dir ( const char *  dir_name)

Checks if dir_name is really a directory entry

Parameters
dir_namedirectory path to check
Returns
true if it is a directory, else false

◆ open_tmp_file()

FILE * open_tmp_file ( sds  filepath)

Opens a temporary file for write using mkstemp

Parameters
filepathfilepath to open, e.g. /tmp/test.XXXXXX XXXXXX is replaced with a random string
Returns
FILE pointer

◆ rename_file()

bool rename_file ( const char *  src,
const char *  dst 
)

Renames a file. src and dst must be in the same filesystem.

Parameters
srcsource filename
dstdestination filename
Returns
true on success, else false

◆ rename_tmp_file()

bool rename_tmp_file ( FILE *  fp,
sds  tmp_file,
bool  write_rc 
)

Closes the tmp file and moves it to its destination name This is done by removing the last 7 characters from the tmp_file. See open_tmp_file for corresponding open function.

Parameters
fpFILE pointer
tmp_filetmp file to close and move
write_rcif false tmp file will be removed
Returns
true on success else false

◆ rm_directory()

bool rm_directory ( const char *  dir_name)

Removes a directory and reports errors

Parameters
dir_namedirectory to remove
Returns
true on success, else false

◆ rm_file()

bool rm_file ( const char *  filepath)

Removes a file and reports all errors

Parameters
filepathfilepath to remove
Returns
true on success else false

◆ sds_getfile()

sds sds_getfile ( sds  s,
const char *  file_path,
size_t  max,
bool  remove_newline,
bool  warn,
int *  nread 
)

Reads a whole file in the sds string s from *fp Removes whitespace characters from start and end

Parameters
san already allocated sds string that should hold the file content
file_pathfilename to read
maxmaximum bytes to read
remove_newlineremoves CR/LF if true
warnlog an error if file does not exist
nreadNumber of bytes read, -1 error reading file, -2 file is too big
Returns
pointer to s

◆ sds_getfile_from_fp()

sds sds_getfile_from_fp ( sds  s,
FILE *  fp,
size_t  max,
bool  remove_newline,
int *  nread 
)

Reads a whole file in the sds string s from *fp Removes whitespace characters from start and end

Parameters
san already allocated sds string that should hold the file content
fpFILE pointer to read
maxmaximum bytes to read
remove_newlineremoves CR/LF if true
nreadNumber of bytes read, -2 if file is too big
Returns
pointer to s

◆ sds_getline()

sds sds_getline ( sds  s,
FILE *  fp,
size_t  max,
int *  nread 
)

Getline function that trims whitespace characters

Parameters
san already allocated sds string
fpa file descriptor to read from
maxmax line length to read
nreadNumber of bytes read, -1 on EOF
Returns
Pointer to s

◆ testdir()

int testdir ( const char *  desc,
const char *  dir_name,
bool  create,
bool  silent 
)

Checks if dir exists

Parameters
descdescriptive name
dir_namedirectory path to check
createtrue creates the directory
silenttrue to report only errors
Returns
enum testdir_status

◆ testfile_read()

bool testfile_read ( const char *  filename)

Checks if a filename can be opened read-only

Parameters
filenamefilename to check
Returns
true on success, else false

◆ try_rm_file()

int try_rm_file ( const char *  filepath)

Removes a file and ignores none existing error

Parameters
filepathfilepath to remove
Returns
RM_FILE_ENOENT if file does not exist RM_FILE_ERROR error from unlink call RM_FILE_OK file was removed

◆ update_mtime()

bool update_mtime ( const char *  filename)

Updates the timestamp of a file

Parameters
filenamefile to update the timestamp
Returns
true on success, else false

◆ write_data_to_file()

bool write_data_to_file ( const char *  filepath,
const char *  data,
size_t  data_len 
)

Writes data to a file

Parameters
filepathfilepath to write to
datadata to write
data_lendata length to write
Returns
true on success else false