From 94dd2baeb46ff24c52b6fc9628f52a42e3573ace Mon Sep 17 00:00:00 2001 From: silicons <2003111+silicons@users.noreply.github.com> Date: Sat, 12 Nov 2022 10:12:09 -0800 Subject: [PATCH] sorts file helpers; add directory walk helpers (#4703) Co-authored-by: VM_USER --- citadel.dme | 5 +- .../{files.dm => files/client_io.dm} | 59 ------------------- code/__HELPERS/files/md5.dm | 14 +++++ code/__HELPERS/files/paths.dm | 24 ++++++++ code/__HELPERS/files/walk.dm | 52 ++++++++++++++++ .../controllers/subsystem/mapping/_mapping.dm | 4 -- .../subsystem/mapping/obfuscation.dm | 4 ++ 7 files changed, 98 insertions(+), 64 deletions(-) rename code/__HELPERS/{files.dm => files/client_io.dm} (54%) create mode 100644 code/__HELPERS/files/md5.dm create mode 100644 code/__HELPERS/files/paths.dm create mode 100644 code/__HELPERS/files/walk.dm diff --git a/citadel.dme b/citadel.dme index 16ce72c04f5..9679e164365 100644 --- a/citadel.dme +++ b/citadel.dme @@ -240,7 +240,6 @@ #include "code\__HELPERS\datum.dm" #include "code\__HELPERS\do_after.dm" #include "code\__HELPERS\events.dm" -#include "code\__HELPERS\files.dm" #include "code\__HELPERS\filters.dm" #include "code\__HELPERS\game.dm" #include "code\__HELPERS\global_lists.dm" @@ -270,6 +269,10 @@ #include "code\__HELPERS\varset_callback.dm" #include "code\__HELPERS\vector.dm" #include "code\__HELPERS\view.dm" +#include "code\__HELPERS\files\client_io.dm" +#include "code\__HELPERS\files\md5.dm" +#include "code\__HELPERS\files\paths.dm" +#include "code\__HELPERS\files\walk.dm" #include "code\__HELPERS\icons\flatten.dm" #include "code\__HELPERS\icons\flatten_old.dm" #include "code\__HELPERS\lists\_string_lists.dm" diff --git a/code/__HELPERS/files.dm b/code/__HELPERS/files/client_io.dm similarity index 54% rename from code/__HELPERS/files.dm rename to code/__HELPERS/files/client_io.dm index 113aa724987..ee09423b994 100644 --- a/code/__HELPERS/files.dm +++ b/code/__HELPERS/files/client_io.dm @@ -66,62 +66,3 @@ GLOBAL_VAR_INIT(fileaccess_timer, 0) return FALSE #undef FTPDELAY #undef ADMIN_FTPDELAY_MODIFIER - -/proc/pathwalk(path) - var/list/jobs = list(path) - var/list/filenames = list() - - while(jobs.len) - var/current_dir = pop(jobs) - var/list/new_filenames = flist(current_dir) - for(var/new_filename in new_filenames) - // If filename ends in / it is a directory, append to currdir. - if(findtext(new_filename, "/", -1)) - jobs += current_dir + new_filename - else - filenames += current_dir + new_filename - return filenames - -/proc/pathflatten(path) - return replacetext(path, "/", "_") - -/** - * Returns the md5 of a file at a given path. - */ -/proc/md5filepath(path) - . = md5(file(path)) - -/** - * Save file as an external file then md5 it. - * Used because md5ing files stored in the rsc sometimes gives incorrect md5 results. - */ -/proc/md5asfile(file) - var/static/notch = 0 - // It's importaint this code can handle md5filepath sleeping instead of hard blocking, if it's converted to use rust_g. - var/filename = "tmp/md5asfile.[world.realtime].[world.timeofday].[world.time].[world.tick_usage].[notch]" - notch = WRAP(notch+1, 0, 2**15) - fcopy(file, filename) - . = md5filepath(filename) - fdel(filename) - -/** - * Sanitizes the name of each node in the path. - * - * Im case you are wondering when to use this proc and when to use SANITIZE_FILENAME, - * - * You use SANITIZE_FILENAME to sanitize the name of a file [e.g. example.txt] - * - * You use sanitize_filepath sanitize the path of a file [e.g. root/node/example.txt] - * - * If you use SANITIZE_FILENAME to sanitize a file path things will break. - */ -/proc/sanitize_filepath(path) - . = "" - // Very much intentionally hardcoded. - var/delimiter = "/" - var/list/all_nodes = splittext(path, delimiter) - for(var/node in all_nodes) - if(.) - // Add the delimiter before each successive node. - . += delimiter - . += SANITIZE_FILENAME(node) diff --git a/code/__HELPERS/files/md5.dm b/code/__HELPERS/files/md5.dm new file mode 100644 index 00000000000..d318830710f --- /dev/null +++ b/code/__HELPERS/files/md5.dm @@ -0,0 +1,14 @@ +/// Returns the md5 of a file at a given path. +/proc/md5filepath(path) + . = md5(file(path)) + +/// Save file as an external file then md5 it. +/// Used because md5ing files stored in the rsc sometimes gives incorrect md5 results. +/proc/md5asfile(file) + var/static/notch = 0 + // its importaint this code can handle md5filepath sleeping instead of hard blocking, if it's converted to use rust_g. + var/filename = "tmp/md5asfile.[world.realtime].[world.timeofday].[world.time].[world.tick_usage].[notch]" + notch = WRAP(notch+1, 0, 2^15) + fcopy(file, filename) + . = md5filepath(filename) + fdel(filename) diff --git a/code/__HELPERS/files/paths.dm b/code/__HELPERS/files/paths.dm new file mode 100644 index 00000000000..c12b4bf3247 --- /dev/null +++ b/code/__HELPERS/files/paths.dm @@ -0,0 +1,24 @@ +/proc/pathflatten(path) + return replacetext(path, "/", "_") + +/** + * Sanitizes the name of each node in the path. + * + * Im case you are wondering when to use this proc and when to use SANITIZE_FILENAME, + * + * You use SANITIZE_FILENAME to sanitize the name of a file [e.g. example.txt] + * + * You use sanitize_filepath sanitize the path of a file [e.g. root/node/example.txt] + * + * If you use SANITIZE_FILENAME to sanitize a file path things will break. + */ +/proc/sanitize_filepath(path) + . = "" + // Very much intentionally hardcoded. + var/delimiter = "/" + var/list/all_nodes = splittext(path, delimiter) + for(var/node in all_nodes) + if(.) + // Add the delimiter before each successive node. + . += delimiter + . += SANITIZE_FILENAME(node) diff --git a/code/__HELPERS/files/walk.dm b/code/__HELPERS/files/walk.dm new file mode 100644 index 00000000000..76e749f2caa --- /dev/null +++ b/code/__HELPERS/files/walk.dm @@ -0,0 +1,52 @@ +/** + * walks a directory, returning paths of all files. + * + * @params + * - roots - a single or a list of roots. roots MUST end with /, or they won't work. + * - maxdepth - maximum depth to walk. default is 5. + */ +/proc/directory_walk(list/roots, maxdepth = 5) + ASSERT(roots) + if(!islist(roots)) + roots = list(roots) + return directory_walk_internal(islist(roots)? roots : list(roots), null, maxdepth) + +/** + * walks a directory, returning paths of all files with certain extensions + * + * @params + * - roots - a single or a list of roots. roots MUST end with /, or they won't work. + * - exts - a single or a list of extensions. do not including the ., e.g. pass in "txt", not ".txt". + * - maxdepth - maximum depth to walk. default is 5. + */ +/proc/directory_walk_exts(list/roots, list/exts, maxdepth = 5) + ASSERT(roots) + if(!islist(roots)) + roots = list(roots) + ASSERT(exts) + if(!islist(exts)) + exts = list(exts) + return directory_walk_internal(islist(roots)? roots : list(roots), new /regex("\\.([exts.Join("|")])$", "i"), maxdepth) + +/proc/directory_walk_internal(list/roots, regex/R, depth_remaining) + if(!length(roots)) + return + if(!islist(roots)) + CRASH("Invalid roots: [roots]") + if(depth_remaining < 0) + return + . = list() + var/list/nested = list() + for(var/path in roots) + if(path[1] == "/") + // seriously you don't need byond servers touching outside of their sandboxes. + CRASH("absolute path not allowed: [path]") + for(var/found in flist(path)) + if(found[length(found)] != "/") // file + if(!R || R.Find(found)) + . += path + found + else + nested += path + found + var/list/recursed = directory_walk_internal(nested, R, depth_remaining - 1) + if(recursed) + . += recursed diff --git a/code/controllers/subsystem/mapping/_mapping.dm b/code/controllers/subsystem/mapping/_mapping.dm index 0b8168c85c1..22a254da608 100644 --- a/code/controllers/subsystem/mapping/_mapping.dm +++ b/code/controllers/subsystem/mapping/_mapping.dm @@ -29,10 +29,6 @@ SUBSYSTEM_DEF(mapping) /// Cached map name for statpanel var/static/stat_map_name = "Loading..." - // Obfuscation Module - /// "secret" key - var/obfuscation_secret - //dlete dis once #39770 is resolved /datum/controller/subsystem/mapping/proc/HACK_LoadMapConfig() if(!config) diff --git a/code/controllers/subsystem/mapping/obfuscation.dm b/code/controllers/subsystem/mapping/obfuscation.dm index 502aaf8d9a3..f6080ab817c 100644 --- a/code/controllers/subsystem/mapping/obfuscation.dm +++ b/code/controllers/subsystem/mapping/obfuscation.dm @@ -3,6 +3,10 @@ * * Allows for generating arbitrary obfuscation IDs from static mapload time IDs, that are deterministic within a round. */ +/datum/controller/subsystem/mapping + /// "secret" key + var/obfuscation_secret + /datum/controller/subsystem/mapping/PreInit() . = ..() if(!obfuscation_secret)