Re: [PATCH] Extend runtime prefix computation

2016-04-20 Thread Michael Weiser
Hi,

On Fri, Apr 15, 2016 at 09:43:29AM -0700, Junio C Hamano wrote:

> We tend to avoid system specific includes in individual *.c files.

> Perhaps implement platform specific bits in compat/?  E.g. each
> platform specific file in compat/ may implement and export the same
> git_extract_argv_path() function, perhaps, so that this file does
> not even need to know what platforms it supports?

On Mon, Apr 18, 2016 at 09:20:25AM +0200, Johannes Schindelin wrote:

> I have to admit that I am really, *really* skeptical. To me, it looks like
> this patch opens the door very wide to unintended consequences.

To make it short: I had received interest from another user for
relocatable git installations and an updated patch against current git
(based on my last post of the code to this list in 2012). This
confirmed to me that the use case is still valid. However, I do not see
myself in a position to iterate the code through the review and
improvement process that starting to unfold now. So if someone sometime
picks this up and carries on, I'd be happy. Otherwise please disregard.
-- 
Thanks,
Michael
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH] Extend runtime prefix computation

2016-04-18 Thread Johannes Schindelin
Hi Junio,

On Fri, 15 Apr 2016, Junio C Hamano wrote:

> Michael Weiser  writes:
> 
> > Make git fully relocatable at runtime extending the runtime prefix
> > calculation. Handle absolute and relative paths in argv0. Handle no path
> > at all in argv0 in a system-specific manner.  Replace assertions with
> > initialised variables and checks that lead to fallback to the static
> > prefix.
> 
> That's a dense description of "what" without saying much about
> "why".  Hint: start by describing what case(s) the current code
> fails to find the correct runtime prefix.  That would give readers a
> better understanding of what problem you are trying to solve.

I have to admit that I am really, *really* skeptical. To me, it looks like
this patch opens the door very wide to unintended consequences.

> >  #ifdef RUNTIME_PREFIX
> > -   assert(argv0_path);
> > -   assert(is_absolute_path(argv0_path));
> 
> Aren't these protecting against future and careless change that
> forgets to call extract-argv0-path or make that function return
> something that is not an absolute path?

This (first) assert() indeed saved me a couple of times from hunting for
bugs in the wrong place. Let's keep it.

Ciao,
Dscho
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH] Extend runtime prefix computation

2016-04-15 Thread Junio C Hamano
Michael Weiser  writes:

> Make git fully relocatable at runtime extending the runtime prefix
> calculation. Handle absolute and relative paths in argv0. Handle no path
> at all in argv0 in a system-specific manner.  Replace assertions with
> initialised variables and checks that lead to fallback to the static
> prefix.

That's a dense description of "what" without saying much about
"why".  Hint: start by describing what case(s) the current code
fails to find the correct runtime prefix.  That would give readers a
better understanding of what problem you are trying to solve.

Missing sign-off.

> diff --git a/exec_cmd.c b/exec_cmd.c
> index 9d5703a..f0db070 100644
> --- a/exec_cmd.c
> +++ b/exec_cmd.c
> @@ -3,30 +3,27 @@
>  #include "quote.h"
>  #include "argv-array.h"
>  #define MAX_ARGS 32
> +#if defined(__APPLE__)
> +#include 
> +#endif

We tend to avoid system specific includes in individual *.c files.

Perhaps implement platform specific bits in compat/?  E.g. each
platform specific file in compat/ may implement and export the same
git_extract_argv_path() function, perhaps, so that this file does
not even need to know what platforms it supports?

