Libify the index refresh logic
[git/jnareb-git.git] / entry.c
blob793724fd52063157545f523f6d88953b96385c2c
1 #include <sys/types.h>
2 #include <dirent.h>
3 #include "cache.h"
4 #include "blob.h"
6 static void create_directories(const char *path, struct checkout *state)
8 int len = strlen(path);
9 char *buf = xmalloc(len + 1);
10 const char *slash = path;
12 while ((slash = strchr(slash+1, '/')) != NULL) {
13 len = slash - path;
14 memcpy(buf, path, len);
15 buf[len] = 0;
16 if (mkdir(buf, 0777)) {
17 if (errno == EEXIST) {
18 struct stat st;
19 if (len > state->base_dir_len && state->force && !unlink(buf) && !mkdir(buf, 0777))
20 continue;
21 if (!stat(buf, &st) && S_ISDIR(st.st_mode))
22 continue; /* ok */
24 die("cannot create directory at %s", buf);
27 free(buf);
30 static void remove_subtree(const char *path)
32 DIR *dir = opendir(path);
33 struct dirent *de;
34 char pathbuf[PATH_MAX];
35 char *name;
37 if (!dir)
38 die("cannot opendir %s", path);
39 strcpy(pathbuf, path);
40 name = pathbuf + strlen(path);
41 *name++ = '/';
42 while ((de = readdir(dir)) != NULL) {
43 struct stat st;
44 if ((de->d_name[0] == '.') &&
45 ((de->d_name[1] == 0) ||
46 ((de->d_name[1] == '.') && de->d_name[2] == 0)))
47 continue;
48 strcpy(name, de->d_name);
49 if (lstat(pathbuf, &st))
50 die("cannot lstat %s", pathbuf);
51 if (S_ISDIR(st.st_mode))
52 remove_subtree(pathbuf);
53 else if (unlink(pathbuf))
54 die("cannot unlink %s", pathbuf);
56 closedir(dir);
57 if (rmdir(path))
58 die("cannot rmdir %s", path);
61 static int create_file(const char *path, unsigned int mode)
63 mode = (mode & 0100) ? 0777 : 0666;
64 return open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
67 static int write_entry(struct cache_entry *ce, char *path, struct checkout *state, int to_tempfile)
69 int fd;
70 void *new;
71 unsigned long size;
72 long wrote;
73 char type[20];
75 new = read_sha1_file(ce->sha1, type, &size);
76 if (!new || strcmp(type, blob_type)) {
77 if (new)
78 free(new);
79 return error("git-checkout-index: unable to read sha1 file of %s (%s)",
80 path, sha1_to_hex(ce->sha1));
82 switch (ntohl(ce->ce_mode) & S_IFMT) {
83 case S_IFREG:
84 if (to_tempfile) {
85 strcpy(path, ".merge_file_XXXXXX");
86 fd = mkstemp(path);
87 } else
88 fd = create_file(path, ntohl(ce->ce_mode));
89 if (fd < 0) {
90 free(new);
91 return error("git-checkout-index: unable to create file %s (%s)",
92 path, strerror(errno));
94 wrote = write(fd, new, size);
95 close(fd);
96 free(new);
97 if (wrote != size)
98 return error("git-checkout-index: unable to write file %s", path);
99 break;
100 case S_IFLNK:
101 if (to_tempfile) {
102 strcpy(path, ".merge_link_XXXXXX");
103 fd = mkstemp(path);
104 if (fd < 0) {
105 free(new);
106 return error("git-checkout-index: unable to create "
107 "file %s (%s)", path, strerror(errno));
109 wrote = write(fd, new, size);
110 close(fd);
111 free(new);
112 if (wrote != size)
113 return error("git-checkout-index: unable to write file %s",
114 path);
115 } else {
116 wrote = symlink(new, path);
117 free(new);
118 if (wrote)
119 return error("git-checkout-index: unable to create "
120 "symlink %s (%s)", path, strerror(errno));
122 break;
123 default:
124 free(new);
125 return error("git-checkout-index: unknown file mode for %s", path);
128 if (state->refresh_cache) {
129 struct stat st;
130 lstat(ce->name, &st);
131 fill_stat_cache_info(ce, &st);
133 return 0;
136 int checkout_entry(struct cache_entry *ce, struct checkout *state, char *topath)
138 static char path[MAXPATHLEN+1];
139 struct stat st;
140 int len = state->base_dir_len;
142 if (topath)
143 return write_entry(ce, topath, state, 1);
145 memcpy(path, state->base_dir, len);
146 strcpy(path + len, ce->name);
148 if (!lstat(path, &st)) {
149 unsigned changed = ce_match_stat(ce, &st, 1);
150 if (!changed)
151 return 0;
152 if (!state->force) {
153 if (!state->quiet)
154 fprintf(stderr, "git-checkout-index: %s already exists\n", path);
155 return -1;
159 * We unlink the old file, to get the new one with the
160 * right permissions (including umask, which is nasty
161 * to emulate by hand - much easier to let the system
162 * just do the right thing)
164 unlink(path);
165 if (S_ISDIR(st.st_mode)) {
166 if (!state->force)
167 return error("%s is a directory", path);
168 remove_subtree(path);
170 } else if (state->not_new)
171 return 0;
172 create_directories(path, state);
173 return write_entry(ce, path, state, 0);