Patchwork [1,of,2] chg: make "get default sockdir" a separate method

login
register
mail settings
Submitter Jun Wu
Date Dec. 26, 2016, 12:04 a.m.
Message ID <92eb460b9c9af3e5a625.1482710695@x1c>
Download mbox | patch
Permalink /patch/18039/
State Accepted
Headers show

Comments

Jun Wu - Dec. 26, 2016, 12:04 a.m.
# HG changeset patch
# User Jun Wu <quark@fb.com>
# Date 1482709794 0
#      Sun Dec 25 23:49:54 2016 +0000
# Node ID 92eb460b9c9af3e5a6251ed6b3924824030de906
# Parent  2af8d24aadfd34dc46ef8bf122dc8fa7654048fd
# Available At https://bitbucket.org/quark-zju/hg-draft
#              hg pull https://bitbucket.org/quark-zju/hg-draft -r 92eb460b9c9a
chg: make "get default sockdir" a separate method

The logic to get a default socket directory will become longer in the next
patch. So let's move it out.
Yuya Nishihara - Dec. 26, 2016, 1:29 p.m.
On Mon, 26 Dec 2016 00:04:55 +0000, Jun Wu wrote:
> # HG changeset patch
> # User Jun Wu <quark@fb.com>
> # Date 1482709794 0
> #      Sun Dec 25 23:49:54 2016 +0000
> # Node ID 92eb460b9c9af3e5a6251ed6b3924824030de906
> # Parent  2af8d24aadfd34dc46ef8bf122dc8fa7654048fd
> # Available At https://bitbucket.org/quark-zju/hg-draft
> #              hg pull https://bitbucket.org/quark-zju/hg-draft -r 92eb460b9c9a
> chg: make "get default sockdir" a separate method

Nice, queued, thanks.

Patch

diff --git a/contrib/chg/chg.c b/contrib/chg/chg.c
--- a/contrib/chg/chg.c
+++ b/contrib/chg/chg.c
@@ -128,4 +128,15 @@  static void preparesockdir(const char *s
 }
 
+static void getdefaultsockdir(char sockdir[], size_t size) {
+	/* by default, put socket file in secure directory
+	 * (permission of socket file may be ignored on some Unices) */
+	const char *tmpdir = getenv("TMPDIR");
+	if (!tmpdir)
+		tmpdir = "/tmp";
+	int r = snprintf(sockdir, size, "%s/chg%d", tmpdir, geteuid());
+	if (r < 0 || (size_t)r >= size)
+		abortmsg("too long TMPDIR (r = %d)", r);
+}
+
 static void setcmdserveropts(struct cmdserveropts *opts)
 {
@@ -134,13 +145,5 @@  static void setcmdserveropts(struct cmds
 	const char *envsockname = getenv("CHGSOCKNAME");
 	if (!envsockname) {
-		/* by default, put socket file in secure directory
-		 * (permission of socket file may be ignored on some Unices) */
-		const char *tmpdir = getenv("TMPDIR");
-		if (!tmpdir)
-			tmpdir = "/tmp";
-		r = snprintf(sockdir, sizeof(sockdir), "%s/chg%d",
-			     tmpdir, geteuid());
-		if (r < 0 || (size_t)r >= sizeof(sockdir))
-			abortmsg("too long TMPDIR (r = %d)", r);
+		getdefaultsockdir(sockdir, sizeof(sockdir));
 		preparesockdir(sockdir);
 	}