view core/src/luan/modules/host/Hosting.luan @ 615:abc3198c86dd

fix tail recursion bug; add Hosting.exists();
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 17 Dec 2015 01:53:48 -0700
parents b4f3dbe1c6e3
children cc3a68033179
line wrap: on
line source

-- Hosting

local Luan = require "luan:Luan"
local error = Luan.error
local ipairs = Luan.ipairs or error()
local pairs = Luan.pairs or error()
local Io = require "luan:Io"
local print = Io.print or error()
local Rpc = require "luan:Rpc"


local M = {}

M.port = 9101

function M.push(domain,password,dir)
	local my_dir = Io.uri("file:"..dir)
	my_dir.exists() or error("directory '"..dir.."' not found")
	my_dir.is_directory() or error("'"..dir.."' is not a directory")
	local socket = "socket:" .. domain .. ":" .. M.port
	local host = Rpc.remote(socket)
	local tree = host.get(domain,password)
	if tree == nil then
		print("creating "..domain)
		tree = host.create(domain,password)
	end

	local function process(there_parent,there,here)
		if here.is_file() then
			if there == nil or there.last_modified < here.last_modified() then
				print("copying "..here.to_string())
				host.copy_file(domain,password,there_parent.path,here.name(),here.read_binary())
			end
		elseif here.is_directory() then
			if here.name() == "local" then
				return
			end
			if there == nil then
				there = host.mkdir(domain,password,there_parent.path,here.name())
			end
			for _, here_child in ipairs(here.children()) do
				local name = here_child.name()
				process(there,there.children[name],here_child)
				there.children[name] = nil
			end
			for _, there_child in pairs(there.children) do
				print("deleting "..there_child.name)
				host.delete_unused(domain,password,there_child.path)
			end
		else
			error "not file or dir"
		end
	end

	process( nil, tree, my_dir )

	host.remove_handler(domain,password)
end

function M.delete(domain,password)
	local socket = "socket:" .. domain .. ":" .. M.port
	local host = Rpc.remote(socket)
	host.delete(domain,password)
end

function M.exists(domain)
	local socket = "socket:" .. domain .. ":" .. M.port
	local host = Rpc.remote(socket)
	return host.exists(domain)
end

return M