# HG changeset patch # User Goffi # Date 1620221841 -7200 # Node ID 30779935c0aaaed49806ba47f3e8acd774c7962c # Parent 41a6c144dc90f9288309f80acbc074bea808a964 core (memory, sqlite): new `fileGetUsedSpace` method diff -r 41a6c144dc90 -r 30779935c0aa sat/memory/memory.py --- a/sat/memory/memory.py Wed May 05 12:58:11 2021 +0200 +++ b/sat/memory/memory.py Wed May 05 15:37:21 2021 +0200 @@ -1456,8 +1456,8 @@ client, owner: Optional[jid.JID], peer_jid: Optional[jid.JID], - file_id: Optional[str], - parent: Optional[str] + file_id: Optional[str] = None, + parent: Optional[str] = None ) -> jid.JID: """Get owner to use for a file operation @@ -1709,6 +1709,24 @@ extra=extra, ) + async def fileGetUsedSpace( + self, + client, + peer_jid: jid.JID, + owner: Optional[jid.JID] = None + ) -> int: + """Get space taken by all files owned by an entity + + @param peer_jid: entity requesting the size + @param owner: entity owning the file to check. If None, will be determined by + getFilesOwner + @return: size of total space used by files of this owner + """ + owner = self.getFilesOwner(client, owner, peer_jid) + if peer_jid.userhostJID() != owner and client.profile not in self.admins: + raise exceptions.PermissionError("You are not allowed to check this size") + return await self.storage.fileGetUsedSpace(client, owner) + def fileUpdate(self, file_id, column, update_cb): """Update a file column taking care of race condition diff -r 41a6c144dc90 -r 30779935c0aa sat/memory/sqlite.py --- a/sat/memory/sqlite.py Wed May 05 12:58:11 2021 +0200 +++ b/sat/memory/sqlite.py Wed May 05 15:37:21 2021 +0200 @@ -986,6 +986,15 @@ d.addErrback(lambda failure: log.error(_("Can't save file metadata for [{profile}]: {reason}".format(profile=client.profile, reason=failure)))) return d + async def fileGetUsedSpace(self, client, owner): + """Get space used by owner of file""" + query = "SELECT SUM(size) FROM files WHERE owner=? AND type='file' AND profile_id=?" + ret = await self.dbpool.runQuery( + query, + (owner.userhost(), self.profiles[client.profile]) + ) + return ret[0][0] + def _fileUpdate(self, cursor, file_id, column, update_cb): query = 'SELECT {column} FROM files where id=?'.format(column=column) for i in range(5):