xref: /unit/src/nxt_file.h (revision 564:762f8c976ead)
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 
113 /* The file open access modes. */
114 #define NXT_FILE_RDONLY             O_RDONLY
115 #define NXT_FILE_WRONLY             O_WRONLY
116 #define NXT_FILE_RDWR               O_RDWR
117 #define NXT_FILE_APPEND             (O_WRONLY | O_APPEND)
118 
119 /* The file creation modes. */
120 #define NXT_FILE_CREATE_OR_OPEN     O_CREAT
121 #define NXT_FILE_OPEN               0
122 #define NXT_FILE_TRUNCATE           (O_CREAT | O_TRUNC)
123 
124 /* The file access rights. */
125 #define NXT_FILE_DEFAULT_ACCESS     0644
126 #define NXT_FILE_OWNER_ACCESS       0600
127 
128 
129 NXT_EXPORT void nxt_file_close(nxt_task_t *task, nxt_file_t *file);
130 NXT_EXPORT ssize_t nxt_file_write(nxt_file_t *file, const u_char *buf,
131     size_t size, nxt_off_t offset);
132 NXT_EXPORT ssize_t nxt_file_read(nxt_file_t *file, u_char *buf, size_t size,
133     nxt_off_t offset);
134 NXT_EXPORT void nxt_file_read_ahead(nxt_file_t *file, nxt_off_t offset,
135     size_t size);
136 NXT_EXPORT nxt_int_t nxt_file_info(nxt_file_t *file, nxt_file_info_t *fi);
137 
138 
139 #define                                                                       \
140 nxt_is_dir(fi)                                                                \
141     (S_ISDIR((fi)->st_mode))
142 
143 #define                                                                       \
144 nxt_is_file(fi)                                                               \
145     (S_ISREG((fi)->st_mode))
146 
147 #define                                                                       \
148 nxt_file_size(fi)                                                             \
149     (fi)->st_size
150 
151 #define                                                                       \
152 nxt_file_mtime(fi)                                                            \
153     (fi)->st_mtime
154 
155 
156 NXT_EXPORT nxt_int_t nxt_file_delete(nxt_file_name_t *name);
157 NXT_EXPORT nxt_int_t nxt_file_set_access(nxt_file_name_t *name,
158     nxt_file_access_t access);
159 NXT_EXPORT nxt_int_t nxt_file_rename(nxt_file_name_t *old_name,
160     nxt_file_name_t *new_name);
161 
162 NXT_EXPORT nxt_int_t nxt_fd_nonblocking(nxt_task_t *task, nxt_fd_t fd);
163 NXT_EXPORT nxt_int_t nxt_fd_blocking(nxt_task_t *task, nxt_fd_t fd);
164 NXT_EXPORT ssize_t nxt_fd_write(nxt_fd_t fd, u_char *buf, size_t size);
165 NXT_EXPORT ssize_t nxt_fd_read(nxt_fd_t fd, u_char *buf, size_t size);
166 NXT_EXPORT void nxt_fd_close(nxt_fd_t fd);
167 
168 NXT_EXPORT nxt_int_t nxt_file_redirect(nxt_file_t *file, nxt_fd_t fd);
169 NXT_EXPORT nxt_int_t nxt_file_stderr(nxt_file_t *file);
170 NXT_EXPORT nxt_int_t nxt_stderr_start(void);
171 
172 
173 #define nxt_stdout  STDOUT_FILENO
174 #define nxt_stderr  STDERR_FILENO
175 
176 
177 #define                                                                       \
178 nxt_write_console(fd, buf, size)                                              \
179     write(fd, buf, size)
180 
181 #define                                                                       \
182 nxt_write_syslog(priority, message)                                           \
183     syslog(priority, "%s", message)
184 
185 
186 NXT_EXPORT nxt_int_t nxt_pipe_create(nxt_task_t *task, nxt_fd_t *pp,
187     nxt_bool_t nbread, nxt_bool_t nbwrite);
188 NXT_EXPORT void nxt_pipe_close(nxt_task_t *task, nxt_fd_t *pp);
189 
190 NXT_EXPORT size_t nxt_dir_current(char *buf, size_t len);
191 
192 
193 #endif /* _NXT_UNIX_FILE_H_INCLUDED_ */
194