FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
rstudio/src/cpp/core/FileUtils.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
/
core
/
FileUtils.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
208 lines (173 loc) · 5.15 KB
Breadcrumbs
rstudio
/
src
/
cpp
/
core
/
FileUtils.cpp
Copy path
File metadata and controls
208 lines (173 loc) · 5.15 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
/*
* FileUtils.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 Software, then
* this program is licensed to you under the terms of version 3 of the
* GNU Affero General Public License. This program is distributed WITHOUT
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
*
*/
#
include
<
fstream
>
#
include
<
iostream
>
#
include
<
boost/bind/bind.hpp
>
#
include
<
core/FileUtils.hpp
>
#
include
<
core/FileSerializer.hpp
>
#
include
<
shared_core/FilePath.hpp
>
#
include
<
core/StringUtils.hpp
>
#
include
<
core/system/System.hpp
>
#
ifndef
_WIN32
#
include
<
core/system/PosixUser.hpp
>
#
endif
using
namespace
boost
::placeholders
;
namespace
rstudio
{
namespace
core
{
namespace
file_utils
{
namespace
{
bool
copySourceFile
(
const
FilePath& sourceDir,
const
FilePath& destDir,
const
FilePath& sourceFilePath)
{
//
compute the target path
std::string relativePath = sourceFilePath.
getRelativePath
(sourceDir);
FilePath targetPath = destDir.
completePath
(relativePath);
//
if the copy item is a directory just create it
if
(sourceFilePath.
isDirectory
())
{
Error error = targetPath.
ensureDirectory
();
if
(error)
LOG_ERROR
(error);
}
//
otherwise copy it
else
{
Error error = sourceFilePath.
copy
(targetPath);
if
(error)
LOG_ERROR
(error);
}
return
true
;
}
}
//
anonymous namespace
//
NOTE: Assumes that the path string is UTF-8 encoded.
std::string
shortPathName
(
const
std::string& path)
{
#
ifdef
_WIN32
std::wstring widePath =
string_utils::utf8ToWide
(path);
DWORD
length = ::
GetShortPathNameW
(widePath.
c_str
(),
nullptr
,
0
);
if
(length ==
0
)
return
path;
std::vector<
WCHAR
>
buffer
(length,
0
);
DWORD
result = ::
GetShortPathNameW
(widePath.
c_str
(), &buffer[
0
], length);
if
(result ==
0
)
return
path;
return
string_utils::wideToUtf8
(
std::wstring
(&buffer[
0
]));
#
else
return
path;
#
endif
}
FilePath
uniqueFilePath
(
const
FilePath& parent,
const
std::string& prefix,
const
std::string& extension)
{
//
try up to 100 times then fallback to a uuid
for
(
int
i=
0
; i<
100
; i++)
{
//
get a shortened uuid
std::string shortentedUuid =
core::system::generateShortenedUuid
();
//
form full path
FilePath uniqueDir = parent.
completeChildPath
(prefix + shortentedUuid + extension);
//
return if it doesn't exist
if
(!uniqueDir.
exists
())
return
uniqueDir;
}
//
if we didn't succeed then return prefix + uuid
return
parent.
completeChildPath
(prefix +
core::system::generateUuid
(
false
) + extension);
}
std::string
readFile
(
const
FilePath& filePath)
{
std::ifstream
stream
(
filePath.
getAbsolutePath
().
c_str
(),
std::ios::in | std::ios::binary);
std::string content;
if
(stream)
{
stream.
seekg
(
0
, std::ios::end);
std::streamsize size = stream.
tellg
();
content.
resize
(
static_cast
<
size_t
>(size));
stream.
seekg
(
0
, std::ios::beg);
stream.
read
(&content[
0
], size);
stream.
close
();
}
return
content;
}
Error
writeFile
(
const
FilePath& filePath,
const
std::string& content)
{
std::shared_ptr<std::ostream> stream;
Error error = filePath.
openForWrite
(stream);
if
(error)
{
return
error;
}
*stream << content;
stream->
flush
();
stream.
reset
();
return
Success
();
}
#
ifdef
_WIN32
//
test a filename to see if it corresponds to a reserved device name on
//
Windows
bool
isWindowsReservedName
(
const
std::string& name)
{
const
char
* reserved[] = {
"
con
"
,
"
prn
"
,
"
aux
"
,
"
nul
"
,
"
com1
"
,
"
com2
"
,
"
com3
"
,
"
com4
"
,
"
com5
"
,
"
com6
"
,
"
com7
"
,
"
com8
"
,
"
com9
"
,
"
lpt1
"
,
"
lpt2
"
,
"
lpt3
"
,
"
lpt4
"
,
"
lpt5
"
,
"
lpt6
"
,
"
lpt7
"
,
"
lpt8
"
,
"
lpt9
"
};
std::string lowerName =
string_utils::toLower
(name);
for
(
int
i =
0
; i <
sizeof
(reserved)/
sizeof
(
char
*); i++)
{
if
(lowerName == reserved[i])
{
return
true
;
}
}
return
false
;
}
#
endif
Error
copyDirectory
(
const
FilePath& sourceDirectory,
const
FilePath& targetDirectory)
{
//
create the target directory
Error error = targetDirectory.
ensureDirectory
();
if
(error)
return
error;
//
iterate over the source
return
sourceDirectory.
getChildrenRecursive
(
boost::bind
(copySourceFile, sourceDirectory, targetDirectory, _2));
}
bool
isDirectoryWriteable
(
const
FilePath& directory)
{
std::string
prefix
(
#
ifndef
_WIN32
"
.
"
#
endif
"
write-test-
"
);
FilePath testFile = directory.
completePath
(prefix +
core::system::generateUuid
());
Error error =
core::writeStringToFile
(testFile,
"
test
"
);
if
(error)
{
return
false
;
}
else
{
error = testFile.
removeIfExists
();
if
(error)
LOG_ERROR
(error);
return
true
;
}
}
}
//
namespace file_utils
}
//
namespace core
}
//
namespace rstudio
Back
|
FazBrowse Home
|
New Git URL