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
804
805
806
807
808
809
810
811
812
813
814
815
|
/**
* WinPR: Windows Portable Runtime
* Path Functions
*
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
* Copyright 2016 David PHAM-VAN <d.phamvan@inuvika.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <winpr/config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <winpr/crt.h>
#include <winpr/platform.h>
#include <winpr/file.h>
#include <winpr/tchar.h>
#include <winpr/environment.h>
#include <winpr/path.h>
#include <winpr/wlog.h>
#include "../log.h"
#define TAG WINPR_TAG("path.shell")
#if defined(__IOS__)
#include "shell_ios.h"
#endif
#if defined(WIN32)
#include <shlobj.h>
#else
#include <errno.h>
#include <dirent.h>
#endif
static char* GetPath_XDG_CONFIG_HOME(void);
static char* GetPath_XDG_RUNTIME_DIR(void);
/**
* SHGetKnownFolderPath function:
* http://msdn.microsoft.com/en-us/library/windows/desktop/bb762188/
*/
/**
* XDG Base Directory Specification:
* http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
*/
char* GetEnvAlloc(LPCSTR lpName)
{
DWORD nSize = 0;
DWORD nStatus = 0;
char* env = NULL;
nSize = GetEnvironmentVariableX(lpName, NULL, 0);
if (nSize > 0)
{
env = malloc(nSize);
if (!env)
return NULL;
nStatus = GetEnvironmentVariableX(lpName, env, nSize);
if (nStatus != (nSize - 1))
{
free(env);
return NULL;
}
}
return env;
}
static char* GetPath_HOME(void)
{
char* path = NULL;
#ifdef _WIN32
path = GetEnvAlloc("UserProfile");
#elif defined(__IOS__)
path = ios_get_home();
#else
path = GetEnvAlloc("HOME");
#endif
return path;
}
static char* GetPath_TEMP(void)
{
char* path = NULL;
#ifdef _WIN32
path = GetEnvAlloc("TEMP");
#elif defined(__IOS__)
path = ios_get_temp();
#else
path = GetEnvAlloc("TMPDIR");
if (!path)
path = _strdup("/tmp");
#endif
return path;
}
static char* GetPath_XDG_DATA_HOME(void)
{
char* path = NULL;
#if defined(WIN32) || defined(__IOS__)
path = GetPath_XDG_CONFIG_HOME();
#else
size_t size = 0;
char* home = NULL;
/**
* There is a single base directory relative to which user-specific data files should be
* written. This directory is defined by the environment variable $XDG_DATA_HOME.
*
* $XDG_DATA_HOME defines the base directory relative to which user specific data files should
* be stored. If $XDG_DATA_HOME is either not set or empty, a default equal to
* $HOME/.local/share should be used.
*/
path = GetEnvAlloc("XDG_DATA_HOME");
if (path)
return path;
home = GetPath_HOME();
if (!home)
return NULL;
size = strlen(home) + strlen("/.local/share") + 1;
path = (char*)malloc(size);
if (!path)
{
free(home);
return NULL;
}
sprintf_s(path, size, "%s%s", home, "/.local/share");
free(home);
#endif
return path;
}
static char* GetPath_XDG_CONFIG_HOME(void)
{
char* path = NULL;
#if defined(WIN32) && !defined(_UWP)
path = calloc(MAX_PATH, sizeof(char));
if (!path)
return NULL;
if (FAILED(SHGetFolderPathA(0, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, path)))
{
free(path);
return NULL;
}
#elif defined(__IOS__)
path = ios_get_data();
#else
size_t size = 0;
char* home = NULL;
/**
* There is a single base directory relative to which user-specific configuration files should
* be written. This directory is defined by the environment variable $XDG_CONFIG_HOME.
*
* $XDG_CONFIG_HOME defines the base directory relative to which user specific configuration
* files should be stored. If $XDG_CONFIG_HOME is either not set or empty, a default equal to
* $HOME/.config should be used.
*/
path = GetEnvAlloc("XDG_CONFIG_HOME");
if (path)
return path;
home = GetPath_HOME();
if (!home)
home = GetPath_TEMP();
if (!home)
return NULL;
size = strlen(home) + strlen("/.config") + 1;
path = (char*)malloc(size);
if (!path)
{
free(home);
return NULL;
}
sprintf_s(path, size, "%s%s", home, "/.config");
free(home);
#endif
return path;
}
static char* GetPath_XDG_CACHE_HOME(void)
{
char* path = NULL;
char* home = NULL;
#if defined(WIN32)
home = GetPath_XDG_RUNTIME_DIR();
if (home)
{
path = GetCombinedPath(home, "cache");
if (!winpr_PathFileExists(path))
if (!CreateDirectoryA(path, NULL))
path = NULL;
}
free(home);
#elif defined(__IOS__)
path = ios_get_cache();
#else
size_t size = 0;
/**
* There is a single base directory relative to which user-specific non-essential (cached) data
* should be written. This directory is defined by the environment variable $XDG_CACHE_HOME.
*
* $XDG_CACHE_HOME defines the base directory relative to which user specific non-essential data
* files should be stored. If $XDG_CACHE_HOME is either not set or empty, a default equal to
* $HOME/.cache should be used.
*/
path = GetEnvAlloc("XDG_CACHE_HOME");
if (path)
return path;
home = GetPath_HOME();
if (!home)
return NULL;
size = strlen(home) + strlen("/.cache") + 1;
path = (char*)malloc(size);
if (!path)
{
free(home);
return NULL;
}
sprintf_s(path, size, "%s%s", home, "/.cache");
free(home);
#endif
return path;
}
char* GetPath_XDG_RUNTIME_DIR(void)
{
char* path = NULL;
#if defined(WIN32) && !defined(_UWP)
path = calloc(MAX_PATH, sizeof(char));
if (!path)
return NULL;
if (FAILED(SHGetFolderPathA(0, CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, path)))
{
free(path);
return NULL;
}
#else
/**
* There is a single base directory relative to which user-specific runtime files and other file
* objects should be placed. This directory is defined by the environment variable
* $XDG_RUNTIME_DIR.
*
* $XDG_RUNTIME_DIR defines the base directory relative to which user-specific non-essential
* runtime files and other file objects (such as sockets, named pipes, ...) should be stored.
* The directory MUST be owned by the user, and he MUST be the only one having read and write
* access to it. Its Unix access mode MUST be 0700.
*
* The lifetime of the directory MUST be bound to the user being logged in. It MUST be created
* when the user first logs in and if the user fully logs out the directory MUST be removed. If
* the user logs in more than once he should get pointed to the same directory, and it is
* mandatory that the directory continues to exist from his first login to his last logout on
* the system, and not removed in between. Files in the directory MUST not survive reboot or a
* full logout/login cycle.
*
* The directory MUST be on a local file system and not shared with any other system. The
* directory MUST by fully-featured by the standards of the operating system. More specifically,
* on Unix-like operating systems AF_UNIX sockets, symbolic links, hard links, proper
* permissions, file locking, sparse files, memory mapping, file change notifications, a
* reliable hard link count must be supported, and no restrictions on the file name character
* set should be imposed. Files in this directory MAY be subjected to periodic clean-up. To
* ensure that your files are not removed, they should have their access time timestamp modified
* at least once every 6 hours of monotonic time or the 'sticky' bit should be set on the file.
*
* If $XDG_RUNTIME_DIR is not set applications should fall back to a replacement directory with
* similar capabilities and print a warning message. Applications should use this directory for
* communication and synchronization purposes and should not place larger files in it, since it
* might reside in runtime memory and cannot necessarily be swapped out to disk.
*/
path = GetEnvAlloc("XDG_RUNTIME_DIR");
#endif
if (path)
return path;
path = GetPath_TEMP();
return path;
}
char* GetKnownPath(int id)
{
char* path = NULL;
switch (id)
{
case KNOWN_PATH_HOME:
path = GetPath_HOME();
break;
case KNOWN_PATH_TEMP:
path = GetPath_TEMP();
break;
case KNOWN_PATH_XDG_DATA_HOME:
path = GetPath_XDG_DATA_HOME();
break;
case KNOWN_PATH_XDG_CONFIG_HOME:
path = GetPath_XDG_CONFIG_HOME();
break;
case KNOWN_PATH_XDG_CACHE_HOME:
path = GetPath_XDG_CACHE_HOME();
break;
case KNOWN_PATH_XDG_RUNTIME_DIR:
path = GetPath_XDG_RUNTIME_DIR();
break;
default:
path = NULL;
break;
}
if (!path)
WLog_WARN(TAG, "Path %s is %p", GetKnownPathIdString(id), path);
return path;
}
char* GetKnownSubPath(int id, const char* path)
{
char* subPath = NULL;
char* knownPath = NULL;
knownPath = GetKnownPath(id);
if (!knownPath)
return NULL;
subPath = GetCombinedPath(knownPath, path);
free(knownPath);
return subPath;
}
char* GetEnvironmentPath(char* name)
{
char* env = NULL;
DWORD nSize = 0;
DWORD nStatus = 0;
nSize = GetEnvironmentVariableX(name, NULL, 0);
if (nSize)
{
env = (LPSTR)malloc(nSize);
if (!env)
return NULL;
nStatus = GetEnvironmentVariableX(name, env, nSize);
if (nStatus != (nSize - 1))
{
free(env);
return NULL;
}
}
return env;
}
char* GetEnvironmentSubPath(char* name, const char* path)
{
char* env = NULL;
char* subpath = NULL;
env = GetEnvironmentPath(name);
if (!env)
return NULL;
subpath = GetCombinedPath(env, path);
free(env);
return subpath;
}
char* GetCombinedPath(const char* basePath, const char* subPath)
{
size_t length = 0;
HRESULT status = 0;
char* path = NULL;
char* subPathCpy = NULL;
size_t basePathLength = 0;
size_t subPathLength = 0;
if (basePath)
basePathLength = strlen(basePath);
if (subPath)
subPathLength = strlen(subPath);
length = basePathLength + subPathLength + 1;
path = (char*)calloc(1, length + 1);
if (!path)
goto fail;
if (basePath)
CopyMemory(path, basePath, basePathLength);
if (FAILED(PathCchConvertStyleA(path, basePathLength, PATH_STYLE_NATIVE)))
goto fail;
if (!subPath)
return path;
subPathCpy = _strdup(subPath);
if (!subPathCpy)
goto fail;
if (FAILED(PathCchConvertStyleA(subPathCpy, subPathLength, PATH_STYLE_NATIVE)))
goto fail;
status = NativePathCchAppendA(path, length + 1, subPathCpy);
if (FAILED(status))
goto fail;
free(subPathCpy);
return path;
fail:
free(path);
free(subPathCpy);
return NULL;
}
BOOL PathMakePathA(LPCSTR path, LPSECURITY_ATTRIBUTES lpAttributes)
{
#if defined(_UWP)
return FALSE;
#elif defined(_WIN32)
return (SHCreateDirectoryExA(NULL, path, lpAttributes) == ERROR_SUCCESS);
#else
const char delim = PathGetSeparatorA(PATH_STYLE_NATIVE);
char* dup = NULL;
BOOL result = TRUE;
/* we only operate on a non-null, absolute path */
#if defined(__OS2__)
if (!path)
return FALSE;
#else
if (!path || *path != delim)
return FALSE;
#endif
if (!(dup = _strdup(path)))
return FALSE;
#ifdef __OS2__
p = (strlen(dup) > 3) && (dup[1] == ':') && (dup[2] == delim)) ? &dup[3] : dup;
while (p)
#else
for (char* p = dup; p;)
#endif
{
if ((p = strchr(p + 1, delim)))
*p = '\0';
if (mkdir(dup, 0777) != 0)
if (errno != EEXIST)
{
result = FALSE;
break;
}
if (p)
*p = delim;
}
free(dup);
return (result);
#endif
}
BOOL PathMakePathW(LPCWSTR path, LPSECURITY_ATTRIBUTES lpAttributes)
{
#if defined(_UWP)
return FALSE;
#elif defined(_WIN32)
return (SHCreateDirectoryExW(NULL, path, lpAttributes) == ERROR_SUCCESS);
#else
const WCHAR wdelim = PathGetSeparatorW(PATH_STYLE_NATIVE);
const char delim = PathGetSeparatorA(PATH_STYLE_NATIVE);
char* dup = NULL;
BOOL result = TRUE;
/* we only operate on a non-null, absolute path */
#if defined(__OS2__)
if (!path)
return FALSE;
#else
if (!path || *path != wdelim)
return FALSE;
#endif
dup = ConvertWCharToUtf8Alloc(path, NULL);
if (!dup)
return FALSE;
#ifdef __OS2__
p = (strlen(dup) > 3) && (dup[1] == ':') && (dup[2] == delim)) ? &dup[3] : dup;
while (p)
#else
for (char* p = dup; p;)
#endif
{
if ((p = strchr(p + 1, delim)))
*p = '\0';
if (mkdir(dup, 0777) != 0)
if (errno != EEXIST)
{
result = FALSE;
break;
}
if (p)
*p = delim;
}
free(dup);
return (result);
#endif
}
#if !defined(_WIN32) || defined(_UWP)
BOOL PathIsRelativeA(LPCSTR pszPath)
{
if (!pszPath)
return FALSE;
return pszPath[0] != '/';
}
BOOL PathIsRelativeW(LPCWSTR pszPath)
{
LPSTR lpFileNameA = NULL;
BOOL ret = FALSE;
if (!pszPath)
goto fail;
lpFileNameA = ConvertWCharToUtf8Alloc(pszPath, NULL);
if (!lpFileNameA)
goto fail;
ret = PathIsRelativeA(lpFileNameA);
fail:
free(lpFileNameA);
return ret;
}
BOOL PathFileExistsA(LPCSTR pszPath)
{
struct stat stat_info;
if (stat(pszPath, &stat_info) != 0)
return FALSE;
return TRUE;
}
BOOL PathFileExistsW(LPCWSTR pszPath)
{
LPSTR lpFileNameA = NULL;
BOOL ret = FALSE;
if (!pszPath)
goto fail;
lpFileNameA = ConvertWCharToUtf8Alloc(pszPath, NULL);
if (!lpFileNameA)
goto fail;
ret = winpr_PathFileExists(lpFileNameA);
fail:
free(lpFileNameA);
return ret;
}
BOOL PathIsDirectoryEmptyA(LPCSTR pszPath)
{
struct dirent* dp = NULL;
int empty = 1;
DIR* dir = opendir(pszPath);
if (dir == NULL) /* Not a directory or doesn't exist */
return 1;
while ((dp = readdir(dir)) != NULL)
{
if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0)
continue; /* Skip . and .. */
empty = 0;
break;
}
closedir(dir);
return empty;
}
BOOL PathIsDirectoryEmptyW(LPCWSTR pszPath)
{
LPSTR lpFileNameA = NULL;
BOOL ret = FALSE;
if (!pszPath)
goto fail;
lpFileNameA = ConvertWCharToUtf8Alloc(pszPath, NULL);
if (!lpFileNameA)
goto fail;
ret = PathIsDirectoryEmptyA(lpFileNameA);
fail:
free(lpFileNameA);
return ret;
}
#endif
BOOL winpr_MoveFile(LPCSTR lpExistingFileName, LPCSTR lpNewFileName)
{
#ifndef _WIN32
return MoveFileA(lpExistingFileName, lpNewFileName);
#else
BOOL result = FALSE;
LPWSTR lpExistingFileNameW = NULL;
LPWSTR lpNewFileNameW = NULL;
if (!lpExistingFileName || !lpNewFileName)
return FALSE;
lpExistingFileNameW = ConvertUtf8ToWCharAlloc(lpExistingFileName, NULL);
if (!lpExistingFileNameW)
goto cleanup;
lpNewFileNameW = ConvertUtf8ToWCharAlloc(lpNewFileName, NULL);
if (!lpNewFileNameW)
goto cleanup;
result = MoveFileW(lpExistingFileNameW, lpNewFileNameW);
cleanup:
free(lpExistingFileNameW);
free(lpNewFileNameW);
return result;
#endif
}
BOOL winpr_MoveFileEx(LPCSTR lpExistingFileName, LPCSTR lpNewFileName, DWORD dwFlags)
{
#ifndef _WIN32
return MoveFileExA(lpExistingFileName, lpNewFileName, dwFlags);
#else
BOOL result = FALSE;
LPWSTR lpExistingFileNameW = NULL;
LPWSTR lpNewFileNameW = NULL;
if (!lpExistingFileName || !lpNewFileName)
return FALSE;
lpExistingFileNameW = ConvertUtf8ToWCharAlloc(lpExistingFileName, NULL);
if (!lpExistingFileNameW)
goto cleanup;
lpNewFileNameW = ConvertUtf8ToWCharAlloc(lpNewFileName, NULL);
if (!lpNewFileNameW)
goto cleanup;
result = MoveFileExW(lpExistingFileNameW, lpNewFileNameW, dwFlags);
cleanup:
free(lpExistingFileNameW);
free(lpNewFileNameW);
return result;
#endif
}
BOOL winpr_DeleteFile(const char* lpFileName)
{
#ifndef _WIN32
return DeleteFileA(lpFileName);
#else
LPWSTR lpFileNameW = NULL;
BOOL result = FALSE;
if (lpFileName)
{
lpFileNameW = ConvertUtf8ToWCharAlloc(lpFileName, NULL);
if (!lpFileNameW)
goto cleanup;
}
result = DeleteFileW(lpFileNameW);
cleanup:
free(lpFileNameW);
return result;
#endif
}
BOOL winpr_RemoveDirectory(LPCSTR lpPathName)
{
#ifndef _WIN32
return RemoveDirectoryA(lpPathName);
#else
LPWSTR lpPathNameW = NULL;
BOOL result = FALSE;
if (lpPathName)
{
lpPathNameW = ConvertUtf8ToWCharAlloc(lpPathName, NULL);
if (!lpPathNameW)
goto cleanup;
}
result = RemoveDirectoryW(lpPathNameW);
cleanup:
free(lpPathNameW);
return result;
#endif
}
BOOL winpr_PathFileExists(const char* pszPath)
{
if (!pszPath)
return FALSE;
#ifndef _WIN32
return PathFileExistsA(pszPath);
#else
WCHAR* pathW = ConvertUtf8ToWCharAlloc(pszPath, NULL);
BOOL result = FALSE;
if (!pathW)
return FALSE;
result = PathFileExistsW(pathW);
free(pathW);
return result;
#endif
}
BOOL winpr_PathMakePath(const char* path, LPSECURITY_ATTRIBUTES lpAttributes)
{
if (!path)
return FALSE;
#ifndef _WIN32
return PathMakePathA(path, lpAttributes);
#else
WCHAR* pathW = ConvertUtf8ToWCharAlloc(path, NULL);
BOOL result = FALSE;
if (!pathW)
return FALSE;
result = SHCreateDirectoryExW(NULL, pathW, lpAttributes) == ERROR_SUCCESS;
free(pathW);
return result;
#endif
}
|