Library code for user-relative paths, take three.
[git.git] / path.c
1 /*
2  * I'm tired of doing "vsnprintf()" etc just to open a
3  * file, so here's a "return static buffer with printf"
4  * interface for paths.
5  *
6  * It's obviously not thread-safe. Sue me. But it's quite
7  * useful for doing things like
8  *
9  *   f = open(mkpath("%s/%s.git", base, name), O_RDONLY);
10  *
11  * which is what it's designed for.
12  */
13 #include "cache.h"
14 #include <pwd.h>
15
16 static char pathname[PATH_MAX];
17 static char bad_path[] = "/bad-path/";
18
19 static char *cleanup_path(char *path)
20 {
21         /* Clean it up */
22         if (!memcmp(path, "./", 2)) {
23                 path += 2;
24                 while (*path == '/')
25                         path++;
26         }
27         return path;
28 }
29
30 char *mkpath(const char *fmt, ...)
31 {
32         va_list args;
33         unsigned len;
34
35         va_start(args, fmt);
36         len = vsnprintf(pathname, PATH_MAX, fmt, args);
37         va_end(args);
38         if (len >= PATH_MAX)
39                 return bad_path;
40         return cleanup_path(pathname);
41 }
42
43 char *git_path(const char *fmt, ...)
44 {
45         const char *git_dir = get_git_dir();
46         va_list args;
47         unsigned len;
48
49         len = strlen(git_dir);
50         if (len > PATH_MAX-100)
51                 return bad_path;
52         memcpy(pathname, git_dir, len);
53         if (len && git_dir[len-1] != '/')
54                 pathname[len++] = '/';
55         va_start(args, fmt);
56         len += vsnprintf(pathname + len, PATH_MAX - len, fmt, args);
57         va_end(args);
58         if (len >= PATH_MAX)
59                 return bad_path;
60         return cleanup_path(pathname);
61 }
62
63
64 /* git_mkstemp() - create tmp file honoring TMPDIR variable */
65 int git_mkstemp(char *path, size_t len, const char *template)
66 {
67         char *env, *pch = path;
68
69         if ((env = getenv("TMPDIR")) == NULL) {
70                 strcpy(pch, "/tmp/");
71                 len -= 5;
72                 pch += 5;
73         } else {
74                 size_t n = snprintf(pch, len, "%s/", env);
75
76                 len -= n;
77                 pch += n;
78         }
79
80         safe_strncpy(pch, template, len);
81
82         return mkstemp(path);
83 }
84
85
86 char *safe_strncpy(char *dest, const char *src, size_t n)
87 {
88         strncpy(dest, src, n);
89         dest[n - 1] = '\0';
90
91         return dest;
92 }
93
94 static char *current_dir()
95 {
96         return getcwd(pathname, sizeof(pathname));
97 }
98
99 /* Take a raw path from is_git_repo() and canonicalize it using Linus'
100  * idea of a blind chdir() and getcwd(). */
101 static const char *canonical_path(char *path, int strict)
102 {
103         char *dir = path;
104
105         if(strict && *dir != '/')
106                 return NULL;
107
108         if(*dir == '~') {               /* user-relative path */
109                 struct passwd *pw;
110                 char *slash = strchr(dir, '/');
111
112                 dir++;
113                 /* '~/' and '~' (no slash) means users own home-dir */
114                 if(!*dir || *dir == '/')
115                         pw = getpwuid(getuid());
116                 else {
117                         if (slash) {
118                                 *slash = '\0';
119                                 pw = getpwnam(dir);
120                                 *slash = '/';
121                         }
122                         else
123                                 pw = getpwnam(dir);
124                 }
125
126                 /* make sure we got something back that we can chdir() to */
127                 if(!pw || chdir(pw->pw_dir) < 0)
128                         return NULL;
129
130                 if(!slash || !slash[1]) /* no path following username */
131                         return current_dir();
132
133                 dir = slash + 1;
134         }
135
136         /* ~foo/path/to/repo is now path/to/repo and we're in foo's homedir */
137         if(chdir(dir) < 0)
138                 return NULL;
139
140         return current_dir();
141 }
142
143 char *enter_repo(char *path, int strict)
144 {
145         if(!path)
146                 return NULL;
147
148         if(!canonical_path(path, strict)) {
149                 if(strict || !canonical_path(mkpath("%s.git", path), strict))
150                         return NULL;
151         }
152
153         /* This is perfectly safe, and people tend to think of the directory
154          * where they ran git-init-db as their repository, so humour them. */
155         (void)chdir(".git");
156
157         if(access("objects", X_OK) == 0 && access("refs", X_OK) == 0) {
158                 putenv("GIT_DIR=.");
159                 return current_dir();
160         }
161
162         return NULL;
163 }