fix folder check
This commit is contained in:
parent
b310d1391f
commit
0d939165de
2 changed files with 38 additions and 23 deletions
4
PKGBUILD
4
PKGBUILD
|
@ -1,6 +1,6 @@
|
||||||
# Maintainer: 1ridic <i@8f.al>
|
# Maintainer: 1ridic <i@8f.al>
|
||||||
pkgname=uptimes
|
pkgname=uptimes
|
||||||
pkgver=0.1.0
|
pkgver=0.1.1
|
||||||
pkgrel=1
|
pkgrel=1
|
||||||
pkgdesc="status of total uptime"
|
pkgdesc="status of total uptime"
|
||||||
arch=('any')
|
arch=('any')
|
||||||
|
@ -11,7 +11,7 @@ source=(
|
||||||
local://uptimes
|
local://uptimes
|
||||||
local://uptimesd.service
|
local://uptimesd.service
|
||||||
)
|
)
|
||||||
md5sums=('b2bac26fc929e533923497311f4de415'
|
md5sums=('749fafe23dbedae4d9375b5ef4c09d64'
|
||||||
'f598fdb7ab331933d3495c0bb53358c2')
|
'f598fdb7ab331933d3495c0bb53358c2')
|
||||||
|
|
||||||
package() {
|
package() {
|
||||||
|
|
51
uptimes
51
uptimes
|
@ -55,6 +55,36 @@ def readUpTime():
|
||||||
uptime = float(uptime)
|
uptime = float(uptime)
|
||||||
return 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():
|
def daemon():
|
||||||
global uptime, now, s
|
global uptime, now, s
|
||||||
# fork
|
# fork
|
||||||
|
@ -104,27 +134,11 @@ def daemon():
|
||||||
signal.signal(signal.SIGQUIT, sigterm_handler)
|
signal.signal(signal.SIGQUIT, sigterm_handler)
|
||||||
signal.signal(signal.SIGPIPE, sigterm_handler)
|
signal.signal(signal.SIGPIPE, sigterm_handler)
|
||||||
|
|
||||||
# if /etc/uptimes/uptimes.db exist
|
pathExists()
|
||||||
if os.path.exists(uptimes_db):
|
# read uptime
|
||||||
with open(uptimes_db, 'r') as f:
|
with open(uptimes_db, 'r') as f:
|
||||||
uptime = f.readline()
|
uptime = f.readline()
|
||||||
if uptime == '':
|
|
||||||
uptime = 0
|
|
||||||
else:
|
|
||||||
uptime = float(uptime)
|
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:
|
while True:
|
||||||
conn, addr = s.accept()
|
conn, addr = s.accept()
|
||||||
|
@ -159,6 +173,7 @@ def updateTime():
|
||||||
global uptime, startTime
|
global uptime, startTime
|
||||||
now = readUpTime()
|
now = readUpTime()
|
||||||
uptime += now - startTime
|
uptime += now - startTime
|
||||||
|
pathExists()
|
||||||
with open(uptimes_db, 'w') as f:
|
with open(uptimes_db, 'w') as f:
|
||||||
f.write(str(uptime))
|
f.write(str(uptime))
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue