2 * I'm tired of doing "vsnprintf()" etc just to open a
3 * file, so here's a "return static buffer with printf"
6 * It's obviously not thread-safe. Sue me. But it's quite
7 * useful for doing things like
9 * f = open(mkpath("%s/%s.git", base, name), O_RDONLY);
11 * which is what it's designed for.
15 static char bad_path
[] = "/bad-path/";
17 static char *get_pathname(void)
19 static char pathname_array
[4][PATH_MAX
];
21 return pathname_array
[3 & ++index
];
24 static char *cleanup_path(char *path
)
27 if (!memcmp(path
, "./", 2)) {
35 char *mkpath(const char *fmt
, ...)
39 char *pathname
= get_pathname();
42 len
= vsnprintf(pathname
, PATH_MAX
, fmt
, args
);
46 return cleanup_path(pathname
);
49 char *git_path(const char *fmt
, ...)
51 const char *git_dir
= get_git_dir();
52 char *pathname
= get_pathname();
56 len
= strlen(git_dir
);
57 if (len
> PATH_MAX
-100)
59 memcpy(pathname
, git_dir
, len
);
60 if (len
&& git_dir
[len
-1] != '/')
61 pathname
[len
++] = '/';
63 len
+= vsnprintf(pathname
+ len
, PATH_MAX
- len
, fmt
, args
);
67 return cleanup_path(pathname
);
71 /* git_mkstemp() - create tmp file honoring TMPDIR variable */
72 int git_mkstemp(char *path
, size_t len
, const char *template)
74 char *env
, *pch
= path
;
76 if ((env
= getenv("TMPDIR")) == NULL
) {
81 size_t n
= snprintf(pch
, len
, "%s/", env
);
87 strlcpy(pch
, template, len
);
93 int validate_headref(const char *path
)
96 char *buf
, buffer
[256];
97 unsigned char sha1
[20];
100 if (lstat(path
, &st
) < 0)
103 /* Make sure it is a "refs/.." symlink */
104 if (S_ISLNK(st
.st_mode
)) {
105 len
= readlink(path
, buffer
, sizeof(buffer
)-1);
106 if (len
>= 5 && !memcmp("refs/", buffer
, 5))
112 * Anything else, just open it and try to see if it is a symbolic ref.
114 fd
= open(path
, O_RDONLY
);
117 len
= read_in_full(fd
, buffer
, sizeof(buffer
)-1);
121 * Is it a symbolic ref?
125 if (!memcmp("ref:", buffer
, 4)) {
128 while (len
&& isspace(*buf
))
130 if (len
>= 5 && !memcmp("refs/", buf
, 5))
135 * Is this a detached HEAD?
137 if (!get_sha1_hex(buffer
, sha1
))
143 static char *user_path(char *buf
, char *path
, int sz
)
149 if (!path
|| path
[0] != '~')
152 slash
= strchr(path
, '/');
153 if (path
[0] == '/' || !path
[0]) {
154 pw
= getpwuid(getuid());
165 if (!pw
|| !pw
->pw_dir
|| sz
<= strlen(pw
->pw_dir
))
167 baselen
= strlen(pw
->pw_dir
);
168 memcpy(buf
, pw
->pw_dir
, baselen
);
169 while ((1 < baselen
) && (buf
[baselen
-1] == '/')) {
173 if (slash
&& slash
[1]) {
175 if (sz
<= baselen
+ len
)
177 memcpy(buf
+ baselen
, slash
, len
+ 1);
183 * First, one directory to try is determined by the following algorithm.
185 * (0) If "strict" is given, the path is used as given and no DWIM is
187 * (1) "~/path" to mean path under the running user's home directory;
188 * (2) "~user/path" to mean path under named user's home directory;
189 * (3) "relative/path" to mean cwd relative directory; or
190 * (4) "/absolute/path" to mean absolute directory.
192 * Unless "strict" is given, we try access() for existence of "%s.git/.git",
193 * "%s/.git", "%s.git", "%s" in this order. The first one that exists is
196 * Second, we try chdir() to that. Upon failure, we return NULL.
198 * Then, we try if the current directory is a valid git repository.
199 * Upon failure, we return NULL.
201 * If all goes well, we return the directory we used to chdir() (but
202 * before ~user is expanded), avoiding getcwd() resolving symbolic
203 * links. User relative paths are also returned as they are given,
204 * except DWIM suffixing.
206 char *enter_repo(char *path
, int strict
)
208 static char used_path
[PATH_MAX
];
209 static char validated_path
[PATH_MAX
];
215 static const char *suffix
[] = {
216 ".git/.git", "/.git", ".git", "", NULL
,
218 int len
= strlen(path
);
220 while ((1 < len
) && (path
[len
-1] == '/')) {
226 if (path
[0] == '~') {
227 if (!user_path(used_path
, path
, PATH_MAX
))
229 strcpy(validated_path
, path
);
232 else if (PATH_MAX
- 10 < len
)
235 path
= strcpy(used_path
, path
);
236 strcpy(validated_path
, path
);
239 for (i
= 0; suffix
[i
]; i
++) {
240 strcpy(path
+ len
, suffix
[i
]);
241 if (!access(path
, F_OK
)) {
242 strcat(validated_path
, suffix
[i
]);
246 if (!suffix
[i
] || chdir(path
))
248 path
= validated_path
;
250 else if (chdir(path
))
253 if (access("objects", X_OK
) == 0 && access("refs", X_OK
) == 0 &&
254 validate_headref("HEAD") == 0) {
255 setenv("GIT_DIR", ".", 1);
256 check_repository_format();
263 int adjust_shared_perm(const char *path
)
268 if (!shared_repository
)
270 if (lstat(path
, &st
) < 0)
274 mode
|= (shared_repository
== PERM_GROUP
276 : (shared_repository
== PERM_EVERYBODY
284 mode
|= (shared_repository
== PERM_GROUP
286 : (shared_repository
== PERM_EVERYBODY
291 if ((mode
& st
.st_mode
) != mode
&& chmod(path
, mode
) < 0)