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

[PATCH v2 05/26] copy.c: convert bb_(p)error_msg to (sys_)error

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Feb 16, 2016, 13:29 UTC
Message-ID
<1455629367-26193-6-git-send-email-pclouds@gmail.com>
In-Reply-To
<1455629367-26193-1-git-send-email-pclouds@gmail.com>
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 copy.c | 85 ++++++++++++++++++++++++------------------------------------------
 1 file changed, 31 insertions(+), 54 deletions(-)
diff --git a/copy.c b/copy.c
index d24a8ae..cdb38d5 100644
--- a/copy.c
+++ b/copy.c
@@ -82,23 +82,16 @@ int FAST_FUNC copy_file(const char *source, const char *dest, int flags)
 	smallint dest_exists = 0;
 	smallint ovr;
 
-	if (lstat(source, &source_stat) < 0) {
-		bb_perror_msg("can't stat '%s'", source);
-		return -1;
-	}
+	if (lstat(source, &source_stat) < 0)
+		return sys_error(_("can't stat '%s'"), source);
 
 	if (lstat(dest, &dest_stat) < 0) {
-		if (errno != ENOENT) {
-			bb_perror_msg("can't stat '%s'", dest);
-			return -1;
-		}
+		if (errno != ENOENT)
+			return sys_error(_("can't stat '%s'"), dest);
 	} else {
-		if (source_stat.st_dev == dest_stat.st_dev
-		 && source_stat.st_ino == dest_stat.st_ino
-		) {
-			bb_error_msg("'%s' and '%s' are the same file", source, dest);
-			return -1;
-		}
+		if (source_stat.st_dev == dest_stat.st_dev &&
+		    source_stat.st_ino == dest_stat.st_ino)
+			return error(_("'%s' and '%s' are the same file"), source, dest);
 		dest_exists = 1;
 	}
 
@@ -110,18 +103,14 @@ int FAST_FUNC copy_file(const char *source, const char *dest, int flags)
 
 		/* Did we ever create source ourself before? */
 		tp = is_in_ino_dev_hashtable(&source_stat);
-		if (tp) {
+		if (tp)
 			/* We did! it's a recursion! man the lifeboats... */
-			bb_error_msg("recursion detected, omitting directory '%s'",
-					source);
-			return -1;
-		}
+			return error(_("recursion detected, omitting directory '%s'"),
+				     source);
 
 		if (dest_exists) {
-			if (!S_ISDIR(dest_stat.st_mode)) {
-				bb_error_msg("target '%s' is not a directory", dest);
-				return -1;
-			}
+			if (!S_ISDIR(dest_stat.st_mode))
+				return error(_("target '%s' is not a directory"), dest);
 			/* race here: user can substitute a symlink between
 			 * this check and actual creation of files inside dest */
 		} else {
@@ -134,15 +123,12 @@ int FAST_FUNC copy_file(const char *source, const char *dest, int flags)
 			mode |= S_IRWXU;
 			if (mkdir(dest, mode) < 0) {
 				umask(saved_umask);
-				bb_perror_msg("can't create directory '%s'", dest);
-				return -1;
+				return sys_error(_("can't create directory '%s'"), dest);
 			}
 			umask(saved_umask);
 			/* need stat info for add_to_ino_dev_hashtable */
-			if (lstat(dest, &dest_stat) < 0) {
-				bb_perror_msg("can't stat '%s'", dest);
-				return -1;
-			}
+			if (lstat(dest, &dest_stat) < 0)
+				return sys_error(_("can't stat '%s'"), dest);
 		}
 		/* remember (dev,inode) of each created dir.
 		 * NULL: name is not remembered */
@@ -172,7 +158,7 @@ int FAST_FUNC copy_file(const char *source, const char *dest, int flags)
 		if (!dest_exists
 		 && chmod(dest, source_stat.st_mode & ~saved_umask) < 0
 		) {
-			bb_perror_msg("can't preserve %s of '%s'", "permissions", dest);
+			sys_error(_("can't preserve permissions of '%s'"), dest);
 			/* retval = -1; - WRONG! copy *WAS* made */
 		}
 		goto preserve_mode_ugid_time;
@@ -196,10 +182,8 @@ int FAST_FUNC copy_file(const char *source, const char *dest, int flags)
 					ovr = ask_and_unlink(dest, flags);
 					if (ovr <= 0)
 						return ovr;
-					if (link(link_target, dest) < 0) {
-						bb_perror_msg("can't create link '%s'", dest);
-						return -1;
-					}
+					if (link(link_target, dest) < 0)
+						return sys_error(_("can't create link '%s'"), dest);
 				}
 				return 0;
 			}
