summaryrefslogtreecommitdiffhomepage
path: root/Storage/Local.hs
blob: bbc3c41ec632303032971904d4ac35b20dd7f11e (plain)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
{- Copyright 2016 Joey Hess <id@joeyh.name>
 -
 - Licensed under the GNU AGPL version 3 or higher.
 -}

module Storage.Local (localStorage, uploadQueue) where

import Types
import Types.Storage
import Storage.Network (Server(..))
import Serialization ()
import qualified Data.ByteString as B
import qualified Data.ByteString.UTF8 as U8
import Data.Monoid
import Data.List
import System.Posix.User
import System.IO
import System.Directory
import System.Posix
import System.FilePath
import Raaz.Core.Encode
import Control.DeepSeq
import Control.Exception
import Control.Monad

newtype Section = Section String

localStorage :: String -> Storage
localStorage n = Storage
	{ storeShard = store section
	, retrieveShard = retrieve section
	, obscureShards = obscure section
	, countShards = count section
	, moveShards = move section
	}
  where
	section = Section n

uploadQueue :: Server -> Storage
uploadQueue s = localStorage ("uploadqueue" </> serverName s)

store :: Section -> StorableObjectIdent -> Shard -> IO StoreResult
store section i s = onError (StoreFailure . show) $ do
	dir <- shardDir section
	createDirectoryIfMissing True dir
	let dest = dir </> shardFile i
	exists <- doesFileExist dest
	if exists
		then return StoreAlreadyExists
		else do
			let tmp = dest ++ ".tmp"
			fd <- openFd tmp WriteOnly (Just 0o400)
				(defaultFileFlags { exclusive = True } )
			h <- fdToHandle fd
			B.hPut h (toByteString s)
			hClose h
			renameFile tmp dest
			return StoreSuccess

retrieve :: Section -> ShardNum -> StorableObjectIdent -> IO RetrieveResult
retrieve section n i = onError (RetrieveFailure . show) $ do
	dir <- shardDir section
	fd <- openFd (dir </> shardFile i) ReadOnly Nothing defaultFileFlags
	h <- fdToHandle fd
	b <- B.hGetContents h
	b `deepseq` hClose h
	return $ RetrieveSuccess $ Shard n (StorableObject b)

-- | Set atime and mtime to epoch, to obscure access and modification
-- patterns.
--
-- There is no way to set the ctime to the epoch, but setting the other
-- times does at least set it to the current time, which makes all
-- currently stored files look alike.
--
-- Note that the contents of shards is never changed, so it's ok to set the
-- mtime to the epoch; backup programs won't be confused.
obscure :: Section -> IO ObscureResult
obscure section = onError (ObscureFailure . show) $ do
	dir <- shardDir section
	fs <- filter isShardFile <$> getDirectoryContents dir
	mapM_ (\f -> setFileTimes (dir </> f) 0 0) fs
	return ObscureSuccess

count :: Section -> IO CountResult
count section = onError (CountFailure . show) $ do
	dir <- shardDir section
	CountResult . genericLength . filter isShardFile
		<$> getDirectoryContents dir

move :: Section -> Storage -> IO ()
move section storage = do
	dir <- shardDir section
	fs <- getDirectoryContents dir
	forM_ fs $ \f -> case fromShardFile f of
		Nothing -> return ()
		Just i -> do
			-- Use a dummy shard number of 0; it doesn't
			-- matter because we're not going to be
			-- recombining the shard, just sending its contents
			-- on the the server.
			r <- retrieve section 0 i
			case r of
				RetrieveFailure _ -> return ()
				RetrieveSuccess shard -> do
					s <- storeShard storage i shard
					case s of
						StoreFailure _ -> return ()
						_ -> removeFile f

onError :: (IOException -> a) -> IO a -> IO a
onError f a = do
	v <- try a
	return $ case v of
		Left e -> f e
		Right r -> r

shardDir :: Section -> IO FilePath
shardDir (Section section) = do
	u <- getUserEntryForID =<< getEffectiveUserID
	return $ homeDirectory u </> dotdir </> section

shardFile :: StorableObjectIdent -> FilePath
shardFile i = U8.toString (toByteString i) <> ext

fromShardFile :: FilePath -> Maybe StorableObjectIdent
fromShardFile f
	| isShardFile f = fromByteString $ U8.fromString $ dropExtension f
	| otherwise = Nothing

isShardFile :: FilePath -> Bool
isShardFile f = ext `isSuffixOf` f

ext :: String
ext = ".keysafe"

dotdir :: FilePath
dotdir = ".keysafe" </> "objects"