common: add bt_common_read() function
[babeltrace.git] / include / babeltrace / common-internal.h
index 2c9f8a2f1e63a0c4000f105963d7cf168c8fb384..19594fe3c347f4345788befe9078df3dac13bcf2 100644 (file)
 #include <babeltrace/trace-ir/event-class-const.h>
 #include <babeltrace/graph/self-message-iterator.h>
 #include <babeltrace/value.h>
-#include <stdarg.h>
 #include <inttypes.h>
+#include <stdarg.h>
 #include <stdint.h>
+#include <unistd.h>
 #include <glib.h>
 
 #define BT_COMMON_COLOR_RESET              "\033[0m"
@@ -305,6 +306,55 @@ void bt_common_custom_snprintf(char *buf, size_t buf_size,
 BT_HIDDEN
 size_t bt_common_get_page_size(void);
 
+/*
+ * Wraps read() function to handle EINTR and partial reads.
+ * On success, it returns `count` received as parameter. On error, it returns a
+ * value smaller than the requested `count`.
+ */
+static inline
+ssize_t bt_common_read(int fd, void *buf, size_t count)
+{
+       size_t i = 0;
+       ssize_t ret;
+
+       BT_ASSERT(buf);
+
+       /* Never return an overflow value. */
+       BT_ASSERT(count <= SSIZE_MAX);
+
+       do {
+               ret = read(fd, buf + i, count - i);
+               if (ret < 0) {
+                       if (errno == EINTR) {
+#ifdef BT_LOGD_STR
+                               BT_LOGD_STR("read() call interrupted. Retrying...");
+#endif
+                               /* retry operation */
+                               continue;
+                       } else {
+#ifdef BT_LOGE_ERRNO
+                               BT_LOGE_ERRNO("Error while reading", ": fd=%d",
+                                       fd);
+#endif
+                               goto end;
+                       }
+               }
+               i += ret;
+               BT_ASSERT(i <= count);
+       } while (count - i > 0 && ret > 0);
+
+end:
+       if (ret >= 0) {
+               if (i == 0) {
+                       ret = -1;
+               } else {
+                       ret = i;
+               }
+       }
+
+       return ret;
+}
+
 static inline
 const char *bt_common_field_class_type_string(enum bt_field_class_type class_type)
 {
@@ -446,9 +496,21 @@ GString *bt_common_field_path_string(struct bt_field_path *path)
        g_string_append_printf(str, "[%s", bt_common_scope_string(
                bt_field_path_get_root_scope(path)));
 
-       for (i = 0; i < bt_field_path_get_index_count(path); i++) {
-               g_string_append_printf(str, ", %" PRIu64,
-                       bt_field_path_get_index_by_index(path, i));
+       for (i = 0; i < bt_field_path_get_item_count(path); i++) {
+               const struct bt_field_path_item *fp_item =
+                       bt_field_path_borrow_item_by_index_const(path, i);
+
+               switch (bt_field_path_item_get_type(fp_item)) {
+               case BT_FIELD_PATH_ITEM_TYPE_INDEX:
+                       g_string_append_printf(str, ", %" PRIu64,
+                               bt_field_path_item_index_get_index(fp_item));
+                       break;
+               case BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT:
+                       g_string_append(str, ", <CUR>");
+                       break;
+               default:
+                       abort();
+               }
        }
 
        g_string_append(str, "]");
@@ -569,9 +631,9 @@ end:
 }
 
 static inline
-bt_self_message_iterator_status bt_message_iterator_status_to_self(
-               bt_message_iterator_status status)
+enum bt_self_message_iterator_status bt_common_message_iterator_status_to_self(
+               enum bt_message_iterator_status status)
 {
-       return (bt_self_message_iterator_status) status;
+       return (int) status;
 }
 #endif /* BABELTRACE_COMMON_INTERNAL_H */
This page took 0.025512 seconds and 4 git commands to generate.