sorts file helpers; add directory walk helpers (#4703)

Co-authored-by: VM_USER <VM_USER>
This commit is contained in:
silicons
2022-11-12 19:12:09 +01:00
committed by GitHub
co-authored by VM_USER <VM_USER>
parent 09dd21359b
commit 94dd2baeb4
7 changed files with 98 additions and 64 deletions
+4 -1
View File
@@ -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"
@@ -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)
+14
View File
@@ -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)
+24
View File
@@ -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)
+52
View File
@@ -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
@@ -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)
@@ -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)