Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Server error: getCurrentDirectory: does not exist (No such file or directory) #303

Closed
tstgruby opened this Issue · 0 comments

2 participants

@tstgruby

Hi.

I receive this error repeatedly once an error occured during an action wrapped in `withTempDir'.

E.g. trying to export to PDF:

  1. Export.hs respondPDF uses withTempDir
  2. withTempDir creates a temporary directory
  3. respondPDF calls setCurrentDir to change into it
  4. an error occurs
  5. withTempDir's error handler removes the directory
  6. subsequent calls to getCurrentDirectory will fail.

I suggest changing the withTempDir function in this way:

-- |  Perform a function after changing into a temporary directory. Clean up afterwards.
withTempDir :: FilePath -> ((FilePath, FilePath) -> IO a) -> IO a
withTempDir baseName =
  bracket
   (do
      tempDir <- createTempDir 0 baseName
      oldDir <- getCurrentDirectory
      setCurrentDirectory tempDir
      return (oldDir, tempDir))
   (\ (oldDir, tempDir) -> do
      setCurrentDirectory oldDir
      removeDirectoryRecursive tempDir)

Usage:

respondPDF :: String -> Pandoc -> Handler
respondPDF page old_pndc = fixURLs page old_pndc >>= \pndc -> do
  cfg <- getConfig
  unless (pdfExport cfg) $ error "PDF export disabled"
  let cacheName = pathForPage page ++ ".export.pdf"
  cached <- if useCache cfg
               then lookupCache cacheName
               else return Nothing
  pdf' <- case cached of
            Just (_modtime, bs) -> return $ Right (False, L.fromChunks [bs])
            Nothing -> liftIO $ do
              template' <- liftIO $ getDefaultTemplate (pandocUserData cfg) "latex"
              template  <- either throwIO return template'
              let toc = tableOfContents cfg
              let latex = writeLaTeX defaultRespOptions{writerTemplate = template
                                                       ,writerTableOfContents = toc} pndc
              let tempfile = "export" <.> "tex"
              withTempDir "gitit-tmp-pdf" (\ (curdir, tempdir) -> do
                writeFile tempfile latex
                -- run pdflatex twice to get the references and toc right
                let cmd = "pdflatex"
                oldEnv <- getEnvironment
                let env = Just $ ("TEXINPUTS",".:" ++
                                 escapeStringUsing [(' ',"\\ "),('"',"\\\"")]
                                 (curdir </> repositoryPath cfg) ++ ":") : oldEnv
                let opts = ["-interaction=batchmode", "-no-shell-escape", tempfile]
                _ <- runShellCommand tempdir env cmd opts
                canary <- runShellCommand tempdir env cmd opts
                case canary of
                  ExitSuccess   -> do pdfBS <- L.readFile (tempdir </> "export" <.> "pdf")
                                      return $ Right (useCache cfg, pdfBS)
                  ExitFailure n -> do l <- readFileUTF8 (tempdir </> "export" <.> "log")
                                      return $ Left (n, l))
  case pdf' of
       Left (n,logOutput) -> simpleErrorHandler ("PDF creation failed with code: " ++
                               show n ++ "\n" ++ logOutput)
       Right (needsCaching, pdfBS) -> do
              when needsCaching $
                 cacheContents cacheName $ B.concat . L.toChunks $ pdfBS
              ok $ setContentType "application/pdf" $ setFilename (page ++ ".pdf") $
                        (toResponse noHtml) {rsBody = pdfBS}
@jgm jgm closed this issue from a commit
@jgm Fixed withTempDir so it returns to old directory.
Closes #303.  This is a slightly different solution than the one
proposed by tstgruby, but it has the advantage that only withTempDir
itself needs to be changed.
2cd8ca6
@jgm jgm closed this in 2cd8ca6
@segasai segasai referenced this issue from a commit in segasai/gitit
@jgm Fixed withTempDir so it returns to old directory.
Closes #303.  This is a slightly different solution than the one
proposed by tstgruby, but it has the advantage that only withTempDir
itself needs to be changed.
a406290
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.