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
|
%% options
copyright owner = Dirk Krause
copyright year = 2015-xxxx
license = bsd
%% header
/** @file
Make directory hierarchy (create all parent directories for a
file or directory and the directory itself if required),
the file or directory name is specified as wchar_t string.
*/
#ifndef DK4CONF_H_INCLUDED
#include "dk4conf.h"
#endif
#ifndef DK4TYPES_H_INCLUDED
#include "dk4types.h"
#endif
#ifndef DK4ERROR_H_INCLUDED
#include "dk4error.h"
#endif
#if DK4_HAVE_WCHAR_H
#ifndef WCHAR_H_INCLUDED
#include <wchar.h>
#endif
#endif
#ifdef __cplusplus
extern "C" {
#endif
/** Find position of first separator for real directory.
This function is not intended for use in application programs,
it is exported only for use from the dk4appmkdh module.
The function either returns a value or sets the back variable
to 1 or reports an error.
@param fn File name.
@param bptr Pointer to the back variable.
@param lc Flag: Last component in fn is directory too.
@param erp Error report, may be NULL.
@return First separator after a real directory on success if
found, NULL otherwise.
*/
wchar_t *
dk4mkdir_hierarchy_wc_first_sep(wchar_t *fn, int *bptr, int lc, dk4_er_t *erp);
/** Check whether directory exists or attempt to create directory.
@param fn File name.
@param ro Flag: Read only (test directory presence),
do not attempt to create directory.
@param erp Error report, may be NULL.
@return 1 if directory is available now, 0 otherwise.
Error codes:
- DK4_E_INVALID_ARGUMENTS<br>
if fn is NULL,
- DK4_E_NOT_FOUND<br>
if a server/share combination is not present on Windows,
- DK4_E_NON_DIR<br>
if any component in the path is not a directory,
- DK4_E_CREATE_DIR_FAILED<br>
with GetLastError() result in lDetails1 if the CreateDirectory()
function failed.
*/
int
dk4mkdir_hierarchy_wc_one(const wchar_t *fn, int ro, dk4_er_t *erp);
/** Create directory structure.
CRT on Windows: Optional.
@param fn File name.
@param lc Flag: Last component in fn is directory too.
@param erp Error report, may be NULL.
@return 1 on success, 0 on error.
Error codes:
- DK4_E_INVALID_ARGUMENTS<br>
if fn is NULL,
- DK4_E_BUFFER_TOO_SMALL<br>
if fn is too long,
- DK4_E_MATH_OVERFLOW<br>
if a mathematical overflow occured in size allocation,
- DK4_E_NOT_FOUND<br>
if a server/share combination is not present on Windows,
- DK4_E_NON_DIR<br>
if one of the components in fn is not a directory,
- DK4_E_MKDIR_FAILED<br>
with errno in idetails if the function fails to create the directory
or a parent directory,
- DK4_E_CREATE_DIR_FAILED<br>
with GetLastError() result in lDetails1 if the CreateDirectory()
function failed on a Windows system.
*/
int
dk4mkdir_hierarchy_wc(const wchar_t *fn, int lc, dk4_er_t *erp);
#ifdef __cplusplus
}
#endif
%% module
#include "dk4conf.h"
#include "dk4types.h"
#if DK4_ON_WINDOWS
#ifndef WINDOWS_H_INCLUDED
#include <windows.h>
#define WINDOWS_H_INCLUDED 1
#endif
#endif
#include "dk4mkdh8.h"
#if DK4_HAVE_SYS_STAT_H
#ifndef SYS_STAT_H_INCLUDED
#include <sys/stat.h>
#define SYS_STAT_H_INCLUDED 1
#endif
#endif
#if DK4_HAVE_SYS_TYPES_H
#ifndef SYS_TYPES_H_INCLUDED
#include <sys/types.h>
#define SYS_TYPES_H_INCLUDED 1
#endif
#endif
#if DK4_HAVE_DIRECT_H
#ifndef DIRECT_H_INCLUDED
#include <direct.h>
#define DIRECT_H_INCLUDED 1
#endif
#endif
#if DK4_HAVE_STDLIB_H
#ifndef STDLIB_H_INCLUDED
#include <stdlib.h>
#define STDLIB_H_INCLUDED 1
#endif
#endif
#if DK4_HAVE_UNISTD_H
#ifndef UNISTD_H_INCLUDED
#include <unistd.h>
#define UNISTD_H_INCLUDED 1
#endif
#endif
#if DK4_HAVE_ERRNO_H
#ifndef ERRNO_H_INCLUDED
#include <errno.h>
#define ERRNO_H_INCLUDED 1
#endif
#endif
#ifndef DK4MEM_H_INCLUDED
#include "dk4mem.h"
#endif
#ifndef DK4MPL_H_INCLUDED
#include "dk4mpl.h"
#endif
#ifndef DK4STRW_H_INCLUDED
#include "dk4strw.h"
#endif
#ifndef DK4PATHW_H_INCLUDED
#include "dk4pathw.h"
#endif
#ifndef DK4STATW_H_INCLUDED
#include "dk4statw.h"
#endif
#ifndef DK4STAT_H_INCLUDED
#include "dk4stat.h"
#endif
$!trace-include
#if DK4_ON_WINDOWS
static
int
dk4mkdir_hierarchy_wc_is_char(wchar_t c)
{
int back = 0;
if ((L'a' <= c) && (L'z' >= c)) {
back = 1;
} else {
if ((L'A' <= c) && (L'Z' >= c)) {
back = 1;
}
}
return back;
}
int
dk4mkdir_hierarchy_wc_one(const wchar_t *fn, int ro, dk4_er_t *erp)
{
BY_HANDLE_FILE_INFORMATION bhfi;
WIN32_FIND_DATAW ffdata;
HANDLE ha;
DWORD dwattr;
int found;
int i;
int back = 0;
$? "+ dk4mkdir_hierarchy_wc_one \"%ls\"", fn
if (NULL == fn) {
dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
goto finished;
}
#if 1
dwattr = GetFileAttributesW(fn);
if (INVALID_FILE_ATTRIBUTES == dwattr) {
ha = FindFirstFileW(fn, &ffdata);
if (INVALID_HANDLE_VALUE != ha) {
dwattr = ffdata.dwFileAttributes;
FindClose(ha);
}
}
if (INVALID_FILE_ATTRIBUTES != dwattr) { $? ". exists"
if (0 != (FILE_ATTRIBUTE_DIRECTORY & dwattr)) { $? ". dir"
back = 1;
} else { $? "! not dir"
if (0 != (FILE_ATTRIBUTE_REPARSE_POINT & dwattr)) {
found = 0;
for (i = 0; ((3 > i) && (0 == back) && (0 == found)); i++) {
ha = CreateFileW(
fn,
(
(0 == i)
? (0)
: ((1 == i) ? (FILE_READ_ATTRIBUTES) : (GENERIC_READ))
),
(FILE_SHARE_READ | FILE_SHARE_WRITE),
NULL,
OPEN_EXISTING,
(dwattr | FILE_FLAG_BACKUP_SEMANTICS),
NULL
);
if (INVALID_HANDLE_VALUE != ha) {
if (GetFileInformationByHandle(ha, &bhfi)) {
found = 1;
if (0 != (FILE_ATTRIBUTE_DIRECTORY & (bhfi.dwFileAttributes))) {
back = 1;
}
}
CloseHandle(ha);
}
}
}
if (0 == back) {
dk4error_set_simple_error_code(erp, DK4_E_NON_DIR);
}
}
} else { $? ". not yet"
if (0 != ro) {
dk4error_set_simple_error_code(erp, DK4_E_NOT_FOUND);
} else {
#if DK4_WIN_AVOID_CRT || DK4_WIN_DENY_CRT
if (CreateDirectoryW(fn, NULL)) { $? ". created"
back = 1;
} else { $? "! CreateDirectoryW"
dk4error_set_ldetails(
erp, DK4_E_CREATE_DIR_FAILED ,
(long)((unsigned long)GetLastError())
);
}
#else
if (CreateDirectoryW(fn, NULL)) { $? ". created"
back = 1;
} else { $? "! CreateDirectoryW"
errno = 0;
if (0 == _wmkdir(fn)) {
back = 1;
} else {
dk4error_set_idetails(erp, DK4_E_MKDIR_FAILED, errno);
}
}
#endif
}
}
#else
back = 1;
#endif
finished:
$? "- dk4mkdir_hierarchy_wc_one %d", back
return back;
}
wchar_t *
dk4mkdir_hierarchy_wc_first_sep(wchar_t *fn, int *bptr, int lc, dk4_er_t *erp)
{
wchar_t *pc = NULL;
wchar_t sc;
int back = 0;
int isunc = 0;
if (NULL == fn) {
dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
goto finished;
}
if (L'\\' == fn[0]) { /* One backslash at start */
if (L'\\' == fn[1]) { /* Two backslashes */
if (L'?' == fn[2]) { /* Probably extended long path */
if (L'\\' == fn[3]) { /* Extended path */
if (dk4mkdir_hierarchy_wc_is_char(fn[4])) {
if (L':' == fn[5]) { /* Extended, drive */
if (L'\\' == fn[6]) { /* Extended, drive abs */
sc = fn[7];
fn[7] = L'\0';
back = dk4mkdir_hierarchy_wc_one(fn, 1, erp);
fn[7] = sc;
if (0 != back) {
pc = dk4strw_chr(&(fn[7]), L'\\');
if (NULL == pc) {
back = dk4mkdir_hierarchy_wc_one(fn, 0, erp);
} else {
back = 1;
}
} else {
pc = NULL;
}
} else { /* Extended, drive rel */
#if 0
pc = dk4strw_chr(&(fn[7]), L'\\');
if (NULL == pc) { back = 1; }
#endif
/* 2015-02-05 For extended paths we can use
full absolute path names only!
*/
dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
}
} else { /* Extended, server and share */
if ((L'U' == fn[4]) || (L'u' == fn[4])) {
if ((L'N' == fn[5]) || (L'n' == fn[5])) {
if ((L'C' == fn[6]) || (L'c' == fn[6])) {
if (L'\\' == fn[7]) {
isunc = 1;
}
}
}
}
if (0 != isunc) { /* UNC path */
pc = dk4strw_chr(&(fn[8]), L'\\');
if (NULL != pc) { /* Share */
pc = dk4strw_chr(&(pc[1]), L'\\');
if (NULL != pc) { /* First real directory */
*pc = L'\0';
back = dk4mkdir_hierarchy_wc_one(fn, 1, erp);
*pc = L'\\';
if (0 != back) {
back = 0;
pc = dk4strw_chr(&(pc[1]), L'\\');
if (NULL == pc) {
if (0 != lc) {
back = dk4mkdir_hierarchy_wc_one(fn, 0, erp);
} else {
back = 1;
}
}
} else {
pc = dk4strw_chr(&(pc[1]), L'\\');
if (NULL == pc) {
if (0 != lc) {
back = dk4mkdir_hierarchy_wc_one(fn, 0, erp);
}
}
}
} else { /* No real directory after share */
if (0 != lc) {
back = dk4mkdir_hierarchy_wc_one(fn, 1, erp);
} else {
dk4error_set_simple_error_code(
erp, DK4_E_INVALID_ARGUMENTS
);
}
}
} else {
dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
}
} else { /* Not UNC, normal path */
pc = dk4strw_chr(&(fn[4]), L'\\');
if (NULL == pc) {
if (0 != lc) {
back = dk4mkdir_hierarchy_wc_one(fn, 0, erp);
} else {
back = 1;
}
}
}
}
} else { /* Some unknown extended path */
pc = dk4strw_chr(&(fn[4]), L'\\');
if (NULL == pc) {
if (0 != lc) {
back = dk4mkdir_hierarchy_wc_one(fn, 0, erp);
} else {
back = 1;
}
}
}
} else { /* Illegal path */
dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
}
} else { /* Server and share */
pc = dk4strw_chr(&(fn[2]), L'\\');
if (NULL != pc) { /* Share */
pc = dk4strw_chr(&(pc[1]), L'\\');
if (NULL != pc) { /* First dir */
*pc = L'\0';
back = dk4mkdir_hierarchy_wc_one(fn, 1, erp);
*pc = L'\\';
if (0 != back) {
back = 0;
pc = dk4strw_chr(&(pc[1]), L'\\');
if (NULL == pc) {
if (0 != lc) {
back = dk4mkdir_hierarchy_wc_one(fn, 0, erp);
} else {
back = 1;
}
}
} else {
pc = dk4strw_chr(&(pc[1]), L'\\');
if (NULL == pc) {
if (0 != lc) {
back = dk4mkdir_hierarchy_wc_one(fn, 0, erp);
}
}
}
} else { /* Share name but no dir */
if (0 != lc) {
back = dk4mkdir_hierarchy_wc_one(fn, 1, erp);
} else {
dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
}
}
} else { /* No share */
dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
}
}
} else { /* Absolute path wo drive */
pc = dk4strw_chr(&(fn[1]), L'\\');
if (NULL == pc) {
if (0 != lc) {
back = dk4mkdir_hierarchy_wc_one(fn, 0, erp);
} else {
back = 1;
}
}
}
} else { /* No backslash at start */
if (dk4mkdir_hierarchy_wc_is_char(fn[0])) {
if (L':' == fn[1]) { /* Drive */
if (L'\\' == fn[2]) { /* Drive abs */
sc = fn[3];
fn[3] = L'\0';
back = dk4mkdir_hierarchy_wc_one(fn, 1, erp);
fn[3] = sc;
if (0 != back) {
back = 0;
pc = dk4strw_chr(&(fn[3]), L'\\');
if (NULL == pc) {
if (0 != lc) {
back = dk4mkdir_hierarchy_wc_one(fn, 0, erp);
} else {
back = 1;
}
}
}
} else { /* Drive rel */
sc = fn[2];
fn[2] = L'\0';
back = dk4mkdir_hierarchy_wc_one(fn, 1, erp);
fn[2] = sc;
if (0 != back) {
back = 0;
pc = dk4strw_chr(&(fn[3]), L'\\');
if (NULL == pc) {
if (0 != lc) {
back = dk4mkdir_hierarchy_wc_one(fn, 0, erp);
} else {
back = 1;
}
}
}
}
} else { /* Normal file name */
pc = dk4strw_chr(fn, L'\\');
if (NULL == pc) {
if (0 != lc) {
back = dk4mkdir_hierarchy_wc_one(fn, 0, erp);
} else {
back = 1;
}
}
}
} else { /* Normal file name */
pc = dk4strw_chr(fn, L'\\');
if (NULL == pc) {
if (0 != lc) {
back = dk4mkdir_hierarchy_wc_one(fn, 0, erp);
} else {
back = 1;
}
}
}
}
finished:
if (NULL != bptr) { *bptr = back; }
return pc;
}
/** Run on writable copy of file name.
@param fn File name.
@param lc Flag: Last component is directory too.
@param erp Error report, may be NULL.
@return 1 on success, 0 on error.
*/
static
int
dk4mkdir_hierarchy_wc_on_copy(wchar_t *fn, int lc, dk4_er_t *erp)
{
wchar_t *pc = NULL;
int back = 0;
/* Correct path name */
dk4pathw_correct_sep(fn);
/* Find first real separator */
pc = dk4mkdir_hierarchy_wc_first_sep(fn, &back, lc, erp);
/* Attempt to create directory, parent directories too if necessary */
if (NULL != pc) {
back = 1;
while (NULL != pc) {
*pc = L'\0';
if (0 < dk4strw_len(fn)) {
if (0 == dk4mkdir_hierarchy_wc_one(fn, 0, erp)) {
back = 0;
}
}
*pc = L'\\';
pc = dk4strw_chr(&(pc[1]), L'\\');
}
if (0 != lc) {
if (0 == dk4mkdir_hierarchy_wc_one(fn, 0, erp)) {
back = 0;
}
}
} else {
if (0 != back) {
if (0 != lc) {
if (0 == dk4mkdir_hierarchy_wc_one(fn, 0, erp)) {
back = 0;
}
}
}
}
return back;
}
/** Create writable copy in local buffer and run.
@param fn Original file name.
@param lc Flag: Last component is directory too.
@param erp Error report, may be NULL.
@return 1 on success, 0 on error.
*/
static
int
dk4mkdir_hierarchy_wc_local(const wchar_t *fn, int lc, dk4_er_t *erp)
{
wchar_t mycp[DK4_MAX_PATH];
int back = 0;
if (dk4strw_cpy_s(mycp, DK4_SIZEOF(mycp,wchar_t), fn, erp)) {
back = dk4mkdir_hierarchy_wc_on_copy(mycp, lc, erp);
}
return back;
}
#endif
int
dk4mkdir_hierarchy_wc(const wchar_t *fn, int lc, dk4_er_t *erp)
{
#if DK4_ON_WINDOWS
wchar_t *mycp;
size_t sz;
int back = 0;
if (NULL != fn) {
sz = dk4strw_len(fn);
if (0 < sz) {
if (DK4_MAX_PATH > sz) {
back = dk4mkdir_hierarchy_wc_local(fn, lc, erp);
} else {
mycp = dk4strw_dup(fn, erp);
if (NULL != mycp) {
dk4mkdir_hierarchy_wc_on_copy(mycp, lc, erp);
dk4mem_free(mycp);
}
}
} else {
dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
}
} else {
dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
}
return back;
#else
dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED);
$? "+ dk4mkdir_hierarchy"
$? "! not supported on this platform"
$? "- dk4mkdir_hierarchy 0"
return 0;
#endif
}
|