All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Tzvetomir Stoyanov (VMware)" <tz.stoyanov@gmail.com>
To: rostedt@goodmis.org
Cc: linux-trace-devel@vger.kernel.org
Subject: [PATCH v3 1/5] trace-cmd: Add new libtracefs API tracefs_instance_file_append()
Date: Wed, 26 Feb 2020 18:31:55 +0200	[thread overview]
Message-ID: <20200226163159.20232-2-tz.stoyanov@gmail.com> (raw)
In-Reply-To: <20200226163159.20232-1-tz.stoyanov@gmail.com>

The existing tracefs_instance_file_write() API truncates the file before writing to it.
The are use cases where the file must not be truncated. The new
  tracefs_instance_file_append()
API appends to the end of the file.

Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com>
---
 include/tracefs/tracefs.h      |  2 ++
 lib/tracefs/tracefs-instance.c | 53 +++++++++++++++++++++++++---------
 2 files changed, 41 insertions(+), 14 deletions(-)

diff --git a/include/tracefs/tracefs.h b/include/tracefs/tracefs.h
index bc8bebcb..22f147a3 100644
--- a/include/tracefs/tracefs.h
+++ b/include/tracefs/tracefs.h
@@ -30,6 +30,8 @@ tracefs_instance_get_file(struct tracefs_instance *instance, const char *file);
 char *tracefs_instance_get_dir(struct tracefs_instance *instance);
 int tracefs_instance_file_write(struct tracefs_instance *instance,
 				const char *file, const char *str);
+int tracefs_instance_file_append(struct tracefs_instance *instance,
+				 const char *file, const char *str);
 char *tracefs_instance_file_read(struct tracefs_instance *instance,
 				 char *file, int *psize);
 
diff --git a/lib/tracefs/tracefs-instance.c b/lib/tracefs/tracefs-instance.c
index 67123e7c..cc4bc1b4 100644
--- a/lib/tracefs/tracefs-instance.c
+++ b/lib/tracefs/tracefs-instance.c
@@ -176,12 +176,16 @@ char *tracefs_instance_get_name(struct tracefs_instance *instance)
 	return NULL;
 }
 
-static int write_file(const char *file, const char *str)
+static int write_file(const char *file, const char *str, bool appned)
 {
 	int ret;
 	int fd;
 
-	fd = open(file, O_WRONLY | O_TRUNC);
+	if (appned)
+		fd = open(file, O_WRONLY | O_APPEND);
+	else
+		fd = open(file, O_WRONLY | O_TRUNC);
+
 	if (fd < 0) {
 		warning("Failed to open '%s'", file);
 		return -1;
@@ -191,17 +195,8 @@ static int write_file(const char *file, const char *str)
 	return ret;
 }
 
-
-/**
- * tracefs_instance_file_write - Write in trace file of specific instance.
- * @instance: ftrace instance, can be NULL for the top instance
- * @file: name of the file
- * @str: nul terminated string, that will be written in the file.
- *
- * Returns the number of written bytes, or -1 in case of an error
- */
-int tracefs_instance_file_write(struct tracefs_instance *instance,
-				 const char *file, const char *str)
+static int instance_file_write(struct tracefs_instance *instance,
+			       const char *file, const char *str, bool appned)
 {
 	struct stat st;
 	char *path;
@@ -212,12 +207,42 @@ int tracefs_instance_file_write(struct tracefs_instance *instance,
 		return -1;
 	ret = stat(path, &st);
 	if (ret == 0)
-		ret = write_file(path, str);
+		ret = write_file(path, str, appned);
 	tracefs_put_tracing_file(path);
 
 	return ret;
 }
 
+/**
+ * tracefs_instance_file_write - Write in trace file of specific instance.
+ * @instance: ftrace instance, can be NULL for the top instance
+ * @file: name of the file
+ * @str: nul terminated string, that will be written in the file.
+ *
+ * Returns the number of written bytes, or -1 in case of an error.
+ * The content of the file is replaced with the new one.
+ *
+ */
+int tracefs_instance_file_write(struct tracefs_instance *instance,
+				 const char *file, const char *str)
+{
+	return instance_file_write(instance, file, str, false);
+}
+
+/**
+ * tracefs_instance_file_append - Append to a trace file of specific instance.
+ * @instance: ftrace instance, can be NULL for the top instance
+ * @file: name of the file
+ * @str: nul terminated string, that will be written in the file.
+ *
+ * Returns the number of written bytes, or -1 in case of an error.
+ */
+int tracefs_instance_file_append(struct tracefs_instance *instance,
+				 const char *file, const char *str)
+{
+	return instance_file_write(instance, file, str, true);
+}
+
 /**
  * tracefs_instance_file_read - Read from a trace file of specific instance.
  * @instance: ftrace instance, can be NULL for the top instance
-- 
2.24.1


  reply	other threads:[~2020-02-26 16:32 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-26 16:31 [PATCH v3 0/5] trace-cmd: SQL-like syntax for ftrace histograms configuration Tzvetomir Stoyanov (VMware)
2020-02-26 16:31 ` Tzvetomir Stoyanov (VMware) [this message]
2020-02-26 16:31 ` [PATCH v3 2/5] trace-cmd: Unit test for tracefs_instance_file_append() API Tzvetomir Stoyanov (VMware)
2020-02-26 16:31 ` [PATCH v3 3/5] trace-cmd: Add new libtraceevent flag to suppress parsing warnings Tzvetomir Stoyanov (VMware)
2020-02-26 16:31 ` [PATCH v3 4/5] trace-cmd: Suppress parsing warnings in tracefs_local_events() API Tzvetomir Stoyanov (VMware)
2020-02-26 16:31 ` [PATCH v3 5/5] trace-cmd: Add "--sql" option to trace-cmd start and record sub commands Tzvetomir Stoyanov (VMware)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200226163159.20232-2-tz.stoyanov@gmail.com \
    --to=tz.stoyanov@gmail.com \
    --cc=linux-trace-devel@vger.kernel.org \
    --cc=rostedt@goodmis.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.