File: Utils.hs

package info (click to toggle)
dfsbuild 0.99.3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 308 kB
  • ctags: 6
  • sloc: haskell: 815; sh: 197; makefile: 99
file content (113 lines) | stat: -rw-r--r-- 3,496 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
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
{- dfsbuilt Utilities
Copyright (c) 2006 John Goerzen
Please see COPYRIGHT for more details
-}

module Utils where
import System.Random
import MissingH.Logging.Logger
import System.Time
import Text.Printf
import Control.Exception
import System.Posix.Files
import MissingH.Str
import MissingH.List
import MissingH.Either
import MissingH.ConfigParser
import MissingH.Cmd
import System.IO.Unsafe
import System.IO.Error
import Text.Regex
import MissingH.Path.FilePath
import System.Directory(doesFileExist)

data DFSEnv = DFSEnv 
    {wdir :: String,
     libdir :: String,
     cp :: ConfigParser,
     isDebugging :: Bool,
     defaultArch :: String,
     targetdir :: String,
     marker :: String,
     datestr :: String}

data DFSState = Fresh | Initialized | Mirrored | Bootstrapped | Installed
              | LibsInstalled | DebsInstalled | CfgHandled | InitPrepped 
              | RDPrepped
              | KernelsInstalled | RamdiskBuilt | BootloaderInstalled
              deriving (Eq, Show, Read, Ord)

im = infoM "dfs"
wm = warningM "dfs"
dm = debugM "dfs"

getUniqueCDID :: IO String
getUniqueCDID = 
    do t <- getClockTime
       random1 <- randomIO
       random2 <- randomIO
       return $ printf "DFS CD IMAGE, format 2, ID: %d,%d,%d\n"
                ((\(TOD x _) -> x) t) (random1::Int) (random2::Int)

getDefaultArch = 
    do (ph, iarchstr) <- pipeFrom "dpkg" ["--print-architecture"]
       let archstr = (seqList (strip iarchstr))
       forceSuccess ph
       return archstr

eget :: DFSEnv -> String -> String
eget env opt = forceEither $ get (cp env) (defaultArch env) opt
esget env s o = forceEither $ get (cp env) s o

egetbool :: DFSEnv -> String -> Bool
egetbool env opt = forceEither $ get (cp env) (defaultArch env) opt

saveState :: DFSEnv -> DFSState -> IO ()
saveState env state =
    writeFile ((wdir env) ++ "/state") (show state)

getState :: DFSEnv -> IO DFSState
getState env =
    do st <- readFile ((wdir env) ++ "/state")
       return (read st)

getCodeName :: FilePath -> IO String
getCodeName fp =
    do c_old <- System.IO.Error.catch (readFile (fp ++ "Release"))
	       (\e -> if System.IO.Error.isDoesNotExistError e then return "" else ioError e)
       c_new <- System.IO.Error.catch (readFile (fp ++ "_dists_._Release"))
	       (\e -> if System.IO.Error.isDoesNotExistError e then return "" else ioError e)
       c <- if length(c_old) > 0 then return c_old else return c_new   
       let cr = mkRegex "Codename: ([a-z]+)"
       case matchRegex cr c of
         Just [cn] -> return cn
         x -> fail $ "Error finding Codename: " ++ show x

deleteit fn =
    do dm $ "Deleting: " ++ fn
       handle (\e -> wm ("Delete failed: " ++ show e)) 
              (removeLink fn)

getrdsize_kb env =
    do st <- getFileStatus $ targetdir env ++ "/boot/initrd.dfs"
       return $ ((fileSize st) `div` 1024) + 1

getrdparam env =
    do kb <- getrdsize_kb env
       return $ if kb < 4096
                   then " "
                   else " ramdisk_size=" ++ show kb ++ " "

getinitrdname env kernpath =
    do dfe <- doesFileExist (targetdir env ++ "/boot/" ++ rdname)
       if dfe
          then return rdname
          else return "initrd.dfs"
    where kname = snd . splitFileName $ kernpath
          rdname = subRegex (mkRegex "vmlinu.") kname "initrd.img"

getrootdevname env kernpath =
    do initrd <- getinitrdname env kernpath
       if initrd == "initrd.dfs"
          then return "/dev/ram0"
          else return "/dev/root"