+local function _read(id)
+ local blob = fs.readfile(sessionpath .. "/" .. id)
+ return blob
+end
+
+local function _write(id, data)
+ local f = nixio.open(sessionpath .. "/" .. id, "w", 600)
+ f:writeall(data)
+ f:close()
+end
+
+local function _checkid(id)
+ return not not (id and #id == 32 and id:match("^[a-fA-F0-9]+$"))
+end
+
+--- Write session data to a session file.
+-- @param id Session identifier
+-- @param data Session data table
+function write(id, data)
+ if not sane() then
+ prepare()
+ end
+
+ assert(_checkid(id), "Security Exception: Session ID is invalid!")
+ assert(type(data) == "table", "Security Exception: Session data invalid!")
+
+ data.atime = luci.sys.uptime()
+
+ _write(id, luci.util.get_bytecode(data))
+end
+