FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
rstudio/src/cpp/shared_core/FilePath.cpp at main · ssh352/rstudio · GitHub
ssh352
/
rstudio
Public
forked from
rstudio/rstudio
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
rstudio
/
src
/
cpp
/
shared_core
/
FilePath.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
1830 lines (1588 loc) · 50.7 KB
Breadcrumbs
rstudio
/
src
/
cpp
/
shared_core
/
FilePath.cpp
Copy path
File metadata and controls
1830 lines (1588 loc) · 50.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* FilePath.cpp
*
* Copyright (C) 2022 by Posit Software, PBC
*
* Unless you have received this program directly from Posit Software pursuant to the terms of a commercial license agreement
* with Posit, then this program is licensed to you under the following terms:
*
* 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.
*
*/
#
include
<
shared_core/FilePath.hpp
>
#
include
<
algorithm
>
#
include
<
fstream
>
#
ifdef
_WIN32
#
include
<
windows.h
>
#
include
<
boost/iostreams/stream.hpp
>
#
include
<
boost/iostreams/device/file_descriptor.hpp
>
#
include
<
boost/system/windows_error.hpp
>
#
include
<
shared_core/system/Win32StringUtils.hpp
>
#
else
#
include
<
sys/stat.h
>
#
include
<
sys/unistd.h
>
#
include
<
shared_core/system/PosixSystem.hpp
>
#
endif
#
define
BOOST_NO_CXX11_SCOPED_ENUMS
#
include
<
boost/filesystem.hpp
>
#
undef
BOOST_NO_CXX11_SCOPED_ENUMS
#
include
<
boost/bind/bind.hpp
>
#
include
<
boost/make_shared.hpp
>
#
include
<
boost/algorithm/string/case_conv.hpp
>
#
include
<
boost/algorithm/string/predicate.hpp
>
#
include
<
shared_core/Logger.hpp
>
#
include
<
shared_core/Error.hpp
>
#
include
<
shared_core/SafeConvert.hpp
>
#
include
<
shared_core/system/User.hpp
>
typedef
boost::filesystem::path
path_t
;
using
namespace
boost
::placeholders
;
namespace
rstudio
{
namespace
core
{
//
Helpers =============================================================================================================
namespace
{
struct
MimeType
{
const
char
* extension;
const
char
* contentType;
};
//
NOTE: should be synced with mime type database in FileSystemItem.java
MimeType s_mimeTypes[] =
{
//
most common web types
{
"
htm
"
,
"
text/html
"
},
{
"
html
"
,
"
text/html
"
},
{
"
css
"
,
"
text/css
"
},
{
"
sass
"
,
"
text/sass
"
},
{
"
scss
"
,
"
text/scss
"
},
{
"
less
"
,
"
text/less
"
},
{
"
gif
"
,
"
image/gif
"
},
{
"
jpg
"
,
"
image/jpeg
"
},
{
"
jpeg
"
,
"
image/jpeg
"
},
{
"
jpe
"
,
"
image/jpeg
"
},
{
"
png
"
,
"
image/png
"
},
{
"
webp
"
,
"
image/webp
"
},
{
"
js
"
,
"
text/javascript
"
},
{
"
pdf
"
,
"
application/pdf
"
},
{
"
svg
"
,
"
image/svg+xml
"
},
{
"
swf
"
,
"
application/x-shockwave-flash
"
},
{
"
ttf
"
,
"
application/x-font-ttf
"
},
{
"
woff
"
,
"
application/font-woff
"
},
{
"
woff2
"
,
"
application/font-woff2
"
},
{
"
wasm
"
,
"
application/wasm
"
},
//
markdown types
{
"
md
"
,
"
text/x-markdown
"
},
{
"
mdtxt
"
,
"
text/x-markdown
"
},
{
"
markdown
"
,
"
text/x-markdown
"
},
{
"
yaml
"
,
"
text/x-yaml
"
},
{
"
yml
"
,
"
text/x-yaml
"
},
//
programming language types
{
"
f
"
,
"
text/x-fortran
"
},
{
"
py
"
,
"
text/x-python
"
},
{
"
smk
"
,
"
text/x-python
"
},
{
"
sh
"
,
"
text/x-shell
"
},
{
"
sql
"
,
"
text/x-sql
"
},
{
"
stan
"
,
"
text/x-stan
"
},
{
"
clj
"
,
"
text/x-clojure
"
},
{
"
ts
"
,
"
text/x-typescript
"
},
{
"
ojs
"
,
"
text/javascript
"
},
{
"
lua
"
,
"
text/x-lua
"
},
{
"
groovy
"
,
"
text/x-groovy
"
},
{
"
nf
"
,
"
text/x-groovy
"
},
//
other types we are likely to serve
{
"
xml
"
,
"
text/xml
"
},
{
"
csv
"
,
"
text/csv
"
},
{
"
ico
"
,
"
image/x-icon
"
},
{
"
zip
"
,
"
application/zip
"
},
{
"
bz
"
,
"
application/x-bzip
"
},
{
"
bz2
"
,
"
application/x-bzip2
"
},
{
"
gz
"
,
"
application/x-gzip
"
},
{
"
tar
"
,
"
application/x-tar
"
},
{
"
json
"
,
"
application/json
"
},
{
"
rstheme
"
,
"
text/css
"
},
//
yet more types...
{
"
shtml
"
,
"
text/html
"
},
{
"
tsv
"
,
"
text/tab-separated-values
"
},
{
"
tab
"
,
"
text/tab-separated-values
"
},
{
"
cl
"
,
"
text/plain
"
},
{
"
dcf
"
,
"
text/debian-control-file
"
},
{
"
i
"
,
"
text/plain
"
},
{
"
ini
"
,
"
text/plain
"
},
{
"
txt
"
,
"
text/plain
"
},
{
"
mml
"
,
"
text/mathml
"
},
{
"
log
"
,
"
text/plain
"
},
{
"
lintr
"
,
"
text/plain
"
},
{
"
out
"
,
"
text/plain
"
},
{
"
csl
"
,
"
text/x-csl
"
},
{
"
R
"
,
"
text/x-r-source
"
},
{
"
S
"
,
"
text/x-r-source
"
},
{
"
q
"
,
"
text/x-r-source
"
},
{
"
Rd
"
,
"
text/x-r-doc
"
},
{
"
Rnw
"
,
"
text/x-r-sweave
"
},
{
"
Rmd
"
,
"
text/x-r-markdown
"
},
{
"
Rhtml
"
,
"
text/x-r-html
"
},
{
"
Rpres
"
,
"
text/x-r-presentation
"
},
{
"
qmd
"
,
"
text/x-quarto-markdown
"
},
{
"
Rout
"
,
"
text/plain
"
},
{
"
po
"
,
"
text/plain
"
},
{
"
pot
"
,
"
text/plain
"
},
{
"
ps1
"
,
"
text/plain
"
},
{
"
rst
"
,
"
text/plain
"
},
{
"
gitignore
"
,
"
text/plain
"
},
{
"
Rbuildignore
"
,
"
text/plain
"
},
{
"
Rprofile
"
,
"
text/x-r-source
"
},
{
"
Renviron
"
,
"
text/x-shell
"
},
{
"
rprofvis
"
,
"
text/x-r-profile
"
},
{
"
vcxproj
"
,
"
text/xml
"
},
{
"
tif
"
,
"
image/tiff
"
},
{
"
tiff
"
,
"
image/tiff
"
},
{
"
bmp
"
,
"
image/bmp
"
},
{
"
ps
"
,
"
application/postscript
"
},
{
"
eps
"
,
"
application/postscript
"
},
{
"
dvi
"
,
"
application/x-dvi
"
},
{
"
atom
"
,
"
application/atom+xml
"
},
{
"
rss
"
,
"
application/rss+xml
"
},
{
"
doc
"
,
"
application/msword
"
},
{
"
docx
"
,
"
application/vnd.openxmlformats-officedocument.wordprocessingml.document
"
},
{
"
odt
"
,
"
application/vnd.oasis.opendocument.text
"
},
{
"
rtf
"
,
"
application/rtf
"
},
{
"
xls
"
,
"
application/vnd.ms-excel
"
},
{
"
xlsx
"
,
"
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
"
},
{
"
ods
"
,
"
application/x-vnd.oasis.opendocument.spreadsheet
"
},
{
"
ppt
"
,
"
application/vnd.ms-powerpoint
"
},
{
"
pps
"
,
"
application/vnd.ms-powerpoint
"
},
{
"
pptx
"
,
"
application/vnd.openxmlformats-officedocument.presentationml.presentation
"
},
{
"
sit
"
,
"
application/x-stuffit
"
},
{
"
sxw
"
,
"
application/vnd.sun.xml.writer
"
},
{
"
iso
"
,
"
application/octet-stream
"
},
{
"
dmg
"
,
"
application/octet-stream
"
},
{
"
exe
"
,
"
application/octet-stream
"
},
{
"
dll
"
,
"
application/octet-stream
"
},
{
"
deb
"
,
"
application/octet-stream
"
},
{
"
otf
"
,
"
application/octet-stream
"
},
{
"
xpi
"
,
"
application/x-xpinstall
"
},
{
"
mp2
"
,
"
audio/mpeg
"
},
{
"
mpg
"
,
"
video/mpeg
"
},
{
"
mpeg
"
,
"
video/mpeg
"
},
{
"
flv
"
,
"
video/x-flv
"
},
{
"
mp4
"
,
"
video/mp4
"
},
{
"
webm
"
,
"
video/webm
"
},
{
"
ogv
"
,
"
video/ogg
"
},
{
"
mp3
"
,
"
audio/mp3
"
},
{
"
wav
"
,
"
audio/wav
"
},
{
"
oga
"
,
"
audio/ogg
"
},
{
"
ogg
"
,
"
audio/ogg
"
},
{
nullptr
,
nullptr
}
};
const
std::string&
homePathAlias
()
{
static
const
std::string homePathAlias =
"
~/
"
;
return
homePathAlias;
}
const
std::string&
homePathLeafAlias
()
{
static
const
std::string homePathLeafAlias =
"
~
"
;
return
homePathLeafAlias;
}
//
We use boost::filesystem in one of two ways:
//
- On Windows, we use Filesystem v3 with wide character paths. This is
//
because narrow character paths on Windows can't cover the entire
//
Unicode space since there is no ANSI code page for UTF-8.
//
- On non-Windows, we use Filesystem v3.
#
ifdef
_WIN32
#
define
BOOST_FS_STRING
(
path
) toString((path).generic_wstring())
#
define
BOOST_FS_PATH2STR
(
path
) toString((path).generic_wstring())
#
define
BOOST_FS_PATH2STRNATIVE
(
path
) toString((path).wstring())
#
define
BOOST_FS_COMPLETE
(
p, base
) boost::filesystem::absolute(fromString(p), base)
typedef
boost::filesystem::directory_iterator dir_iterator;
typedef
boost::filesystem::recursive_directory_iterator recursive_dir_iterator;
#
elif
defined(BOOST_FILESYSTEM_VERSION) && BOOST_FILESYSTEM_VERSION != 2
#
define
BOOST_FS_STRING
(
path
) ((path).generic_string())
#
define
BOOST_FS_PATH2STR
(
path
) ((path).generic_string())
#
define
BOOST_FS_PATH2STRNATIVE
(
path
) ((path).generic_string())
#
define
BOOST_FS_COMPLETE
(
p, base
) boost::filesystem::absolute(p, base)
typedef
boost::filesystem::directory_iterator dir_iterator;
typedef
boost::filesystem::recursive_directory_iterator recursive_dir_iterator;
#
else
#
error
FilePath requires Filesystem v3
#
endif
#
ifdef
_WIN32
//
For Windows only, we need to use the wide character versions of the file
//
APIs in order to deal properly with characters that cannot be represented
//
in the default system encoding. (It would be preferable if UTF-8 were the
//
system encoding, but Windows doesn't support that.) However, we can't give
//
FilePath a wide character API because Mac needs to use narrow characters
//
(see note below). So we use wstring internally, and translate to/from UTF-8
//
narrow strings that are used in the API.
typedef
std::wstring internal_string;
std::string
toString
(
const
internal_string& value)
{
return
string_utils::wideToUtf8
(value);
}
internal_string
fromString
(
const
std::string& value)
{
return
string_utils::utf8ToWide
(value);
}
#
else
//
We only support running with UTF-8 codeset on Mac and Linux, so
//
strings are a passthrough.
typedef
std::string internal_string;
inline
internal_string
fromString
(
const
std::string& in_value)
{
return
in_value;
}
#
endif
void
addErrorProperties
(
path_t
path, Error* pError)
{
pError->
addProperty
(
"
path
"
,
BOOST_FS_PATH2STR
(path));
}
bool
addItemSize
(
const
FilePath& item, boost::shared_ptr<
uintmax_t
> pTotal)
{
if
(!item.
isDirectory
())
*pTotal = *pTotal + item.
getSize
();
return
true
;
}
#
ifndef
_WIN32
int
octalStrToFileMode
(
const
std::string& fileModeStr)
{
return
safe_convert::stringTo<
int
>(fileModeStr,
0666
, std::oct);
}
inline
Error
changeFileModeImpl
(
const
std::string& filePath,
mode_t
mode)
{
//
change the mode
errno =
0
;
if
(::
chmod
(filePath.
c_str
(), mode) <
0
)
{
Error error =
systemError
(errno,
ERROR_LOCATION
);
error.
addProperty
(
"
path
"
, filePath);
return
error;
}
else
return
Success
();
}
#
endif
bool
copySingleItem
(
const
FilePath& from,
const
FilePath& to,
const
FilePath& path,
bool
overwrite)
{
std::string relativePath = path.
getRelativePath
(from);
FilePath target = to.
completePath
(relativePath);
Error error = path.
isDirectory
() ?
target.
ensureDirectory
() :
path.
copy
(target, overwrite);
if
(error)
log::logError
(error);
return
true
;
}
void
logError
(
path_t
path,
const
boost::filesystem::filesystem_error& e,
const
ErrorLocation& errorLocation)
{
Error
error
(e.
code
(), errorLocation);
addErrorProperties
(path, &error);
log::logError
(error, errorLocation);
}
Error
notFoundError
(
const
FilePath& filePath,
const
ErrorLocation& location)
{
Error error =
pathNotFoundError
(location);
if
(!filePath.
isEmpty
())
error.
addProperty
(
"
path
"
, filePath.
getAbsolutePath
());
return
error;
}
}
//
anonymous namespace
//
FilePath ============================================================================================================
struct
FilePath
::Impl
{
/*
*
* @brief Default constructor.
*/
Impl
() =
default
;
/*
*
* @brief Constructor.
*
* @param in_path The underlying path of this FilePath.
*/
explicit
Impl
(
path_t
in_path) :
Path(std::move(in_path))
{
}
/*
* The underlying path of this FilePath.
*/
path_t
Path;
};
FilePath::FilePath
() :
m_impl
(
new
Impl())
{
}
FilePath::FilePath
(
const
std::string& in_absolutePath) :
m_impl
(
new
Impl(fromString(std::string(in_absolutePath.c_str()))))
//
thwart ref-count
{
}
#
ifdef
_WIN32
FilePath::FilePath
(
const
std::wstring& absolutePath)
: m_impl(
new
Impl(absolutePath))
//
thwart ref-count
{
}
#
endif
FilePath::FilePath
(
const
char
* in_absolutePath) :
m_impl
(in_absolutePath ?
new
Impl(fromString(in_absolutePath)) :
new
Impl())
{
if
(in_absolutePath ==
nullptr
)
{
log::logDebugMessage
(
"
Creating an empty FilePath from a null path
"
,
ERROR_LOCATION
);
}
}
#
ifdef
_WIN32
FilePath::FilePath
(
const
wchar_t
* in_absolutePath)
: m_impl(in_absolutePath ?
new
Impl(in_absolutePath):
new Impl())
{
if
(in_absolutePath ==
nullptr
)
{
log::logDebugMessage
(
"
Creating an empty FilePath from a null path
"
,
ERROR_LOCATION
);
}
}
#
endif
bool
FilePath::
operator
==(
const
FilePath& in_other)
const
{
return
m_impl->
Path
== in_other.
m_impl
->
Path
;
}
bool
FilePath::
operator
!=(
const
FilePath& in_other)
const
{
return
m_impl->
Path
!= in_other.
m_impl
->
Path
;
}
bool
FilePath::
operator
<(
const
FilePath& in_other)
const
{
return
m_impl->
Path
< in_other.
m_impl
->
Path
;
}
std::string
FilePath::createAliasedPath
(
const
FilePath& in_filePath,
const
FilePath& in_userHomePath)
{
//
Special case for "~"
if
(in_filePath == in_userHomePath)
return
homePathLeafAlias
();
#
ifdef
_WIN32
//
Also check for case where paths are identical
//
after normalizing separators.
bool
samePath =
in_filePath.
m_impl
->
Path
.
generic_path
() ==
in_userHomePath.
m_impl
->
Path
.
generic_path
();
if
(samePath)
return
homePathLeafAlias
();
#
endif
//
if the path is contained within the home path then alias it
if
(in_filePath.
isWithin
(in_userHomePath))
{
std::string homeRelativePath = in_filePath.
getRelativePath
(in_userHomePath);
std::string aliasedPath =
homePathAlias
() + homeRelativePath;
return
aliasedPath;
}
else
//
no aliasing
{
return
in_filePath.
getAbsolutePath
();
}
}
bool
FilePath::exists
(
const
std::string& in_filePath)
{
if
(in_filePath.
empty
())
return
false
;
path_t
p
(
fromString
(in_filePath));
try
{
return
boost::filesystem::exists
(p);
}
catch
(
const
boost::filesystem::filesystem_error& e)
{
logError
(p, e,
ERROR_LOCATION
);
return
false
;
}
}
bool
FilePath::isEqualCaseInsensitive
(
const
FilePath& in_filePath1,
const
FilePath& in_filePath2)
{
std::string file1Lower =
boost::algorithm::to_lower_copy
(in_filePath1.
getAbsolutePath
());
std::string file2Lower =
boost::algorithm::to_lower_copy
(in_filePath2.
getAbsolutePath
());
return
file1Lower < file2Lower;
}
bool
FilePath::isRootPath
(
const
std::string& in_filePath)
{
if
(in_filePath.
empty
())
return
false
;
path_t
p
(
fromString
(in_filePath));
try
{
return
p.
has_root_path
();
}
catch
(
const
boost::filesystem::filesystem_error& e)
{
logError
(p, e,
ERROR_LOCATION
);
return
false
;
}
}
Error
FilePath::makeCurrent
(
const
std::string& in_filePath)
{
return
FilePath
(in_filePath).
makeCurrentPath
();
}
FilePath
FilePath::resolveAliasedPath
(
const
std::string& in_aliasedPath,
const
FilePath& in_userHomePath)
{
//
Special case for empty string or "~"
if
(in_aliasedPath.
empty
() || (in_aliasedPath ==
homePathLeafAlias
()))
return
in_userHomePath;
//
if the path starts with the home alias then substitute the home path
if
(in_aliasedPath.
find
(
homePathAlias
()) ==
0
)
{
std::string resolvedPath = in_userHomePath.
getAbsolutePath
() +
in_aliasedPath.
substr
(
1
);
return
FilePath
(resolvedPath);
}
else
//
no aliasing, this is either an absolute path or path
//
relative to the current directory
{
return
FilePath::safeCurrentPath
(in_userHomePath).
completePath
(in_aliasedPath);
}
}
FilePath
FilePath::safeCurrentPath
(
const
FilePath& in_revertToPath)
{
try
{
#
ifdef
_WIN32
return
FilePath
(
boost::filesystem::current_path
().
wstring
());
#
else
return
FilePath
(
boost::filesystem::current_path
().
string
());
#
endif
}
catch
(
const
boost::filesystem::filesystem_error& e)
{
if
(e.
code
() != boost::system::errc::no_such_file_or_directory)
log::logError
(
Error
(e.
code
(),
ERROR_LOCATION
));
}
CATCH_UNEXPECTED_EXCEPTION
//
revert to the specified path if it exists, otherwise
//
take the user home path from the system
FilePath safePath = in_revertToPath;
if
(!safePath.
exists
())
safePath =
system::User::getUserHomePath
();
Error error = safePath.
makeCurrentPath
();
if
(error)
log::logError
(error);
return
safePath;
}
Error
FilePath::tempFilePath
(FilePath& out_filePath)
{
return
tempFilePath
(
std::string
(), out_filePath);
}
Error
FilePath::tempFilePath
(
const
std::string& in_extension, FilePath& out_filePath)
{
using
namespace
boost
::filesystem
;
try
{
path_t
path =
temp_directory_path
();
return
uniqueFilePath
(path.
string
(), in_extension, out_filePath);
}
catch
(
const
filesystem_error& e)
{
return
Error
(e.
code
(),
ERROR_LOCATION
);
}
//
keep compiler happy
return
pathNotFoundError
(
ERROR_LOCATION
);
}
Error
FilePath::uniqueFilePath
(
const
std::string& in_basePath, FilePath& out_filePath)
{
return
uniqueFilePath
(in_basePath,
std::string
(), out_filePath);
}
Error
FilePath::uniqueFilePath
(
const
std::string& in_basePath,
const
std::string& in_extension, FilePath& out_filePath)
{
using
namespace
boost
::filesystem
;
try
{
path_t
path =
absolute
(
unique_path
(),
fromString
(in_basePath));
std::string pathStr =
BOOST_FS_PATH2STR
(path);
if
(!in_extension.
empty
())
pathStr += in_extension;
out_filePath =
FilePath
(pathStr);
return
Success
();
}
catch
(
const
filesystem_error& e)
{
return
Error
(e.
code
(),
ERROR_LOCATION
);
}
//
keep compiler happy
return
pathNotFoundError
(
ERROR_LOCATION
);
}
#
ifndef
_WIN32
Error
FilePath::changeFileMode
(
const
std::string& fileModeStr)
const
{
return
changeFileModeImpl
(
getAbsolutePath
(),
octalStrToFileMode
(fileModeStr));
}
Error
FilePath::changeFileMode
(FileMode in_fileMode,
bool
in_setStickyBit)
const
{
mode_t
mode;
switch
(in_fileMode)
{
case
FileMode::
USER_READ_WRITE
:
mode =
S_IRUSR
|
S_IWUSR
;
break
;
case
FileMode::
USER_READ_WRITE_EXECUTE
:
mode =
S_IRUSR
|
S_IWUSR
|
S_IXUSR
;
break
;
case
FileMode::
USER_READ_WRITE_GROUP_READ
:
mode =
S_IRUSR
|
S_IWUSR
|
S_IRGRP
;
break
;
case
FileMode::
USER_READ_WRITE_ALL_READ
:
mode =
S_IRUSR
|
S_IWUSR
|
S_IRGRP
|
S_IROTH
;
break
;
case
FileMode::
USER_READ_WRITE_EXECUTE_ALL_READ_EXECUTE
:
mode =
S_IRWXU
|
S_IRGRP
|
S_IXGRP
|
S_IROTH
|
S_IXOTH
;
break
;
case
FileMode::
ALL_READ
:
mode =
S_IRUSR
|
S_IRGRP
|
S_IROTH
;
break
;
case
FileMode::
ALL_READ_WRITE
:
mode =
S_IRUSR
|
S_IWUSR
|
S_IRGRP
|
S_IWGRP
|
S_IROTH
|
S_IWOTH
;
break
;
case
FileMode::
ALL_READ_WRITE_EXECUTE
:
mode =
S_IRWXU
|
S_IRWXG
|
S_IRWXO
;
break
;
case
FileMode::
USER_READ_WRITE_EXECUTE_GROUP_READ_WRITE_EXECUTE_ALL_READ_EXECUTE
:
mode =
S_IRWXU
|
S_IRWXG
|
S_IROTH
|
S_IXOTH
;
break
;
default
:
return
systemError
(
ENOTSUP
,
ERROR_LOCATION
);
}
//
check for sticky bit
if
(in_setStickyBit)
mode |=
S_ISVTX
;
return
changeFileModeImpl
(
getAbsolutePath
(), mode);
}
Error
FilePath::changeOwnership
(
const
system::User& in_newUser,
bool
in_recursive,
const
RecursiveIterationFunction& in_shouldChown)
const
{
//
changes ownership of file to the server user
auto
chown = [&](
const
std::string& in_absolutePath)
{
return
core::system::posix::posixCall<
int
>(
boost::bind
(::chown,
in_absolutePath.
c_str
(),
in_newUser.
getUserId
(),
in_newUser.
getGroupId
()),
ERROR_LOCATION
);
};
Error error =
chown
(
getAbsolutePath
());
if
(error)
{
error.
addProperty
(
"
path
"
,
getAbsolutePath
());
return
error;
}
if
(!in_recursive)
return
Success
();
//
recurse into subdirectories
if
(
isDirectory
())
{
getChildrenRecursive
([&](
int
depth,
const
FilePath& child)
{
if
(in_shouldChown && !
in_shouldChown
(depth, child))
return
true
;
error =
chown
(child.
getAbsolutePath
());
if
(error)
error.
addProperty
(
"
path
"
, child.
getAbsolutePath
());
//
if there was an error, stop iterating
return
!error;
});
}
return
error;
}
#
endif
//
note: this differs from complete in the following ways:
//
- the passed path can be an empty string (returns self)
//
- the passed path must be relative
FilePath
FilePath::completeChildPath
(
const
std::string& in_filePath)
const
{
FilePath childPath;
Error error =
completeChildPath
(in_filePath, childPath);
if
(error)
log::logError
(error);
return
childPath;
}
Error
FilePath::completeChildPath
(
const
std::string& in_filePath, FilePath& out_childPath)
const
{
try
{
if
(in_filePath.
empty
())
{
out_childPath = *
this
;
}
else
{
//
confirm this is a relative path
path_t
relativePath
(
fromString
(in_filePath));
if
(relativePath.
has_root_path
())
{
throw
boost::filesystem::filesystem_error
(
"
absolute path not permitted
"
,
boost::system::error_code
(
boost::system::errc::no_such_file_or_directory,
boost::system::system_category
()));
}
out_childPath =
completePath
(in_filePath);
if
(!out_childPath.
isWithin
(*
this
))
{
throw
boost::filesystem::filesystem_error
(
"
child path must be inside parent path
"
,
boost::system::error_code
(
boost::system::errc::no_such_file_or_directory,
boost::system::system_category
()));
}
}
}
catch
(
const
boost::filesystem::filesystem_error& e)
{
out_childPath = *
this
;
Error
error
(e.
code
(),
ERROR_LOCATION
);
addErrorProperties
(m_impl->
Path
, &error);
error.
addProperty
(
"
path
"
, in_filePath);
return
error;
}
return
Success
();
}
FilePath
FilePath::completePath
(
const
std::string& in_filePath)
const
{
//
in-theory boost::filesystem::complete can throw but the conditions
//
are very obscure and are in any case a programming error. therefore,
//
we log silently if there is an error so that clients don't have to
//
deal with any error states (if there an error then a copy of
//
this path is returned)
try
{
//
NOTE: The path gets round-tripped through toString/fromString, would
//
be nice to have a direct constructor
return
FilePath
(
BOOST_FS_PATH2STR
(
BOOST_FS_COMPLETE
(in_filePath, m_impl->
Path
)));
}
catch
(
const
boost::filesystem::filesystem_error& e)
{
Error
error
(e.
code
(),
ERROR_LOCATION
);
addErrorProperties
(m_impl->
Path
, &error);
error.
addProperty
(
"
path
"
, in_filePath);
log::logError
(error);
return
*
this
;
}
}
Error
FilePath::copy
(
const
FilePath& in_targetPath,
bool
overwrite)
const
{
try
{
using
boost::filesystem::copy_options;
boost::filesystem::copy_file
(
m_impl->
Path
,
in_targetPath.
m_impl
->
Path
,
overwrite ? copy_options::overwrite_existing : copy_options::none);
return
Success
();
}
catch
(
const
boost::filesystem::filesystem_error& e)
{
Error
error
(e.
code
(),
ERROR_LOCATION
);
addErrorProperties
(m_impl->
Path
, &error);
error.
addProperty
(
"
target-path
"
, in_targetPath.
getAbsolutePath
());
return
error;
}
}
Error
FilePath::copyDirectoryRecursive
(
const
FilePath& in_targetPath,
bool
overwrite)
const
{
Error error = in_targetPath.
ensureDirectory
();
if
(error)
return
error;
return
getChildrenRecursive
(
boost::bind
(copySingleItem, *
this
, in_targetPath, _2, overwrite));
}
Error
FilePath::createDirectory
(
const
std::string& in_filePath)
const
{
try
{
path_t
targetDirectory;
if
(in_filePath.
empty
())
targetDirectory = m_impl->
Path
;
else
targetDirectory =
BOOST_FS_COMPLETE
(in_filePath, m_impl->
Path
);
boost::filesystem::create_directories
(targetDirectory);
return
Success
();
}
catch
(
const
boost::filesystem::filesystem_error& e)
{
Error
error
(e.
code
(),
ERROR_LOCATION
);
addErrorProperties
(m_impl->
Path
, &error);
error.
addProperty
(
"
target-dir
"
, in_filePath);
return
error;
}
}
Error
FilePath::ensureDirectory
()
const
{
if
(!
exists
())
return
createDirectory
(
std::string
());
else
return
Success
();
}
Error
FilePath::ensureFile
()
const
{
//
nothing to do if the file already exists
if
(
exists
())
return
Success
();
//
create output stream to ensure file creation
std::shared_ptr<std::ostream> pStream;
Error error =
openForWrite
(pStream);
if
(error)
return
error;
//
release file handle
pStream->
flush
();
pStream.
reset
();
return
Success
();
}
bool
FilePath::exists
()
const
{
try
{
return
!
isEmpty
() &&
boost::filesystem::exists
(m_impl->
Path
);
}
catch
(
const
boost::filesystem::filesystem_error& e)
{
if
(e.
code
() != boost::system::errc::permission_denied)
logError
(m_impl->
Path
, e,
ERROR_LOCATION
);
return
false
;
}
}
std::string
FilePath::getAbsolutePath
()
const
{
if
(
isEmpty
())
return
std::string
();
else
return
BOOST_FS_PATH2STR
(m_impl->
Path
);
}
std::string
FilePath::getAbsolutePathNative
()
const
{
if
(
isEmpty
())
return
std::string
();
else
return
BOOST_FS_PATH2STRNATIVE
(m_impl->
Path
);
}
#
ifdef
_WIN32
std::wstring
FilePath::getAbsolutePathW
()
const
{
if
(
isEmpty
())
return
std::wstring
();
else
return
m_impl->
Path
.
wstring
();
}
#
endif
std::string
FilePath::getCanonicalPath
()
const
{
if
(
isEmpty
())
return
std::string
();
else
return
BOOST_FS_PATH2STR
(
boost::filesystem::canonical
(m_impl->
Path
));
}
Error
FilePath::getChildren
(std::vector<FilePath>& out_filePaths)
const
{
if
(!
exists
())
return
notFoundError
(*
this
,
ERROR_LOCATION
);
try
{
dir_iterator end;
for
(dir_iterator
itr
(m_impl->
Path
); itr != end; ++itr)
{
//
NOTE: The path gets round-tripped through toString/fromString, would
//
be nice to have a direct constructor
std::string itemPath =
BOOST_FS_PATH2STR
(itr->
path
());
out_filePaths.
emplace_back
(itemPath);
}
return
Success
();
}
catch
(
const
boost::filesystem::filesystem_error& e)
{
Error
error
(e.
code
(),
ERROR_LOCATION
);
addErrorProperties
(m_impl->
Path
, &error);
return
error;
}
}
Error
FilePath::getChildrenRecursive
(
const
RecursiveIterationFunction& in_iterationFunction)
const
{
if
(!
exists
())
return
notFoundError
(*
this
,
ERROR_LOCATION
);
try
{
recursive_dir_iterator end;
for
(recursive_dir_iterator
itr
(m_impl->
Path
); itr != end; ++itr)
{
//
NOTE: The path gets round-tripped through toString/fromString, would
//
be nice to have a direct constructor
if
(!
in_iterationFunction
(itr.
depth
(),
FilePath
(
BOOST_FS_PATH2STR
(itr->
path
()))))
{
//
end the iteration if requested
break
;
}
}
return
Success
();
}
catch
(
const
boost::filesystem::filesystem_error& e)
{
Error
error
(e.
code
(),
ERROR_LOCATION
);
addErrorProperties
(m_impl->
Path
, &error);
return
error;
}
}
std::string
FilePath::getExtension
()
const
{
return
BOOST_FS_STRING
(m_impl->
Path
.
extension
());
}
std::string
FilePath::getExtensionLowerCase
()
const
{
return
boost::algorithm::to_lower_copy
(
getExtension
());
}
#
ifndef
_WIN32
Error
FilePath::getFileMode
(FileMode& out_fileMode)
const
{
struct
stat
st;
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL