From 4d17687f12e59e05038582b8c796b5176246e151 Mon Sep 17 00:00:00 2001 From: mfranz Date: Fri, 13 Jun 2008 13:01:38 +0000 Subject: [PATCH] move fixpath back to string.nas. It *is* about strings, after all, and also usable for property paths, so io.nas doesn't seem the right place. --- Nasal/io.nas | 38 ++++---------------------------------- Nasal/string.nas | 31 +++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 34 deletions(-) diff --git a/Nasal/io.nas b/Nasal/io.nas index d765a01c1..7d2f8132e 100644 --- a/Nasal/io.nas +++ b/Nasal/io.nas @@ -113,44 +113,14 @@ var writexml = func(path, node, indent = "\t", prefix = "___") { die("writexml(): tree has more than one root node"); } -# Removes superfluous slashes, empty and "." elements, expands -# all ".." elements, and turns all backslashes into slashes. -# The result will start with a slash if it started with a slash -# or backslash, it will end without slash. Should be applied on -# absolute property or file paths, otherwise ".." elements might -# be resolved wrongly. -# -var fixpath = func(path) { - var d = ""; - for(var i = 0; i < size(path); i += 1) - d ~= path[i] == `\\` ? "/" : chr(path[i]); - - var prefix = d[0] == `/` ? "/" : ""; - var stack = []; - foreach(var e; split("/", d)) { - if(e == "." or e == "") - continue; - elsif(e == "..") - pop(stack); - else - append(stack, e); - } - if(!size(stack)) - return "/"; - path = stack[0]; - foreach(var s; subvec(stack, 1)) - path ~= "/" ~ s; - return prefix ~ path; -} - # Redefine io.open() such that files can only be written under authorized directories. # setlistener("/sim/signals/nasal-dir-initialized", func { var _open = open; var writable_dirs = [ - # "*", # any - # fixpath(getprop("/sim/fg-root")) ~ "/Scenery/", - fixpath(getprop("/sim/fg-home")) ~ "/", + # "", # any + # string.fixpath(getprop("/sim/fg-root")) ~ "/Scenery/", + string.fixpath(getprop("/sim/fg-home")) ~ "/", "/tmp/", "/var/tmp/", "[A-Za-z]:TMP/", "[A-Za-z]:TEMP/", "[A-Za-z]:/TMP/", "[A-Za-z]:/TEMP/", @@ -166,7 +136,7 @@ setlistener("/sim/signals/nasal-dir-initialized", func { return _open(path, mode); } - var fpath = fixpath(path); + var fpath = string.fixpath(path); if(fpath != path) print(debug._error("SECURITY: fix path '" ~ path ~ "' -> '" ~ fpath ~ "'")); foreach(var p; writable_dirs) { print("SECURITY: check for path '" ~ p ~ "'"); diff --git a/Nasal/string.nas b/Nasal/string.nas index 92232c242..2b2204dca 100644 --- a/Nasal/string.nas +++ b/Nasal/string.nas @@ -155,3 +155,34 @@ var match = func(str, patt) { } +## +# Removes superfluous slashes, empty and "." elements, expands +# all ".." elements, and turns all backslashes into slashes. +# The result will start with a slash if it started with a slash +# or backslash, it will end without slash. Should be applied on +# absolute property or file paths, otherwise ".." elements might +# be resolved wrongly. +# +var fixpath = func(path) { + var d = ""; + for (var i = 0; i < size(path); i += 1) + d ~= path[i] == `\\` ? "/" : chr(path[i]); + + var prefix = d[0] == `/` ? "/" : ""; + var stack = []; + foreach (var e; split("/", d)) { + if (e == "." or e == "") + continue; + elsif (e == "..") + pop(stack); + else + append(stack, e); + } + if (!size(stack)) + return "/"; + path = stack[0]; + foreach (var s; subvec(stack, 1)) + path ~= "/" ~ s; + return prefix ~ path; +} +