]> git.ipfire.org Git - thirdparty/git.git/blame - entry.c
Add basic infrastructure to assign attributes to paths
[thirdparty/git.git] / entry.c
CommitLineData
12dccc16 1#include "cache.h"
8e440259 2#include "blob.h"
12dccc16
LT
3
4static void create_directories(const char *path, struct checkout *state)
5{
6 int len = strlen(path);
7 char *buf = xmalloc(len + 1);
8 const char *slash = path;
9
10 while ((slash = strchr(slash+1, '/')) != NULL) {
11 len = slash - path;
12 memcpy(buf, path, len);
13 buf[len] = 0;
f312de01 14 if (mkdir(buf, 0777)) {
12dccc16
LT
15 if (errno == EEXIST) {
16 struct stat st;
f312de01 17 if (len > state->base_dir_len && state->force && !unlink(buf) && !mkdir(buf, 0777))
12dccc16
LT
18 continue;
19 if (!stat(buf, &st) && S_ISDIR(st.st_mode))
20 continue; /* ok */
21 }
22 die("cannot create directory at %s", buf);
23 }
24 }
25 free(buf);
26}
27
28static void remove_subtree(const char *path)
29{
30 DIR *dir = opendir(path);
31 struct dirent *de;
32 char pathbuf[PATH_MAX];
33 char *name;
34
35 if (!dir)
36 die("cannot opendir %s", path);
37 strcpy(pathbuf, path);
38 name = pathbuf + strlen(path);
39 *name++ = '/';
40 while ((de = readdir(dir)) != NULL) {
41 struct stat st;
42 if ((de->d_name[0] == '.') &&
43 ((de->d_name[1] == 0) ||
44 ((de->d_name[1] == '.') && de->d_name[2] == 0)))
45 continue;
46 strcpy(name, de->d_name);
47 if (lstat(pathbuf, &st))
48 die("cannot lstat %s", pathbuf);
49 if (S_ISDIR(st.st_mode))
50 remove_subtree(pathbuf);
51 else if (unlink(pathbuf))
52 die("cannot unlink %s", pathbuf);
53 }
54 closedir(dir);
55 if (rmdir(path))
56 die("cannot rmdir %s", path);
57}
58
d48a72f3 59static int create_file(const char *path, unsigned int mode)
12dccc16 60{
12dccc16 61 mode = (mode & 0100) ? 0777 : 0666;
781411ed 62 return open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
12dccc16
LT
63}
64
de84f99c 65static int write_entry(struct cache_entry *ce, char *path, struct checkout *state, int to_tempfile)
12dccc16
LT
66{
67 int fd;
68 void *new;
69 unsigned long size;
70 long wrote;
21666f1a 71 enum object_type type;
12dccc16 72
21666f1a
NP
73 new = read_sha1_file(ce->sha1, &type, &size);
74 if (!new || type != OBJ_BLOB) {
12dccc16
LT
75 if (new)
76 free(new);
215a7ad1 77 return error("git-checkout-index: unable to read sha1 file of %s (%s)",
12dccc16
LT
78 path, sha1_to_hex(ce->sha1));
79 }
80 switch (ntohl(ce->ce_mode) & S_IFMT) {
6c510bee
LT
81 char *buf;
82 unsigned long nsize;
83
12dccc16 84 case S_IFREG:
de84f99c
SP
85 if (to_tempfile) {
86 strcpy(path, ".merge_file_XXXXXX");
87 fd = mkstemp(path);
88 } else
89 fd = create_file(path, ntohl(ce->ce_mode));
12dccc16
LT
90 if (fd < 0) {
91 free(new);
215a7ad1 92 return error("git-checkout-index: unable to create file %s (%s)",
12dccc16
LT
93 path, strerror(errno));
94 }
6c510bee
LT
95
96 /*
97 * Convert from git internal format to working tree format
98 */
99 buf = new;
100 nsize = size;
101 if (convert_to_working_tree(ce->name, &buf, &nsize)) {
102 free(new);
103 new = buf;
104 size = nsize;
105 }
106
93822c22 107 wrote = write_in_full(fd, new, size);
12dccc16
LT
108 close(fd);
109 free(new);
110 if (wrote != size)
215a7ad1 111 return error("git-checkout-index: unable to write file %s", path);
12dccc16
LT
112 break;
113 case S_IFLNK:
78a8d641
JS
114 if (to_tempfile || !has_symlinks) {
115 if (to_tempfile) {
116 strcpy(path, ".merge_link_XXXXXX");
117 fd = mkstemp(path);
118 } else
119 fd = create_file(path, 0666);
de84f99c
SP
120 if (fd < 0) {
121 free(new);
122 return error("git-checkout-index: unable to create "
123 "file %s (%s)", path, strerror(errno));
124 }
93822c22 125 wrote = write_in_full(fd, new, size);
de84f99c
SP
126 close(fd);
127 free(new);
128 if (wrote != size)
129 return error("git-checkout-index: unable to write file %s",
130 path);
131 } else {
132 wrote = symlink(new, path);
12dccc16 133 free(new);
de84f99c
SP
134 if (wrote)
135 return error("git-checkout-index: unable to create "
136 "symlink %s (%s)", path, strerror(errno));
12dccc16 137 }
12dccc16
LT
138 break;
139 default:
140 free(new);
215a7ad1 141 return error("git-checkout-index: unknown file mode for %s", path);
12dccc16
LT
142 }
143
6ee67f26 144 if (state->refresh_cache) {
12dccc16
LT
145 struct stat st;
146 lstat(ce->name, &st);
147 fill_stat_cache_info(ce, &st);
148 }
149 return 0;
150}
151
de84f99c 152int checkout_entry(struct cache_entry *ce, struct checkout *state, char *topath)
12dccc16 153{
095c424d 154 static char path[PATH_MAX + 1];
de84f99c 155 struct stat st;
12dccc16
LT
156 int len = state->base_dir_len;
157
de84f99c
SP
158 if (topath)
159 return write_entry(ce, topath, state, 1);
160
12dccc16
LT
161 memcpy(path, state->base_dir, len);
162 strcpy(path + len, ce->name);
163
164 if (!lstat(path, &st)) {
5f73076c 165 unsigned changed = ce_match_stat(ce, &st, 1);
12dccc16
LT
166 if (!changed)
167 return 0;
168 if (!state->force) {
169 if (!state->quiet)
215a7ad1 170 fprintf(stderr, "git-checkout-index: %s already exists\n", path);
4b12dae6 171 return -1;
12dccc16
LT
172 }
173
174 /*
175 * We unlink the old file, to get the new one with the
176 * right permissions (including umask, which is nasty
177 * to emulate by hand - much easier to let the system
178 * just do the right thing)
179 */
180 unlink(path);
d48a72f3
LT
181 if (S_ISDIR(st.st_mode)) {
182 if (!state->force)
183 return error("%s is a directory", path);
184 remove_subtree(path);
185 }
de84f99c 186 } else if (state->not_new)
12dccc16
LT
187 return 0;
188 create_directories(path, state);
de84f99c 189 return write_entry(ce, path, state, 0);
12dccc16 190}