From 2837d42b8cf69fae42b825ee93762f29769ecaf7 Mon Sep 17 00:00:00 2001 From: Rob Hoes Date: Tue, 12 Oct 2010 12:15:54 +0100 Subject: [PATCH] CA-46289: PIF_metrics vendor and devices names read incorrectly PIF vendor and device names were listed as "unknown" in the PIF_metrics. This was due to a recent change in the Unixext.readfile_line function in combination with a slightly dubious way of jumping out of the read loop by raising End_of_file in pciutil.ml. It is now possible to raise Unixext.Break in the function passed to Unixext.readfile_line, to break out of the read loop (if you really want to). Signed-off-by: Rob Hoes --- pciutil/pciutil.ml | 2 +- stdext/unixext.ml | 4 +++- stdext/unixext.mli | 3 +++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/pciutil/pciutil.ml b/pciutil/pciutil.ml index f157da8..00d46c3 100644 --- a/pciutil/pciutil.ml +++ b/pciutil/pciutil.ml @@ -41,7 +41,7 @@ let parse_from file vendor device = if xdevice = device then ( device_str := Some (String.sub line 7 (String.length line - 7)); (* abort reading, we found what we want *) - raise End_of_file + raise Unixext.Break ) ) ) else ( diff --git a/stdext/unixext.ml b/stdext/unixext.ml index 3ac6864..14a266e 100644 --- a/stdext/unixext.ml +++ b/stdext/unixext.ml @@ -85,6 +85,8 @@ let daemonize () = end | _ -> exit 0 +exception Break + let file_lines_fold f start file_path = let input = open_in file_path in let rec fold accumulator = @@ -92,7 +94,7 @@ let file_lines_fold f start file_path = try Some (input_line input) with End_of_file -> None in match line with - | Some line -> fold (f accumulator line) + | Some line -> (try fold (f accumulator line) with Break -> accumulator) | None -> accumulator in finally (fun () -> fold start) diff --git a/stdext/unixext.mli b/stdext/unixext.mli index f26d545..9664fc9 100644 --- a/stdext/unixext.mli +++ b/stdext/unixext.mli @@ -23,6 +23,9 @@ val daemonize : unit -> unit val with_file : string -> Unix.open_flag list -> Unix.file_perm -> (Unix.file_descr -> 'a) -> 'a val with_directory : string -> (Unix.dir_handle -> 'a) -> 'a +(** Exception to be raised in function to break out of [file_lines_fold]. *) +exception Break + (** Folds function [f] over every line in the file at [file_path] using the starting value [start]. *) val file_lines_fold : ('a -> string -> 'a) -> 'a -> string -> 'a -- 2.39.5