1
2/*
3 * Copyright (C) Igor Sysoev
4 * Copyright (C) Nginx, Inc.
5 */
6
7
8#include <ngx_config.h>
9#include <ngx_core.h>
10
11
12#define NGX_UTF16_BUFLEN  256
13
14static ngx_int_t ngx_win32_check_filename(u_char *name, u_short *u,
15    size_t len);
16static u_short *ngx_utf8_to_utf16(u_short *utf16, u_char *utf8, size_t *len);
17
18
19/* FILE_FLAG_BACKUP_SEMANTICS allows to obtain a handle to a directory */
20
21ngx_fd_t
22ngx_open_file(u_char *name, u_long mode, u_long create, u_long access)
23{
24    size_t      len;
25    u_short    *u;
26    ngx_fd_t    fd;
27    ngx_err_t   err;
28    u_short     utf16[NGX_UTF16_BUFLEN];
29
30    len = NGX_UTF16_BUFLEN;
31    u = ngx_utf8_to_utf16(utf16, name, &len);
32
33    if (u == NULL) {
34        return INVALID_HANDLE_VALUE;
35    }
36
37    fd = INVALID_HANDLE_VALUE;
38
39    if (create == NGX_FILE_OPEN
40        && ngx_win32_check_filename(name, u, len) != NGX_OK)
41    {
42        goto failed;
43    }
44
45    fd = CreateFileW(u, mode,
46                     FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
47                     NULL, create, FILE_FLAG_BACKUP_SEMANTICS, NULL);
48
49failed:
50
51    if (u != utf16) {
52        err = ngx_errno;
53        ngx_free(u);
54        ngx_set_errno(err);
55    }
56
57    return fd;
58}
59
60
61ssize_t
62ngx_read_file(ngx_file_t *file, u_char *buf, size_t size, off_t offset)
63{
64    u_long      n;
65    ngx_err_t   err;
66    OVERLAPPED  ovlp, *povlp;
67
68    ovlp.Internal = 0;
69    ovlp.InternalHigh = 0;
70    ovlp.Offset = (u_long) offset;
71    ovlp.OffsetHigh = (u_long) (offset >> 32);
72    ovlp.hEvent = NULL;
73
74    povlp = &ovlp;
75
76    if (ReadFile(file->fd, buf, size, &n, povlp) == 0) {
77        err = ngx_errno;
78
79        if (err == ERROR_HANDLE_EOF) {
80            return 0;
81        }
82
83        ngx_log_error(NGX_LOG_ERR, file->log, err,
84                      "ReadFile() \"%s\" failed", file->name.data);
85        return NGX_ERROR;
86    }
87
88    file->offset += n;
89
90    return n;
91}
92
93
94ssize_t
95ngx_write_file(ngx_file_t *file, u_char *buf, size_t size, off_t offset)
96{
97    u_long      n;
98    OVERLAPPED  ovlp, *povlp;
99
100    ovlp.Internal = 0;
101    ovlp.InternalHigh = 0;
102    ovlp.Offset = (u_long) offset;
103    ovlp.OffsetHigh = (u_long) (offset >> 32);
104    ovlp.hEvent = NULL;
105
106    povlp = &ovlp;
107
108    if (WriteFile(file->fd, buf, size, &n, povlp) == 0) {
109        ngx_log_error(NGX_LOG_ERR, file->log, ngx_errno,
110                      "WriteFile() \"%s\" failed", file->name.data);
111        return NGX_ERROR;
112    }
113
114    if (n != size) {
115        ngx_log_error(NGX_LOG_CRIT, file->log, 0,
116                      "WriteFile() \"%s\" has written only %ul of %uz",
117                      file->name.data, n, size);
118        return NGX_ERROR;
119    }
120
121    file->offset += n;
122
123    return n;
124}
125
126
127ssize_t
128ngx_write_chain_to_file(ngx_file_t *file, ngx_chain_t *cl, off_t offset,
129    ngx_pool_t *pool)
130{
131    u_char   *buf, *prev;
132    size_t    size;
133    ssize_t   total, n;
134
135    total = 0;
136
137    while (cl) {
138        buf = cl->buf->pos;
139        prev = buf;
140        size = 0;
141
142        /* coalesce the neighbouring bufs */
143
144        while (cl && prev == cl->buf->pos) {
145            size += cl->buf->last - cl->buf->pos;
146            prev = cl->buf->last;
147            cl = cl->next;
148        }
149
150        n = ngx_write_file(file, buf, size, offset);
151
152        if (n == NGX_ERROR) {
153            return NGX_ERROR;
154        }
155
156        total += n;
157        offset += n;
158    }
159
160    return total;
161}
162
163
164ssize_t
165ngx_read_fd(ngx_fd_t fd, void *buf, size_t size)
166{
167    u_long  n;
168
169    if (ReadFile(fd, buf, size, &n, NULL) != 0) {
170        return (size_t) n;
171    }
172
173    return -1;
174}
175
176
177ssize_t
178ngx_write_fd(ngx_fd_t fd, void *buf, size_t size)
179{
180    u_long  n;
181
182    if (WriteFile(fd, buf, size, &n, NULL) != 0) {
183        return (size_t) n;
184    }
185
186    return -1;
187}
188
189
190ssize_t
191ngx_write_console(ngx_fd_t fd, void *buf, size_t size)
192{
193    u_long  n;
194
195    (void) CharToOemBuff(buf, buf, size);
196
197    if (WriteFile(fd, buf, size, &n, NULL) != 0) {
198        return (size_t) n;
199    }
200
201    return -1;
202}
203
204
205ngx_err_t
206ngx_win32_rename_file(ngx_str_t *from, ngx_str_t *to, ngx_log_t *log)
207{
208    u_char             *name;
209    ngx_err_t           err;
210    ngx_uint_t          collision;
211    ngx_atomic_uint_t   num;
212
213    name = ngx_alloc(to->len + 1 + NGX_ATOMIC_T_LEN + 1 + sizeof("DELETE"),
214                     log);
215    if (name == NULL) {
216        return NGX_ENOMEM;
217    }
218
219    ngx_memcpy(name, to->data, to->len);
220
221    collision = 0;
222
223    /* mutex_lock() (per cache or single ?) */
224
225    for ( ;; ) {
226        num = ngx_next_temp_number(collision);
227
228        ngx_sprintf(name + to->len, ".%0muA.DELETE%Z", num);
229
230        if (MoveFile((const char *) to->data, (const char *) name) != 0) {
231            break;
232        }
233
234        collision = 1;
235
236        ngx_log_error(NGX_LOG_CRIT, log, ngx_errno,
237                      "MoveFile() \"%s\" to \"%s\" failed", to->data, name);
238    }
239
240    if (MoveFile((const char *) from->data, (const char *) to->data) == 0) {
241        err = ngx_errno;
242
243    } else {
244        err = 0;
245    }
246
247    if (DeleteFile((const char *) name) == 0) {
248        ngx_log_error(NGX_LOG_CRIT, log, ngx_errno,
249                      "DeleteFile() \"%s\" failed", name);
250    }
251
252    /* mutex_unlock() */
253
254    ngx_free(name);
255
256    return err;
257}
258
259
260ngx_int_t
261ngx_file_info(u_char *file, ngx_file_info_t *sb)
262{
263    size_t                      len;
264    long                        rc;
265    u_short                    *u;
266    ngx_err_t                   err;
267    WIN32_FILE_ATTRIBUTE_DATA   fa;
268    u_short                     utf16[NGX_UTF16_BUFLEN];
269
270    len = NGX_UTF16_BUFLEN;
271
272    u = ngx_utf8_to_utf16(utf16, file, &len);
273
274    if (u == NULL) {
275        return NGX_FILE_ERROR;
276    }
277
278    rc = NGX_FILE_ERROR;
279
280    if (ngx_win32_check_filename(file, u, len) != NGX_OK) {
281        goto failed;
282    }
283
284    rc = GetFileAttributesExW(u, GetFileExInfoStandard, &fa);
285
286    sb->dwFileAttributes = fa.dwFileAttributes;
287    sb->ftCreationTime = fa.ftCreationTime;
288    sb->ftLastAccessTime = fa.ftLastAccessTime;
289    sb->ftLastWriteTime = fa.ftLastWriteTime;
290    sb->nFileSizeHigh = fa.nFileSizeHigh;
291    sb->nFileSizeLow = fa.nFileSizeLow;
292
293failed:
294
295    if (u != utf16) {
296        err = ngx_errno;
297        ngx_free(u);
298        ngx_set_errno(err);
299    }
300
301    return rc;
302}
303
304
305ngx_int_t
306ngx_set_file_time(u_char *name, ngx_fd_t fd, time_t s)
307{
308    uint64_t  intervals;
309    FILETIME  ft;
310
311    /* 116444736000000000 is commented in src/os/win32/ngx_time.c */
312
313    intervals = s * 10000000 + 116444736000000000;
314
315    ft.dwLowDateTime = (DWORD) intervals;
316    ft.dwHighDateTime = (DWORD) (intervals >> 32);
317
318    if (SetFileTime(fd, NULL, NULL, &ft) != 0) {
319        return NGX_OK;
320    }
321
322    return NGX_ERROR;
323}
324
325
326ngx_int_t
327ngx_create_file_mapping(ngx_file_mapping_t *fm)
328{
329    LARGE_INTEGER  size;
330
331    fm->fd = ngx_open_file(fm->name, NGX_FILE_RDWR, NGX_FILE_TRUNCATE,
332                           NGX_FILE_DEFAULT_ACCESS);
333    if (fm->fd == NGX_INVALID_FILE) {
334        ngx_log_error(NGX_LOG_CRIT, fm->log, ngx_errno,
335                      ngx_open_file_n " \"%s\" failed", fm->name);
336        return NGX_ERROR;
337    }
338
339    fm->handle = NULL;
340
341    size.QuadPart = fm->size;
342
343    if (SetFilePointerEx(fm->fd, size, NULL, FILE_BEGIN) == 0) {
344        ngx_log_error(NGX_LOG_CRIT, fm->log, ngx_errno,
345                      "SetFilePointerEx(\"%s\", %uz) failed",
346                      fm->name, fm->size);
347        goto failed;
348    }
349
350    if (SetEndOfFile(fm->fd) == 0) {
351        ngx_log_error(NGX_LOG_CRIT, fm->log, ngx_errno,
352                      "SetEndOfFile() \"%s\" failed", fm->name);
353        goto failed;
354    }
355
356    fm->handle = CreateFileMapping(fm->fd, NULL, PAGE_READWRITE,
357                                   (u_long) ((off_t) fm->size >> 32),
358                                   (u_long) ((off_t) fm->size & 0xffffffff),
359                                   NULL);
360    if (fm->handle == NULL) {
361        ngx_log_error(NGX_LOG_CRIT, fm->log, ngx_errno,
362                      "CreateFileMapping(%s, %uz) failed",
363                      fm->name, fm->size);
364        goto failed;
365    }
366
367    fm->addr = MapViewOfFile(fm->handle, FILE_MAP_WRITE, 0, 0, 0);
368
369    if (fm->addr != NULL) {
370        return NGX_OK;
371    }
372
373    ngx_log_error(NGX_LOG_CRIT, fm->log, ngx_errno,
374                  "MapViewOfFile(%uz) of file mapping \"%s\" failed",
375                  fm->size, fm->name);
376
377failed:
378
379    if (fm->handle) {
380        if (CloseHandle(fm->handle) == 0) {
381            ngx_log_error(NGX_LOG_ALERT, fm->log, ngx_errno,
382                          "CloseHandle() of file mapping \"%s\" failed",
383                          fm->name);
384        }
385    }
386
387    if (ngx_close_file(fm->fd) == NGX_FILE_ERROR) {
388        ngx_log_error(NGX_LOG_ALERT, fm->log, ngx_errno,
389                      ngx_close_file_n " \"%s\" failed", fm->name);
390    }
391
392    return NGX_ERROR;
393}
394
395
396void
397ngx_close_file_mapping(ngx_file_mapping_t *fm)
398{
399    if (UnmapViewOfFile(fm->addr) == 0) {
400        ngx_log_error(NGX_LOG_ALERT, fm->log, ngx_errno,
401                      "UnmapViewOfFile(%p) of file mapping \"%s\" failed",
402                      fm->addr, &fm->name);
403    }
404
405    if (CloseHandle(fm->handle) == 0) {
406        ngx_log_error(NGX_LOG_ALERT, fm->log, ngx_errno,
407                      "CloseHandle() of file mapping \"%s\" failed",
408                      &fm->name);
409    }
410
411    if (ngx_close_file(fm->fd) == NGX_FILE_ERROR) {
412        ngx_log_error(NGX_LOG_ALERT, fm->log, ngx_errno,
413                      ngx_close_file_n " \"%s\" failed", fm->name);
414    }
415}
416
417
418u_char *
419ngx_realpath(u_char *path, u_char *resolved)
420{
421    /* STUB */
422    return path;
423}
424
425
426ngx_int_t
427ngx_open_dir(ngx_str_t *name, ngx_dir_t *dir)
428{
429    ngx_cpystrn(name->data + name->len, NGX_DIR_MASK, NGX_DIR_MASK_LEN + 1);
430
431    dir->dir = FindFirstFile((const char *) name->data, &dir->finddata);
432
433    name->data[name->len] = '\0';
434
435    if (dir->dir == INVALID_HANDLE_VALUE) {
436        return NGX_ERROR;
437    }
438
439    dir->valid_info = 1;
440    dir->ready = 1;
441
442    return NGX_OK;
443}
444
445
446ngx_int_t
447ngx_read_dir(ngx_dir_t *dir)
448{
449    if (dir->ready) {
450        dir->ready = 0;
451        return NGX_OK;
452    }
453
454    if (FindNextFile(dir->dir, &dir->finddata) != 0) {
455        dir->type = 1;
456        return NGX_OK;
457    }
458
459    return NGX_ERROR;
460}
461
462
463ngx_int_t
464ngx_close_dir(ngx_dir_t *dir)
465{
466    if (FindClose(dir->dir) == 0) {
467        return NGX_ERROR;
468    }
469
470    return NGX_OK;
471}
472
473
474ngx_int_t
475ngx_open_glob(ngx_glob_t *gl)
476{
477    u_char     *p;
478    size_t      len;
479    ngx_err_t   err;
480
481    gl->dir = FindFirstFile((const char *) gl->pattern, &gl->finddata);
482
483    if (gl->dir == INVALID_HANDLE_VALUE) {
484
485        err = ngx_errno;
486
487        if ((err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND)
488             && gl->test)
489        {
490            gl->no_match = 1;
491            return NGX_OK;
492        }
493
494        return NGX_ERROR;
495    }
496
497    for (p = gl->pattern; *p; p++) {
498        if (*p == '/') {
499            gl->last = p + 1 - gl->pattern;
500        }
501    }
502
503    len = ngx_strlen(gl->finddata.cFileName);
504    gl->name.len = gl->last + len;
505
506    gl->name.data = ngx_alloc(gl->name.len + 1, gl->log);
507    if (gl->name.data == NULL) {
508        return NGX_ERROR;
509    }
510
511    ngx_memcpy(gl->name.data, gl->pattern, gl->last);
512    ngx_cpystrn(gl->name.data + gl->last, (u_char *) gl->finddata.cFileName,
513                len + 1);
514
515    gl->ready = 1;
516
517    return NGX_OK;
518}
519
520
521ngx_int_t
522ngx_read_glob(ngx_glob_t *gl, ngx_str_t *name)
523{
524    size_t     len;
525    ngx_err_t  err;
526
527    if (gl->no_match) {
528        return NGX_DONE;
529    }
530
531    if (gl->ready) {
532        *name = gl->name;
533
534        gl->ready = 0;
535        return NGX_OK;
536    }
537
538    ngx_free(gl->name.data);
539    gl->name.data = NULL;
540
541    if (FindNextFile(gl->dir, &gl->finddata) != 0) {
542
543        len = ngx_strlen(gl->finddata.cFileName);
544        gl->name.len = gl->last + len;
545
546        gl->name.data = ngx_alloc(gl->name.len + 1, gl->log);
547        if (gl->name.data == NULL) {
548            return NGX_ERROR;
549        }
550
551        ngx_memcpy(gl->name.data, gl->pattern, gl->last);
552        ngx_cpystrn(gl->name.data + gl->last, (u_char *) gl->finddata.cFileName,
553                    len + 1);
554
555        *name = gl->name;
556
557        return NGX_OK;
558    }
559
560    err = ngx_errno;
561
562    if (err == NGX_ENOMOREFILES) {
563        return NGX_DONE;
564    }
565
566    ngx_log_error(NGX_LOG_ALERT, gl->log, err,
567                  "FindNextFile(%s) failed", gl->pattern);
568
569    return NGX_ERROR;
570}
571
572
573void
574ngx_close_glob(ngx_glob_t *gl)
575{
576    if (gl->name.data) {
577        ngx_free(gl->name.data);
578    }
579
580    if (gl->dir == INVALID_HANDLE_VALUE) {
581        return;
582    }
583
584    if (FindClose(gl->dir) == 0) {
585        ngx_log_error(NGX_LOG_ALERT, gl->log, ngx_errno,
586                      "FindClose(%s) failed", gl->pattern);
587    }
588}
589
590
591ngx_int_t
592ngx_de_info(u_char *name, ngx_dir_t *dir)
593{
594    return NGX_OK;
595}
596
597
598ngx_int_t
599ngx_de_link_info(u_char *name, ngx_dir_t *dir)
600{
601    return NGX_OK;
602}
603
604
605ngx_int_t
606ngx_read_ahead(ngx_fd_t fd, size_t n)
607{
608    return ~NGX_FILE_ERROR;
609}
610
611
612ngx_int_t
613ngx_directio_on(ngx_fd_t fd)
614{
615    return ~NGX_FILE_ERROR;
616}
617
618
619ngx_int_t
620ngx_directio_off(ngx_fd_t fd)
621{
622    return ~NGX_FILE_ERROR;
623}
624
625
626size_t
627ngx_fs_bsize(u_char *name)
628{
629    u_char  root[4];
630    u_long  sc, bs, nfree, ncl;
631
632    if (name[2] == ':') {
633        ngx_cpystrn(root, name, 4);
634        name = root;
635    }
636
637    if (GetDiskFreeSpace((const char *) name, &sc, &bs, &nfree, &ncl) == 0) {
638        return 512;
639    }
640
641    return sc * bs;
642}
643
644
645static ngx_int_t
646ngx_win32_check_filename(u_char *name, u_short *u, size_t len)
647{
648    u_char     *p, ch;
649    u_long      n;
650    u_short    *lu;
651    ngx_err_t   err;
652    enum {
653        sw_start = 0,
654        sw_normal,
655        sw_after_slash,
656        sw_after_colon,
657        sw_after_dot
658    } state;
659
660    /* check for NTFS streams (":"), trailing dots and spaces */
661
662    lu = NULL;
663    state = sw_start;
664
665    for (p = name; *p; p++) {
666        ch = *p;
667
668        switch (state) {
669
670        case sw_start:
671
672            /*
673             * skip till first "/" to allow paths starting with drive and
674             * relative path, like "c:html/"
675             */
676
677            if (ch == '/' || ch == '\\') {
678                state = sw_after_slash;
679            }
680
681            break;
682
683        case sw_normal:
684
685            if (ch == ':') {
686                state = sw_after_colon;
687                break;
688            }
689
690            if (ch == '.' || ch == ' ') {
691                state = sw_after_dot;
692                break;
693            }
694
695            if (ch == '/' || ch == '\\') {
696                state = sw_after_slash;
697                break;
698            }
699
700            break;
701
702        case sw_after_slash:
703
704            if (ch == '/' || ch == '\\') {
705                break;
706            }
707
708            if (ch == '.') {
709                break;
710            }
711
712            if (ch == ':') {
713                state = sw_after_colon;
714                break;
715            }
716
717            state = sw_normal;
718            break;
719
720        case sw_after_colon:
721
722            if (ch == '/' || ch == '\\') {
723                state = sw_after_slash;
724                break;
725            }
726
727            goto invalid;
728
729        case sw_after_dot:
730
731            if (ch == '/' || ch == '\\') {
732                goto invalid;
733            }
734
735            if (ch == ':') {
736                goto invalid;
737            }
738
739            if (ch == '.' || ch == ' ') {
740                break;
741            }
742
743            state = sw_normal;
744            break;
745        }
746    }
747
748    if (state == sw_after_dot) {
749        goto invalid;
750    }
751
752    /* check if long name match */
753
754    lu = malloc(len * 2);
755    if (lu == NULL) {
756        return NGX_ERROR;
757    }
758
759    n = GetLongPathNameW(u, lu, len);
760
761    if (n == 0) {
762        goto failed;
763    }
764
765    if (n != len - 1 || _wcsicmp(u, lu) != 0) {
766        goto invalid;
767    }
768
769    ngx_free(lu);
770
771    return NGX_OK;
772
773invalid:
774
775    ngx_set_errno(NGX_ENOENT);
776
777failed:
778
779    if (lu) {
780        err = ngx_errno;
781        ngx_free(lu);
782        ngx_set_errno(err);
783    }
784
785    return NGX_ERROR;
786}
787
788
789static u_short *
790ngx_utf8_to_utf16(u_short *utf16, u_char *utf8, size_t *len)
791{
792    u_char    *p;
793    u_short   *u, *last;
794    uint32_t   n;
795
796    p = utf8;
797    u = utf16;
798    last = utf16 + *len;
799
800    while (u < last) {
801
802        if (*p < 0x80) {
803            *u++ = (u_short) *p;
804
805            if (*p == 0) {
806                *len = u - utf16;
807                return utf16;
808            }
809
810            p++;
811
812            continue;
813        }
814
815        if (u + 1 == last) {
816            *len = u - utf16;
817            break;
818        }
819
820        n = ngx_utf8_decode(&p, 4);
821
822        if (n > 0x10ffff) {
823            ngx_set_errno(NGX_EILSEQ);
824            return NULL;
825        }
826
827        if (n > 0xffff) {
828            n -= 0x10000;
829            *u++ = (u_short) (0xd800 + (n >> 10));
830            *u++ = (u_short) (0xdc00 + (n & 0x03ff));
831            continue;
832        }
833
834        *u++ = (u_short) n;
835    }
836
837    /* the given buffer is not enough, allocate a new one */
838
839    u = malloc(((p - utf8) + ngx_strlen(p) + 1) * sizeof(u_short));
840    if (u == NULL) {
841        return NULL;
842    }
843
844    ngx_memcpy(u, utf16, *len * 2);
845
846    utf16 = u;
847    u += *len;
848
849    for ( ;; ) {
850
851        if (*p < 0x80) {
852            *u++ = (u_short) *p;
853
854            if (*p == 0) {
855                *len = u - utf16;
856                return utf16;
857            }
858
859            p++;
860
861            continue;
862        }
863
864        n = ngx_utf8_decode(&p, 4);
865
866        if (n > 0x10ffff) {
867            ngx_free(utf16);
868            ngx_set_errno(NGX_EILSEQ);
869            return NULL;
870        }
871
872        if (n > 0xffff) {
873            n -= 0x10000;
874            *u++ = (u_short) (0xd800 + (n >> 10));
875            *u++ = (u_short) (0xdc00 + (n & 0x03ff));
876            continue;
877        }
878
879        *u++ = (u_short) n;
880    }
881
882    /* unreachable */
883}
884