mirror of
https://github.com/libguestfs/libguestfs.git
synced 2026-03-21 22:53:37 +00:00
This change was done almost entirely automatically using the script
below. This uses the OCaml lexer to read the source files and extract
the strings and locations. Strings which are "candidates" (in this
case, longer than 3 lines) are replaced in the output with quoted
string literals.
Since the OCaml lexer is used, it already substitutes all escape
sequences correctly. I diffed the output of the generator and it is
identical after this change, except for UUIDs, which change because of
how Utils.stable_uuid is implemented.
Thanks: Nicolas Ojeda Bar
$ ocamlfind opt -package unix,compiler-libs.common find_strings.ml \
-o find_strings.opt -linkpkg
$ for f in $( git ls-files -- \*.ml ) ; do ./find_strings.opt $f ; done
open Printf
let read_whole_file path =
let buf = Buffer.create 16384 in
let chan = open_in path in
let maxlen = 16384 in
let b = Bytes.create maxlen in
let rec loop () =
let r = input chan b 0 maxlen in
if r > 0 then (
Buffer.add_substring buf (Bytes.to_string b) 0 r;
loop ()
)
in
loop ();
close_in chan;
Buffer.contents buf
let count_chars c str =
let count = ref 0 in
for i = 0 to String.length str - 1 do
if c = String.unsafe_get str i then incr count
done;
!count
let subs = ref []
let consider_string str loc =
let nr_lines = count_chars '\n' str in
if nr_lines > 3 then
subs := (str, loc) :: !subs
let () =
Lexer.init ();
let filename = Sys.argv.(1) in
let content = read_whole_file filename in
let lexbuf = Lexing.from_string content in
let rec loop () =
let token = Lexer.token lexbuf in
(match token with
| Parser.EOF -> ();
| STRING (s, loc, sopt) ->
consider_string s loc; (* sopt? *)
loop ();
| token ->
loop ();
)
in
loop ();
(* The list of subs is already reversed, which is convenient
* because we must the file substitutions in reverse order.
*)
let subs = !subs in
let new_content = ref content in
List.iter (
fun (str, loc) ->
let { Location.loc_start = { pos_cnum = p1 };
loc_end = { pos_cnum = p2 } } = loc in
let len = String.length !new_content in
let before = String.sub !new_content 0 (p1-1) in
let after = String.sub !new_content (p2+1) (len - p2 - 1) in
new_content := before ^ "{|" ^ str ^ "|}" ^ after
) subs;
let new_content = !new_content in
if content <> new_content then (
(* Update the file in place. *)
let new_filename = filename ^ ".new"
and backup_filename = filename ^ ".bak" in
let chan = open_out new_filename in
fprintf chan "%s" new_content;
close_out chan;
Unix.rename filename backup_filename;
Unix.rename new_filename filename
)
231 lines
8.6 KiB
OCaml
231 lines
8.6 KiB
OCaml
(* libguestfs
|
|
* Copyright (C) 2009-2025 Red Hat Inc.
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program; if not, write to the Free Software
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
*)
|
|
|
|
(* Please read generator/README first. *)
|
|
|
|
open Types
|
|
|
|
(* Hivex APIs. *)
|
|
|
|
let daemon_functions = [
|
|
{ defaults with
|
|
name = "hivex_open"; added = (1, 19, 35);
|
|
style = RErr, [String (Pathname, "filename")], [OBool "verbose"; OBool "debug"; OBool "write"; OBool "unsafe"];
|
|
optional = Some "hivex";
|
|
tests = [
|
|
InitScratchFS, Always, TestRun (
|
|
[["upload"; "$srcdir/../test-data/files/minimal"; "/hivex_open"];
|
|
["hivex_open"; "/hivex_open"; ""; ""; "false"; ""];
|
|
["hivex_root"]; (* in this hive, it returns 0x1020 *)
|
|
["hivex_node_name"; "0x1020"];
|
|
["hivex_node_children"; "0x1020"];
|
|
["hivex_node_values"; "0x1020"]]), [["hivex_close"]]
|
|
];
|
|
shortdesc = "open a Windows Registry hive file";
|
|
longdesc = {|Open the Windows Registry hive file named F<filename>.
|
|
If there was any previous hivex handle associated with this
|
|
guestfs session, then it is closed.
|
|
|
|
This is a wrapper around the L<hivex(3)> call of the same name.|} };
|
|
|
|
{ defaults with
|
|
name = "hivex_close"; added = (1, 19, 35);
|
|
style = RErr, [], [];
|
|
optional = Some "hivex";
|
|
shortdesc = "close the current hivex handle";
|
|
longdesc = "\
|
|
Close the current hivex handle.
|
|
|
|
This is a wrapper around the L<hivex(3)> call of the same name." };
|
|
|
|
{ defaults with
|
|
name = "hivex_root"; added = (1, 19, 35);
|
|
style = RInt64 "nodeh", [], [];
|
|
optional = Some "hivex";
|
|
shortdesc = "return the root node of the hive";
|
|
longdesc = "\
|
|
Return the root node of the hive.
|
|
|
|
This is a wrapper around the L<hivex(3)> call of the same name." };
|
|
|
|
{ defaults with
|
|
name = "hivex_node_name"; added = (1, 19, 35);
|
|
style = RString (RPlainString, "name"), [Int64 "nodeh"], [];
|
|
optional = Some "hivex";
|
|
shortdesc = "return the name of the node";
|
|
longdesc = "\
|
|
Return the name of C<nodeh>.
|
|
|
|
This is a wrapper around the L<hivex(3)> call of the same name." };
|
|
|
|
{ defaults with
|
|
name = "hivex_node_children"; added = (1, 19, 35);
|
|
style = RStructList ("nodehs", "hivex_node"), [Int64 "nodeh"], [];
|
|
optional = Some "hivex";
|
|
shortdesc = "return list of nodes which are subkeys of node";
|
|
longdesc = "\
|
|
Return the list of nodes which are subkeys of C<nodeh>.
|
|
|
|
This is a wrapper around the L<hivex(3)> call of the same name." };
|
|
|
|
{ defaults with
|
|
name = "hivex_node_get_child"; added = (1, 19, 35);
|
|
style = RInt64 "child", [Int64 "nodeh"; String (PlainString, "name")], [];
|
|
optional = Some "hivex";
|
|
shortdesc = "return the named child of node";
|
|
longdesc = "\
|
|
Return the child of C<nodeh> with the name C<name>, if it exists.
|
|
This can return C<0> meaning the name was not found.
|
|
|
|
This is a wrapper around the L<hivex(3)> call of the same name." };
|
|
|
|
{ defaults with
|
|
name = "hivex_node_parent"; added = (1, 19, 35);
|
|
style = RInt64 "parent", [Int64 "nodeh"], [];
|
|
optional = Some "hivex";
|
|
shortdesc = "return the parent of node";
|
|
longdesc = "\
|
|
Return the parent node of C<nodeh>.
|
|
|
|
This is a wrapper around the L<hivex(3)> call of the same name." };
|
|
|
|
{ defaults with
|
|
name = "hivex_node_values"; added = (1, 19, 35);
|
|
style = RStructList ("valuehs", "hivex_value"), [Int64 "nodeh"], [];
|
|
optional = Some "hivex";
|
|
shortdesc = "return list of values attached to node";
|
|
longdesc = "\
|
|
Return the array of (key, datatype, data) tuples attached to C<nodeh>.
|
|
|
|
This is a wrapper around the L<hivex(3)> call of the same name." };
|
|
|
|
{ defaults with
|
|
name = "hivex_node_get_value"; added = (1, 19, 35);
|
|
style = RInt64 "valueh", [Int64 "nodeh"; String (PlainString, "key")], [];
|
|
optional = Some "hivex";
|
|
shortdesc = "return the named value";
|
|
longdesc = {|Return the value attached to C<nodeh> which has the
|
|
name C<key>, if it exists. This can return C<0> meaning
|
|
the key was not found.
|
|
|
|
This is a wrapper around the L<hivex(3)> call of the same name.|} };
|
|
|
|
{ defaults with
|
|
name = "hivex_value_key"; added = (1, 19, 35);
|
|
style = RString (RPlainString, "key"), [Int64 "valueh"], [];
|
|
optional = Some "hivex";
|
|
shortdesc = "return the key field from the (key, datatype, data) tuple";
|
|
longdesc = "\
|
|
Return the key (name) field of a (key, datatype, data) tuple.
|
|
|
|
This is a wrapper around the L<hivex(3)> call of the same name." };
|
|
|
|
{ defaults with
|
|
name = "hivex_value_type"; added = (1, 19, 35);
|
|
style = RInt64 "datatype", [Int64 "valueh"], [];
|
|
optional = Some "hivex";
|
|
shortdesc = "return the data type from the (key, datatype, data) tuple";
|
|
longdesc = "\
|
|
Return the data type field from a (key, datatype, data) tuple.
|
|
|
|
This is a wrapper around the L<hivex(3)> call of the same name." };
|
|
|
|
{ defaults with
|
|
name = "hivex_value_value"; added = (1, 19, 35);
|
|
style = RBufferOut "databuf", [Int64 "valueh"], [];
|
|
optional = Some "hivex";
|
|
shortdesc = "return the data field from the (key, datatype, data) tuple";
|
|
longdesc = {|Return the data field of a (key, datatype, data) tuple.
|
|
|
|
This is a wrapper around the L<hivex(3)> call of the same name.
|
|
|
|
See also: C<guestfs_hivex_value_utf8>.|} };
|
|
|
|
{ defaults with
|
|
name = "hivex_value_string"; added = (1, 37, 22);
|
|
style = RString (RPlainString, "databuf"), [Int64 "valueh"], [];
|
|
optional = Some "hivex";
|
|
shortdesc = "return the data field as a UTF-8 string";
|
|
longdesc = {|This calls C<guestfs_hivex_value_value> (which returns the
|
|
data field from a hivex value tuple). It then assumes that
|
|
the field is a UTF-16LE string and converts the result to
|
|
UTF-8 (or if this is not possible, it returns an error).
|
|
|
|
This is useful for reading strings out of the Windows registry.
|
|
However it is not foolproof because the registry is not
|
|
strongly-typed and fields can contain arbitrary or unexpected
|
|
data.|} };
|
|
|
|
{ defaults with
|
|
name = "hivex_commit"; added = (1, 19, 35);
|
|
style = RErr, [OptString "filename"], [];
|
|
optional = Some "hivex";
|
|
tests = [
|
|
InitScratchFS, Always, TestRun (
|
|
[["upload"; "$srcdir/../test-data/files/minimal"; "/hivex_commit1"];
|
|
["hivex_open"; "/hivex_commit1"; ""; ""; "true"; ""];
|
|
["hivex_commit"; "NULL"]]), [["hivex_close"]];
|
|
InitScratchFS, Always, TestResultTrue (
|
|
[["upload"; "$srcdir/../test-data/files/minimal"; "/hivex_commit2"];
|
|
["hivex_open"; "/hivex_commit2"; ""; ""; "true"; ""];
|
|
["hivex_commit"; "/hivex_commit2_copy"];
|
|
["is_file"; "/hivex_commit2_copy"; "false"]]), [["hivex_close"]]
|
|
];
|
|
shortdesc = "commit (write) changes back to the hive";
|
|
longdesc = {|Commit (write) changes to the hive.
|
|
|
|
If the optional F<filename> parameter is null, then the changes
|
|
are written back to the same hive that was opened. If this is
|
|
not null then they are written to the alternate filename given
|
|
and the original hive is left untouched.
|
|
|
|
This is a wrapper around the L<hivex(3)> call of the same name.|} };
|
|
|
|
{ defaults with
|
|
name = "hivex_node_add_child"; added = (1, 19, 35);
|
|
style = RInt64 "nodeh", [Int64 "parent"; String (PlainString, "name")], [];
|
|
optional = Some "hivex";
|
|
shortdesc = "add a child node";
|
|
longdesc = "\
|
|
Add a child node to C<parent> named C<name>.
|
|
|
|
This is a wrapper around the L<hivex(3)> call of the same name." };
|
|
|
|
{ defaults with
|
|
name = "hivex_node_delete_child"; added = (1, 19, 35);
|
|
style = RErr, [Int64 "nodeh"], [];
|
|
optional = Some "hivex";
|
|
shortdesc = "delete a node (recursively)";
|
|
longdesc = "\
|
|
Delete C<nodeh>, recursively if necessary.
|
|
|
|
This is a wrapper around the L<hivex(3)> call of the same name." };
|
|
|
|
{ defaults with
|
|
name = "hivex_node_set_value"; added = (1, 19, 35);
|
|
style = RErr, [Int64 "nodeh"; String (PlainString, "key"); Int64 "t"; BufferIn "val"], [];
|
|
optional = Some "hivex";
|
|
shortdesc = "set or replace a single value in a node";
|
|
longdesc = "\
|
|
Set or replace a single value under the node C<nodeh>. The
|
|
C<key> is the name, C<t> is the type, and C<val> is the data.
|
|
|
|
This is a wrapper around the L<hivex(3)> call of the same name." };
|
|
|
|
]
|