fix folder check

This commit is contained in:
iridiumR 2023-08-07 21:26:39 +08:00
parent b310d1391f
commit 0d939165de
No known key found for this signature in database
GPG key ID: 49735733EB1A32C8
2 changed files with 38 additions and 23 deletions

View file

@ -1,6 +1,6 @@
# Maintainer: 1ridic <i@8f.al>
pkgname=uptimes
pkgver=0.1.0
pkgver=0.1.1
pkgrel=1
pkgdesc="status of total uptime"
arch=('any')
@ -11,7 +11,7 @@ source=(
local://uptimes
local://uptimesd.service
)
md5sums=('b2bac26fc929e533923497311f4de415'
md5sums=('749fafe23dbedae4d9375b5ef4c09d64'
'f598fdb7ab331933d3495c0bb53358c2')
package() {

51
uptimes
View file

@ -55,6 +55,36 @@ def readUpTime():
uptime = float(uptime)
return uptime
def pathExists():
# if /etc/uptimes/uptimes.db exist
if os.path.exists(uptimes_db):
# check permissions
if os.stat(uptimes_db).st_mode != 0o600:
# change permissions
os.chmod(uptimes_db, 0o600)
# check the content
with open(uptimes_db, 'r') as f:
content = f.readline()
if content == '':
# write 0
with open(uptimes_db, 'w') as f:
f.write('0')
os.chmod(uptimes_db, 0o600)
else:
folder = uptimes_db.split('/')
folder.pop()
folder = '/'.join(folder)
if not os.path.exists(folder):
# create folder with 600
os.mkdir(folder, 0o600)
# create uptimes.db with 600
with open(uptimes_db, 'w') as f:
f.write('0')
os.chmod(uptimes_db, 0o600)
def daemon():
global uptime, now, s
# fork
@ -104,27 +134,11 @@ def daemon():
signal.signal(signal.SIGQUIT, sigterm_handler)
signal.signal(signal.SIGPIPE, sigterm_handler)
# if /etc/uptimes/uptimes.db exist
if os.path.exists(uptimes_db):
pathExists()
# read uptime
with open(uptimes_db, 'r') as f:
uptime = f.readline()
if uptime == '':
uptime = 0
else:
uptime = float(uptime)
else:
folder = uptimes_db.split('/')
folder.pop()
folder = '/'.join(folder)
if not os.path.exists(folder):
# create folder with 600
os.mkdir(folder, 0o600)
# create db with 600
with open(uptimes_db, 'w') as f:
f.write(str(uptime))
startTime = uptime
while True:
conn, addr = s.accept()
@ -159,6 +173,7 @@ def updateTime():
global uptime, startTime
now = readUpTime()
uptime += now - startTime
pathExists()
with open(uptimes_db, 'w') as f:
f.write(str(uptime))