From 1bf6e76a2d62e1f2746a6a4187dbd0d7a82e79f8 Mon Sep 17 00:00:00 2001 From: Mika Havela Date: Fri, 25 Jan 2008 12:44:54 +0000 Subject: Fixed bug when config file is missing or empty git-svn-id: svn://svn.alpinelinux.org/acf/alpine-conf/trunk@649 ab2d0c66-481e-0410-8bed-d214d4d58bed --- lbu-model.lua | 101 ++++++++++++++++++++++++++++------------------------------ 1 file changed, 48 insertions(+), 53 deletions(-) (limited to 'lbu-model.lua') diff --git a/lbu-model.lua b/lbu-model.lua index 14fb9d3..9ff31a4 100644 --- a/lbu-model.lua +++ b/lbu-model.lua @@ -77,43 +77,24 @@ local function getincexl(state) return incexl end -local function checkexistens(file,variable) - local filecontent = fs.read_file_as_array(file) - for k,v in ipairs(filecontent) do - return v - end - return nil -end - -- ################################################################################ -- PUBLIC FUNCTIONS function getstatus () local path = configfile local status = {} - local errors = {} local statustxt = nil - local lbustatus = list() - if (#lbustatus == 0) then + if (#list() == 0) then statustxt = "OK! (There is no uncommited files)" else statustxt = "WARNING! (Until you commit, you will lose your changes at next reboot/shutdown!)" end - local config = getconfig() + local config, errors = getconfig() status["LBU_MEDIA"] = config["LBU_MEDIA"] status["ENCRYPTION"] = config["ENCRYPTION"] status["DEFAULT_CIPHER"] = config["DEFAULT_CIPHER"] status["version"] = get_version() status["status"] = statustxt - if (status["LBU_MEDIA"] == "") or (status["LBU_MEDIA"] == nil) then - errors["LBU_MEDIA"] = "'Media' needs to be configured!" - end - if (config["PASSWORD"] == nil) and (config["ENCRYPTION"] ~= nil) then - errors["PASSWORD"] = "Encryption without password is not allowed!
Deactivate 'Password protection' or configure a password!" - end - for k,v in pairs(errors) do - errors["last"] = v - end return status, errors end @@ -141,23 +122,32 @@ end function getconfig () local path = configfile local config = {} + local errors = {} local lbumedias = {} table.insert(lbumedias, {name="floppy", value="floppy"}) table.insert(lbumedias, {name="usb", value="usb"}) if (fs.is_file(path)) then - config = getopts.getoptsfromfile(path) - config["lbu_included"]= getincexl("include") - config["lbu_excluded"]= getincexl("exclude") - config["LBU_MEDIA_LIST"]= lbumedias - else - --FIXME: What if the configfile doesn't exist (below is a beginning of fixing it... please continue) - config["lbu_included"]= "" - config["lbu_excluded"]= "" - config["LBU_MEDIA_LIST"]= "" - end - config["DEFAULT_CIPHER_LIST"]= getciphers() --- config["debug"] = getciphers() - return config + config = getopts.getoptsfromfile(path) or {} + end + config["lbu_included"]= getincexl("include") or {} + config["lbu_excluded"]= getincexl("exclude") or {} + config["LBU_MEDIA_LIST"]= lbumedias or {} + config["DEFAULT_CIPHER_LIST"]= getciphers() or {} + -- Next section is to set/show default values when a option is not configured in the configfile + config["LBU_MEDIA"] = config["LBU_MEDIA"] or "" + config["DEFAULT_CIPHER"] = config["DEFAULT_CIPHER"] or "" + -- Next section is to print errormessages when configs are wrong + if (config["LBU_MEDIA"] == "") or (config["LBU_MEDIA"] == nil) then + errors["LBU_MEDIA"] = "'Media' needs to be configured!" + end + if (config["PASSWORD"] == nil) and (config["ENCRYPTION"] ~= nil) then + errors["PASSWORD"] = "Encryption without password is not allowed!
Deactivate 'Password protection' or configure a password!" + end + for k,v in pairs(errors) do + errors["last"] = v + end + + return config, errors end function lbuincexcl(self,state,value,addremove) @@ -180,29 +170,34 @@ function lbuincexcl(self,state,value,addremove) return incexl end -function editconfig (self,variable,value,state) +function editconfig (self,variable,value) local configfilecontent = nil local path = configfile - local cmdoutput = {} - if (state == "change_value" ) then - configfilecontent = fs.read_file_as_array(configfile) - if not (value) or (value == "") then - cmdoutput = format.search_replace(configfilecontent,"^%s*%#*%s*" .. variable, "#" .. variable) - else - cmdoutput = format.search_replace(configfilecontent,"^%s*%#*%s*" .. variable .. ".*$", variable .. "=" .. value) - end - fs.write_file(configfile,table.concat(cmdoutput,"\n")) - elseif (state == "change_state" ) then - configfilecontent = fs.read_file_as_array(configfile) - if not (value) or (value == "") then - cmdoutput = format.search_replace(configfilecontent,"^%s*%#*%s*" .. variable, "#" .. variable) - else - cmdoutput = format.search_replace(configfilecontent,"^%s*%#*%s*" .. variable, variable) + local cmdoutput = nil + if not (fs.is_file(path)) then + fs.write_file(path,"") + end + local deactivate = "" + if not (value) or (value == "") then + deactivate = "#" + end + -- Hardcode some parameters (e.g for some checkboxes) + if (variable == "ENCRYPTION") then value = "$DEFAULT_CIPHER" end + configfilecontent = fs.read_file_as_array(path) or {} + -- Search if the variable is defined in the file + for k,v in pairs(configfilecontent) do + if (string.match(v,"^%s*%#*%s*" .. variable)) then + cmdoutput = format.search_replace(configfilecontent,"^%s*%#*%s*" .. variable .. ".*$", deactivate .. variable .. "=" .. value) + break end - fs.write_file(configfile,table.concat(cmdoutput,"\n")) - else - return "Function ediconfig() - Wrong usage of this function! usage editconfig(variable,value,state)\nvariable=Name of the variable\nvalue=The new value (when adding)\nstate=change_value|change_state depending on if you want to add some value or remove some variable." end + --If variable is not changed (didn't exist) then create a new row with this variable + if not (cmdoutput) then + cmdoutput = configfilecontent + table.insert(cmdoutput,deactivate .. variable .. "=" .. (value or "")) + end + -- Write changes to file + fs.write_file(path,table.concat(cmdoutput,"\n")) return cmdoutput end -- cgit v1.2.3