<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">2016-04-04 13:09 GMT+03:00 Pino Toscano <span dir="ltr"><<a href="mailto:ptoscano@redhat.com" target="_blank">ptoscano@redhat.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
some of the comments for patch #3 apply also for this one, namely:<br>
- wrapping of commit message<br>
- indentation of forward declarations<br>
- usage of XDR deserialization from guestfs_protocol.h<br>
-<br>
<div><div class="h5"><br>
On Sunday 03 April 2016 16:30:49 Matteo Cafasso wrote:<br>
> The filesystem_walk command is the appliance's<br>
> counterpart of the daemon's<br>
> internal_filesystem_walk command.<br>
><br>
> It writes the daemon's command output<br>
> on a temporary file and parses it, deserialising<br>
> the XDR formatted tsk_dirent structs.<br>
><br>
> It returns to the caller the list<br>
> of tsk_dirent structs generated by the<br>
> internal_filesystem_walk command.<br>
><br>
> Signed-off-by: Matteo Cafasso <<a href="mailto:noxdafox@gmail.com">noxdafox@gmail.com</a>><br>
> ---<br>
>  generator/<a href="http://actions.ml" rel="noreferrer" target="_blank">actions.ml</a> |  69 ++++++++++++++++++++++<br>
>  src/Makefile.am      |   1 +<br>
>  src/tsk.c            | 162 +++++++++++++++++++++++++++++++++++++++++++++++++++<br>
>  3 files changed, 232 insertions(+)<br>
>  create mode 100644 src/tsk.c<br>
><br>
> diff --git a/generator/<a href="http://actions.ml" rel="noreferrer" target="_blank">actions.ml</a> b/generator/<a href="http://actions.ml" rel="noreferrer" target="_blank">actions.ml</a><br>
> index 449ffa0..9457c3f 100644<br>
> --- a/generator/<a href="http://actions.ml" rel="noreferrer" target="_blank">actions.ml</a><br>
> +++ b/generator/<a href="http://actions.ml" rel="noreferrer" target="_blank">actions.ml</a><br>
> @@ -3546,6 +3546,75 @@ The environment variable C<XDG_RUNTIME_DIR> controls the default<br>
>  value: If C<XDG_RUNTIME_DIR> is set, then that is the default.<br>
>  Else F</tmp> is the default." };<br>
><br>
> +  { defaults with<br>
> +    name = "filesystem_walk"; added = (1, 33, 17);<br>
> +    style = RStructList ("dirents", "tsk_dirent"), [Mountable "device";], [];<br>
> +    optional = Some "libtsk";<br>
> +    progress = true; cancellable = true;<br>
> +    shortdesc = "walk through the filesystem content";<br>
> +    longdesc = "\<br>
> +Walk through the internal structures of a disk partition<br>
> +(eg. F</dev/sda1>) in order to return a list of all the files<br>
> +and directories stored within.<br>
> +<br>
> +It is not necessary to mount the disk partition to run this command.<br>
> +<br>
> +All entries in the filesystem are returned, excluding C<.> and<br>
> +C<..>. This function can list deleted or unaccessible files.<br>
> +The entries are I<not> sorted.<br>
> +<br>
> +If the entry is not allocated (ex: it has been deleted),<br>
> +its inode, type or size might not be recovered correctly.<br>
> +In such case, the inode and the size will be 0 while the type<br>
> +will be unidentified 'u'.<br>
<br>
</div></div>As said for patch #3, unknown sizes should be -1 and not 0.<br>
<div><div class="h5"><br>
> +This call returns as well basic file type information about each<br>
> +file.  The C<tsk_type> field will contain one of the following characters:<br>
> +<br>
> +=over 4<br>
> +<br>
> +=item 'b'<br>
> +<br>
> +Block special<br>
> +<br>
> +=item 'c'<br>
> +<br>
> +Char special<br>
> +<br>
> +=item 'd'<br>
> +<br>
> +Directory<br>
> +<br>
> +=item 'f'<br>
> +<br>
> +FIFO (named pipe)<br>
> +<br>
> +=item 'l'<br>
> +<br>
> +Symbolic link<br>
> +<br>
> +=item 'r'<br>
> +<br>
> +Regular file<br>
> +<br>
> +=item 's'<br>
> +<br>
> +Socket<br>
> +<br>
> +=item 'h'<br>
> +<br>
> +Shadow inode (Solaris)<br>
> +<br>
> +=item 'w'<br>
> +<br>
> +Whiteout inode (BSD)<br>
> +<br>
> +=item 'u'<br>
> +<br>
> +Unknown file type<br>
> +<br>
> +=back" };<br>
> +<br>
>  ]<br>
><br>
>  (* daemon_functions are any functions which cause some action<br>
> diff --git a/src/Makefile.am b/src/Makefile.am<br>
> index 3b4cd10..9f8af4c 100644<br>
> --- a/src/Makefile.am<br>
> +++ b/src/Makefile.am<br>
> @@ -130,6 +130,7 @@ libguestfs_la_SOURCES = \<br>
>       structs-copy.c \<br>
>       structs-free.c \<br>
>       tmpdirs.c \<br>
> +     tsk.c \<br>
>       whole-file.c \<br>
>       libguestfs.syms<br>
><br>
> diff --git a/src/tsk.c b/src/tsk.c<br>
> new file mode 100644<br>
> index 0000000..d77bc0a<br>
> --- /dev/null<br>
> +++ b/src/tsk.c<br>
> @@ -0,0 +1,162 @@<br>
> +/* libguestfs<br>
> + * Copyright (C) 2016 Red Hat Inc.<br>
> + *<br>
> + * This library is free software; you can redistribute it and/or<br>
> + * modify it under the terms of the GNU Lesser General Public<br>
> + * License as published by the Free Software Foundation; either<br>
> + * version 2 of the License, or (at your option) any later version.<br>
> + *<br>
> + * This library is distributed in the hope that it will be useful,<br>
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of<br>
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU<br>
> + * Lesser General Public License for more details.<br>
> + *<br>
> + * You should have received a copy of the GNU Lesser General Public<br>
> + * License along with this library; if not, write to the Free Software<br>
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA<br>
> + */<br>
> +<br>
> +#include <config.h><br>
> +<br>
> +#include <stdio.h><br>
> +#include <stdlib.h><br>
> +#include <fcntl.h><br>
> +#include <unistd.h><br>
> +#include <sys/types.h><br>
> +#include <sys/stat.h><br>
> +#include <string.h><br>
> +#include <rpc/xdr.h><br>
> +#include <rpc/types.h><br>
> +<br>
> +#include "full-read.h"<br>
> +<br>
> +#include "guestfs.h"<br>
> +#include "guestfs_protocol.h"<br>
> +#include "guestfs-internal.h"<br>
> +#include "guestfs-internal-all.h"<br>
> +#include "guestfs-internal-actions.h"<br>
> +<br>
> +static struct guestfs_tsk_dirent_list *parse_filesystem_walk<br>
> +(guestfs_h *g, char *buf, size_t bufsize);<br>
> +int deserialise_dirent_list (guestfs_h *g, char *buf, size_t bufsize,<br>
> +                             struct guestfs_tsk_dirent_list **dirents);<br>
> +static int deserialise_dirent (guestfs_h *g, XDR *xdrs,<br>
> +                               struct guestfs_tsk_dirent *dirent);<br>
> +<br>
> +struct guestfs_tsk_dirent_list *<br>
> +guestfs_impl_filesystem_walk (guestfs_h *g, const char *mountable)<br>
> +{<br>
> +  int ret = 0;<br>
> +  size_t size = 0;<br>
> +  CLEANUP_FREE char *buf = NULL;<br>
> +  CLEANUP_UNLINK_FREE char *tmpfile = NULL;<br>
> +<br>
> +  ret = guestfs_int_lazy_make_tmpdir (g);<br>
> +  if (ret < 0)<br>
> +    return NULL;<br>
> +<br>
> +  tmpfile = safe_asprintf (g, "%s/filesystem_walk%d", g->tmpdir, ++g->unique);<br>
> +<br>
> +  ret = guestfs_internal_filesystem_walk (g, mountable, tmpfile);<br>
> +  if (ret < 0)<br>
> +    return NULL;<br>
> +<br>
> +  ret = guestfs_int_read_whole_file (g, tmpfile, &buf, &size);<br>
> +  if (ret < 0)<br>
> +    return NULL;<br>
> +<br>
> +  return parse_filesystem_walk (g, buf, size);  /* caller frees */<br>
> +}<br>
> +<br>
> +/* Parse buf content and return dirents list.<br>
> + * Return a list of tsk_dirent on success, NULL on error.<br>
> + */<br>
> +static struct guestfs_tsk_dirent_list *<br>
> +parse_filesystem_walk (guestfs_h *g, char *buf, size_t bufsize)<br>
> +{<br>
> +  int ret = 0;<br>
> +  struct guestfs_tsk_dirent_list *dirents = NULL;<br>
> +<br>
> +  /* Initialise results array. */<br>
> +  dirents = safe_malloc (g, sizeof (struct guestfs_tsk_dirent_list));<br>
> +  dirents->len = 8;<br>
> +  dirents->val = safe_malloc (g, dirents->len *<br>
> +                              sizeof (struct guestfs_tsk_dirent));<br>
<br>
</div></div>sizeof (*dirents->val) (and below too) can make the code slightly<br>
shorter (just an hint).<br>
<span class=""><br>
> +<br>
> +  /* Deserialise buffer into dirent list. */<br>
> +  ret = deserialise_dirent_list (g, buf, bufsize, &dirents);<br>
> +  if (ret < 0) {<br>
> +    guestfs_free_tsk_dirent_list (dirents);<br>
> +    return NULL;<br>
> +  }<br>
> +<br>
> +  /* Resize the array to correct number of entries. */<br>
> +  dirents->len = ret;<br>
> +  dirents->val = safe_realloc (g, dirents->val,<br>
> +                               dirents->len *<br>
> +                               sizeof (struct guestfs_tsk_dirent));<br>
<br>
</span>You don't need to shrink the array with values -- users knows already<br>
they don't have to access it past 'len' values.<br></blockquote><div><br></div><div>If I then run the command in guestfish I get plenty of empty structs.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<span class=""><br>
> +<br>
> +  return dirents;<br>
> +}<br>
> +<br>
> +/* Deserialise buf content and populate the dirent list.<br>
> + * Return the number of deserialised dirents, -1 on error.<br>
> + */<br>
> +int<br>
> +deserialise_dirent_list (guestfs_h *g, char *buf, size_t bufsize,<br>
> +                         struct guestfs_tsk_dirent_list **dirents)<br>
<br>
</span>Since you don't change the location of 'dirents', you don't need to<br>
pass a pointer to it to change its values.<br></blockquote><div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Thanks,<br>
<span class="HOEnZb"><font color="#888888">--<br>
Pino Toscano</font></span><br>_______________________________________________<br>
Libguestfs mailing list<br>
<a href="mailto:Libguestfs@redhat.com">Libguestfs@redhat.com</a><br>
<a href="https://www.redhat.com/mailman/listinfo/libguestfs" rel="noreferrer" target="_blank">https://www.redhat.com/mailman/listinfo/libguestfs</a><br></blockquote></div><br></div></div>