libsysprof-capture: add file reader helper

This commit is contained in:
Christian Hergert
2019-05-27 15:59:05 -07:00
parent 336dae4d1f
commit 596d29496d
3 changed files with 81 additions and 0 deletions

View File

@ -1168,6 +1168,10 @@ sysprof_capture_reader_read_file (SysprofCaptureReader *self)
if ((self->pos % SYSPROF_CAPTURE_ALIGN) != 0)
return NULL;
/* Make sure len is < the extra frame data */
if (file_chunk->len > (file_chunk->frame.len - sizeof *file_chunk))
return NULL;
/* Ensure trailing \0 in .path */
file_chunk->path[sizeof file_chunk->path - 1] = 0;
@ -1209,3 +1213,64 @@ sysprof_capture_reader_list_files (SysprofCaptureReader *self)
return (gchar **)g_ptr_array_free (g_steal_pointer (&ar), FALSE);
}
gboolean
sysprof_capture_reader_read_file_fd (SysprofCaptureReader *self,
const gchar *path,
gint fd)
{
g_assert (self != NULL);
g_assert (path != NULL);
g_assert (fd > -1);
for (;;)
{
SysprofCaptureFrameType type;
const SysprofCaptureFileChunk *file;
const guint8 *buf;
gsize to_write;
if (!sysprof_capture_reader_peek_type (self, &type))
return FALSE;
if (type != SYSPROF_CAPTURE_FRAME_FILE_CHUNK)
goto skip;
if (!(file = sysprof_capture_reader_read_file (self)))
return FALSE;
if (g_strcmp0 (path, file->path) != 0)
goto skip;
buf = file->data;
to_write = file->len;
while (to_write > 0)
{
gssize written;
written = _sysprof_write (fd, buf, to_write);
if (written < 0)
return FALSE;
if (written == 0 && errno != EAGAIN)
return FALSE;
g_assert (written <= (gssize)to_write);
buf += written;
to_write -= written;
}
if (!file->is_last)
continue;
return TRUE;
skip:
if (!sysprof_capture_reader_skip (self))
return FALSE;
}
g_return_val_if_reached (FALSE);
}