From b268b55198eaab64455aa908fa09baa329d50603 Mon Sep 17 00:00:00 2001 From: liuh-80 Date: Mon, 27 Mar 2023 03:10:17 +0000 Subject: [PATCH] Check config file not empty after modify it in hostcfgd. --- files/image_config/hostcfgd/hostcfgd | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/files/image_config/hostcfgd/hostcfgd b/files/image_config/hostcfgd/hostcfgd index a7a817b9f7..368a589796 100755 --- a/files/image_config/hostcfgd/hostcfgd +++ b/files/image_config/hostcfgd/hostcfgd @@ -168,11 +168,26 @@ class AaaCfg(object): def tacacs_server_update(self, key): self.modify_conf_file() + def check_file_not_empty(self, filename): + exists = os.path.exists(filename) + if not exists: + syslog.syslog(syslog.LOG_ERR, "file size check failed: {} is missing".format(filename)) + return + + size = os.path.getsize(filename) + if size == 0: + syslog.syslog(syslog.LOG_ERR, "file size check failed: {} is empty, file corrupted".format(filename)) + return + + syslog.syslog(syslog.LOG_INFO, "file size check pass: {} size is ({}) bytes".format(filename, size)) + def modify_single_file(self, filename, operations=None): if operations: cmd = "sed -e {0} {1} > {1}.new; mv -f {1} {1}.old; mv -f {1}.new {1}".format(' -e '.join(operations), filename) os.system(cmd) + self.check_file_not_empty(filename) + def modify_conf_file(self): with lock_mgr(): self.auth = self.config_db.get_table('AAA').get("authentication", {})