@@ -238,10 +222,8 @@ int FAST_FUNC copy_file(const char *source, const char *dest, int flags)
 		if (bb_copyfd_eof(src_fd, dst_fd) == -1)
 			retval = -1;
 		/* Careful with writing... */
-		if (close(dst_fd) < 0) {
-			bb_perror_msg("error writing to '%s'", dest);
-			retval = -1;
-		}
+		if (close(dst_fd) < 0)
+			retval = sys_error(_("error writing to '%s'"), dest);
 		/* ...but read size is already checked by bb_copyfd_eof */
 		close(src_fd);
 		/* "cp /dev/something new_file" should not
@@ -265,12 +247,10 @@ int FAST_FUNC copy_file(const char *source, const char *dest, int flags)
 		if (lpath) {
 			int r = symlink(lpath, dest);
 			free(lpath);
-			if (r < 0) {
-				bb_perror_msg("can't create symlink '%s'", dest);
-				return -1;
-			}
+			if (r < 0)
+				return sys_error(_("can't create symlink '%s'"), dest);
 			if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0)
-				bb_perror_msg("can't preserve %s of '%s'", "ownership", dest);
+				sys_error(_("can't preserve %s of '%s'"), "ownership", dest);
 		}
 		/* _Not_ jumping to preserve_mode_ugid_time:
 		 * symlinks don't have those */
@@ -279,14 +259,11 @@ int FAST_FUNC copy_file(const char *source, const char *dest, int flags)
 	if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode)
 	 || S_ISSOCK(source_stat.st_mode) || S_ISFIFO(source_stat.st_mode)
 	) {
-		if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) {
-			bb_perror_msg("can't create '%s'", dest);
-			return -1;
-		}
-	} else {
-		bb_error_msg("unrecognized file '%s' with mode %x", source, source_stat.st_mode);
-		return -1;
-	}
+		if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0)
+			return sys_error(_("can't create '%s'"), dest);
+	} else
+		return error(_("unrecognized file '%s' with mode %x"),
+			     source, source_stat.st_mode);
 
  preserve_mode_ugid_time:
 
@@ -297,13 +274,13 @@ int FAST_FUNC copy_file(const char *source, const char *dest, int flags)
 		times[1].tv_usec = times[0].tv_usec = 0;
 		/* BTW, utimes sets usec-precision time - just FYI */
 		if (utimes(dest, times) < 0)
-			bb_perror_msg("can't preserve %s of '%s'", "times", dest);
+			sys_error(_("can't preserve %s of '%s'"), "times", dest);
 		if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) {
 			source_stat.st_mode &= ~(S_ISUID | S_ISGID);
-			bb_perror_msg("can't preserve %s of '%s'", "ownership", dest);
+			sys_error(_("can't preserve %s of '%s'"), "ownership", dest);
 		}
 		if (chmod(dest, source_stat.st_mode) < 0)
-			bb_perror_msg("can't preserve %s of '%s'", "permissions", dest);
+			sys_error(_("can't preserve %s of '%s'"), "permissions", dest);
 	}
 
 	return retval;
