changeset 1374:72b699bad1a4

add Backup.luan
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 07 Jul 2019 22:49:36 -0600
parents 372906d1d08b
children 5c3702f60200
files src/luan/host/Backup.luan src/luan/modules/Thread.luan
diffstat 2 files changed, 72 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/luan/host/Backup.luan	Sun Jul 07 22:49:36 2019 -0600
@@ -0,0 +1,69 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local ipairs = Luan.ipairs or error()
+local Io = require "luan:Io.luan"
+local uri = Io.uri or error()
+local Thread = require "luan:Thread.luan"
+local run_in_lock = Thread.run_in_lock or error()
+local backup_write_lock = Thread.backup_write_lock or error()
+
+
+local Backup = {}
+
+local function backup_nonlocal(from,to)
+	to.mkdir()
+	for _, from_child in ipairs(from.children()) do
+		local name = from_child.name()
+		local to_child = to.child(name)
+		if from_child.is_directory() then
+			if name ~= "local" then
+				backup_nonlocal( from_child, to_child )
+			end
+		elseif from_child.is_file() then
+			to_child.link_to(from_child)
+		else
+			error(from_child.to_string().." isn't dir or file")
+		end
+	end
+end
+
+local function backup_all(from,to)
+	to.mkdir()
+	for _, from_child in ipairs(from.children()) do
+		local name = from_child.name()
+		local to_child = to.child(name)
+		if from_child.is_directory() then
+			backup_all( from_child, to_child )
+		elseif from_child.is_file() then
+			to_child.link_to(from_child)
+		else
+			error(from_child.to_string().." isn't dir or file")
+		end
+	end
+end
+
+local function backup_local(from,to)
+	local path = "site/private/local/backup"
+	from = from.child(path)
+	to = to.child(path)
+	if from.exists() then
+		from.is_directory() or error(from.to_string().." isn't dir")
+		backup_all(from,to)
+	end
+end
+
+local function backup(backup_dir)
+	local sites_dir = uri "file:sites"
+	backup_dir.mkdir()
+	for _, site_dir in ipairs(sites_dir.children()) do
+		local to = backup_dir.child(site_dir.name())
+		backup_nonlocal( site_dir, to )
+		backup_local( site_dir, to )
+	end
+end
+
+function Backup.backup(backup_dir)
+	run_in_lock( backup_write_lock, backup, backup_dir )
+end
+
+return Backup
--- a/src/luan/modules/Thread.luan	Mon Jul 01 22:54:37 2019 -0600
+++ b/src/luan/modules/Thread.luan	Sun Jul 07 22:49:36 2019 -0600
@@ -86,6 +86,9 @@
 local backup_lock = ThreadLuan.backupLock.readLock()
 local run_in_lock = ThreadLuan.runInLock
 
+Thread.run_in_lock = run_in_lock
+Thread.backup_write_lock = ThreadLuan.backupLock.writeLock()
+
 function Thread.run_for_backup(fn)
 	return run_in_lock(backup_lock,fn)
 end