use xgetcwd() to get the current directory or die

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>
This commit is contained in:
René Scharfe 2014-07-28 20:30:39 +02:00 committed by Junio C Hamano
parent aa14e980ff
commit 56b9f6e738
5 changed files with 26 additions and 22 deletions

View File

@ -426,8 +426,9 @@ int init_db(const char *template_dir, unsigned int flags)
static int guess_repository_type(const char *git_dir) static int guess_repository_type(const char *git_dir)
{ {
char cwd[PATH_MAX];
const char *slash; const char *slash;
char *cwd;
int cwd_is_git_dir;
/* /*
* "GIT_DIR=. git init" is always bare. * "GIT_DIR=. git init" is always bare.
@ -435,9 +436,10 @@ static int guess_repository_type(const char *git_dir)
*/ */
if (!strcmp(".", git_dir)) if (!strcmp(".", git_dir))
return 1; return 1;
if (!getcwd(cwd, sizeof(cwd))) cwd = xgetcwd();
die_errno(_("cannot tell cwd")); cwd_is_git_dir = !strcmp(git_dir, cwd);
if (!strcmp(git_dir, cwd)) free(cwd);
if (cwd_is_git_dir)
return 1; return 1;
/* /*
* "GIT_DIR=.git or GIT_DIR=something/.git is usually not. * "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)); git_work_tree_cfg = xstrdup(real_path(rel));
free(rel); free(rel);
} }
if (!git_work_tree_cfg) { if (!git_work_tree_cfg)
git_work_tree_cfg = xcalloc(PATH_MAX, 1); git_work_tree_cfg = xgetcwd();
if (!getcwd(git_work_tree_cfg, PATH_MAX))
die_errno (_("Cannot access current working directory"));
}
if (work_tree) if (work_tree)
set_git_work_tree(real_path(work_tree)); set_git_work_tree(real_path(work_tree));
else else

View File

@ -734,7 +734,7 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
} }
if (!strcmp(arg, "--git-dir")) { if (!strcmp(arg, "--git-dir")) {
const char *gitdir = getenv(GIT_DIR_ENVIRONMENT); const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
static char cwd[PATH_MAX]; char *cwd;
int len; int len;
if (gitdir) { if (gitdir) {
puts(gitdir); puts(gitdir);
@ -744,10 +744,10 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
puts(".git"); puts(".git");
continue; continue;
} }
if (!getcwd(cwd, PATH_MAX)) cwd = xgetcwd();
die_errno("unable to get current working directory");
len = strlen(cwd); len = strlen(cwd);
printf("%s%s.git\n", cwd, len && cwd[len-1] != '/' ? "/" : ""); printf("%s%s.git\n", cwd, len && cwd[len-1] != '/' ? "/" : "");
free(cwd);
continue; continue;
} }
if (!strcmp(arg, "--resolve-git-dir")) { if (!strcmp(arg, "--resolve-git-dir")) {

12
dir.c
View File

@ -1500,12 +1500,16 @@ int dir_inside_of(const char *subdir, const char *dir)
int is_inside_dir(const char *dir) int is_inside_dir(const char *dir)
{ {
char cwd[PATH_MAX]; char *cwd;
int rc;
if (!dir) if (!dir)
return 0; return 0;
if (!getcwd(cwd, sizeof(cwd)))
die_errno("can't find the current directory"); cwd = xgetcwd();
return dir_inside_of(cwd, dir) >= 0; rc = (dir_inside_of(cwd, dir) >= 0);
free(cwd);
return rc;
} }
int is_empty_dir(const char *path) int is_empty_dir(const char *path)

View File

@ -434,16 +434,16 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
if (is_absolute_path(git_work_tree_cfg)) if (is_absolute_path(git_work_tree_cfg))
set_git_work_tree(git_work_tree_cfg); set_git_work_tree(git_work_tree_cfg);
else { else {
char core_worktree[PATH_MAX]; char *core_worktree;
if (chdir(gitdirenv)) if (chdir(gitdirenv))
die_errno("Could not chdir to '%s'", gitdirenv); die_errno("Could not chdir to '%s'", gitdirenv);
if (chdir(git_work_tree_cfg)) if (chdir(git_work_tree_cfg))
die_errno("Could not chdir to '%s'", git_work_tree_cfg); die_errno("Could not chdir to '%s'", git_work_tree_cfg);
if (!getcwd(core_worktree, PATH_MAX)) core_worktree = xgetcwd();
die_errno("Could not get directory '%s'", git_work_tree_cfg);
if (chdir(cwd->buf)) if (chdir(cwd->buf))
die_errno("Could not come back to cwd"); die_errno("Could not come back to cwd");
set_git_work_tree(core_worktree); set_git_work_tree(core_worktree);
free(core_worktree);
} }
} }
else if (!git_env_bool(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, 1)) { else if (!git_env_bool(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, 1)) {

View File

@ -158,13 +158,12 @@ void trace_repo_setup(const char *prefix)
{ {
static const char *key = "GIT_TRACE_SETUP"; static const char *key = "GIT_TRACE_SETUP";
const char *git_work_tree; const char *git_work_tree;
char cwd[PATH_MAX]; char *cwd;
if (!trace_want(key)) if (!trace_want(key))
return; return;
if (!getcwd(cwd, PATH_MAX)) cwd = xgetcwd();
die("Unable to get current working directory");
if (!(git_work_tree = get_git_work_tree())) if (!(git_work_tree = get_git_work_tree()))
git_work_tree = "(null)"; 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: worktree: %s\n", quote_crnl(git_work_tree));
trace_printf_key(key, "setup: cwd: %s\n", quote_crnl(cwd)); trace_printf_key(key, "setup: cwd: %s\n", quote_crnl(cwd));
trace_printf_key(key, "setup: prefix: %s\n", quote_crnl(prefix)); trace_printf_key(key, "setup: prefix: %s\n", quote_crnl(prefix));
free(cwd);
} }
int trace_want(const char *key) int trace_want(const char *key)