From 0d939165deaca3a8a039af736f2b9ce85f0f7059 Mon Sep 17 00:00:00 2001 From: iridiumR Date: Mon, 7 Aug 2023 21:26:39 +0800 Subject: [PATCH] fix folder check --- PKGBUILD | 4 ++-- uptimes | 57 +++++++++++++++++++++++++++++++++++--------------------- 2 files changed, 38 insertions(+), 23 deletions(-) diff --git a/PKGBUILD b/PKGBUILD index 12e5034..9f3ed84 100644 --- a/PKGBUILD +++ b/PKGBUILD @@ -1,6 +1,6 @@ # Maintainer: 1ridic 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() { diff --git a/uptimes b/uptimes index a23cc02..6a644c5 100755 --- a/uptimes +++ b/uptimes @@ -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): - 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 + pathExists() + # read uptime + with open(uptimes_db, 'r') as f: + uptime = f.readline() + uptime = float(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))