Annotation of embedaddon/nginx/src/os/unix/ngx_files.h, revision 1.1.1.1

1.1       misho       1: 
                      2: /*
                      3:  * Copyright (C) Igor Sysoev
                      4:  * Copyright (C) Nginx, Inc.
                      5:  */
                      6: 
                      7: 
                      8: #ifndef _NGX_FILES_H_INCLUDED_
                      9: #define _NGX_FILES_H_INCLUDED_
                     10: 
                     11: 
                     12: #include <ngx_config.h>
                     13: #include <ngx_core.h>
                     14: 
                     15: 
                     16: typedef int                      ngx_fd_t;
                     17: typedef struct stat              ngx_file_info_t;
                     18: typedef ino_t                    ngx_file_uniq_t;
                     19: 
                     20: 
                     21: typedef struct {
                     22:     u_char                      *name;
                     23:     size_t                       size;
                     24:     void                        *addr;
                     25:     ngx_fd_t                     fd;
                     26:     ngx_log_t                   *log;
                     27: } ngx_file_mapping_t;
                     28: 
                     29: 
                     30: typedef struct {
                     31:     DIR                         *dir;
                     32:     struct dirent               *de;
                     33:     struct stat                  info;
                     34: 
                     35:     unsigned                     type:8;
                     36:     unsigned                     valid_info:1;
                     37: } ngx_dir_t;
                     38: 
                     39: 
                     40: typedef struct {
                     41:     size_t                       n;
                     42:     glob_t                       pglob;
                     43:     u_char                      *pattern;
                     44:     ngx_log_t                   *log;
                     45:     ngx_uint_t                   test;
                     46: } ngx_glob_t;
                     47: 
                     48: 
                     49: #define NGX_INVALID_FILE         -1
                     50: #define NGX_FILE_ERROR           -1
                     51: 
                     52: 
                     53: 
                     54: #ifdef __CYGWIN__
                     55: 
                     56: #define NGX_HAVE_CASELESS_FILESYSTEM  1
                     57: 
                     58: #define ngx_open_file(name, mode, create, access)                            \
                     59:     open((const char *) name, mode|create|O_BINARY, access)
                     60: 
                     61: #else
                     62: 
                     63: #define ngx_open_file(name, mode, create, access)                            \
                     64:     open((const char *) name, mode|create, access)
                     65: 
                     66: #endif
                     67: 
                     68: #define ngx_open_file_n          "open()"
                     69: 
                     70: #define NGX_FILE_RDONLY          O_RDONLY
                     71: #define NGX_FILE_WRONLY          O_WRONLY
                     72: #define NGX_FILE_RDWR            O_RDWR
                     73: #define NGX_FILE_CREATE_OR_OPEN  O_CREAT
                     74: #define NGX_FILE_OPEN            0
                     75: #define NGX_FILE_TRUNCATE        O_CREAT|O_TRUNC
                     76: #define NGX_FILE_APPEND          O_WRONLY|O_APPEND
                     77: #define NGX_FILE_NONBLOCK        O_NONBLOCK
                     78: 
                     79: #if (NGX_HAVE_OPENAT)
                     80: #define NGX_FILE_NOFOLLOW        O_NOFOLLOW
                     81: 
                     82: #if defined(O_DIRECTORY)
                     83: #define NGX_FILE_DIRECTORY       O_DIRECTORY
                     84: #else
                     85: #define NGX_FILE_DIRECTORY       0
                     86: #endif
                     87: 
                     88: #if defined(O_SEARCH)
                     89: #define NGX_FILE_SEARCH          O_SEARCH|NGX_FILE_DIRECTORY
                     90: 
                     91: #elif defined(O_EXEC)
                     92: #define NGX_FILE_SEARCH          O_EXEC|NGX_FILE_DIRECTORY
                     93: 
                     94: #else
                     95: #define NGX_FILE_SEARCH          O_RDONLY|NGX_FILE_DIRECTORY
                     96: #endif
                     97: 
                     98: #endif /* NGX_HAVE_OPENAT */
                     99: 
                    100: #define NGX_FILE_DEFAULT_ACCESS  0644
                    101: #define NGX_FILE_OWNER_ACCESS    0600
                    102: 
                    103: 
                    104: #define ngx_close_file           close
                    105: #define ngx_close_file_n         "close()"
                    106: 
                    107: 
                    108: #define ngx_delete_file(name)    unlink((const char *) name)
                    109: #define ngx_delete_file_n        "unlink()"
                    110: 
                    111: 
                    112: ngx_fd_t ngx_open_tempfile(u_char *name, ngx_uint_t persistent,
                    113:     ngx_uint_t access);
                    114: #define ngx_open_tempfile_n      "open()"
                    115: 
                    116: 
                    117: ssize_t ngx_read_file(ngx_file_t *file, u_char *buf, size_t size, off_t offset);
                    118: #if (NGX_HAVE_PREAD)
                    119: #define ngx_read_file_n          "pread()"
                    120: #else
                    121: #define ngx_read_file_n          "read()"
                    122: #endif
                    123: 
                    124: ssize_t ngx_write_file(ngx_file_t *file, u_char *buf, size_t size,
                    125:     off_t offset);
                    126: 
                    127: ssize_t ngx_write_chain_to_file(ngx_file_t *file, ngx_chain_t *ce,
                    128:     off_t offset, ngx_pool_t *pool);
                    129: 
                    130: 
                    131: #define ngx_read_fd              read
                    132: #define ngx_read_fd_n            "read()"
                    133: 
                    134: /*
                    135:  * we use inlined function instead of simple #define
                    136:  * because glibc 2.3 sets warn_unused_result attribute for write()
                    137:  * and in this case gcc 4.3 ignores (void) cast
                    138:  */
                    139: static ngx_inline ssize_t
                    140: ngx_write_fd(ngx_fd_t fd, void *buf, size_t n)
                    141: {
                    142:     return write(fd, buf, n);
                    143: }
                    144: 
                    145: #define ngx_write_fd_n           "write()"
                    146: 
                    147: 
                    148: #define ngx_write_console        ngx_write_fd
                    149: 
                    150: 
                    151: #define ngx_linefeed(p)          *p++ = LF;
                    152: #define NGX_LINEFEED_SIZE        1
                    153: #define NGX_LINEFEED             "\x0a"
                    154: 
                    155: 
                    156: #define ngx_rename_file(o, n)    rename((const char *) o, (const char *) n)
                    157: #define ngx_rename_file_n        "rename()"
                    158: 
                    159: 
                    160: #define ngx_change_file_access(n, a) chmod((const char *) n, a)
                    161: #define ngx_change_file_access_n "chmod()"
                    162: 
                    163: 
                    164: ngx_int_t ngx_set_file_time(u_char *name, ngx_fd_t fd, time_t s);
                    165: #define ngx_set_file_time_n      "utimes()"
                    166: 
                    167: 
                    168: #define ngx_file_info(file, sb)  stat((const char *) file, sb)
                    169: #define ngx_file_info_n          "stat()"
                    170: 
                    171: #define ngx_fd_info(fd, sb)      fstat(fd, sb)
                    172: #define ngx_fd_info_n            "fstat()"
                    173: 
                    174: #define ngx_link_info(file, sb)  lstat((const char *) file, sb)
                    175: #define ngx_link_info_n          "lstat()"
                    176: 
                    177: #define ngx_is_dir(sb)           (S_ISDIR((sb)->st_mode))
                    178: #define ngx_is_file(sb)          (S_ISREG((sb)->st_mode))
                    179: #define ngx_is_link(sb)          (S_ISLNK((sb)->st_mode))
                    180: #define ngx_is_exec(sb)          (((sb)->st_mode & S_IXUSR) == S_IXUSR)
                    181: #define ngx_file_access(sb)      ((sb)->st_mode & 0777)
                    182: #define ngx_file_size(sb)        (sb)->st_size
                    183: #define ngx_file_fs_size(sb)     ngx_max((sb)->st_size, (sb)->st_blocks * 512)
                    184: #define ngx_file_mtime(sb)       (sb)->st_mtime
                    185: #define ngx_file_uniq(sb)        (sb)->st_ino
                    186: 
                    187: 
                    188: ngx_int_t ngx_create_file_mapping(ngx_file_mapping_t *fm);
                    189: void ngx_close_file_mapping(ngx_file_mapping_t *fm);
                    190: 
                    191: 
                    192: #if (NGX_HAVE_CASELESS_FILESYSTEM)
                    193: 
                    194: #define ngx_filename_cmp(s1, s2, n)  strncasecmp((char *) s1, (char *) s2, n)
                    195: 
                    196: #else
                    197: 
                    198: #define ngx_filename_cmp         ngx_memcmp
                    199: 
                    200: #endif
                    201: 
                    202: 
                    203: #define ngx_realpath(p, r)       (u_char *) realpath((char *) p, (char *) r)
                    204: #define ngx_realpath_n           "realpath()"
                    205: #define ngx_getcwd(buf, size)    (getcwd((char *) buf, size) != NULL)
                    206: #define ngx_getcwd_n             "getcwd()"
                    207: #define ngx_path_separator(c)    ((c) == '/')
                    208: 
                    209: 
                    210: #if defined(PATH_MAX)
                    211: 
                    212: #define NGX_HAVE_MAX_PATH        1
                    213: #define NGX_MAX_PATH             PATH_MAX
                    214: 
                    215: #else
                    216: 
                    217: #define NGX_MAX_PATH             4096
                    218: 
                    219: #endif
                    220: 
                    221: 
                    222: #define NGX_DIR_MASK_LEN         0
                    223: 
                    224: 
                    225: ngx_int_t ngx_open_dir(ngx_str_t *name, ngx_dir_t *dir);
                    226: #define ngx_open_dir_n           "opendir()"
                    227: 
                    228: 
                    229: #define ngx_close_dir(d)         closedir((d)->dir)
                    230: #define ngx_close_dir_n          "closedir()"
                    231: 
                    232: 
                    233: ngx_int_t ngx_read_dir(ngx_dir_t *dir);
                    234: #define ngx_read_dir_n           "readdir()"
                    235: 
                    236: 
                    237: #define ngx_create_dir(name, access) mkdir((const char *) name, access)
                    238: #define ngx_create_dir_n         "mkdir()"
                    239: 
                    240: 
                    241: #define ngx_delete_dir(name)     rmdir((const char *) name)
                    242: #define ngx_delete_dir_n         "rmdir()"
                    243: 
                    244: 
                    245: #define ngx_dir_access(a)        (a | (a & 0444) >> 2)
                    246: 
                    247: 
                    248: #define ngx_de_name(dir)         ((u_char *) (dir)->de->d_name)
                    249: #if (NGX_HAVE_D_NAMLEN)
                    250: #define ngx_de_namelen(dir)      (dir)->de->d_namlen
                    251: #else
                    252: #define ngx_de_namelen(dir)      ngx_strlen((dir)->de->d_name)
                    253: #endif
                    254: 
                    255: static ngx_inline ngx_int_t
                    256: ngx_de_info(u_char *name, ngx_dir_t *dir)
                    257: {
                    258:     dir->type = 0;
                    259:     return stat((const char *) name, &dir->info);
                    260: }
                    261: 
                    262: #define ngx_de_info_n            "stat()"
                    263: #define ngx_de_link_info(name, dir)  lstat((const char *) name, &(dir)->info)
                    264: #define ngx_de_link_info_n       "lstat()"
                    265: 
                    266: #if (NGX_HAVE_D_TYPE)
                    267: 
                    268: /*
                    269:  * some file systems (e.g. XFS on Linux and CD9660 on FreeBSD)
                    270:  * do not set dirent.d_type
                    271:  */
                    272: 
                    273: #define ngx_de_is_dir(dir)                                                   \
                    274:     (((dir)->type) ? ((dir)->type == DT_DIR) : (S_ISDIR((dir)->info.st_mode)))
                    275: #define ngx_de_is_file(dir)                                                  \
                    276:     (((dir)->type) ? ((dir)->type == DT_REG) : (S_ISREG((dir)->info.st_mode)))
                    277: #define ngx_de_is_link(dir)                                                  \
                    278:     (((dir)->type) ? ((dir)->type == DT_LNK) : (S_ISLNK((dir)->info.st_mode)))
                    279: 
                    280: #else
                    281: 
                    282: #define ngx_de_is_dir(dir)       (S_ISDIR((dir)->info.st_mode))
                    283: #define ngx_de_is_file(dir)      (S_ISREG((dir)->info.st_mode))
                    284: #define ngx_de_is_link(dir)      (S_ISLNK((dir)->info.st_mode))
                    285: 
                    286: #endif
                    287: 
                    288: #define ngx_de_access(dir)       (((dir)->info.st_mode) & 0777)
                    289: #define ngx_de_size(dir)         (dir)->info.st_size
                    290: #define ngx_de_fs_size(dir)                                                  \
                    291:     ngx_max((dir)->info.st_size, (dir)->info.st_blocks * 512)
                    292: #define ngx_de_mtime(dir)        (dir)->info.st_mtime
                    293: 
                    294: 
                    295: ngx_int_t ngx_open_glob(ngx_glob_t *gl);
                    296: #define ngx_open_glob_n          "glob()"
                    297: ngx_int_t ngx_read_glob(ngx_glob_t *gl, ngx_str_t *name);
                    298: void ngx_close_glob(ngx_glob_t *gl);
                    299: 
                    300: 
                    301: ngx_err_t ngx_trylock_fd(ngx_fd_t fd);
                    302: ngx_err_t ngx_lock_fd(ngx_fd_t fd);
                    303: ngx_err_t ngx_unlock_fd(ngx_fd_t fd);
                    304: 
                    305: #define ngx_trylock_fd_n         "fcntl(F_SETLK, F_WRLCK)"
                    306: #define ngx_lock_fd_n            "fcntl(F_SETLKW, F_WRLCK)"
                    307: #define ngx_unlock_fd_n          "fcntl(F_SETLK, F_UNLCK)"
                    308: 
                    309: 
                    310: #if (NGX_HAVE_F_READAHEAD)
                    311: 
                    312: #define NGX_HAVE_READ_AHEAD      1
                    313: 
                    314: #define ngx_read_ahead(fd, n)    fcntl(fd, F_READAHEAD, (int) n)
                    315: #define ngx_read_ahead_n         "fcntl(fd, F_READAHEAD)"
                    316: 
                    317: #elif (NGX_HAVE_POSIX_FADVISE)
                    318: 
                    319: #define NGX_HAVE_READ_AHEAD      1
                    320: 
                    321: ngx_int_t ngx_read_ahead(ngx_fd_t fd, size_t n);
                    322: #define ngx_read_ahead_n         "posix_fadvise(POSIX_FADV_SEQUENTIAL)"
                    323: 
                    324: #else
                    325: 
                    326: #define ngx_read_ahead(fd, n)    0
                    327: #define ngx_read_ahead_n         "ngx_read_ahead_n"
                    328: 
                    329: #endif
                    330: 
                    331: 
                    332: #if (NGX_HAVE_O_DIRECT)
                    333: 
                    334: ngx_int_t ngx_directio_on(ngx_fd_t fd);
                    335: #define ngx_directio_on_n        "fcntl(O_DIRECT)"
                    336: 
                    337: ngx_int_t ngx_directio_off(ngx_fd_t fd);
                    338: #define ngx_directio_off_n       "fcntl(!O_DIRECT)"
                    339: 
                    340: #elif (NGX_HAVE_F_NOCACHE)
                    341: 
                    342: #define ngx_directio_on(fd)      fcntl(fd, F_NOCACHE, 1)
                    343: #define ngx_directio_on_n        "fcntl(F_NOCACHE, 1)"
                    344: 
                    345: #elif (NGX_HAVE_DIRECTIO)
                    346: 
                    347: #define ngx_directio_on(fd)      directio(fd, DIRECTIO_ON)
                    348: #define ngx_directio_on_n        "directio(DIRECTIO_ON)"
                    349: 
                    350: #else
                    351: 
                    352: #define ngx_directio_on(fd)      0
                    353: #define ngx_directio_on_n        "ngx_directio_on_n"
                    354: 
                    355: #endif
                    356: 
                    357: size_t ngx_fs_bsize(u_char *name);
                    358: 
                    359: 
                    360: #if (NGX_HAVE_OPENAT)
                    361: 
                    362: #define ngx_openat_file(fd, name, mode, create, access)                      \
                    363:     openat(fd, (const char *) name, mode|create, access)
                    364: 
                    365: #define ngx_openat_file_n        "openat()"
                    366: 
                    367: #define ngx_file_at_info(fd, name, sb, flag)                                 \
                    368:     fstatat(fd, (const char *) name, sb, flag)
                    369: 
                    370: #define ngx_file_at_info_n       "fstatat()"
                    371: 
                    372: #define NGX_AT_FDCWD             (ngx_fd_t) AT_FDCWD
                    373: 
                    374: #endif
                    375: 
                    376: 
                    377: #define ngx_stderr               STDERR_FILENO
                    378: #define ngx_set_stderr(fd)       dup2(fd, STDERR_FILENO)
                    379: #define ngx_set_stderr_n         "dup2(STDERR_FILENO)"
                    380: 
                    381: 
                    382: #if (NGX_HAVE_FILE_AIO)
                    383: 
                    384: ssize_t ngx_file_aio_read(ngx_file_t *file, u_char *buf, size_t size,
                    385:     off_t offset, ngx_pool_t *pool);
                    386: 
                    387: extern ngx_uint_t  ngx_file_aio;
                    388: 
                    389: #endif
                    390: 
                    391: 
                    392: #endif /* _NGX_FILES_H_INCLUDED_ */

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>