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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
|
/*
+----------------------------------------------------------------------+
| PHP Version 7 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2018 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Anatol Belski <ab@php.net> |
+----------------------------------------------------------------------+
*/
/* This file integrates several modified parts from the libuv project, which
* is copyrighted to
*
* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#ifndef PHP_WIN32_IOUTIL_H
#define PHP_WIN32_IOUTIL_H
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
#include "win32/winutil.h"
#include "win32/codepage.h"
#ifdef __cplusplus
extern "C" {
#endif
#ifdef PHP_EXPORTS
# define PW32IO __declspec(dllexport)
#else
# define PW32IO __declspec(dllimport)
#endif
#define PHP_WIN32_IOUTIL_MAXPATHLEN 2048
#if !defined(MAXPATHLEN) || MAXPATHLEN < PHP_WIN32_IOUTIL_MAXPATHLEN
# undef MAXPATHLEN
# define MAXPATHLEN PHP_WIN32_IOUTIL_MAXPATHLEN
#endif
#ifndef mode_t
typedef unsigned short mode_t;
#endif
/* these are not defined in win32 headers */
#ifndef W_OK
#define W_OK 0x02
#endif
#ifndef R_OK
#define R_OK 0x04
#endif
#ifndef X_OK
#define X_OK 0x01
#endif
#ifndef F_OK
#define F_OK 0x00
#endif
typedef struct {
DWORD access;
DWORD share;
DWORD disposition;
DWORD attributes;
} php_ioutil_open_opts;
typedef enum {
PHP_WIN32_IOUTIL_IS_ASCII,
PHP_WIN32_IOUTIL_IS_ANSI,
PHP_WIN32_IOUTIL_IS_UTF8
} php_win32_ioutil_encoding;
typedef enum {
PHP_WIN32_IOUTIL_NORM_OK,
PHP_WIN32_IOUTIL_NORM_PARTIAL,
PHP_WIN32_IOUTIL_NORM_FAIL,
} php_win32_ioutil_normalization_result;
#define PHP_WIN32_IOUTIL_FW_SLASHW L'/'
#define PHP_WIN32_IOUTIL_FW_SLASH '/'
#define PHP_WIN32_IOUTIL_BW_SLASHW L'\\'
#define PHP_WIN32_IOUTIL_BW_SLASH '\\'
#define PHP_WIN32_IOUTIL_DEFAULT_SLASHW PHP_WIN32_IOUTIL_BW_SLASHW
#define PHP_WIN32_IOUTIL_DEFAULT_SLASH PHP_WIN32_IOUTIL_BW_SLASH
#define PHP_WIN32_IOUTIL_DEFAULT_DIR_SEPARATORW L';'
#define PHP_WIN32_IOUTIL_IS_SLASHW(c) ((c) == PHP_WIN32_IOUTIL_BW_SLASHW || (c) == PHP_WIN32_IOUTIL_FW_SLASHW)
#define PHP_WIN32_IOUTIL_IS_LETTERW(c) (((c) >= L'a' && (c) <= L'z') || ((c) >= L'A' && (c) <= L'Z'))
#define PHP_WIN32_IOUTIL_JUNCTION_PREFIXW L"\\??\\"
#define PHP_WIN32_IOUTIL_JUNCTION_PREFIX_LENW 4
#define PHP_WIN32_IOUTIL_LONG_PATH_PREFIXW L"\\\\?\\"
#define PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW 4
#define PHP_WIN32_IOUTIL_UNC_PATH_PREFIXW L"\\\\?\\UNC\\"
#define PHP_WIN32_IOUTIL_UNC_PATH_PREFIX_LENW 8
#define PHP_WIN32_IOUTIL_IS_LONG_PATHW(pathw, path_lenw) (path_lenw >= PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW \
&& 0 == wcsncmp((pathw), PHP_WIN32_IOUTIL_LONG_PATH_PREFIXW, PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW))
#define PHP_WIN32_IOUTIL_IS_UNC_PATHW(pathw, path_lenw) (path_lenw >= PHP_WIN32_IOUTIL_UNC_PATH_PREFIX_LENW \
&& 0 == wcsncmp((pathw), PHP_WIN32_IOUTIL_UNC_PATH_PREFIXW, PHP_WIN32_IOUTIL_UNC_PATH_PREFIX_LENW))
#define PHP_WIN32_IOUTIL_IS_JUNCTION_PATHW(pathw, path_lenw) (path_lenw >= PHP_WIN32_IOUTIL_JUNCTION_PREFIX_LENW \
&& 0 == wcsncmp((pathw), PHP_WIN32_IOUTIL_JUNCTION_PREFIXW, PHP_WIN32_IOUTIL_JUNCTION_PREFIX_LENW))
#define PHP_WIN32_IOUTIL_IS_ABSOLUTEW(pathw, path_lenw) (PHP_WIN32_IOUTIL_IS_LONG_PATHW(pathw, path_lenw) \
|| path_lenw >= 3 && PHP_WIN32_IOUTIL_IS_LETTERW(pathw[0]) && L':' == pathw[1] && PHP_WIN32_IOUTIL_IS_SLASHW(pathw[2]))
#define PHP_WIN32_IOUTIL_IS_UNC(pathw, path_lenw) (path_lenw >= 2 && PHP_WIN32_IOUTIL_IS_SLASHW(pathw[0]) && PHP_WIN32_IOUTIL_IS_SLASHW(pathw[1]) \
|| path_lenw >= PHP_WIN32_IOUTIL_UNC_PATH_PREFIX_LENW && 0 == wcsncmp((pathw), PHP_WIN32_IOUTIL_UNC_PATH_PREFIXW, PHP_WIN32_IOUTIL_UNC_PATH_PREFIX_LENW))
#define PHP_WIN32_IOUTIL_DEFAULT_SHARE_MODE (FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE)
#define PHP_WIN32_IOUTIL_INIT_W(path) \
wchar_t *pathw = php_win32_ioutil_any_to_w(path); \
#define PHP_WIN32_IOUTIL_CLEANUP_W() do { \
free(pathw); \
pathw = NULL; \
} while (0);
#define PHP_WIN32_IOUTIL_REINIT_W(path) do { \
PHP_WIN32_IOUTIL_CLEANUP_W() \
pathw = php_win32_ioutil_any_to_w(path); \
} while (0);
#define PHP_WIN32_IOUTIL_PATH_IS_OK_W(pathw, len) \
(!((len) >= 1 && L' ' == pathw[(len)-1] || \
(len) > 1 && !PHP_WIN32_IOUTIL_IS_SLASHW(pathw[(len)-2]) && L'.' != pathw[(len)-2] && L'.' == pathw[(len)-1]))
#define PHP_WIN32_IOUTIL_CHECK_PATH_W(pathw, ret, dealloc) do { \
size_t _len = wcslen(pathw); \
if (!PHP_WIN32_IOUTIL_PATH_IS_OK_W(pathw, _len)) { \
if (dealloc) { \
free((void *)pathw); \
} \
SET_ERRNO_FROM_WIN32_CODE(ERROR_ACCESS_DENIED); \
return ret; \
} \
} while (0);
PW32IO php_win32_ioutil_normalization_result php_win32_ioutil_normalize_path_w(wchar_t **buf, size_t len, size_t *new_len);
#ifdef PHP_EXPORTS
/* This symbols are needed only for the DllMain, but should not be exported
or be available when used with PHP binaries. */
BOOL php_win32_ioutil_init(void);
#endif
/* Keep these functions aliased for case some additional handling
is needed later. */
__forceinline static wchar_t *php_win32_ioutil_conv_any_to_w(const char* in, size_t in_len, size_t *out_len)
{/*{{{*/
wchar_t *mb, *ret;
size_t mb_len;
mb = php_win32_cp_conv_any_to_w(in, in_len, &mb_len);
if (!mb) {
return NULL;
}
/* Only prefix with long if it's needed. */
if (mb_len >= _MAX_PATH) {
size_t new_mb_len;
ret = (wchar_t *) malloc((mb_len + PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW + 1) * sizeof(wchar_t));
if (!ret) {
free(mb);
return NULL;
}
if (PHP_WIN32_IOUTIL_NORM_FAIL == php_win32_ioutil_normalize_path_w(&mb, mb_len, &new_mb_len)) {
free(ret);
free(mb);
return NULL;
}
if (new_mb_len > mb_len) {
wchar_t *tmp = (wchar_t *) realloc(ret, (new_mb_len + 1) * sizeof(wchar_t));
if (!tmp) {
free(ret);
free(mb);
return NULL;
}
ret = tmp;
mb_len = new_mb_len;
}
if (PHP_WIN32_IOUTIL_IS_LONG_PATHW(mb, mb_len) || PHP_WIN32_IOUTIL_IS_JUNCTION_PATHW(mb, mb_len) || PHP_WIN32_IOUTIL_IS_UNC_PATHW(mb, mb_len)) {
memmove(ret, mb, mb_len * sizeof(wchar_t));
ret[mb_len] = L'\0';
} else {
memmove(ret, PHP_WIN32_IOUTIL_LONG_PATH_PREFIXW, PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW * sizeof(wchar_t));
memmove(ret+PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW, mb, mb_len * sizeof(wchar_t));
ret[mb_len + PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW] = L'\0';
mb_len += PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW;
}
free(mb);
} else {
ret = mb;
}
if (PHP_WIN32_CP_IGNORE_LEN_P != out_len) {
*out_len = mb_len;
}
return ret;
}/*}}}*/
#define php_win32_ioutil_any_to_w(in) php_win32_ioutil_conv_any_to_w(in, PHP_WIN32_CP_IGNORE_LEN, PHP_WIN32_CP_IGNORE_LEN_P)
#define php_win32_ioutil_ascii_to_w php_win32_cp_ascii_to_w
#define php_win32_ioutil_utf8_to_w php_win32_cp_utf8_to_w
#define php_win32_ioutil_cur_to_w php_win32_cp_cur_to_w
#define php_win32_ioutil_w_to_any php_win32_cp_w_to_any
#define php_win32_ioutil_conv_w_to_any php_win32_cp_conv_w_to_any
/*__forceinline static char *php_win32_ioutil_w_to_any(wchar_t* w_source_ptr)
{
return php_win32_cp_w_to_any(w_source_ptr);
}*/
#define php_win32_ioutil_w_to_utf8 php_win32_cp_w_to_utf8
#define php_win32_ioutil_w_to_thread php_win32_cp_w_to_thread
PW32IO int php_win32_ioutil_close(int fd);
PW32IO BOOL php_win32_ioutil_posix_to_open_opts(int flags, mode_t mode, php_ioutil_open_opts *opts);
PW32IO size_t php_win32_ioutil_dirname(char *buf, size_t len);
PW32IO int php_win32_ioutil_open_w(const wchar_t *path, int flags, ...);
PW32IO int php_win32_ioutil_chdir_w(const wchar_t *path);
PW32IO int php_win32_ioutil_rename_w(const wchar_t *oldname, const wchar_t *newname);
PW32IO wchar_t *php_win32_ioutil_getcwd_w(wchar_t *buf, size_t len);
PW32IO int php_win32_ioutil_unlink_w(const wchar_t *path);
PW32IO int php_win32_ioutil_access_w(const wchar_t *path, mode_t mode);
PW32IO int php_win32_ioutil_mkdir_w(const wchar_t *path, mode_t mode);
PW32IO FILE *php_win32_ioutil_fopen_w(const wchar_t *path, const wchar_t *mode);
PW32IO wchar_t *php_win32_ioutil_realpath_w(const wchar_t *path, wchar_t *resolved);
PW32IO wchar_t *php_win32_ioutil_realpath_w_ex0(const wchar_t *path, wchar_t *resolved, PBY_HANDLE_FILE_INFORMATION info);
PW32IO int php_win32_ioutil_symlink_w(const wchar_t *target, const wchar_t *link);
PW32IO int php_win32_ioutil_link_w(const wchar_t *target, const wchar_t *link);
__forceinline static int php_win32_ioutil_access(const char *path, mode_t mode)
{/*{{{*/
PHP_WIN32_IOUTIL_INIT_W(path)
int ret, err;
if (!pathw) {
SET_ERRNO_FROM_WIN32_CODE(ERROR_INVALID_PARAMETER);
return -1;
}
PHP_WIN32_IOUTIL_CHECK_PATH_W(pathw, -1, 1)
ret = php_win32_ioutil_access_w(pathw, mode);
if (0 > ret) {
err = GetLastError();
}
PHP_WIN32_IOUTIL_CLEANUP_W()
if (0 > ret) {
SET_ERRNO_FROM_WIN32_CODE(err);
}
return ret;
}/*}}}*/
__forceinline static int php_win32_ioutil_open(const char *path, int flags, ...)
{/*{{{*/
mode_t mode = 0;
PHP_WIN32_IOUTIL_INIT_W(path)
int ret = -1;
DWORD err;
if (!pathw) {
SET_ERRNO_FROM_WIN32_CODE(ERROR_INVALID_PARAMETER);
return -1;
}
PHP_WIN32_IOUTIL_CHECK_PATH_W(pathw, -1, 1)
if (flags & O_CREAT) {
va_list arg;
va_start(arg, flags);
mode = (mode_t) va_arg(arg, int);
va_end(arg);
}
ret = php_win32_ioutil_open_w(pathw, flags, mode);
if (0 > ret) {
err = GetLastError();
}
PHP_WIN32_IOUTIL_CLEANUP_W()
if (0 > ret) {
SET_ERRNO_FROM_WIN32_CODE(err);
}
return ret;
}/*}}}*/
__forceinline static int php_win32_ioutil_unlink(const char *path)
{/*{{{*/
PHP_WIN32_IOUTIL_INIT_W(path)
int ret = -1;
DWORD err;
if (!pathw) {
SET_ERRNO_FROM_WIN32_CODE(ERROR_INVALID_PARAMETER);
return -1;
}
ret = php_win32_ioutil_unlink_w(pathw);
if (0 > ret) {
err = GetLastError();
}
PHP_WIN32_IOUTIL_CLEANUP_W()
if (0 > ret) {
SET_ERRNO_FROM_WIN32_CODE(err);
}
return ret;
}/*}}}*/
__forceinline static int php_win32_ioutil_rmdir(const char *path)
{/*{{{*/
PHP_WIN32_IOUTIL_INIT_W(path)
int ret = 0;
DWORD err = 0;
if (!pathw) {
SET_ERRNO_FROM_WIN32_CODE(ERROR_INVALID_PARAMETER);
return -1;
}
PHP_WIN32_IOUTIL_CHECK_PATH_W(pathw, -1, 1)
if (!RemoveDirectoryW(pathw)) {
err = GetLastError();
ret = -1;
}
PHP_WIN32_IOUTIL_CLEANUP_W()
if (0 > ret) {
SET_ERRNO_FROM_WIN32_CODE(err);
}
return ret;
}/*}}}*/
__forceinline static FILE *php_win32_ioutil_fopen(const char *patha, const char *modea)
{/*{{{*/
FILE *ret;
wchar_t modew[16] = {0};
int i = 0;
PHP_WIN32_IOUTIL_INIT_W(patha)
if (!pathw) {
SET_ERRNO_FROM_WIN32_CODE(ERROR_INVALID_PARAMETER);
return NULL;
}
PHP_WIN32_IOUTIL_CHECK_PATH_W(pathw, NULL, 1)
while (i < (sizeof(modew)-1)/sizeof(wchar_t) && modea[i]) {
modew[i] = (wchar_t)modea[i];
i++;
}
ret = php_win32_ioutil_fopen_w(pathw, modew);
if (!ret) {
int err = GetLastError();
PHP_WIN32_IOUTIL_CLEANUP_W()
SET_ERRNO_FROM_WIN32_CODE(err);
return NULL;
}
PHP_WIN32_IOUTIL_CLEANUP_W()
return ret;
}/*}}}*/
__forceinline static int php_win32_ioutil_rename(const char *oldnamea, const char *newnamea)
{/*{{{*/
wchar_t *oldnamew;
wchar_t *newnamew;
int ret;
DWORD err = 0;
oldnamew = php_win32_ioutil_any_to_w(oldnamea);
if (!oldnamew) {
SET_ERRNO_FROM_WIN32_CODE(ERROR_INVALID_PARAMETER);
return -1;
}
PHP_WIN32_IOUTIL_CHECK_PATH_W(oldnamew, -1, 1)
newnamew = php_win32_ioutil_any_to_w(newnamea);
if (!newnamew) {
free(oldnamew);
SET_ERRNO_FROM_WIN32_CODE(ERROR_INVALID_PARAMETER);
return -1;
} else {
size_t newnamew_len = wcslen(newnamew);
if (!PHP_WIN32_IOUTIL_PATH_IS_OK_W(newnamew, newnamew_len)) {
free(oldnamew);
free(newnamew);
SET_ERRNO_FROM_WIN32_CODE(ERROR_ACCESS_DENIED);
return -1;
}
}
ret = php_win32_ioutil_rename_w(oldnamew, newnamew);
if (0 > ret) {
err = GetLastError();
}
free(oldnamew);
free(newnamew);
if (0 > ret) {
SET_ERRNO_FROM_WIN32_CODE(err);
}
return ret;
}/*}}}*/
__forceinline static int php_win32_ioutil_chdir(const char *patha)
{/*{{{*/
int ret;
wchar_t *pathw = php_win32_ioutil_any_to_w(patha);
DWORD err = 0;
if (!pathw) {
SET_ERRNO_FROM_WIN32_CODE(ERROR_INVALID_PARAMETER);
return -1;
}
ret = php_win32_ioutil_chdir_w(pathw);
if (0 > ret) {
err = GetLastError();
}
free(pathw);
if (0 > ret) {
SET_ERRNO_FROM_WIN32_CODE(err);
}
return ret;
}/*}}}*/
__forceinline static char *php_win32_ioutil_getcwd(char *buf, size_t len)
{/*{{{*/
wchar_t tmp_bufw[PHP_WIN32_IOUTIL_MAXPATHLEN];
char *tmp_bufa = NULL;
size_t tmp_bufa_len;
DWORD err = 0;
if (php_win32_ioutil_getcwd_w(tmp_bufw, len ? len : PHP_WIN32_IOUTIL_MAXPATHLEN) == NULL) {
err = GetLastError();
SET_ERRNO_FROM_WIN32_CODE(err);
return NULL;
}
tmp_bufa = php_win32_cp_conv_w_to_any(tmp_bufw, wcslen(tmp_bufw), &tmp_bufa_len);
if (!tmp_bufa) {
err = GetLastError();
SET_ERRNO_FROM_WIN32_CODE(err);
return NULL;
} else if (tmp_bufa_len + 1 > PHP_WIN32_IOUTIL_MAXPATHLEN) {
free(tmp_bufa);
SET_ERRNO_FROM_WIN32_CODE(ERROR_BAD_LENGTH);
return NULL;
} else if (tmp_bufa_len + 1 > len) {
free(tmp_bufa);
SET_ERRNO_FROM_WIN32_CODE(ERROR_INSUFFICIENT_BUFFER);
return NULL;
}
if (!buf && !len) {
/* If buf was NULL, the result has to be freed outside here. */
buf = tmp_bufa;
} else {
memmove(buf, tmp_bufa, tmp_bufa_len + 1);
free(tmp_bufa);
}
return buf;
}/*}}}*/
/* TODO improve with usage of native APIs, split for _a and _w. */
__forceinline static int php_win32_ioutil_chmod(const char *patha, int mode)
{/*{{{*/
wchar_t *pathw = php_win32_ioutil_any_to_w(patha);
int err = 0;
int ret;
if (!pathw) {
SET_ERRNO_FROM_WIN32_CODE(ERROR_INVALID_PARAMETER);
return -1;
}
PHP_WIN32_IOUTIL_CHECK_PATH_W(pathw, -1, 1)
ret = _wchmod(pathw, mode);
if (0 > ret) {
_get_errno(&err);
}
free(pathw);
if (0 > ret) {
_set_errno(err);
}
return ret;
}/*}}}*/
__forceinline static int php_win32_ioutil_mkdir(const char *path, mode_t mode)
{/*{{{*/
int ret;
DWORD err = 0;
PHP_WIN32_IOUTIL_INIT_W(path)
if (!pathw) {
SET_ERRNO_FROM_WIN32_CODE(ERROR_INVALID_PARAMETER);
return -1;
}
ret = php_win32_ioutil_mkdir_w(pathw, mode);
if (0 > ret) {
err = GetLastError();
}
PHP_WIN32_IOUTIL_CLEANUP_W()
if (0 > ret) {
SET_ERRNO_FROM_WIN32_CODE(err);
}
return ret;
}/*}}}*/
__forceinline static int php_win32_ioutil_symlink(const char *target, const char *link)
{/*{{{*/
wchar_t *targetw, *linkw;
int ret;
targetw = php_win32_ioutil_any_to_w(target);
if (!targetw) {
SET_ERRNO_FROM_WIN32_CODE(ERROR_INVALID_PARAMETER);
return -1;
}
linkw = php_win32_ioutil_any_to_w(link);
if (!linkw) {
free(targetw);
SET_ERRNO_FROM_WIN32_CODE(ERROR_INVALID_PARAMETER);
return -1;
}
ret = php_win32_ioutil_symlink_w(targetw, linkw);
free(targetw);
free(linkw);
return ret;
}/*}}}*/
__forceinline static int php_win32_ioutil_link(const char *target, const char *link)
{/*{{{*/
wchar_t *targetw, *linkw;
int ret;
targetw = php_win32_ioutil_any_to_w(target);
if (!targetw) {
SET_ERRNO_FROM_WIN32_CODE(ERROR_INVALID_PARAMETER);
return -1;
}
linkw = php_win32_ioutil_any_to_w(link);
if (!linkw) {
free(targetw);
SET_ERRNO_FROM_WIN32_CODE(ERROR_INVALID_PARAMETER);
return -1;
}
ret = php_win32_ioutil_link_w(targetw, linkw);
free(targetw);
free(linkw);
return ret;
}/*}}}*/
#define HAVE_REALPATH 1
PW32IO char *realpath(const char *path, char *resolved);
__forceinline static char *php_win32_ioutil_realpath_ex0(const char *path, char *resolved, PBY_HANDLE_FILE_INFORMATION info)
{/*{{{*/
wchar_t retw[PHP_WIN32_IOUTIL_MAXPATHLEN];
char *reta;
size_t reta_len;
PHP_WIN32_IOUTIL_INIT_W(path)
if (!pathw) {
SET_ERRNO_FROM_WIN32_CODE(ERROR_INVALID_PARAMETER);
return NULL;
}
if (NULL == php_win32_ioutil_realpath_w_ex0(pathw, retw, info)) {
DWORD err = GetLastError();
PHP_WIN32_IOUTIL_CLEANUP_W()
SET_ERRNO_FROM_WIN32_CODE(err);
return NULL;
}
reta = php_win32_cp_conv_w_to_any(retw, PHP_WIN32_CP_IGNORE_LEN, &reta_len);
if (!reta || reta_len > PHP_WIN32_IOUTIL_MAXPATHLEN) {
DWORD err = GetLastError();
PHP_WIN32_IOUTIL_CLEANUP_W()
SET_ERRNO_FROM_WIN32_CODE(err);
return NULL;
}
if (NULL == resolved) {
/* ret is expected to be either NULL or a buffer of capable size. */
resolved = (char *) malloc(reta_len + 1);
if (!resolved) {
free(reta);
PHP_WIN32_IOUTIL_CLEANUP_W()
SET_ERRNO_FROM_WIN32_CODE(ERROR_NOT_ENOUGH_MEMORY);
return NULL;
}
}
memmove(resolved, reta, reta_len+1);
PHP_WIN32_IOUTIL_CLEANUP_W()
free(reta);
return resolved;
}/*}}}*/
__forceinline static char *php_win32_ioutil_realpath(const char *path, char *resolved)
{/*{{{*/
return php_win32_ioutil_realpath_ex0(path, resolved, NULL);
}/*}}}*/
#include <sys/stat.h>
#if _WIN64
typedef unsigned __int64 php_win32_ioutil_dev_t;
typedef unsigned __int64 php_win32_ioutil_ino_t;
typedef __time64_t php_win32_ioutil_time_t;
typedef __int64 php_win32_ioutil_size_t;
#else
typedef unsigned __int32 php_win32_ioutil_dev_t;
typedef unsigned __int32 php_win32_ioutil_ino_t;
typedef __time32_t php_win32_ioutil_time_t;
typedef __int32 php_win32_ioutil_size_t;
#endif
typedef struct {
php_win32_ioutil_dev_t st_dev;
php_win32_ioutil_ino_t st_ino;
unsigned __int32 st_mode;
unsigned __int32 st_nlink;
unsigned short st_uid;
unsigned short st_gid;
php_win32_ioutil_dev_t st_rdev;
php_win32_ioutil_size_t st_size;
#if 0
__int32 st_blksize;
__int32 st_blocks;
#endif
php_win32_ioutil_time_t st_atime;
php_win32_ioutil_time_t st_mtime;
php_win32_ioutil_time_t st_ctime;
} php_win32_ioutil_stat_t;
typedef struct {
unsigned long ReparseTag;
unsigned short ReparseDataLength;
unsigned short Reserved;
union {
struct {
unsigned short SubstituteNameOffset;
unsigned short SubstituteNameLength;
unsigned short PrintNameOffset;
unsigned short PrintNameLength;
unsigned long Flags;
wchar_t ReparseTarget[1];
} SymbolicLinkReparseBuffer;
struct {
unsigned short SubstituteNameOffset;
unsigned short SubstituteNameLength;
unsigned short PrintNameOffset;
unsigned short PrintNameLength;
wchar_t ReparseTarget[1];
} MountPointReparseBuffer;
struct {
unsigned char ReparseTarget[1];
} GenericReparseBuffer;
};
} PHP_WIN32_IOUTIL_REPARSE_DATA_BUFFER, *PHP_WIN32_IOUTIL_PREPARSE_DATA_BUFFER;
PW32IO int php_win32_ioutil_stat_ex_w(const wchar_t *path, size_t path_len, php_win32_ioutil_stat_t *buf, int lstat);
PW32IO int php_win32_ioutil_fstat(int fd, php_win32_ioutil_stat_t *buf);
__forceinline static int php_win32_ioutil_stat_ex(const char *path, php_win32_ioutil_stat_t *buf, int lstat)
{/*{{{*/
size_t pathw_len;
wchar_t *pathw = php_win32_ioutil_conv_any_to_w(path, PHP_WIN32_CP_IGNORE_LEN, &pathw_len);
int ret;
if (!pathw) {
SET_ERRNO_FROM_WIN32_CODE(ERROR_INVALID_PARAMETER);
return -1;
}
ret = php_win32_ioutil_stat_ex_w(pathw, pathw_len, buf, lstat);
free(pathw);
return ret;
}/*}}}*/
#define php_win32_ioutil_stat(path, buf) php_win32_ioutil_stat_ex(path, buf, 0)
#define php_win32_ioutil_lstat(path, buf) php_win32_ioutil_stat_ex(path, buf, 1)
PW32IO ssize_t php_win32_ioutil_readlink_w(const wchar_t *path, wchar_t *buf, size_t buf_len);
__forceinline static ssize_t php_win32_ioutil_readlink(const char *path, char *buf, size_t buf_len)
{/*{{{*/
size_t pathw_len, ret_buf_len;
wchar_t *pathw = php_win32_ioutil_conv_any_to_w(path, PHP_WIN32_CP_IGNORE_LEN, &pathw_len);
wchar_t retw[PHP_WIN32_IOUTIL_MAXPATHLEN];
char *ret_buf;
ssize_t ret;
if (!pathw) {
SET_ERRNO_FROM_WIN32_CODE(ERROR_INVALID_PARAMETER);
return -1;
}
ret = php_win32_ioutil_readlink_w(pathw, retw, sizeof(retw)-1);
if (ret < 0) {
DWORD _err = GetLastError();
free(pathw);
SET_ERRNO_FROM_WIN32_CODE(_err);
return ret;
}
ret_buf = php_win32_ioutil_conv_w_to_any(retw, ret, &ret_buf_len);
if (!ret_buf || ret_buf_len >= buf_len || ret_buf_len >= MAXPATHLEN) {
free(pathw);
SET_ERRNO_FROM_WIN32_CODE(ERROR_BAD_PATHNAME);
return -1;
}
memcpy(buf, ret_buf, ret_buf_len + 1);
free(pathw);
return ret_buf_len;
}/*}}}*/
#ifdef __cplusplus
}
#endif
#endif /* PHP_WIN32_IOUTIL_H */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: sw=4 ts=4 fdm=marker
* vim<600: sw=4 ts=4
*/
|