2 * Check-out files from the "current cache directory"
4 * Copyright (C) 2005 Linus Torvalds
6 * Careful: order of argument flags does matter. For example,
8 * checkout-cache -a -f file.c
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).
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".
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
23 * find . -name '*.h' -print0 | xargs -0 checkout-cache -f --
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
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!).
35 #include <sys/types.h>
39 static int force
= 0, quiet
= 0, not_new
= 0;
41 static void create_directories(const char *path
)
43 int len
= strlen(path
);
44 char *buf
= xmalloc(len
+ 1);
45 const char *slash
= path
;
47 while ((slash
= strchr(slash
+1, '/')) != NULL
) {
49 memcpy(buf
, path
, len
);
51 if (mkdir(buf
, 0755)) {
52 if (errno
== EEXIST
) {
54 if (!lstat(buf
, &st
) && S_ISDIR(st
.st_mode
))
56 if (force
&& !unlink(buf
) && !mkdir(buf
, 0755))
59 die("cannot create directory at %s", buf
);
65 static void remove_subtree(const char *path
)
67 DIR *dir
= opendir(path
);
69 char pathbuf
[PATH_MAX
];
73 die("cannot opendir %s", path
);
74 strcpy(pathbuf
, path
);
75 name
= pathbuf
+ strlen(path
);
77 while ((de
= readdir(dir
)) != NULL
) {
79 if ((de
->d_name
[0] == '.') &&
80 ((de
->d_name
[1] == 0) ||
81 ((de
->d_name
[1] == '.') && de
->d_name
[2] == 0)))
83 strcpy(name
, de
->d_name
);
84 if (lstat(pathbuf
, &st
))
85 die("cannot lstat %s", pathbuf
);
86 if (S_ISDIR(st
.st_mode
))
87 remove_subtree(pathbuf
);
88 else if (unlink(pathbuf
))
89 die("cannot unlink %s", pathbuf
);
93 die("cannot rmdir %s", path
);
96 static int create_file(const char *path
, unsigned int mode
)
100 mode
= (mode
& 0100) ? 0777 : 0666;
101 create_directories(path
);
102 fd
= open(path
, O_WRONLY
| O_TRUNC
| O_CREAT
, mode
);
104 if (errno
== EISDIR
&& force
) {
105 remove_subtree(path
);
106 fd
= open(path
, O_WRONLY
| O_TRUNC
| O_CREAT
, mode
);
112 static int write_entry(struct cache_entry
*ce
, const char *path
)
121 new = read_sha1_file(ce
->sha1
, type
, &size
);
122 if (!new || strcmp(type
, "blob")) {
125 return error("checkout-cache: unable to read sha1 file of %s (%s)",
126 path
, sha1_to_hex(ce
->sha1
));
128 switch (ntohl(ce
->ce_mode
) & S_IFMT
) {
130 fd
= create_file(path
, ntohl(ce
->ce_mode
));
133 return error("checkout-cache: unable to create file %s (%s)",
134 path
, strerror(errno
));
136 wrote
= write(fd
, new, size
);
140 return error("checkout-cache: unable to write file %s", path
);
143 memcpy(target
, new, size
);
145 create_directories(path
);
146 if (symlink(target
, path
)) {
148 return error("checkout-cache: unable to create symlink %s (%s)",
149 path
, strerror(errno
));
155 return error("checkout-cache: unknown file mode for %s", path
);
160 static int checkout_entry(struct cache_entry
*ce
, const char *base_dir
)
163 static char path
[MAXPATHLEN
+1];
164 int len
= strlen(base_dir
);
166 memcpy(path
, base_dir
, len
);
167 strcpy(path
+ len
, ce
->name
);
169 if (!lstat(path
, &st
)) {
170 unsigned changed
= cache_match_stat(ce
, &st
);
175 fprintf(stderr
, "checkout-cache: %s already exists\n", path
);
180 * We unlink the old file, to get the new one with the
181 * right permissions (including umask, which is nasty
182 * to emulate by hand - much easier to let the system
183 * just do the right thing)
188 return write_entry(ce
, path
);
191 static int checkout_file(const char *name
, const char *base_dir
)
193 int pos
= cache_name_pos(name
, strlen(name
));
198 "checkout-cache: %s is %s.\n",
201 !strcmp(active_cache
[pos
]->name
, name
)) ?
202 "unmerged" : "not in the cache");
206 return checkout_entry(active_cache
[pos
], base_dir
);
209 static int checkout_all(const char *base_dir
)
213 for (i
= 0; i
< active_nr
; i
++) {
214 struct cache_entry
*ce
= active_cache
[i
];
217 if (checkout_entry(ce
, base_dir
) < 0)
223 int main(int argc
, char **argv
)
225 int i
, force_filename
= 0;
226 const char *base_dir
= "";
228 if (read_cache() < 0) {
229 die("invalid cache");
232 for (i
= 1; i
< argc
; i
++) {
233 const char *arg
= argv
[i
];
234 if (!force_filename
) {
235 if (!strcmp(arg
, "-a")) {
236 checkout_all(base_dir
);
239 if (!strcmp(arg
, "--")) {
243 if (!strcmp(arg
, "-f")) {
247 if (!strcmp(arg
, "-q")) {
251 if (!strcmp(arg
, "-n")) {
255 if (!memcmp(arg
, "--prefix=", 9)) {
260 checkout_file(arg
, base_dir
);