From e2a4c13a607dea371afe0009d349409f429facee Mon Sep 17 00:00:00 2001 From: arzumify Date: Mon, 4 Nov 2024 17:17:49 +0000 Subject: [PATCH] Made addReserved check for space Signed-off-by: arzumify --- services-src/storage/main.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/services-src/storage/main.go b/services-src/storage/main.go index 9f7284e..fda604e 100644 --- a/services-src/storage/main.go +++ b/services-src/storage/main.go @@ -90,11 +90,29 @@ func addQuota(information library.ServiceInitializationInformation, message libr func addReserved(information library.ServiceInitializationInformation, message library.InterServiceMessage) { // Add more reserved space to a user if checkUserExists(message.Message.(nucleusLibrary.Quota).User) { - _, err := conn.DB.Exec("UPDATE users SET reserved = reserved + $1 WHERE id = $2", message.Message.(nucleusLibrary.Quota).Bytes, message.Message.(nucleusLibrary.Quota).User) + // Check if the user has enough space + quota, err := getQuota(message.Message.(nucleusLibrary.Quota).User) + if err != nil { + respondError(err.Error(), information, true, message.ServiceID) + } + used, err := getUsed(message.Message.(nucleusLibrary.Quota).User, information) + if err != nil { + respondError(err.Error(), information, true, message.ServiceID) + } + if used+message.Message.(nucleusLibrary.Quota).Bytes > quota { + respondError("insufficient storage", information, false, message.ServiceID) + return + } + _, err = conn.DB.Exec("UPDATE users SET reserved = reserved + $1 WHERE id = $2", message.Message.(nucleusLibrary.Quota).Bytes, message.Message.(nucleusLibrary.Quota).User) if err != nil { respondError(err.Error(), information, true, message.ServiceID) } } else { + // Check if the user has enough space + if int64(information.Configuration["defaultQuota"].(float64)) < message.Message.(nucleusLibrary.Quota).Bytes { + respondError("insufficient storage", information, false, message.ServiceID) + return + } _, err := conn.DB.Exec("INSERT INTO users (id, quota, reserved) VALUES ($1, $2, $3)", message.Message.(nucleusLibrary.Quota).User, int64(information.Configuration["defaultQuota"].(float64)), message.Message.(nucleusLibrary.Quota).Bytes) if err != nil { respondError(err.Error(), information, true, message.ServiceID)