]>
git.ipfire.org Git - people/ms/network.git/blob - src/functions/functions.editor
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2017 IPFire Network Development Team #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
20 ###############################################################################
23 # Cleanup after a file was edited
29 lock_release
"${file}.lock"
34 # Open a file with the best available editor
40 for editor
in ${EDITOR} vim vi
; do
49 ${EXIT_COMMAND_NOT_FOUND})
59 error
"Unable to find a working editor"
61 return ${EXIT_COMMAND_NOT_FOUND}
65 # This function open a file for editing and take care of all preperation and postprocessing
69 if [ ! -f ${file} ] ||
[ ! -w ${file} ]; then
70 error
"${file} is not valid file or is not writeable"
76 # check if the file is locked
77 if lock_exists
"${file}.lock"; then
78 error
"Cannot edit ${file} because it is locked"
83 if ! lock_acquire
"${file}.lock"; then
84 error
"Cannot lock file ${file}"
88 # create a temporary file
89 local temp_file
=$
(mktemp
)
91 if ! [ -f "${temp_file}" ]; then
92 error
"Cannot create temporary file"
95 # copy the content into this temporary file
96 cp -f "${file}" "${temp_file}"
99 if ! editor_find_best
"${temp_file}"; then
100 error
"Could not edit ${file}"
102 editor_cleanup
"${file}" "${temp_file}"
105 # run the check if we have one
106 if isset check_func
&& ! editor_check
"${check_func}" "${temp_file}"; then
110 # copy the changes back
111 cp -f "${temp_file}" "${file}"
114 editor_cleanup
"${file}" "${temp_file}"
119 # Execute the check function to make sure that the changes does not break anything
120 local check_func
="${1}"
123 # Execute the check function
129 ${EXIT_OK}|
${EXIT_TRUE})
130 log DEBUG
"Check succeeded"
135 ${EXIT_ERROR}|
${EXIT_FALSE})
136 log CRITICAL
"Check failed"
141 ${EXIT_COMMAND_NOT_FOUND})
142 log CRITICAL
"Check function '${check_func}' was not found"
147 log CRITICAL
"Unhandled exit code for '${check_func}': ${ret}"