"daemon: Added internal_filesystem_walk command"
-> "New API: internal_filesystem_walk"
On Tuesday 05 April 2016 18:47:30 Matteo Cafasso wrote:
The internal_filesystem_walk command walks through the FS structures
of a disk partition and returns all the files or directories
which could be found.
The command is able to retrieve information regarding deleted
or unaccessible files where other commands such as stat or find
would fail.
The gathered list of tsk_dirent structs is serialised into XDR format
and written to a file by the appliance.
Signed-off-by: Matteo Cafasso <noxdafox(a)gmail.com>
---
daemon/Makefile.am | 4 +-
daemon/tsk.c | 233 +++++++++++++++++++++++++++++++++++++++++++++++++++
generator/actions.ml | 9 ++
src/MAX_PROC_NR | 2 +-
4 files changed, 246 insertions(+), 2 deletions(-)
create mode 100644 daemon/tsk.c
diff --git a/daemon/Makefile.am b/daemon/Makefile.am
index beb7962..03bf71f 100644
--- a/daemon/Makefile.am
+++ b/daemon/Makefile.am
@@ -179,6 +179,7 @@ guestfsd_SOURCES = \
sync.c \
syslinux.c \
tar.c \
+ tsk.c \
truncate.c \
umask.c \
upload.c \
@@ -209,7 +210,8 @@ guestfsd_LDADD = \
$(LIB_CLOCK_GETTIME) \
$(LIBINTL) \
$(SERVENT_LIB) \
- $(PCRE_LIBS)
+ $(PCRE_LIBS) \
+ $(TSK_LIBS)
guestfsd_CPPFLAGS = \
-I$(top_srcdir)/gnulib/lib \
diff --git a/daemon/tsk.c b/daemon/tsk.c
new file mode 100644
index 0000000..cd4879b
--- /dev/null
+++ b/daemon/tsk.c
@@ -0,0 +1,233 @@
+/* libguestfs - the guestfsd daemon
+ * Copyright (C) 2016 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.
+ */
+
+#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <inttypes.h>
+#include <string.h>
+#include <unistd.h>
+#include <rpc/xdr.h>
+#include <rpc/types.h>
+
+#include "guestfs_protocol.h"
+#include "daemon.h"
+#include "actions.h"
+#include "optgroups.h"
+
+#ifdef HAVE_LIBTSK
+
+#include <tsk/libtsk.h>
+
+static int open_filesystem (const char *, TSK_IMG_INFO **, TSK_FS_INFO **);
+static TSK_WALK_RET_ENUM fswalk_callback (TSK_FS_FILE *, const char *, void *);
+static char file_type (TSK_FS_FILE *);
+static int send_dirent_info (guestfs_int_tsk_dirent *);
+static void reply_with_tsk_error (const char *);
+
+int
+do_internal_filesystem_walk (const mountable_t *mountable)
+{
+ int ret = -1;
+ TSK_FS_INFO *fs = NULL;
+ TSK_IMG_INFO *img = NULL; /* Used internally by tsk_fs_dir_walk */
+ int flags = TSK_FS_DIR_WALK_FLAG_ALLOC | TSK_FS_DIR_WALK_FLAG_UNALLOC |
+ TSK_FS_DIR_WALK_FLAG_RECURSE | TSK_FS_DIR_WALK_FLAG_NOORPHAN;
+
+ ret = open_filesystem (mountable->device, &img, &fs);
+ if (ret < 0)
+ return ret;
+
+ reply (NULL, NULL); /* Reply message. */
+
+ ret = tsk_fs_dir_walk (fs, fs->root_inum, flags, fswalk_callback, NULL);
+ if (ret == 0)
+ ret = send_file_end (0); /* File transfer end. */
+ else
+ send_file_end (1); /* Cancel file transfer. */
+
+ fs->close (fs);
+ img->close (img);
+
+ return ret;
+}
+
+/* Inspect the device and initialises the img and fs structures.
+ * Return 0 on success, -1 on error.
+ */
+static int
+open_filesystem (const char *device, TSK_IMG_INFO **img, TSK_FS_INFO **fs)
+{
+ const char *images[] = { device };
+
+ *img = tsk_img_open (1, images, TSK_IMG_TYPE_DETECT , 0);
Extra space here ... ^
+ if (*img == NULL) {
+ reply_with_tsk_error ("tsk_image_open");
+ return -1;
+ }
+
+ *fs = tsk_fs_open_img (*img, 0, TSK_FS_TYPE_DETECT);
+ if (*fs == NULL) {
+ reply_with_tsk_error ("tsk_fs_open_img");
+ (*img)->close (*img);
+ return -1;
+ }
+
+ return 0;
+}
+
+/* Filesystem walk callback, it gets called on every FS node.
+ * Parse the node, encode it into an XDR structure and send it to the appliance.
+ * Return TSK_WALK_CONT on success, TSK_WALK_ERROR on error.
+ */
+static TSK_WALK_RET_ENUM
+fswalk_callback (TSK_FS_FILE *fsfile, const char *path, void *data)
+{
+ int ret = 0;
+ CLEANUP_FREE char *fname = NULL;
+ struct guestfs_int_tsk_dirent dirent;
+
+ /* Ignore ./ and ../ */
+ ret = TSK_FS_ISDOT (fsfile->name->name);
+ if (ret != 0)
+ return TSK_WALK_CONT;
+
+ /* Build the full relative path of the entry */
+ ret = asprintf_nowarn (&fname, "%s%s", path, fsfile->name->name);
Since there are no more custom printf formatters, then plain asprintf
can be used again, so we get GCC warnings on formats/arguments
mismatches.
+ if (ret < 0) {
+ fprintf (stderr, "asprintf: %m");
perror ("asprintf") does the same, i.e. print a message and the string
representation of errno on stderr.
+ return TSK_WALK_ERROR;
+ }
+
+ dirent.tsk_inode = fsfile->name->meta_addr;
+ dirent.tsk_type = file_type (fsfile);
+ dirent.tsk_size = (fsfile->meta != NULL) ? fsfile->meta->size : -1;
+ dirent.tsk_name = fname;
+ dirent.tsk_allocated = !(fsfile->name->flags & TSK_FS_NAME_FLAG_UNALLOC);
+
+ ret = send_dirent_info (&dirent);
+ ret = (ret == 0) ? TSK_WALK_CONT : TSK_WALK_ERROR;
+
+ return ret;
+}
+
+/* Inspect fsfile to identify its type. */
+static char
+file_type(TSK_FS_FILE *fsfile)
Missing ^ space.
+{
+ if (fsfile->name->type < TSK_FS_NAME_TYPE_STR_MAX)
+ switch(fsfile->name->type) {
+ case TSK_FS_NAME_TYPE_UNDEF: return 'u';
+ case TSK_FS_NAME_TYPE_FIFO: return 'f';
+ case TSK_FS_NAME_TYPE_CHR: return 'c';
+ case TSK_FS_NAME_TYPE_DIR: return 'd';
+ case TSK_FS_NAME_TYPE_BLK: return 'b';
+ case TSK_FS_NAME_TYPE_REG: return 'r';
+ case TSK_FS_NAME_TYPE_LNK: return 'l';
+ case TSK_FS_NAME_TYPE_SOCK: return 's';
+ case TSK_FS_NAME_TYPE_SHAD: return 'h';
+ case TSK_FS_NAME_TYPE_WHT: return 'w';
+ case TSK_FS_NAME_TYPE_VIRT: return 'u'; /* Temp files created by TSK */
+ default: return 'u';
I'd move the fallback outsize the default of the switch, so the switch
has only cases for existing enum values.
+ }
+ else if (fsfile->meta != NULL &&
+ fsfile->meta->type < TSK_FS_META_TYPE_STR_MAX)
+ switch(fsfile->name->type) {
+ case TSK_FS_META_TYPE_REG: return 'r';
+ case TSK_FS_META_TYPE_DIR: return 'd';
+ case TSK_FS_META_TYPE_FIFO: return 'f';
+ case TSK_FS_META_TYPE_CHR: return 'c';
+ case TSK_FS_META_TYPE_BLK: return 'b';
+ case TSK_FS_META_TYPE_LNK: return 'l';
+ case TSK_FS_META_TYPE_SHAD: return 'h';
+ case TSK_FS_META_TYPE_SOCK: return 's';
+ case TSK_FS_META_TYPE_WHT: return 'w';
+ case TSK_FS_META_TYPE_VIRT: return 'u'; /* Temp files created by TSK */
+ default: return 'u';
Ditto.
+ }
+ else
+ return 'u';
+}
+
+/* Serialise dirent into XDR stream and send it to the appliance.
+ * Return 0 on success, -1 on error.
+ */
+static int
+send_dirent_info (guestfs_int_tsk_dirent *dirent)
+{
+ XDR xdr;
+ size_t len = 0;
+ bool_t ret = FALSE;
+ CLEANUP_FREE char *buf;
Make sure to always initialize CLEANUP_* variables: in this case there
is nothing between the declaration and the initialization of 'buf',
but if something that returns earlier is added between them, then it
will cause the handler run by CLEANUP_FREE to try to free an
uninitialized pointer.
+
+ buf = malloc (GUESTFS_MAX_CHUNK_SIZE);
+ if (buf == NULL) {
+ fprintf (stderr, "malloc: %m");
perror
+ return -1;
+ }
+
+ /* Serialise tsk_dirent struct. */
+ xdrmem_create (&xdr, buf, GUESTFS_MAX_CHUNK_SIZE, XDR_ENCODE);
+
+ ret = xdr_guestfs_int_tsk_dirent(&xdr, dirent);
Missing space here ... ^
+ if (ret == FALSE) {
+ fprintf (stderr, "xdr_guestfs_int_tsk_dirent: %m");
perror
+ return -1;
+ }
+
+ /* Resize buffer to actual length. */
+ len = xdr_getpos (&xdr);
+ xdr_destroy (&xdr);
+ buf = realloc (buf, len);
Why do you need to shrink the buffer? After all, send_file_write gets
amount of bytes to send back to the library.
Thanks,
--
Pino Toscano