Onionr/onionr/storagecounter.py

68 lines
2.4 KiB
Python
Raw Permalink Normal View History

2018-08-23 14:01:17 +00:00
'''
2019-06-16 06:06:32 +00:00
Onionr - Private P2P Communication
2018-08-23 14:01:17 +00:00
Keeps track of how much disk space we're using
'''
'''
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
'''
import config
2019-02-03 18:19:50 +00:00
config.reload()
2018-08-23 14:01:17 +00:00
class StorageCounter:
def __init__(self, coreInst):
self._core = coreInst
self.dataFile = self._core.usageFile
return
def isFull(self):
retData = False
2019-02-03 18:19:50 +00:00
if self._core.config.get('allocations.disk', 2000000000) <= (self.getAmount() + 1000):
2018-08-23 14:01:17 +00:00
retData = True
return retData
def _update(self, data):
with open(self.dataFile, 'w') as dataFile:
dataFile.write(str(data))
2018-08-23 14:45:51 +00:00
def getAmount(self):
2018-08-23 14:01:17 +00:00
'''Return how much disk space we're using (according to record)'''
retData = 0
try:
2018-08-23 14:51:53 +00:00
with open(self.dataFile, 'r') as dataFile:
2018-08-23 14:01:17 +00:00
retData = int(dataFile.read())
except FileNotFoundError:
pass
except ValueError:
pass # Possibly happens when the file is empty
2018-08-23 14:01:17 +00:00
return retData
2018-12-24 06:12:46 +00:00
def getPercent(self):
'''Return percent (decimal/float) of disk space we're using'''
amount = self.getAmount()
return round(amount / self._core.config.get('allocations.disk', 2000000000), 2)
2018-08-23 14:01:17 +00:00
def addBytes(self, amount):
'''Record that we are now using more disk space, unless doing so would exceed configured max'''
newAmount = amount + self.getAmount()
retData = newAmount
if newAmount > self._core.config.get('allocations.disk', 2000000000):
2018-08-23 14:01:17 +00:00
retData = False
else:
self._update(newAmount)
return retData
def removeBytes(self, amount):
'''Record that we are now using less disk space'''
newAmount = self.getAmount() - amount
self._update(newAmount)
return newAmount