xref: /unit/src/nxt_file.h (revision 2084:7d479274f334)
10Sigor@sysoev.ru 
20Sigor@sysoev.ru /*
30Sigor@sysoev.ru  * Copyright (C) Igor Sysoev
40Sigor@sysoev.ru  * Copyright (C) NGINX, Inc.
50Sigor@sysoev.ru  */
60Sigor@sysoev.ru 
70Sigor@sysoev.ru #ifndef _NXT_UNIX_FILE_H_INCLUDED_
80Sigor@sysoev.ru #define _NXT_UNIX_FILE_H_INCLUDED_
90Sigor@sysoev.ru 
100Sigor@sysoev.ru 
110Sigor@sysoev.ru typedef int                         nxt_fd_t;
120Sigor@sysoev.ru 
130Sigor@sysoev.ru #define NXT_FILE_INVALID            -1
140Sigor@sysoev.ru 
150Sigor@sysoev.ru typedef nxt_uint_t                  nxt_file_access_t;
160Sigor@sysoev.ru typedef struct stat                 nxt_file_info_t;
170Sigor@sysoev.ru 
180Sigor@sysoev.ru 
190Sigor@sysoev.ru #define NXT_FILE_SYSTEM_NAME_UTF8   1
200Sigor@sysoev.ru 
210Sigor@sysoev.ru typedef u_char                      nxt_file_name_t;
220Sigor@sysoev.ru 
230Sigor@sysoev.ru 
240Sigor@sysoev.ru typedef struct {
250Sigor@sysoev.ru     size_t                          len;
260Sigor@sysoev.ru     nxt_file_name_t                 *start;
270Sigor@sysoev.ru } nxt_file_name_str_t;
280Sigor@sysoev.ru 
290Sigor@sysoev.ru 
30*2084Salx.manpages@gmail.com #define nxt_file_name_str_set(file_name, mem_pool, name)                      \
310Sigor@sysoev.ru     ((file_name) = (nxt_file_name_t *) (name), NXT_OK)
320Sigor@sysoev.ru 
330Sigor@sysoev.ru 
34*2084Salx.manpages@gmail.com #define nxt_file_name_alloc(mem_pool, len)                                    \
3565Sigor@sysoev.ru     nxt_mp_nget(mem_pool, len)
360Sigor@sysoev.ru 
370Sigor@sysoev.ru 
38*2084Salx.manpages@gmail.com #define nxt_file_name_copy(dst, src, len)                                     \
390Sigor@sysoev.ru     nxt_cpymem(dst, src, len)
400Sigor@sysoev.ru 
410Sigor@sysoev.ru 
42*2084Salx.manpages@gmail.com #define nxt_file_name_add(dst, src, len)                                      \
430Sigor@sysoev.ru     nxt_cpymem(dst, src, len)
440Sigor@sysoev.ru 
450Sigor@sysoev.ru 
460Sigor@sysoev.ru #if (NXT_HAVE_CASELESS_FILESYSTEM)
470Sigor@sysoev.ru 
480Sigor@sysoev.ru /* MacOSX, Cygwin. */
490Sigor@sysoev.ru 
50*2084Salx.manpages@gmail.com #define nxt_file_name_eq(fn1, fn2)                                            \
510Sigor@sysoev.ru     (nxt_strcasecmp(fn1, fn2) == 0)
520Sigor@sysoev.ru 
530Sigor@sysoev.ru #else
540Sigor@sysoev.ru 
55*2084Salx.manpages@gmail.com #define nxt_file_name_eq(fn1, fn2)                                            \
560Sigor@sysoev.ru     (nxt_strcmp(fn1, fn2) == 0)
570Sigor@sysoev.ru 
580Sigor@sysoev.ru #endif
590Sigor@sysoev.ru 
600Sigor@sysoev.ru 
61*2084Salx.manpages@gmail.com #define nxt_file_name_is_absolute(name)                                       \
620Sigor@sysoev.ru     (name[0] == '/')
630Sigor@sysoev.ru 
640Sigor@sysoev.ru 
650Sigor@sysoev.ru #define NXT_MAX_PATH_LEN            MAXPATHLEN
660Sigor@sysoev.ru 
670Sigor@sysoev.ru 
680Sigor@sysoev.ru typedef enum {
690Sigor@sysoev.ru     NXT_FILE_UNKNOWN = 0,
700Sigor@sysoev.ru     NXT_FILE_REGULAR,
710Sigor@sysoev.ru     NXT_FILE_DIRECTORY,
720Sigor@sysoev.ru } nxt_file_type_t;
730Sigor@sysoev.ru 
740Sigor@sysoev.ru 
750Sigor@sysoev.ru typedef struct {
760Sigor@sysoev.ru     nxt_file_name_t                 *name;
770Sigor@sysoev.ru 
780Sigor@sysoev.ru     /* Both are int's. */
790Sigor@sysoev.ru     nxt_fd_t                        fd;
800Sigor@sysoev.ru     nxt_err_t                       error;
810Sigor@sysoev.ru 
82611Svbart@nginx.com #define NXT_FILE_ACCESSED_LONG_AGO  0xFFFF
830Sigor@sysoev.ru     /*
840Sigor@sysoev.ru      * Number of seconds ago the file content was last
850Sigor@sysoev.ru      * read.  The maximum value is about 18 hours.
860Sigor@sysoev.ru      */
870Sigor@sysoev.ru     uint16_t                        accessed;
880Sigor@sysoev.ru 
890Sigor@sysoev.ru     uint8_t                         type;       /* nxt_file_type_t */
900Sigor@sysoev.ru 
910Sigor@sysoev.ru     /*
920Sigor@sysoev.ru      * Log open() file error with given log level if it is non zero.
93564Svbart@nginx.com      * Note that zero log level is NXT_LOG_ALERT.
940Sigor@sysoev.ru      */
950Sigor@sysoev.ru     uint8_t                         log_level;
960Sigor@sysoev.ru 
970Sigor@sysoev.ru     nxt_time_t                      mtime;
980Sigor@sysoev.ru     nxt_off_t                       size;
990Sigor@sysoev.ru } nxt_file_t;
1000Sigor@sysoev.ru 
1010Sigor@sysoev.ru 
10220Sigor@sysoev.ru NXT_EXPORT nxt_int_t nxt_file_open(nxt_task_t *task, nxt_file_t *file,
10320Sigor@sysoev.ru     nxt_uint_t mode, nxt_uint_t create, nxt_file_access_t access);
1040Sigor@sysoev.ru 
1051855Sz.hong@f5.com #if (NXT_HAVE_OPENAT2)
1061855Sz.hong@f5.com NXT_EXPORT nxt_int_t nxt_file_openat2(nxt_task_t *task, nxt_file_t *file,
1071855Sz.hong@f5.com     nxt_uint_t mode, nxt_uint_t create, nxt_file_access_t access, nxt_fd_t dfd,
1081855Sz.hong@f5.com     nxt_uint_t resolve);
1091855Sz.hong@f5.com #endif
1101855Sz.hong@f5.com 
1110Sigor@sysoev.ru 
1120Sigor@sysoev.ru /* The file open access modes. */
1130Sigor@sysoev.ru #define NXT_FILE_RDONLY             O_RDONLY
1140Sigor@sysoev.ru #define NXT_FILE_WRONLY             O_WRONLY
1150Sigor@sysoev.ru #define NXT_FILE_RDWR               O_RDWR
1160Sigor@sysoev.ru #define NXT_FILE_APPEND             (O_WRONLY | O_APPEND)
1170Sigor@sysoev.ru 
1181855Sz.hong@f5.com #if (NXT_HAVE_OPENAT2)
1191855Sz.hong@f5.com 
1201855Sz.hong@f5.com #if defined(O_DIRECTORY)
1211855Sz.hong@f5.com #define NXT_FILE_DIRECTORY          O_DIRECTORY
1221855Sz.hong@f5.com #else
1231855Sz.hong@f5.com #define NXT_FILE_DIRECTORY          0
1241855Sz.hong@f5.com #endif
1251855Sz.hong@f5.com 
1261855Sz.hong@f5.com #if defined(O_SEARCH)
1271855Sz.hong@f5.com #define NXT_FILE_SEARCH             (O_SEARCH|NXT_FILE_DIRECTORY)
1281855Sz.hong@f5.com 
1291855Sz.hong@f5.com #elif defined(O_EXEC)
1301855Sz.hong@f5.com #define NXT_FILE_SEARCH             (O_EXEC|NXT_FILE_DIRECTORY)
1311855Sz.hong@f5.com 
1321855Sz.hong@f5.com #else
1331855Sz.hong@f5.com /*
1341855Sz.hong@f5.com  * O_PATH is used in combination with O_RDONLY.  The last one is ignored
1351855Sz.hong@f5.com  * if O_PATH is used, but it allows Unit to not fail when it was built on
1361855Sz.hong@f5.com  * modern system (i.e. glibc 2.14+) and run with a kernel older than 2.6.39.
1371855Sz.hong@f5.com  * Then O_PATH is unknown to the kernel and ignored, while O_RDONLY is used.
1381855Sz.hong@f5.com  */
1391855Sz.hong@f5.com #define NXT_FILE_SEARCH             (O_PATH|O_RDONLY|NXT_FILE_DIRECTORY)
1401855Sz.hong@f5.com #endif
1411855Sz.hong@f5.com 
1421855Sz.hong@f5.com #endif /* NXT_HAVE_OPENAT2 */
1431855Sz.hong@f5.com 
1440Sigor@sysoev.ru /* The file creation modes. */
1450Sigor@sysoev.ru #define NXT_FILE_CREATE_OR_OPEN     O_CREAT
1460Sigor@sysoev.ru #define NXT_FILE_OPEN               0
1470Sigor@sysoev.ru #define NXT_FILE_TRUNCATE           (O_CREAT | O_TRUNC)
1480Sigor@sysoev.ru 
1490Sigor@sysoev.ru /* The file access rights. */
1500Sigor@sysoev.ru #define NXT_FILE_DEFAULT_ACCESS     0644
1510Sigor@sysoev.ru #define NXT_FILE_OWNER_ACCESS       0600
1520Sigor@sysoev.ru 
1530Sigor@sysoev.ru 
15420Sigor@sysoev.ru NXT_EXPORT void nxt_file_close(nxt_task_t *task, nxt_file_t *file);
1550Sigor@sysoev.ru NXT_EXPORT ssize_t nxt_file_write(nxt_file_t *file, const u_char *buf,
1560Sigor@sysoev.ru     size_t size, nxt_off_t offset);
1570Sigor@sysoev.ru NXT_EXPORT ssize_t nxt_file_read(nxt_file_t *file, u_char *buf, size_t size,
1580Sigor@sysoev.ru     nxt_off_t offset);
1590Sigor@sysoev.ru NXT_EXPORT void nxt_file_read_ahead(nxt_file_t *file, nxt_off_t offset,
1600Sigor@sysoev.ru     size_t size);
1610Sigor@sysoev.ru NXT_EXPORT nxt_int_t nxt_file_info(nxt_file_t *file, nxt_file_info_t *fi);
1620Sigor@sysoev.ru 
1630Sigor@sysoev.ru 
164*2084Salx.manpages@gmail.com #define nxt_is_dir(fi)                                                        \
1650Sigor@sysoev.ru     (S_ISDIR((fi)->st_mode))
1660Sigor@sysoev.ru 
167*2084Salx.manpages@gmail.com #define nxt_is_file(fi)                                                       \
1680Sigor@sysoev.ru     (S_ISREG((fi)->st_mode))
1690Sigor@sysoev.ru 
170*2084Salx.manpages@gmail.com #define nxt_file_size(fi)                                                     \
1710Sigor@sysoev.ru     (fi)->st_size
1720Sigor@sysoev.ru 
173*2084Salx.manpages@gmail.com #define nxt_file_mtime(fi)                                                    \
1740Sigor@sysoev.ru     (fi)->st_mtime
1750Sigor@sysoev.ru 
1760Sigor@sysoev.ru 
1770Sigor@sysoev.ru NXT_EXPORT nxt_int_t nxt_file_delete(nxt_file_name_t *name);
1780Sigor@sysoev.ru NXT_EXPORT nxt_int_t nxt_file_set_access(nxt_file_name_t *name,
1790Sigor@sysoev.ru     nxt_file_access_t access);
1800Sigor@sysoev.ru NXT_EXPORT nxt_int_t nxt_file_rename(nxt_file_name_t *old_name,
1810Sigor@sysoev.ru     nxt_file_name_t *new_name);
1820Sigor@sysoev.ru 
18313Sigor@sysoev.ru NXT_EXPORT nxt_int_t nxt_fd_nonblocking(nxt_task_t *task, nxt_fd_t fd);
18413Sigor@sysoev.ru NXT_EXPORT nxt_int_t nxt_fd_blocking(nxt_task_t *task, nxt_fd_t fd);
1850Sigor@sysoev.ru NXT_EXPORT ssize_t nxt_fd_write(nxt_fd_t fd, u_char *buf, size_t size);
1860Sigor@sysoev.ru NXT_EXPORT ssize_t nxt_fd_read(nxt_fd_t fd, u_char *buf, size_t size);
1870Sigor@sysoev.ru NXT_EXPORT void nxt_fd_close(nxt_fd_t fd);
1880Sigor@sysoev.ru 
1890Sigor@sysoev.ru NXT_EXPORT nxt_int_t nxt_file_redirect(nxt_file_t *file, nxt_fd_t fd);
1900Sigor@sysoev.ru NXT_EXPORT nxt_int_t nxt_file_stderr(nxt_file_t *file);
1910Sigor@sysoev.ru NXT_EXPORT nxt_int_t nxt_stderr_start(void);
1920Sigor@sysoev.ru 
1930Sigor@sysoev.ru 
1940Sigor@sysoev.ru #define nxt_stdout  STDOUT_FILENO
1950Sigor@sysoev.ru #define nxt_stderr  STDERR_FILENO
1960Sigor@sysoev.ru 
1970Sigor@sysoev.ru 
198*2084Salx.manpages@gmail.com #define nxt_write_console(fd, buf, size)                                      \
1990Sigor@sysoev.ru     write(fd, buf, size)
2000Sigor@sysoev.ru 
201*2084Salx.manpages@gmail.com #define nxt_write_syslog(priority, message)                                   \
2020Sigor@sysoev.ru     syslog(priority, "%s", message)
2030Sigor@sysoev.ru 
2040Sigor@sysoev.ru 
20513Sigor@sysoev.ru NXT_EXPORT nxt_int_t nxt_pipe_create(nxt_task_t *task, nxt_fd_t *pp,
20613Sigor@sysoev.ru     nxt_bool_t nbread, nxt_bool_t nbwrite);
20713Sigor@sysoev.ru NXT_EXPORT void nxt_pipe_close(nxt_task_t *task, nxt_fd_t *pp);
2080Sigor@sysoev.ru 
2090Sigor@sysoev.ru NXT_EXPORT size_t nxt_dir_current(char *buf, size_t len);
2100Sigor@sysoev.ru 
2110Sigor@sysoev.ru 
2120Sigor@sysoev.ru #endif /* _NXT_UNIX_FILE_H_INCLUDED_ */
213