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
)
91 lines
2.9 KiB
OCaml
91 lines
2.9 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
|
|
|
|
(* Yara APIs. *)
|
|
|
|
let non_daemon_functions = [
|
|
{ defaults with
|
|
name = "yara_scan"; added = (1, 37, 13);
|
|
style = RStructList ("detections", "yara_detection"), [String (Pathname, "path")], [];
|
|
optional = Some "libyara";
|
|
progress = true; cancellable = true;
|
|
shortdesc = "scan a file with the loaded yara rules";
|
|
longdesc = {|Scan a file with the previously loaded Yara rules.
|
|
|
|
For each matching rule, a C<yara_detection> structure is returned.
|
|
|
|
The C<yara_detection> structure contains the following fields.
|
|
|
|
=over 4
|
|
|
|
=item C<yara_name>
|
|
|
|
Path of the file matching a Yara rule.
|
|
|
|
=item C<yara_rule>
|
|
|
|
Identifier of the Yara rule which matched against the given file.
|
|
|
|
=back|} };
|
|
|
|
]
|
|
|
|
let daemon_functions = [
|
|
{ defaults with
|
|
name = "yara_load"; added = (1, 37, 13);
|
|
style = RErr, [String (FileIn, "filename")], [];
|
|
progress = true; cancellable = true;
|
|
optional = Some "libyara";
|
|
shortdesc = "load yara rules within libguestfs";
|
|
longdesc = {|Upload a set of Yara rules from local file F<filename>.
|
|
|
|
Yara rules allow to categorize files based on textual or binary patterns
|
|
within their content.
|
|
See C<guestfs_yara_scan> to see how to scan files with the loaded rules.
|
|
|
|
Rules can be in binary format, as when compiled with yarac command, or
|
|
in source code format. In the latter case, the rules will be first
|
|
compiled and then loaded.
|
|
|
|
Rules in source code format cannot include external files. In such cases,
|
|
it is recommended to compile them first.
|
|
|
|
Previously loaded rules will be destroyed.|} };
|
|
|
|
{ defaults with
|
|
name = "yara_destroy"; added = (1, 37, 13);
|
|
style = RErr, [], [];
|
|
optional = Some "libyara";
|
|
shortdesc = "destroy previously loaded yara rules";
|
|
longdesc = "\
|
|
Destroy previously loaded Yara rules in order to free libguestfs resources." };
|
|
|
|
{ defaults with
|
|
name = "internal_yara_scan"; added = (1, 37, 13);
|
|
style = RErr, [String (Pathname, "path"); String (FileOut, "filename")], [];
|
|
visibility = VInternal;
|
|
optional = Some "libyara";
|
|
shortdesc = "scan a file with the loaded yara rules";
|
|
longdesc = "Internal function for yara_scan." };
|
|
|
|
]
|