]> git.ipfire.org Git - people/ms/network.git/blobdiff - src/functions/functions.editor
Drop old locking functions
[people/ms/network.git] / src / functions / functions.editor
index 6edac62a2125ba2d1809820453bd1502f7ecda00..8f0cc0bc901082b75478061a4ad4ca2d23fdafc0 100644 (file)
 #                                                                             #
 ###############################################################################
 
-editor_cleanup() {
-       # Cleanup after a file was edited
-       assert [ $# -eq 2 ]
-
-       local file=${1}
-       local temp_file=${2}
-
-       lock_release "${file}.lock"
-       rm -f ${temp_file}
-}
-
 editor_find_best() {
        # Open a file with the best available editor
        assert [ $# -eq 1 ]
@@ -62,31 +51,26 @@ editor_find_best() {
 }
 
 editor() {
-       # This function open a file for editing and take care of all preperation and postprocessing
-       assert [ $# -ge 1 ]
+       local file="${1}"
+       assert isset file
 
-       local file=${1}
        if [ ! -f ${file} ] || [ ! -w ${file} ]; then
                error "${file} is not valid file or is not writeable"
                return ${EXIT_ERROR}
        fi
 
-       local check_func=${2}
+       lock "${file}.lock" __editor "$@"
+}
 
-       # check if the file is locked
-       if lock_exists "${file}.lock"; then
-               error "Cannot edit ${file} because it is locked"
-               return ${EXIT_ERROR}
-       fi
+__editor() {
+       # This function open a file for editing and take care of all preperation and postprocessing
+       assert [ $# -ge 1 ]
 
-       # lock the file
-       if ! lock_acquire "${file}.lock"; then
-               error "Cannot lock file ${file}"
-               return ${EXIT_ERROR}
-       fi
+       local file="${1}"
+       local check_func="${2}"
 
        # create a temporary file
-       local temp_file=$(mktemp)
+       local temp_file="$(mktemp)"
 
        if ! [ -f "${temp_file}" ]; then
                error "Cannot create temporary file"
@@ -98,21 +82,26 @@ editor() {
        # edit the file
        if ! editor_find_best "${temp_file}"; then
                error "Could not edit ${file}"
-               # cleanup
-               editor_cleanup "${file}" "${temp_file}"
+
+               # Delete temporary file
+               file_delete "${temp_file}"
+
+               return ${EXIT_ERROR}
        fi
 
        # run the check if we have one
        if isset check_func && ! editor_check "${check_func}" "${temp_file}"; then
+               # Delete temporary file
+               file_delete "${temp_file}"
+
                return ${EXIT_ERROR}
        fi
 
        # copy the changes back
        cp -f "${temp_file}" "${file}"
 
-       # cleanup
-       editor_cleanup "${file}" "${temp_file}"
-
+       # Delete temporary file
+       file_delete "${temp_file}"
 }
 
 editor_check() {