File management functions. More...
#include "config.h"#include <errno.h>#include <fcntl.h>#include <limits.h>#include <stdbool.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/stat.h>#include <time.h>#include <unistd.h>#include <utime.h>#include <wchar.h>#include "file.h"#include "buffer.h"#include "charset.h"#include "ctype2.h"#include "date.h"#include "logging2.h"#include "memory.h"#include "message.h"#include "path.h"#include "pool.h"#include "string2.h"
Include dependency graph for file.c:Go to the source code of this file.
Macros | |
| #define | MAX_LOCK_ATTEMPTS 5 |
| #define | O_NOFOLLOW 0 |
Functions | |
| static bool | stat_equal (struct stat *st_old, struct stat *st_new) |
| Compare the struct stat's of two files/dirs. | |
| int | mutt_file_fclose_full (FILE **fp, const char *file, int line, const char *func) |
| Close a FILE handle (and NULL the pointer) | |
| int | mutt_file_fsync_close (FILE **fp) |
| Flush the data, before closing a file (and NULL the pointer) | |
| void | mutt_file_unlink (const char *s) |
| Delete a file, carefully. | |
| 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. | |
| int | mutt_file_symlink (const char *oldpath, const char *newpath) |
| Create a symlink. | |
| int | mutt_file_safe_rename (const char *src, const char *target) |
| NFS-safe renaming of files. | |
| int | mutt_file_rmtree (const char *path) |
| Recursively remove a directory. | |
| const char * | mutt_file_rotate (const char *path, int count) |
| Rotate a set of numbered files. | |
| 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. | |
| 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. | |
| 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. | |
| bool | mutt_file_seek (FILE *fp, LOFF_T offset, int whence) |
| Wrapper for fseeko with error handling. | |
| char * | mutt_file_read_line (char *line, size_t *size, FILE *fp, int *line_num, ReadLineFlags flags) |
| Read a line from a file. | |
| bool | mutt_file_iter_line (struct MuttFileIter *iter, FILE *fp, ReadLineFlags flags) |
| Iterate over the lines from an open file pointer. | |
| 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. | |
| void | buf_quote_filename (struct Buffer *buf, const char *filename, bool add_outer) |
| Quote a filename to survive the shell's quoting rules. | |
| int | mutt_file_mkdir (const char *path, mode_t mode) |
| Recursively create directories. | |
| time_t | mutt_file_decrease_mtime (const char *fp, struct stat *st) |
| Decrease a file's modification time by 1 second. | |
| void | mutt_file_set_mtime (const char *from, const char *to) |
| Set the modification time of one file from another. | |
| void | mutt_file_touch_atime (int fd) |
| Set the access time to current time. | |
| bool | mutt_file_touch (const char *path) |
| Make sure a file exists. | |
| 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_lock (int fd, bool excl, bool timeout) |
| (Try to) Lock a file using fcntl() | |
| int | mutt_file_unlock (int fd) |
| Unlock a file previously locked by mutt_file_lock() | |
| void | mutt_file_unlink_empty (const char *path) |
| Delete a file if it's empty. | |
| int | mutt_file_rename (const char *oldfile, const char *newfile) |
| Rename a file. | |
| char * | mutt_file_read_keyword (const char *file, char *buf, size_t buflen) |
| Read a keyword from a file. | |
| int | mutt_file_check_empty (const char *path) |
| Is the mailbox empty. | |
| void | buf_file_expand_fmt_quote (struct Buffer *dest, const char *fmt, const char *src) |
Replace s in a string with a filename. | |
| void | mutt_file_expand_fmt (struct Buffer *dest, const char *fmt, const char *src) |
Replace s in a string with a filename. | |
| 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. | |
| int | mutt_file_timespec_compare (struct timespec *a, struct timespec *b) |
| Compare to time values. | |
| void | mutt_file_get_stat_timespec (struct timespec *dest, struct stat *st, enum MuttStatType type) |
| Read the stat() time into a time value. | |
| 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_stat_compare (struct stat *st1, enum MuttStatType st1_type, struct stat *st2, enum MuttStatType st2_type) |
| Compare two stat infos. | |
| void | mutt_file_resolve_symlink (struct Buffer *buf) |
| Resolve a symlink in place. | |
| size_t | mutt_file_save_str (FILE *fp, const char *str) |
| Save a string to a file. | |
Variables | |
| static const char | RxSpecialChars [] = "^.[$()|*+?{\\" |
| These characters must be escaped in regular expressions. | |
| const char | FilenameSafeChars [] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+@{}._-:%/" |
| 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.c.
|
static |
Compare the struct stat's of two files/dirs.
| st_old | struct stat of the first file/dir |
| st_new | struct stat of the second file/dir |
| true | They match |
This compares the device id (st_dev), inode number (st_ino) and special id (st_rdev) of the files/dirs.
Definition at line 88 of file file.c.
Here is the caller graph for this function:| 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 103 of file file.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 131 of file file.c.
Here is the caller graph for this function:| void mutt_file_unlink | ( | const char * | s | ) |
Delete a file, carefully.
| s | Filename |
This won't follow symlinks.
Definition at line 159 of file file.c.
Here is the caller graph for this function:| 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 195 of file file.c.
Here is the caller graph for this function:| 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 225 of file file.c.
Here is the caller graph for this function:| 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 253 of file file.c.
Here is the call graph for this function:
Here is the caller graph for this function:| 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 309 of file file.c.
Here is the call graph for this function:
Here is the caller graph for this function:| int mutt_file_rmtree | ( | const char * | path | ) |
Recursively remove a directory.
| path | Directory to delete |
| 0 | Success |
| -1 | Error, see errno |
Definition at line 424 of file file.c.
Here is the call graph for this function:
Here is the caller graph for this function:| 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 484 of file file.c.
Here is the call graph for this function:
Here is the caller graph for this function:| 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 515 of file file.c.
Here is the caller graph for this function:| 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 542 of file file.c.
Here is the call graph for this function:
Here is the caller graph for this function:| 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" write-only; "a" append; "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 |
Definition at line 563 of file file.c.
Here is the caller graph for this function:| 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 589 of file file.c.
Here is the caller graph for this function:| 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 631 of file file.c.
Here is the call graph for this function:
Here is the caller graph for this function:| 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 655 of file file.c.
Here is the caller graph for this function:| 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 685 of file file.c.
Here is the caller graph for this function:| 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 766 of file file.c.
Here is the call graph for this function:
Here is the caller graph for this function:| 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 787 of file file.c.
Here is the call graph for this function:
Here is the caller graph for this function:| 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 810 of file file.c.
Here is the call graph for this function:
Here is the caller graph for this function:| 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 851 of file file.c.
Here is the call graph for this function:
Here is the caller graph for this function:| 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 905 of file file.c.
Here is the call graph for this function:
Here is the caller graph for this function:| 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 945 of file file.c.
Here is the caller graph for this function:| 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 968 of file file.c.
Here is the caller graph for this function:| bool mutt_file_touch | ( | const char * | path | ) |
Make sure a file exists.
| path | Filename |
| true | if succeeded |
Definition at line 981 of file file.c.
Here is the caller graph for this function:| 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 1009 of file file.c.
Here is the call graph for this function:
Here is the caller graph for this function:| 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 1032 of file file.c.
Here is the caller graph for this function:| 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 1066 of file file.c.
Here is the caller graph for this function:| 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 1095 of file file.c.
Here is the caller graph for this function:| 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 1142 of file file.c.
Here is the caller graph for this function:| void mutt_file_unlink_empty | ( | const char * | path | ) |
Delete a file if it's empty.
| path | File to delete |
Definition at line 1228 of file file.c.
Here is the call graph for this function:
Here is the caller graph for this function:| 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 1264 of file file.c.
Here is the call graph for this function:
Here is the caller graph for this function:| 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 1299 of file file.c.
Here is the call graph for this function:
Here is the caller graph for this function:| int mutt_file_check_empty | ( | const char * | path | ) |
| 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 1349 of file file.c.
Here is the call graph for this function:
Here is the caller graph for this function:| 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 1364 of file file.c.
Here is the call graph for this function:
Here is the caller graph for this function:| 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 1412 of file file.c.
Here is the caller graph for this function:| 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 1430 of file file.c.
Here is the caller graph for this function:| 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 1450 of file file.c.
Here is the caller graph for this function:| 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 1472 of file file.c.
Here is the caller graph for this function:| 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 1512 of file file.c.
Here is the call graph for this function:
Here is the caller graph for this function:| 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 1534 of file file.c.
Here is the call graph for this function:
Here is the caller graph for this function:| void mutt_file_resolve_symlink | ( | struct Buffer * | buf | ) |
Resolve a symlink in place.
| buf | Input/output path |
Definition at line 1552 of file file.c.
Here is the call graph for this function:
Here is the caller graph for this function:| 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 1572 of file file.c.
Here is the call graph for this function:
Here is the caller graph for this function:
|
static |