File management functions. More...
#include "config.h"
#include <dirent.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/types.h>
Go to the source code of this file.
Data Structures | |
struct | MuttFileIter |
State record for mutt_file_iter_line() More... | |
Macros | |
#define | MUTT_RL_NO_FLAGS 0 |
No flags are set. | |
#define | MUTT_RL_CONT (1 << 0) |
-continuation | |
#define | MUTT_RL_EOL (1 << 1) |
don't strip \n / \r\n | |
#define | mutt_file_fopen(PATH, MODE) mutt_file_fopen_full(PATH, MODE, 0600, __FILE__, __LINE__, __func__) |
#define | mutt_file_fclose(FP) mutt_file_fclose_full(FP, __FILE__, __LINE__, __func__) |
Typedefs | |
typedef uint8_t | ReadLineFlags |
Flags for mutt_file_read_line(), e.g. MUTT_RL_CONT. | |
typedef bool(* | mutt_file_map_t) (char *line, int line_num, void *user_data) |
Enumerations | |
enum | MuttStatType { MUTT_STAT_ATIME , MUTT_STAT_MTIME , MUTT_STAT_CTIME } |
Flags for mutt_file_get_stat_timespec. More... | |
enum | MuttOpenDirMode { MUTT_OPENDIR_NONE , MUTT_OPENDIR_CREATE } |
Mode flag for mutt_file_opendir() More... | |
Functions | |
int | mutt_file_check_empty (const char *path) |
Is the mailbox empty. | |
int | mutt_file_chmod_add (const char *path, mode_t mode) |
Add permissions to a file. | |
int | mutt_file_chmod_add_stat (const char *path, mode_t mode, struct stat *st) |
Add permissions to a file. | |
int | mutt_file_chmod_rm_stat (const char *path, mode_t mode, struct stat *st) |
Remove permissions from a file. | |
int | mutt_file_copy_bytes (FILE *fp_in, FILE *fp_out, size_t size) |
Copy some content from one file to another. | |
int | mutt_file_copy_stream (FILE *fp_in, FILE *fp_out) |
Copy the contents of one file into another. | |
time_t | mutt_file_decrease_mtime (const char *fp, struct stat *st) |
Decrease a file's modification time by 1 second. | |
void | mutt_file_expand_fmt (struct Buffer *dest, const char *fmt, const char *src) |
Replace s in a string with a filename. | |
void | mutt_file_expand_fmt_quote (char *dest, size_t destlen, const char *fmt, const char *src) |
int | mutt_file_fclose_full (FILE **fp, const char *file, int line, const char *func) |
Close a FILE handle (and NULL the pointer) | |
FILE * | mutt_file_fopen_full (const char *path, const char *mode, const mode_t perms, const char *file, int line, const char *func) |
Call fopen() safely. | |
FILE * | mutt_file_fopen_masked_full (const char *path, const char *mode, const char *file, int line, const char *func) |
Wrapper around mutt_file_fopen_full() | |
int | mutt_file_fsync_close (FILE **fp) |
Flush the data, before closing a file (and NULL the pointer) | |
long | mutt_file_get_size (const char *path) |
Get the size of a file. | |
long | mutt_file_get_size_fp (FILE *fp) |
Get the size of a file. | |
void | mutt_file_get_stat_timespec (struct timespec *dest, struct stat *st, enum MuttStatType type) |
Read the stat() time into a time value. | |
bool | mutt_file_iter_line (struct MuttFileIter *iter, FILE *fp, ReadLineFlags flags) |
Iterate over the lines from an open file pointer. | |
int | mutt_file_lock (int fd, bool excl, bool timeout) |
(Try to) Lock a file using fcntl() | |
bool | mutt_file_map_lines (mutt_file_map_t func, void *user_data, FILE *fp, ReadLineFlags flags) |
Process lines of text read from a file pointer. | |
int | mutt_file_mkdir (const char *path, mode_t mode) |
Recursively create directories. | |
int | mutt_file_open (const char *path, uint32_t flags, mode_t mode) |
Open a file. | |
DIR * | mutt_file_opendir (const char *path, enum MuttOpenDirMode mode) |
Open a directory. | |
char * | mutt_file_read_keyword (const char *file, char *buf, size_t buflen) |
Read a keyword from a file. | |
char * | mutt_file_read_line (char *line, size_t *size, FILE *fp, int *line_num, ReadLineFlags flags) |
Read a line from a file. | |
int | mutt_file_rename (const char *oldfile, const char *newfile) |
Rename a file. | |
int | mutt_file_rmtree (const char *path) |
Recursively remove a directory. | |
const char * | mutt_file_rotate (const char *path, int num) |
Rotate a set of numbered files. | |
int | mutt_file_safe_rename (const char *src, const char *target) |
NFS-safe renaming of files. | |
void | mutt_file_sanitize_filename (char *path, bool slash) |
Replace unsafe characters in a filename. | |
int | mutt_file_sanitize_regex (struct Buffer *dest, const char *src) |
Escape any regex-magic characters in a string. | |
size_t | mutt_file_save_str (FILE *fp, const char *str) |
Save a string to a file. | |
bool | mutt_file_seek (FILE *fp, LOFF_T offset, int whence) |
Wrapper for fseeko with error handling. | |
void | mutt_file_set_mtime (const char *from, const char *to) |
Set the modification time of one file from another. | |
int | mutt_file_stat_compare (struct stat *st1, enum MuttStatType st1_type, struct stat *st2, enum MuttStatType st2_type) |
Compare two stat infos. | |
int | mutt_file_stat_timespec_compare (struct stat *st, enum MuttStatType type, struct timespec *b) |
Compare stat info with a time value. | |
int | mutt_file_symlink (const char *oldpath, const char *newpath) |
Create a symlink. | |
int | mutt_file_timespec_compare (struct timespec *a, struct timespec *b) |
Compare to time values. | |
void | mutt_file_touch_atime (int fd) |
Set the access time to current time. | |
void | mutt_file_unlink (const char *s) |
Delete a file, carefully. | |
void | mutt_file_unlink_empty (const char *path) |
Delete a file if it's empty. | |
int | mutt_file_unlock (int fd) |
Unlock a file previously locked by mutt_file_lock() | |
void | mutt_file_resolve_symlink (struct Buffer *buf) |
Resolve a symlink in place. | |
void | buf_quote_filename (struct Buffer *buf, const char *filename, bool add_outer) |
Quote a filename to survive the shell's quoting rules. | |
void | buf_file_expand_fmt_quote (struct Buffer *dest, const char *fmt, const char *src) |
Replace s in a string with a filename. | |
Variables | |
const char | FilenameSafeChars [] |
Set of characters <=0x7F that are safe to use in filenames. | |
File management functions.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.
Definition in file file.h.
#define mutt_file_fopen | ( | PATH, | |
MODE | |||
) | mutt_file_fopen_full(PATH, MODE, 0600, __FILE__, __LINE__, __func__) |
#define mutt_file_fclose | ( | FP | ) | mutt_file_fclose_full(FP, __FILE__, __LINE__, __func__) |
typedef uint8_t ReadLineFlags |
Flags for mutt_file_read_line(), e.g. MUTT_RL_CONT.
typedef bool(* mutt_file_map_t) (char *line, int line_num, void *user_data) |
enum MuttStatType |
Flags for mutt_file_get_stat_timespec.
These represent filesystem timestamps returned by stat()
Enumerator | |
---|---|
MUTT_STAT_ATIME | File/dir's atime - last accessed time. |
MUTT_STAT_MTIME | File/dir's mtime - last modified time. |
MUTT_STAT_CTIME | File/dir's ctime - creation time. |
enum MuttOpenDirMode |
Mode flag for mutt_file_opendir()
Enumerator | |
---|---|
MUTT_OPENDIR_NONE | Plain opendir() |
MUTT_OPENDIR_CREATE | Create the directory if it doesn't exist. |
int mutt_file_check_empty | ( | const char * | path | ) |
int mutt_file_chmod_add | ( | const char * | path, |
mode_t | mode | ||
) |
Add permissions to a file.
path | Filename |
mode | the permissions to add |
num | Same as chmod(2) |
Adds the given permissions to the file. Permissions not mentioned in mode will stay as they are. This function resembles the chmod ugoa+rwxXst
command family. Example:
mutt_file_chmod_add(path, S_IWUSR | S_IWGRP | S_IWOTH);
will add write permissions to path but does not alter read and other permissions.
Definition at line 1116 of file file.c.
int mutt_file_chmod_add_stat | ( | const char * | path, |
mode_t | mode, | ||
struct stat * | st | ||
) |
Add permissions to a file.
path | Filename |
mode | the permissions to add |
st | struct stat for the file (optional) |
num | Same as chmod(2) |
Same as mutt_file_chmod_add() but saves a system call to stat() if a non-NULL stat structure is given. Useful if the stat structure of the file was retrieved before by the calling code. Example:
struct stat st; stat(path, &st); // ... do something else with st ... mutt_file_chmod_add_stat(path, S_IWUSR | S_IWGRP | S_IWOTH, st);
Definition at line 1139 of file file.c.
int mutt_file_chmod_rm_stat | ( | const char * | path, |
mode_t | mode, | ||
struct stat * | st | ||
) |
Remove permissions from a file.
path | Filename |
mode | the permissions to remove |
st | struct stat for the file (optional) |
num | Same as chmod(2) |
Same as mutt_file_chmod_rm() but saves a system call to stat() if a non-NULL stat structure is given. Useful if the stat structure of the file was retrieved before by the calling code. Example:
struct stat st; stat(path, &st); // ... do something else with st ... mutt_file_chmod_rm_stat(path, S_IWUSR | S_IWGRP | S_IWOTH, st);
Definition at line 1173 of file file.c.
int mutt_file_copy_bytes | ( | FILE * | fp_in, |
FILE * | fp_out, | ||
size_t | size | ||
) |
Copy some content from one file to another.
fp_in | Source file |
fp_out | Destination file |
size | Maximum number of bytes to copy |
0 | Success |
-1 | Error, see errno |
Definition at line 257 of file file.c.
int mutt_file_copy_stream | ( | FILE * | fp_in, |
FILE * | fp_out | ||
) |
Copy the contents of one file into another.
fp_in | Source file |
fp_out | Destination file |
num | Success, number of bytes copied |
-1 | Error, see errno |
Definition at line 287 of file file.c.
time_t mutt_file_decrease_mtime | ( | const char * | fp, |
struct stat * | st | ||
) |
Decrease a file's modification time by 1 second.
fp | Filename |
st | struct stat for the file (optional) |
num | Updated Unix mtime |
-1 | Error, see errno |
If a file's mtime is NOW, then set it to 1 second in the past.
Definition at line 1028 of file file.c.
void mutt_file_expand_fmt | ( | struct Buffer * | dest, |
const char * | fmt, | ||
const char * | src | ||
) |
Replace s
in a string with a filename.
dest | Buffer for the result |
fmt | printf-like format string |
src | Filename to substitute |
Definition at line 1471 of file file.c.
void mutt_file_expand_fmt_quote | ( | char * | dest, |
size_t | destlen, | ||
const char * | fmt, | ||
const char * | src | ||
) |
int mutt_file_fclose_full | ( | FILE ** | fp, |
const char * | file, | ||
int | line, | ||
const char * | func | ||
) |
Close a FILE handle (and NULL the pointer)
[out] | fp | FILE handle to close |
[in] | file | Source file |
[in] | line | Source line number |
[in] | func | Source function |
0 | Success |
EOF | Error, see errno |
Definition at line 165 of file file.c.
FILE * mutt_file_fopen_full | ( | const char * | path, |
const char * | mode, | ||
const mode_t | perms, | ||
const char * | file, | ||
int | line, | ||
const char * | func | ||
) |
Call fopen() safely.
path | Filename |
mode | Mode e.g. "r" readonly; "w" read-write |
perms | Permissions of the file (Relevant only when writing or appending) |
file | Source file |
line | Source line number |
func | Source function |
ptr | FILE handle |
NULL | Error, see errno |
When opening files for writing, make sure the file doesn't already exist to avoid race conditions.
Definition at line 666 of file file.c.
FILE * mutt_file_fopen_masked_full | ( | const char * | path, |
const char * | mode, | ||
const char * | file, | ||
int | line, | ||
const char * | func | ||
) |
Wrapper around mutt_file_fopen_full()
path | Filename |
mode | Mode e.g. "r" readonly; "w" read-write |
file | Source file |
line | Source line number |
func | Source function |
ptr | FILE handle |
NULL | Error, see errno |
Apply the user's umask, then call mutt_file_fopen_full().
Definition at line 230 of file neomutt.c.
int mutt_file_fsync_close | ( | FILE ** | fp | ) |
Flush the data, before closing a file (and NULL the pointer)
[out] | fp | FILE handle to close |
0 | Success |
EOF | Error, see errno |
Definition at line 193 of file file.c.
long mutt_file_get_size | ( | const char * | path | ) |
Get the size of a file.
path | File to measure |
num | Size in bytes |
0 | Error |
Definition at line 1519 of file file.c.
long mutt_file_get_size_fp | ( | FILE * | fp | ) |
Get the size of a file.
fp | FILE* to measure |
num | Size in bytes |
0 | Error |
Definition at line 1537 of file file.c.
void mutt_file_get_stat_timespec | ( | struct timespec * | dest, |
struct stat * | st, | ||
enum MuttStatType | type | ||
) |
Read the stat() time into a time value.
dest | Time value to populate |
st | stat info |
type | Type of stat info to read, e.g. MUTT_STAT_ATIME |
Definition at line 1579 of file file.c.
bool mutt_file_iter_line | ( | struct MuttFileIter * | iter, |
FILE * | fp, | ||
ReadLineFlags | flags | ||
) |
Iterate over the lines from an open file pointer.
iter | State of iteration including ptr to line |
fp | File pointer to read from |
flags | Same as mutt_file_read_line() |
true | Data read |
false | On eof |
This is a slightly cleaner interface for mutt_file_read_line() which avoids the eternal C loop initialization ugliness. Use like this:
Definition at line 889 of file file.c.
int mutt_file_lock | ( | int | fd, |
bool | excl, | ||
bool | timeout | ||
) |
(Try to) Lock a file using fcntl()
fd | File descriptor to file |
excl | If true, try to lock exclusively |
timeout | If true, Retry MAX_LOCK_ATTEMPTS times |
0 | Success |
-1 | Failure |
Use fcntl() to lock a file.
Use mutt_file_unlock() to unlock the file.
Definition at line 1202 of file file.c.
bool mutt_file_map_lines | ( | mutt_file_map_t | func, |
void * | user_data, | ||
FILE * | fp, | ||
ReadLineFlags | flags | ||
) |
Process lines of text read from a file pointer.
func | Callback function to call for each line, see mutt_file_map_t |
user_data | Arbitrary data passed to "func" |
fp | File pointer to read from |
flags | Same as mutt_file_read_line() |
true | All data mapped |
false | "func" returns false |
Definition at line 910 of file file.c.
int mutt_file_mkdir | ( | const char * | path, |
mode_t | mode | ||
) |
Recursively create directories.
path | Directories to create |
mode | Permissions for final directory |
0 | Success |
-1 | Error (errno set) |
Create a directory, creating the parents if necessary. (like mkdir -p)
Definition at line 974 of file file.c.
int mutt_file_open | ( | const char * | path, |
uint32_t | flags, | ||
mode_t | mode | ||
) |
Open a file.
path | Pathname to open |
flags | Flags, e.g. O_EXCL |
mode | Permissions of the file (Relevant only when writing or appending) |
>0 | Success, file handle |
-1 | Error |
Definition at line 577 of file file.c.
DIR * mutt_file_opendir | ( | const char * | path, |
enum MuttOpenDirMode | mode | ||
) |
Open a directory.
path | Directory path |
mode | See MuttOpenDirMode |
ptr | DIR handle |
NULL | Error, see errno |
Definition at line 642 of file file.c.
char * mutt_file_read_keyword | ( | const char * | file, |
char * | buf, | ||
size_t | buflen | ||
) |
Read a keyword from a file.
file | File to read |
buf | Buffer to store the keyword |
buflen | Length of the buf |
ptr | Start of the keyword |
Read one line from the start of a file. Skip any leading whitespace and extract the first token.
Definition at line 1406 of file file.c.
char * mutt_file_read_line | ( | char * | line, |
size_t * | size, | ||
FILE * | fp, | ||
int * | line_num, | ||
ReadLineFlags | flags | ||
) |
Read a line from a file.
[out] | line | Buffer allocated on the head (optional) |
[in] | size | Length of buffer |
[in] | fp | File to read |
[out] | line_num | Current line number (optional) |
[in] | flags | Flags, e.g. MUTT_RL_CONT |
ptr | The allocated string |
Read a line from "fp" into the dynamically allocated "line", increasing "line" if necessary. The ending "\n" or "\r\n" is removed. If a line ends with "\", this char and the linefeed is removed, and the next line is read too.
Definition at line 808 of file file.c.
int mutt_file_rename | ( | const char * | oldfile, |
const char * | newfile | ||
) |
Rename a file.
oldfile | Old filename |
newfile | New filename |
0 | Success |
1 | Old file doesn't exist |
2 | New file already exists |
3 | Some other error |
Definition at line 1371 of file file.c.
int mutt_file_rmtree | ( | const char * | path | ) |
Recursively remove a directory.
path | Directory to delete |
0 | Success |
-1 | Error, see errno |
Definition at line 486 of file file.c.
const char * mutt_file_rotate | ( | const char * | path, |
int | count | ||
) |
Rotate a set of numbered files.
path | Template filename |
count | Maximum number of files |
ptr | Name of the 0'th file |
Given a template 'temp', rename files numbered 0 to (count-1).
Rename:
Definition at line 546 of file file.c.
int mutt_file_safe_rename | ( | const char * | src, |
const char * | target | ||
) |
NFS-safe renaming of files.
src | Original filename |
target | New filename |
0 | Success |
-1 | Error, see errno |
Warning: We don't check whether src and target are equal.
Definition at line 371 of file file.c.
void mutt_file_sanitize_filename | ( | char * | path, |
bool | slash | ||
) |
Replace unsafe characters in a filename.
path | Filename to make safe |
slash | Replace '/' characters too |
Definition at line 712 of file file.c.
int mutt_file_sanitize_regex | ( | struct Buffer * | dest, |
const char * | src | ||
) |
Escape any regex-magic characters in a string.
dest | Buffer for result |
src | String to transform |
0 | Success |
-1 | Error |
Definition at line 754 of file file.c.
size_t mutt_file_save_str | ( | FILE * | fp, |
const char * | str | ||
) |
Save a string to a file.
fp | Open file to save to |
str | String to save |
num | Bytes written to file |
Definition at line 1679 of file file.c.
bool mutt_file_seek | ( | FILE * | fp, |
LOFF_T | offset, | ||
int | whence | ||
) |
Wrapper for fseeko with error handling.
[in] | fp | File to seek |
[in] | offset | Offset |
[in] | whence | Seek mode |
true | Seek was successful |
false | Seek failed |
Definition at line 778 of file file.c.
void mutt_file_set_mtime | ( | const char * | from, |
const char * | to | ||
) |
Set the modification time of one file from another.
from | Filename whose mtime should be copied |
to | Filename to update |
Definition at line 1068 of file file.c.
int mutt_file_stat_compare | ( | struct stat * | st1, |
enum MuttStatType | st1_type, | ||
struct stat * | st2, | ||
enum MuttStatType | st2_type | ||
) |
Compare two stat infos.
st1 | First stat info |
st1_type | Type of first stat info, e.g. MUTT_STAT_ATIME |
st2 | Second stat info |
st2_type | Type of second stat info, e.g. MUTT_STAT_ATIME |
-1 | a precedes b |
0 | a and b are identical |
1 | b precedes a |
Definition at line 1641 of file file.c.
int mutt_file_stat_timespec_compare | ( | struct stat * | st, |
enum MuttStatType | type, | ||
struct timespec * | b | ||
) |
Compare stat info with a time value.
st | stat info |
type | Type of stat info, e.g. MUTT_STAT_ATIME |
b | Time value |
-1 | a precedes b |
0 | a and b are identical |
1 | b precedes a |
Definition at line 1619 of file file.c.
int mutt_file_symlink | ( | const char * | oldpath, |
const char * | newpath | ||
) |
Create a symlink.
oldpath | Existing pathname |
newpath | New pathname |
0 | Success |
-1 | Error, see errno |
Definition at line 315 of file file.c.
int mutt_file_timespec_compare | ( | struct timespec * | a, |
struct timespec * | b | ||
) |
Compare to time values.
a | First time value |
b | Second time value |
-1 | a precedes b |
0 | a and b are identical |
1 | b precedes a |
Definition at line 1557 of file file.c.
void mutt_file_touch_atime | ( | int | fd | ) |
Set the access time to current time.
fd | File descriptor of the file to alter |
This is just as read() would do on !noatime. Silently ignored if futimens() isn't supported.
Definition at line 1091 of file file.c.
void mutt_file_unlink | ( | const char * | s | ) |
Delete a file, carefully.
s | Filename |
This won't follow symlinks.
Definition at line 221 of file file.c.
void mutt_file_unlink_empty | ( | const char * | path | ) |
Delete a file if it's empty.
path | File to delete |
Definition at line 1335 of file file.c.
int mutt_file_unlock | ( | int | fd | ) |
Unlock a file previously locked by mutt_file_lock()
fd | File descriptor to file |
0 | Always |
Definition at line 1249 of file file.c.
void mutt_file_resolve_symlink | ( | struct Buffer * | buf | ) |
Resolve a symlink in place.
buf | Input/output path |
Definition at line 1659 of file file.c.
void buf_quote_filename | ( | struct Buffer * | buf, |
const char * | filename, | ||
bool | add_outer | ||
) |
Quote a filename to survive the shell's quoting rules.
buf | Buffer for the result |
filename | String to convert |
add_outer | If true, add 'single quotes' around the result |
Definition at line 933 of file file.c.
void buf_file_expand_fmt_quote | ( | struct Buffer * | dest, |
const char * | fmt, | ||
const char * | src | ||
) |
Replace s
in a string with a filename.
dest | Buffer for the result |
fmt | printf-like format string |
src | Filename to substitute |
This function also quotes the file to prevent shell problems.
Definition at line 1456 of file file.c.