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>
René Scharfe committed
Jul 28, 2014 at 20:30 UTC
56b9f6e738af6f5238f57a29e96103cf61e3f8cd
5 files changed
+26
-22
builtin/init-db.c
+8
-9
@@ -426,8 +426,9 @@ int init_db(const char *template_dir, unsigned int flags)
426
427
static int guess_repository_type(const char *git_dir)
428
{
429
- char cwd[PATH_MAX];
429
const char *slash;
430
+ char *cwd;
431
+ int cwd_is_git_dir;
432
433
/*
434
* "GIT_DIR=. git init" is always bare.
@@ -435,9 +436,10 @@ static int guess_repository_type(const char *git_dir)
436
*/
437
if (!strcmp(".", git_dir))
438
return 1;
438
- if (!getcwd(cwd, sizeof(cwd)))
439
- die_errno(_("cannot tell cwd"));
440
- if (!strcmp(git_dir, cwd))
439
+ cwd = xgetcwd();
440
+ cwd_is_git_dir = !strcmp(git_dir, cwd);
441
+ free(cwd);
442
+ if (cwd_is_git_dir)
443
return 1;
444
/*
445
* "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)
574
git_work_tree_cfg = xstrdup(real_path(rel));
575
free(rel);
576
}
575
- if (!git_work_tree_cfg) {
576
- git_work_tree_cfg = xcalloc(PATH_MAX, 1);
577
- if (!getcwd(git_work_tree_cfg, PATH_MAX))
578
- die_errno (_("Cannot access current working directory"));
579
- }
577
+ if (!git_work_tree_cfg)
578
+ git_work_tree_cfg = xgetcwd();
579
if (work_tree)
580
set_git_work_tree(real_path(work_tree));
581
else
builtin/rev-parse.c
+3
-3
@@ -734,7 +734,7 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
734
}
735
if (!strcmp(arg, "--git-dir")) {
736
const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
737
- static char cwd[PATH_MAX];
737
+ char *cwd;
738
int len;
739
if (gitdir) {
740
puts(gitdir);
@@ -744,10 +744,10 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
744
puts(".git");
745
continue;
746
}
747
- if (!getcwd(cwd, PATH_MAX))
748
- die_errno("unable to get current working directory");
747
+ cwd = xgetcwd();
748
len = strlen(cwd);
749
printf("%s%s.git\n", cwd, len && cwd[len-1] != '/' ? "/" : "");
750
+ free(cwd);
751
continue;
752
}
753
if (!strcmp(arg, "--resolve-git-dir")) {
dir.c
+8
-4
@@ -1500,12 +1500,16 @@ int dir_inside_of(const char *subdir, const char *dir)
1500
1501
int is_inside_dir(const char *dir)
1502
{
1503
- char cwd[PATH_MAX];
1503
+ char *cwd;
1504
+ int rc;
1505
+
1506
if (!dir)
1507
return 0;
1506
- if (!getcwd(cwd, sizeof(cwd)))
1507
- die_errno("can't find the current directory");
1508
- return dir_inside_of(cwd, dir) >= 0;
1508
+
1509
+ cwd = xgetcwd();
1510
+ rc = (dir_inside_of(cwd, dir) >= 0);
1511
+ free(cwd);
1512
+ return rc;
1513
}
1514
1515
int is_empty_dir(const char *path)
setup.c
+3
-3
@@ -434,16 +434,16 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
434
if (is_absolute_path(git_work_tree_cfg))
435
set_git_work_tree(git_work_tree_cfg);
436
else {
437
- char core_worktree[PATH_MAX];
437
+ char *core_worktree;
438
if (chdir(gitdirenv))
439
die_errno("Could not chdir to '%s'", gitdirenv);
440
if (chdir(git_work_tree_cfg))
441
die_errno("Could not chdir to '%s'", git_work_tree_cfg);
442
- if (!getcwd(core_worktree, PATH_MAX))
443
- die_errno("Could not get directory '%s'", git_work_tree_cfg);
442
+ core_worktree = xgetcwd();
443
if (chdir(cwd->buf))
444
die_errno("Could not come back to cwd");
445
set_git_work_tree(core_worktree);
446
+ free(core_worktree);
447
}
448
}
449
else if (!git_env_bool(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, 1)) {
trace.c
+4
-3
@@ -158,13 +158,12 @@ void trace_repo_setup(const char *prefix)
158
{
159
static const char *key = "GIT_TRACE_SETUP";
160
const char *git_work_tree;
161
- char cwd[PATH_MAX];
161
+ char *cwd;
162
163
if (!trace_want(key))
164
return;
165
166
- if (!getcwd(cwd, PATH_MAX))
167
- die("Unable to get current working directory");
166
+ cwd = xgetcwd();
167
168
if (!(git_work_tree = get_git_work_tree()))
169
git_work_tree = "(null)";
@@ -176,6 +175,8 @@ void trace_repo_setup(const char *prefix)
175
trace_printf_key(key, "setup: worktree: %s\n", quote_crnl(git_work_tree));
176
trace_printf_key(key, "setup: cwd: %s\n", quote_crnl(cwd));
177
trace_printf_key(key, "setup: prefix: %s\n", quote_crnl(prefix));
178
+
179
+ free(cwd);
180
}
181
182
int trace_want(const char *key)