Skip to content

Commit 631f464

Browse files
committed
Support for user file quota
1 parent f95148a commit 631f464

File tree

5 files changed

+47
-6
lines changed

5 files changed

+47
-6
lines changed

src/cloud/src/file_index.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,36 @@ qsizetype FileIndex::getSize() const
108108
return this->index.size();
109109
}
110110

111-
qint64 FileIndex::findStoreSize() const
111+
qint64 FileIndex::findStoreSize(const QString &user) const
112112
{
113113
qint64 totalSize = 0;
114114

115115
for (auto it = this->index.cbegin(); it != this->index.cend(); ++it)
116116
{
117-
totalSize += it.value().getSize();
117+
if (user.isEmpty() || user == it.value().getAccessRights().getOwner().getUser())
118+
{
119+
totalSize += it.value().getSize();
120+
}
118121
}
119122

120123
return totalSize;
121124
}
122125

126+
qint64 FileIndex::findStoreCount(const QString &user) const
127+
{
128+
qint64 totalCount = 0;
129+
130+
for (auto it = this->index.cbegin(); it != this->index.cend(); ++it)
131+
{
132+
if (user.isEmpty() || user == it.value().getAccessRights().getOwner().getUser())
133+
{
134+
totalCount++;
135+
}
136+
}
137+
138+
return totalCount;
139+
}
140+
123141
QJsonObject FileIndex::getStatisticsJson() const
124142
{
125143
RLogger::debug("[%s] Producting statistics\n",QString("FileIndex").toUtf8().constData());

src/cloud/src/file_index.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,11 @@ class FileIndex
7171
//! Return size of the index (number of entries).
7272
qsizetype getSize() const;
7373

74-
//! Find store size.
75-
qint64 findStoreSize() const;
74+
//! Find store size (total file size).
75+
qint64 findStoreSize(const QString &user = QString()) const;
76+
77+
//! Find store count (total file count).
78+
qint64 findStoreCount(const QString &user = QString()) const;
7679

7780
//! Get statistics output in Json form.
7881
QJsonObject getStatisticsJson() const;

src/cloud/src/file_manager.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,16 @@ RError::Type FileManager::storeFile(const RUserInfo &executor, const FileObject
387387
R_LOG_TRACE_RETURN(RError::Unauthorized);
388388
}
389389

390+
RFileQuota userStoreQuota(this->fileIndex.findStoreSize(executor.getName())+object.getContent().size(),
391+
object.getContent().size(),
392+
this->fileIndex.findStoreCount(executor.getName())+1);
393+
394+
if (executor.getFileQuota().quotaExceeded(userStoreQuota))
395+
{
396+
RLogger::error("[%s] User file quota exceeded.\n",this->settings.getName().toUtf8().constData());
397+
R_LOG_TRACE_RETURN(RError::InvalidInput);
398+
}
399+
390400
if (this->settings.getMaxFileSize() > 0)
391401
{
392402
if (object.getContent().size() > this->settings.getMaxFileSize())
@@ -523,6 +533,16 @@ RError::Type FileManager::updateFile(const RUserInfo &executor, const FileObject
523533
R_LOG_TRACE_RETURN(RError::Unauthorized);
524534
}
525535

536+
RFileQuota userStoreQuota(this->fileIndex.findStoreSize(executor.getName()) + object.getContent().size() - fileInfo.getSize(),
537+
object.getContent().size() - fileInfo.getSize(),
538+
this->fileIndex.findStoreCount(executor.getName()));
539+
540+
if (executor.getFileQuota().quotaExceeded(userStoreQuota))
541+
{
542+
RLogger::error("[%s] User file quota exceeded.\n",this->settings.getName().toUtf8().constData());
543+
R_LOG_TRACE_RETURN(RError::InvalidInput);
544+
}
545+
526546
if (!RFileInfo::isPathValid(object.getInfo().getPath()))
527547
{
528548
output = QString("Invalid path \"%1\"").arg(object.getInfo().getPath()).toUtf8();

0 commit comments

Comments
 (0)