mirror of
https://github.com/libguestfs/libguestfs.git
synced 2026-03-21 22:53:37 +00:00
New API: filesystem_walk
Library's counterpart of the daemon's internal_filesystem_walk command. It writes the daemon's command output on a temporary file and parses it, deserialising the XDR formatted tsk_dirent structs. It returns to the caller the list of tsk_dirent structs generated by the internal_filesystem_walk command. Signed-off-by: Matteo Cafasso <noxdafox@gmail.com>
This commit is contained in:
committed by
Richard W.M. Jones
parent
7d8fbde4b2
commit
19e7a52f8f
@@ -3551,6 +3551,120 @@ The environment variable C<XDG_RUNTIME_DIR> controls the default
|
||||
value: If C<XDG_RUNTIME_DIR> is set, then that is the default.
|
||||
Else F</tmp> is the default." };
|
||||
|
||||
{ defaults with
|
||||
name = "filesystem_walk"; added = (1, 33, 37);
|
||||
style = RStructList ("dirents", "tsk_dirent"), [Mountable "device";], [];
|
||||
optional = Some "libtsk";
|
||||
progress = true; cancellable = true;
|
||||
shortdesc = "walk through the filesystem content";
|
||||
longdesc = "\
|
||||
Walk through the internal structures of a disk partition
|
||||
(eg. F</dev/sda1>) in order to return a list of all the files
|
||||
and directories stored within.
|
||||
|
||||
It is not necessary to mount the disk partition to run this command.
|
||||
|
||||
All entries in the filesystem are returned, excluding C<.> and
|
||||
C<..>. This function can list deleted or unaccessible files.
|
||||
The entries are I<not> sorted.
|
||||
|
||||
The C<tsk_dirent> structure contains the following fields.
|
||||
|
||||
=over 4
|
||||
|
||||
=item 'tsk_inode'
|
||||
|
||||
Filesystem reference number of the node. It migh be C<0>
|
||||
if the node has been deleted.
|
||||
|
||||
=item 'tsk_type'
|
||||
|
||||
Basic file type information.
|
||||
See below for a detailed list of values.
|
||||
|
||||
=item 'tsk_size'
|
||||
|
||||
File size in bytes. It migh be C<-1>
|
||||
if the node has been deleted.
|
||||
|
||||
=item 'tsk_name'
|
||||
|
||||
The file path relative to its directory.
|
||||
|
||||
=item 'tsk_flags'
|
||||
|
||||
Bitfield containing extra information regarding the entry.
|
||||
It contains the logical OR of the following values:
|
||||
|
||||
=over 4
|
||||
|
||||
=item 0x0001
|
||||
|
||||
If set to C<1>, the file is allocated and visible within the filesystem.
|
||||
Otherwise, the file has been deleted.
|
||||
Under certain circumstances, the function C<download_inode>
|
||||
can be used to recover deleted files.
|
||||
|
||||
=item 0x0002
|
||||
|
||||
Filesystem such as NTFS and Ext2 or greater, separate the file name
|
||||
from the metadata structure.
|
||||
The bit is set to C<1> when the file name is in an unallocated state
|
||||
and the metadata structure is in an allocated one.
|
||||
This generally implies the metadata has been reallocated to a new file.
|
||||
Therefore, information such as file type and file size
|
||||
might not correspond with the ones of the original deleted entry.
|
||||
|
||||
=back
|
||||
|
||||
=back
|
||||
|
||||
The C<tsk_type> field will contain one of the following characters:
|
||||
|
||||
=over 4
|
||||
|
||||
=item 'b'
|
||||
|
||||
Block special
|
||||
|
||||
=item 'c'
|
||||
|
||||
Char special
|
||||
|
||||
=item 'd'
|
||||
|
||||
Directory
|
||||
|
||||
=item 'f'
|
||||
|
||||
FIFO (named pipe)
|
||||
|
||||
=item 'l'
|
||||
|
||||
Symbolic link
|
||||
|
||||
=item 'r'
|
||||
|
||||
Regular file
|
||||
|
||||
=item 's'
|
||||
|
||||
Socket
|
||||
|
||||
=item 'h'
|
||||
|
||||
Shadow inode (Solaris)
|
||||
|
||||
=item 'w'
|
||||
|
||||
Whiteout inode (BSD)
|
||||
|
||||
=item 'u'
|
||||
|
||||
Unknown file type
|
||||
|
||||
=back" };
|
||||
|
||||
]
|
||||
|
||||
(* daemon_functions are any functions which cause some action
|
||||
|
||||
@@ -131,6 +131,7 @@ libguestfs_la_SOURCES = \
|
||||
structs-copy.c \
|
||||
structs-free.c \
|
||||
tmpdirs.c \
|
||||
tsk.c \
|
||||
umask.c \
|
||||
wait.c \
|
||||
whole-file.c \
|
||||
|
||||
128
src/tsk.c
Normal file
128
src/tsk.c
Normal file
@@ -0,0 +1,128 @@
|
||||
/* libguestfs
|
||||
* Copyright (C) 2016 Red Hat Inc.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; 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 <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <string.h>
|
||||
#include <rpc/xdr.h>
|
||||
#include <rpc/types.h>
|
||||
|
||||
#include "guestfs.h"
|
||||
#include "guestfs_protocol.h"
|
||||
#include "guestfs-internal.h"
|
||||
#include "guestfs-internal-all.h"
|
||||
#include "guestfs-internal-actions.h"
|
||||
|
||||
static struct guestfs_tsk_dirent_list *parse_filesystem_walk (guestfs_h *, FILE *);
|
||||
static int deserialise_dirent_list (guestfs_h *, FILE *, struct guestfs_tsk_dirent_list *);
|
||||
|
||||
struct guestfs_tsk_dirent_list *
|
||||
guestfs_impl_filesystem_walk (guestfs_h *g, const char *mountable)
|
||||
{
|
||||
int ret = 0;
|
||||
CLEANUP_FCLOSE FILE *fp = NULL;
|
||||
CLEANUP_UNLINK_FREE char *tmpfile = NULL;
|
||||
|
||||
ret = guestfs_int_lazy_make_tmpdir (g);
|
||||
if (ret < 0)
|
||||
return NULL;
|
||||
|
||||
tmpfile = safe_asprintf (g, "%s/filesystem_walk%d", g->tmpdir, ++g->unique);
|
||||
|
||||
ret = guestfs_internal_filesystem_walk (g, mountable, tmpfile);
|
||||
if (ret < 0)
|
||||
return NULL;
|
||||
|
||||
fp = fopen (tmpfile, "r");
|
||||
if (fp == NULL) {
|
||||
perrorf (g, "fopen: %s", tmpfile);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return parse_filesystem_walk (g, fp); /* caller frees */
|
||||
}
|
||||
|
||||
/* Parse the file content and return dirents list.
|
||||
* Return a list of tsk_dirent on success, NULL on error.
|
||||
*/
|
||||
static struct guestfs_tsk_dirent_list *
|
||||
parse_filesystem_walk (guestfs_h *g, FILE *fp)
|
||||
{
|
||||
int ret = 0;
|
||||
struct guestfs_tsk_dirent_list *dirents = NULL;
|
||||
|
||||
/* Initialise results array. */
|
||||
dirents = safe_malloc (g, sizeof (*dirents));
|
||||
dirents->len = 8;
|
||||
dirents->val = safe_malloc (g, dirents->len * sizeof (*dirents->val));
|
||||
|
||||
/* Deserialise buffer into dirent list. */
|
||||
ret = deserialise_dirent_list (g, fp, dirents);
|
||||
if (ret < 0) {
|
||||
guestfs_free_tsk_dirent_list (dirents);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return dirents;
|
||||
}
|
||||
|
||||
/* Deserialise the file content and populate the dirent list.
|
||||
* Return the number of deserialised dirents, -1 on error.
|
||||
*/
|
||||
static int
|
||||
deserialise_dirent_list (guestfs_h *g, FILE *fp,
|
||||
struct guestfs_tsk_dirent_list *dirents)
|
||||
{
|
||||
XDR xdr;
|
||||
int ret = 0;
|
||||
uint32_t index = 0;
|
||||
struct stat statbuf;
|
||||
|
||||
ret = fstat (fileno(fp), &statbuf);
|
||||
if (ret == -1)
|
||||
return -1;
|
||||
|
||||
xdrstdio_create (&xdr, fp, XDR_DECODE);
|
||||
|
||||
for (index = 0; xdr_getpos (&xdr) < statbuf.st_size; index++) {
|
||||
if (index == dirents->len) {
|
||||
dirents->len = 2 * dirents->len;
|
||||
dirents->val = safe_realloc (g, dirents->val,
|
||||
dirents->len *
|
||||
sizeof (*dirents->val));
|
||||
}
|
||||
|
||||
/* Clear the entry so xdr logic will allocate necessary memory. */
|
||||
memset (&dirents->val[index], 0, sizeof (*dirents->val));
|
||||
ret = xdr_guestfs_int_tsk_dirent (&xdr, (guestfs_int_tsk_dirent *)
|
||||
&dirents->val[index]);
|
||||
if (ret == 0)
|
||||
break;
|
||||
}
|
||||
|
||||
xdr_destroy (&xdr);
|
||||
dirents->len = index;
|
||||
|
||||
return ret ? 0 : -1;
|
||||
}
|
||||
Reference in New Issue
Block a user