[sysprof] tools: check result of write



commit 7df9221f8b8e56746cb5471a7184835867f75c00
Author: Christian Hergert <chergert redhat com>
Date:   Mon Jun 3 12:40:48 2019 -0700

    tools: check result of write
    
    and also use the platform helpers

 .../sysprof-capture-util-private.h                 | 22 +++++++++----------
 src/tools/sysprof-cat.c                            | 25 +++++++++++++++++++++-
 2 files changed, 35 insertions(+), 12 deletions(-)
---
diff --git a/src/libsysprof-capture/sysprof-capture-util-private.h 
b/src/libsysprof-capture/sysprof-capture-util-private.h
index 041ec97..e750d93 100644
--- a/src/libsysprof-capture/sysprof-capture-util-private.h
+++ b/src/libsysprof-capture/sysprof-capture-util-private.h
@@ -38,20 +38,20 @@
 #else
 size_t  _sysprof_getpagesize (void);
 ssize_t _sysprof_pread       (int     fd,
-                         void   *buf,
-                         size_t  count,
-                         off_t   offset);
+                              void   *buf,
+                              size_t  count,
+                              off_t   offset);
 ssize_t _sysprof_pwrite      (int         fd,
-                         const void *buf,
-                         size_t      count,
-                         off_t       offset);
+                              const void *buf,
+                              size_t      count,
+                              off_t       offset);
 ssize_t _sysprof_write       (int         fd,
-                         const void *buf,
-                         size_t      count);
+                              const void *buf,
+                              size_t      count);
 gint32  _sysprof_getpid      (void);
 ssize_t _sysprof_sendfile    (int     out_fd,
-                         int     in_fd,
-                         off_t  *offset,
-                         size_t  count);
+                              int     in_fd,
+                              off_t  *offset,
+                              size_t  count);
 
 #endif
diff --git a/src/tools/sysprof-cat.c b/src/tools/sysprof-cat.c
index b12890f..b4187bf 100644
--- a/src/tools/sysprof-cat.c
+++ b/src/tools/sysprof-cat.c
@@ -22,11 +22,14 @@
 
 #include "config.h"
 
+#include <errno.h>
 #include <glib/gstdio.h>
 #include <stdlib.h>
 #include <sysprof-capture.h>
 #include <unistd.h>
 
+#include "sysprof-capture-util-private.h"
+
 gint
 main (gint   argc,
       gchar *argv[])
@@ -84,7 +87,27 @@ main (gint   argc,
     }
 
   if (g_file_get_contents (tmpname, &contents, &len, NULL))
-    write (STDOUT_FILENO, contents, len);
+    {
+      const gchar *buf = contents;
+      gsize to_write = len;
+
+      while (to_write > 0)
+        {
+          gssize n_written;
+
+          n_written = _sysprof_write (STDOUT_FILENO, buf, to_write);
+
+          if (n_written < 0)
+            {
+              g_printerr ("%s\n", g_strerror (errno));
+              g_unlink (tmpname);
+              return EXIT_FAILURE;
+            }
+
+          buf += n_written;
+          to_write -= n_written;
+        }
+    }
 
   g_unlink (tmpname);
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]