1 /**
2  * @file
3  * File management functions
4  *
5  * @authors
6  * Copyright (C) 2017 Richard Russon <rich@flatcap.org>
7  *
8  * @copyright
9  * This program is free software: you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License as published by the Free Software
11  * Foundation, either version 2 of the License, or (at your option) any later
12  * version.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17  * details.
18  *
19  * You should have received a copy of the GNU General Public License along with
20  * this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #ifndef MUTT_LIB_FILE_H
24 #define MUTT_LIB_FILE_H
25 
26 #include "config.h"
27 #include <stdbool.h>
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <sys/types.h>
31 
32 struct Buffer;
33 struct stat;
34 extern const char filename_safe_chars[];
35 
36 /* Flags for mutt_file_read_line() */
37 typedef uint8_t ReadLineFlags;             ///< Flags for mutt_file_read_line(), e.g. #MUTT_RL_CONT
38 #define MUTT_RL_NO_FLAGS        0  ///< No flags are set
39 #define MUTT_RL_CONT      (1 << 0) ///< \-continuation
40 #define MUTT_RL_EOL       (1 << 1) ///< don't strip `\n` / `\r\n`
41 
42 #ifdef HAVE_STRUCT_TIMESPEC
43 struct timespec;
44 #else
45 /**
46  * struct timespec - Time value with nanosecond precision
47  */
48 struct timespec
49 {
50   time_t tv_sec; ///< Number of seconds since the epoch
51   long tv_nsec;  ///< Number of nanosecond, on top
52 };
53 #endif
54 
55 /**
56  * enum MuttStatType - Flags for mutt_file_get_stat_timespec
57  *
58  * These represent filesystem timestamps returned by stat()
59  */
60 enum MuttStatType
61 {
62   MUTT_STAT_ATIME, ///< File/dir's atime - last accessed time
63   MUTT_STAT_MTIME, ///< File/dir's mtime - last modified time
64   MUTT_STAT_CTIME, ///< File/dir's ctime - creation time
65 };
66 
67 /**
68  * struct MuttFileIter - State record for mutt_file_iter_line()
69  */
70 struct MuttFileIter
71 {
72   char *line;   ///< the line data
73   size_t size;  ///< allocated size of line data
74   int line_num; ///< line number
75 };
76 
77 /**
78  * @defgroup mutt_file_map_api File Mapping API
79  *
80  * Prototype for a text handler function for mutt_file_map_lines()
81  *
82  * @param line      Line of text read
83  * @param line_num  Line number
84  * @param user_data Data to pass to the callback function
85  * @retval true  Read was successful
86  * @retval false Abort the reading and free the string
87  */
88 typedef bool (*mutt_file_map_t)(char *line, int line_num, void *user_data);
89 
90 int         mutt_file_check_empty(const char *path);
91 int         mutt_file_chmod(const char *path, mode_t mode);
92 int         mutt_file_chmod_add(const char *path, mode_t mode);
93 int         mutt_file_chmod_add_stat(const char *path, mode_t mode, struct stat *st);
94 int         mutt_file_chmod_rm(const char *path, mode_t mode);
95 int         mutt_file_chmod_rm_stat(const char *path, mode_t mode, struct stat *st);
96 int         mutt_file_copy_bytes(FILE *fp_in, FILE *fp_out, size_t size);
97 int         mutt_file_copy_stream(FILE *fp_in, FILE *fp_out);
98 time_t      mutt_file_decrease_mtime(const char *fp, struct stat *st);
99 void        mutt_file_expand_fmt(struct Buffer *dest, const char *fmt, const char *src);
100 void        mutt_file_expand_fmt_quote(char *dest, size_t destlen, const char *fmt, const char *src);
101 int         mutt_file_fclose(FILE **fp);
102 FILE *      mutt_file_fopen(const char *path, const char *mode);
103 int         mutt_file_fsync_close(FILE **fp);
104 long        mutt_file_get_size(const char *path);
105 long        mutt_file_get_size_fp(FILE* fp);
106 void        mutt_file_get_stat_timespec(struct timespec *dest, struct stat *st, enum MuttStatType type);
107 bool        mutt_file_iter_line(struct MuttFileIter *iter, FILE *fp, ReadLineFlags flags);
108 int         mutt_file_lock(int fd, bool excl, bool timeout);
109 bool        mutt_file_map_lines(mutt_file_map_t func, void *user_data, FILE *fp, ReadLineFlags flags);
110 int         mutt_file_mkdir(const char *path, mode_t mode);
111 FILE *      mutt_file_mkstemp_full(const char *file, int line, const char *func);
112 #define     mutt_file_mkstemp() mutt_file_mkstemp_full(__FILE__, __LINE__, __func__)
113 int         mutt_file_open(const char *path, uint32_t flags);
114 size_t      mutt_file_quote_filename(const char *filename, char *buf, size_t buflen);
115 char *      mutt_file_read_keyword(const char *file, char *buf, size_t buflen);
116 char *      mutt_file_read_line(char *line, size_t *size, FILE *fp, int *line_num, ReadLineFlags flags);
117 int         mutt_file_rename(const char *oldfile, const char *newfile);
118 int         mutt_file_rmtree(const char *path);
119 int         mutt_file_safe_rename(const char *src, const char *target);
120 void        mutt_file_sanitize_filename(char *path, bool slash);
121 int         mutt_file_sanitize_regex(struct Buffer *dest, const char *src);
122 void        mutt_file_set_mtime(const char *from, const char *to);
123 int         mutt_file_stat_compare(struct stat *st1, enum MuttStatType st1_type, struct stat *st2, enum MuttStatType st2_type);
124 int         mutt_file_stat_timespec_compare(struct stat *st, enum MuttStatType type, struct timespec *b);
125 int         mutt_file_symlink(const char *oldpath, const char *newpath);
126 int         mutt_file_timespec_compare(struct timespec *a, struct timespec *b);
127 void        mutt_file_touch_atime(int fd);
128 void        mutt_file_unlink(const char *s);
129 void        mutt_file_unlink_empty(const char *path);
130 int         mutt_file_unlock(int fd);
131 void        mutt_file_resolve_symlink(struct Buffer *buf);
132 
133 void        mutt_buffer_quote_filename(struct Buffer *buf, const char *filename, bool add_outer);
134 void        mutt_buffer_file_expand_fmt_quote(struct Buffer *dest, const char *fmt, const char *src);
135 
136 #endif /* MUTT_LIB_FILE_H */
137