>  char *system_path(const char *path)
>  {
> -#ifdef RUNTIME_PREFIX
> - static const char *prefix;
> -#else
>   static const char *prefix = PREFIX;
> -#endif
>   struct strbuf d = STRBUF_INIT;
>  
>   if (is_absolute_path(path))
>   return xstrdup(path);
>  
>  #ifdef RUNTIME_PREFIX
> - assert(argv0_path);
> - assert(is_absolute_path(argv0_path));

Aren't these protecting against future and careless change that
forgets to call extract-argv0-path or make that function return
something that is not an absolute path?

Is it a good idea to drop these checks, and if so why?

> - if (!prefix &&
> - !(prefix = strip_path_suffix(argv0_path, GIT_EXEC_PATH)) &&
> - !(prefix = strip_path_suffix(argv0_path, BINDIR)) &&
> - !(prefix = strip_path_suffix(argv0_path, "git"))) {
> + if (!argv0_path ||
> + !is_absolute_path(argv0_path) ||
> + (!(prefix = strip_path_suffix(argv0_path, GIT_EXEC_PATH)) &&
> +  !(prefix = strip_path_suffix(argv0_path, BINDIR)) &&
> +  !(prefix = strip_path_suffix(argv0_path, "git" {
>   prefix = PREFIX;
>   trace_printf("RUNTIME_PREFIX requested, "
>   "but prefix computation failed.  "
> @@ -41,6 +38,8 @@ char *system_path(const char *path)
>  const char *git_extract_argv0_path(const char *argv0)
>  {
>   const char *slash;
> + char *to_resolve = NULL;
> + const char *resolved;
>  
>   if (!argv0 || !*argv0)
>   return NULL;
> @@ -48,11 +47,56 @@ const char *git_extract_argv0_path(const char *argv0)
>   slash = find_last_dir_sep(argv0);
>  
>   if (slash) {
> - argv0_path = xstrndup(argv0, slash - argv0);
> - return slash + 1;
> + /* ... it's either an absolute path */
> + if (is_absolute_path(argv0)) {
> + argv0_path = xstrndup(argv0, slash - argv0);
> + return slash + 1;
> + }
> +
> + /* ... or a relative path, in which case we have to make it
> +  * absolute first and do the whole thing again */
> + to_resolve = xstrdup(argv0);
> + } else {
> + /* argv0 is no path at all, just a name. Resolve it into a
> +  * path. Unfortunately, this gets system specific. */
> +#if defined(__linux__)
> + struct stat st;
> + if (!stat("/proc/self/exe", ))
> + to_resolve = xstrdup("/proc/self/exe");
> +#elif defined(__APPLE__)
> + /* call with len == 0 queries the necessary buffer size */
> + uint32_t len = 0;
> + if(_NSGetExecutablePath(NULL, ) != 0) {
> + to_resolve = malloc(len);
> + if (to_resolve) {
> + /* get the executable path now we have a buffer
> +  * of proper size */
> + if(_NSGetExecutablePath(to_resolve, ) != 0) 
> {
> + free(to_resolve);
> + return argv0;
> + }
> + }
> + }
> +#endif
> +
> + /* if to_resolve is still NULL here, something failed above or
> +  * we are on an unsupported system. system_path() will warn
> +  * and fall back to the static prefix */
> + if (!to_resolve)
> + return argv0;
>   }
>  
> - return argv0;
> + /* resolve path from absolute to canonical */
> + resolved = real_path(to_resolve);
> + free(to_resolve);
> +
> + slash = find_last_dir_sep(resolved);
> + if (!slash)
> + return argv0;
> +
> + argv0_path = 

[PATCH] Extend runtime prefix computation

2016-04-15 Thread Michael Weiser
Make git fully relocatable at runtime extending the runtime prefix
calculation. Handle absolute and relative paths in argv0. Handle no path
at all in argv0 in a system-specific manner.  Replace assertions with
initialised variables and checks that lead to fallback to the static
prefix.
---

Notes:
Tested-by: David Abdurachmanov 
Pull-Request: https://github.com/git/git/pull/224

 exec_cmd.c | 72 ++
 1 file changed, 58 insertions(+), 14 deletions(-)

diff --git a/exec_cmd.c b/exec_cmd.c
index 9d5703a..f0db070 100644
--- a/exec_cmd.c
+++ b/exec_cmd.c
@@ -3,30 +3,27 @@
 #include "quote.h"
 #include "argv-array.h"
 #define MAX_ARGS   32
+#if defined(__APPLE__)
+#include 
+#endif
 
 static const char *argv_exec_path;
 static const char *argv0_path;
 
 char *system_path(const char *path)
 {
-#ifdef RUNTIME_PREFIX
-   static const char *prefix;
-#else
static const char *prefix = PREFIX;
-#endif
struct strbuf d = STRBUF_INIT;
 
if (is_absolute_path(path))
return xstrdup(path);
 
 #ifdef RUNTIME_PREFIX
-   assert(argv0_path);
-   assert(is_absolute_path(argv0_path));
-
-   if (!prefix &&
-   !(prefix = strip_path_suffix(argv0_path, GIT_EXEC_PATH)) &&
-   !(prefix = strip_path_suffix(argv0_path, BINDIR)) &&
-   !(prefix = strip_path_suffix(argv0_path, "git"))) {
+   if (!argv0_path ||
+   !is_absolute_path(argv0_path) ||
+   (!(prefix = strip_path_suffix(argv0_path, GIT_EXEC_PATH)) &&
+!(prefix = strip_path_suffix(argv0_path, BINDIR)) &&
+!(prefix = strip_path_suffix(argv0_path, "git" {
prefix = PREFIX;
trace_printf("RUNTIME_PREFIX requested, "
"but prefix computation failed.  "
@@ -41,6 +38,8 @@ char *system_path(const char *path)
 const char *git_extract_argv0_path(const char *argv0)
 {
const char *slash;
+   char *to_resolve = NULL;
+   const char *resolved;
 
if (!argv0 || !*argv0)
return NULL;
@@ -48,11 +47,56 @@ const char *git_extract_argv0_path(const char *argv0)
slash = find_last_dir_sep(argv0);
 
if (slash) {
-   argv0_path = xstrndup(argv0, slash - argv0);
-   return slash + 1;
+   /* ... it's either an absolute path */
+   if (is_absolute_path(argv0)) {
+   argv0_path = xstrndup(argv0, slash - argv0);
+   return slash + 1;
+   }
+
+   /* ... or a relative path, in which case we have to make it
+* absolute first and do the whole thing again */
+   to_resolve = xstrdup(argv0);
+   } else {
+   /* argv0 is no path at all, just a name. Resolve it into a
+* path. Unfortunately, this gets system specific. */
+#if defined(__linux__)
+   struct stat st;
+   if (!stat("/proc/self/exe", ))
+   to_resolve = xstrdup("/proc/self/exe");
+#elif defined(__APPLE__)
+   /* call with len == 0 queries the necessary buffer size */
+   uint32_t len = 0;
+   if(_NSGetExecutablePath(NULL, ) != 0) {
+   to_resolve = malloc(len);
+   if (to_resolve) {
+   /* get the executable path now we have a buffer
+* of proper size */
+   if(_NSGetExecutablePath(to_resolve, ) != 0) 
{
+   free(to_resolve);
+   return argv0;
+   }
+   }
+   }
+#endif
+
+   /* if to_resolve is still NULL here, something failed above or
+* we are on an unsupported system. system_path() will warn
+* and fall back to the static prefix */
+   if (!to_resolve)
+   return argv0;
}
 
-   return argv0;
+   /* resolve path from absolute to canonical */
+   resolved = real_path(to_resolve);
+   free(to_resolve);
+
+   slash = find_last_dir_sep(resolved);
+   if (!slash)
+   return argv0;
+
+   argv0_path = xstrndup(resolved, slash - resolved);
+   slash = xstrdup(slash + 1);
+   return slash;
 }
 
 void git_set_argv_exec_path(const char *exec_path)
-- 
2.6.4 (Apple Git-63)
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH] Extend runtime prefix computation

2013-10-04 Thread Michael Weiser
Hi,

On Wed, Apr 17, 2013 at 08:06:47AM +0200, Michael Weiser wrote:

Support determining the binaries' installation path at runtime even if
called without any path components (i.e. via search path).

  What's the reason you want it on other platforms?

 It's part of an in-house development toolchain featuring git that I want
 to hand to users as a binary installation for a number of platforms but
 give them the choice where to install it.

I'd still like to have this included in mainline git. What can I do to
get it there?

Thanks,
-- 
Michael Weiserscience + computing ag
Senior Systems Engineer   Geschaeftsstelle Duesseldorf
  Martinstrasse 47-55, Haus A
phone: +49 211 302 708 32 D-40223 Duesseldorf
fax:   +49 211 302 708 50 www.science-computing.de
-- 
Vorstandsvorsitzender/Chairman of the board of management:
Gerd-Lothar Leonhart
Vorstand/Board of Management:
Dr. Bernd Finkbeiner, Michael Heinrichs, 
Dr. Arno Steitz, Dr. Ingrid Zech
Vorsitzender des Aufsichtsrats/
Chairman of the Supervisory Board:
Philippe Miltin
Sitz/Registered Office: Tuebingen
Registergericht/Registration Court: Stuttgart
Registernummer/Commercial Register No.: HRB 382196

--
To unsubscribe from this list: send the line unsubscribe git in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH] Extend runtime prefix computation

2013-04-17 Thread Michael Weiser
Hello Erik,

On Tue, Apr 16, 2013 at 05:18:49PM +0200, Erik Faye-Lund wrote:

   Support determining the binaries' installation path at runtime even if
   called without any path components (i.e. via search path).
 I think the motivation for the feature in the first place is Windows,
 where the installation path isn't known at build-time. In the
 unix-world, this is generally known (/usr/bin or something like that).
 What's the reason you want it on other platforms?

It's part of an in-house development toolchain featuring git that I want
to hand to users as a binary installation for a number of platforms but
give them the choice where to install it.

Secondly, once the infrastructure is in place, it's easier to do or
enhance relocatability support for other platforms such as Windows.

Finally: Others do it. Perl's done it and I've already needed that as
well - on Mac OS X.

Thanks,
-- 
Michael Weiserscience + computing ag
Senior Systems Engineer   Geschaeftsstelle Duesseldorf
  Martinstrasse 47-55, Haus A
phone: +49 211 302 708 32 D-40223 Duesseldorf
fax:   +49 211 302 708 50 www.science-computing.de
-- 
Vorstandsvorsitzender/Chairman of the board of management:
Gerd-Lothar Leonhart
Vorstand/Board of Management:
Dr. Bernd Finkbeiner, Michael Heinrichs, 
Dr. Arno Steitz, Dr. Ingrid Zech
Vorsitzender des Aufsichtsrats/
Chairman of the Supervisory Board:
Philippe Miltin
Sitz/Registered Office: Tuebingen
Registergericht/Registration Court: Stuttgart
Registernummer/Commercial Register No.: HRB 382196

--
To unsubscribe from this list: send the line unsubscribe git in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH] Extend runtime prefix computation

2013-04-16 Thread Michael Weiser
Hello Junio,
Hello list,

On Wed, Mar 06, 2013 at 09:19:42AM +0100, Michael Weiser wrote:

   Support determining the binaries' installation path at runtime even if
   called without any path components (i.e. via search path).
  The default for any change is not to include it.  Is there any
  reason why we want this change?
 It makes a binary git installation fully relocatable. Seeing how git
 already has basic support for it I thought other people might be
 interested in this.

I am still interested in getting this accepted into git. Where do I push
to get it committed?

Thanks,
-- 
Michael Weiserscience + computing ag
Senior Systems Engineer   Geschaeftsstelle Duesseldorf
  Martinstrasse 47-55, Haus A
phone: +49 211 302 708 32 D-40223 Duesseldorf
fax:   +49 211 302 708 50 www.science-computing.de
-- 
Vorstandsvorsitzender/Chairman of the board of management:
Gerd-Lothar Leonhart
Vorstand/Board of Management:
Dr. Bernd Finkbeiner, Michael Heinrichs, 
Dr. Arno Steitz, Dr. Ingrid Zech
Vorsitzender des Aufsichtsrats/
Chairman of the Supervisory Board:
Philippe Miltin
Sitz/Registered Office: Tuebingen
Registergericht/Registration Court: Stuttgart
Registernummer/Commercial Register No.: HRB 382196

--
To unsubscribe from this list: send the line unsubscribe git in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH] Extend runtime prefix computation

2013-04-16 Thread Erik Faye-Lund
On Wed, Mar 6, 2013 at 9:19 AM, Michael Weiser
m.wei...@science-computing.de wrote:
 Hello Junio,

 On Tue, Mar 05, 2013 at 08:13:50AM -0800, Junio C Hamano wrote:

  Support determining the binaries' installation path at runtime even if
  called without any path components (i.e. via search path).

 The default for any change is not to include it.  Is there any
 reason why we want this change?

 It makes a binary git installation fully relocatable. Seeing how git
 already has basic support for it I thought other people might be
 interested in this.

I think the motivation for the feature in the first place is Windows,
where the installation path isn't known at build-time. In the
unix-world, this is generally known (/usr/bin or something like that).
What's the reason you want it on other platforms?
--
To unsubscribe from this list: send the line unsubscribe git in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH] Extend runtime prefix computation

2013-04-16 Thread Junio C Hamano
nobody writes:

 Hello Junio,
 Hello list,

 On Wed, Mar 06, 2013 at 09:19:42AM +0100, Michael Weiser wrote:

   Support determining the binaries' installation path at runtime even if
   called without any path components (i.e. via search path).
  The default for any change is not to include it.  Is there any
  reason why we want this change?
 It makes a binary git installation fully relocatable. Seeing how git
 already has basic support for it I thought other people might be
 interested in this.

 I am still interested in getting this accepted into git. Where do I push
 to get it committed?

I do not have a strong objection to what it tries to achieve, but
I'd prefer to see no #ifdef platform code in a very generic part
of the system like exec_cmd.
--
To unsubscribe from this list: send the line unsubscribe git in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH] Extend runtime prefix computation

2013-03-06 Thread Michael Weiser
Hello Junio,

On Tue, Mar 05, 2013 at 08:13:50AM -0800, Junio C Hamano wrote:

  Support determining the binaries' installation path at runtime even if
  called without any path components (i.e. via search path).

 The default for any change is not to include it.  Is there any
 reason why we want this change?

It makes a binary git installation fully relocatable. Seeing how git
already has basic support for it I thought other people might be
interested in this.

Thanks,
-- 
Michael Weiserscience + computing ag
Senior Systems Engineer   Geschaeftsstelle Duesseldorf
  Martinstrasse 47-55, Haus A
phone: +49 211 302 708 32 D-40223 Duesseldorf
fax:   +49 211 302 708 50 www.science-computing.de
-- 
Vorstandsvorsitzender/Chairman of the board of management:
Gerd-Lothar Leonhart
Vorstand/Board of Management:
Dr. Bernd Finkbeiner, Michael Heinrichs, 
Dr. Arno Steitz, Dr. Ingrid Zech
Vorsitzender des Aufsichtsrats/
Chairman of the Supervisory Board:
Philippe Miltin
Sitz/Registered Office: Tuebingen
Registergericht/Registration Court: Stuttgart
Registernummer/Commercial Register No.: HRB 382196

--
To unsubscribe from this list: send the line unsubscribe git in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH] Extend runtime prefix computation

2013-03-05 Thread Michael Weiser
Hello,

On Tue, Nov 27, 2012 at 05:30:04PM +0100, Michael Weiser wrote:

 Support determining the binaries' installation path at runtime even if
 called without any path components (i.e. via search path). Implement
 fallback to compiled-in prefix if determination fails or is impossible.

I see this hasn't made it into git, yet. Is there any reason? What can
I do to get it included?

Thanks,
-- 
Michael Weiserscience + computing ag
Senior Systems Engineer   Geschaeftsstelle Duesseldorf
  Martinstrasse 47-55, Haus A
phone: +49 211 302 708 32 D-40223 Duesseldorf
fax:   +49 211 302 708 50 www.science-computing.de
-- 
Vorstandsvorsitzender/Chairman of the board of management:
Gerd-Lothar Leonhart
Vorstand/Board of Management:
Dr. Bernd Finkbeiner, Michael Heinrichs, 
Dr. Arno Steitz, Dr. Ingrid Zech
Vorsitzender des Aufsichtsrats/
Chairman of the Supervisory Board:
Philippe Miltin
Sitz/Registered Office: Tuebingen
Registergericht/Registration Court: Stuttgart
Registernummer/Commercial Register No.: HRB 382196

--
To unsubscribe from this list: send the line unsubscribe git in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH] Extend runtime prefix computation

2013-03-05 Thread Junio C Hamano
Michael Weiser m.wei...@science-computing.de writes:

 On Tue, Nov 27, 2012 at 05:30:04PM +0100, Michael Weiser wrote:

 Support determining the binaries' installation path at runtime even if
 called without any path components (i.e. via search path). Implement
 fallback to compiled-in prefix if determination fails or is impossible.

 I see this hasn't made it into git, yet. Is there any reason?

Most likely nobody was interested.

The default for any change is not to include it.  Is there any
reason why we want this change?
--
To unsubscribe from this list: send the line unsubscribe git in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH] Extend runtime prefix computation

2012-11-30 Thread Erik Faye-Lund
On Tue, Nov 27, 2012 at 5:30 PM, Michael Weiser
m.wei...@science-computing.de wrote:
 Support determining the binaries' installation path at runtime even if
 called without any path components (i.e. via search path). Implement
 fallback to compiled-in prefix if determination fails or is impossible.

 Signed-off-by: Michael Weiser wei...@science-computing.de
 ---
 - Has two very minor memory leaks - function is called only once per
   program execution. Do we care? Alternative: Use static buffer instead.

  exec_cmd.c |   68 ++-
  1 files changed, 53 insertions(+), 15 deletions(-)

 diff --git a/exec_cmd.c b/exec_cmd.c
 index 125fa6f..d50d7f8 100644
 --- a/exec_cmd.c
 +++ b/exec_cmd.c
 @@ -4,28 +4,22 @@
  #define MAX_ARGS   32

  static const char *argv_exec_path;
 -static const char *argv0_path;
 +static const char *argv0_path = NULL;

  const char *system_path(const char *path)
  {
 -#ifdef RUNTIME_PREFIX
 -   static const char *prefix;
 -#else
 static const char *prefix = PREFIX;
 -#endif
 struct strbuf d = STRBUF_INIT;

 if (is_absolute_path(path))
 return path;

  #ifdef RUNTIME_PREFIX
 -   assert(argv0_path);
 -   assert(is_absolute_path(argv0_path));
 -
 -   if (!prefix 
 -   !(prefix = strip_path_suffix(argv0_path, GIT_EXEC_PATH)) 
 -   !(prefix = strip_path_suffix(argv0_path, BINDIR)) 
 -   !(prefix = strip_path_suffix(argv0_path, git))) {
 +   if (!argv0_path ||
 +   !is_absolute_path(argv0_path) ||
 +   (!(prefix = strip_path_suffix(argv0_path, GIT_EXEC_PATH)) 
 +!(prefix = strip_path_suffix(argv0_path, BINDIR)) 
 +!(prefix = strip_path_suffix(argv0_path, git {
 prefix = PREFIX;
 trace_printf(RUNTIME_PREFIX requested, 
 but prefix computation failed.  
 @@ -41,20 +35,64 @@ const char *system_path(const char *path)
  const char *git_extract_argv0_path(const char *argv0)
  {
 const char *slash;
 +   char *abs_argv0 = NULL;

 if (!argv0 || !*argv0)
 return NULL;
 slash = argv0 + strlen(argv0);

 +   /* walk to the first slash from the end */
 while (argv0 = slash  !is_dir_sep(*slash))
 slash--;

 +   /* if there was a slash ... */
 if (slash = argv0) {
 -   argv0_path = xstrndup(argv0, slash - argv0);
 -   return slash + 1;
 +   /* ... it's either an absolute path */
 +   if (is_absolute_path(argv0)) {
 +   /* FIXME: memory leak here */
 +   argv0_path = xstrndup(argv0, slash - argv0);
 +   return slash + 1;
 +   }
 +
 +   /* ... or a relative path, in which case we have to make it
 +* absolute first and do the whole thing again */
 +   abs_argv0 = xstrdup(real_path(argv0));
 +   } else {
 +   /* argv0 is no path at all, just a name. Resolve it into a
 +* path. Unfortunately, this gets system specific. */
 +#if defined(__linux__)
 +   struct stat st;
 +   if (!stat(/proc/self/exe, st)) {
 +   abs_argv0 = xstrdup(real_path(/proc/self/exe));
 +   }
 +#elif defined(__APPLE__)
 +   /* Mac OS X has realpath, which incidentally allocates its own
 +* memory, which in turn is why we do all the xstrdup's in the
 +* other cases. */
 +   abs_argv0 = realpath(argv0, NULL);
 +#endif

...perhaps a GetModuleFileName(NULL, ...) for Windows is in place here?
--
To unsubscribe from this list: send the line unsubscribe git in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH] Extend runtime prefix computation

2012-11-30 Thread Michael Weiser
Hello Erik,

On Fri, Nov 30, 2012 at 11:20:52AM +0100, Erik Faye-Lund wrote:

  +#if defined(__linux__)
  +   struct stat st;
  +   if (!stat(/proc/self/exe, st)) {
  +   abs_argv0 = xstrdup(real_path(/proc/self/exe));
  +   }
  +#elif defined(__APPLE__)
  +   /* Mac OS X has realpath, which incidentally allocates its 
  own
  +* memory, which in turn is why we do all the xstrdup's in 
  the
  +* other cases. */
  +   abs_argv0 = realpath(argv0, NULL);
  +#endif
 ...perhaps a GetModuleFileName(NULL, ...) for Windows is in place here?

Agreed. However, I do not use git on Windows and don't have a Windows
devel toolchain in place. So I guess this should be added in a separate
patch by someone actually in need of it and in a position to develop and
test it?

Thanks,
-- 
Michael Weiserscience + computing ag
Senior Systems Engineer   Geschaeftsstelle Duesseldorf
  Martinstrasse 47-55, Haus A
phone: +49 211 302 708 32 D-40223 Duesseldorf
fax:   +49 211 302 708 50 www.science-computing.de
-- 
Vorstandsvorsitzender/Chairman of the board of management:
Gerd-Lothar Leonhart
Vorstand/Board of Management:
Dr. Bernd Finkbeiner, Michael Heinrichs, 
Dr. Arno Steitz, Dr. Ingrid Zech
Vorsitzender des Aufsichtsrats/
Chairman of the Supervisory Board:
Philippe Miltin
Sitz/Registered Office: Tuebingen
Registergericht/Registration Court: Stuttgart
Registernummer/Commercial Register No.: HRB 382196

--
To unsubscribe from this list: send the line unsubscribe git in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH] Extend runtime prefix computation

2012-11-27 Thread Michael Weiser
Support determining the binaries' installation path at runtime even if
called without any path components (i.e. via search path). Implement
fallback to compiled-in prefix if determination fails or is impossible.

Signed-off-by: Michael Weiser wei...@science-computing.de
---
- Has two very minor memory leaks - function is called only once per
  program execution. Do we care? Alternative: Use static buffer instead.

 exec_cmd.c |   68 ++-
 1 files changed, 53 insertions(+), 15 deletions(-)

diff --git a/exec_cmd.c b/exec_cmd.c
index 125fa6f..d50d7f8 100644
--- a/exec_cmd.c
+++ b/exec_cmd.c
@@ -4,28 +4,22 @@
 #define MAX_ARGS   32
 
 static const char *argv_exec_path;
-static const char *argv0_path;
+static const char *argv0_path = NULL;
 
 const char *system_path(const char *path)
 {
-#ifdef RUNTIME_PREFIX
-   static const char *prefix;
-#else
static const char *prefix = PREFIX;
-#endif
struct strbuf d = STRBUF_INIT;
 
if (is_absolute_path(path))
return path;
 
 #ifdef RUNTIME_PREFIX
-   assert(argv0_path);
-   assert(is_absolute_path(argv0_path));
-
-   if (!prefix 
-   !(prefix = strip_path_suffix(argv0_path, GIT_EXEC_PATH)) 
-   !(prefix = strip_path_suffix(argv0_path, BINDIR)) 
-   !(prefix = strip_path_suffix(argv0_path, git))) {
+   if (!argv0_path ||
+   !is_absolute_path(argv0_path) ||
+   (!(prefix = strip_path_suffix(argv0_path, GIT_EXEC_PATH)) 
+!(prefix = strip_path_suffix(argv0_path, BINDIR)) 
+!(prefix = strip_path_suffix(argv0_path, git {
prefix = PREFIX;
trace_printf(RUNTIME_PREFIX requested, 
but prefix computation failed.  
@@ -41,20 +35,64 @@ const char *system_path(const char *path)
 const char *git_extract_argv0_path(const char *argv0)
 {
const char *slash;
+   char *abs_argv0 = NULL;
 
if (!argv0 || !*argv0)
return NULL;
slash = argv0 + strlen(argv0);
 
+   /* walk to the first slash from the end */
while (argv0 = slash  !is_dir_sep(*slash))
slash--;
 
+   /* if there was a slash ... */
if (slash = argv0) {
-   argv0_path = xstrndup(argv0, slash - argv0);
-   return slash + 1;
+   /* ... it's either an absolute path */
+   if (is_absolute_path(argv0)) {
+   /* FIXME: memory leak here */
+   argv0_path = xstrndup(argv0, slash - argv0);
+   return slash + 1;
+   }
+
+   /* ... or a relative path, in which case we have to make it
+* absolute first and do the whole thing again */
+   abs_argv0 = xstrdup(real_path(argv0));
+   } else {
+   /* argv0 is no path at all, just a name. Resolve it into a
+* path. Unfortunately, this gets system specific. */
+#if defined(__linux__)
+   struct stat st;
+   if (!stat(/proc/self/exe, st)) {
+   abs_argv0 = xstrdup(real_path(/proc/self/exe));
+   }
+#elif defined(__APPLE__)
+   /* Mac OS X has realpath, which incidentally allocates its own
+* memory, which in turn is why we do all the xstrdup's in the
+* other cases. */
+   abs_argv0 = realpath(argv0, NULL);
+#endif
+
+   /* if abs_argv0 is still NULL here, something failed above or
+* we are on an unsupported system. system_path() will warn
+* and fall back to the static prefix */
+   if (!abs_argv0) {
+   argv0_path = NULL;
+   return argv0;
+   }
}
 
-   return argv0;
+   /* abs_argv0 is an absolute path now for which memory was allocated
+* with malloc */
+
+   slash = abs_argv0 + strlen(abs_argv0);
+   while (abs_argv0 = slash  !is_dir_sep(*slash))
+   slash--;
+
+   /* FIXME: memory leaks here */
+   argv0_path = xstrndup(abs_argv0, slash - abs_argv0);
+   slash = xstrdup(slash + 1);
+   free(abs_argv0);
+   return slash;
 }
 
 void git_set_argv_exec_path(const char *exec_path)
-- 
1.7.3.4
-- 
Vorstandsvorsitzender/Chairman of the board of management:
Gerd-Lothar Leonhart
Vorstand/Board of Management:
Dr. Bernd Finkbeiner, Michael Heinrichs, 
Dr. Arno Steitz, Dr. Ingrid Zech
Vorsitzender des Aufsichtsrats/
Chairman of the Supervisory Board:
Philippe Miltin
Sitz/Registered Office: Tuebingen
Registergericht/Registration Court: Stuttgart
Registernummer/Commercial Register No.: HRB 382196

--
To unsubscribe from this list: send the line unsubscribe git in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html