FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
rstudio/src/cpp/session/modules/SessionFilesQuotas.cpp at master · pearsonca/rstudio · GitHub
pearsonca
/
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
/
session
/
modules
/
SessionFilesQuotas.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
246 lines (211 loc) · 5.88 KB
Breadcrumbs
rstudio
/
src
/
cpp
/
session
/
modules
/
SessionFilesQuotas.cpp
Copy path
File metadata and controls
246 lines (211 loc) · 5.88 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
/*
* SessionFilesQuotas.cpp
*
* Copyright (C) 2009-12 by RStudio, PBC
*
* Unless you have received this program directly from RStudio pursuant
* to the terms of a commercial license agreement with RStudio, 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
"
SessionFilesQuotas.hpp
"
#
include
<
iostream
>
#
include
<
boost/tokenizer.hpp
>
#
include
<
boost/lexical_cast.hpp
>
#
include
<
boost/numeric/conversion/cast.hpp
>
#
include
<
core/BoostThread.hpp
>
#
include
<
shared_core/Error.hpp
>
#
include
<
core/BoostErrors.hpp
>
#
include
<
core/Log.hpp
>
#
include
<
core/system/Process.hpp
>
#
include
<
r/RExec.hpp
>
#
include
<
session/SessionModuleContext.hpp
>
using
namespace
rstudio
::core
;
namespace
rstudio
{
namespace
session
{
namespace
modules
{
namespace
files
{
namespace
quotas
{
namespace
{
//
does the system have quotas?
bool
s_systemHasQuotas =
false
;
struct
QuotaInfo
{
typedef
long
long
size_type;
QuotaInfo
()
: hasQuota(
false
), used(
0
), quota(
0
), limit(
0
)
{
}
bool
hasQuota;
size_type used;
size_type quota;
size_type limit;
};
void
quotaInfoToJson
(
const
QuotaInfo& quotaInfo,
json::Object* pQuotaInfoJson)
{
//
first get the data into json serializable types
double
used =
0
;
double
quota =
0
;
double
limit =
0
;
try
{
used = boost::numeric_cast<
double
>(quotaInfo.
used
);
quota = boost::numeric_cast<
double
>(quotaInfo.
quota
);
limit = boost::numeric_cast<
double
>(quotaInfo.
limit
);
}
catch
(boost::numeric::bad_numeric_cast& e)
{
LOG_ERROR_MESSAGE
(
std::string
(
"
Error converting quota info to double:
"
) +
e.
what
());
}
//
write to json
json::Object& quotaInfoJson = *pQuotaInfoJson;
quotaInfoJson[
"
used
"
] = used;
quotaInfoJson[
"
quota
"
] = quota;
quotaInfoJson[
"
limit
"
] = limit;
}
QuotaInfo::size_type
quotaBytes
(
const
std::string& quotaKb)
{
QuotaInfo::size_type kb = boost::lexical_cast<QuotaInfo::size_type>(quotaKb);
return
kb *
1024L
;
}
Error
parseQuotaInfo
(
const
std::string& quotaInfo, QuotaInfo* pInfo)
{
//
if there was no quota info returned then there is no quota on this box
if
(quotaInfo.
empty
())
{
pInfo->
hasQuota
=
false
;
return
Success
();
}
//
tokenzie results and seek quota info
int
resultIndex =
0
;
boost::char_separator<
char
>
sep
(
"
\t
"
);
typedef
boost::tokenizer<boost::char_separator<
char
> > QuotaInfoTokenizer;
QuotaInfoTokenizer
tok
(quotaInfo, sep);
for
(QuotaInfoTokenizer::const_iterator it = tok.
begin
();
it != tok.
end
();
++it)
{
try
{
switch
(resultIndex++)
{
//
ingore first entry (device)
case
0
:
break
;
//
Used
case
1
:
pInfo->
used
=
quotaBytes
(*it);
break
;
//
Quota
case
2
:
pInfo->
quota
=
quotaBytes
(*it);
break
;
//
Limit
case
3
:
pInfo->
limit
=
quotaBytes
(*it);
pInfo->
hasQuota
=
true
;
break
;
}
}
catch
(boost::bad_lexical_cast&)
{
return
systemError
(boost::system::errc::result_out_of_range,
ERROR_LOCATION
);
}
//
out of here....
if
(pInfo->
hasQuota
)
break
;
}
//
make sure we got all of the results
if
(!pInfo->
hasQuota
)
{
return
systemError
(boost::system::errc::result_out_of_range,
ERROR_LOCATION
);
}
else
{
return
Success
();
}
}
void
checkQuotaThread
()
{
try
{
//
run the command
core::system::ProcessResult result;
Error error =
runCommand
(
"
xfs_quota -c 'quota -N'
"
,
core::system::ProcessOptions
(),
&result);
if
(error)
{
LOG_ERROR
(error);
return
;
}
//
parse output
QuotaInfo quotaInfo;
error =
parseQuotaInfo
(result.
stdOut
, "aInfo);
if
(error)
{
LOG_ERROR
(error);
return
;
}
//
send event only if there are quotas established
if
(quotaInfo.
hasQuota
)
{
json::Object quotaInfoJson;
quotaInfoToJson
(quotaInfo, "aInfoJson);
ClientEvent
event
(client_events::
kQuotaStatus
, quotaInfoJson);
module_context::enqueClientEvent
(event);
}
}
CATCH_UNEXPECTED_EXCEPTION
}
}
//
anonymous namespace
Error
initialize
()
{
//
one time initialization of s_systemHasQuotas
if
( (
session::options
().
programMode
() ==
kSessionProgramModeServer
) &&
session::options
().
limitXfsDiskQuota
() )
{
std::string out;
Error error =
r::exec::system
(
"
which xfs_quota
"
, &out);
s_systemHasQuotas = !out.
empty
();
}
else
{
s_systemHasQuotas =
false
;
}
return
Success
();
}
void
checkQuotaStatus
()
{
if
(s_systemHasQuotas)
{
try
{
//
block all signals for launch of background thread (will cause it
//
to never receive signals)
core::system::SignalBlocker signalBlocker;
Error error = signalBlocker.
blockAll
();
if
(error)
LOG_ERROR
(error);
boost::thread
t
(checkQuotaThread);
}
catch
(
const
boost::thread_resource_error& e)
{
LOG_ERROR
(
Error
(
boost::thread_error::ec_from_exception
(e),
ERROR_LOCATION
));
}
}
}
}
//
namespace quotas
}
//
namespace files
}
//
namespace modules
}
//
namespace session
}
//
namespace rstudio
Back
|
FazBrowse Home
|
New Git URL