xref: /unit/src/nxt_file.h (revision 611)
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 
300Sigor@sysoev.ru #define                                                                       \
310Sigor@sysoev.ru nxt_file_name_str_set(file_name, mem_pool, name)                              \
320Sigor@sysoev.ru     ((file_name) = (nxt_file_name_t *) (name), NXT_OK)
330Sigor@sysoev.ru 
340Sigor@sysoev.ru 
350Sigor@sysoev.ru #define                                                                       \
360Sigor@sysoev.ru nxt_file_name_alloc(mem_pool, len)                                            \
3765Sigor@sysoev.ru     nxt_mp_nget(mem_pool, len)
380Sigor@sysoev.ru 
390Sigor@sysoev.ru 
400Sigor@sysoev.ru #define                                                                       \
410Sigor@sysoev.ru nxt_file_name_copy(dst, src, len)                                             \
420Sigor@sysoev.ru     nxt_cpymem(dst, src, len)
430Sigor@sysoev.ru 
440Sigor@sysoev.ru 
450Sigor@sysoev.ru #define                                                                       \
460Sigor@sysoev.ru nxt_file_name_add(dst, src, len)                                              \
470Sigor@sysoev.ru     nxt_cpymem(dst, src, len)
480Sigor@sysoev.ru 
490Sigor@sysoev.ru 
500Sigor@sysoev.ru #if (NXT_HAVE_CASELESS_FILESYSTEM)
510Sigor@sysoev.ru 
520Sigor@sysoev.ru /* MacOSX, Cygwin. */
530Sigor@sysoev.ru 
540Sigor@sysoev.ru #define                                                                       \
550Sigor@sysoev.ru nxt_file_name_eq(fn1, fn2)                                                    \
560Sigor@sysoev.ru     (nxt_strcasecmp(fn1, fn2) == 0)
570Sigor@sysoev.ru 
580Sigor@sysoev.ru #else
590Sigor@sysoev.ru 
600Sigor@sysoev.ru #define                                                                       \
610Sigor@sysoev.ru nxt_file_name_eq(fn1, fn2)                                                    \
620Sigor@sysoev.ru     (nxt_strcmp(fn1, fn2) == 0)
630Sigor@sysoev.ru 
640Sigor@sysoev.ru #endif
650Sigor@sysoev.ru 
660Sigor@sysoev.ru 
670Sigor@sysoev.ru #define                                                                       \
680Sigor@sysoev.ru nxt_file_name_is_absolute(name)                                               \
690Sigor@sysoev.ru     (name[0] == '/')
700Sigor@sysoev.ru 
710Sigor@sysoev.ru 
720Sigor@sysoev.ru #define NXT_MAX_PATH_LEN            MAXPATHLEN
730Sigor@sysoev.ru 
740Sigor@sysoev.ru 
750Sigor@sysoev.ru typedef enum {
760Sigor@sysoev.ru     NXT_FILE_UNKNOWN = 0,
770Sigor@sysoev.ru     NXT_FILE_REGULAR,
780Sigor@sysoev.ru     NXT_FILE_DIRECTORY,
790Sigor@sysoev.ru } nxt_file_type_t;
800Sigor@sysoev.ru 
810Sigor@sysoev.ru 
820Sigor@sysoev.ru typedef struct {
830Sigor@sysoev.ru     nxt_file_name_t                 *name;
840Sigor@sysoev.ru 
850Sigor@sysoev.ru     /* Both are int's. */
860Sigor@sysoev.ru     nxt_fd_t                        fd;
870Sigor@sysoev.ru     nxt_err_t                       error;
880Sigor@sysoev.ru 
89*611Svbart@nginx.com #define NXT_FILE_ACCESSED_LONG_AGO  0xFFFF
900Sigor@sysoev.ru     /*
910Sigor@sysoev.ru      * Number of seconds ago the file content was last
920Sigor@sysoev.ru      * read.  The maximum value is about 18 hours.
930Sigor@sysoev.ru      */
940Sigor@sysoev.ru     uint16_t                        accessed;
950Sigor@sysoev.ru 
960Sigor@sysoev.ru     uint8_t                         type;       /* nxt_file_type_t */
970Sigor@sysoev.ru 
980Sigor@sysoev.ru     /*
990Sigor@sysoev.ru      * Log open() file error with given log level if it is non zero.
100564Svbart@nginx.com      * Note that zero log level is NXT_LOG_ALERT.
1010Sigor@sysoev.ru      */
1020Sigor@sysoev.ru     uint8_t                         log_level;
1030Sigor@sysoev.ru 
1040Sigor@sysoev.ru     nxt_time_t                      mtime;
1050Sigor@sysoev.ru     nxt_off_t                       size;
1060Sigor@sysoev.ru } nxt_file_t;
1070Sigor@sysoev.ru 
1080Sigor@sysoev.ru 
10920Sigor@sysoev.ru NXT_EXPORT nxt_int_t nxt_file_open(nxt_task_t *task, nxt_file_t *file,
11020Sigor@sysoev.ru     nxt_uint_t mode, nxt_uint_t create, nxt_file_access_t access);
1110Sigor@sysoev.ru 
1120Sigor@sysoev.ru 
1130Sigor@sysoev.ru /* The file open access modes. */
1140Sigor@sysoev.ru #define NXT_FILE_RDONLY             O_RDONLY
1150Sigor@sysoev.ru #define NXT_FILE_WRONLY             O_WRONLY
1160Sigor@sysoev.ru #define NXT_FILE_RDWR               O_RDWR
1170Sigor@sysoev.ru #define NXT_FILE_APPEND             (O_WRONLY | O_APPEND)
1180Sigor@sysoev.ru 
1190Sigor@sysoev.ru /* The file creation modes. */
1200Sigor@sysoev.ru #define NXT_FILE_CREATE_OR_OPEN     O_CREAT
1210Sigor@sysoev.ru #define NXT_FILE_OPEN               0
1220Sigor@sysoev.ru #define NXT_FILE_TRUNCATE           (O_CREAT | O_TRUNC)
1230Sigor@sysoev.ru 
1240Sigor@sysoev.ru /* The file access rights. */
1250Sigor@sysoev.ru #define NXT_FILE_DEFAULT_ACCESS     0644
1260Sigor@sysoev.ru #define NXT_FILE_OWNER_ACCESS       0600
1270Sigor@sysoev.ru 
1280Sigor@sysoev.ru 
12920Sigor@sysoev.ru NXT_EXPORT void nxt_file_close(nxt_task_t *task, nxt_file_t *file);
1300Sigor@sysoev.ru NXT_EXPORT ssize_t nxt_file_write(nxt_file_t *file, const u_char *buf,
1310Sigor@sysoev.ru     size_t size, nxt_off_t offset);
1320Sigor@sysoev.ru NXT_EXPORT ssize_t nxt_file_read(nxt_file_t *file, u_char *buf, size_t size,
1330Sigor@sysoev.ru     nxt_off_t offset);
1340Sigor@sysoev.ru NXT_EXPORT void nxt_file_read_ahead(nxt_file_t *file, nxt_off_t offset,
1350Sigor@sysoev.ru     size_t size);
1360Sigor@sysoev.ru NXT_EXPORT nxt_int_t nxt_file_info(nxt_file_t *file, nxt_file_info_t *fi);
1370Sigor@sysoev.ru 
1380Sigor@sysoev.ru 
1390Sigor@sysoev.ru #define                                                                       \
1400Sigor@sysoev.ru nxt_is_dir(fi)                                                                \
1410Sigor@sysoev.ru     (S_ISDIR((fi)->st_mode))
1420Sigor@sysoev.ru 
1430Sigor@sysoev.ru #define                                                                       \
1440Sigor@sysoev.ru nxt_is_file(fi)                                                               \
1450Sigor@sysoev.ru     (S_ISREG((fi)->st_mode))
1460Sigor@sysoev.ru 
1470Sigor@sysoev.ru #define                                                                       \
1480Sigor@sysoev.ru nxt_file_size(fi)                                                             \
1490Sigor@sysoev.ru     (fi)->st_size
1500Sigor@sysoev.ru 
1510Sigor@sysoev.ru #define                                                                       \
1520Sigor@sysoev.ru nxt_file_mtime(fi)                                                            \
1530Sigor@sysoev.ru     (fi)->st_mtime
1540Sigor@sysoev.ru 
1550Sigor@sysoev.ru 
1560Sigor@sysoev.ru NXT_EXPORT nxt_int_t nxt_file_delete(nxt_file_name_t *name);
1570Sigor@sysoev.ru NXT_EXPORT nxt_int_t nxt_file_set_access(nxt_file_name_t *name,
1580Sigor@sysoev.ru     nxt_file_access_t access);
1590Sigor@sysoev.ru NXT_EXPORT nxt_int_t nxt_file_rename(nxt_file_name_t *old_name,
1600Sigor@sysoev.ru     nxt_file_name_t *new_name);
1610Sigor@sysoev.ru 
16213Sigor@sysoev.ru NXT_EXPORT nxt_int_t nxt_fd_nonblocking(nxt_task_t *task, nxt_fd_t fd);
16313Sigor@sysoev.ru NXT_EXPORT nxt_int_t nxt_fd_blocking(nxt_task_t *task, nxt_fd_t fd);
1640Sigor@sysoev.ru NXT_EXPORT ssize_t nxt_fd_write(nxt_fd_t fd, u_char *buf, size_t size);
1650Sigor@sysoev.ru NXT_EXPORT ssize_t nxt_fd_read(nxt_fd_t fd, u_char *buf, size_t size);
1660Sigor@sysoev.ru NXT_EXPORT void nxt_fd_close(nxt_fd_t fd);
1670Sigor@sysoev.ru 
1680Sigor@sysoev.ru NXT_EXPORT nxt_int_t nxt_file_redirect(nxt_file_t *file, nxt_fd_t fd);
1690Sigor@sysoev.ru NXT_EXPORT nxt_int_t nxt_file_stderr(nxt_file_t *file);
1700Sigor@sysoev.ru NXT_EXPORT nxt_int_t nxt_stderr_start(void);
1710Sigor@sysoev.ru 
1720Sigor@sysoev.ru 
1730Sigor@sysoev.ru #define nxt_stdout  STDOUT_FILENO
1740Sigor@sysoev.ru #define nxt_stderr  STDERR_FILENO
1750Sigor@sysoev.ru 
1760Sigor@sysoev.ru 
1770Sigor@sysoev.ru #define                                                                       \
1780Sigor@sysoev.ru nxt_write_console(fd, buf, size)                                              \
1790Sigor@sysoev.ru     write(fd, buf, size)
1800Sigor@sysoev.ru 
1810Sigor@sysoev.ru #define                                                                       \
1820Sigor@sysoev.ru nxt_write_syslog(priority, message)                                           \
1830Sigor@sysoev.ru     syslog(priority, "%s", message)
1840Sigor@sysoev.ru 
1850Sigor@sysoev.ru 
18613Sigor@sysoev.ru NXT_EXPORT nxt_int_t nxt_pipe_create(nxt_task_t *task, nxt_fd_t *pp,
18713Sigor@sysoev.ru     nxt_bool_t nbread, nxt_bool_t nbwrite);
18813Sigor@sysoev.ru NXT_EXPORT void nxt_pipe_close(nxt_task_t *task, nxt_fd_t *pp);
1890Sigor@sysoev.ru 
1900Sigor@sysoev.ru NXT_EXPORT size_t nxt_dir_current(char *buf, size_t len);
1910Sigor@sysoev.ru 
1920Sigor@sysoev.ru 
1930Sigor@sysoev.ru #endif /* _NXT_UNIX_FILE_H_INCLUDED_ */
194