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.
16 static char pathname
[PATH_MAX
];
17 static char bad_path
[] = "/bad-path/";
19 static char *cleanup_path(char *path
)
22 if (!memcmp(path
, "./", 2)) {
30 char *mkpath(const char *fmt
, ...)
36 len
= vsnprintf(pathname
, PATH_MAX
, fmt
, args
);
40 return cleanup_path(pathname
);
43 char *git_path(const char *fmt
, ...)
45 const char *git_dir
= get_git_dir();
49 len
= strlen(git_dir
);
50 if (len
> PATH_MAX
-100)
52 memcpy(pathname
, git_dir
, len
);
53 if (len
&& git_dir
[len
-1] != '/')
54 pathname
[len
++] = '/';
56 len
+= vsnprintf(pathname
+ len
, PATH_MAX
- len
, fmt
, args
);
60 return cleanup_path(pathname
);
64 /* git_mkstemp() - create tmp file honoring TMPDIR variable */
65 int git_mkstemp(char *path
, size_t len
, const char *template)
67 char *env
, *pch
= path
;
69 if ((env
= getenv("TMPDIR")) == NULL
) {
74 size_t n
= snprintf(pch
, len
, "%s/", env
);
80 strlcpy(pch
, template, len
);
86 int validate_symref(const char *path
)
89 char *buf
, buffer
[256];
92 if (lstat(path
, &st
) < 0)
95 /* Make sure it is a "refs/.." symlink */
96 if (S_ISLNK(st
.st_mode
)) {
97 len
= readlink(path
, buffer
, sizeof(buffer
)-1);
98 if (len
>= 5 && !memcmp("refs/", buffer
, 5))
104 * Anything else, just open it and try to see if it is a symbolic ref.
106 fd
= open(path
, O_RDONLY
);
109 len
= read(fd
, buffer
, sizeof(buffer
)-1);
113 * Is it a symbolic ref?
115 if (len
< 4 || memcmp("ref:", buffer
, 4))
119 while (len
&& isspace(*buf
))
121 if (len
>= 5 && !memcmp("refs/", buf
, 5))
126 static char *user_path(char *buf
, char *path
, int sz
)
132 if (!path
|| path
[0] != '~')
135 slash
= strchr(path
, '/');
136 if (path
[0] == '/' || !path
[0]) {
137 pw
= getpwuid(getuid());
148 if (!pw
|| !pw
->pw_dir
|| sz
<= strlen(pw
->pw_dir
))
150 baselen
= strlen(pw
->pw_dir
);
151 memcpy(buf
, pw
->pw_dir
, baselen
);
152 while ((1 < baselen
) && (buf
[baselen
-1] == '/')) {
156 if (slash
&& slash
[1]) {
158 if (sz
<= baselen
+ len
)
160 memcpy(buf
+ baselen
, slash
, len
+ 1);
166 * First, one directory to try is determined by the following algorithm.
168 * (0) If "strict" is given, the path is used as given and no DWIM is
170 * (1) "~/path" to mean path under the running user's home directory;
171 * (2) "~user/path" to mean path under named user's home directory;
172 * (3) "relative/path" to mean cwd relative directory; or
173 * (4) "/absolute/path" to mean absolute directory.
175 * Unless "strict" is given, we try access() for existence of "%s.git/.git",
176 * "%s/.git", "%s.git", "%s" in this order. The first one that exists is
179 * Second, we try chdir() to that. Upon failure, we return NULL.
181 * Then, we try if the current directory is a valid git repository.
182 * Upon failure, we return NULL.
184 * If all goes well, we return the directory we used to chdir() (but
185 * before ~user is expanded), avoiding getcwd() resolving symbolic
186 * links. User relative paths are also returned as they are given,
187 * except DWIM suffixing.
189 char *enter_repo(char *path
, int strict
)
191 static char used_path
[PATH_MAX
];
192 static char validated_path
[PATH_MAX
];
198 static const char *suffix
[] = {
199 ".git/.git", "/.git", ".git", "", NULL
,
201 int len
= strlen(path
);
203 while ((1 < len
) && (path
[len
-1] == '/')) {
209 if (path
[0] == '~') {
210 if (!user_path(used_path
, path
, PATH_MAX
))
212 strcpy(validated_path
, path
);
215 else if (PATH_MAX
- 10 < len
)
218 path
= strcpy(used_path
, path
);
219 strcpy(validated_path
, path
);
222 for (i
= 0; suffix
[i
]; i
++) {
223 strcpy(path
+ len
, suffix
[i
]);
224 if (!access(path
, F_OK
)) {
225 strcat(validated_path
, suffix
[i
]);
229 if (!suffix
[i
] || chdir(path
))
231 path
= validated_path
;
233 else if (chdir(path
))
236 if (access("objects", X_OK
) == 0 && access("refs", X_OK
) == 0 &&
237 validate_symref("HEAD") == 0) {
239 check_repository_format();
246 int adjust_shared_perm(const char *path
)
251 if (!shared_repository
)
253 if (lstat(path
, &st
) < 0)
257 mode
|= (shared_repository
== PERM_GROUP
259 : (shared_repository
== PERM_EVERYBODY
267 mode
|= (shared_repository
== PERM_GROUP
269 : (shared_repository
== PERM_EVERYBODY
274 if (chmod(path
, mode
) < 0)