xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Wei Liu <wei.liu2@citrix.com>
To: Xen-devel <xen-devel@lists.xenproject.org>
Cc: Wei Liu <wei.liu2@citrix.com>, Ian Jackson <Ian.Jackson@eu.citrix.com>
Subject: [PATCH 6/6] xenconsoled: handle --log-backups 0 in logfile_rollover
Date: Mon, 6 Jun 2016 16:59:41 +0100	[thread overview]
Message-ID: <1465228781-22754-7-git-send-email-wei.liu2@citrix.com> (raw)
In-Reply-To: <1465228781-22754-1-git-send-email-wei.liu2@citrix.com>

We now allow user to configure the number of backups to keep. We need to
handle when the number is set to 0.

Check if number of backup is 0. If so, just unlink the file. Move the
rotation to `else' branch so that we skip it altogether.

Signed-off-by: Wei Liu <wei.liu2@citrix.com>
---
 tools/console/daemon/logfile.c | 63 +++++++++++++++++++++++++-----------------
 1 file changed, 37 insertions(+), 26 deletions(-)

diff --git a/tools/console/daemon/logfile.c b/tools/console/daemon/logfile.c
index 3b95f84..fd29ba5 100644
--- a/tools/console/daemon/logfile.c
+++ b/tools/console/daemon/logfile.c
@@ -114,38 +114,49 @@ static int logfile_rollover(struct logfile *file)
 	char *next = NULL;
 	unsigned i;
 
-	if (asprintf(&next, "%s.%u", file->basepath,
-		     log_backups) < 0) {
-		dolog(LOG_ERR, "Failed to asprintf %s.%u",
-		      file->basepath, log_backups);
-		goto err;
-	}
+	if (log_backups == 0) {
+		if (unlink(file->basepath) < 0 &&
+		    errno != ENOENT) {
+			dolog(LOG_ERR, "Failed to unlink %s",
+			      file->basepath);
+			goto err;
+		}
+	} else {
+		if (asprintf(&next, "%s.%u", file->basepath,
+			     log_backups) < 0) {
+			dolog(LOG_ERR, "Failed to asprintf %s.%u",
+			      file->basepath, log_backups);
+			goto err;
+		}
 
-	for (i = log_backups; i > 0; i--) {
-		if (i == 1) {
-			this = strdup(file->basepath);
-			if (!this) {
-				dolog(LOG_ERR, "Failed to strdup %s",
-				      file->basepath);
-				goto err;
+		for (i = log_backups; i > 0; i--) {
+			if (i == 1) {
+				this = strdup(file->basepath);
+				if (!this) {
+					dolog(LOG_ERR, "Failed to strdup %s",
+					      file->basepath);
+					goto err;
+				}
+			} else {
+				if (asprintf(&this, "%s.%u", file->basepath,
+					     i-1) < 0) {
+					dolog(LOG_ERR,
+					      "Failed to asprintf %s.%u",
+					      file->basepath, i-1);
+					goto err;
+				}
 			}
-		} else {
-			if (asprintf(&this, "%s.%u", file->basepath, i-1) < 0) {
-				dolog(LOG_ERR, "Failed to asprintf %s.%u",
-				      file->basepath, i-1);
+
+			if (rename(this, next) < 0 && errno != ENOENT) {
+				dolog(LOG_ERR, "Failed to rename %s to %s",
+				      this, next);
 				goto err;
 			}
-		}
 
-		if (rename(this, next) < 0 && errno != ENOENT) {
-			dolog(LOG_ERR, "Failed to rename %s to %s",
-			      this, next);
-			goto err;
+			free(next);
+			next = this;
+			this = NULL;
 		}
-
-		free(next);
-		next = this;
-		this = NULL;
 	}
 
 	new = logfile_entry_new(file->basepath, file->mode);
-- 
2.1.4


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
http://lists.xen.org/xen-devel

  parent reply	other threads:[~2016-06-06 15:59 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-06 15:59 [PATCH 0/6] xenconsoled: rotating log file abstration Wei Liu
2016-06-06 15:59 ` [PATCH 1/6] xenconsoled: introduce log file abstraction Wei Liu
2016-07-08 16:50   ` Ian Jackson
2016-06-06 15:59 ` [PATCH 2/6] xenconsoled: switch hypervisor log to use logfile abstraction Wei Liu
2016-07-08 16:58   ` Ian Jackson
2016-06-06 15:59 ` [PATCH 3/6] xenconsoled: switch guest " Wei Liu
2016-07-08 17:01   ` Ian Jackson
2016-06-06 15:59 ` [PATCH 4/6] xenconsoled: delete two now unused functions Wei Liu
2016-07-08 17:01   ` Ian Jackson
2016-06-06 15:59 ` [PATCH 5/6] xenconsoled: options to control log rotation Wei Liu
2016-07-08 17:02   ` Ian Jackson
2016-06-06 15:59 ` Wei Liu [this message]
2016-07-08 17:03   ` [PATCH 6/6] xenconsoled: handle --log-backups 0 in logfile_rollover Ian Jackson
2016-06-06 20:40 ` [PATCH 0/6] xenconsoled: rotating log file abstration Doug Goldstein
2016-06-07  8:07   ` Wei Liu
2016-06-07  9:44 ` David Vrabel
2016-06-07  9:55   ` Wei Liu
2016-06-07 10:17     ` David Vrabel
2016-06-07 10:21       ` Wei Liu
2016-06-07 10:29         ` David Vrabel
2016-06-07 10:34           ` Wei Liu
2016-06-07 10:35             ` David Vrabel
2016-06-07 10:43               ` Wei Liu
2016-07-02 10:24 ` Wei Liu
2016-07-04 17:07   ` Ian Jackson

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=1465228781-22754-7-git-send-email-wei.liu2@citrix.com \
    --to=wei.liu2@citrix.com \
    --cc=Ian.Jackson@eu.citrix.com \
    --cc=xen-devel@lists.xenproject.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 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).