xref: /unit/src/nxt_file.h (revision 1855:a946d8cd7f8c)
1 
2 /*
3  * Copyright (C) Igor Sysoev
4  * Copyright (C) NGINX, Inc.
5  */
6 
7 #ifndef _NXT_UNIX_FILE_H_INCLUDED_
8 #define _NXT_UNIX_FILE_H_INCLUDED_
9 
10 
11 typedef int                         nxt_fd_t;
12 
13 #define NXT_FILE_INVALID            -1
14 
15 typedef nxt_uint_t                  nxt_file_access_t;
16 typedef struct stat                 nxt_file_info_t;
17 
18 
19 #define NXT_FILE_SYSTEM_NAME_UTF8   1
20 
21 typedef u_char                      nxt_file_name_t;
22 
23 
24 typedef struct {
25     size_t                          len;
26     nxt_file_name_t                 *start;
27 } nxt_file_name_str_t;
28 
29 
30 #define                                                                       \
31 nxt_file_name_str_set(file_name, mem_pool, name)                              \
32     ((file_name) = (nxt_file_name_t *) (name), NXT_OK)
33 
34 
35 #define                                                                       \
36 nxt_file_name_alloc(mem_pool, len)                                            \
37     nxt_mp_nget(mem_pool, len)
38 
39 
40 #define                                                                       \
41 nxt_file_name_copy(dst, src, len)                                             \
42     nxt_cpymem(dst, src, len)
43 
44 
45 #define                                                                       \
46 nxt_file_name_add(dst, src, len)                                              \
47     nxt_cpymem(dst, src, len)
48 
49 
50 #if (NXT_HAVE_CASELESS_FILESYSTEM)
51 
52 /* MacOSX, Cygwin. */
53 
54 #define                                                                       \
55 nxt_file_name_eq(fn1, fn2)                                                    \
56     (nxt_strcasecmp(fn1, fn2) == 0)
57 
58 #else
59 
60 #define                                                                       \
61 nxt_file_name_eq(fn1, fn2)                                                    \
62     (nxt_strcmp(fn1, fn2) == 0)
63 
64 #endif
65 
66 
67 #define                                                                       \
68 nxt_file_name_is_absolute(name)                                               \
69     (name[0] == '/')
70 
71 
72 #define NXT_MAX_PATH_LEN            MAXPATHLEN
73 
74 
75 typedef enum {
76     NXT_FILE_UNKNOWN = 0,
77     NXT_FILE_REGULAR,
78     NXT_FILE_DIRECTORY,
79 } nxt_file_type_t;
80 
81 
82 typedef struct {
83     nxt_file_name_t                 *name;
84 
85     /* Both are int's. */
86     nxt_fd_t                        fd;
87     nxt_err_t                       error;
88 
89 #define NXT_FILE_ACCESSED_LONG_AGO  0xFFFF
90     /*
91      * Number of seconds ago the file content was last
92      * read.  The maximum value is about 18 hours.
93      */
94     uint16_t                        accessed;
95 
96     uint8_t                         type;       /* nxt_file_type_t */
97 
98     /*
99      * Log open() file error with given log level if it is non zero.
100      * Note that zero log level is NXT_LOG_ALERT.
101      */
102     uint8_t                         log_level;
103 
104     nxt_time_t                      mtime;
105     nxt_off_t                       size;
106 } nxt_file_t;
107 
108 
109 NXT_EXPORT nxt_int_t nxt_file_open(nxt_task_t *task, nxt_file_t *file,
110     nxt_uint_t mode, nxt_uint_t create, nxt_file_access_t access);
111 
112 #if (NXT_HAVE_OPENAT2)
113 NXT_EXPORT nxt_int_t nxt_file_openat2(nxt_task_t *task, nxt_file_t *file,
114     nxt_uint_t mode, nxt_uint_t create, nxt_file_access_t access, nxt_fd_t dfd,
115     nxt_uint_t resolve);
116 #endif
117 
118 
119 /* The file open access modes. */
120 #define NXT_FILE_RDONLY             O_RDONLY
121 #define NXT_FILE_WRONLY             O_WRONLY
122 #define NXT_FILE_RDWR               O_RDWR
123 #define NXT_FILE_APPEND             (O_WRONLY | O_APPEND)
124 
125 #if (NXT_HAVE_OPENAT2)
126 
127 #if defined(O_DIRECTORY)
128 #define NXT_FILE_DIRECTORY          O_DIRECTORY
129 #else
130 #define NXT_FILE_DIRECTORY          0
131 #endif
132 
133 #if defined(O_SEARCH)
134 #define NXT_FILE_SEARCH             (O_SEARCH|NXT_FILE_DIRECTORY)
135 
136 #elif defined(O_EXEC)
137 #define NXT_FILE_SEARCH             (O_EXEC|NXT_FILE_DIRECTORY)
138 
139 #else
140 /*
141  * O_PATH is used in combination with O_RDONLY.  The last one is ignored
142  * if O_PATH is used, but it allows Unit to not fail when it was built on
143  * modern system (i.e. glibc 2.14+) and run with a kernel older than 2.6.39.
144  * Then O_PATH is unknown to the kernel and ignored, while O_RDONLY is used.
145  */
146 #define NXT_FILE_SEARCH             (O_PATH|O_RDONLY|NXT_FILE_DIRECTORY)
147 #endif
148 
149 #endif /* NXT_HAVE_OPENAT2 */
150 
151 /* The file creation modes. */
152 #define NXT_FILE_CREATE_OR_OPEN     O_CREAT
153 #define NXT_FILE_OPEN               0
154 #define NXT_FILE_TRUNCATE           (O_CREAT | O_TRUNC)
155 
156 /* The file access rights. */
157 #define NXT_FILE_DEFAULT_ACCESS     0644
158 #define NXT_FILE_OWNER_ACCESS       0600
159 
160 
161 NXT_EXPORT void nxt_file_close(nxt_task_t *task, nxt_file_t *file);
162 NXT_EXPORT ssize_t nxt_file_write(nxt_file_t *file, const u_char *buf,
163     size_t size, nxt_off_t offset);
164 NXT_EXPORT ssize_t nxt_file_read(nxt_file_t *file, u_char *buf, size_t size,
165     nxt_off_t offset);
166 NXT_EXPORT void nxt_file_read_ahead(nxt_file_t *file, nxt_off_t offset,
167     size_t size);
168 NXT_EXPORT nxt_int_t nxt_file_info(nxt_file_t *file, nxt_file_info_t *fi);
169 
170 
171 #define                                                                       \
172 nxt_is_dir(fi)                                                                \
173     (S_ISDIR((fi)->st_mode))
174 
175 #define                                                                       \
176 nxt_is_file(fi)                                                               \
177     (S_ISREG((fi)->st_mode))
178 
179 #define                                                                       \
180 nxt_file_size(fi)                                                             \
181     (fi)->st_size
182 
183 #define                                                                       \
184 nxt_file_mtime(fi)                                                            \
185     (fi)->st_mtime
186 
187 
188 NXT_EXPORT nxt_int_t nxt_file_delete(nxt_file_name_t *name);
189 NXT_EXPORT nxt_int_t nxt_file_set_access(nxt_file_name_t *name,
190     nxt_file_access_t access);
191 NXT_EXPORT nxt_int_t nxt_file_rename(nxt_file_name_t *old_name,
192     nxt_file_name_t *new_name);
193 
194 NXT_EXPORT nxt_int_t nxt_fd_nonblocking(nxt_task_t *task, nxt_fd_t fd);
195 NXT_EXPORT nxt_int_t nxt_fd_blocking(nxt_task_t *task, nxt_fd_t fd);
196 NXT_EXPORT ssize_t nxt_fd_write(nxt_fd_t fd, u_char *buf, size_t size);
197 NXT_EXPORT ssize_t nxt_fd_read(nxt_fd_t fd, u_char *buf, size_t size);
198 NXT_EXPORT void nxt_fd_close(nxt_fd_t fd);
199 
200 NXT_EXPORT nxt_int_t nxt_file_redirect(nxt_file_t *file, nxt_fd_t fd);
201 NXT_EXPORT nxt_int_t nxt_file_stderr(nxt_file_t *file);
202 NXT_EXPORT nxt_int_t nxt_stderr_start(void);
203 
204 
205 #define nxt_stdout  STDOUT_FILENO
206 #define nxt_stderr  STDERR_FILENO
207 
208 
209 #define                                                                       \
210 nxt_write_console(fd, buf, size)                                              \
211     write(fd, buf, size)
212 
213 #define                                                                       \
214 nxt_write_syslog(priority, message)                                           \
215     syslog(priority, "%s", message)
216 
217 
218 NXT_EXPORT nxt_int_t nxt_pipe_create(nxt_task_t *task, nxt_fd_t *pp,
219     nxt_bool_t nbread, nxt_bool_t nbwrite);
220 NXT_EXPORT void nxt_pipe_close(nxt_task_t *task, nxt_fd_t *pp);
221 
222 NXT_EXPORT size_t nxt_dir_current(char *buf, size_t len);
223 
224 
225 #endif /* _NXT_UNIX_FILE_H_INCLUDED_ */
226