summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Estabrook <jestabro@vyos.io>2025-11-23 15:10:01 -0600
committerJohn Estabrook <jestabro@vyos.io>2025-12-02 13:10:41 -0600
commitf28bad075bc46c95dd2e52037d6e52d1621d2a49 (patch)
tree25637740098ebb3edb896fd11ca6ff72bc2e91d3
parent46890b13b9e565b7b61d2c75ca92441cb7490f7f (diff)
downloadvyconf-f28bad075bc46c95dd2e52037d6e52d1621d2a49.tar.gz
vyconf-f28bad075bc46c95dd2e52037d6e52d1621d2a49.zip
T8032: add vyconfd handlers for config_unsaved
-rw-r--r--src/session.ml16
-rw-r--r--src/session.mli2
-rw-r--r--src/vyconfd.ml11
3 files changed, 25 insertions, 4 deletions
diff --git a/src/session.ml b/src/session.ml
index 248121d..478c423 100644
--- a/src/session.ml
+++ b/src/session.ml
@@ -379,6 +379,18 @@ let save w s file =
| Error e -> raise (Session_error (Printf.sprintf "Error saving config: %s" e))
| Ok () -> s
+let remove_file file =
+ if Sys.file_exists file then Sys.remove file
+
+let config_unsaved w s file id =
+ let tmp_save = Printf.sprintf "/tmp/config.running_%s" id in
+ let res =
+ try
+ let _ = save w s tmp_save in
+ not (Vyos1x.Util.file_compare ~ignore_line_prefix:"//" tmp_save file)
+ with Session_error _ -> true (* false positive on unlikely error *)
+ in remove_file tmp_save; res
+
let write_running_cache w =
(* alert exn Internal.write_internal:
[Internal.Write_error] caught
@@ -480,10 +492,6 @@ let get_config w s id =
in id
let cleanup_config w id =
- let remove_file file =
- if Sys.file_exists file then
- Sys.remove file
- in
let vc = w.vyconf_config in
let running_cache = Printf.sprintf "%s_%s" vc.running_cache id in
let session_cache = Printf.sprintf "%s_%s" vc.session_cache id in
diff --git a/src/session.mli b/src/session.mli
index 757bc18..cb49bf6 100644
--- a/src/session.mli
+++ b/src/session.mli
@@ -69,6 +69,8 @@ val merge : world -> session_data -> string -> bool -> session_data
val save : world -> session_data -> string -> session_data
+val config_unsaved : world -> session_data -> string -> string -> bool
+
val get_value : world -> session_data -> string list -> string
val get_values : world -> session_data -> string list -> string list
diff --git a/src/vyconfd.ml b/src/vyconfd.ml
index ed84978..dc302e5 100644
--- a/src/vyconfd.ml
+++ b/src/vyconfd.ml
@@ -407,6 +407,16 @@ let edit_level_root world token (_req: request_edit_level_root) =
if Session.edit_level_root world (find_session token) then response_tmpl
else {response_tmpl with status=Fail}
+let config_unsaved world token (req: request_config_unsaved) =
+ let saved_file =
+ match req.file with
+ | None -> defaults.legacy_config_path
+ | Some file -> file
+ in
+ if Session.config_unsaved world (find_session token) saved_file token
+ then response_tmpl
+ else {response_tmpl with status=Fail}
+
let send_response oc resp =
let enc = Pbrt.Encoder.create () in
let%lwt () = encode_pb_response resp enc |> return in
@@ -463,6 +473,7 @@ let rec handle_connection world ic oc () =
| Some t, Reset_edit_level r -> reset_edit_level world t r
| Some t, Get_edit_level r -> get_edit_level world t r
| Some t, Edit_level_root r -> edit_level_root world t r
+ | Some t, Config_unsaved r -> config_unsaved world t r
| _ -> failwith "Unimplemented"
) |> Lwt.return
end