From 5664c0699dbf0cb553d31bfbd4e9c75e0933d806 Mon Sep 17 00:00:00 2001 From: hallgren Date: Tue, 1 Nov 2011 17:45:57 +0000 Subject: [PATCH] gfse: grammar sharing: bug fixes and win32 support win32 support is untested --- src/compiler/GFServer.hs | 25 ++++++++++++++++++++----- src/www/gfse/cloud2.js | 18 ++++++++++++------ src/www/gfse/editor.js | 1 + 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/compiler/GFServer.hs b/src/compiler/GFServer.hs index 8fe0c3294..f14ff6d89 100644 --- a/src/compiler/GFServer.hs +++ b/src/compiler/GFServer.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE CPP #-} module GFServer(server) where import Data.List(partition) import qualified Data.Map as M @@ -9,8 +10,10 @@ import System.Directory(doesDirectoryExist,doesFileExist,createDirectory, setCurrentDirectory,getCurrentDirectory, getDirectoryContents,removeFile,removeDirectory) 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) +#endif import Control.Concurrent.MVar(newMVar,modifyMVar) import Network.URI(URI(..)) import Network.Shed.Httpd(initServer,Request(..),Response(..),queryToArguments, @@ -90,7 +93,10 @@ handle state0 cache execute1 do cwd <- getCurrentDirectory b <- try $ setCurrentDirectory dir 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 r <- try (ok dir qs') setCurrentDirectory cwd @@ -143,11 +149,20 @@ handle state0 cache execute1 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 ".." - st <- getFileStatus old - if isSymbolicLink st then removeLink old else removeDir old + logPutStrLn =<< getCurrentDirectory + 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 +#endif return (state,ok200 "") where old = takeFileName olddir diff --git a/src/www/gfse/cloud2.js b/src/www/gfse/cloud2.js index 71de22387..60e57f9ba 100644 --- a/src/www/gfse/cloud2.js +++ b/src/www/gfse/cloud2.js @@ -1,11 +1,19 @@ 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",""); - if(/^\/tmp\//.test(dir)) cont(dir); + if(/^\/tmp\//.test(dir)) have_dir(dir); else ajax_http_get("/new", function(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 function upload_json(cont) { - function upload2(dir) { + function upload2(dir,unique_id) { function upload3(resptext,status) { local.put("json_uploaded",Date.now()); //debug("Upload complete") @@ -64,7 +72,6 @@ function upload_json(cont) { } } - var prefix=dir.substr(10)+"-" // skip "/tmp/gfse." //debug("New form data"); //var form=new FormData(); // !!! Doesn't work on Android 2.2! var form={dir:dir}; @@ -73,14 +80,13 @@ function upload_json(cont) { var g=local.get(i,null); if(g) { if(!g.unique_name) { - g.unique_name=prefix+i; + g.unique_name=unique_id+"-"+i; save_grammar(g) } //form.append(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) } diff --git a/src/www/gfse/editor.js b/src/www/gfse/editor.js index 859f33a04..95c72d972 100644 --- a/src/www/gfse/editor.js +++ b/src/www/gfse/editor.js @@ -1075,6 +1075,7 @@ function download_from_cloud() { } function download3() { //debug("Uploading local grammars to cloud"); + local.put("dir",newdir); upload_json(download4) } function download4() {