summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMika Havela <mika.havela@gmail.com>2008-01-25 12:44:54 +0000
committerMika Havela <mika.havela@gmail.com>2008-01-25 12:44:54 +0000
commit1bf6e76a2d62e1f2746a6a4187dbd0d7a82e79f8 (patch)
treeb56be3fe9a72c604ff681fa4367e4ed2fc51b97e
parent9cd4c138af69421eab0899df495a834d7a6816fb (diff)
downloadacf-alpine-conf-1bf6e76a2d62e1f2746a6a4187dbd0d7a82e79f8.tar.bz2
acf-alpine-conf-1bf6e76a2d62e1f2746a6a4187dbd0d7a82e79f8.tar.xz
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
-rw-r--r--lbu-config-html.lsp5
-rw-r--r--lbu-controller.lua6
-rw-r--r--lbu-model.lua101
3 files changed, 51 insertions, 61 deletions
diff --git a/lbu-config-html.lsp b/lbu-config-html.lsp
index 0f2abe2..13e94c3 100644
--- a/lbu-config-html.lsp
+++ b/lbu-config-html.lsp
@@ -16,7 +16,7 @@
<dl>
<dt <? if (view.errors) and (view.errors.LBU_MEDIA) then io.write("class='error'") end ?>>Default media for commit</dt>
<dd><select name="LBU_MEDIA" size="1">
- <? if (view.config.LBU_MEDIA == nil) then io.write("<option value='' selected='selected'>No default</option>") end ?>
+ <? if (view.config.LBU_MEDIA == "") then io.write("<option value='' selected='selected'>No default</option>") end ?>
<? for i=1, table.maxn(view.config.LBU_MEDIA_LIST) do ?>
<option value="<?= view.config.LBU_MEDIA_LIST[i]["value"] ?>"
<? if (string.lower(view.config.LBU_MEDIA_LIST[i]["value"]) == string.lower(view.config.LBU_MEDIA or "")) then io.write("selected='selected'") end?>>
@@ -58,6 +58,7 @@
<dd><input type="checkbox" name="ENCRYPTION" <? if (view.config.ENCRYPTION) then io.write("checked=yes") end ?>></dd>
<dt>Cipher to use at encryption</dt>
<dd><select name="DEFAULT_CIPHER" size="1">
+ <? if (view.config.DEFAULT_CIPHER == "") then io.write("<option value='' selected='selected'>No default</option>") end ?>
<? for i=1, table.maxn(view.config.DEFAULT_CIPHER_LIST) do ?>
<option value="<?= view.config.DEFAULT_CIPHER_LIST[i] ?>" <? if (string.lower(view.config.DEFAULT_CIPHER_LIST[i]) == string.lower(view.config.DEFAULT_CIPHER)) then io.write("selected='selected'") end?>><?= view.config.DEFAULT_CIPHER_LIST[i] ?></option>
<? end ?>
@@ -81,6 +82,4 @@ io.write(debugs.variables(view))
--]]
?>
-
-
<? -- vim: set filetype=lua : ?>
diff --git a/lbu-controller.lua b/lbu-controller.lua
index d8703e9..dbe910f 100644
--- a/lbu-controller.lua
+++ b/lbu-controller.lua
@@ -36,15 +36,11 @@ config = function (self)
cmdresult = self.model:lbuincexcl("include", self.clientdata.lbu_included, "remove")
end
if (self.clientdata.config_submit) then
- local variables="LBU_MEDIA DEFAULT_CIPHER PASSWORD"
+ local variables="ENCRYPTION LBU_MEDIA DEFAULT_CIPHER PASSWORD"
cmdresult = {}
for var in string.gmatch(variables, "%S+") do
cmdresult[var] = self.model:editconfig(var, self.clientdata[var], "change_value")
end
- local variables="ENCRYPTION"
- for var in string.gmatch(variables, "%S+") do
- cmdresult[var] = self.model:editconfig(var, self.clientdata[var], "change_state")
- end
end
if (self.clientdata.cmd_add_include) and (self.clientdata.item_add_include) then
cmdresult = self.model:lbuincexcl("include", self.clientdata.item_add_include, "add")
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!<BR> 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!<BR> 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