[PATCH 13 of 14] chg: extra the logic opening current directory to opencwdx()

Jun Wu quark at fb.com
Sun Apr 10 19:57:30 EDT 2016


# HG changeset patch
# User Jun Wu <quark at fb.com>
# Date 1460332228 -3600
#      Mon Apr 11 00:50:28 2016 +0100
# Node ID 58ea17bb6caad0e680f23473b37f68c03c546146
# Parent  a4651e43ceb70c27db15ebe43950e9a788192096
chg: extra the logic opening current directory to opencwdx()

We have 2 places opening current directory and will have another one. Move
the logic to a standalone utility function.

diff --git a/contrib/chg/chg.c b/contrib/chg/chg.c
--- a/contrib/chg/chg.c
+++ b/contrib/chg/chg.c
@@ -251,9 +251,7 @@
 		return name;
 
 	static char path[PATH_MAX];
-	int cwdfd = open(".", O_DIRECTORY);
-	if (cwdfd == -1)
-		abortmsgerrno("failed to open current directory");
+	int cwdfd = opencwdx();
 	fchdirx(dirfd);
 	if (getcwd(path, sizeof(path)) == NULL)
 		abortmsgerrno("failed to getcwd");
diff --git a/contrib/chg/util.c b/contrib/chg/util.c
--- a/contrib/chg/util.c
+++ b/contrib/chg/util.c
@@ -101,6 +101,14 @@
 		abortmsgerrno("cannot set flags of fd %d", fd);
 }
 
+int opencwdx()
+{
+	int cwdfd = open(".", O_DIRECTORY);
+	if (cwdfd == -1)
+		abortmsgerrno("cannot open cwd");
+	return cwdfd;
+}
+
 void *mallocx(size_t size)
 {
 	void *result = malloc(size);
@@ -193,9 +201,7 @@
 {
 	if (fd == AT_FDCWD)
 		return connect(s, addr, addrlen);
-	int cwdfd = open(".", O_DIRECTORY);
-	if (cwdfd == -1)
-		abortmsgerrno("cannot open cwd");
+	int cwdfd = opencwdx();
 	fchdirx(fd);
 	int r = connect(s, addr, addrlen);
 	fchdirx(cwdfd);
diff --git a/contrib/chg/util.h b/contrib/chg/util.h
--- a/contrib/chg/util.h
+++ b/contrib/chg/util.h
@@ -27,6 +27,7 @@
 
 void fchdirx(int dirfd);
 void fsetcloexec(int fd);
+int opencwdx();
 void *mallocx(size_t size);
 void *reallocx(void *ptr, size_t size);
 


More information about the Mercurial-devel mailing list