-- 
2.7.0.377.g4cd97dd
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 27 of 50 in “"git worktree move" preview”
  1. 00/20 "git worktree move" previewNguyễn Thái Ngọc Duy, Feb 3, 2016
  2. 01/20 usage.c: move format processing out of die_errno()Nguyễn Thái Ngọc Duy, Feb 3, 2016
  3. 02/20 usage.c: add sys_error() that prints strerror() automaticallyNguyễn Thái Ngọc Duy, Feb 3, 2016
  4. 03/20 path.c: add git_common_path() and strbuf_git_common_path()Nguyễn Thái Ngọc Duy, Feb 3, 2016
  5. 04/20 path.c: add is_git_path_shared()Nguyễn Thái Ngọc Duy, Feb 3, 2016
  6. 05/20 copy.c: add copy_dir_recursively()Nguyễn Thái Ngọc Duy, Feb 3, 2016
  7. 06/20 worktree.c: use is_dot_or_dotdot()Nguyễn Thái Ngọc Duy, Feb 3, 2016
  8. 07/20 worktree.c: store "id" instead of "git_dir"Nguyễn Thái Ngọc Duy, Feb 3, 2016
  9. 08/20 worktree.c: add clear_worktree()Nguyễn Thái Ngọc Duy, Feb 3, 2016
  10. 09/20 worktree.c: add find_worktree_by_path()Nguyễn Thái Ngọc Duy, Feb 3, 2016
  11. 10/20 worktree.c: add is_main_worktree()Nguyễn Thái Ngọc Duy, Feb 3, 2016
  12. 11/20 worktree.c: recognize no main worktreeNguyễn Thái Ngọc Duy, Feb 3, 2016
  13. 12/20 worktree.c: add update_worktree_location()Nguyễn Thái Ngọc Duy, Feb 3, 2016
  14. 13/20 worktree.c: add update_worktree_gitfile()Nguyễn Thái Ngọc Duy, Feb 3, 2016
  15. 14/20 worktree.c: add collect_per_worktree_git_paths()Nguyễn Thái Ngọc Duy, Feb 3, 2016
  16. 15/20 worktree: avoid 0{40}, too many zeroes, hard to readNguyễn Thái Ngọc Duy, Feb 3, 2016
  17. 16/20 worktree: simplify prefixing pathsNguyễn Thái Ngọc Duy, Feb 3, 2016
  18. 17/20 worktree: add "move" commmandNguyễn Thái Ngọc Duy, Feb 3, 2016
  19. 18/20 worktree: refactor add_worktree()Nguyễn Thái Ngọc Duy, Feb 3, 2016
  20. 19/20 worktree: move repo, simple caseNguyễn Thái Ngọc Duy, Feb 3, 2016
  21. 20/20 worktree: move repo, convert main worktreeNguyễn Thái Ngọc Duy, Feb 3, 2016
  22. 00/26 worktree lock, move, remove and unlockNguyễn Thái Ngọc Duy, Feb 16, 2016
  23. 01/26 usage.c: move format processing out of die_errno()Nguyễn Thái Ngọc Duy, Feb 16, 2016
  24. 02/26 usage.c: add sys_error() that prints strerror() automaticallyNguyễn Thái Ngọc Duy, Feb 16, 2016
  25. 03/26 copy.c: import copy_file() from busyboxNguyễn Thái Ngọc Duy, Feb 16, 2016
  26. 04/26 copy.c: delete unused code in copy_file()Nguyễn Thái Ngọc Duy, Feb 16, 2016
  27. 05/26 copy.c: convert bb_(p)error_msg to (sys_)errorNguyễn Thái Ngọc Duy, Feb 16, 2016
  28. 06/26 copy.c: style fixNguyễn Thái Ngọc Duy, Feb 16, 2016
  29. 07/26 copy.c: convert copy_file() to copy_dir_recursively()Nguyễn Thái Ngọc Duy, Feb 16, 2016
  30. 08/26 completion: support git-worktreeNguyễn Thái Ngọc Duy, Feb 16, 2016
  31. 09/26 git-worktree.txt: keep subcommand listing in alphabetical orderNguyễn Thái Ngọc Duy, Feb 16, 2016
  32. 10/26 wrapper.c: allow to create an empty file with write_file()Nguyễn Thái Ngọc Duy, Feb 16, 2016
  33. Junio C HamanoFeb 17, 2016
  34. Duy NguyenFeb 18, 2016
  35. 11/26 path.c: add git_common_path() and strbuf_git_common_path()Nguyễn Thái Ngọc Duy, Feb 16, 2016
  36. 12/26 worktree.c: use is_dot_or_dotdot()Nguyễn Thái Ngọc Duy, Feb 16, 2016
  37. 13/26 worktree.c: store "id" instead of "git_dir"Nguyễn Thái Ngọc Duy, Feb 16, 2016
  38. 14/26 worktree.c: add clear_worktree()Nguyễn Thái Ngọc Duy, Feb 16, 2016
  39. 15/26 worktree.c: add find_worktree_by_path()Nguyễn Thái Ngọc Duy, Feb 16, 2016
  40. 16/26 worktree.c: add is_main_worktree()Nguyễn Thái Ngọc Duy, Feb 16, 2016
  41. 17/26 worktree.c: add validate_worktree()Nguyễn Thái Ngọc Duy, Feb 16, 2016
  42. 18/26 worktree.c: add update_worktree_location()Nguyễn Thái Ngọc Duy, Feb 16, 2016
  43. 19/26 worktree.c: add is_worktree_locked()Nguyễn Thái Ngọc Duy, Feb 16, 2016
  44. 20/26 worktree: avoid 0{40}, too many zeroes, hard to readNguyễn Thái Ngọc Duy, Feb 16, 2016
  45. 21/26 worktree: simplify prefixing pathsNguyễn Thái Ngọc Duy, Feb 16, 2016
  46. 22/26 worktree: add "lock" commandNguyễn Thái Ngọc Duy, Feb 16, 2016
  47. 23/26 worktree: add "unlock" commandNguyễn Thái Ngọc Duy, Feb 16, 2016
  48. 24/26 worktree: add "move" commmandNguyễn Thái Ngọc Duy, Feb 16, 2016
  49. 25/26 worktree move: accept destination as directoryNguyễn Thái Ngọc Duy, Feb 16, 2016
  50. 26/26 worktree: add "remove" commandNguyễn Thái Ngọc Duy, Feb 16, 2016

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.