summaryrefslogtreecommitdiffhomepage
path: root/src/nxt_file.h
blob: 5216fb1ce8fbc26cbffeb0d4e4ce58933d588a05 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193

/*
 * Copyright (C) Igor Sysoev
 * Copyright (C) NGINX, Inc.
 */

#ifndef _NXT_UNIX_FILE_H_INCLUDED_
#define _NXT_UNIX_FILE_H_INCLUDED_


typedef int                         nxt_fd_t;

#define NXT_FILE_INVALID            -1

typedef nxt_uint_t                  nxt_file_access_t;
typedef struct stat                 nxt_file_info_t;


#define NXT_FILE_SYSTEM_NAME_UTF8   1

typedef u_char                      nxt_file_name_t;


typedef struct {
    size_t                          len;
    nxt_file_name_t                 *start;
} nxt_file_name_str_t;


#define                                                                       \
nxt_file_name_str_set(file_name, mem_pool, name)                              \
    ((file_name) = (nxt_file_name_t *) (name), NXT_OK)


#define                                                                       \
nxt_file_name_alloc(mem_pool, len)                                            \
    nxt_mp_nget(mem_pool, len)


#define                                                                       \
nxt_file_name_copy(dst, src, len)                                             \
    nxt_cpymem(dst, src, len)


#define                                                                       \
nxt_file_name_add(dst, src, len)                                              \
    nxt_cpymem(dst, src, len)


#if (NXT_HAVE_CASELESS_FILESYSTEM)

/* MacOSX, Cygwin. */

#define                                                                       \
nxt_file_name_eq(fn1, fn2)                                                    \
    (nxt_strcasecmp(fn1, fn2) == 0)

#else

#define                                                                       \
nxt_file_name_eq(fn1, fn2)                                                    \
    (nxt_strcmp(fn1, fn2) == 0)

#endif


#define                                                                       \
nxt_file_name_is_absolute(name)                                               \
    (name[0] == '/')


#define NXT_MAX_PATH_LEN            MAXPATHLEN


typedef enum {
    NXT_FILE_UNKNOWN = 0,
    NXT_FILE_REGULAR,
    NXT_FILE_DIRECTORY,
} nxt_file_type_t;


typedef struct {
    nxt_file_name_t                 *name;

    /* Both are int's. */
    nxt_fd_t                        fd;
    nxt_err_t                       error;

#define NXT_FILE_ACCESSED_LONG_AGO  0xffff
    /*
     * Number of seconds ago the file content was last
     * read.  The maximum value is about 18 hours.
     */
    uint16_t                        accessed;

    uint8_t                         type;       /* nxt_file_type_t */

    /*
     * Log open() file error with given log level if it is non zero.
     * Note that zero log level is NXT_LOG_ALERT.
     */
    uint8_t                         log_level;

    nxt_time_t                      mtime;
    nxt_off_t                       size;
} nxt_file_t;


NXT_EXPORT nxt_int_t nxt_file_open(nxt_task_t *task, nxt_file_t *file,
    nxt_uint_t mode, nxt_uint_t create, nxt_file_access_t access);


/* The file open access modes. */
#define NXT_FILE_RDONLY             O_RDONLY
#define NXT_FILE_WRONLY             O_WRONLY
#define NXT_FILE_RDWR               O_RDWR
#define NXT_FILE_APPEND             (O_WRONLY | O_APPEND)

/* The file creation modes. */
#define NXT_FILE_CREATE_OR_OPEN     O_CREAT
#define NXT_FILE_OPEN               0
#define NXT_FILE_TRUNCATE           (O_CREAT | O_TRUNC)

/* The file access rights. */
#define NXT_FILE_DEFAULT_ACCESS     0644
#define NXT_FILE_OWNER_ACCESS       0600


NXT_EXPORT void nxt_file_close(nxt_task_t *task, nxt_file_t *file);
NXT_EXPORT ssize_t nxt_file_write(nxt_file_t *file, const u_char *buf,
    size_t size, nxt_off_t offset);
NXT_EXPORT ssize_t nxt_file_read(nxt_file_t *file, u_char *buf, size_t size,
    nxt_off_t offset);
NXT_EXPORT void nxt_file_read_ahead(nxt_file_t *file, nxt_off_t offset,
    size_t size);
NXT_EXPORT nxt_int_t nxt_file_info(nxt_file_t *file, nxt_file_info_t *fi);


#define                                                                       \
nxt_is_dir(fi)                                                                \
    (S_ISDIR((fi)->st_mode))

#define                                                                       \
nxt_is_file(fi)                                                               \
    (S_ISREG((fi)->st_mode))

#define                                                                       \
nxt_file_size(fi)                                                             \
    (fi)->st_size

#define                                                                       \
nxt_file_mtime(fi)                                                            \
    (fi)->st_mtime


NXT_EXPORT nxt_int_t nxt_file_delete(nxt_file_name_t *name);
NXT_EXPORT nxt_int_t nxt_file_set_access(nxt_file_name_t *name,
    nxt_file_access_t access);
NXT_EXPORT nxt_int_t nxt_file_rename(nxt_file_name_t *old_name,
    nxt_file_name_t *new_name);

NXT_EXPORT nxt_int_t nxt_fd_nonblocking(nxt_task_t *task, nxt_fd_t fd);
NXT_EXPORT nxt_int_t nxt_fd_blocking(nxt_task_t *task, nxt_fd_t fd);
NXT_EXPORT ssize_t nxt_fd_write(nxt_fd_t fd, u_char *buf, size_t size);
NXT_EXPORT ssize_t nxt_fd_read(nxt_fd_t fd, u_char *buf, size_t size);
NXT_EXPORT void nxt_fd_close(nxt_fd_t fd);

NXT_EXPORT nxt_int_t nxt_file_redirect(nxt_file_t *file, nxt_fd_t fd);
NXT_EXPORT nxt_int_t nxt_file_stderr(nxt_file_t *file);
NXT_EXPORT nxt_int_t nxt_stderr_start(void);


#define nxt_stdout  STDOUT_FILENO
#define nxt_stderr  STDERR_FILENO


#define                                                                       \
nxt_write_console(fd, buf, size)                                              \
    write(fd, buf, size)

#define                                                                       \
nxt_write_syslog(priority, message)                                           \
    syslog(priority, "%s", message)


NXT_EXPORT nxt_int_t nxt_pipe_create(nxt_task_t *task, nxt_fd_t *pp,
    nxt_bool_t nbread, nxt_bool_t nbwrite);
NXT_EXPORT void nxt_pipe_close(nxt_task_t *task, nxt_fd_t *pp);

NXT_EXPORT size_t nxt_dir_current(char *buf, size_t len);


#endif /* _NXT_UNIX_FILE_H_INCLUDED_ */