]>
Commit | Line | Data |
---|---|---|
12dccc16 | 1 | #include "cache.h" |
8e440259 | 2 | #include "blob.h" |
8ca12c0d | 3 | #include "dir.h" |
12dccc16 | 4 | |
81a9aa60 KB |
5 | static void create_directories(const char *path, int path_len, |
6 | const struct checkout *state) | |
12dccc16 | 7 | { |
81a9aa60 KB |
8 | char *buf = xmalloc(path_len + 1); |
9 | int len = 0; | |
10 | ||
11 | while (len < path_len) { | |
12 | do { | |
13 | buf[len] = path[len]; | |
14 | len++; | |
15 | } while (len < path_len && path[len] != '/'); | |
16 | if (len >= path_len) | |
17 | break; | |
12dccc16 | 18 | buf[len] = 0; |
fa2e71c9 | 19 | |
bad4a54f KB |
20 | /* |
21 | * For 'checkout-index --prefix=<dir>', <dir> is | |
22 | * allowed to be a symlink to an existing directory, | |
23 | * and we set 'state->base_dir_len' below, such that | |
24 | * we test the path components of the prefix with the | |
25 | * stat() function instead of the lstat() function. | |
26 | */ | |
57199892 | 27 | if (has_dirs_only_path(buf, len, state->base_dir_len)) |
fa2e71c9 JH |
28 | continue; /* ok, it is already a directory. */ |
29 | ||
30 | /* | |
bad4a54f KB |
31 | * If this mkdir() would fail, it could be that there |
32 | * is already a symlink or something else exists | |
33 | * there, therefore we then try to unlink it and try | |
34 | * one more time to create the directory. | |
fa2e71c9 | 35 | */ |
f312de01 | 36 | if (mkdir(buf, 0777)) { |
fa2e71c9 JH |
37 | if (errno == EEXIST && state->force && |
38 | !unlink(buf) && !mkdir(buf, 0777)) | |
39 | continue; | |
12dccc16 LT |
40 | die("cannot create directory at %s", buf); |
41 | } | |
42 | } | |
43 | free(buf); | |
44 | } | |
45 | ||
46 | static void remove_subtree(const char *path) | |
47 | { | |
48 | DIR *dir = opendir(path); | |
49 | struct dirent *de; | |
50 | char pathbuf[PATH_MAX]; | |
51 | char *name; | |
a6080a0a | 52 | |
12dccc16 | 53 | if (!dir) |
cc2903fc | 54 | die("cannot opendir %s (%s)", path, strerror(errno)); |
12dccc16 LT |
55 | strcpy(pathbuf, path); |
56 | name = pathbuf + strlen(path); | |
57 | *name++ = '/'; | |
58 | while ((de = readdir(dir)) != NULL) { | |
59 | struct stat st; | |
8ca12c0d | 60 | if (is_dot_or_dotdot(de->d_name)) |
12dccc16 LT |
61 | continue; |
62 | strcpy(name, de->d_name); | |
63 | if (lstat(pathbuf, &st)) | |
cc2903fc | 64 | die("cannot lstat %s (%s)", pathbuf, strerror(errno)); |
12dccc16 LT |
65 | if (S_ISDIR(st.st_mode)) |
66 | remove_subtree(pathbuf); | |
67 | else if (unlink(pathbuf)) | |
cc2903fc | 68 | die("cannot unlink %s (%s)", pathbuf, strerror(errno)); |
12dccc16 LT |
69 | } |
70 | closedir(dir); | |
71 | if (rmdir(path)) | |
cc2903fc | 72 | die("cannot rmdir %s (%s)", path, strerror(errno)); |
12dccc16 LT |
73 | } |
74 | ||
d48a72f3 | 75 | static int create_file(const char *path, unsigned int mode) |
12dccc16 | 76 | { |
12dccc16 | 77 | mode = (mode & 0100) ? 0777 : 0666; |
781411ed | 78 | return open(path, O_WRONLY | O_CREAT | O_EXCL, mode); |
12dccc16 LT |
79 | } |
80 | ||
f0807e62 LT |
81 | static void *read_blob_entry(struct cache_entry *ce, const char *path, unsigned long *size) |
82 | { | |
83 | enum object_type type; | |
84 | void *new = read_sha1_file(ce->sha1, &type, size); | |
85 | ||
86 | if (new) { | |
87 | if (type == OBJ_BLOB) | |
88 | return new; | |
89 | free(new); | |
90 | } | |
91 | return NULL; | |
92 | } | |
93 | ||
efbc5831 | 94 | static int write_entry(struct cache_entry *ce, char *path, const struct checkout *state, int to_tempfile) |
12dccc16 LT |
95 | { |
96 | int fd; | |
12dccc16 | 97 | long wrote; |
12dccc16 | 98 | |
7a51ed66 | 99 | switch (ce->ce_mode & S_IFMT) { |
5ecd293d PH |
100 | char *new; |
101 | struct strbuf buf; | |
a2d7c6c6 | 102 | unsigned long size; |
6c510bee | 103 | |
12dccc16 | 104 | case S_IFREG: |
f0807e62 LT |
105 | new = read_blob_entry(ce, path, &size); |
106 | if (!new) | |
7e44c935 | 107 | return error("git checkout-index: unable to read sha1 file of %s (%s)", |
f0807e62 | 108 | path, sha1_to_hex(ce->sha1)); |
1a9d7e9b JH |
109 | |
110 | /* | |
111 | * Convert from git internal format to working tree format | |
112 | */ | |
5ecd293d PH |
113 | strbuf_init(&buf, 0); |
114 | if (convert_to_working_tree(ce->name, new, size, &buf)) { | |
c32f749f | 115 | size_t newsize = 0; |
1a9d7e9b | 116 | free(new); |
c32f749f RS |
117 | new = strbuf_detach(&buf, &newsize); |
118 | size = newsize; | |
1a9d7e9b JH |
119 | } |
120 | ||
de84f99c SP |
121 | if (to_tempfile) { |
122 | strcpy(path, ".merge_file_XXXXXX"); | |
123 | fd = mkstemp(path); | |
124 | } else | |
7a51ed66 | 125 | fd = create_file(path, ce->ce_mode); |
12dccc16 LT |
126 | if (fd < 0) { |
127 | free(new); | |
7e44c935 | 128 | return error("git checkout-index: unable to create file %s (%s)", |
12dccc16 LT |
129 | path, strerror(errno)); |
130 | } | |
6c510bee | 131 | |
93822c22 | 132 | wrote = write_in_full(fd, new, size); |
12dccc16 LT |
133 | close(fd); |
134 | free(new); | |
135 | if (wrote != size) | |
7e44c935 | 136 | return error("git checkout-index: unable to write file %s", path); |
12dccc16 LT |
137 | break; |
138 | case S_IFLNK: | |
f0807e62 LT |
139 | new = read_blob_entry(ce, path, &size); |
140 | if (!new) | |
7e44c935 | 141 | return error("git checkout-index: unable to read sha1 file of %s (%s)", |
f0807e62 | 142 | path, sha1_to_hex(ce->sha1)); |
78a8d641 JS |
143 | if (to_tempfile || !has_symlinks) { |
144 | if (to_tempfile) { | |
145 | strcpy(path, ".merge_link_XXXXXX"); | |
146 | fd = mkstemp(path); | |
147 | } else | |
148 | fd = create_file(path, 0666); | |
de84f99c SP |
149 | if (fd < 0) { |
150 | free(new); | |
7e44c935 | 151 | return error("git checkout-index: unable to create " |
de84f99c SP |
152 | "file %s (%s)", path, strerror(errno)); |
153 | } | |
93822c22 | 154 | wrote = write_in_full(fd, new, size); |
de84f99c SP |
155 | close(fd); |
156 | free(new); | |
157 | if (wrote != size) | |
7e44c935 | 158 | return error("git checkout-index: unable to write file %s", |
de84f99c SP |
159 | path); |
160 | } else { | |
161 | wrote = symlink(new, path); | |
12dccc16 | 162 | free(new); |
de84f99c | 163 | if (wrote) |
7e44c935 | 164 | return error("git checkout-index: unable to create " |
de84f99c | 165 | "symlink %s (%s)", path, strerror(errno)); |
12dccc16 | 166 | } |
12dccc16 | 167 | break; |
302b9282 | 168 | case S_IFGITLINK: |
f0807e62 | 169 | if (to_tempfile) |
7e44c935 | 170 | return error("git checkout-index: cannot create temporary subproject %s", path); |
f0807e62 | 171 | if (mkdir(path, 0777) < 0) |
7e44c935 | 172 | return error("git checkout-index: cannot create subproject directory %s", path); |
f0807e62 | 173 | break; |
12dccc16 | 174 | default: |
7e44c935 | 175 | return error("git checkout-index: unknown file mode for %s", path); |
12dccc16 LT |
176 | } |
177 | ||
6ee67f26 | 178 | if (state->refresh_cache) { |
12dccc16 LT |
179 | struct stat st; |
180 | lstat(ce->name, &st); | |
181 | fill_stat_cache_info(ce, &st); | |
182 | } | |
183 | return 0; | |
184 | } | |
185 | ||
efbc5831 | 186 | int checkout_entry(struct cache_entry *ce, const struct checkout *state, char *topath) |
12dccc16 | 187 | { |
095c424d | 188 | static char path[PATH_MAX + 1]; |
de84f99c | 189 | struct stat st; |
12dccc16 LT |
190 | int len = state->base_dir_len; |
191 | ||
de84f99c SP |
192 | if (topath) |
193 | return write_entry(ce, topath, state, 1); | |
194 | ||
12dccc16 LT |
195 | memcpy(path, state->base_dir, len); |
196 | strcpy(path + len, ce->name); | |
81a9aa60 | 197 | len += ce_namelen(ce); |
12dccc16 LT |
198 | |
199 | if (!lstat(path, &st)) { | |
4bd5b7da | 200 | unsigned changed = ce_match_stat(ce, &st, CE_MATCH_IGNORE_VALID); |
12dccc16 LT |
201 | if (!changed) |
202 | return 0; | |
203 | if (!state->force) { | |
204 | if (!state->quiet) | |
215a7ad1 | 205 | fprintf(stderr, "git-checkout-index: %s already exists\n", path); |
4b12dae6 | 206 | return -1; |
12dccc16 LT |
207 | } |
208 | ||
209 | /* | |
210 | * We unlink the old file, to get the new one with the | |
211 | * right permissions (including umask, which is nasty | |
212 | * to emulate by hand - much easier to let the system | |
213 | * just do the right thing) | |
214 | */ | |
d48a72f3 | 215 | if (S_ISDIR(st.st_mode)) { |
f0807e62 | 216 | /* If it is a gitlink, leave it alone! */ |
7a51ed66 | 217 | if (S_ISGITLINK(ce->ce_mode)) |
f0807e62 | 218 | return 0; |
d48a72f3 LT |
219 | if (!state->force) |
220 | return error("%s is a directory", path); | |
221 | remove_subtree(path); | |
971f229c LT |
222 | } else if (unlink(path)) |
223 | return error("unable to unlink old '%s' (%s)", path, strerror(errno)); | |
de84f99c | 224 | } else if (state->not_new) |
12dccc16 | 225 | return 0; |
81a9aa60 | 226 | create_directories(path, len, state); |
de84f99c | 227 | return write_entry(ce, path, state, 0); |
12dccc16 | 228 | } |