Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.13.12 ]​[ nginx-1.12.2 ]​

0001 
0002 /*
0003  * Copyright (C) Igor Sysoev
0004  * Copyright (C) Nginx, Inc.
0005  */
0006 
0007 
0008 #ifndef _NGX_FILES_H_INCLUDED_
0009 #define _NGX_FILES_H_INCLUDED_
0010 
0011 
0012 #include <ngx_config.h>
0013 #include <ngx_core.h>
0014 
0015 
0016 typedef int                      ngx_fd_t;
0017 typedef struct stat              ngx_file_info_t;
0018 typedef ino_t                    ngx_file_uniq_t;
0019 
0020 
0021 typedef struct {
0022     u_char                      *name;
0023     size_t                       size;
0024     void                        *addr;
0025     ngx_fd_t                     fd;
0026     ngx_log_t                   *log;
0027 } ngx_file_mapping_t;
0028 
0029 
0030 typedef struct {
0031     DIR                         *dir;
0032     struct dirent               *de;
0033     struct stat                  info;
0034 
0035     unsigned                     type:8;
0036     unsigned                     valid_info:1;
0037 } ngx_dir_t;
0038 
0039 
0040 typedef struct {
0041     size_t                       n;
0042     glob_t                       pglob;
0043     u_char                      *pattern;
0044     ngx_log_t                   *log;
0045     ngx_uint_t                   test;
0046 } ngx_glob_t;
0047 
0048 
0049 #define NGX_INVALID_FILE         -1
0050 #define NGX_FILE_ERROR           -1
0051 
0052 
0053 
0054 #ifdef __CYGWIN__
0055 
0056 #ifndef NGX_HAVE_CASELESS_FILESYSTEM
0057 #define NGX_HAVE_CASELESS_FILESYSTEM  1
0058 #endif
0059 
0060 #define ngx_open_file(name, mode, create, access)                            \
0061     open((const char *) name, mode|create|O_BINARY, access)
0062 
0063 #else
0064 
0065 #define ngx_open_file(name, mode, create, access)                            \
0066     open((const char *) name, mode|create, access)
0067 
0068 #endif
0069 
0070 #define ngx_open_file_n          "open()"
0071 
0072 #define NGX_FILE_RDONLY          O_RDONLY
0073 #define NGX_FILE_WRONLY          O_WRONLY
0074 #define NGX_FILE_RDWR            O_RDWR
0075 #define NGX_FILE_CREATE_OR_OPEN  O_CREAT
0076 #define NGX_FILE_OPEN            0
0077 #define NGX_FILE_TRUNCATE        (O_CREAT|O_TRUNC)
0078 #define NGX_FILE_APPEND          (O_WRONLY|O_APPEND)
0079 #define NGX_FILE_NONBLOCK        O_NONBLOCK
0080 
0081 #if (NGX_HAVE_OPENAT)
0082 #define NGX_FILE_NOFOLLOW        O_NOFOLLOW
0083 
0084 #if defined(O_DIRECTORY)
0085 #define NGX_FILE_DIRECTORY       O_DIRECTORY
0086 #else
0087 #define NGX_FILE_DIRECTORY       0
0088 #endif
0089 
0090 #if defined(O_SEARCH)
0091 #define NGX_FILE_SEARCH          (O_SEARCH|NGX_FILE_DIRECTORY)
0092 
0093 #elif defined(O_EXEC)
0094 #define NGX_FILE_SEARCH          (O_EXEC|NGX_FILE_DIRECTORY)
0095 
0096 #elif (NGX_HAVE_O_PATH)
0097 #define NGX_FILE_SEARCH          (O_PATH|O_RDONLY|NGX_FILE_DIRECTORY)
0098 
0099 #else
0100 #define NGX_FILE_SEARCH          (O_RDONLY|NGX_FILE_DIRECTORY)
0101 #endif
0102 
0103 #endif /* NGX_HAVE_OPENAT */
0104 
0105 #define NGX_FILE_DEFAULT_ACCESS  0644
0106 #define NGX_FILE_OWNER_ACCESS    0600
0107 
0108 
0109 #define ngx_close_file           close
0110 #define ngx_close_file_n         "close()"
0111 
0112 
0113 #define ngx_delete_file(name)    unlink((const char *) name)
0114 #define ngx_delete_file_n        "unlink()"
0115 
0116 
0117 ngx_fd_t ngx_open_tempfile(u_char *name, ngx_uint_t persistent,
0118     ngx_uint_t access);
0119 #define ngx_open_tempfile_n      "open()"
0120 
0121 
0122 ssize_t ngx_read_file(ngx_file_t *file, u_char *buf, size_t size, off_t offset);
0123 #if (NGX_HAVE_PREAD)
0124 #define ngx_read_file_n          "pread()"
0125 #else
0126 #define ngx_read_file_n          "read()"
0127 #endif
0128 
0129 ssize_t ngx_write_file(ngx_file_t *file, u_char *buf, size_t size,
0130     off_t offset);
0131 
0132 ssize_t ngx_write_chain_to_file(ngx_file_t *file, ngx_chain_t *ce,
0133     off_t offset, ngx_pool_t *pool);
0134 
0135 
0136 #define ngx_read_fd              read
0137 #define ngx_read_fd_n            "read()"
0138 
0139 /*
0140  * we use inlined function instead of simple #define
0141  * because glibc 2.3 sets warn_unused_result attribute for write()
0142  * and in this case gcc 4.3 ignores (void) cast
0143  */
0144 static ngx_inline ssize_t
0145 ngx_write_fd(ngx_fd_t fd, void *buf, size_t n)
0146 {
0147     return write(fd, buf, n);
0148 }
0149 
0150 #define ngx_write_fd_n           "write()"
0151 
0152 
0153 #define ngx_write_console        ngx_write_fd
0154 
0155 
0156 #define ngx_linefeed(p)          *p++ = LF;
0157 #define NGX_LINEFEED_SIZE        1
0158 #define NGX_LINEFEED             "\x0a"
0159 
0160 
0161 #define ngx_rename_file(o, n)    rename((const char *) o, (const char *) n)
0162 #define ngx_rename_file_n        "rename()"
0163 
0164 
0165 #define ngx_change_file_access(n, a) chmod((const char *) n, a)
0166 #define ngx_change_file_access_n "chmod()"
0167 
0168 
0169 ngx_int_t ngx_set_file_time(u_char *name, ngx_fd_t fd, time_t s);
0170 #define ngx_set_file_time_n      "utimes()"
0171 
0172 
0173 #define ngx_file_info(file, sb)  stat((const char *) file, sb)
0174 #define ngx_file_info_n          "stat()"
0175 
0176 #define ngx_fd_info(fd, sb)      fstat(fd, sb)
0177 #define ngx_fd_info_n            "fstat()"
0178 
0179 #define ngx_link_info(file, sb)  lstat((const char *) file, sb)
0180 #define ngx_link_info_n          "lstat()"
0181 
0182 #define ngx_is_dir(sb)           (S_ISDIR((sb)->st_mode))
0183 #define ngx_is_file(sb)          (S_ISREG((sb)->st_mode))
0184 #define ngx_is_link(sb)          (S_ISLNK((sb)->st_mode))
0185 #define ngx_is_exec(sb)          (((sb)->st_mode & S_IXUSR) == S_IXUSR)
0186 #define ngx_file_access(sb)      ((sb)->st_mode & 0777)
0187 #define ngx_file_size(sb)        (sb)->st_size
0188 #define ngx_file_fs_size(sb)     ngx_max((sb)->st_size, (sb)->st_blocks * 512)
0189 #define ngx_file_mtime(sb)       (sb)->st_mtime
0190 #define ngx_file_uniq(sb)        (sb)->st_ino
0191 
0192 
0193 ngx_int_t ngx_create_file_mapping(ngx_file_mapping_t *fm);
0194 void ngx_close_file_mapping(ngx_file_mapping_t *fm);
0195 
0196 
0197 #define ngx_realpath(p, r)       (u_char *) realpath((char *) p, (char *) r)
0198 #define ngx_realpath_n           "realpath()"
0199 #define ngx_getcwd(buf, size)    (getcwd((char *) buf, size) != NULL)
0200 #define ngx_getcwd_n             "getcwd()"
0201 #define ngx_path_separator(c)    ((c) == '/')
0202 
0203 
0204 #if defined(PATH_MAX)
0205 
0206 #define NGX_HAVE_MAX_PATH        1
0207 #define NGX_MAX_PATH             PATH_MAX
0208 
0209 #else
0210 
0211 #define NGX_MAX_PATH             4096
0212 
0213 #endif
0214 
0215 
0216 #define NGX_DIR_MASK_LEN         0
0217 
0218 
0219 ngx_int_t ngx_open_dir(ngx_str_t *name, ngx_dir_t *dir);
0220 #define ngx_open_dir_n           "opendir()"
0221 
0222 
0223 #define ngx_close_dir(d)         closedir((d)->dir)
0224 #define ngx_close_dir_n          "closedir()"
0225 
0226 
0227 ngx_int_t ngx_read_dir(ngx_dir_t *dir);
0228 #define ngx_read_dir_n           "readdir()"
0229 
0230 
0231 #define ngx_create_dir(name, access) mkdir((const char *) name, access)
0232 #define ngx_create_dir_n         "mkdir()"
0233 
0234 
0235 #define ngx_delete_dir(name)     rmdir((const char *) name)
0236 #define ngx_delete_dir_n         "rmdir()"
0237 
0238 
0239 #define ngx_dir_access(a)        (a | (a & 0444) >> 2)
0240 
0241 
0242 #define ngx_de_name(dir)         ((u_char *) (dir)->de->d_name)
0243 #if (NGX_HAVE_D_NAMLEN)
0244 #define ngx_de_namelen(dir)      (dir)->de->d_namlen
0245 #else
0246 #define ngx_de_namelen(dir)      ngx_strlen((dir)->de->d_name)
0247 #endif
0248 
0249 static ngx_inline ngx_int_t
0250 ngx_de_info(u_char *name, ngx_dir_t *dir)
0251 {
0252     dir->type = 0;
0253     return stat((const char *) name, &dir->info);
0254 }
0255 
0256 #define ngx_de_info_n            "stat()"
0257 #define ngx_de_link_info(name, dir)  lstat((const char *) name, &(dir)->info)
0258 #define ngx_de_link_info_n       "lstat()"
0259 
0260 #if (NGX_HAVE_D_TYPE)
0261 
0262 /*
0263  * some file systems (e.g. XFS on Linux and CD9660 on FreeBSD)
0264  * do not set dirent.d_type
0265  */
0266 
0267 #define ngx_de_is_dir(dir)                                                   \
0268     (((dir)->type) ? ((dir)->type == DT_DIR) : (S_ISDIR((dir)->info.st_mode)))
0269 #define ngx_de_is_file(dir)                                                  \
0270     (((dir)->type) ? ((dir)->type == DT_REG) : (S_ISREG((dir)->info.st_mode)))
0271 #define ngx_de_is_link(dir)                                                  \
0272     (((dir)->type) ? ((dir)->type == DT_LNK) : (S_ISLNK((dir)->info.st_mode)))
0273 
0274 #else
0275 
0276 #define ngx_de_is_dir(dir)       (S_ISDIR((dir)->info.st_mode))
0277 #define ngx_de_is_file(dir)      (S_ISREG((dir)->info.st_mode))
0278 #define ngx_de_is_link(dir)      (S_ISLNK((dir)->info.st_mode))
0279 
0280 #endif
0281 
0282 #define ngx_de_access(dir)       (((dir)->info.st_mode) & 0777)
0283 #define ngx_de_size(dir)         (dir)->info.st_size
0284 #define ngx_de_fs_size(dir)                                                  \
0285     ngx_max((dir)->info.st_size, (dir)->info.st_blocks * 512)
0286 #define ngx_de_mtime(dir)        (dir)->info.st_mtime
0287 
0288 
0289 ngx_int_t ngx_open_glob(ngx_glob_t *gl);
0290 #define ngx_open_glob_n          "glob()"
0291 ngx_int_t ngx_read_glob(ngx_glob_t *gl, ngx_str_t *name);
0292 void ngx_close_glob(ngx_glob_t *gl);
0293 
0294 
0295 ngx_err_t ngx_trylock_fd(ngx_fd_t fd);
0296 ngx_err_t ngx_lock_fd(ngx_fd_t fd);
0297 ngx_err_t ngx_unlock_fd(ngx_fd_t fd);
0298 
0299 #define ngx_trylock_fd_n         "fcntl(F_SETLK, F_WRLCK)"
0300 #define ngx_lock_fd_n            "fcntl(F_SETLKW, F_WRLCK)"
0301 #define ngx_unlock_fd_n          "fcntl(F_SETLK, F_UNLCK)"
0302 
0303 
0304 #if (NGX_HAVE_F_READAHEAD)
0305 
0306 #define NGX_HAVE_READ_AHEAD      1
0307 
0308 #define ngx_read_ahead(fd, n)    fcntl(fd, F_READAHEAD, (int) n)
0309 #define ngx_read_ahead_n         "fcntl(fd, F_READAHEAD)"
0310 
0311 #elif (NGX_HAVE_POSIX_FADVISE)
0312 
0313 #define NGX_HAVE_READ_AHEAD      1
0314 
0315 ngx_int_t ngx_read_ahead(ngx_fd_t fd, size_t n);
0316 #define ngx_read_ahead_n         "posix_fadvise(POSIX_FADV_SEQUENTIAL)"
0317 
0318 #else
0319 
0320 #define ngx_read_ahead(fd, n)    0
0321 #define ngx_read_ahead_n         "ngx_read_ahead_n"
0322 
0323 #endif
0324 
0325 
0326 #if (NGX_HAVE_O_DIRECT)
0327 
0328 ngx_int_t ngx_directio_on(ngx_fd_t fd);
0329 #define ngx_directio_on_n        "fcntl(O_DIRECT)"
0330 
0331 ngx_int_t ngx_directio_off(ngx_fd_t fd);
0332 #define ngx_directio_off_n       "fcntl(!O_DIRECT)"
0333 
0334 #elif (NGX_HAVE_F_NOCACHE)
0335 
0336 #define ngx_directio_on(fd)      fcntl(fd, F_NOCACHE, 1)
0337 #define ngx_directio_on_n        "fcntl(F_NOCACHE, 1)"
0338 
0339 #elif (NGX_HAVE_DIRECTIO)
0340 
0341 #define ngx_directio_on(fd)      directio(fd, DIRECTIO_ON)
0342 #define ngx_directio_on_n        "directio(DIRECTIO_ON)"
0343 
0344 #else
0345 
0346 #define ngx_directio_on(fd)      0
0347 #define ngx_directio_on_n        "ngx_directio_on_n"
0348 
0349 #endif
0350 
0351 size_t ngx_fs_bsize(u_char *name);
0352 
0353 
0354 #if (NGX_HAVE_OPENAT)
0355 
0356 #define ngx_openat_file(fd, name, mode, create, access)                      \
0357     openat(fd, (const char *) name, mode|create, access)
0358 
0359 #define ngx_openat_file_n        "openat()"
0360 
0361 #define ngx_file_at_info(fd, name, sb, flag)                                 \
0362     fstatat(fd, (const char *) name, sb, flag)
0363 
0364 #define ngx_file_at_info_n       "fstatat()"
0365 
0366 #define NGX_AT_FDCWD             (ngx_fd_t) AT_FDCWD
0367 
0368 #endif
0369 
0370 
0371 #define ngx_stdout               STDOUT_FILENO
0372 #define ngx_stderr               STDERR_FILENO
0373 #define ngx_set_stderr(fd)       dup2(fd, STDERR_FILENO)
0374 #define ngx_set_stderr_n         "dup2(STDERR_FILENO)"
0375 
0376 
0377 #if (NGX_HAVE_FILE_AIO)
0378 
0379 ngx_int_t ngx_file_aio_init(ngx_file_t *file, ngx_pool_t *pool);
0380 ssize_t ngx_file_aio_read(ngx_file_t *file, u_char *buf, size_t size,
0381     off_t offset, ngx_pool_t *pool);
0382 
0383 extern ngx_uint_t  ngx_file_aio;
0384 
0385 #endif
0386 
0387 #if (NGX_THREADS)
0388 ssize_t ngx_thread_read(ngx_file_t *file, u_char *buf, size_t size,
0389     off_t offset, ngx_pool_t *pool);
0390 ssize_t ngx_thread_write_chain_to_file(ngx_file_t *file, ngx_chain_t *cl,
0391     off_t offset, ngx_pool_t *pool);
0392 #endif
0393 
0394 
0395 #endif /* _NGX_FILES_H_INCLUDED_ */