Fixed a leak in read-tree
[git.git] / checkout-cache.c
1 /*
2  * Check-out files from the "current cache directory"
3  *
4  * Copyright (C) 2005 Linus Torvalds
5  *
6  * Careful: order of argument flags does matter. For example,
7  *
8  *      checkout-cache -a -f file.c
9  *
10  * Will first check out all files listed in the cache (but not
11  * overwrite any old ones), and then force-checkout "file.c" a
12  * second time (ie that one _will_ overwrite any old contents
13  * with the same filename).
14  *
15  * Also, just doing "checkout-cache" does nothing. You probably
16  * meant "checkout-cache -a". And if you want to force it, you
17  * want "checkout-cache -f -a".
18  *
19  * Intuitiveness is not the goal here. Repeatability is. The
20  * reason for the "no arguments means no work" thing is that
21  * from scripts you are supposed to be able to do things like
22  *
23  *      find . -name '*.h' -print0 | xargs -0 checkout-cache -f --
24  *
25  * which will force all existing *.h files to be replaced with
26  * their cached copies. If an empty command line implied "all",
27  * then this would force-refresh everything in the cache, which
28  * was not the point.
29  *
30  * Oh, and the "--" is just a good idea when you know the rest
31  * will be filenames. Just so that you wouldn't have a filename
32  * of "-a" causing problems (not possible in the above example,
33  * but get used to it in scripting!).
34  */
35 #include "cache.h"
36
37 static int force = 0, quiet = 0, not_new = 0;
38
39 static void create_directories(const char *path)
40 {
41         int len = strlen(path);
42         char *buf = xmalloc(len + 1);
43         const char *slash = path;
44
45         while ((slash = strchr(slash+1, '/')) != NULL) {
46                 len = slash - path;
47                 memcpy(buf, path, len);
48                 buf[len] = 0;
49                 mkdir(buf, 0755);
50         }
51         free(buf);
52 }
53
54 static int create_file(const char *path, unsigned int mode)
55 {
56         int fd;
57
58         mode = (mode & 0100) ? 0777 : 0666;
59         fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode);
60         if (fd < 0) {
61                 if (errno == ENOENT) {
62                         create_directories(path);
63                         fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode);
64                 }
65         }
66         return fd;
67 }
68
69 static int write_entry(struct cache_entry *ce, const char *path)
70 {
71         int fd;
72         void *new;
73         unsigned long size;
74         long wrote;
75         char type[20];
76         char target[1024];
77
78         new = read_sha1_file(ce->sha1, type, &size);
79         if (!new || strcmp(type, "blob")) {
80                 if (new)
81                         free(new);
82                 return error("checkout-cache: unable to read sha1 file of %s (%s)",
83                         path, sha1_to_hex(ce->sha1));
84         }
85         switch (ntohl(ce->ce_mode) & S_IFMT) {
86         case S_IFREG:
87                 fd = create_file(path, ntohl(ce->ce_mode));
88                 if (fd < 0) {
89                         free(new);
90                         return error("checkout-cache: unable to create file %s (%s)",
91                                 path, strerror(errno));
92                 }
93                 wrote = write(fd, new, size);
94                 close(fd);
95                 free(new);
96                 if (wrote != size)
97                         return error("checkout-cache: unable to write file %s", path);
98                 break;
99         case S_IFLNK:
100                 memcpy(target, new, size);
101                 target[size] = '\0';
102                 create_directories(path);
103                 if (symlink(target, path)) {
104                         free(new);
105                         return error("checkout-cache: unable to create symlink %s (%s)",
106                                 path, strerror(errno));
107                 }
108                 free(new);
109                 break;
110         default:
111                 free(new);
112                 return error("checkout-cache: unknown file mode for %s", path);
113         }
114         return 0;
115 }
116
117 static int checkout_entry(struct cache_entry *ce, const char *base_dir)
118 {
119         struct stat st;
120         static char path[MAXPATHLEN+1];
121         int len = strlen(base_dir);
122
123         memcpy(path, base_dir, len);
124         strcpy(path + len, ce->name);
125
126         if (!lstat(path, &st)) {
127                 unsigned changed = cache_match_stat(ce, &st);
128                 if (!changed)
129                         return 0;
130                 if (!force) {
131                         if (!quiet)
132                                 fprintf(stderr, "checkout-cache: %s already exists\n", path);
133                         return 0;
134                 }
135
136                 /*
137                  * We unlink the old file, to get the new one with the
138                  * right permissions (including umask, which is nasty
139                  * to emulate by hand - much easier to let the system
140                  * just do the right thing)
141                  */
142                 unlink(path);
143         } else if (not_new) 
144                 return 0;
145         return write_entry(ce, path);
146 }
147
148 static int checkout_file(const char *name, const char *base_dir)
149 {
150         int pos = cache_name_pos(name, strlen(name));
151         if (pos < 0) {
152                 if (!quiet) {
153                         pos = -pos - 1;
154                         fprintf(stderr,
155                                 "checkout-cache: %s is %s.\n",
156                                 name,
157                                 (pos < active_nr &&
158                                  !strcmp(active_cache[pos]->name, name)) ?
159                                 "unmerged" : "not in the cache");
160                 }
161                 return -1;
162         }
163         return checkout_entry(active_cache[pos], base_dir);
164 }
165
166 static int checkout_all(const char *base_dir)
167 {
168         int i;
169
170         for (i = 0; i < active_nr ; i++) {
171                 struct cache_entry *ce = active_cache[i];
172                 if (ce_stage(ce))
173                         continue;
174                 if (checkout_entry(ce, base_dir) < 0)
175                         return -1;
176         }
177         return 0;
178 }
179
180 int main(int argc, char **argv)
181 {
182         int i, force_filename = 0;
183         const char *base_dir = "";
184
185         if (read_cache() < 0) {
186                 die("invalid cache");
187         }
188
189         for (i = 1; i < argc; i++) {
190                 const char *arg = argv[i];
191                 if (!force_filename) {
192                         if (!strcmp(arg, "-a")) {
193                                 checkout_all(base_dir);
194                                 continue;
195                         }
196                         if (!strcmp(arg, "--")) {
197                                 force_filename = 1;
198                                 continue;
199                         }
200                         if (!strcmp(arg, "-f")) {
201                                 force = 1;
202                                 continue;
203                         }
204                         if (!strcmp(arg, "-q")) {
205                                 quiet = 1;
206                                 continue;
207                         }
208                         if (!strcmp(arg, "-n")) {
209                                 not_new = 1;
210                                 continue;
211                         }
212                         if (!memcmp(arg, "--prefix=", 9)) {
213                                 base_dir = arg+9;
214                                 continue;
215                         }
216                 }
217                 checkout_file(arg, base_dir);
218         }
219         return 0;
220 }