Advertisement
Guest User

Untitled

a guest
May 13th, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.88 KB | None | 0 0
  1. let logFileName = "vlog.txt"
  2. let hdd = @"\device\harddisk3\dr3"
  3.  
  4. let linesOfFile (fileName : string) = [
  5.     use sr = new StreamReader (fileName)
  6.     while not sr.EndOfStream do yield sr.ReadLine ()
  7. ]
  8.  
  9. let (|Match|_|) pattern input =
  10.     let m = Regex.Match(input, pattern) in
  11.     if m.Success then Some (List.tail [ for x in m.Groups -> x.Value ]) else None
  12.  
  13. let shellExecute program arguments =
  14.     let startInfo = new ProcessStartInfo ()
  15.     startInfo.FileName  <- program
  16.     startInfo.Arguments <- arguments
  17.  
  18.     startInfo.UseShellExecute <- false
  19.     startInfo.RedirectStandardOutput <- true
  20.  
  21.     let proc = new Process ()
  22.     proc.EnableRaisingEvents <- true
  23.  
  24.     let output_seq = new List<string> ()
  25.     proc.OutputDataReceived.AddHandler (fun sender args -> output_seq.Add args.Data)
  26.  
  27.     proc.StartInfo <- startInfo
  28.     proc.Start () |> ignore
  29.     proc.BeginOutputReadLine ()
  30.  
  31.     proc.WaitForExit()
  32.     (proc.ExitCode, output_seq)
  33.  
  34.  
  35. let listToDict (list : 'a list) =
  36.    let dict = new Dictionary<'a,int> ()
  37.     list |> List.iter (fun key -> if dict.ContainsKey key then dict.[key] <- dict.[key] + 1 else dict.Add (key, 1))
  38.     dict
  39.    
  40.  
  41. let getBlockOrNone = function
  42.     | Match ".*Block ([0-9]+) Error.*" [block] -> Some(int64 block)
  43.     | _ -> None
  44.  
  45. let getClusterByBlock block = block
  46.  
  47. let getFileNameByCluster hdd cluster =
  48.     let (exitCode, output) = shellExecute "nfi" (hdd + " " + (string cluster))
  49.     output.[10]
  50.  
  51. let printFileNamesFromDict dict =
  52.     for KeyValue(k, v) in dict do printfn "%s [%d bad blocks]" k v
  53.  
  54.  
  55. linesOfFile logFileName
  56. |> List.choose getBlockOrNone          // (ref:getBlocks)
  57. |> List.map getClusterByBlock          // (ref:getClusters)
  58. |> List.map (getFileNameByCluster hdd) // (ref:getFileNames)
  59. |> listToDict                          // (ref:unifyFileNames)
  60. |> printFileNamesFromDict              // (ref:print)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement