Klaus Demo nginx / fadc7a7
win32 ngx_open_file() supports utf8 names and NGX_FILE_APPEND Igor Sysoev 13 years ago
7 changed file(s) with 127 addition(s) and 27 deletion(s). Raw diff Collapse all Expand all
358358 continue;
359359 }
360360
361 file[i].fd = ngx_open_file(file[i].name.data, NGX_FILE_RDWR,
362 NGX_FILE_CREATE_OR_OPEN|NGX_FILE_APPEND,
361 file[i].fd = ngx_open_file(file[i].name.data,
362 NGX_FILE_RDWR|NGX_FILE_APPEND,
363 NGX_FILE_CREATE_OR_OPEN,
363364 NGX_FILE_DEFAULT_ACCESS);
364365
365366 ngx_log_debug3(NGX_LOG_DEBUG_CORE, log, 0,
10631064 file[i].pos = file[i].buffer;
10641065 }
10651066
1066 fd = ngx_open_file(file[i].name.data, NGX_FILE_RDWR,
1067 NGX_FILE_CREATE_OR_OPEN|NGX_FILE_APPEND,
1068 NGX_FILE_DEFAULT_ACCESS);
1067 fd = ngx_open_file(file[i].name.data, NGX_FILE_RDWR|NGX_FILE_APPEND,
1068 NGX_FILE_CREATE_OR_OPEN, NGX_FILE_DEFAULT_ACCESS);
10691069
10701070 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
10711071 "reopen file \"%s\", old:%d new:%d",
199199
200200 ngx_stderr_fileno = GetStdHandle(STD_ERROR_HANDLE);
201201
202 ngx_stderr.fd = ngx_open_file(NGX_ERROR_LOG_PATH, NGX_FILE_RDWR,
203 NGX_FILE_CREATE_OR_OPEN|NGX_FILE_APPEND, 0);
202 ngx_stderr.fd = ngx_open_file((u_char *) NGX_ERROR_LOG_PATH,
203 NGX_FILE_RDWR|NGX_FILE_APPEND,
204 NGX_FILE_CREATE_OR_OPEN,
205 NGX_FILE_DEFAULT_ACCESS);
204206
205207 if (ngx_stderr.fd == NGX_INVALID_FILE) {
206208 ngx_message_box("nginx", MB_OK, ngx_errno,
207209 "Could not open error log file: "
208210 ngx_open_file_n " \"" NGX_ERROR_LOG_PATH "\" failed");
209 return NULL;
210 }
211
212 if (ngx_file_append_mode(ngx_stderr.fd) == NGX_ERROR) {
213 ngx_message_box("nginx", MB_OK, ngx_errno,
214 "Could not open error log file: "
215 ngx_file_append_mode_n " \"" NGX_ERROR_LOG_PATH
216 "\" failed");
217211 return NULL;
218212 }
219213
465465 fd = ngx_open_file(name, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0);
466466
467467 } else {
468 fd = ngx_open_file(name, NGX_FILE_RDWR,
469 NGX_FILE_CREATE_OR_OPEN|NGX_FILE_APPEND,
470 NGX_FILE_DEFAULT_ACCESS);
468 fd = ngx_open_file(name, NGX_FILE_RDWR|NGX_FILE_APPEND,
469 NGX_FILE_CREATE_OR_OPEN, NGX_FILE_DEFAULT_ACCESS);
471470 }
472471
473472 if (fd == NGX_INVALID_FILE) {
4343 #define NGX_EHOSTUNREACH EHOSTUNREACH
4444 #define NGX_ENOSYS ENOSYS
4545 #define NGX_ECANCELED ECANCELED
46 #define NGX_EILSEQ EILSEQ
4647 #define NGX_ENOMOREFILES 0
4748
4849
4747 #define NGX_EHOSTDOWN WSAEHOSTDOWN
4848 #define NGX_EHOSTUNREACH WSAEHOSTUNREACH
4949 #define NGX_ENOMOREFILES ERROR_NO_MORE_FILES
50 #define NGX_EILSEQ ERROR_NO_UNICODE_TRANSLATION
5051
5152 #define NGX_EALREADY WSAEALREADY
5253 #define NGX_EINVAL WSAEINVAL
55
66 #include <ngx_config.h>
77 #include <ngx_core.h>
8
9
10 #define NGX_UTF16_BUFLEN 256
11
12 static u_short *ngx_utf8_to_utf16(u_short *utf16, u_char *utf8, size_t len);
13
14
15 /* FILE_FLAG_BACKUP_SEMANTICS allows to obtain a handle to a directory */
16
17 ngx_fd_t
18 ngx_open_file(u_char *name, u_long mode, u_long create, u_long access)
19 {
20 u_short *u;
21 ngx_fd_t fd;
22 u_short utf16[NGX_UTF16_BUFLEN];
23
24 u = ngx_utf8_to_utf16(utf16, name, NGX_UTF16_BUFLEN);
25
26 if (u == NULL) {
27 return INVALID_HANDLE_VALUE;
28 }
29
30 fd = CreateFileW(u, mode,
31 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
32 NULL, create, FILE_FLAG_BACKUP_SEMANTICS, NULL);
33
34 if (u != utf16) {
35 ngx_free(u);
36 }
37
38 return fd;
39 }
840
941
1042 ssize_t
528560
529561 return sc * bs;
530562 }
563
564
565 static u_short *
566 ngx_utf8_to_utf16(u_short *utf16, u_char *utf8, size_t len)
567 {
568 u_char *p;
569 u_short *u, *last;
570 uint32_t n;
571
572 p = utf8;
573 u = utf16;
574 last = utf16 + len;
575
576 while (u < last) {
577
578 if (*p < 0x80) {
579 *u = (u_short) *p;
580
581 if (*p == 0) {
582 return utf16;
583 }
584
585 u++;
586 p++;
587
588 continue;
589 }
590
591 n = ngx_utf8_decode(&p, 4);
592
593 if (n > 0xffff) {
594 free(utf16);
595 ngx_set_errno(NGX_EILSEQ);
596 return NULL;
597 }
598
599 *u++ = (u_short) n;
600 }
601
602 /* the given buffer is not enough, allocate a new one */
603
604 u = malloc(((p - utf8) + ngx_strlen(p) + 1) * sizeof(u_short));
605 if (u == NULL) {
606 return NULL;
607 }
608
609 ngx_memcpy(u, utf16, len * 2);
610
611 utf16 = u;
612 u += len;
613
614 for ( ;; ) {
615
616 if (*p < 0x80) {
617 *u = (u_short) *p;
618
619 if (*p == 0) {
620 return utf16;
621 }
622
623 u++;
624 p++;
625
626 continue;
627 }
628
629 n = ngx_utf8_decode(&p, 4);
630
631 if (n > 0xffff) {
632 free(utf16);
633 ngx_set_errno(NGX_EILSEQ);
634 return NULL;
635 }
636
637 *u++ = (u_short) n;
638 }
639
640 /* unreachable */
641 }
5656 #define NGX_FILE_ERROR 0
5757
5858
59 /*
60 * FILE_FLAG_BACKUP_SEMANTICS allows to obtain a handle to a directory
61 */
62
63 #define ngx_open_file(name, mode, create, access) \
64 CreateFile((const char *) name, mode, \
65 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, \
66 NULL, create, FILE_FLAG_BACKUP_SEMANTICS, NULL)
59 ngx_fd_t ngx_open_file(u_char *name, u_long mode, u_long create, u_long access);
6760 #define ngx_open_file_n "CreateFile()"
6861
6962 #define NGX_FILE_RDONLY GENERIC_READ
7063 #define NGX_FILE_WRONLY GENERIC_WRITE
7164 #define NGX_FILE_RDWR GENERIC_READ|GENERIC_WRITE
65 #define NGX_FILE_APPEND FILE_APPEND_DATA|SYNCHRONIZE
66
7267 #define NGX_FILE_CREATE_OR_OPEN OPEN_ALWAYS
7368 #define NGX_FILE_OPEN OPEN_EXISTING
74 #define NGX_FILE_APPEND 0
7569
7670 #define NGX_FILE_DEFAULT_ACCESS 0
7771 #define NGX_FILE_OWNER_ACCESS 0