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;
|
250
|
12 static ngx_atomic_uint_t ngx_random_number;
|
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,
|
278
|
22 tf->persistent, tf->clean, tf->access);
|
0
|
23
|
|
24 if (rc == NGX_ERROR || rc == NGX_AGAIN) {
|
|
25 return rc;
|
|
26 }
|
|
27
|
216
|
28 if (tf->log_level) {
|
|
29 ngx_log_error(tf->log_level, tf->file.log, 0, "%s %V",
|
186
|
30 tf->warn, &tf->file.name);
|
0
|
31 }
|
|
32 }
|
|
33
|
|
34 return ngx_write_chain_to_file(&tf->file, chain, tf->offset, tf->pool);
|
|
35 }
|
|
36
|
|
37
|
42
|
38 ngx_int_t
|
|
39 ngx_create_temp_file(ngx_file_t *file, ngx_path_t *path, ngx_pool_t *pool,
|
278
|
40 ngx_uint_t persistent, ngx_uint_t clean, ngx_uint_t access)
|
0
|
41 {
|
282
|
42 uint32_t n;
|
58
|
43 ngx_err_t err;
|
112
|
44 ngx_pool_cleanup_t *cln;
|
|
45 ngx_pool_cleanup_file_t *clnf;
|
0
|
46
|
282
|
47 file->name.len = path->name.len + 1 + path->len + 10;
|
0
|
48
|
382
|
49 file->name.data = ngx_pnalloc(pool, file->name.len + 1);
|
50
|
50 if (file->name.data == NULL) {
|
16
|
51 return NGX_ERROR;
|
|
52 }
|
0
|
53
|
|
54 #if 0
|
|
55 for (i = 0; i < file->name.len; i++) {
|
|
56 file->name.data[i] = 'X';
|
|
57 }
|
|
58 #endif
|
|
59
|
|
60 ngx_memcpy(file->name.data, path->name.data, path->name.len);
|
|
61
|
282
|
62 n = (uint32_t) ngx_next_temp_number(0);
|
0
|
63
|
356
|
64 cln = ngx_pool_cleanup_add(pool, sizeof(ngx_pool_cleanup_file_t));
|
|
65 if (cln == NULL) {
|
|
66 return NGX_ERROR;
|
|
67 }
|
|
68
|
0
|
69 for ( ;; ) {
|
66
|
70 (void) ngx_sprintf(file->name.data + path->name.len + 1 + path->len,
|
282
|
71 "%010uD%Z", n);
|
0
|
72
|
356
|
73 ngx_create_hashed_filename(path, file->name.data, file->name.len);
|
0
|
74
|
356
|
75 ngx_log_debug1(NGX_LOG_DEBUG_CORE, file->log, 0,
|
|
76 "hashed path: %s", file->name.data);
|
112
|
77
|
276
|
78 file->fd = ngx_open_tempfile(file->name.data, persistent, access);
|
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
|
278
|
85 cln->handler = clean ? ngx_pool_delete_file : ngx_pool_cleanup_file;
|
112
|
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) {
|
282
|
98 n = (uint32_t) ngx_next_temp_number(1);
|
0
|
99 continue;
|
|
100 }
|
|
101
|
514
|
102 if ((path->level[0] == 0) || (err != NGX_ENOPATH)) {
|
0
|
103 ngx_log_error(NGX_LOG_CRIT, file->log, err,
|
|
104 ngx_open_tempfile_n " \"%s\" failed",
|
|
105 file->name.data);
|
|
106 return NGX_ERROR;
|
|
107 }
|
|
108
|
|
109 if (ngx_create_path(file, path) == NGX_ERROR) {
|
|
110 return NGX_ERROR;
|
|
111 }
|
|
112 }
|
|
113 }
|
|
114
|
|
115
|
42
|
116 void
|
356
|
117 ngx_create_hashed_filename(ngx_path_t *path, u_char *file, size_t len)
|
0
|
118 {
|
356
|
119 size_t i, level;
|
|
120 ngx_uint_t n;
|
0
|
121
|
356
|
122 i = path->name.len + 1;
|
0
|
123
|
356
|
124 file[path->name.len + path->len] = '/';
|
0
|
125
|
356
|
126 for (n = 0; n < 3; n++) {
|
|
127 level = path->level[n];
|
0
|
128
|
|
129 if (level == 0) {
|
|
130 break;
|
|
131 }
|
|
132
|
356
|
133 len -= level;
|
|
134 file[i - 1] = '/';
|
|
135 ngx_memcpy(&file[i], &file[len], level);
|
|
136 i += level + 1;
|
0
|
137 }
|
|
138 }
|
|
139
|
|
140
|
42
|
141 ngx_int_t
|
|
142 ngx_create_path(ngx_file_t *file, ngx_path_t *path)
|
0
|
143 {
|
66
|
144 size_t pos;
|
|
145 ngx_err_t err;
|
|
146 ngx_uint_t i;
|
0
|
147
|
|
148 pos = path->name.len;
|
|
149
|
|
150 for (i = 0; i < 3; i++) {
|
|
151 if (path->level[i] == 0) {
|
|
152 break;
|
|
153 }
|
|
154
|
|
155 pos += path->level[i] + 1;
|
|
156
|
|
157 file->name.data[pos] = '\0';
|
|
158
|
|
159 ngx_log_debug1(NGX_LOG_DEBUG_CORE, file->log, 0,
|
|
160 "temp file: \"%s\"", file->name.data);
|
|
161
|
218
|
162 if (ngx_create_dir(file->name.data, 0700) == NGX_FILE_ERROR) {
|
0
|
163 err = ngx_errno;
|
|
164 if (err != NGX_EEXIST) {
|
|
165 ngx_log_error(NGX_LOG_CRIT, file->log, err,
|
|
166 ngx_create_dir_n " \"%s\" failed",
|
|
167 file->name.data);
|
|
168 return NGX_ERROR;
|
|
169 }
|
|
170 }
|
|
171
|
|
172 file->name.data[pos] = '/';
|
|
173 }
|
|
174
|
|
175 return NGX_OK;
|
|
176 }
|
|
177
|
|
178
|
186
|
179 ngx_err_t
|
218
|
180 ngx_create_full_path(u_char *dir, ngx_uint_t access)
|
186
|
181 {
|
|
182 u_char *p, ch;
|
|
183 ngx_err_t err;
|
|
184
|
|
185 for (p = dir + 1; *p; p++) {
|
|
186 ch = *p;
|
|
187
|
|
188 if (ch != '/') {
|
|
189 continue;
|
|
190 }
|
|
191
|
|
192 *p = '\0';
|
|
193
|
218
|
194 if (ngx_create_dir(dir, access) == NGX_FILE_ERROR) {
|
186
|
195 err = ngx_errno;
|
|
196 if (err != NGX_EEXIST) {
|
|
197 return err;
|
|
198 }
|
|
199 }
|
|
200
|
|
201 *p = '/';
|
|
202 }
|
|
203
|
|
204 return 0;
|
|
205 }
|
|
206
|
|
207
|
42
|
208 void
|
48
|
209 ngx_init_temp_number(void)
|
0
|
210 {
|
42
|
211 ngx_temp_number = 0;
|
250
|
212 ngx_random_number = 123456;
|
0
|
213 }
|
|
214
|
|
215
|
44
|
216 ngx_atomic_uint_t
|
42
|
217 ngx_next_temp_number(ngx_uint_t collision)
|
0
|
218 {
|
|
219 if (collision) {
|
250
|
220 ngx_temp_number += ngx_random_number;
|
0
|
221 }
|
|
222
|
|
223 return ngx_temp_number++;
|
|
224 }
|
|
225
|
|
226
|
42
|
227 char *
|
|
228 ngx_conf_set_path_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
0
|
229 {
|
|
230 char *p = conf;
|
|
231
|
16
|
232 ssize_t level;
|
178
|
233 ngx_str_t *value;
|
0
|
234 ngx_uint_t i, n;
|
28
|
235 ngx_path_t *path, **slot;
|
0
|
236
|
16
|
237 slot = (ngx_path_t **) (p + cmd->offset);
|
0
|
238
|
16
|
239 if (*slot) {
|
0
|
240 return "is duplicate";
|
|
241 }
|
|
242
|
50
|
243 path = ngx_pcalloc(cf->pool, sizeof(ngx_path_t));
|
|
244 if (path == NULL) {
|
16
|
245 return NGX_CONF_ERROR;
|
|
246 }
|
0
|
247
|
16
|
248 value = cf->args->elts;
|
0
|
249
|
|
250 path->name = value[1];
|
178
|
251
|
210
|
252 if (path->name.data[path->name.len - 1] == '/') {
|
|
253 path->name.len--;
|
|
254 }
|
|
255
|
454
|
256 if (ngx_conf_full_name(cf->cycle, &path->name, 0) != NGX_OK) {
|
178
|
257 return NULL;
|
|
258 }
|
|
259
|
0
|
260 path->len = 0;
|
514
|
261 path->manager = NULL;
|
|
262 path->loader = NULL;
|
16
|
263 path->conf_file = cf->conf_file->file.name.data;
|
|
264 path->line = cf->conf_file->line;
|
0
|
265
|
|
266 for (i = 0, n = 2; n < cf->args->nelts; i++, n++) {
|
|
267 level = ngx_atoi(value[n].data, value[n].len);
|
|
268 if (level == NGX_ERROR || level == 0) {
|
|
269 return "invalid value";
|
|
270 }
|
|
271
|
|
272 path->level[i] = level;
|
|
273 path->len += level + 1;
|
|
274 }
|
|
275
|
|
276 while (i < 3) {
|
|
277 path->level[i++] = 0;
|
|
278 }
|
|
279
|
28
|
280 *slot = path;
|
16
|
281
|
28
|
282 if (ngx_add_path(cf, slot) == NGX_ERROR) {
|
|
283 return NGX_CONF_ERROR;
|
|
284 }
|
|
285
|
|
286 return NGX_CONF_OK;
|
|
287 }
|
|
288
|
|
289
|
318
|
290 char *
|
464
|
291 ngx_conf_merge_path_value(ngx_conf_t *cf, ngx_path_t **path, ngx_path_t *prev,
|
|
292 ngx_path_init_t *init)
|
|
293 {
|
|
294 if (*path) {
|
|
295 return NGX_CONF_OK;
|
|
296 }
|
|
297
|
|
298 if (prev) {
|
|
299 *path = prev;
|
|
300 return NGX_CONF_OK;
|
|
301 }
|
|
302
|
|
303 *path = ngx_palloc(cf->pool, sizeof(ngx_path_t));
|
|
304 if (*path == NULL) {
|
|
305 return NGX_CONF_ERROR;
|
|
306 }
|
|
307
|
|
308 (*path)->name = init->name;
|
|
309
|
|
310 if (ngx_conf_full_name(cf->cycle, &(*path)->name, 0) != NGX_OK) {
|
|
311 return NGX_CONF_ERROR;
|
|
312 }
|
|
313
|
|
314 (*path)->level[0] = init->level[0];
|
|
315 (*path)->level[1] = init->level[1];
|
|
316 (*path)->level[2] = init->level[2];
|
|
317
|
|
318 (*path)->len = init->level[0] + (init->level[0] ? 1 : 0)
|
|
319 + init->level[1] + (init->level[1] ? 1 : 0)
|
|
320 + init->level[2] + (init->level[2] ? 1 : 0);
|
|
321
|
468
|
322 (*path)->manager = NULL;
|
514
|
323 (*path)->loader = NULL;
|
464
|
324 (*path)->conf_file = NULL;
|
|
325
|
|
326 if (ngx_add_path(cf, path) != NGX_OK) {
|
|
327 return NGX_CONF_ERROR;
|
|
328 }
|
|
329
|
|
330 return NGX_CONF_OK;
|
|
331 }
|
|
332
|
|
333
|
|
334 char *
|
318
|
335 ngx_conf_set_access_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
336 {
|
|
337 char *confp = conf;
|
|
338
|
|
339 u_char *p;
|
|
340 ngx_str_t *value;
|
|
341 ngx_uint_t i, right, shift, *access;
|
|
342
|
|
343 access = (ngx_uint_t *) (confp + cmd->offset);
|
320
|
344
|
318
|
345 if (*access != NGX_CONF_UNSET_UINT) {
|
|
346 return "is duplicate";
|
|
347 }
|
320
|
348
|
318
|
349 value = cf->args->elts;
|
|
350
|
|
351 *access = 0600;
|
|
352
|
|
353 for (i = 1; i < cf->args->nelts; i++) {
|
|
354
|
|
355 p = value[i].data;
|
|
356
|
|
357 if (ngx_strncmp(p, "user:", sizeof("user:") - 1) == 0) {
|
|
358 shift = 6;
|
|
359 p += sizeof("user:") - 1;
|
|
360
|
|
361 } else if (ngx_strncmp(p, "group:", sizeof("group:") - 1) == 0) {
|
|
362 shift = 3;
|
|
363 p += sizeof("group:") - 1;
|
|
364
|
|
365 } else if (ngx_strncmp(p, "all:", sizeof("all:") - 1) == 0) {
|
|
366 shift = 0;
|
|
367 p += sizeof("all:") - 1;
|
|
368
|
|
369 } else {
|
|
370 goto invalid;
|
|
371 }
|
|
372
|
|
373 if (ngx_strcmp(p, "rw") == 0) {
|
|
374 right = 6;
|
|
375
|
|
376 } else if (ngx_strcmp(p, "r") == 0) {
|
|
377 right = 4;
|
|
378
|
|
379 } else {
|
|
380 goto invalid;
|
|
381 }
|
|
382
|
|
383 *access |= right << shift;
|
|
384 }
|
320
|
385
|
318
|
386 return NGX_CONF_OK;
|
|
387
|
|
388 invalid:
|
|
389
|
|
390 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid value \"%V\"", &value[i]);
|
|
391
|
|
392 return NGX_CONF_ERROR;
|
|
393 }
|
|
394
|
|
395
|
42
|
396 ngx_int_t
|
|
397 ngx_add_path(ngx_conf_t *cf, ngx_path_t **slot)
|
28
|
398 {
|
|
399 ngx_uint_t i, n;
|
|
400 ngx_path_t *path, **p;
|
|
401
|
|
402 path = *slot;
|
|
403
|
|
404 p = cf->cycle->pathes.elts;
|
16
|
405 for (i = 0; i < cf->cycle->pathes.nelts; i++) {
|
28
|
406 if (p[i]->name.len == path->name.len
|
|
407 && ngx_strcmp(p[i]->name.data, path->name.data) == 0)
|
16
|
408 {
|
|
409 for (n = 0; n < 3; n++) {
|
28
|
410 if (p[i]->level[n] != path->level[n]) {
|
|
411 if (path->conf_file == NULL) {
|
30
|
412 if (p[i]->conf_file == NULL) {
|
|
413 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
|
|
414 "the default path name \"%V\" has "
|
|
415 "the same name as another default path, "
|
|
416 "but the different levels, you need to "
|
|
417 "redefine one of them in http section",
|
|
418 &p[i]->name);
|
|
419 return NGX_ERROR;
|
|
420 }
|
|
421
|
28
|
422 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
|
|
423 "the path name \"%V\" in %s:%ui has "
|
30
|
424 "the same name as default path, but "
|
28
|
425 "the different levels, you need to "
|
|
426 "define default path in http section",
|
|
427 &p[i]->name, p[i]->conf_file, p[i]->line);
|
|
428 return NGX_ERROR;
|
|
429 }
|
|
430
|
16
|
431 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
28
|
432 "the same path name \"%V\" in %s:%ui "
|
|
433 "has the different levels than",
|
|
434 &p[i]->name, p[i]->conf_file, p[i]->line);
|
|
435 return NGX_ERROR;
|
16
|
436 }
|
|
437
|
28
|
438 if (p[i]->level[n] == 0) {
|
16
|
439 break;
|
|
440 }
|
|
441 }
|
|
442
|
28
|
443 *slot = p[i];
|
16
|
444
|
28
|
445 return NGX_OK;
|
16
|
446 }
|
|
447 }
|
|
448
|
50
|
449 p = ngx_array_push(&cf->cycle->pathes);
|
|
450 if (p == NULL) {
|
28
|
451 return NGX_ERROR;
|
16
|
452 }
|
|
453
|
28
|
454 *p = path;
|
0
|
455
|
28
|
456 return NGX_OK;
|
0
|
457 }
|
16
|
458
|
|
459
|
42
|
460 ngx_int_t
|
|
461 ngx_create_pathes(ngx_cycle_t *cycle, ngx_uid_t user)
|
16
|
462 {
|
|
463 ngx_err_t err;
|
|
464 ngx_uint_t i;
|
|
465 ngx_path_t **path;
|
|
466
|
|
467 path = cycle->pathes.elts;
|
|
468 for (i = 0; i < cycle->pathes.nelts; i++) {
|
|
469
|
218
|
470 if (ngx_create_dir(path[i]->name.data, 0700) == NGX_FILE_ERROR) {
|
16
|
471 err = ngx_errno;
|
|
472 if (err != NGX_EEXIST) {
|
|
473 ngx_log_error(NGX_LOG_EMERG, cycle->log, err,
|
|
474 ngx_create_dir_n " \"%s\" failed",
|
|
475 path[i]->name.data);
|
|
476 return NGX_ERROR;
|
|
477 }
|
|
478 }
|
|
479
|
66
|
480 if (user == (ngx_uid_t) NGX_CONF_UNSET_UINT) {
|
16
|
481 continue;
|
|
482 }
|
|
483
|
|
484 #if !(NGX_WIN32)
|
356
|
485 {
|
|
486 ngx_file_info_t fi;
|
16
|
487
|
484
|
488 if (ngx_file_info((const char *) path[i]->name.data, &fi)
|
|
489 == NGX_FILE_ERROR)
|
|
490 {
|
16
|
491 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
|
|
492 ngx_file_info_n " \"%s\" failed", path[i]->name.data);
|
|
493 return NGX_ERROR;
|
|
494 }
|
|
495
|
|
496 if (fi.st_uid != user) {
|
|
497 if (chown((const char *) path[i]->name.data, user, -1) == -1) {
|
|
498 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
|
|
499 "chown(\"%s\", %d) failed",
|
|
500 path[i]->name.data, user);
|
|
501 return NGX_ERROR;
|
|
502 }
|
|
503 }
|
|
504
|
|
505 if ((fi.st_mode & (S_IRUSR|S_IWUSR|S_IXUSR))
|
|
506 != (S_IRUSR|S_IWUSR|S_IXUSR))
|
|
507 {
|
|
508 fi.st_mode |= (S_IRUSR|S_IWUSR|S_IXUSR);
|
|
509
|
|
510 if (chmod((const char *) path[i]->name.data, fi.st_mode) == -1) {
|
|
511 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
|
|
512 "chmod() \"%s\" failed", path[i]->name.data);
|
|
513 return NGX_ERROR;
|
|
514 }
|
|
515 }
|
356
|
516 }
|
16
|
517 #endif
|
|
518 }
|
|
519
|
|
520 return NGX_OK;
|
|
521 }
|
272
|
522
|
|
523
|
|
524 ngx_int_t
|
362
|
525 ngx_ext_rename_file(ngx_str_t *src, ngx_str_t *to, ngx_ext_rename_file_t *ext)
|
356
|
526 {
|
514
|
527 u_char *name;
|
|
528 ngx_err_t err;
|
|
529 ngx_copy_file_t cf;
|
356
|
530
|
|
531 #if !(NGX_WIN32)
|
|
532
|
430
|
533 if (ext->access) {
|
|
534 if (ngx_change_file_access(src->data, ext->access) == NGX_FILE_ERROR) {
|
|
535 ngx_log_error(NGX_LOG_CRIT, ext->log, ngx_errno,
|
|
536 ngx_change_file_access_n " \"%s\" failed", src->data);
|
|
537 err = 0;
|
|
538 goto failed;
|
|
539 }
|
356
|
540 }
|
|
541
|
|
542 #endif
|
|
543
|
362
|
544 if (ext->time != -1) {
|
|
545 if (ngx_set_file_time(src->data, ext->fd, ext->time) != NGX_OK) {
|
|
546 ngx_log_error(NGX_LOG_CRIT, ext->log, ngx_errno,
|
|
547 ngx_set_file_time_n " \"%s\" failed", src->data);
|
|
548 err = 0;
|
|
549 goto failed;
|
|
550 }
|
|
551 }
|
|
552
|
356
|
553 if (ngx_rename_file(src->data, to->data) != NGX_FILE_ERROR) {
|
|
554 return NGX_OK;
|
|
555 }
|
|
556
|
|
557 err = ngx_errno;
|
|
558
|
514
|
559 if (err == NGX_ENOPATH) {
|
|
560
|
362
|
561 if (!ext->create_path) {
|
356
|
562 goto failed;
|
|
563 }
|
|
564
|
430
|
565 err = ngx_create_full_path(to->data, ngx_dir_access(ext->path_access));
|
356
|
566
|
|
567 if (err) {
|
362
|
568 ngx_log_error(NGX_LOG_CRIT, ext->log, err,
|
356
|
569 ngx_create_dir_n " \"%s\" failed", to->data);
|
|
570 err = 0;
|
|
571 goto failed;
|
|
572 }
|
|
573
|
|
574 if (ngx_rename_file(src->data, to->data) != NGX_FILE_ERROR) {
|
|
575 return NGX_OK;
|
|
576 }
|
|
577
|
|
578 err = ngx_errno;
|
|
579 goto failed;
|
|
580 }
|
|
581
|
|
582 #if (NGX_WIN32)
|
|
583
|
|
584 if (err == NGX_EEXIST) {
|
362
|
585 if (ngx_win32_rename_file(src, to, ext->log) == NGX_OK) {
|
356
|
586
|
|
587 if (ngx_rename_file(src->data, to->data) != NGX_FILE_ERROR) {
|
|
588 return NGX_OK;
|
|
589 }
|
|
590
|
|
591 err = ngx_errno;
|
|
592
|
|
593 } else {
|
|
594 err = 0;
|
|
595 }
|
|
596 }
|
|
597
|
|
598 #endif
|
|
599
|
514
|
600 if (err == NGX_EXDEV) {
|
|
601
|
|
602 cf.size = -1;
|
|
603 cf.buf_size = 0;
|
|
604 cf.access = ext->access;
|
|
605 cf.time = ext->time;
|
|
606 cf.log = ext->log;
|
|
607
|
|
608 name = ngx_alloc(to->len + 1 + 10, ext->log);
|
|
609 if (name == NULL) {
|
|
610 return NGX_ERROR;
|
|
611 }
|
|
612
|
|
613 (void) ngx_sprintf(name, "%*s.%010uD%Z", to->len - 1, to->data,
|
|
614 (uint32_t) ngx_next_temp_number(0));
|
|
615
|
|
616 if (ngx_copy_file(src->data, name, &cf) == NGX_OK) {
|
|
617
|
|
618 if (ngx_rename_file(name, to->data) == NGX_FILE_ERROR) {
|
|
619 ngx_free(name);
|
|
620 goto failed;
|
|
621 }
|
|
622
|
|
623 ngx_free(name);
|
|
624
|
|
625 if (ngx_delete_file(src->data) == NGX_FILE_ERROR) {
|
|
626 ngx_log_error(NGX_LOG_CRIT, ext->log, ngx_errno,
|
|
627 ngx_delete_file_n " \"%s\" failed", src->data);
|
|
628
|
|
629 return NGX_ERROR;
|
|
630 }
|
|
631
|
|
632 return NGX_OK;
|
|
633 }
|
|
634
|
|
635 ngx_free(name);
|
|
636 }
|
|
637
|
356
|
638 failed:
|
|
639
|
366
|
640 if (ext->delete_file) {
|
356
|
641 if (ngx_delete_file(src->data) == NGX_FILE_ERROR) {
|
362
|
642 ngx_log_error(NGX_LOG_CRIT, ext->log, ngx_errno,
|
356
|
643 ngx_delete_file_n " \"%s\" failed", src->data);
|
|
644 }
|
|
645 }
|
|
646
|
514
|
647 if (err) {
|
362
|
648 ngx_log_error(NGX_LOG_CRIT, ext->log, err,
|
356
|
649 ngx_rename_file_n " \"%s\" to \"%s\" failed",
|
|
650 src->data, to->data);
|
|
651 }
|
|
652
|
514
|
653 return NGX_ERROR;
|
|
654 }
|
|
655
|
|
656
|
|
657 ngx_int_t
|
|
658 ngx_copy_file(u_char *from, u_char *to, ngx_copy_file_t *cf)
|
|
659 {
|
|
660 char *buf;
|
|
661 off_t size;
|
|
662 size_t len;
|
|
663 ssize_t n;
|
|
664 ngx_fd_t fd, nfd;
|
|
665 ngx_int_t rc;
|
|
666 ngx_file_info_t fi;
|
|
667
|
|
668 rc = NGX_ERROR;
|
|
669 buf = NULL;
|
|
670 nfd = NGX_INVALID_FILE;
|
|
671
|
|
672 fd = ngx_open_file(from, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0);
|
|
673
|
|
674 if (fd == NGX_INVALID_FILE) {
|
|
675 ngx_log_error(NGX_LOG_CRIT, cf->log, ngx_errno,
|
|
676 ngx_open_file_n " \"%s\" failed", from);
|
|
677 goto failed;
|
|
678 }
|
|
679
|
|
680 if (cf->size != -1) {
|
|
681 size = cf->size;
|
|
682
|
|
683 } else {
|
|
684 if (ngx_fd_info(fd, &fi) == NGX_FILE_ERROR) {
|
|
685 ngx_log_error(NGX_LOG_ALERT, cf->log, ngx_errno,
|
|
686 ngx_fd_info_n " \"%s\" failed", from);
|
|
687
|
|
688 goto failed;
|
|
689 }
|
|
690
|
|
691 size = ngx_file_size(&fi);
|
|
692 }
|
|
693
|
|
694 len = cf->buf_size ? cf->buf_size : 65536;
|
|
695
|
|
696 if ((off_t) len > size) {
|
|
697 len = (size_t) size;
|
|
698 }
|
|
699
|
|
700 buf = ngx_alloc(len, cf->log);
|
|
701 if (buf == NULL) {
|
|
702 goto failed;
|
|
703 }
|
|
704
|
|
705 nfd = ngx_open_file(to, NGX_FILE_WRONLY, NGX_FILE_CREATE_OR_OPEN,
|
|
706 cf->access);
|
|
707
|
|
708 if (nfd == NGX_INVALID_FILE) {
|
|
709 ngx_log_error(NGX_LOG_CRIT, cf->log, ngx_errno,
|
|
710 ngx_open_file_n " \"%s\" failed", to);
|
|
711 goto failed;
|
|
712 }
|
|
713
|
|
714 while (size > 0) {
|
430
|
715
|
514
|
716 if ((off_t) len > size) {
|
|
717 len = (size_t) size;
|
|
718 }
|
|
719
|
|
720 n = ngx_read_fd(fd, buf, len);
|
|
721
|
|
722 if (n == NGX_FILE_ERROR) {
|
|
723 ngx_log_error(NGX_LOG_ALERT, cf->log, ngx_errno,
|
|
724 ngx_read_fd_n " \"%s\" failed", from);
|
|
725 goto failed;
|
|
726 }
|
|
727
|
|
728 if ((size_t) n != len) {
|
|
729 ngx_log_error(NGX_LOG_ALERT, cf->log, ngx_errno,
|
|
730 ngx_read_fd_n " has read only %z of %uz from %s",
|
|
731 n, size, from);
|
|
732 goto failed;
|
|
733 }
|
|
734
|
|
735 n = ngx_write_fd(nfd, buf, len);
|
|
736
|
|
737 if (n == NGX_FILE_ERROR) {
|
|
738 ngx_log_error(NGX_LOG_ALERT, cf->log, ngx_errno,
|
|
739 ngx_write_fd_n " \"%s\" failed", to);
|
|
740 goto failed;
|
|
741 }
|
|
742
|
|
743 if ((size_t) n != len) {
|
|
744 ngx_log_error(NGX_LOG_ALERT, cf->log, ngx_errno,
|
|
745 ngx_write_fd_n " has written only %z of %uz to %s",
|
|
746 n, size, to);
|
|
747 goto failed;
|
|
748 }
|
|
749
|
|
750 size -= n;
|
|
751 }
|
|
752
|
|
753 if (ngx_set_file_time(to, nfd, cf->time) != NGX_OK) {
|
|
754 ngx_log_error(NGX_LOG_ALERT, cf->log, ngx_errno,
|
|
755 ngx_set_file_time_n " \"%s\" failed", to);
|
|
756 goto failed;
|
|
757 }
|
|
758
|
|
759 rc = NGX_OK;
|
|
760
|
|
761 failed:
|
|
762
|
|
763 if (nfd != NGX_INVALID_FILE) {
|
|
764 if (ngx_close_file(nfd) == NGX_FILE_ERROR) {
|
|
765 ngx_log_error(NGX_LOG_ALERT, cf->log, ngx_errno,
|
|
766 ngx_close_file_n " \"%s\" failed", to);
|
|
767 }
|
|
768 }
|
|
769
|
|
770 if (fd != NGX_INVALID_FILE) {
|
|
771 if (ngx_close_file(fd) == NGX_FILE_ERROR) {
|
|
772 ngx_log_error(NGX_LOG_ALERT, cf->log, ngx_errno,
|
|
773 ngx_close_file_n " \"%s\" failed", from);
|
|
774 }
|
|
775 }
|
|
776
|
|
777 if (buf) {
|
|
778 ngx_free(buf);
|
|
779 }
|
|
780
|
|
781 return rc;
|
356
|
782 }
|
|
783
|
|
784
|
|
785 /*
|
|
786 * ctx->init_handler() - see ctx->alloc
|
|
787 * ctx->file_handler() - file handler
|
|
788 * ctx->pre_tree_handler() - handler is called before entering directory
|
|
789 * ctx->post_tree_handler() - handler is called after leaving directory
|
|
790 * ctx->spec_handler() - special (socket, FIFO, etc.) file handler
|
|
791 *
|
|
792 * ctx->data - some data structure, it may be the same on all levels, or
|
|
793 * reallocated if ctx->alloc is nonzero
|
|
794 *
|
|
795 * ctx->alloc - a size of data structure that is allocated at every level
|
|
796 * and is initilialized by ctx->init_handler()
|
|
797 *
|
|
798 * ctx->log - a log
|
|
799 *
|
|
800 * on fatal (memory) error handler must return NGX_ABORT to stop walking tree
|
|
801 */
|
|
802
|
|
803 ngx_int_t
|
272
|
804 ngx_walk_tree(ngx_tree_ctx_t *ctx, ngx_str_t *tree)
|
|
805 {
|
|
806 void *data, *prev;
|
|
807 u_char *p, *name;
|
|
808 size_t len;
|
|
809 ngx_int_t rc;
|
|
810 ngx_err_t err;
|
|
811 ngx_str_t file, buf;
|
|
812 ngx_dir_t dir;
|
|
813
|
|
814 buf.len = 0;
|
|
815 buf.data = NULL;
|
|
816
|
|
817 ngx_log_debug1(NGX_LOG_DEBUG_CORE, ctx->log, 0,
|
|
818 "walk tree \"%V\"", tree);
|
|
819
|
|
820 if (ngx_open_dir(tree, &dir) == NGX_ERROR) {
|
|
821 ngx_log_error(NGX_LOG_CRIT, ctx->log, ngx_errno,
|
|
822 ngx_open_dir_n " \"%s\" failed", tree->data);
|
|
823 return NGX_ERROR;
|
|
824 }
|
|
825
|
|
826 prev = ctx->data;
|
|
827
|
276
|
828 if (ctx->alloc) {
|
|
829 data = ngx_alloc(ctx->alloc, ctx->log);
|
272
|
830 if (data == NULL) {
|
|
831 goto failed;
|
|
832 }
|
|
833
|
|
834 if (ctx->init_handler(data, prev) == NGX_ABORT) {
|
|
835 goto failed;
|
|
836 }
|
|
837
|
|
838 ctx->data = data;
|
322
|
839
|
|
840 } else {
|
|
841 data = NULL;
|
272
|
842 }
|
|
843
|
|
844 for ( ;; ) {
|
|
845
|
|
846 ngx_set_errno(0);
|
|
847
|
|
848 if (ngx_read_dir(&dir) == NGX_ERROR) {
|
|
849 err = ngx_errno;
|
|
850
|
|
851 if (err == NGX_ENOMOREFILES) {
|
|
852 rc = NGX_OK;
|
|
853
|
|
854 } else {
|
|
855 ngx_log_error(NGX_LOG_CRIT, ctx->log, err,
|
|
856 ngx_read_dir_n " \"%s\" failed", tree->data);
|
|
857 rc = NGX_ERROR;
|
|
858 }
|
|
859
|
|
860 goto done;
|
|
861 }
|
|
862
|
|
863 len = ngx_de_namelen(&dir);
|
|
864 name = ngx_de_name(&dir);
|
|
865
|
|
866 ngx_log_debug2(NGX_LOG_DEBUG_CORE, ctx->log, 0,
|
|
867 "tree name %uz:\"%s\"", len, name);
|
|
868
|
|
869 if (len == 1 && name[0] == '.') {
|
|
870 continue;
|
|
871 }
|
|
872
|
|
873 if (len == 2 && name[0] == '.' && name[1] == '.') {
|
|
874 continue;
|
|
875 }
|
|
876
|
|
877 file.len = tree->len + 1 + len;
|
|
878
|
|
879 if (file.len + NGX_DIR_MASK_LEN > buf.len) {
|
|
880
|
|
881 if (buf.len) {
|
|
882 ngx_free(buf.data);
|
|
883 }
|
|
884
|
|
885 buf.len = tree->len + 1 + len + NGX_DIR_MASK_LEN;
|
|
886
|
|
887 buf.data = ngx_alloc(buf.len + 1, ctx->log);
|
|
888 if (buf.data == NULL) {
|
|
889 goto failed;
|
|
890 }
|
|
891 }
|
|
892
|
|
893 p = ngx_cpymem(buf.data, tree->data, tree->len);
|
|
894 *p++ = '/';
|
|
895 ngx_memcpy(p, name, len + 1);
|
|
896
|
|
897 file.data = buf.data;
|
|
898
|
|
899 ngx_log_debug1(NGX_LOG_DEBUG_CORE, ctx->log, 0,
|
|
900 "tree path \"%s\"", file.data);
|
|
901
|
|
902 if (!dir.valid_info) {
|
|
903 if (ngx_de_info(file.data, &dir) == NGX_FILE_ERROR) {
|
|
904 ngx_log_error(NGX_LOG_CRIT, ctx->log, ngx_errno,
|
|
905 ngx_de_info_n " \"%s\" failed", file.data);
|
|
906 continue;
|
|
907 }
|
|
908 }
|
|
909
|
|
910 if (ngx_de_is_file(&dir)) {
|
|
911
|
|
912 ngx_log_debug1(NGX_LOG_DEBUG_CORE, ctx->log, 0,
|
|
913 "tree file \"%s\"", file.data);
|
|
914
|
276
|
915 ctx->size = ngx_de_size(&dir);
|
|
916 ctx->access = ngx_de_access(&dir);
|
|
917 ctx->mtime = ngx_de_mtime(&dir);
|
|
918
|
272
|
919 if (ctx->file_handler(ctx, &file) == NGX_ABORT) {
|
|
920 goto failed;
|
|
921 }
|
|
922
|
|
923 } else if (ngx_de_is_dir(&dir)) {
|
|
924
|
|
925 ngx_log_debug1(NGX_LOG_DEBUG_CORE, ctx->log, 0,
|
|
926 "tree enter dir \"%s\"", file.data);
|
|
927
|
276
|
928 ctx->access = ngx_de_access(&dir);
|
|
929 ctx->mtime = ngx_de_mtime(&dir);
|
|
930
|
272
|
931 if (ctx->pre_tree_handler(ctx, &file) == NGX_ABORT) {
|
|
932 goto failed;
|
|
933 }
|
|
934
|
|
935 if (ngx_walk_tree(ctx, &file) == NGX_ABORT) {
|
|
936 goto failed;
|
|
937 }
|
|
938
|
276
|
939 ctx->access = ngx_de_access(&dir);
|
|
940 ctx->mtime = ngx_de_mtime(&dir);
|
|
941
|
272
|
942 if (ctx->post_tree_handler(ctx, &file) == NGX_ABORT) {
|
|
943 goto failed;
|
|
944 }
|
|
945
|
|
946 } else {
|
|
947
|
|
948 ngx_log_debug1(NGX_LOG_DEBUG_CORE, ctx->log, 0,
|
|
949 "tree special \"%s\"", file.data);
|
|
950
|
|
951 if (ctx->spec_handler(ctx, &file) == NGX_ABORT) {
|
|
952 goto failed;
|
|
953 }
|
|
954 }
|
|
955 }
|
|
956
|
|
957 failed:
|
|
958
|
|
959 rc = NGX_ABORT;
|
|
960
|
|
961 done:
|
|
962
|
|
963 if (buf.len) {
|
|
964 ngx_free(buf.data);
|
|
965 }
|
|
966
|
322
|
967 if (data) {
|
|
968 ngx_free(data);
|
272
|
969 ctx->data = prev;
|
|
970 }
|
|
971
|
|
972 if (ngx_close_dir(&dir) == NGX_ERROR) {
|
|
973 ngx_log_error(NGX_LOG_CRIT, ctx->log, ngx_errno,
|
|
974 ngx_close_dir_n " \"%s\" failed", tree->data);
|
|
975 }
|
|
976
|
|
977 return rc;
|
|
978 }
|