From 7a2805a412eb381ddc31889b22caebb347b160eb Mon Sep 17 00:00:00 2001 From: Jiaqiang Xu Date: Fri, 26 Oct 2012 14:31:23 +0800 Subject: [PATCH] Complete quota management RPC. --- common/rpc-service.c | 19 +++++++++++++++++++ include/seafile-rpc.h | 3 +++ python/seafile/rpcclient.py | 10 ++++++++++ server/quota-mgr.c | 26 +++++++++++++++++++------- server/seaf-server.c | 4 ++++ server/seafile-session.c | 19 +++++++++++++++++++ server/seafile-session.h | 5 +++++ 7 files changed, 79 insertions(+), 7 deletions(-) diff --git a/common/rpc-service.c b/common/rpc-service.c index 8bf2998b..5821e02c 100644 --- a/common/rpc-service.c +++ b/common/rpc-service.c @@ -1721,6 +1721,25 @@ seafile_get_org_quota_usage (int org_id, GError **error) return ret; } +gint64 +seafile_get_org_user_quota_usage (int org_id, const char *user, GError **error) +{ + gint64 ret; + + if (!user) { + g_set_error (error, SEAFILE_DOMAIN, SEAF_ERR_BAD_ARGS, "Bad user id"); + return -1; + } + + ret = get_org_user_quota_usage (seaf, org_id, user); + if (ret < 0) { + g_set_error (error, SEAFILE_DOMAIN, SEAF_ERR_GENERAL, "Internal server error"); + return -1; + } + + return ret; +} + gint64 seafile_server_repo_size(const char *repo_id, GError **error) { diff --git a/include/seafile-rpc.h b/include/seafile-rpc.h index 92d4b3b5..e7e54846 100644 --- a/include/seafile-rpc.h +++ b/include/seafile-rpc.h @@ -483,6 +483,9 @@ gint64 seafile_get_user_quota_usage (const char *email, GError **error); gint64 seafile_get_org_quota_usage (int org_id, GError **error); +gint64 +seafile_get_org_user_quota_usage (int org_id, const char *user, GError **error); + gint64 seafile_server_repo_size(const char *repo_id, GError **error); diff --git a/python/seafile/rpcclient.py b/python/seafile/rpcclient.py index 344bc9ed..c4520a4f 100644 --- a/python/seafile/rpcclient.py +++ b/python/seafile/rpcclient.py @@ -527,6 +527,16 @@ class SeafServerThreadedRpcClient(ccnet.RpcClientBase): pass get_user_quota_usage = seafile_get_user_quota_usage + @searpc_func("int64", ["int"]) + def seafile_get_org_quota_usage(org_id): + pass + get_org_quota_usage = seafile_get_org_quota_usage + + @searpc_func("int64", ["int", "string"]) + def seafile_get_org_user_quota_usage(org_id, user): + pass + get_org_user_quota_usage = seafile_get_org_user_quota_usage + @searpc_func("int", ["string", "int64"]) def set_user_quota(user, quota): pass diff --git a/server/quota-mgr.c b/server/quota-mgr.c index a613a1b1..986ae848 100644 --- a/server/quota-mgr.c +++ b/server/quota-mgr.c @@ -75,11 +75,16 @@ seaf_quota_manager_get_user_quota (SeafQuotaManager *mgr, const char *user) { char sql[512]; + gint64 quota; snprintf (sql, sizeof(sql), "SELECT quota FROM UserQuota WHERE user='%s'", user); - return seaf_db_get_int64 (mgr->session->db, sql); + quota = seaf_db_get_int64 (mgr->session->db, sql); + if (quota <= 0) + quota = mgr->default_quota; + + return quota; } int @@ -100,11 +105,16 @@ seaf_quota_manager_get_org_quota (SeafQuotaManager *mgr, int org_id) { char sql[512]; + gint64 quota; snprintf (sql, sizeof(sql), "SELECT quota FROM OrgQuota WHERE org_id='%d'", org_id); - return seaf_db_get_int64 (mgr->session->db, sql); + quota = seaf_db_get_int64 (mgr->session->db, sql); + if (quota <= 0) + quota = mgr->default_quota; + + return quota; } int @@ -127,11 +137,17 @@ seaf_quota_manager_get_org_user_quota (SeafQuotaManager *mgr, const char *user) { char sql[512]; + gint64 quota; snprintf (sql, sizeof(sql), "SELECT quota FROM OrgUserQuota WHERE org_id='%d' AND user='%s'", org_id, user); - return seaf_db_get_int64 (mgr->session->db, sql); + quota = seaf_db_get_int64 (mgr->session->db, sql); + /* return org quota if per user quota is not set. */ + if (quota <= 0) + quota = seaf_quota_manager_get_org_quota (mgr, org_id); + + return quota; } int @@ -145,8 +161,6 @@ seaf_quota_manager_check_quota (SeafQuotaManager *mgr, user = seaf_repo_manager_get_repo_owner (seaf->repo_mgr, repo_id); if (user != NULL) { quota = seaf_quota_manager_get_user_quota (mgr, user); - if (quota <= 0) - quota = mgr->default_quota; } else if (seaf->cloud_mode) { org_id = seaf_repo_manager_get_repo_org (seaf->repo_mgr, repo_id); if (org_id < 0) { @@ -155,8 +169,6 @@ seaf_quota_manager_check_quota (SeafQuotaManager *mgr, } quota = seaf_quota_manager_get_org_quota (mgr, org_id); - if (quota <= 0) - quota = mgr->default_quota; } else { seaf_warning ("Repo %s has no owner.\n", repo_id); return -1; diff --git a/server/seaf-server.c b/server/seaf-server.c index e82c4d48..ae21d813 100644 --- a/server/seaf-server.c +++ b/server/seaf-server.c @@ -363,6 +363,10 @@ static void start_rpc_service (CcnetClient *client, int cloud_mode) seafile_get_org_quota_usage, "seafile_get_org_quota_usage", searpc_signature_int64__int()); + searpc_server_register_function ("seafserv-threaded-rpcserver", + seafile_get_org_user_quota_usage, + "seafile_get_org_user_quota_usage", + searpc_signature_int64__int_string()); /* -------- rpc services -------- */ diff --git a/server/seafile-session.c b/server/seafile-session.c index 7aa7d554..db6d686d 100644 --- a/server/seafile-session.c +++ b/server/seafile-session.c @@ -283,3 +283,22 @@ get_org_quota_usage (SeafileSession *seaf, int org_id) return ret; } + +gint64 +get_org_user_quota_usage (SeafileSession *seaf, + int org_id, + const char *user) +{ + char sql[256]; + gint64 ret = 0; + + snprintf (sql, sizeof(sql), + "SELECT size FROM OrgRepo, RepoSize WHERE " + "org_id=%d AND user = '%s' AND OrgRepo.repo_id=RepoSize.repo_id", + org_id, user); + if (seaf_db_foreach_selected_row (seaf->db, sql, + get_total_size, &ret) < 0) + return -1; + + return ret; +} diff --git a/server/seafile-session.h b/server/seafile-session.h index f0fe4c54..10e331f2 100644 --- a/server/seafile-session.h +++ b/server/seafile-session.h @@ -102,4 +102,9 @@ get_user_quota_usage (SeafileSession *seaf, const char *user_id); gint64 get_org_quota_usage (SeafileSession *seaf, int org_id); +gint64 +get_org_user_quota_usage (SeafileSession *seaf, + int org_id, + const char *user); + #endif /* SEAFILE_H */