gfse: grammar sharing: bug fixes and win32 support

win32 support is untested
This commit is contained in:
hallgren
2011-11-01 17:45:57 +00:00
parent 51f35132df
commit 5664c0699d
3 changed files with 33 additions and 11 deletions

View File

@@ -1,3 +1,4 @@
{-# LANGUAGE CPP #-}
module GFServer(server) where module GFServer(server) where
import Data.List(partition) import Data.List(partition)
import qualified Data.Map as M import qualified Data.Map as M
@@ -9,8 +10,10 @@ import System.Directory(doesDirectoryExist,doesFileExist,createDirectory,
setCurrentDirectory,getCurrentDirectory, setCurrentDirectory,getCurrentDirectory,
getDirectoryContents,removeFile,removeDirectory) getDirectoryContents,removeFile,removeDirectory)
import System.FilePath(takeExtension,takeFileName,takeDirectory,(</>)) import System.FilePath(takeExtension,takeFileName,takeDirectory,(</>))
import System.Posix.Files(getFileStatus,isSymbolicLink,removeLink, #ifndef mingw32_HOST_OS
import System.Posix.Files(getSymbolicLinkStatus,isSymbolicLink,removeLink,
createSymbolicLink) createSymbolicLink)
#endif
import Control.Concurrent.MVar(newMVar,modifyMVar) import Control.Concurrent.MVar(newMVar,modifyMVar)
import Network.URI(URI(..)) import Network.URI(URI(..))
import Network.Shed.Httpd(initServer,Request(..),Response(..),queryToArguments, import Network.Shed.Httpd(initServer,Request(..),Response(..),queryToArguments,
@@ -90,7 +93,10 @@ handle state0 cache execute1
do cwd <- getCurrentDirectory do cwd <- getCurrentDirectory
b <- try $ setCurrentDirectory dir b <- try $ setCurrentDirectory dir
case b of case b of
Left _ -> return (state,resp404 dir) Left _ -> do b <- try $ readFile dir -- poor man's symbolic links
case b of
Left _ -> return (state,resp404 dir)
Right dir' -> cd dir' qs'
Right _ -> do logPutStrLn $ "cd "++dir Right _ -> do logPutStrLn $ "cd "++dir
r <- try (ok dir qs') r <- try (ok dir qs')
setCurrentDirectory cwd setCurrentDirectory cwd
@@ -143,11 +149,20 @@ handle state0 cache execute1
download path _ = (,) state `fmap` serveStaticFile path download path _ = (,) state `fmap` serveStaticFile path
link_directories olddir newdir@('/':'t':'m':'p':'/':_) _ | olddir/=newdir = link_directories olddir newdir@('/':'t':'m':'p':'/':_) _ | old/=new =
do setCurrentDirectory ".." do setCurrentDirectory ".."
st <- getFileStatus old logPutStrLn =<< getCurrentDirectory
if isSymbolicLink st then removeLink old else removeDir old logPutStrLn $ "link_dirs new="++new++", old="++old
#ifdef mingw32_HOST_OS
isDir <- doesDirectoryExist old
if isDir then removeDir old else removeFile old
writeFile old new -- poor man's symbolic links
#else
isLink <- isSymbolicLink `fmap` getSymbolicLinkStatus old
logPutStrLn $ "old is link: "++show isLink
if isLink then removeLink old else removeDir old
createSymbolicLink new old createSymbolicLink new old
#endif
return (state,ok200 "") return (state,ok200 "")
where where
old = takeFileName olddir old = takeFileName olddir

View File

@@ -1,11 +1,19 @@
function with_dir(cont) { function with_dir(cont) {
function have_dir(dir) {
var unique_id=local.get("unique_id")
if(!unique_id) {
unique_id=dir.substr(10) // skip "/tmp/gfse."
local.put("unique_id",unique_id)
}
cont(dir,unique_id)
}
var dir=local.get("dir",""); var dir=local.get("dir","");
if(/^\/tmp\//.test(dir)) cont(dir); if(/^\/tmp\//.test(dir)) have_dir(dir);
else ajax_http_get("/new", else ajax_http_get("/new",
function(dir) { function(dir) {
local.put("dir",dir); local.put("dir",dir);
cont(dir); have_dir(dir);
}); });
} }
@@ -42,7 +50,7 @@ function upload(g) {
// Upload the grammar to store it in the cloud // Upload the grammar to store it in the cloud
function upload_json(cont) { function upload_json(cont) {
function upload2(dir) { function upload2(dir,unique_id) {
function upload3(resptext,status) { function upload3(resptext,status) {
local.put("json_uploaded",Date.now()); local.put("json_uploaded",Date.now());
//debug("Upload complete") //debug("Upload complete")
@@ -64,7 +72,6 @@ function upload_json(cont) {
} }
} }
var prefix=dir.substr(10)+"-" // skip "/tmp/gfse."
//debug("New form data"); //debug("New form data");
//var form=new FormData(); // !!! Doesn't work on Android 2.2! //var form=new FormData(); // !!! Doesn't work on Android 2.2!
var form={dir:dir}; var form={dir:dir};
@@ -73,14 +80,13 @@ function upload_json(cont) {
var g=local.get(i,null); var g=local.get(i,null);
if(g) { if(g) {
if(!g.unique_name) { if(!g.unique_name) {
g.unique_name=prefix+i; g.unique_name=unique_id+"-"+i;
save_grammar(g) save_grammar(g)
} }
//form.append(g.unique_name+".json",JSON.stringify(g)); //form.append(g.unique_name+".json",JSON.stringify(g));
form[encodeURIComponent(g.unique_name+".json")]=JSON.stringify(g) form[encodeURIComponent(g.unique_name+".json")]=JSON.stringify(g)
} }
} }
//debug("Upload to "+prefix);
ajax_http_post("/cloud","command=upload"+encodeArgs(form),upload3,cont) ajax_http_post("/cloud","command=upload"+encodeArgs(form),upload3,cont)
} }

View File

@@ -1075,6 +1075,7 @@ function download_from_cloud() {
} }
function download3() { function download3() {
//debug("Uploading local grammars to cloud"); //debug("Uploading local grammars to cloud");
local.put("dir",newdir);
upload_json(download4) upload_json(download4)
} }
function download4() { function download4() {