From 847f0a4564e6f2b6084d591a0e8539c080bb49bb Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Sat, 25 Mar 2023 20:28:58 +0100 Subject: global-functions: $MkDir: drop unused function --- global-functions.rsc | 1 - 1 file changed, 1 deletion(-) diff --git a/global-functions.rsc b/global-functions.rsc index 2249fe9..416ea08 100644 --- a/global-functions.rsc +++ b/global-functions.rsc @@ -558,7 +558,6 @@ :global CleanFilePath; :global GetRandom20CharAlNum; :global LogPrintExit2; - :global RequiredRouterOS; :global WaitForFile; :set Path [ $CleanFilePath $Path ]; -- cgit v1.2.3-54-g00ecf From 7293306f76b638f8ca94e9916e92389aed34b770 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Sat, 25 Mar 2023 21:49:12 +0100 Subject: global-functions: $MkDir: return false on error --- global-functions.rsc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/global-functions.rsc b/global-functions.rsc index 416ea08..ffe9a4b 100644 --- a/global-functions.rsc +++ b/global-functions.rsc @@ -589,7 +589,7 @@ $WaitForFile "tmpfs"; } on-error={ $LogPrintExit2 warning $0 ("Creating disk of type tmpfs failed!") false; - :set Error true; + :return false; } } :set Continue true; -- cgit v1.2.3-54-g00ecf From d3ca5063947f4350721cfe1828faae2cd771f133 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Sat, 25 Mar 2023 21:27:24 +0100 Subject: global-functions: $MkDir: create tmpfs in local function --- global-functions.rsc | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/global-functions.rsc b/global-functions.rsc index ffe9a4b..2eb0416 100644 --- a/global-functions.rsc +++ b/global-functions.rsc @@ -560,6 +560,26 @@ :global LogPrintExit2; :global WaitForFile; + :local MkTmpfs do={ + :global LogPrintExit2; + :global WaitForFile; + + :if ([ :len [ /disk/find where slot=tmpfs type=tmpfs ] ] = 1) do={ + :return true; + } + + $LogPrintExit2 info $0 ("Creating disk of type tmpfs.") false; + /file/remove [ find where name="tmpfs" type="directory" ]; + :do { + /disk/add slot=tmpfs type=tmpfs tmpfs-max-size=([ /system/resource/get total-memory ] / 3); + $WaitForFile "tmpfs"; + } on-error={ + $LogPrintExit2 warning $0 ("Creating disk of type tmpfs failed!") false; + :return false; + } + :return true; + } + :set Path [ $CleanFilePath $Path ]; :if ($Path = "") do={ @@ -581,16 +601,8 @@ } :if ($Continue = false && $PathNext = "tmpfs") do={ - :if ([ :len [ /disk/find where slot=tmpfs type=tmpfs ] ] = 0) do={ - $LogPrintExit2 info $0 ("Creating disk of type tmpfs.") false; - /file/remove [ find where name="tmpfs" type="directory" ]; - :do { - /disk/add slot=tmpfs type=tmpfs tmpfs-max-size=([ /system/resource/get total-memory ] / 3); - $WaitForFile "tmpfs"; - } on-error={ - $LogPrintExit2 warning $0 ("Creating disk of type tmpfs failed!") false; - :return false; - } + :if ([ $MkTmpfs ] = false) do={ + :return false; } :set Continue true; } -- cgit v1.2.3-54-g00ecf From 7fdec1abed5be3c7dc651cdd6b878519c848f15f Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Tue, 28 Mar 2023 11:07:45 +0200 Subject: global-functions: $MkDir: add extra block for indention... ... to make the next commit cleaner. No functional change. --- global-functions.rsc | 62 +++++++++++++++++++++++++++------------------------- 1 file changed, 32 insertions(+), 30 deletions(-) diff --git a/global-functions.rsc b/global-functions.rsc index 2eb0416..3f0aa6a 100644 --- a/global-functions.rsc +++ b/global-functions.rsc @@ -590,41 +590,43 @@ :return true; } - :local Error false; - :local PathNext ""; - :foreach Dir in=[ :toarray [ $CharacterReplace $Path "/" "," ] ] do={ - :local Continue false; - :set PathNext [ $CleanFilePath ($PathNext . "/" . $Dir) ]; - - :if ([ :len [ /file/find where name=$PathNext !(name="tmpfs") type="directory" ] ] = 1) do={ - :set Continue true; - } - - :if ($Continue = false && $PathNext = "tmpfs") do={ - :if ([ $MkTmpfs ] = false) do={ - :return false; + { + :local Error false; + :local PathNext ""; + :foreach Dir in=[ :toarray [ $CharacterReplace $Path "/" "," ] ] do={ + :local Continue false; + :set PathNext [ $CleanFilePath ($PathNext . "/" . $Dir) ]; + + :if ([ :len [ /file/find where name=$PathNext !(name="tmpfs") type="directory" ] ] = 1) do={ + :set Continue true; } - :set Continue true; - } - :if ($Continue = false && [ :len [ /file/find where name=$PathNext ] ] = 1) do={ - $LogPrintExit2 warning $0 ("The path '" . $PathNext . "' exists, but is not a directory.") false; - :return false; - } - - :if ($Continue = false) do={ - :local Name ($PathNext . "-" . [ $GetRandom20CharAlNum 6 ]); - :do { - /ip/smb/share/add disabled=yes directory=$PathNext name=$Name; - $WaitForFile $PathNext; - } on-error={ - $LogPrintExit2 warning $0 ("Making directory '" . $PathNext . "' failed!") false; - :set Error true; + :if ($Continue = false && $PathNext = "tmpfs") do={ + :if ([ $MkTmpfs ] = false) do={ + :return false; + } + :set Continue true; } - /ip/smb/share/remove [ find where name=$Name ]; - :if ($Error = true) do={ + + :if ($Continue = false && [ :len [ /file/find where name=$PathNext ] ] = 1) do={ + $LogPrintExit2 warning $0 ("The path '" . $PathNext . "' exists, but is not a directory.") false; :return false; } + + :if ($Continue = false) do={ + :local Name ($PathNext . "-" . [ $GetRandom20CharAlNum 6 ]); + :do { + /ip/smb/share/add disabled=yes directory=$PathNext name=$Name; + $WaitForFile $PathNext; + } on-error={ + $LogPrintExit2 warning $0 ("Making directory '" . $PathNext . "' failed!") false; + :set Error true; + } + /ip/smb/share/remove [ find where name=$Name ]; + :if ($Error = true) do={ + :return false; + } + } } } :return true; -- cgit v1.2.3-54-g00ecf From f2e811527206373d91ffa36f4a9662b1adb563d2 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Sat, 25 Mar 2023 21:59:40 +0100 Subject: global-functions: $MkDir: make directory by adding file This is new functionality in RouterOS 7.9beta4, where new file can be added with `/file/add ...`. This also creates directories for the full path. --- global-functions.rsc | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/global-functions.rsc b/global-functions.rsc index 3f0aa6a..24b6223 100644 --- a/global-functions.rsc +++ b/global-functions.rsc @@ -558,6 +558,7 @@ :global CleanFilePath; :global GetRandom20CharAlNum; :global LogPrintExit2; + :global RequiredRouterOS; :global WaitForFile; :local MkTmpfs do={ @@ -590,7 +591,23 @@ :return true; } - { + :if ([ $RequiredRouterOS $0 "7.9beta4" false ] = true) do={ + :if ([ :pick $Path 0 5 ] = "tmpfs") do={ + :if ([ $MkTmpfs ] = false) do={ + :return false; + } + } + + :do { + :local File ($Path . "/file"); + /file/add name=$File; + $WaitForFile $File; + /file/remove $File; + } on-error={ + $LogPrintExit2 warning $0 ("Making directory '" . $Path . "' failed!") false; + :return false; + } + } else={ :local Error false; :local PathNext ""; :foreach Dir in=[ :toarray [ $CharacterReplace $Path "/" "," ] ] do={ -- cgit v1.2.3-54-g00ecf