Advertisement
Guest User

Untitled

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