On Mon, 17 Feb 2020 19:39:56 +0200 "Tzvetomir Stoyanov (VMware)" <tz.stoyanov@xxxxxxxxx> wrote: > 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@xxxxxxxxx> > --- > include/tracefs/tracefs.h | 2 ++ > lib/tracefs/tracefs-instance.c | 40 ++++++++++++++++++++++++++++++---- > 2 files changed, 38 insertions(+), 4 deletions(-) > > diff --git a/include/tracefs/tracefs.h b/include/tracefs/tracefs.h > index bd3f732..16fcb66 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 b96ab61..e6b0a95 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) Typo, s/appned/append/ > { > 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; > @@ -198,7 +202,9 @@ static int write_file(const char *file, const char *str) > * @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 > + * 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) > @@ -212,7 +218,33 @@ 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, false); > + tracefs_put_tracing_file(path); > + > + return ret; > +} > + > +/** > + * 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) > +{ > + struct stat st; > + char *path; > + int ret; > + > + path = tracefs_instance_get_file(instance, file); > + if (!path) > + return -1; > + ret = stat(path, &st); > + if (ret == 0) > + ret = write_file(path, str, true); > tracefs_put_tracing_file(path); > > return ret; I would actually make one more static helper as the above two functions are identical except for the write file part. static int instance_file_write(struct tracefs_instance *instance, const char *file, const char *str, bool append) { struct stat st; char *path; int ret; path = tracefs_instance_get_file(instance, file); if (!path) return -1; ret = stat(path, &st); if (ret == 0) ret = write_file(path, str, append); tracefs_put_tracing_file(path); return ret; } int tracefs_instance_file_write(struct tracefs_instance *instance, const char *file, const char *str) { return instance_file_write(instance, file, str, false); } int tracefs_instance_file_append(struct tracefs_instance *instance, const char *file, const char *str) { return instance_file_write(instance, file, str, true); } -- Steve