git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Recommendations for adding strerror() in log statament

From
牛牛旭 <niuxu16@nudt.edu.cn>
Date
Oct 17, 2018, 08:34 UTC
Message-ID
<4413c65c.101fa.16681291a04.Coremail.niuxu16@nudt.edu.cn>

Hi, Our team works on enhance logging practices by learning from historical log revisions in evolution. And we find 2 patches that add strerror() to the log statement which is printed when the return value of commit_lock_file() is smaller than 0.

While applying this rule to git-2.14.2, we find 3 missed spots. We suggest to add strerror() or just use error_errno() instead of error().

Here are the missed spots:
1) Line 307 in file git-2.14.2/sequencer.c:
static int write_message(const void *buf, size_t len, const char *filename,
			 int append_eol)
{
	...
	if (append_eol && write(msg_fd, "\n", 1) < 0) {
		rollback_lock_file(&msg_file);
		return error_errno(_("could not write eol to '%s'"), filename);
	}
	if (commit_lock_file(&msg_file) < 0) {
		rollback_lock_file(&msg_file);
		return error(_("failed to finalize '%s'."), filename);
	}
2) Line 1582 in file git-2.14.2/sequencer.c:
static int save_head(const char *head)
{
	...
	strbuf_addf(&buf, "%s\n", head);
	if (write_in_full(fd, buf.buf, buf.len) < 0) {
		rollback_lock_file(&head_lock);
		return error_errno(_("could not write to '%s'"),
				   git_path_head_file());
	}
	if (commit_lock_file(&head_lock) < 0) {
		rollback_lock_file(&head_lock);
		return error(_("failed to finalize '%s'."), git_path_head_file());
	}
3) Line 1706 in file git-2.14.2/sequencer.c:
static int save_todo(struct todo_list *todo_list, struct replay_opts *opts)
{
	...
	if (write_in_full(fd, todo_list->buf.buf + offset,
			todo_list->buf.len - offset) < 0)
		return error_errno(_("could not write to '%s'"), todo_path);
	if (commit_lock_file(&todo_lock) < 0)
		return error(_("failed to finalize '%s'."), todo_path);
Following are the 2 patches that support our opinion:
1) Line 2147 in file git-2.6.4/config.c:
 	if (commit_lock_file(lock) < 0) {
-		error("could not commit config file %s", config_filename);
+		error("could not write config file %s: %s", config_filename,
+		      strerror(errno));
 		ret = CONFIG_NO_WRITE;
 		lock = NULL;
 		goto out_free;
 	}
2) Line 2333 in file git-2.6.4/config.c:
 unlock_and_out:
 	if (commit_lock_file(lock) < 0)
-		ret = error("could not commit config file %s", config_filename);
+		ret = error("could not write config file %s: %s",
+			    config_filename, strerror(errno));
 out:
 	free(filename_buf);

Thanks for reading, we are looking forward to your reply about the correctness of our suggestion~ May you a good day ^^

Best regards, Xu

Message 1 of 1 in “Recommendations for adding strerror() in log statament”
  1. 牛旭Oct 17, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.