I also renamed the functions with the correct ‘guestfs_int_*’ namespace. The corresponding change is made for OCaml C_utils and Visit. No functional change, just code motion. --- cat/ls.c | 23 ++++--- common/mlutils/c_utils-c.c | 32 +++++++++ common/mlutils/c_utils.ml | 10 +++ common/mlutils/c_utils.mli | 19 ++++++ common/mlvisit/Makefile.am | 6 +- common/mlvisit/visit-c.c | 32 --------- common/mlvisit/visit.ml | 11 --- common/mlvisit/visit.mli | 19 ------ common/mlvisit/visit_tests.ml | 1 + common/utils/guestfs-utils.h | 8 +++ common/utils/utils.c | 109 ++++++++++++++++++++++++++++++ common/visit/visit.c | 82 ++-------------------- common/visit/visit.h | 10 --- diff/diff.c | 36 ++++++---- sysprep/sysprep_operation_backup_files.ml | 1 + 15 files changed, 225 insertions(+), 174 deletions(-) diff --git a/cat/ls.c b/cat/ls.c index d71b800f0..9f0641892 100644 --- a/cat/ls.c +++ b/cat/ls.c @@ -47,6 +47,7 @@ #include "options.h" #include "display-options.h" +#include "guestfs-utils.h" #include "visit.h" /* Currently open libguestfs handle. */ @@ -481,19 +482,19 @@ show_file (const char *dir, const char *name, /* Display the basic fields. */ output_start_line (); - if (is_reg (stat->st_mode)) + if (guestfs_int_is_reg (stat->st_mode)) filetype = "-"; - else if (is_dir (stat->st_mode)) + else if (guestfs_int_is_dir (stat->st_mode)) filetype = "d"; - else if (is_chr (stat->st_mode)) + else if (guestfs_int_is_chr (stat->st_mode)) filetype = "c"; - else if (is_blk (stat->st_mode)) + else if (guestfs_int_is_blk (stat->st_mode)) filetype = "b"; - else if (is_fifo (stat->st_mode)) + else if (guestfs_int_is_fifo (stat->st_mode)) filetype = "p"; - else if (is_lnk (stat->st_mode)) + else if (guestfs_int_is_lnk (stat->st_mode)) filetype = "l"; - else if (is_sock (stat->st_mode)) + else if (guestfs_int_is_sock (stat->st_mode)) filetype = "s"; else filetype = "u"; @@ -527,10 +528,12 @@ show_file (const char *dir, const char *name, output_xattrs (xattrs); */ - path = full_path (dir, name); + path = guestfs_int_full_path (dir, name); + if (!path) + error (EXIT_FAILURE, errno, "guestfs_int_full_path"); if (checksum) { - if (is_reg (stat->st_mode)) { + if (guestfs_int_is_reg (stat->st_mode)) { csum = guestfs_checksum (g, checksum, path); if (!csum) exit (EXIT_FAILURE); @@ -542,7 +545,7 @@ show_file (const char *dir, const char *name, output_string (path); - if (is_lnk (stat->st_mode)) + if (guestfs_int_is_lnk (stat->st_mode)) /* XXX Fix this for NTFS. */ link = guestfs_readlink (g, path); if (link) diff --git a/common/mlutils/c_utils-c.c b/common/mlutils/c_utils-c.c index 50841de42..2e8aeff9b 100644 --- a/common/mlutils/c_utils-c.c +++ b/common/mlutils/c_utils-c.c @@ -75,3 +75,35 @@ guestfs_int_mlutils_shell_unquote (value strv) free (ret); CAMLreturn (retv); } + +#define is(t) \ + value \ + guestfs_int_mlutils_is_##t (value iv) \ + { \ + return Val_bool (guestfs_int_is_##t (Int64_val (iv))); \ + } +is(reg) +is(dir) +is(chr) +is(blk) +is(fifo) +is(lnk) +is(sock) + +value +guestfs_int_mlutils_full_path (value dirv, value namev) +{ + CAMLparam2 (dirv, namev); + CAMLlocal1 (rv); + const char *name = NULL; + char *ret; + + if (namev != Val_int (0)) + name = String_val (Field (namev, 0)); + + ret = guestfs_int_full_path (String_val (dirv), name); + rv = caml_copy_string (ret); + free (ret); + + CAMLreturn (rv); +} diff --git a/common/mlutils/c_utils.ml b/common/mlutils/c_utils.ml index e4263962d..00a6fff4e 100644 --- a/common/mlutils/c_utils.ml +++ b/common/mlutils/c_utils.ml @@ -24,3 +24,13 @@ external drive_name : int -> string = "guestfs_int_mlutils_drive_name" external drive_index : string -> int = "guestfs_int_mlutils_drive_index" external shell_unquote : string -> string = "guestfs_int_mlutils_shell_unquote" + +external is_reg : int64 -> bool = "guestfs_int_mlutils_is_reg" "noalloc" +external is_dir : int64 -> bool = "guestfs_int_mlutils_is_dir" "noalloc" +external is_chr : int64 -> bool = "guestfs_int_mlutils_is_chr" "noalloc" +external is_blk : int64 -> bool = "guestfs_int_mlutils_is_blk" "noalloc" +external is_fifo : int64 -> bool = "guestfs_int_mlutils_is_fifo" "noalloc" +external is_lnk : int64 -> bool = "guestfs_int_mlutils_is_lnk" "noalloc" +external is_sock : int64 -> bool = "guestfs_int_mlutils_is_sock" "noalloc" + +external full_path : string -> string option -> string = "guestfs_int_mlutils_full_path" diff --git a/common/mlutils/c_utils.mli b/common/mlutils/c_utils.mli index 7824f9658..68edc47a9 100644 --- a/common/mlutils/c_utils.mli +++ b/common/mlutils/c_utils.mli @@ -28,3 +28,22 @@ val shell_unquote : string -> string This is just intended to deal with quoting in configuration files (like ones under /etc/sysconfig), and it doesn't deal with some situations such as $variable interpolation. *) + +val is_reg : int64 -> bool +(** Returns true if [G.statns.st_mode] represents a regular file. *) +val is_dir : int64 -> bool +(** Returns true if [G.statns.st_mode] represents a directory. *) +val is_chr : int64 -> bool +(** Returns true if [G.statns.st_mode] represents a character device. *) +val is_blk : int64 -> bool +(** Returns true if [G.statns.st_mode] represents a block device. *) +val is_fifo : int64 -> bool +(** Returns true if [G.statns.st_mode] represents a FIFO. *) +val is_lnk : int64 -> bool +(** Returns true if [G.statns.st_mode] represents a symbolic link. *) +val is_sock : int64 -> bool +(** Returns true if [G.statns.st_mode] represents a Unix domain socket. *) + +val full_path : string -> string option -> string +(** This can be called with the [dir] and [name] parameters from + [visitor_function] to return the full canonical path. *) diff --git a/common/mlvisit/Makefile.am b/common/mlvisit/Makefile.am index fcf3bc0be..6466efbe0 100644 --- a/common/mlvisit/Makefile.am +++ b/common/mlvisit/Makefile.am @@ -70,6 +70,7 @@ XOBJECTS = $(BOBJECTS:.cmo=.cmx) # installed copy of libguestfs. OCAMLPACKAGES = \ -package str,unix \ + -I $(top_builddir)/common/mlutils \ -I $(top_builddir)/lib/.libs \ -I $(top_builddir)/gnulib/lib/.libs \ -I $(top_builddir)/ocaml \ @@ -108,10 +109,13 @@ visit_tests_THEOBJECTS = $(visit_tests_XOBJECTS) visit_tests.cmx: OCAMLPACKAGES += $(OCAMLPACKAGES_TESTS) endif -OCAMLLINKFLAGS = mlguestfs.$(MLARCHIVE) $(LINK_CUSTOM_OCAMLC_ONLY) +OCAMLLINKFLAGS = \ + mlcutils.$(MLARCHIVE) \ + mlguestfs.$(MLARCHIVE) $(LINK_CUSTOM_OCAMLC_ONLY) visit_tests_DEPENDENCIES = \ $(visit_tests_THEOBJECTS) \ + ../mlutils/mlcutils.$(MLARCHIVE) \ $(MLVISIT_CMA) \ $(top_srcdir)/ocaml-link.sh visit_tests_LINK = \ diff --git a/common/mlvisit/visit-c.c b/common/mlvisit/visit-c.c index 4cd1c6cea..fcd0428f7 100644 --- a/common/mlvisit/visit-c.c +++ b/common/mlvisit/visit-c.c @@ -133,38 +133,6 @@ visitor_function_wrapper (const char *dir, CAMLreturnT (int, 0); } -value -guestfs_int_mllib_full_path (value dirv, value namev) -{ - CAMLparam2 (dirv, namev); - CAMLlocal1 (rv); - const char *name = NULL; - char *ret; - - if (namev != Val_int (0)) - name = String_val (Field (namev, 0)); - - ret = full_path (String_val (dirv), name); - rv = caml_copy_string (ret); - free (ret); - - CAMLreturn (rv); -} - -#define is(t) \ - value \ - guestfs_int_mllib_is_##t (value iv) \ - { \ - return Val_bool (is_##t (Int64_val (iv))); \ - } -is(reg) -is(dir) -is(chr) -is(blk) -is(fifo) -is(lnk) -is(sock) - /* The functions below are copied from ocaml/guestfs-c-actions.c. */ static value diff --git a/common/mlvisit/visit.ml b/common/mlvisit/visit.ml index c979e3e9e..da2e122ed 100644 --- a/common/mlvisit/visit.ml +++ b/common/mlvisit/visit.ml @@ -23,14 +23,3 @@ external c_visit : int64 -> string -> visitor_function -> unit = let visit g dir f = c_visit (Guestfs.c_pointer g) dir f - -external full_path : string -> string option -> string = - "guestfs_int_mllib_full_path" - -external is_reg : int64 -> bool = "guestfs_int_mllib_is_reg" "noalloc" -external is_dir : int64 -> bool = "guestfs_int_mllib_is_dir" "noalloc" -external is_chr : int64 -> bool = "guestfs_int_mllib_is_chr" "noalloc" -external is_blk : int64 -> bool = "guestfs_int_mllib_is_blk" "noalloc" -external is_fifo : int64 -> bool = "guestfs_int_mllib_is_fifo" "noalloc" -external is_lnk : int64 -> bool = "guestfs_int_mllib_is_lnk" "noalloc" -external is_sock : int64 -> bool = "guestfs_int_mllib_is_sock" "noalloc" diff --git a/common/mlvisit/visit.mli b/common/mlvisit/visit.mli index 57ff85fa9..cba85785e 100644 --- a/common/mlvisit/visit.mli +++ b/common/mlvisit/visit.mli @@ -50,22 +50,3 @@ val visit : Guestfs.t -> string -> visitor_function -> unit If the visit function returns normally you can assume there was no error. *) - -val full_path : string -> string option -> string -(** This can be called with the [dir] and [name] parameters from - [visitor_function] to return the full canonical path. *) - -val is_reg : int64 -> bool -(** Returns true if [G.statns.st_mode] represents a regular file. *) -val is_dir : int64 -> bool -(** Returns true if [G.statns.st_mode] represents a directory. *) -val is_chr : int64 -> bool -(** Returns true if [G.statns.st_mode] represents a character device. *) -val is_blk : int64 -> bool -(** Returns true if [G.statns.st_mode] represents a block device. *) -val is_fifo : int64 -> bool -(** Returns true if [G.statns.st_mode] represents a FIFO. *) -val is_lnk : int64 -> bool -(** Returns true if [G.statns.st_mode] represents a symbolic link. *) -val is_sock : int64 -> bool -(** Returns true if [G.statns.st_mode] represents a Unix domain socket. *) diff --git a/common/mlvisit/visit_tests.ml b/common/mlvisit/visit_tests.ml index f5d2b57d3..7b0f01347 100644 --- a/common/mlvisit/visit_tests.ml +++ b/common/mlvisit/visit_tests.ml @@ -20,6 +20,7 @@ open Printf +open C_utils open Visit module G = Guestfs diff --git a/common/utils/guestfs-utils.h b/common/utils/guestfs-utils.h index 5758059ec..452611620 100644 --- a/common/utils/guestfs-utils.h +++ b/common/utils/guestfs-utils.h @@ -62,6 +62,14 @@ extern void guestfs_int_fadvise_noreuse (int fd); //extern void guestfs_int_fadvise_dontneed (int fd); //extern void guestfs_int_fadvise_willneed (int fd); extern char *guestfs_int_shell_unquote (const char *str); +extern int guestfs_int_is_reg (int64_t mode); +extern int guestfs_int_is_dir (int64_t mode); +extern int guestfs_int_is_chr (int64_t mode); +extern int guestfs_int_is_blk (int64_t mode); +extern int guestfs_int_is_fifo (int64_t mode); +extern int guestfs_int_is_lnk (int64_t mode); +extern int guestfs_int_is_sock (int64_t mode); +extern char *guestfs_int_full_path (const char *dir, const char *name); /* Not all language bindings know how to deal with Pointer arguments. * Those that don't will use this macro which complains noisily and diff --git a/common/utils/utils.c b/common/utils/utils.c index 58953e58a..105fd03cf 100644 --- a/common/utils/utils.c +++ b/common/utils/utils.c @@ -624,3 +624,112 @@ guestfs_int_shell_unquote (const char *str) return strdup (str); } + +/* In the libguestfs API, modes returned by lstat and friends are + * defined to contain Linux ABI values. However since the "current + * operating system" might not be Linux, we have to hard-code those + * numbers in the functions below. + */ + +/** + * Return true if the C<guestfs_statns> or C<guestfs_lstatns> + * C<st_mode> field represents a regular file. + */ +int +guestfs_int_is_reg (int64_t mode) +{ + return (mode & 0170000) == 0100000; +} + +/** + * Return true if the C<guestfs_statns> or C<guestfs_lstatns> + * C<st_mode> field represents a directory. + */ +int +guestfs_int_is_dir (int64_t mode) +{ + return (mode & 0170000) == 0040000; +} + +/** + * Return true if the C<guestfs_statns> or C<guestfs_lstatns> + * C<st_mode> field represents a char device. + */ +int +guestfs_int_is_chr (int64_t mode) +{ + return (mode & 0170000) == 0020000; +} + +/** + * Return true if the C<guestfs_statns> or C<guestfs_lstatns> + * C<st_mode> field represents a block device. + */ +int +guestfs_int_is_blk (int64_t mode) +{ + return (mode & 0170000) == 0060000; +} + +/** + * Return true if the C<guestfs_statns> or C<guestfs_lstatns> + * C<st_mode> field represents a named pipe (FIFO). + */ +int +guestfs_int_is_fifo (int64_t mode) +{ + return (mode & 0170000) == 0010000; +} + +/** + * Return true if the C<guestfs_statns> or C<guestfs_lstatns> + * C<st_mode> field represents a symbolic link. + */ +int +guestfs_int_is_lnk (int64_t mode) +{ + return (mode & 0170000) == 0120000; +} + +/** + * Return true if the C<guestfs_statns> or C<guestfs_lstatns> + * C<st_mode> field represents a Unix domain socket. + */ +int +guestfs_int_is_sock (int64_t mode) +{ + return (mode & 0170000) == 0140000; +} + +/** + * Concatenate C<dir> and C<name> to create a path. This correctly + * handles the case of concatenating C<"/" + "filename"> as well + * as C<"/dir" + "filename">. C<name> may be C<NULL>. + * + * The caller must free the returned path. + * + * This function sets C<errno> and returns C<NULL> on error. + */ +char * +guestfs_int_full_path (const char *dir, const char *name) +{ + int r; + char *path; + int len; + + len = strlen (dir); + if (len > 0 && dir[len - 1] == '/') + --len; + + if (STREQ (dir, "/")) + r = asprintf (&path, "/%s", name ? name : ""); + else if (name) + r = asprintf (&path, "%.*s/%s", len, dir, name); + else + r = asprintf (&path, "%.*s", len, dir); + + if (r == -1) + return NULL; + + return path; +} diff --git a/common/visit/visit.c b/common/visit/visit.c index 18380ab43..a7ee0f8fa 100644 --- a/common/visit/visit.c +++ b/common/visit/visit.c @@ -158,8 +158,12 @@ _visit (guestfs_h *g, int depth, const char *dir, return -1; /* Recursively call visit, but only on directories. */ - if (is_dir (stats->val[i].st_mode)) { - path = full_path (dir, names[i]); + if (guestfs_int_is_dir (stats->val[i].st_mode)) { + path = guestfs_int_full_path (dir, names[i]); + if (!path) { + perror ("guestfs_int_full_path"); + return -1; + } if (_visit (g, depth + 1, path, f, opaque) == -1) return -1; } @@ -167,77 +171,3 @@ _visit (guestfs_h *g, int depth, const char *dir, return 0; } - -char * -full_path (const char *dir, const char *name) -{ - int r; - char *path; - int len; - - len = strlen (dir); - if (len > 0 && dir[len - 1] == '/') - --len; - - if (STREQ (dir, "/")) - r = asprintf (&path, "/%s", name ? name : ""); - else if (name) - r = asprintf (&path, "%.*s/%s", len, dir, name); - else - r = asprintf (&path, "%.*s", len, dir); - - if (r == -1) { - perror ("asprintf"); - abort (); - } - - return path; -} - -/* In the libguestfs API, modes returned by lstat and friends are - * defined to contain Linux ABI values. However since the "current - * operating system" might not be Linux, we have to hard-code those - * numbers here. - */ -int -is_reg (int64_t mode) -{ - return (mode & 0170000) == 0100000; -} - -int -is_dir (int64_t mode) -{ - return (mode & 0170000) == 0040000; -} - -int -is_chr (int64_t mode) -{ - return (mode & 0170000) == 0020000; -} - -int -is_blk (int64_t mode) -{ - return (mode & 0170000) == 0060000; -} - -int -is_fifo (int64_t mode) -{ - return (mode & 0170000) == 0010000; -} - -/* symbolic link */ -int -is_lnk (int64_t mode) -{ - return (mode & 0170000) == 0120000; -} - -int -is_sock (int64_t mode) -{ - return (mode & 0170000) == 0140000; -} diff --git a/common/visit/visit.h b/common/visit/visit.h index 3bf0524ff..c8de5adec 100644 --- a/common/visit/visit.h +++ b/common/visit/visit.h @@ -23,14 +23,4 @@ typedef int (*visitor_function) (const char *dir, const char *name, const struct extern int visit (guestfs_h *g, const char *dir, visitor_function f, void *opaque); -extern char *full_path (const char *dir, const char *name); - -extern int is_reg (int64_t mode); -extern int is_dir (int64_t mode); -extern int is_chr (int64_t mode); -extern int is_blk (int64_t mode); -extern int is_fifo (int64_t mode); -extern int is_lnk (int64_t mode); -extern int is_sock (int64_t mode); - #endif /* VISIT_H */ diff --git a/diff/diff.c b/diff/diff.c index ed02f84b7..5851a1c9c 100644 --- a/diff/diff.c +++ b/diff/diff.c @@ -49,6 +49,7 @@ #include "options.h" #include "display-options.h" +#include "guestfs-utils.h" #include "visit.h" /* Internal tree structure built for each guest. */ @@ -479,7 +480,11 @@ visit_entry (const char *dir, const char *name, struct guestfs_xattr_list *xattrs = NULL; size_t i; - path = full_path (dir, name); + path = guestfs_int_full_path (dir, name); + if (!path) { + perror ("guestfs_int_full_path"); + goto error; + } /* Copy the stats and xattrs because the visit function will * free them after we return. @@ -491,7 +496,7 @@ visit_entry (const char *dir, const char *name, if (xattrs == NULL) goto error; - if (checksum && is_reg (stat->st_mode)) { + if (checksum && guestfs_int_is_reg (stat->st_mode)) { csum = guestfs_checksum (t->g, checksum, path); if (!csum) goto error; @@ -504,13 +509,13 @@ visit_entry (const char *dir, const char *name, /* If --dir-links option was NOT passed, flatten nlink field in * directories. */ - if (!dir_links && is_dir (stat->st_mode)) + if (!dir_links && guestfs_int_is_dir (stat->st_mode)) stat->st_nlink = 0; /* If --dir-times option was NOT passed, flatten time fields in * directories. */ - if (!dir_times && is_dir (stat->st_mode)) + if (!dir_times && guestfs_int_is_dir (stat->st_mode)) stat->st_atime_sec = stat->st_mtime_sec = stat->st_ctime_sec = stat->st_atime_nsec = stat->st_mtime_nsec = stat->st_ctime_nsec = 0; @@ -652,7 +657,8 @@ changed (guestfs_h *g1, struct file *file1, { /* Did file content change? */ if (cst != 0 || - (is_reg (file1->stat->st_mode) && is_reg (file2->stat->st_mode) && + (guestfs_int_is_reg (file1->stat->st_mode) && + guestfs_int_is_reg (file2->stat->st_mode) && (file1->stat->st_mtime_sec != file2->stat->st_mtime_sec || file1->stat->st_ctime_sec != file2->stat->st_ctime_sec || file1->stat->st_size != file2->stat->st_size))) { @@ -713,8 +719,8 @@ diff (struct file *file1, guestfs_h *g1, struct file *file2, guestfs_h *g2) CLEANUP_FREE char *tmpd, *tmpda = NULL, *tmpdb = NULL, *cmd = NULL; int r; - assert (is_reg (file1->stat->st_mode)); - assert (is_reg (file2->stat->st_mode)); + assert (guestfs_int_is_reg (file1->stat->st_mode)); + assert (guestfs_int_is_reg (file2->stat->st_mode)); if (asprintf (&tmpd, "%s/virtdiffXXXXXX", tmpdir) < 0) error (EXIT_FAILURE, errno, "asprintf"); @@ -759,19 +765,19 @@ output_file (guestfs_h *g, struct file *file) size_t i; CLEANUP_FREE char *link = NULL; - if (is_reg (file->stat->st_mode)) + if (guestfs_int_is_reg (file->stat->st_mode)) filetype = "-"; - else if (is_dir (file->stat->st_mode)) + else if (guestfs_int_is_dir (file->stat->st_mode)) filetype = "d"; - else if (is_chr (file->stat->st_mode)) + else if (guestfs_int_is_chr (file->stat->st_mode)) filetype = "c"; - else if (is_blk (file->stat->st_mode)) + else if (guestfs_int_is_blk (file->stat->st_mode)) filetype = "b"; - else if (is_fifo (file->stat->st_mode)) + else if (guestfs_int_is_fifo (file->stat->st_mode)) filetype = "p"; - else if (is_lnk (file->stat->st_mode)) + else if (guestfs_int_is_lnk (file->stat->st_mode)) filetype = "l"; - else if (is_sock (file->stat->st_mode)) + else if (guestfs_int_is_sock (file->stat->st_mode)) filetype = "s"; else filetype = "u"; @@ -807,7 +813,7 @@ output_file (guestfs_h *g, struct file *file) output_string (file->path); - if (is_lnk (file->stat->st_mode)) { + if (guestfs_int_is_lnk (file->stat->st_mode)) { /* XXX Fix this for NTFS. */ link = guestfs_readlink (g, file->path); if (link) diff --git a/sysprep/sysprep_operation_backup_files.ml b/sysprep/sysprep_operation_backup_files.ml index 64df8d758..a8b22e1b8 100644 --- a/sysprep/sysprep_operation_backup_files.ml +++ b/sysprep/sysprep_operation_backup_files.ml @@ -19,6 +19,7 @@ open Printf open Std_utils +open C_utils open Common_utils open Common_gettext.Gettext open Visit -- 2.13.2