]>
Commit | Line | Data |
---|---|---|
e524986c JS |
1 | #!/bin/bash |
2 | ############################################################################### | |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
5 | # Copyright (C) 2017 IPFire Network Development Team # | |
6 | # # | |
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. # | |
11 | # # | |
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. # | |
16 | # # | |
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/>. # | |
19 | # # | |
20 | ############################################################################### | |
21 | ||
22 | editor_cleanup() { | |
23 | # Cleanup after a file was edited | |
24 | assert [ $# -eq 2 ] | |
25 | ||
26 | local file=${1} | |
27 | local temp_file=${2} | |
28 | ||
29 | lock_release "${file}.lock" | |
30 | rm -f ${temp_file} | |
31 | } | |
32 | ||
33 | editor_find_best() { | |
34 | # Open a file with the best available editor | |
35 | assert [ $# -eq 1 ] | |
36 | ||
37 | local file=${1} | |
38 | local ret | |
39 | ||
40 | for editor in ${EDITOR} vim vi; do | |
41 | ${editor} "${file}" | |
42 | ret=${?} | |
43 | ||
44 | case "${ret}" in | |
45 | ${EXIT_OK}) | |
46 | return ${EXIT_OK} | |
47 | ;; | |
48 | ||
49 | ${EXIT_COMMAND_NOT_FOUND}) | |
50 | continue | |
51 | ;; | |
52 | ||
53 | *) | |
54 | return ${ret} | |
55 | ;; | |
56 | esac | |
57 | done | |
58 | ||
59 | error "Unable to find a working editor" | |
60 | ||
61 | return ${EXIT_COMMAND_NOT_FOUND} | |
62 | } | |
63 | ||
64 | editor() { | |
65 | # This function open a file for editing and take care of all preperation and postprocessing | |
66 | assert [ $# -ge 1 ] | |
67 | ||
68 | local file=${1} | |
69 | if [ ! -f ${file} ] || [ ! -w ${file} ]; then | |
70 | error "${file} is not valid file or is not writeable" | |
71 | return ${EXIT_ERROR} | |
72 | fi | |
73 | ||
74 | local check_func=${2} | |
75 | ||
76 | # check if the file is locked | |
77 | if lock_exists "${file}.lock"; then | |
78 | error "Cannot edit ${file} because it is locked" | |
79 | return ${EXIT_ERROR} | |
80 | fi | |
81 | ||
82 | # lock the file | |
83 | if ! lock_acquire "${file}.lock"; then | |
84 | error "Cannot lock file ${file}" | |
85 | return ${EXIT_ERROR} | |
86 | fi | |
87 | ||
88 | # create a temporary file | |
89 | local temp_file=$(mktemp) | |
90 | ||
91 | if ! [ -f "${temp_file}" ]; then | |
92 | error "Cannot create temporary file" | |
93 | fi | |
94 | ||
95 | # copy the content into this temporary file | |
96 | cp -f "${file}" "${temp_file}" | |
97 | ||
98 | # edit the file | |
99 | if ! editor_find_best "${temp_file}"; then | |
100 | error "Could not edit ${file}" | |
101 | # cleanup | |
102 | editor_cleanup "${file}" "${temp_file}" | |
103 | fi | |
104 | ||
105 | # run the check if we have one | |
106 | if isset check_func && ! editor_check "${check_func}" "${temp_file}"; then | |
107 | return ${EXIT_ERROR} | |
108 | fi | |
109 | ||
110 | # copy the changes back | |
111 | cp -f "${temp_file}" "${file}" | |
112 | ||
113 | # cleanup | |
114 | editor_cleanup "${file}" "${temp_file}" | |
115 | ||
116 | } | |
117 | ||
118 | editor_check() { | |
119 | # Execute the check function to make sure that the changes does not break anything | |
120 | local check_func="${1}" | |
121 | shift | |
122 | ||
123 | # Execute the check function | |
2212045f | 124 | "${check_func}" "$@" |
e524986c JS |
125 | local ret="${?}" |
126 | ||
127 | case "${ret}" in | |
128 | # OK | |
129 | ${EXIT_OK}|${EXIT_TRUE}) | |
130 | log DEBUG "Check succeeded" | |
131 | return ${EXIT_TRUE} | |
132 | ;; | |
133 | ||
134 | # Error | |
135 | ${EXIT_ERROR}|${EXIT_FALSE}) | |
136 | log CRITICAL "Check failed" | |
137 | return ${EXIT_FALSE} | |
138 | ;; | |
139 | ||
140 | # Command not found | |
141 | ${EXIT_COMMAND_NOT_FOUND}) | |
142 | log CRITICAL "Check function '${check_func}' was not found" | |
143 | return ${EXIT_FALSE} | |
144 | ;; | |
145 | esac | |
146 | ||
147 | log CRITICAL "Unhandled exit code for '${check_func}': ${ret}" | |
148 | return ${EXIT_ERROR} | |
149 | } |