0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9
|
|
10
|
44
|
11 static ngx_atomic_uint_t ngx_temp_number;
|
|
12 static ngx_atomic_uint_t ngx_random;
|
0
|
13
|
|
14
|
42
|
15 ssize_t
|
|
16 ngx_write_chain_to_temp_file(ngx_temp_file_t *tf, ngx_chain_t *chain)
|
0
|
17 {
|
16
|
18 ngx_int_t rc;
|
0
|
19
|
|
20 if (tf->file.fd == NGX_INVALID_FILE) {
|
|
21 rc = ngx_create_temp_file(&tf->file, tf->path, tf->pool,
|
182
|
22 tf->persistent, tf->mode);
|
0
|
23
|
|
24 if (rc == NGX_ERROR || rc == NGX_AGAIN) {
|
|
25 return rc;
|
|
26 }
|
|
27
|
186
|
28 if (tf->log_level == NGX_LOG_NOTICE) {
|
|
29 ngx_log_error(NGX_LOG_NOTICE, tf->file.log, 0, tf->warn);
|
|
30
|
|
31 } else if (tf->log_level == NGX_LOG_WARN) {
|
|
32 ngx_log_error(NGX_LOG_WARN, tf->file.log, 0, "%s %V",
|
|
33 tf->warn, &tf->file.name);
|
0
|
34 }
|
|
35 }
|
|
36
|
|
37 return ngx_write_chain_to_file(&tf->file, chain, tf->offset, tf->pool);
|
|
38 }
|
|
39
|
|
40
|
42
|
41 ngx_int_t
|
|
42 ngx_create_temp_file(ngx_file_t *file, ngx_path_t *path, ngx_pool_t *pool,
|
182
|
43 ngx_uint_t persistent, ngx_uint_t mode)
|
0
|
44 {
|
58
|
45 ngx_err_t err;
|
|
46 ngx_atomic_uint_t n;
|
112
|
47 ngx_pool_cleanup_t *cln;
|
|
48 ngx_pool_cleanup_file_t *clnf;
|
0
|
49
|
42
|
50 file->name.len = path->name.len + 1 + path->len + NGX_ATOMIC_T_LEN;
|
0
|
51
|
50
|
52 file->name.data = ngx_palloc(pool, file->name.len + 1);
|
|
53 if (file->name.data == NULL) {
|
16
|
54 return NGX_ERROR;
|
|
55 }
|
0
|
56
|
|
57 #if 0
|
|
58 for (i = 0; i < file->name.len; i++) {
|
|
59 file->name.data[i] = 'X';
|
|
60 }
|
|
61 #endif
|
|
62
|
|
63 ngx_memcpy(file->name.data, path->name.data, path->name.len);
|
|
64
|
42
|
65 n = ngx_next_temp_number(0);
|
0
|
66
|
|
67 for ( ;; ) {
|
66
|
68 (void) ngx_sprintf(file->name.data + path->name.len + 1 + path->len,
|
|
69 "%0muA%Z", n);
|
0
|
70
|
|
71 ngx_create_hashed_filename(file, path);
|
|
72
|
112
|
73 cln = ngx_pool_cleanup_add(pool, sizeof(ngx_pool_cleanup_file_t));
|
|
74 if (cln == NULL) {
|
|
75 return NGX_ERROR;
|
|
76 }
|
|
77
|
182
|
78 file->fd = ngx_open_tempfile(file->name.data, persistent, mode);
|
0
|
79
|
|
80 ngx_log_debug1(NGX_LOG_DEBUG_CORE, file->log, 0,
|
|
81 "temp fd:%d", file->fd);
|
|
82
|
|
83 if (file->fd != NGX_INVALID_FILE) {
|
112
|
84
|
|
85 cln->handler = ngx_pool_cleanup_file;
|
|
86 clnf = cln->data;
|
58
|
87
|
112
|
88 clnf->fd = file->fd;
|
|
89 clnf->name = file->name.data;
|
|
90 clnf->log = pool->log;
|
58
|
91
|
0
|
92 return NGX_OK;
|
|
93 }
|
|
94
|
|
95 err = ngx_errno;
|
|
96
|
|
97 if (err == NGX_EEXIST) {
|
42
|
98 n = ngx_next_temp_number(1);
|
0
|
99 continue;
|
|
100 }
|
|
101
|
|
102 if ((path->level[0] == 0)
|
|
103 || (err != NGX_ENOENT
|
10
|
104 #if (NGX_WIN32)
|
0
|
105 && err != NGX_ENOTDIR
|
|
106 #endif
|
16
|
107 ))
|
|
108 {
|
0
|
109 ngx_log_error(NGX_LOG_CRIT, file->log, err,
|
|
110 ngx_open_tempfile_n " \"%s\" failed",
|
|
111 file->name.data);
|
|
112 return NGX_ERROR;
|
|
113 }
|
|
114
|
|
115 if (ngx_create_path(file, path) == NGX_ERROR) {
|
|
116 return NGX_ERROR;
|
|
117 }
|
|
118 }
|
|
119 }
|
|
120
|
|
121
|
42
|
122 void
|
|
123 ngx_create_hashed_filename(ngx_file_t *file, ngx_path_t *path)
|
0
|
124 {
|
66
|
125 size_t name, pos, level;
|
|
126 ngx_uint_t i;
|
0
|
127
|
|
128 name = file->name.len;
|
|
129 pos = path->name.len + 1;
|
|
130
|
|
131 file->name.data[path->name.len + path->len] = '/';
|
|
132
|
|
133 for (i = 0; i < 3; i++) {
|
|
134 level = path->level[i];
|
|
135
|
|
136 if (level == 0) {
|
|
137 break;
|
|
138 }
|
|
139
|
|
140 name -= level;
|
|
141 file->name.data[pos - 1] = '/';
|
|
142 ngx_memcpy(&file->name.data[pos], &file->name.data[name], level);
|
|
143 pos += level + 1;
|
|
144 }
|
|
145
|
|
146 ngx_log_debug1(NGX_LOG_DEBUG_CORE, file->log, 0,
|
|
147 "hashed path: %s", file->name.data);
|
|
148 }
|
|
149
|
|
150
|
42
|
151 ngx_int_t
|
|
152 ngx_create_path(ngx_file_t *file, ngx_path_t *path)
|
0
|
153 {
|
66
|
154 size_t pos;
|
|
155 ngx_err_t err;
|
|
156 ngx_uint_t i;
|
0
|
157
|
|
158 pos = path->name.len;
|
|
159
|
|
160 for (i = 0; i < 3; i++) {
|
|
161 if (path->level[i] == 0) {
|
|
162 break;
|
|
163 }
|
|
164
|
|
165 pos += path->level[i] + 1;
|
|
166
|
|
167 file->name.data[pos] = '\0';
|
|
168
|
|
169 ngx_log_debug1(NGX_LOG_DEBUG_CORE, file->log, 0,
|
|
170 "temp file: \"%s\"", file->name.data);
|
|
171
|
|
172 if (ngx_create_dir(file->name.data) == NGX_FILE_ERROR) {
|
|
173 err = ngx_errno;
|
|
174 if (err != NGX_EEXIST) {
|
|
175 ngx_log_error(NGX_LOG_CRIT, file->log, err,
|
|
176 ngx_create_dir_n " \"%s\" failed",
|
|
177 file->name.data);
|
|
178 return NGX_ERROR;
|
|
179 }
|
|
180 }
|
|
181
|
|
182 file->name.data[pos] = '/';
|
|
183 }
|
|
184
|
|
185 return NGX_OK;
|
|
186 }
|
|
187
|
|
188
|
186
|
189 ngx_err_t
|
|
190 ngx_create_full_path(u_char *dir)
|
|
191 {
|
|
192 u_char *p, ch;
|
|
193 ngx_err_t err;
|
|
194
|
|
195 for (p = dir + 1; *p; p++) {
|
|
196 ch = *p;
|
|
197
|
|
198 if (ch != '/') {
|
|
199 continue;
|
|
200 }
|
|
201
|
|
202 *p = '\0';
|
|
203
|
|
204 if (ngx_create_dir(dir) == NGX_FILE_ERROR) {
|
|
205 err = ngx_errno;
|
|
206 if (err != NGX_EEXIST) {
|
|
207 return err;
|
|
208 }
|
|
209 }
|
|
210
|
|
211 *p = '/';
|
|
212 }
|
|
213
|
|
214 return 0;
|
|
215 }
|
|
216
|
|
217
|
42
|
218 void
|
48
|
219 ngx_init_temp_number(void)
|
0
|
220 {
|
42
|
221 ngx_temp_number = 0;
|
|
222 ngx_random = 123456;
|
0
|
223 }
|
|
224
|
|
225
|
44
|
226 ngx_atomic_uint_t
|
42
|
227 ngx_next_temp_number(ngx_uint_t collision)
|
0
|
228 {
|
|
229 if (collision) {
|
|
230 ngx_temp_number += ngx_random;
|
|
231 }
|
|
232
|
|
233 return ngx_temp_number++;
|
|
234 }
|
|
235
|
|
236
|
42
|
237 char *
|
|
238 ngx_conf_set_path_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
0
|
239 {
|
|
240 char *p = conf;
|
|
241
|
16
|
242 ssize_t level;
|
178
|
243 ngx_str_t *value;
|
0
|
244 ngx_uint_t i, n;
|
28
|
245 ngx_path_t *path, **slot;
|
0
|
246
|
16
|
247 slot = (ngx_path_t **) (p + cmd->offset);
|
0
|
248
|
16
|
249 if (*slot) {
|
0
|
250 return "is duplicate";
|
|
251 }
|
|
252
|
50
|
253 path = ngx_pcalloc(cf->pool, sizeof(ngx_path_t));
|
|
254 if (path == NULL) {
|
16
|
255 return NGX_CONF_ERROR;
|
|
256 }
|
0
|
257
|
16
|
258 value = cf->args->elts;
|
0
|
259
|
|
260 path->name = value[1];
|
178
|
261
|
210
|
262 if (path->name.data[path->name.len - 1] == '/') {
|
|
263 path->name.len--;
|
|
264 }
|
|
265
|
178
|
266 if (ngx_conf_full_name(cf->cycle, &path->name) == NGX_ERROR) {
|
|
267 return NULL;
|
|
268 }
|
|
269
|
0
|
270 path->len = 0;
|
28
|
271 path->cleaner = (ngx_gc_handler_pt) cmd->post;
|
16
|
272 path->conf_file = cf->conf_file->file.name.data;
|
|
273 path->line = cf->conf_file->line;
|
0
|
274
|
|
275 for (i = 0, n = 2; n < cf->args->nelts; i++, n++) {
|
|
276 level = ngx_atoi(value[n].data, value[n].len);
|
|
277 if (level == NGX_ERROR || level == 0) {
|
|
278 return "invalid value";
|
|
279 }
|
|
280
|
|
281 path->level[i] = level;
|
|
282 path->len += level + 1;
|
|
283 }
|
|
284
|
|
285 while (i < 3) {
|
|
286 path->level[i++] = 0;
|
|
287 }
|
|
288
|
28
|
289 *slot = path;
|
16
|
290
|
28
|
291 if (ngx_add_path(cf, slot) == NGX_ERROR) {
|
|
292 return NGX_CONF_ERROR;
|
|
293 }
|
|
294
|
|
295 return NGX_CONF_OK;
|
|
296 }
|
|
297
|
|
298
|
42
|
299 ngx_int_t
|
|
300 ngx_add_path(ngx_conf_t *cf, ngx_path_t **slot)
|
28
|
301 {
|
|
302 ngx_uint_t i, n;
|
|
303 ngx_path_t *path, **p;
|
|
304
|
|
305 path = *slot;
|
|
306
|
|
307 p = cf->cycle->pathes.elts;
|
16
|
308 for (i = 0; i < cf->cycle->pathes.nelts; i++) {
|
28
|
309 if (p[i]->name.len == path->name.len
|
|
310 && ngx_strcmp(p[i]->name.data, path->name.data) == 0)
|
16
|
311 {
|
|
312 for (n = 0; n < 3; n++) {
|
28
|
313 if (p[i]->level[n] != path->level[n]) {
|
|
314 if (path->conf_file == NULL) {
|
30
|
315 if (p[i]->conf_file == NULL) {
|
|
316 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
|
|
317 "the default path name \"%V\" has "
|
|
318 "the same name as another default path, "
|
|
319 "but the different levels, you need to "
|
|
320 "redefine one of them in http section",
|
|
321 &p[i]->name);
|
|
322 return NGX_ERROR;
|
|
323 }
|
|
324
|
28
|
325 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
|
|
326 "the path name \"%V\" in %s:%ui has "
|
30
|
327 "the same name as default path, but "
|
28
|
328 "the different levels, you need to "
|
|
329 "define default path in http section",
|
|
330 &p[i]->name, p[i]->conf_file, p[i]->line);
|
|
331 return NGX_ERROR;
|
|
332 }
|
|
333
|
16
|
334 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
28
|
335 "the same path name \"%V\" in %s:%ui "
|
|
336 "has the different levels than",
|
|
337 &p[i]->name, p[i]->conf_file, p[i]->line);
|
|
338 return NGX_ERROR;
|
16
|
339 }
|
|
340
|
28
|
341 if (p[i]->level[n] == 0) {
|
16
|
342 break;
|
|
343 }
|
|
344 }
|
|
345
|
28
|
346 *slot = p[i];
|
16
|
347
|
28
|
348 return NGX_OK;
|
16
|
349 }
|
|
350 }
|
|
351
|
50
|
352 p = ngx_array_push(&cf->cycle->pathes);
|
|
353 if (p == NULL) {
|
28
|
354 return NGX_ERROR;
|
16
|
355 }
|
|
356
|
28
|
357 *p = path;
|
0
|
358
|
28
|
359 return NGX_OK;
|
0
|
360 }
|
16
|
361
|
|
362
|
42
|
363 ngx_int_t
|
|
364 ngx_create_pathes(ngx_cycle_t *cycle, ngx_uid_t user)
|
16
|
365 {
|
|
366 ngx_err_t err;
|
|
367 ngx_uint_t i;
|
|
368 ngx_path_t **path;
|
|
369 #if !(NGX_WIN32)
|
|
370 ngx_file_info_t fi;
|
|
371 #endif
|
|
372
|
|
373 path = cycle->pathes.elts;
|
|
374 for (i = 0; i < cycle->pathes.nelts; i++) {
|
|
375
|
|
376 if (ngx_create_dir(path[i]->name.data) == NGX_FILE_ERROR) {
|
|
377 err = ngx_errno;
|
|
378 if (err != NGX_EEXIST) {
|
|
379 ngx_log_error(NGX_LOG_EMERG, cycle->log, err,
|
|
380 ngx_create_dir_n " \"%s\" failed",
|
|
381 path[i]->name.data);
|
|
382 return NGX_ERROR;
|
|
383 }
|
|
384 }
|
|
385
|
66
|
386 if (user == (ngx_uid_t) NGX_CONF_UNSET_UINT) {
|
16
|
387 continue;
|
|
388 }
|
|
389
|
|
390 #if !(NGX_WIN32)
|
|
391
|
|
392 if (ngx_file_info((const char *) path[i]->name.data, &fi) == -1) {
|
|
393 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
|
|
394 ngx_file_info_n " \"%s\" failed", path[i]->name.data);
|
|
395 return NGX_ERROR;
|
|
396 }
|
|
397
|
|
398 if (fi.st_uid != user) {
|
|
399 if (chown((const char *) path[i]->name.data, user, -1) == -1) {
|
|
400 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
|
|
401 "chown(\"%s\", %d) failed",
|
|
402 path[i]->name.data, user);
|
|
403 return NGX_ERROR;
|
|
404 }
|
|
405 }
|
|
406
|
|
407 if ((fi.st_mode & (S_IRUSR|S_IWUSR|S_IXUSR))
|
|
408 != (S_IRUSR|S_IWUSR|S_IXUSR))
|
|
409 {
|
|
410 fi.st_mode |= (S_IRUSR|S_IWUSR|S_IXUSR);
|
|
411
|
|
412 if (chmod((const char *) path[i]->name.data, fi.st_mode) == -1) {
|
|
413 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
|
|
414 "chmod() \"%s\" failed", path[i]->name.data);
|
|
415 return NGX_ERROR;
|
|
416 }
|
|
417 }
|
|
418
|
|
419 #endif
|
|
420 }
|
|
421
|
|
422 return NGX_OK;
|
|
423 }
|