Version

Goal:Sand-boxed access to the file system
Difficulty:Intermediate
Duration:5 minutes
Language:All

Demonstrates how to prevent access to the file system outside of a specific folder.

Python

# Sand-boxed access to the file system

import os
import harfang as hg

# mount two std file driver under two different prefixes, only one acts as a sandbox
hg.MountFileDriver(hg.StdFileDriver(os.path.dirname(__file__)), "std:")
hg.MountFileDriver(hg.StdFileDriver(os.path.dirname(__file__), True), "box:")

# access the readme.txt file outside of the std file driver root directory:
std_path = "std:../_data/owl.jpg"
res_std_access = hg.GetFilesystem().Exists(std_path)

# do the same on the sandboxed box: mount point
box_path = "box:../_data/owl.jpg"
res_box_access = hg.GetFilesystem().Exists(box_path)

print("Can access '%s': %s" % (std_path, str(res_std_access)))
print("Can access '%s': %s" % (box_path, str(res_box_access)))

Lua

hg = require("harfang")

-- Sand-boxed access to the file system

-- mount two std file driver under two different prefixes, only one acts as a sandbox
hg.MountFileDriver(hg.StdFileDriver(''), "std:")
hg.MountFileDriver(hg.StdFileDriver('', true), "box:")

-- access a data file outside of the std file driver root directory
std_path = "std:../_data/owl.jpg"
res_std_access = hg.GetFilesystem():Exists(std_path)

print("Can access '"..std_path.."': "..tostring(res_std_access))

-- do the same on the sandboxed mount point
box_path = "box:../_data/owl.jpg"
res_box_access = hg.GetFilesystem():Exists(box_path)

print("Can access '"..box_path.."': "..tostring(res_box_access))