linux-trace-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: "Tzvetomir Stoyanov (VMware)" <tz.stoyanov@gmail.com>
Cc: linux-trace-devel@vger.kernel.org
Subject: Re: [PATCH 1/4] trace-cmd: Add new libtracefs API tracefs_instance_file_append()
Date: Thu, 20 Feb 2020 18:35:56 -0500	[thread overview]
Message-ID: <20200220183556.177dfe7b@gandalf.local.home> (raw)
In-Reply-To: <20200217173959.385278-2-tz.stoyanov@gmail.com>

On Mon, 17 Feb 2020 19:39:56 +0200
"Tzvetomir Stoyanov (VMware)" <tz.stoyanov@gmail.com> 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@gmail.com>
> ---
>  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

  reply	other threads:[~2020-02-20 23:35 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-17 17:39 [PATCH 0/4] trace-cmd: SQL-like syntax for ftrace histograms configuration Tzvetomir Stoyanov (VMware)
2020-02-17 17:39 ` [PATCH 1/4] trace-cmd: Add new libtracefs API tracefs_instance_file_append() Tzvetomir Stoyanov (VMware)
2020-02-20 23:35   ` Steven Rostedt [this message]
2020-02-17 17:39 ` [PATCH 2/4] trace-cmd: Clear synthetic events on reset subcommand Tzvetomir Stoyanov (VMware)
2020-02-17 17:39 ` [PATCH 3/4] trace-cmd: Clear error log " Tzvetomir Stoyanov (VMware)
2020-02-17 17:39 ` [PATCH 4/4] 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=20200220183556.177dfe7b@gandalf.local.home \
    --to=rostedt@goodmis.org \
    --cc=linux-trace-devel@vger.kernel.org \
    --cc=tz.stoyanov@gmail.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).