diff mbox series

[v3,1/5] trace-cmd: Add new libtracefs API tracefs_instance_file_append()

Message ID 20200226163159.20232-2-tz.stoyanov@gmail.com (mailing list archive)
State Superseded
Headers show
Series trace-cmd: SQL-like syntax for ftrace histograms configuration | expand

Commit Message

Tzvetomir Stoyanov (VMware) Feb. 26, 2020, 4:31 p.m. UTC
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 mbox series

Patch

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