mirror of
https://github.com/varun-r-mallya/sysprof.git
synced 2025-12-31 20:36:25 +00:00
libsysprof-capture: Drop GError usage from SysprofCaptureWriter
Use `errno` instead, which is icky, but given that all of the failure modes are from POSIX I/O functions, it’s at least in keeping with them. This is a major API break. Signed-off-by: Philip Withnall <withnall@endlessm.com> Helps: #40
This commit is contained in:
@ -57,6 +57,7 @@
|
||||
#include "config.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <glib/gstdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
@ -169,9 +170,8 @@ translate_table_translate (TranslateTable *tables,
|
||||
}
|
||||
|
||||
bool
|
||||
sysprof_capture_writer_cat (SysprofCaptureWriter *self,
|
||||
SysprofCaptureReader *reader,
|
||||
GError **error)
|
||||
sysprof_capture_writer_cat (SysprofCaptureWriter *self,
|
||||
SysprofCaptureReader *reader)
|
||||
{
|
||||
TranslateTable tables[N_TRANSLATE] = { 0, };
|
||||
SysprofCaptureFrameType type;
|
||||
@ -552,13 +552,9 @@ sysprof_capture_writer_cat (SysprofCaptureWriter *self,
|
||||
return true;
|
||||
|
||||
panic:
|
||||
g_set_error (error,
|
||||
G_FILE_ERROR,
|
||||
G_FILE_ERROR_FAILED,
|
||||
"Failed to write data");
|
||||
|
||||
translate_table_clear (tables, TRANSLATE_ADDR);
|
||||
translate_table_clear (tables, TRANSLATE_CTR);
|
||||
|
||||
errno = EIO;
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -918,24 +918,26 @@ sysprof_capture_writer_flush (SysprofCaptureWriter *self)
|
||||
* sysprof_capture_writer_save_as:
|
||||
* @self: A #SysprofCaptureWriter
|
||||
* @filename: the file to save the capture as
|
||||
* @error: a location for a #GError or %NULL.
|
||||
*
|
||||
* Saves the captured data as the file @filename.
|
||||
*
|
||||
* This is primarily useful if the writer was created with a memory-backed
|
||||
* file-descriptor such as a memfd or tmpfs file on Linux.
|
||||
*
|
||||
* Returns: %TRUE if successful, otherwise %FALSE and @error is set.
|
||||
* `errno` is set on error, to any of the errors returned by `open()`,
|
||||
* sysprof_capture_writer_flush(), `lseek()` or `sendfile()`.
|
||||
*
|
||||
* Returns: %TRUE if successful, otherwise %FALSE and `errno` is set.
|
||||
*/
|
||||
bool
|
||||
sysprof_capture_writer_save_as (SysprofCaptureWriter *self,
|
||||
const char *filename,
|
||||
GError **error)
|
||||
sysprof_capture_writer_save_as (SysprofCaptureWriter *self,
|
||||
const char *filename)
|
||||
{
|
||||
size_t to_write;
|
||||
off_t in_off;
|
||||
off_t pos;
|
||||
int fd = -1;
|
||||
int errsv;
|
||||
|
||||
assert (self != NULL);
|
||||
assert (self->fd != -1);
|
||||
@ -975,10 +977,7 @@ sysprof_capture_writer_save_as (SysprofCaptureWriter *self,
|
||||
return true;
|
||||
|
||||
handle_errno:
|
||||
g_set_error (error,
|
||||
G_FILE_ERROR,
|
||||
g_file_error_from_errno (errno),
|
||||
"%s", g_strerror (errno));
|
||||
errsv = errno;
|
||||
|
||||
if (fd != -1)
|
||||
{
|
||||
@ -986,6 +985,8 @@ handle_errno:
|
||||
unlink (filename);
|
||||
}
|
||||
|
||||
errno = errsv;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -993,7 +994,6 @@ handle_errno:
|
||||
* _sysprof_capture_writer_splice_from_fd:
|
||||
* @self: An #SysprofCaptureWriter
|
||||
* @fd: the fd to read from.
|
||||
* @error: A location for a #GError, or %NULL.
|
||||
*
|
||||
* This is internal API for SysprofCaptureWriter and SysprofCaptureReader to
|
||||
* communicate when splicing a reader into a writer.
|
||||
@ -1003,12 +1003,14 @@ handle_errno:
|
||||
*
|
||||
* This will not advance the position of @fd.
|
||||
*
|
||||
* Returns: %TRUE if successful; otherwise %FALSE and @error is set.
|
||||
* `errno` is set on error, to any of the errors returned by `fstat()` or
|
||||
* `sendfile()`, or `EBADMSG` if the file is corrupt.
|
||||
*
|
||||
* Returns: %TRUE if successful; otherwise %FALSE and `errno` is set.
|
||||
*/
|
||||
bool
|
||||
_sysprof_capture_writer_splice_from_fd (SysprofCaptureWriter *self,
|
||||
int fd,
|
||||
GError **error)
|
||||
_sysprof_capture_writer_splice_from_fd (SysprofCaptureWriter *self,
|
||||
int fd)
|
||||
{
|
||||
struct stat stbuf;
|
||||
off_t in_off;
|
||||
@ -1022,10 +1024,7 @@ _sysprof_capture_writer_splice_from_fd (SysprofCaptureWriter *self,
|
||||
|
||||
if (stbuf.st_size < 256)
|
||||
{
|
||||
g_set_error (error,
|
||||
G_FILE_ERROR,
|
||||
G_FILE_ERROR_INVAL,
|
||||
"Cannot splice, possibly corrupt file.");
|
||||
errno = EBADMSG;
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -1052,11 +1051,7 @@ _sysprof_capture_writer_splice_from_fd (SysprofCaptureWriter *self,
|
||||
return true;
|
||||
|
||||
handle_errno:
|
||||
g_set_error (error,
|
||||
G_FILE_ERROR,
|
||||
g_file_error_from_errno (errno),
|
||||
"%s", g_strerror (errno));
|
||||
|
||||
/* errno is propagated */
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -1064,22 +1059,25 @@ handle_errno:
|
||||
* sysprof_capture_writer_splice:
|
||||
* @self: An #SysprofCaptureWriter
|
||||
* @dest: An #SysprofCaptureWriter
|
||||
* @error: A location for a #GError, or %NULL.
|
||||
*
|
||||
* This function will copy the capture @self into the capture @dest. This
|
||||
* tries to be semi-efficient by using sendfile() to copy the contents between
|
||||
* the captures. @self and @dest will be flushed before the contents are copied
|
||||
* into the @dest file-descriptor.
|
||||
*
|
||||
* Returns: %TRUE if successful, otherwise %FALSE and and @error is set.
|
||||
* `errno` is set on error, to any of the errors returned by
|
||||
* sysprof_capture_writer_flush(), `lseek()` or
|
||||
* _sysprof_capture_writer_splice_from_fd().
|
||||
*
|
||||
* Returns: %TRUE if successful, otherwise %FALSE and and `errno` is set.
|
||||
*/
|
||||
bool
|
||||
sysprof_capture_writer_splice (SysprofCaptureWriter *self,
|
||||
SysprofCaptureWriter *dest,
|
||||
GError **error)
|
||||
sysprof_capture_writer_splice (SysprofCaptureWriter *self,
|
||||
SysprofCaptureWriter *dest)
|
||||
{
|
||||
bool ret;
|
||||
off_t pos;
|
||||
int errsv;
|
||||
|
||||
assert (self != NULL);
|
||||
assert (self->fd != -1);
|
||||
@ -1095,30 +1093,25 @@ sysprof_capture_writer_splice (SysprofCaptureWriter *self,
|
||||
goto handle_errno;
|
||||
|
||||
/* Perform the splice */
|
||||
ret = _sysprof_capture_writer_splice_from_fd (dest, self->fd, error);
|
||||
ret = _sysprof_capture_writer_splice_from_fd (dest, self->fd);
|
||||
errsv = errno;
|
||||
|
||||
/* Now reset or file-descriptor position (it should be the same */
|
||||
if (pos != lseek (self->fd, pos, SEEK_SET))
|
||||
{
|
||||
ret = false;
|
||||
goto handle_errno;
|
||||
}
|
||||
goto handle_errno;
|
||||
|
||||
if (!ret)
|
||||
errno = errsv;
|
||||
return ret;
|
||||
|
||||
handle_errno:
|
||||
g_set_error (error,
|
||||
G_FILE_ERROR,
|
||||
g_file_error_from_errno (errno),
|
||||
"%s", g_strerror (errno));
|
||||
|
||||
/* errno is propagated */
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* sysprof_capture_writer_create_reader:
|
||||
* @self: A #SysprofCaptureWriter
|
||||
* @error: a location for a #GError, or %NULL
|
||||
*
|
||||
* Creates a new reader for the writer.
|
||||
*
|
||||
@ -1127,11 +1120,14 @@ handle_errno:
|
||||
* also consuming from the reader, you could get transient failures unless you
|
||||
* synchronize the operations.
|
||||
*
|
||||
* `errno` is set on error, to any of the errors returned by
|
||||
* sysprof_capture_writer_flush(), `dup()` or
|
||||
* sysprof_capture_reader_new_from_fd().
|
||||
*
|
||||
* Returns: (transfer full): A #SysprofCaptureReader.
|
||||
*/
|
||||
SysprofCaptureReader *
|
||||
sysprof_capture_writer_create_reader (SysprofCaptureWriter *self,
|
||||
GError **error)
|
||||
sysprof_capture_writer_create_reader (SysprofCaptureWriter *self)
|
||||
{
|
||||
SysprofCaptureReader *ret;
|
||||
int copy;
|
||||
@ -1141,10 +1137,7 @@ sysprof_capture_writer_create_reader (SysprofCaptureWriter *self,
|
||||
|
||||
if (!sysprof_capture_writer_flush (self))
|
||||
{
|
||||
g_set_error (error,
|
||||
G_FILE_ERROR,
|
||||
g_file_error_from_errno (errno),
|
||||
"%s", g_strerror (errno));
|
||||
/* errno is propagated */
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@ -1153,10 +1146,18 @@ sysprof_capture_writer_create_reader (SysprofCaptureWriter *self,
|
||||
* uses positioned reads.
|
||||
*/
|
||||
if (-1 == (copy = dup (self->fd)))
|
||||
return NULL;
|
||||
{
|
||||
/* errno is propagated */
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if ((ret = sysprof_capture_reader_new_from_fd (copy, error)))
|
||||
sysprof_capture_reader_set_stat (ret, &self->stat);
|
||||
if (!(ret = sysprof_capture_reader_new_from_fd (copy)))
|
||||
{
|
||||
/* errno is propagated */
|
||||
return NULL;
|
||||
}
|
||||
|
||||
sysprof_capture_reader_set_stat (ret, &self->stat);
|
||||
|
||||
return sysprof_steal_pointer (&ret);
|
||||
}
|
||||
|
||||
@ -207,29 +207,24 @@ SYSPROF_AVAILABLE_IN_ALL
|
||||
bool sysprof_capture_writer_flush (SysprofCaptureWriter *self);
|
||||
SYSPROF_AVAILABLE_IN_ALL
|
||||
bool sysprof_capture_writer_save_as (SysprofCaptureWriter *self,
|
||||
const char *filename,
|
||||
GError **error);
|
||||
const char *filename);
|
||||
SYSPROF_AVAILABLE_IN_ALL
|
||||
unsigned int sysprof_capture_writer_request_counter (SysprofCaptureWriter *self,
|
||||
unsigned int n_counters);
|
||||
SYSPROF_AVAILABLE_IN_ALL
|
||||
SysprofCaptureReader *sysprof_capture_writer_create_reader (SysprofCaptureWriter *self,
|
||||
GError **error);
|
||||
SysprofCaptureReader *sysprof_capture_writer_create_reader (SysprofCaptureWriter *self);
|
||||
SYSPROF_AVAILABLE_IN_ALL
|
||||
bool sysprof_capture_writer_splice (SysprofCaptureWriter *self,
|
||||
SysprofCaptureWriter *dest,
|
||||
GError **error);
|
||||
SysprofCaptureWriter *dest);
|
||||
SYSPROF_AVAILABLE_IN_ALL
|
||||
bool sysprof_capture_writer_cat (SysprofCaptureWriter *self,
|
||||
SysprofCaptureReader *reader,
|
||||
GError **error);
|
||||
SysprofCaptureReader *reader);
|
||||
SYSPROF_INTERNAL
|
||||
bool _sysprof_capture_writer_add_raw (SysprofCaptureWriter *self,
|
||||
const SysprofCaptureFrame *frame);
|
||||
SYSPROF_INTERNAL
|
||||
bool _sysprof_capture_writer_splice_from_fd (SysprofCaptureWriter *self,
|
||||
int fd,
|
||||
GError **error) SYSPROF_INTERNAL;
|
||||
int fd) SYSPROF_INTERNAL;
|
||||
SYSPROF_INTERNAL
|
||||
bool _sysprof_capture_writer_set_time_range (SysprofCaptureWriter *self,
|
||||
int64_t start_time,
|
||||
|
||||
Reference in New Issue
Block a user