This repository was archived by the owner on Oct 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilesystem.cpp
More file actions
618 lines (515 loc) · 22.2 KB
/
filesystem.cpp
File metadata and controls
618 lines (515 loc) · 22.2 KB
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
/*
* Copyright (c) 2019 Riccardo Zaccone, Ksenia Del Conte Akimova, Alice Morano, Martina Bellissimo
*
* This file is part of Symposium.
* Symposium is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Symposium is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with Symposium. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* File: filesystem.cpp
* Project: Symposium
* Authors:
* Riccardo Zaccone <riccardo.zaccone at studenti.polito.it>
* Ksenia Del Conte Akimova <s256669 at studenti.polito.it>
* Alice Morano <s259158 at studenti.polito.it>
* Martina Bellissimo <s257307 at studenti.polito.it>
*
* Created on 18 Giugno 2019, 22.02
*/
#include <boost/serialization/access.hpp>
#include <boost/serialization/export.hpp>
#include <boost/serialization/unique_ptr.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/serialization/weak_ptr.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <fstream>
#include "filesystem.h"
#include "symbol.h"
#include "user.h"
BOOST_CLASS_EXPORT_IMPLEMENT(Symposium::file)
BOOST_CLASS_EXPORT_IMPLEMENT(Symposium::symlink)
BOOST_CLASS_EXPORT_IMPLEMENT(Symposium::directory)
using namespace Symposium;
uint_positive_cnt filesystem::idCounter;
std::shared_ptr<directory> directory::root;
template<class Archive>
void filesystem::serialize(Archive &ar, const unsigned int){
ar & id & name & sharingPolicy & strategy & idCounter;
};
template<class Archive>
void file::serialize(Archive &ar, const unsigned int){
ar & boost::serialization::base_object<Symposium::filesystem>(*this);
ar & doc;
};
template<class Archive>
void symlink::serialize(Archive &ar, const unsigned int){
ar & boost::serialization::base_object<Symposium::filesystem>(*this);
ar & absPathWithoutId & resId;
};
template<class Archive>
void directory::serialize(Archive &ar, const unsigned int){
ar & boost::serialization::base_object<Symposium::filesystem>(*this);
ar & root & contained & parent & self;
}
filesystem::filesystem(const std::string &name, const uint_positive_cnt::type idToAssign) : name(name){
if(idToAssign==0){
id=idCounter;
idCounter++;
}
else
id=idToAssign;
}
uint_positive_cnt::type filesystem::getId() const {
return id;
}
const std::string &filesystem::getName() const {
return name;
}
privilege filesystem::getUserPrivilege(const std::string &) const {
throw filesystemException(filesystemException::objSha, UnpackFileLineFunction());
}
uri &filesystem::getSharingPolicy() {
return sharingPolicy;
}
privilege filesystem::setUserPrivilege(const std::string &, privilege) {
throw filesystemException(filesystemException::objSha, UnpackFileLineFunction());
}
uri filesystem::setSharingPolicy(const std::string &, const uri &) {
throw filesystemException(filesystemException::objSha, UnpackFileLineFunction());
}
std::tuple<std::string, std::string> filesystem::separate(const std::string &path)
{
if(path.empty())
throw filesystemException(filesystemException::pathEmpty, UnpackFileLineFunction());
std::string path2;
std::string path3;
path3.append(path);
std::string id2;
if(path3.back()=='/') //if path terminate with "/", we need to remove it for separate correctly the id
path3.pop_back();
if(path3.at(0)=='.'||path3.at(0)=='/')
{
path3.erase(path3.begin()+0);
return separate(path3);
}
std::size_t found = path3.find_last_of("/\\");//find the last number which represent the id
if(found==std::string::npos)//if there isn't any "/" it means that I', alredy in the correct directory and the path represent only id
{
id2.append(path3);
path3.clear();
return std::make_tuple(path3, id2); //return clear path and the id
}
path2.append(path3,0, found); //path to the directory of the current user
auto it=path3.begin();
std::advance(it, found+1);
id2.append(it,path3.end()); //the id of directory where the current user want to insert the file
return std::make_tuple(path2, id2);
}
bool filesystem::pathIsValid2(const std::string &toCheck) {
std::regex pathPattern{R"(\.(\/[a-zA-Z0-9]+)+)"};
return !toCheck.empty() && std::regex_match(toCheck, pathPattern);
}
bool filesystem::moreOwner(const std::string &username)
{
return strategy->moreOwner(username);
}
bool filesystem::operator==(const filesystem &rhs) const {
return id == rhs.id;
}
bool filesystem::operator!=(const filesystem &rhs) const {
return !(rhs == *this);
}
void filesystem::setName(const std::string &name) {
filesystem::name = name;
}
file::file(const std::string &name, uint_positive_cnt::type idToAssign) : filesystem(name, idToAssign), doc(){
strategy=std::make_unique<RMOAccess>();
}
resourceType file::resType() const {
return resourceType::file;
}
const std::unordered_map<std::string, privilege> file::getUsers()
{
return strategy->getPermission();
}
privilege file::getUserPrivilege(const std::string &targetUser) const {
privilege userPriv= strategy->getPrivilege(targetUser);
return userPriv;
}
privilege file::setUserPrivilege(const std::string &targetUser, privilege newPrivilege) {
strategy->setPrivilege(targetUser,newPrivilege);
return newPrivilege;
}
uri file::setSharingPolicy(const std::string &actionUser, const uri &newSharingPrefs) {
if(strategy->validateAction(actionUser, privilege::owner)) {
this->sharingPolicy=newSharingPrefs;
}
else
throw filesystemException(filesystemException::notOwn, UnpackFileLineFunction());
return newSharingPrefs;
}
document & file::access(const user &targetUser, privilege accessMode) {
bool controllo=this->strategy->validateAction(targetUser.getUsername(), accessMode);
if(!controllo)
throw filesystemException(filesystemException::noPermission, UnpackFileLineFunction());
return doc.access(targetUser, accessMode);
}
void file::storeContent() const {
doc.store();
}
void file::deleteFromStrategy(const std::string &userName)
{
strategy->setPrivilege(userName, privilege::none);
}
std::string file::print(const std::string &targetUser, bool, unsigned int indent) const {
std::ostringstream typeres;
typeres<<resType(); //then the type of the resource
std::ostringstream priv;
priv<<getUserPrivilege(targetUser); //the privilege
std::string spaces;
if(indent>0)
spaces.insert(spaces.begin(), indent, ' ');
return typeres.str()+" "+std::to_string(getId())+" "+spaces+name+" " + priv.str();
}
const document &file::getDoc() const {
return doc;
}
bool file::validateAction(const std::string &userName, privilege priv) {
return strategy->validateAction(userName, priv);
}
void file::replacement(std::shared_ptr<file> replace){
std::swap(this->doc, replace->doc);
this->sharingPolicy=std::move(replace->sharingPolicy);
std::swap(this->strategy, replace->strategy);
}
bool file::operator==(const file &rhs) const {
return static_cast<const Symposium::filesystem &>(*this) == static_cast<const Symposium::filesystem &>(rhs) &&
doc == rhs.doc;
}
bool file::operator!=(const file &rhs) const {
return !(rhs == *this);
}
bool file::isReadyToRemove(const std::string &username) const {
return doc.getActiveUsers().empty();
}
directory::directory(const std::string &name, const uint_positive_cnt::type &idToAssign) : filesystem(name, idToAssign) {
strategy=std::make_unique<TrivialAccess>();
}
std::shared_ptr<directory> directory::emptyDir() {
//this way I should not interfere with ids generation
return std::shared_ptr<directory>(new directory("EmptyDir", -1));
}
std::shared_ptr<directory> directory::getRoot() {
if(root){
return root;
}
std::string name="/";
std::shared_ptr<directory> new_root(new directory(name));
new_root->self=new_root;
root=new_root;
return root;
}
std::tuple<std::string, std::string> directory::separateFirst(std::string path)
{
if(path.empty())
throw filesystemException(filesystemException::pathEmpty, UnpackFileLineFunction());
std::string path2;
std::string id;
if(path.at(0) == '.' || path.at(0) == '/')
{
path.erase(path.begin());
return separateFirst(path);
}
std::size_t found = path.find_first_of("/\\"); //find first character "/" in order to separate the first directory of the path
if(found==std::string::npos)//if there is none of "/" it means that the path is composed by only one directory
{
id.append(path);
path.clear();
return std::make_tuple(path, id);// return clear path and the id
}
path2.append(path,found, path.size());
id.append(path, 0, found);
return std::make_tuple(path2, id);
}
std::shared_ptr<filesystem> directory::get(const std::string &resPath, const std::string &resId) {
if(resPath == "" || resPath == "./")//if path is "here" so I only need to get the object with the id represented by name
{
auto it=std::find_if(contained.begin(), contained.end(),
[resId](std::shared_ptr<filesystem> i){return std::to_string(i->getId()) == resId;});
if(it==contained.end())
throw filesystemException(filesystemException::noGet, UnpackFileLineFunction());
return *it;
}
std::string idRes;
std::string pathRes;
if(resPath.back() == '/')
pathRes.append(resPath.begin(), resPath.end() - 1);
else
pathRes.append(resPath);
tie(pathRes, idRes)= separateFirst(pathRes);//otherwise need to separate the first directory and move into it
auto it=std::find_if(contained.begin(), contained.end(),
[idRes](std::shared_ptr<filesystem> i){return std::to_string(i->getId())==idRes;});
if(it==contained.end())
throw filesystemException(filesystemException::noGet, UnpackFileLineFunction());
if((*it)->resType()!=resourceType::directory)//if the element is not a directory, there is some error in path
throw filesystemException(filesystemException::noGet, UnpackFileLineFunction());
std::shared_ptr <directory> dir=std::dynamic_pointer_cast<directory>(*it);
return dir->get(pathRes, resId);
}
std::shared_ptr<directory> directory::getDir(const std::string &resPath, const std::string &resId) {
std::shared_ptr<filesystem> res=this->get(resPath, resId);
if(res->resType()!=resourceType::directory)
throw filesystemException(filesystemException::noGetDir, UnpackFileLineFunction());
return std::dynamic_pointer_cast<directory>(res);
}
std::shared_ptr<file> directory::getFile(const std::string &resPath, const std::string &resId) {
std::shared_ptr<filesystem> res=this->get(resPath, resId);
if(res->resType()==resourceType::file)
return std::dynamic_pointer_cast<file>(res);
if(res->resType()==resourceType::symlink) //is used also for symlink because symlink is the reference to the file object
{
std::shared_ptr<symlink> sym=std::dynamic_pointer_cast<symlink>(res);
std::string pathSym=sym->getPath();
std::string pathRes;
std::string idRes;
tie(pathRes, idRes)= separate(pathSym);
return this->root->getFile(pathRes, idRes);
}
throw filesystemException(filesystemException::noGetFile, UnpackFileLineFunction());
}
std::string directory::setName(const std::string &resPath, const std::string &resId, const std::string& newName) {
std::string pathRename;
std::string idRename;
std::shared_ptr<directory> save;
if(resPath!="./"){
tie(pathRename, idRename)= separate(resPath);
save=getDir(pathRename, idRename);
}
else
save=this->self.lock();
if(std::any_of(save->contained.begin(), save->contained.end(), [newName](const std::shared_ptr<filesystem> i){return i->getName()==newName;}))
throw filesystemException(filesystemException::sameName, UnpackFileLineFunction());
std::shared_ptr<filesystem> res=this->get(resPath, resId);
std::string old=res->getName();
res->setName(newName);
return old;
}
std::shared_ptr<directory> directory::addDirectory(const std::string &resName, uint_positive_cnt::type idToAssign) {
if(std::any_of(contained.begin(), contained.end(), [resName](const std::shared_ptr<filesystem> &i){return i->getName() == resName;}))
throw filesystemException(filesystemException::sameName, UnpackFileLineFunction());
std::shared_ptr<directory> newDir(new directory(resName, idToAssign));
newDir->parent=this->self;
newDir->self=newDir;
contained.push_back(newDir);
return newDir;
}
std::shared_ptr<file> directory::addFile(const std::string &resPath, const std::string &resName, uint_positive_cnt::type idToAssign) {
std::string pathAdd;
std::string idAdd;
std::shared_ptr<directory> save;
if(resPath != "./")
{
tie(pathAdd, idAdd)= separate(resPath);
save=getDir(pathAdd, idAdd);
}
else
save=self.lock();
if(std::any_of(save->contained.begin(), save->contained.end(), [resName](const std::shared_ptr<filesystem> i){return i->getName() == resName;}))
throw filesystemException(filesystemException::sameName, UnpackFileLineFunction());
std::shared_ptr<file> newFile(new file(resName, idToAssign));
save->contained.push_back(newFile);
return newFile;
}
std::shared_ptr<class symlink>
directory::addLink(const std::string &symPath, const std::string &symName, const std::string &absPathWithoutId,
const std::string &resId, uint_positive_cnt::type idToAssign)
{
std::string pathAdd;
std::string idAdd;
std::shared_ptr<directory> save;
if(symPath != "./")
{
tie(pathAdd, idAdd)= separate(symPath);
save=getDir(pathAdd, idAdd);
}
else
save=self.lock();
if(std::any_of(save->contained.begin(), save->contained.end(), [symName](const std::shared_ptr<filesystem> i){return i->getName() == symName;}))
throw filesystemException(filesystemException::sameName, UnpackFileLineFunction());
std::shared_ptr<symlink> newSym(new symlink(symName, absPathWithoutId, resId, idToAssign));
save->contained.push_back(newSym);
return newSym;
}
resourceType directory::resType() const {
return resourceType::directory;
}
document &
directory::access(const user &targetUser, const std::string &resPath, const std::string &resId, privilege accessMode) {
std::shared_ptr<file> f=getFile(resPath, resId);
return f->access(targetUser, accessMode);
}
std::shared_ptr<filesystem> directory::remove(const user &targetUser, const std::string &resPath, const std::string &resId) {
std::shared_ptr<filesystem> obj=this->get(resPath, resId); //take the object to remove
std::string idRem;
if(resPath != "./" && !resPath.empty())
{
std::string pathRem;
tie(pathRem, idRem)= separate(resPath);
std::shared_ptr<directory> dir=this->getDir(pathRem, idRem);
return dir->remove(targetUser, "", resId); //call remove recursivly if the path is not "here" in order to take the right directory of the object to remove
}
idRem=resId;//otherwise the path is "here"
if(obj->resType()==resourceType::file)//if the object to remove is the file
{
std::shared_ptr<file> f=std::dynamic_pointer_cast<file>(obj);
if(!f->isReadyToRemove(targetUser.getUsername()))
throw filesystemException(filesystemException::someoneWork, UnpackFileLineFunction());
if(!f->validateAction(targetUser.getUsername(), privilege::owner))
throw filesystemException(filesystemException::notOwnDelete, UnpackFileLineFunction());
if(f->moreOwner(targetUser.getUsername()))//check if targetUser is the only user, only in this case the file can be deleted
throw filesystemException(filesystemException::notOnlyOwn, UnpackFileLineFunction());
auto it=std::find_if(contained.begin(), contained.end(),
[idRem, f](const std::shared_ptr<filesystem>& i){return i->getId()==f->getId();});
if(!f->getDoc().deleteFromDisk())
throw filesystemException(filesystemException::wrongRem, UnpackFileLineFunction());
contained.erase(it);//cancel from the container the object
return obj;
}
else if(obj->resType()==resourceType::symlink)//if the object is symlink
{
std::shared_ptr<symlink> s = std::dynamic_pointer_cast<symlink>(obj);
std::string pathFile;
std::string idFile;
tie(pathFile, idFile) = separate(s->getPath());//need to have a pointer for the file and not a symlink in order to operate on file strategy
if (s->isReadyToRemove(targetUser.getUsername()))
{
std::shared_ptr<file> f;
try
{
f = getRoot()->getFile(pathFile, idFile);
}
catch (const filesystemException& e) {
auto it = std::find_if(contained.begin(), contained.end(),
[idRem, s](const std::shared_ptr<filesystem>& i) { return i->getId() == s->getId(); });
contained.erase(it);//delete the object symlink in the right directory of the user
return obj;
}
if (!f->moreOwner(targetUser.getUsername()))
getRoot()->remove(targetUser, pathFile, idFile);
else
f->deleteFromStrategy(targetUser.getUsername());
auto it = std::find_if(contained.begin(), contained.end(),
[idRem, s](std::shared_ptr<filesystem> i) { return i->getId() == s->getId(); });
contained.erase(it);//delete the object symlink in the right directory of the user
return obj;
}
else
throw filesystemException(filesystemException::someoneWork, UnpackFileLineFunction());
}
std::shared_ptr<directory> d=std::dynamic_pointer_cast<directory>(obj);//otherwise the object is a directory
if(!d->isReadyToRemove(targetUser.getUsername()))
throw filesystemException(filesystemException::someoneWork, UnpackFileLineFunction());
while(!d->contained.empty())
{
const auto& toRemove=d->contained.back();
d->remove(targetUser, "", std::to_string(toRemove->getId()));//call remove recursively in order to remove any object in the directory
}
auto it=std::find_if(contained.begin(), contained.end(),
[idRem, d](std::shared_ptr<filesystem> i){return i->getId()==d->getId();});
contained.erase(it);//in the end remove the directory
return obj;
}
void directory::storeContent() const {
for(const auto& el:contained)
el->storeContent();
}
std::string directory::print(const std::string &targetUser, bool recursive, unsigned int indent) const {
std::string result;
if(indent==0)
{result.append(targetUser); result+=" ";}
//otherwise need to invoke recursively the method prints for all subdirectory
for(const auto & it : contained)
{
resourceType type=it->resType();
if(type==resourceType::directory)
{
std::shared_ptr<directory> dir=std::dynamic_pointer_cast<directory>(it);
std::ostringstream typeres;
typeres<<dir->resType();
std::string spaces;
if(indent>0)
spaces.insert(spaces.begin(), indent, ' ');
result=result+typeres.str()+" "+std::to_string(it->getId())+" "+spaces+dir->name+"\n";
if(recursive)
result+=dir->print(targetUser, recursive, indent+1);
}
else
result+=it->print(targetUser, recursive, indent)+"\n ";
}
return result;
}
bool directory::isReadyToRemove(const std::string &username) const {
for(const auto& element:contained)
return element->isReadyToRemove(username);
return true;
}
Symposium::symlink::symlink(const std::string &symName, const std::string &absPathWithoutId, const std::string &resId,
uint_positive_cnt::type idToAssign) : filesystem(symName, idToAssign), absPathWithoutId(absPathWithoutId), resId(resId) {
if(!pathIsValid2(absPathWithoutId))
throw filesystemException(filesystemException::pathNvalid, UnpackFileLineFunction());
strategy=std::make_unique<TrivialAccess>();
}
resourceType Symposium::symlink::resType() const {
return resourceType::symlink;
}
document& Symposium::symlink::access(const user &targetUser, privilege accessMode) {
std::shared_ptr<file> f=directory::getRoot()->getFile(absPathWithoutId, resId);
return f->access(targetUser, accessMode);
}
std::string Symposium::symlink::getPath() {
return absPathWithoutId + "/" + resId;
}
std::string Symposium::symlink::print(const std::string &targetUser, bool, unsigned int indent) const {
//std::shared_ptr<file> file=directory::getRoot()->getFile(absPathWithoutId, resId);
std::ostringstream priv;
std::ostringstream typeres;
typeres<<resType();
try {
std::shared_ptr<file> file=directory::getRoot()->getFile(absPathWithoutId, resId);
priv<<file->getUserPrivilege(targetUser);
} catch (const filesystemException&) {
priv<<privilege::none;
}
//priv<<file->getUserPrivilege(targetUser);
std::string spaces = "";
if(indent>0)
spaces.insert(spaces.begin(), indent, ' ');
return typeres.str()+" "+std::to_string(getId())+" "+spaces+name+" " + priv.str();
}
bool Symposium::symlink::isReadyToRemove(const std::string &username) const {
try
{
std::shared_ptr<file> f=directory::getRoot()->getFile(absPathWithoutId, resId);
if(f->moreOwner(username))
return true;
return f->isReadyToRemove(username);
}
catch (filesystemException& noGet)
{
return true;
}
}
const std::string &Symposium::symlink::getResId() const {
return resId;
}