static gint64 repo_share_usage (const char *user, const char *repo_id) { gint n_shared_to = get_num_shared_to (user, repo_id); if (n_shared_to < 0) { return -1; } else if (n_shared_to == 0) { return 0; } gint64 size = seaf_repo_manager_get_repo_size (seaf->repo_mgr, repo_id); if (size < 0) { seaf_warning ("Cannot get size of repo %s.\n", repo_id); return -1; } /* share_usage = repo_size * n_shared_to */ gint64 usage = size * n_shared_to; return usage; }
static gint64 repo_share_usage (const char *user, const char *repo_id) { GHashTable *user_hash; int dummy; GList *personal = NULL, *groups = NULL, *members = NULL, *p; SearpcClient *client = NULL; gint64 usage = -1; /* seaf_debug ("Computing share usage for repo %s.\n", repo_id); */ /* If a repo is shared to both a user and a group, and that user is also * a member of the group, we don't want to count that user twice. * This also applies to two groups with overlapped members. * So we have to use a hash table to filter out duplicated users. */ user_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL); /* First count personal share */ personal = seaf_share_manager_list_shared_to (seaf->share_mgr, user, repo_id); for (p = personal; p; p = p->next) { char *email = p->data; g_hash_table_insert (user_hash, g_strdup(email), &dummy); /* seaf_debug ("Shared to %s.\n", email); */ } /* Then groups... */ client = ccnet_create_pooled_rpc_client (seaf->client_pool, NULL, "ccnet-threaded-rpcserver"); if (!client) { seaf_warning ("Failed to alloc rpc client.\n"); goto out; } groups = seaf_repo_manager_get_groups_by_repo (seaf->repo_mgr, repo_id, NULL); for (p = groups; p; p = p->next) { members = ccnet_get_group_members (client, (int)p->data); if (!members) { seaf_warning ("Cannot get member list for groupd %d.\n", (int)p->data); goto out; } count_group_members (user_hash, members); } /* Remove myself if i'm in a group. */ g_hash_table_remove (user_hash, user); guint n_shared_to = g_hash_table_size(user_hash); /* seaf_debug ("n_shared_to = %u.\n", n_shared_to); */ if (n_shared_to == 0) { usage = 0; goto out; } gint64 size = seaf_repo_manager_get_repo_size (seaf->repo_mgr, repo_id); if (size < 0) { seaf_warning ("Cannot get size of repo %s.\n", repo_id); goto out; } /* share_usage = repo_size * n_shared_to / 2 */ usage = size * n_shared_to / 2; out: g_hash_table_destroy (user_hash); string_list_free (personal); g_list_free (groups); ccnet_rpc_client_free (client); return usage; }