use xgetcwd() to get the current directory or die
authorRené Scharfe <l.s.r@web.de>
Mon, 28 Jul 2014 18:30:39 +0000 (20:30 +0200)
committerJunio C Hamano <gitster@pobox.com>
Tue, 26 Aug 2014 18:06:06 +0000 (11:06 -0700)
Convert several calls of getcwd() and die() to use xgetcwd() instead.
This way we get rid of fixed-size buffers (which can be too small
depending on the used file system) and gain consistent error messages.

Signed-off-by: Rene Scharfe <l.s.r@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/init-db.c
builtin/rev-parse.c
dir.c
setup.c
trace.c
index 56f85e239ae0d2607c38e3c46013007ba9e71b12..f6dd1727ece7d688bb326a66b582fec4b28298a7 100644 (file)
@@ -426,8 +426,9 @@ int init_db(const char *template_dir, unsigned int flags)
 
 static int guess_repository_type(const char *git_dir)
 {
-       char cwd[PATH_MAX];
        const char *slash;
+       char *cwd;
+       int cwd_is_git_dir;
 
        /*
         * "GIT_DIR=. git init" is always bare.
@@ -435,9 +436,10 @@ static int guess_repository_type(const char *git_dir)
         */
        if (!strcmp(".", git_dir))
                return 1;
-       if (!getcwd(cwd, sizeof(cwd)))
-               die_errno(_("cannot tell cwd"));
-       if (!strcmp(git_dir, cwd))
+       cwd = xgetcwd();
+       cwd_is_git_dir = !strcmp(git_dir, cwd);
+       free(cwd);
+       if (cwd_is_git_dir)
                return 1;
        /*
         * "GIT_DIR=.git or GIT_DIR=something/.git is usually not.
@@ -572,11 +574,8 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
                        git_work_tree_cfg = xstrdup(real_path(rel));
                        free(rel);
                }
-               if (!git_work_tree_cfg) {
-                       git_work_tree_cfg = xcalloc(PATH_MAX, 1);
-                       if (!getcwd(git_work_tree_cfg, PATH_MAX))
-                               die_errno (_("Cannot access current working directory"));
-               }
+               if (!git_work_tree_cfg)
+                       git_work_tree_cfg = xgetcwd();
                if (work_tree)
                        set_git_work_tree(real_path(work_tree));
                else
index 1a6122d3ae29609c30c26959fbb1ff799e13a4b2..a88123ad3e347f443425007dc5a02b7d6a33de79 100644 (file)
@@ -734,7 +734,7 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
                        }
                        if (!strcmp(arg, "--git-dir")) {
                                const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
-                               static char cwd[PATH_MAX];
+                               char *cwd;
                                int len;
                                if (gitdir) {
                                        puts(gitdir);
@@ -744,10 +744,10 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
                                        puts(".git");
                                        continue;
                                }
-                               if (!getcwd(cwd, PATH_MAX))
-                                       die_errno("unable to get current working directory");
+                               cwd = xgetcwd();
                                len = strlen(cwd);
                                printf("%s%s.git\n", cwd, len && cwd[len-1] != '/' ? "/" : "");
+                               free(cwd);
                                continue;
                        }
                        if (!strcmp(arg, "--resolve-git-dir")) {
diff --git a/dir.c b/dir.c
index 797805d6a1d33ef66ccb61155135cdcd1a53d9d0..2c50ccfb4963a854401cb8d03b429947c7400f93 100644 (file)
--- a/dir.c
+++ b/dir.c
@@ -1500,12 +1500,16 @@ int dir_inside_of(const char *subdir, const char *dir)
 
 int is_inside_dir(const char *dir)
 {
-       char cwd[PATH_MAX];
+       char *cwd;
+       int rc;
+
        if (!dir)
                return 0;
-       if (!getcwd(cwd, sizeof(cwd)))
-               die_errno("can't find the current directory");
-       return dir_inside_of(cwd, dir) >= 0;
+
+       cwd = xgetcwd();
+       rc = (dir_inside_of(cwd, dir) >= 0);
+       free(cwd);
+       return rc;
 }
 
 int is_empty_dir(const char *path)
diff --git a/setup.c b/setup.c
index 937dad503c28681d7c21f5dc300ea943cfe5b17a..72a4978ba5414af523a2c88a60a4d2d624606e85 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -434,16 +434,16 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
                if (is_absolute_path(git_work_tree_cfg))
                        set_git_work_tree(git_work_tree_cfg);
                else {
-                       char core_worktree[PATH_MAX];
+                       char *core_worktree;
                        if (chdir(gitdirenv))
                                die_errno("Could not chdir to '%s'", gitdirenv);
                        if (chdir(git_work_tree_cfg))
                                die_errno("Could not chdir to '%s'", git_work_tree_cfg);
-                       if (!getcwd(core_worktree, PATH_MAX))
-                               die_errno("Could not get directory '%s'", git_work_tree_cfg);
+                       core_worktree = xgetcwd();
                        if (chdir(cwd->buf))
                                die_errno("Could not come back to cwd");
                        set_git_work_tree(core_worktree);
+                       free(core_worktree);
                }
        }
        else if (!git_env_bool(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, 1)) {
diff --git a/trace.c b/trace.c
index 08180a90bc0074f821e5f2e05988e27c174be39f..3523667f6f6fb2c9ca38917f8e45144e99186dc7 100644 (file)
--- a/trace.c
+++ b/trace.c
@@ -158,13 +158,12 @@ void trace_repo_setup(const char *prefix)
 {
        static const char *key = "GIT_TRACE_SETUP";
        const char *git_work_tree;
-       char cwd[PATH_MAX];
+       char *cwd;
 
        if (!trace_want(key))
                return;
 
-       if (!getcwd(cwd, PATH_MAX))
-               die("Unable to get current working directory");
+       cwd = xgetcwd();
 
        if (!(git_work_tree = get_git_work_tree()))
                git_work_tree = "(null)";
@@ -176,6 +175,8 @@ void trace_repo_setup(const char *prefix)
        trace_printf_key(key, "setup: worktree: %s\n", quote_crnl(git_work_tree));
        trace_printf_key(key, "setup: cwd: %s\n", quote_crnl(cwd));
        trace_printf_key(key, "setup: prefix: %s\n", quote_crnl(prefix));
+
+       free(cwd);
 }
 
 int trace_want(const char *key)