/
SessionSourceDatabase.cpp
542 lines (451 loc) · 14.8 KB
/
SessionSourceDatabase.cpp
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
/*
* SessionSourceDatabase.cpp
*
* Copyright (C) 2009-11 by RStudio, Inc.
*
* 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 <session/SessionSourceDatabase.hpp>
#include <string>
#include <vector>
#include <algorithm>
#include <boost/bind.hpp>
#include <boost/foreach.hpp>
#include <boost/regex.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <core/Log.hpp>
#include <core/Exec.hpp>
#include <core/Error.hpp>
#include <core/FilePath.hpp>
#include <core/Hash.hpp>
#include <core/FileSerializer.hpp>
#include <core/DateTime.hpp>
#include <core/system/System.hpp>
#include <core/http/Util.hpp>
#include <r/RUtil.hpp>
#include <session/SessionModuleContext.hpp>
// NOTE: if a file is deleted then its properties database entry is not
// deleted. this has two implications:
//
// - storage is not reclaimed
// - the properties can be "resurreced" and re-attached to another
// file with the same path
//
// One way to overcome this might be to use filesystem metadata to store
// properties rather than a side-database
using namespace core;
namespace session {
namespace source_database {
namespace {
struct PropertiesDatabase
{
FilePath path;
FilePath indexFile;
std::map<std::string,std::string> index;
};
Error getPropertiesDatabase(PropertiesDatabase* pDatabase)
{
pDatabase->path = path().complete("properties");
Error error = pDatabase->path.ensureDirectory();
if (error)
return error;
pDatabase->indexFile = pDatabase->path.complete("INDEX");
if (pDatabase->indexFile.exists())
return readStringMapFromFile(pDatabase->indexFile, &(pDatabase->index));
else
return Success();
}
Error putProperties(const std::string& path, const json::Object& properties)
{
// url escape path (so we can use key=value persistence)
std::string escapedPath = http::util::urlEncode(path);
// get properties database
PropertiesDatabase propertiesDB;
Error error = getPropertiesDatabase(&propertiesDB);
if (error)
return error;
// use existing properties file if it exists, otherwise create new
bool updateIndex = false;
std::string propertiesFile = propertiesDB.index[escapedPath];
if (propertiesFile.empty())
{
propertiesFile = core::system::generateUuid(false);
propertiesDB.index[escapedPath] = propertiesFile;
updateIndex = true;
}
// write the file
std::ostringstream ostr ;
json::writeFormatted(properties, ostr);
FilePath propertiesFilePath = propertiesDB.path.complete(propertiesFile);
error = writeStringToFile(propertiesFilePath, ostr.str());
if (error)
return error;
// update the index if necessary
if (updateIndex)
return writeStringMapToFile(propertiesDB.indexFile, propertiesDB.index);
else
return Success();
}
Error getProperties(const std::string& path, json::Object* pProperties)
{
// url escape path (so we can use key=value persistence)
std::string escapedPath = http::util::urlEncode(path);
// get properties database
PropertiesDatabase propertiesDB;
Error error = getPropertiesDatabase(&propertiesDB);
if (error)
return error;
// check for properties file
std::string propertiesFile = propertiesDB.index[escapedPath];
if (propertiesFile.empty())
{
// return empty object if there is none
*pProperties = json::Object();
return Success();
}
// read the properties file
std::string contents ;
FilePath propertiesFilePath = propertiesDB.path.complete(propertiesFile);
error = readStringFromFile(propertiesFilePath, &contents,
options().sourceLineEnding());
if (error)
return error;
// parse the json
json::Value value;
if ( !json::parse(contents, &value) )
return systemError(boost::system::errc::bad_message, ERROR_LOCATION);
// return it
if (json::isType<json::Object>(value))
*pProperties = value.get_obj();
return Success();
}
} // anonymous namespace
SourceDocument::SourceDocument(const std::string& type)
{
id_ = core::system::generateUuid();
type_ = type;
setContents("");
dirty_ = false;
created_ = date_time::millisecondsSinceEpoch();
sourceOnSave_ = false;
}
std::string SourceDocument::getProperty(const std::string& name)
{
if (properties_.find(name) != properties_.end())
{
json::Value valueJson = properties_[name];
if (json::isType<std::string>(valueJson))
return valueJson.get_str();
else
return "";
}
else
{
return "";
}
}
// set contents from string
void SourceDocument::setContents(const std::string& contents)
{
contents_ = contents;
hash_ = hash::crc32Hash(contents_);
}
namespace {
Error readAndDecode(const FilePath& docPath,
const std::string& encoding,
bool allowSubstChars,
std::string* pContents)
{
// read contents
std::string encodedContents;
Error error = readStringFromFile(docPath, &encodedContents,
options().sourceLineEnding());
if (error)
return error ;
error = r::util::iconvstr(encodedContents, encoding, "UTF-8",
allowSubstChars, pContents);
if (error)
return error;
stripBOM(pContents);
// Detect invalid UTF-8 sequences and recover
error = string_utils::utf8Clean(pContents->begin(),
pContents->end(),
'?');
return error ;
}
} // anonymous namespace
// set contents from file
Error SourceDocument::setPathAndContents(const std::string& path,
bool allowSubstChars)
{
// resolve aliased path
FilePath docPath = module_context::resolveAliasedPath(path);
std::string contents;
Error error = readAndDecode(docPath, encoding(), allowSubstChars,
&contents);
if (error)
return error ;
// update path and contents
path_ = path;
setContents(contents);
lastKnownWriteTime_ = docPath.lastWriteTime();
return Success();
}
Error SourceDocument::updateDirty()
{
if (path().empty())
{
dirty_ = !contents_.empty();
}
else if (dirty_)
{
// This doesn't actually guarantee that dirty state is correct. All
// it does, at the most, is take a dirty document and mark it clean
// if the contents are the same as on disk. This is important because
// the client now has logic to detect when undo/redo causes a document
// to be reverted to its previous state (i.e. a dirty document can
// become clean through undo/redo), but that state doesn't get sent
// back to the server.
// We don't make a clean document dirty here, even if the contents
// on disk are different, because we will do that on the client side
// and the UI logic is a little complicated.
FilePath docPath = module_context::resolveAliasedPath(path());
if (docPath.exists() && docPath.size() <= (1024*1024))
{
std::string contents;
Error error = readAndDecode(docPath, encoding(), true, &contents);
if (error)
return error;
if (contents_.length() == contents.length() && hash_ == hash::crc32Hash(contents))
dirty_ = false;
}
}
return Success();
}
void SourceDocument::editProperties(json::Object& properties)
{
std::for_each(properties.begin(),
properties.end(),
boost::bind(&SourceDocument::editProperty, this, _1));
}
void SourceDocument::checkForExternalEdit(std::time_t* pTime)
{
*pTime = 0;
if (path_.empty())
return;
if (lastKnownWriteTime_ == 0)
return;
core::FilePath filePath = module_context::resolveAliasedPath(path_);
if (!filePath.exists())
return;
std::time_t newTime = filePath.lastWriteTime();
if (newTime != lastKnownWriteTime_)
*pTime = newTime;
}
void SourceDocument::updateLastKnownWriteTime()
{
lastKnownWriteTime_ = 0;
if (path_.empty())
return;
core::FilePath filePath = module_context::resolveAliasedPath(path_);
if (!filePath.exists())
return;
lastKnownWriteTime_ = filePath.lastWriteTime();
}
Error SourceDocument::readFromJson(json::Object* pDocJson)
{
// NOTE: since this class is the one who presumably persisted the
// json values in the first place we don't do "checked" access to
// the json data elements. if the persistence format differs from
// what we expect things will blow up. therefore if we change the
// persistence format we need to make sure this code is robust
// in the presence of the old format
json::Object& docJson = *pDocJson;
id_ = docJson["id"].get_str();
json::Value path = docJson["path"];
path_ = !path.is_null() ? path.get_str() : std::string();
json::Value type = docJson["type"];
type_ = !type.is_null() ? type.get_str() : std::string();
setContents(docJson["contents"].get_str());
dirty_ = docJson["dirty"].get_bool();
created_ = docJson["created"].get_real();
sourceOnSave_ = docJson["source_on_save"].get_bool();
// read safely (migration)
json::Value properties = docJson["properties"];
properties_ = !properties.is_null() ? properties.get_obj() : json::Object();
json::Value lastKnownWriteTime = docJson["lastKnownWriteTime"];
lastKnownWriteTime_ = !lastKnownWriteTime.is_null()
? lastKnownWriteTime.get_int64()
: 0;
json::Value encoding = docJson["encoding"];
encoding_ = !encoding.is_null() ? encoding.get_str() : std::string();
return Success();
}
void SourceDocument::writeToJson(json::Object* pDocJson) const
{
json::Object& jsonDoc = *pDocJson;
jsonDoc["id"] = id();
jsonDoc["path"] = !path().empty() ? path_ : json::Value();
jsonDoc["type"] = !type().empty() ? type_ : json::Value();
jsonDoc["hash"] = hash();
jsonDoc["contents"] = contents();
jsonDoc["dirty"] = dirty();
jsonDoc["created"] = created();
jsonDoc["source_on_save"] = sourceOnSave();
jsonDoc["properties"] = properties();
jsonDoc["lastKnownWriteTime"] = json::Value(
static_cast<boost::int64_t>(lastKnownWriteTime_));
jsonDoc["encoding"] = encoding_;
}
void SourceDocument::editProperty(const json::Object::value_type& property)
{
if (property.second.is_null())
{
properties_.erase(property.first);
}
else
{
properties_[property.first] = property.second;
}
}
bool sortByCreated(const SourceDocument& doc1, const SourceDocument& doc2)
{
return doc1.created() < doc2.created();
}
FilePath path()
{
return module_context::userScratchPath().complete("source_database");
}
Error get(const std::string& id, SourceDocument* pDoc)
{
FilePath filePath = source_database::path().complete(id);
if (filePath.exists())
{
// read the contents of the file
std::string contents ;
Error error = readStringFromFile(filePath, &contents,
options().sourceLineEnding());
if (error)
return error;
// parse the json
json::Value value;
if ( !json::parse(contents, &value) )
{
return systemError(boost::system::errc::invalid_argument,
ERROR_LOCATION);
}
// initialize doc from json
json::Object jsonDoc = value.get_obj();
return pDoc->readFromJson(&jsonDoc);
}
else
{
return systemError(boost::system::errc::no_such_file_or_directory,
ERROR_LOCATION);
}
}
Error getDurableProperties(const std::string& path, json::Object* pProperties)
{
return getProperties(path, pProperties);
}
Error list(std::vector<SourceDocument>* pDocs)
{
std::vector<FilePath> files ;
Error error = source_database::path().children(&files);
if (error)
return error ;
BOOST_FOREACH( FilePath& filePath, files )
{
if (!filePath.isDirectory())
{
// get the source doc
SourceDocument doc ;
Error error = source_database::get(filePath.filename(), &doc);
if (!error)
pDocs->push_back(doc);
else
LOG_ERROR(error);
}
}
return Success();
}
Error put(const SourceDocument& doc)
{
// get json representation
json::Object jsonDoc ;
doc.writeToJson(&jsonDoc);
std::ostringstream ostr ;
json::writeFormatted(jsonDoc, ostr);
// write to file
FilePath filePath = source_database::path().complete(doc.id());
Error error = writeStringToFile(filePath, ostr.str());
if (error)
return error ;
// write properties to durable storage
error = putProperties(doc.path(), doc.properties());
if (error)
LOG_ERROR(error);
return Success();
}
Error remove(const std::string& id)
{
return source_database::path().complete(id).removeIfExists();
}
Error removeAll()
{
std::vector<FilePath> files ;
Error error = source_database::path().children(&files);
if (error)
return error ;
BOOST_FOREACH( FilePath& filePath, files )
{
Error error = filePath.remove();
if (error)
return error ;
}
return Success();
}
Error getSourceDocumentsJson(core::json::Array* pJsonDocs)
{
// get the docs and sort them by created
std::vector<SourceDocument> docs ;
Error error = source_database::list(&docs);
if (error)
return error ;
std::sort(docs.begin(), docs.end(), sortByCreated);
// populate the array
pJsonDocs->clear();
BOOST_FOREACH( SourceDocument& doc, docs )
{
// Force dirty state to be checked.
// Client and server dirty state can get out of sync because
// undo/redo on the client side can make dirty documents
// become clean again. I tried pushing the client dirty state
// back to the server but couldn't convince myself that I
// got all the edge cases. This approach is simpler--just
// compare the contents in the doc database to the contents
// on disk, and only do it when listing documents. However
// it does mean that reloading the client may cause a dirty
// document to become clean (if the contents are identical
// to what's on disk).
error = doc.updateDirty();
if (error)
LOG_ERROR(error);
json::Object jsonDoc ;
doc.writeToJson(&jsonDoc);
pJsonDocs->push_back(jsonDoc);
}
return Success();
}
Error initialize()
{
// make sure the source database exists
return source_database::path().ensureDirectory();
}
} // namespace source_database
} // namesapce session