Common logic of mkpath() and mkpathdup() are collected into a
new do_mkpath(). Then, based on do_mkpath(), strbuf_mkpath() is
implemented.
Signed-off-by: Hui Yiqun <redacted>
---
cache.h | 2 ++
path.c | 21 +++++++++++++++++----
2 files changed, 19 insertions(+), 4 deletions(-)
this function does the following:
1. if $XDG_RUNTIME_DIR is non-empty, `$XDG_RUNTIME_DIR/git` is used in next
step, otherwise `/tmp/git-$uid` is taken.
2. ensure that above directory does exist. what's more, it must has correct
permission and ownership.
3. a newly allocated string consisting of the path of above directory and
$filename is returned.
Under following situation, NULL will be returned:
+ the directory mentioned in step 1 exists but have wrong permission or
ownership.
+ the directory or its parent cannot be created.
Notice:
+ the caller is responsible for deallocating the returned string.
Signed-off-by: Hui Yiqun <redacted>
---
cache.h | 23 +++++++++++++++++++++++
path.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 79 insertions(+)
@@ -1206,6 +1207,61 @@ char *xdg_config_home(const char *filename)returnNULL;}+char*xdg_runtime_dir(constchar*filename)+{+structstrbufsb=STRBUF_INIT;+char*runtime_dir;+structstatst;+uid_tuid=getuid();++assert(filename);+runtime_dir=getenv("XDG_RUNTIME_DIR");+if(runtime_dir&&*runtime_dir)+strbuf_mkpath(&sb,"%s/git/",runtime_dir);+else+strbuf_mkpath(&sb,"/tmp/git-%d",uid);++if(!lstat(sb.buf,&st)){+/*+*AsdescribedinXDGbasedirspec[1],thesubdirectory+*under$XDG_RUNTIME_DIRoritsfallbackMUSTbeownedby+*theuser,anditsunixaccessmodeMUSTbe0700.+*+*Callingchmodorchownsilentlymaycausesecurity+*problemifsomebodychdirtoit,sleep,andthen,try+*toopenourprotectedruntimecacheorsocket.+*Sowejustputwarningandleftittousertosolve.+*+*[1]https://specifications.freedesktop.org/basedir-spec/+*basedir-spec-latest.html+*/+if((st.st_mode&0777)!=S_IRWXU){+warning("permission of runtime directory '%s' "+"MUST be 0700 instead of 0%o\n",+sb.buf,(st.st_mode&0777));+returnNULL;+}elseif(st.st_uid!=uid){+warning("owner of runtime directory '%s' "+"MUST be %d instead of %d\n",+sb.buf,uid,st.st_uid);+returnNULL;+}+/* TODO: check whether st.buf is an directory */+}else{+if(safe_create_leading_directories_const(sb.buf)<0){+warning("unable to create directories for '%s'\n",+sb.buf);+returnNULL;+}+if(mkdir(sb.buf,0700)<0){+warning("unable to mkdir '%s'\n",sb.buf);+returnNULL;+}+}+strbuf_addf(&sb,"/%s",filename);+returnstrbuf_detach(&sb,NULL);+}+GIT_PATH_FUNC(git_path_cherry_pick_head,"CHERRY_PICK_HEAD")GIT_PATH_FUNC(git_path_revert_head,"REVERT_HEAD")GIT_PATH_FUNC(git_path_squash_msg,"SQUASH_MSG")
@@ -105,7 +105,7 @@ int main(int argc, const char **argv)op=argv[0];if(!socket_path)-socket_path=expand_user_path("~/.git-credential-cache/socket");+socket_path=xdg_runtime_dir("credential-cache.sock");if(!socket_path)die("unable to find a suitable socket path; use --socket");
@@ -12,7 +12,34 @@ test -z "$NO_UNIX_SOCKETS" || {# don't leave a stale daemon runningtrap'code=$?; git credential-cache exit; (exit $code); die'EXIT+test_expect_success'set $XDG_RUNTIME_DIR''+XDG_RUNTIME_DIR=$HOME/xdg_runtime/+'++helper_testcache++test_expect_success'when $XDG_RUNTIME_DIR is set, `$XDG_RUNTIME_DIR/git` are used''+test_path_is_missing"/tmp/git-$(id-u)/git/credential-cache.sock"&&+test-S"$HOME/xdg_runtime/git/credential-cache.sock"+'++test_expect_success'force git-credential-cache to exit so that socket disappear''+gitcredential-cacheexit&&+test_path_is_missing"$XDG_RUNTIME_DIR/git/credential-cache.sock"&&+unsetXDG_RUNTIME_DIR+'+ helper_testcache++test_expect_success'when $XDG_RUNTIME_DIR is not set, `/tmp/git-$(id -u) is used''+test-S"/tmp/git-$(id-u)/credential-cache.sock"+'++# TODO: if $XDG_RUNTIME_DIR/git/ exists, but has wrong permission and ownership,+# `helper_test cache` must fail.++# TODO: check whether `--socket` works+ helper_test_timeoutcache--timeout=1# we can't rely on our "trap" above working after test_done,
From: Jeff King <hidden> Date: 2016-06-15 23:08:47
On Fri, Mar 18, 2016 at 12:48:44AM +0800, Hui Yiqun wrote:
this function does the following:
1. if $XDG_RUNTIME_DIR is non-empty, `$XDG_RUNTIME_DIR/git` is used in next
step, otherwise `/tmp/git-$uid` is taken.
2. ensure that above directory does exist. what's more, it must has correct
permission and ownership.
3. a newly allocated string consisting of the path of above directory and
$filename is returned.
Under following situation, NULL will be returned:
+ the directory mentioned in step 1 exists but have wrong permission or
ownership.
+ the directory or its parent cannot be created.
Notice:
+ the caller is responsible for deallocating the returned string.
I see a lot of "what" in your commit message (and in the other ones in
this series), but not a lot of "why".
We can see the "what" from the diff already (though it is certainly OK
to point out tricky parts). But you probably want to explain the
motivation for things, alternatives considered, etc, like:
- why is using $XDG_RUNTIME_DIR a good thing?
- why did you choose to fall back to /tmp (as opposed to, say,
returning NULL and letting the caller handle it)
- what is the purpose of the ownership/permission rules? You link to
the XDG spec in an in-code comment, but IMHO that kind of motivation
probably makes more sense in the commit message.
-Peff
From: Jeff King <hidden> Date: 2016-06-15 23:08:48
On Fri, Mar 18, 2016 at 12:38:16PM +0800, 惠轶群 wrote:
quoted
quoted
+test_expect_success 'when $XDG_RUNTIME_DIR is set, `$XDG_RUNTIME_DIR/git` are used' '
+ test_path_is_missing "/tmp/git-$(id -u)/git/credential-cache.sock" &&
+ test -S "$HOME/xdg_runtime/git/credential-cache.sock"
+'
This test fails for me, probably because XDG_RUNTIME_DIR is not
exported.
-Peff
Could you please give a try to the patch set v2, test of which is
definitely passed on my computer.
Yes, that was what I tried earlier (and I just re-tested to make sure).
It still fails. I suspect it has to do with whether XDG_RUNTIME_DIR is
set in our environments (outside of the test suite). If I run:
XDG_RUNTIME_DIR=/tmp/foo ./t0301-credential-cache.sh
it passes.
-Peff
2016-03-18 13:01 GMT+08:00 Jeff King [off-list ref]:
On Fri, Mar 18, 2016 at 12:38:16PM +0800, 惠轶群 wrote:
quoted
quoted
quoted
+test_expect_success 'when $XDG_RUNTIME_DIR is set, `$XDG_RUNTIME_DIR/git` are used' '
+ test_path_is_missing "/tmp/git-$(id -u)/git/credential-cache.sock" &&
+ test -S "$HOME/xdg_runtime/git/credential-cache.sock"
+'
This test fails for me, probably because XDG_RUNTIME_DIR is not
exported.
-Peff
Could you please give a try to the patch set v2, test of which is
definitely passed on my computer.
Yes, that was what I tried earlier (and I just re-tested to make sure).
It still fails. I suspect it has to do with whether XDG_RUNTIME_DIR is
set in our environments (outside of the test suite). If I run:
XDG_RUNTIME_DIR=/tmp/foo ./t0301-credential-cache.sh
it passes.
-Peff
Sorry, I wrongly considered your comment is for my last commit. That
was partly because my thread is
disturbed by my commits in my mailbox(gmail). I hope that did not also
disturb yours.
I sent my commits with `--thread` and `--no-chain-reply-to` in hope
that I would send an summary as
[PATCH 0/x] and all my commits would be sent in reply to it. But I failed.