File: AutoStart.hs

package info (click to toggle)
git-annex 10.20251029-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 75,300 kB
  • sloc: haskell: 91,492; javascript: 9,103; sh: 1,593; makefile: 216; perl: 137; ansic: 44
file content (49 lines) | stat: -rw-r--r-- 1,440 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
{- git-annex autostart file
 -
 - Copyright 2012-2019 Joey Hess <id@joeyh.name>
 -
 - Licensed under the GNU AGPL version 3 or higher.
 -}

{-# OPTIONS_GHC -fno-warn-tabs #-}

module Config.Files.AutoStart where

import Common
import Config.Files
import Utility.Tmp

{- Returns anything listed in the autostart file (which may not exist). -}
readAutoStartFile :: IO [OsPath]
readAutoStartFile = do
	f <- autoStartFile
	filter valid . nub . map (dropTrailingPathSeparator . toOsPath) . lines
		<$> catchDefaultIO "" (readFileString f)
  where
	-- Ignore any relative paths; some old buggy versions added eg "."
	valid = isAbsolute

modifyAutoStartFile :: ([OsPath] -> [OsPath]) -> IO ()
modifyAutoStartFile func = do
	dirs <- readAutoStartFile
	let dirs' = nubBy equalFilePath $ func dirs
	when (dirs' /= dirs) $ do
		f <- autoStartFile
		createDirectoryIfMissing True (parentDir f)
		viaTmp writeFileString f
			(unlines (map fromOsPath dirs'))

{- Adds a directory to the autostart file. If the directory is already
 - present, it's moved to the top, so it will be used as the default
 - when opening the webapp. -}
addAutoStartFile :: OsPath -> IO ()
addAutoStartFile path = do
	path' <- absPath path
	modifyAutoStartFile $ (:) path'

{- Removes a directory from the autostart file. -}
removeAutoStartFile :: OsPath -> IO ()
removeAutoStartFile path = do
	path' <- absPath path
	modifyAutoStartFile $
		filter (not . equalFilePath path')