Properly git-bisect reset after bisecting from non-master head
[git/dscho.git] / entry.c
blob6c47c3a3e1acb8badaadad42dfe3d0bd7a06cac3
1 #include <sys/types.h>
2 #include <dirent.h>
3 #include "cache.h"
5 static void create_directories(const char *path, struct checkout *state)
7 int len = strlen(path);
8 char *buf = xmalloc(len + 1);
9 const char *slash = path;
11 while ((slash = strchr(slash+1, '/')) != NULL) {
12 len = slash - path;
13 memcpy(buf, path, len);
14 buf[len] = 0;
15 if (mkdir(buf, 0777)) {
16 if (errno == EEXIST) {
17 struct stat st;
18 if (len > state->base_dir_len && state->force && !unlink(buf) && !mkdir(buf, 0777))
19 continue;
20 if (!stat(buf, &st) && S_ISDIR(st.st_mode))
21 continue; /* ok */
23 die("cannot create directory at %s", buf);
26 free(buf);
29 static void remove_subtree(const char *path)
31 DIR *dir = opendir(path);
32 struct dirent *de;
33 char pathbuf[PATH_MAX];
34 char *name;
36 if (!dir)
37 die("cannot opendir %s", path);
38 strcpy(pathbuf, path);
39 name = pathbuf + strlen(path);
40 *name++ = '/';
41 while ((de = readdir(dir)) != NULL) {
42 struct stat st;
43 if ((de->d_name[0] == '.') &&
44 ((de->d_name[1] == 0) ||
45 ((de->d_name[1] == '.') && de->d_name[2] == 0)))
46 continue;
47 strcpy(name, de->d_name);
48 if (lstat(pathbuf, &st))
49 die("cannot lstat %s", pathbuf);
50 if (S_ISDIR(st.st_mode))
51 remove_subtree(pathbuf);
52 else if (unlink(pathbuf))
53 die("cannot unlink %s", pathbuf);
55 closedir(dir);
56 if (rmdir(path))
57 die("cannot rmdir %s", path);
60 static int create_file(const char *path, unsigned int mode)
62 mode = (mode & 0100) ? 0777 : 0666;
63 return open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
66 static int write_entry(struct cache_entry *ce, const char *path, struct checkout *state)
68 int fd;
69 void *new;
70 unsigned long size;
71 long wrote;
72 char type[20];
74 new = read_sha1_file(ce->sha1, type, &size);
75 if (!new || strcmp(type, "blob")) {
76 if (new)
77 free(new);
78 return error("git-checkout-index: unable to read sha1 file of %s (%s)",
79 path, sha1_to_hex(ce->sha1));
81 switch (ntohl(ce->ce_mode) & S_IFMT) {
82 case S_IFREG:
83 fd = create_file(path, ntohl(ce->ce_mode));
84 if (fd < 0) {
85 free(new);
86 return error("git-checkout-index: unable to create file %s (%s)",
87 path, strerror(errno));
89 wrote = write(fd, new, size);
90 close(fd);
91 free(new);
92 if (wrote != size)
93 return error("git-checkout-index: unable to write file %s", path);
94 break;
95 case S_IFLNK:
96 if (symlink(new, path)) {
97 free(new);
98 return error("git-checkout-index: unable to create "
99 "symlink %s (%s)", path, strerror(errno));
101 free(new);
102 break;
103 default:
104 free(new);
105 return error("git-checkout-index: unknown file mode for %s", path);
108 if (state->refresh_cache) {
109 struct stat st;
110 lstat(ce->name, &st);
111 fill_stat_cache_info(ce, &st);
113 return 0;
116 int checkout_entry(struct cache_entry *ce, struct checkout *state)
118 struct stat st;
119 static char path[MAXPATHLEN+1];
120 int len = state->base_dir_len;
122 memcpy(path, state->base_dir, len);
123 strcpy(path + len, ce->name);
125 if (!lstat(path, &st)) {
126 unsigned changed = ce_match_stat(ce, &st);
127 if (!changed)
128 return 0;
129 if (!state->force) {
130 if (!state->quiet)
131 fprintf(stderr, "git-checkout-index: %s already exists\n", path);
132 return -1;
136 * We unlink the old file, to get the new one with the
137 * right permissions (including umask, which is nasty
138 * to emulate by hand - much easier to let the system
139 * just do the right thing)
141 unlink(path);
142 if (S_ISDIR(st.st_mode)) {
143 if (!state->force)
144 return error("%s is a directory", path);
145 remove_subtree(path);
147 } else if (state->not_new)
148 return 0;
149 create_directories(path, state);
150 return write_entry(ce, path, state);