[PATCH 06 of 14] chg: use full path as --address to start a new server

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


# HG changeset patch
# User Jun Wu <quark at fb.com>
# Date 1460327827 -3600
#      Sun Apr 10 23:37:07 2016 +0100
# Node ID a0233eac2848926116ecbdb4bf44fd5f066d7f1d
# Parent  8dcda8964985a6e1826e6ceb8e7f476a66b8a3f9
chg: use full path as --address to start a new server

This is a part of the series to support long socket path. We will change
sockname to basename instead of full path soon.

To make sure the server creates the socket file in a correct location,
we have to pass the full path because we may pass "--cwd" as well and
a "chdir" just before starting the server doesn't work with "--cwd".

This patch uses getcwd to resolve the dir fd and get the full path used
for the server.

diff --git a/contrib/chg/chg.c b/contrib/chg/chg.c
--- a/contrib/chg/chg.c
+++ b/contrib/chg/chg.c
@@ -215,6 +215,27 @@
 	return hgcmd;
 }
 
+static const char *getfullpath(int dirfd, const char *name)
+{
+	if (name[0] == '/')
+		return name;
+
+	static char path[PATH_MAX];
+	int cwdfd = open(".", O_DIRECTORY);
+	if (cwdfd == -1)
+		abortmsgerrno("failed to open current directory");
+	fchdirx(dirfd);
+	if (getcwd(path, sizeof(path)) == NULL)
+		abortmsgerrno("failed to getcwd");
+	size_t len = strlen(path);
+	assert(len < sizeof(path));
+	int r = snprintf(path + len, sizeof(path) - len, "/%s", name);
+	if ((size_t)r >= sizeof(path))
+		abortmsg("full socket path is too long");
+	fchdirx(cwdfd);
+	return path;
+}
+
 static void execcmdserver(const struct cmdserveropts *opts)
 {
 	const char *hgcmd = gethgcmd();
@@ -223,7 +244,7 @@
 		hgcmd,
 		"serve",
 		"--cmdserver", "chgunix",
-		"--address", opts->sockname,
+		"--address", getfullpath(opts->sockdirfd, opts->sockname),
 		"--daemon-postexec", "chdir:/",
 		"--config", "extensions.chgserver=",
 	};


More information about the Mercurial-devel mailing list