Code

convert: CRLF_INPUT is a no-op in the output codepath
[git.git] / entry.c
1 #include "cache.h"
2 #include "blob.h"
3 #include "dir.h"
4 #include "streaming.h"
6 static void create_directories(const char *path, int path_len,
7                                const struct checkout *state)
8 {
9         char *buf = xmalloc(path_len + 1);
10         int len = 0;
12         while (len < path_len) {
13                 do {
14                         buf[len] = path[len];
15                         len++;
16                 } while (len < path_len && path[len] != '/');
17                 if (len >= path_len)
18                         break;
19                 buf[len] = 0;
21                 /*
22                  * For 'checkout-index --prefix=<dir>', <dir> is
23                  * allowed to be a symlink to an existing directory,
24                  * and we set 'state->base_dir_len' below, such that
25                  * we test the path components of the prefix with the
26                  * stat() function instead of the lstat() function.
27                  */
28                 if (has_dirs_only_path(buf, len, state->base_dir_len))
29                         continue; /* ok, it is already a directory. */
31                 /*
32                  * If this mkdir() would fail, it could be that there
33                  * is already a symlink or something else exists
34                  * there, therefore we then try to unlink it and try
35                  * one more time to create the directory.
36                  */
37                 if (mkdir(buf, 0777)) {
38                         if (errno == EEXIST && state->force &&
39                             !unlink_or_warn(buf) && !mkdir(buf, 0777))
40                                 continue;
41                         die_errno("cannot create directory at '%s'", buf);
42                 }
43         }
44         free(buf);
45 }
47 static void remove_subtree(const char *path)
48 {
49         DIR *dir = opendir(path);
50         struct dirent *de;
51         char pathbuf[PATH_MAX];
52         char *name;
54         if (!dir)
55                 die_errno("cannot opendir '%s'", path);
56         strcpy(pathbuf, path);
57         name = pathbuf + strlen(path);
58         *name++ = '/';
59         while ((de = readdir(dir)) != NULL) {
60                 struct stat st;
61                 if (is_dot_or_dotdot(de->d_name))
62                         continue;
63                 strcpy(name, de->d_name);
64                 if (lstat(pathbuf, &st))
65                         die_errno("cannot lstat '%s'", pathbuf);
66                 if (S_ISDIR(st.st_mode))
67                         remove_subtree(pathbuf);
68                 else if (unlink(pathbuf))
69                         die_errno("cannot unlink '%s'", pathbuf);
70         }
71         closedir(dir);
72         if (rmdir(path))
73                 die_errno("cannot rmdir '%s'", path);
74 }
76 static int create_file(const char *path, unsigned int mode)
77 {
78         mode = (mode & 0100) ? 0777 : 0666;
79         return open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
80 }
82 static void *read_blob_entry(struct cache_entry *ce, unsigned long *size)
83 {
84         enum object_type type;
85         void *new = read_sha1_file(ce->sha1, &type, size);
87         if (new) {
88                 if (type == OBJ_BLOB)
89                         return new;
90                 free(new);
91         }
92         return NULL;
93 }
95 static int open_output_fd(char *path, struct cache_entry *ce, int to_tempfile)
96 {
97         int symlink = (ce->ce_mode & S_IFMT) != S_IFREG;
98         if (to_tempfile) {
99                 strcpy(path, symlink
100                        ? ".merge_link_XXXXXX" : ".merge_file_XXXXXX");
101                 return mkstemp(path);
102         } else {
103                 return create_file(path, !symlink ? ce->ce_mode : 0666);
104         }
107 static int fstat_output(int fd, const struct checkout *state, struct stat *st)
109         /* use fstat() only when path == ce->name */
110         if (fstat_is_reliable() &&
111             state->refresh_cache && !state->base_dir_len) {
112                 fstat(fd, st);
113                 return 1;
114         }
115         return 0;
118 static int streaming_write_entry(struct cache_entry *ce, char *path,
119                                  const struct checkout *state, int to_tempfile,
120                                  int *fstat_done, struct stat *statbuf)
122         struct git_istream *st;
123         enum object_type type;
124         unsigned long sz;
125         int result = -1;
126         int fd = -1;
128         st = open_istream(ce->sha1, &type, &sz);
129         if (!st)
130                 return -1;
131         if (type != OBJ_BLOB)
132                 goto close_and_exit;
134         fd = open_output_fd(path, ce, to_tempfile);
135         if (fd < 0)
136                 goto close_and_exit;
138         for (;;) {
139                 char buf[10240];
140                 ssize_t wrote;
141                 ssize_t readlen = read_istream(st, buf, sizeof(buf));
143                 if (!readlen)
144                         break;
146                 wrote = write_in_full(fd, buf, readlen);
148                 if (wrote != readlen)
149                         goto close_and_exit;
150         }
151         *fstat_done = fstat_output(fd, state, statbuf);
153 close_and_exit:
154         close_istream(st);
155         if (0 <= fd)
156                 result = close(fd);
157         if (result && 0 <= fd)
158                 unlink(path);
159         return result;
162 static int write_entry(struct cache_entry *ce, char *path, const struct checkout *state, int to_tempfile)
164         unsigned int ce_mode_s_ifmt = ce->ce_mode & S_IFMT;
165         int fd, ret, fstat_done = 0;
166         char *new;
167         struct strbuf buf = STRBUF_INIT;
168         unsigned long size;
169         size_t wrote, newsize = 0;
170         struct stat st;
172         if ((ce_mode_s_ifmt == S_IFREG) &&
173             can_bypass_conversion(path) &&
174             !streaming_write_entry(ce, path, state, to_tempfile,
175                                    &fstat_done, &st))
176                 goto finish;
178         switch (ce_mode_s_ifmt) {
179         case S_IFREG:
180         case S_IFLNK:
181                 new = read_blob_entry(ce, &size);
182                 if (!new)
183                         return error("unable to read sha1 file of %s (%s)",
184                                 path, sha1_to_hex(ce->sha1));
186                 if (ce_mode_s_ifmt == S_IFLNK && has_symlinks && !to_tempfile) {
187                         ret = symlink(new, path);
188                         free(new);
189                         if (ret)
190                                 return error("unable to create symlink %s (%s)",
191                                              path, strerror(errno));
192                         break;
193                 }
195                 /*
196                  * Convert from git internal format to working tree format
197                  */
198                 if (ce_mode_s_ifmt == S_IFREG &&
199                     convert_to_working_tree(ce->name, new, size, &buf)) {
200                         free(new);
201                         new = strbuf_detach(&buf, &newsize);
202                         size = newsize;
203                 }
205                 fd = open_output_fd(path, ce, to_tempfile);
206                 if (fd < 0) {
207                         free(new);
208                         return error("unable to create file %s (%s)",
209                                 path, strerror(errno));
210                 }
212                 wrote = write_in_full(fd, new, size);
213                 if (!to_tempfile)
214                         fstat_done = fstat_output(fd, state, &st);
215                 close(fd);
216                 free(new);
217                 if (wrote != size)
218                         return error("unable to write file %s", path);
219                 break;
220         case S_IFGITLINK:
221                 if (to_tempfile)
222                         return error("cannot create temporary subproject %s", path);
223                 if (mkdir(path, 0777) < 0)
224                         return error("cannot create subproject directory %s", path);
225                 break;
226         default:
227                 return error("unknown file mode for %s in index", path);
228         }
230 finish:
231         if (state->refresh_cache) {
232                 if (!fstat_done)
233                         lstat(ce->name, &st);
234                 fill_stat_cache_info(ce, &st);
235         }
236         return 0;
239 /*
240  * This is like 'lstat()', except it refuses to follow symlinks
241  * in the path, after skipping "skiplen".
242  */
243 static int check_path(const char *path, int len, struct stat *st, int skiplen)
245         const char *slash = path + len;
247         while (path < slash && *slash != '/')
248                 slash--;
249         if (!has_dirs_only_path(path, slash - path, skiplen)) {
250                 errno = ENOENT;
251                 return -1;
252         }
253         return lstat(path, st);
256 int checkout_entry(struct cache_entry *ce, const struct checkout *state, char *topath)
258         static char path[PATH_MAX + 1];
259         struct stat st;
260         int len = state->base_dir_len;
262         if (topath)
263                 return write_entry(ce, topath, state, 1);
265         memcpy(path, state->base_dir, len);
266         strcpy(path + len, ce->name);
267         len += ce_namelen(ce);
269         if (!check_path(path, len, &st, state->base_dir_len)) {
270                 unsigned changed = ce_match_stat(ce, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
271                 if (!changed)
272                         return 0;
273                 if (!state->force) {
274                         if (!state->quiet)
275                                 fprintf(stderr, "%s already exists, no checkout\n", path);
276                         return -1;
277                 }
279                 /*
280                  * We unlink the old file, to get the new one with the
281                  * right permissions (including umask, which is nasty
282                  * to emulate by hand - much easier to let the system
283                  * just do the right thing)
284                  */
285                 if (S_ISDIR(st.st_mode)) {
286                         /* If it is a gitlink, leave it alone! */
287                         if (S_ISGITLINK(ce->ce_mode))
288                                 return 0;
289                         if (!state->force)
290                                 return error("%s is a directory", path);
291                         remove_subtree(path);
292                 } else if (unlink(path))
293                         return error("unable to unlink old '%s' (%s)", path, strerror(errno));
294         } else if (state->not_new)
295                 return 0;
296         create_directories(path, len, state);
297         return write_entry(ce, path, state, 0);