Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 1 | #include "cache.h" |
Peter Eriksen | 8e44025 | 2006-04-02 14:44:09 +0200 | [diff] [blame] | 2 | #include "blob.h" |
Alexander Potashev | 8ca12c0 | 2009-01-10 15:07:50 +0300 | [diff] [blame] | 3 | #include "dir.h" |
Junio C Hamano | dd8e912 | 2011-05-12 14:31:08 -0700 | [diff] [blame] | 4 | #include "streaming.h" |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 5 | |
Kjetil Barvik | 81a9aa6 | 2009-02-09 21:54:08 +0100 | [diff] [blame] | 6 | static void create_directories(const char *path, int path_len, |
| 7 | const struct checkout *state) |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 8 | { |
Jeff King | 3733e69 | 2016-02-22 17:44:28 -0500 | [diff] [blame] | 9 | char *buf = xmallocz(path_len); |
Kjetil Barvik | 81a9aa6 | 2009-02-09 21:54:08 +0100 | [diff] [blame] | 10 | int len = 0; |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 11 | |
Kjetil Barvik | 81a9aa6 | 2009-02-09 21:54:08 +0100 | [diff] [blame] | 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; |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 19 | buf[len] = 0; |
Junio C Hamano | fa2e71c | 2007-07-17 22:58:28 -0700 | [diff] [blame] | 20 | |
Kjetil Barvik | bad4a54 | 2009-01-18 16:14:52 +0100 | [diff] [blame] | 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 | */ |
Kjetil Barvik | 5719989 | 2009-02-09 21:54:06 +0100 | [diff] [blame] | 28 | if (has_dirs_only_path(buf, len, state->base_dir_len)) |
Junio C Hamano | fa2e71c | 2007-07-17 22:58:28 -0700 | [diff] [blame] | 29 | continue; /* ok, it is already a directory. */ |
| 30 | |
| 31 | /* |
Kjetil Barvik | bad4a54 | 2009-01-18 16:14:52 +0100 | [diff] [blame] | 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. |
Junio C Hamano | fa2e71c | 2007-07-17 22:58:28 -0700 | [diff] [blame] | 36 | */ |
Junio C Hamano | f312de0 | 2005-07-06 01:21:46 -0700 | [diff] [blame] | 37 | if (mkdir(buf, 0777)) { |
Junio C Hamano | fa2e71c | 2007-07-17 22:58:28 -0700 | [diff] [blame] | 38 | if (errno == EEXIST && state->force && |
Alex Riesen | 691f1a2 | 2009-04-29 23:22:56 +0200 | [diff] [blame] | 39 | !unlink_or_warn(buf) && !mkdir(buf, 0777)) |
Junio C Hamano | fa2e71c | 2007-07-17 22:58:28 -0700 | [diff] [blame] | 40 | continue; |
Thomas Rast | 0721c31 | 2009-06-27 17:58:47 +0200 | [diff] [blame] | 41 | die_errno("cannot create directory at '%s'", buf); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 42 | } |
| 43 | } |
| 44 | free(buf); |
| 45 | } |
| 46 | |
Michael Haggerty | 2f29e0c | 2014-03-13 10:19:08 +0100 | [diff] [blame] | 47 | static void remove_subtree(struct strbuf *path) |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 48 | { |
Michael Haggerty | 2f29e0c | 2014-03-13 10:19:08 +0100 | [diff] [blame] | 49 | DIR *dir = opendir(path->buf); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 50 | struct dirent *de; |
Michael Haggerty | 2f29e0c | 2014-03-13 10:19:08 +0100 | [diff] [blame] | 51 | int origlen = path->len; |
Junio C Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 52 | |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 53 | if (!dir) |
Michael Haggerty | 2f29e0c | 2014-03-13 10:19:08 +0100 | [diff] [blame] | 54 | die_errno("cannot opendir '%s'", path->buf); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 55 | while ((de = readdir(dir)) != NULL) { |
| 56 | struct stat st; |
Michael Haggerty | 2f29e0c | 2014-03-13 10:19:08 +0100 | [diff] [blame] | 57 | |
Alexander Potashev | 8ca12c0 | 2009-01-10 15:07:50 +0300 | [diff] [blame] | 58 | if (is_dot_or_dotdot(de->d_name)) |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 59 | continue; |
Michael Haggerty | 2f29e0c | 2014-03-13 10:19:08 +0100 | [diff] [blame] | 60 | |
| 61 | strbuf_addch(path, '/'); |
| 62 | strbuf_addstr(path, de->d_name); |
| 63 | if (lstat(path->buf, &st)) |
| 64 | die_errno("cannot lstat '%s'", path->buf); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 65 | if (S_ISDIR(st.st_mode)) |
Michael Haggerty | 2f29e0c | 2014-03-13 10:19:08 +0100 | [diff] [blame] | 66 | remove_subtree(path); |
| 67 | else if (unlink(path->buf)) |
| 68 | die_errno("cannot unlink '%s'", path->buf); |
| 69 | strbuf_setlen(path, origlen); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 70 | } |
| 71 | closedir(dir); |
Michael Haggerty | 2f29e0c | 2014-03-13 10:19:08 +0100 | [diff] [blame] | 72 | if (rmdir(path->buf)) |
| 73 | die_errno("cannot rmdir '%s'", path->buf); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Linus Torvalds | d48a72f | 2005-07-14 09:58:45 -0700 | [diff] [blame] | 76 | static int create_file(const char *path, unsigned int mode) |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 77 | { |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 78 | mode = (mode & 0100) ? 0777 : 0666; |
Alex Riesen | 781411e | 2006-01-05 09:58:06 +0100 | [diff] [blame] | 79 | return open(path, O_WRONLY | O_CREAT | O_EXCL, mode); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 82 | static void *read_blob_entry(const struct cache_entry *ce, unsigned long *size) |
Linus Torvalds | f0807e6 | 2007-04-13 09:26:04 -0700 | [diff] [blame] | 83 | { |
| 84 | enum object_type type; |
| 85 | void *new = read_sha1_file(ce->sha1, &type, size); |
| 86 | |
| 87 | if (new) { |
| 88 | if (type == OBJ_BLOB) |
| 89 | return new; |
| 90 | free(new); |
| 91 | } |
| 92 | return NULL; |
| 93 | } |
| 94 | |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 95 | static int open_output_fd(char *path, const struct cache_entry *ce, int to_tempfile) |
Junio C Hamano | fd5db55 | 2011-05-12 21:36:42 -0700 | [diff] [blame] | 96 | { |
| 97 | int symlink = (ce->ce_mode & S_IFMT) != S_IFREG; |
| 98 | if (to_tempfile) { |
Jeff King | 330c8e2 | 2015-09-24 17:06:53 -0400 | [diff] [blame] | 99 | xsnprintf(path, TEMPORARY_FILENAME_LENGTH, "%s", |
| 100 | symlink ? ".merge_link_XXXXXX" : ".merge_file_XXXXXX"); |
Junio C Hamano | fd5db55 | 2011-05-12 21:36:42 -0700 | [diff] [blame] | 101 | return mkstemp(path); |
| 102 | } else { |
| 103 | return create_file(path, !symlink ? ce->ce_mode : 0666); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | static int fstat_output(int fd, const struct checkout *state, struct stat *st) |
| 108 | { |
| 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; |
| 116 | } |
| 117 | |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 118 | static int streaming_write_entry(const struct cache_entry *ce, char *path, |
Junio C Hamano | b669109 | 2011-05-20 14:33:31 -0700 | [diff] [blame] | 119 | struct stream_filter *filter, |
Junio C Hamano | dd8e912 | 2011-05-12 14:31:08 -0700 | [diff] [blame] | 120 | const struct checkout *state, int to_tempfile, |
| 121 | int *fstat_done, struct stat *statbuf) |
| 122 | { |
Jeff King | d9c31e1 | 2013-03-25 17:49:36 -0400 | [diff] [blame] | 123 | int result = 0; |
Junio C Hamano | 47a02ff | 2012-03-07 17:54:15 +0700 | [diff] [blame] | 124 | int fd; |
Junio C Hamano | dd8e912 | 2011-05-12 14:31:08 -0700 | [diff] [blame] | 125 | |
| 126 | fd = open_output_fd(path, ce, to_tempfile); |
Jeff King | d9c31e1 | 2013-03-25 17:49:36 -0400 | [diff] [blame] | 127 | if (fd < 0) |
| 128 | return -1; |
| 129 | |
| 130 | result |= stream_blob_to_fd(fd, ce->sha1, filter, 1); |
| 131 | *fstat_done = fstat_output(fd, state, statbuf); |
| 132 | result |= close(fd); |
| 133 | |
| 134 | if (result) |
Junio C Hamano | dd8e912 | 2011-05-12 14:31:08 -0700 | [diff] [blame] | 135 | unlink(path); |
| 136 | return result; |
| 137 | } |
| 138 | |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 139 | static int write_entry(struct cache_entry *ce, |
| 140 | char *path, const struct checkout *state, int to_tempfile) |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 141 | { |
Kjetil Barvik | 4857c76 | 2009-02-09 21:54:50 +0100 | [diff] [blame] | 142 | unsigned int ce_mode_s_ifmt = ce->ce_mode & S_IFMT; |
Kjetil Barvik | e4c7292 | 2009-02-09 21:54:51 +0100 | [diff] [blame] | 143 | int fd, ret, fstat_done = 0; |
Kjetil Barvik | 4857c76 | 2009-02-09 21:54:50 +0100 | [diff] [blame] | 144 | char *new; |
| 145 | struct strbuf buf = STRBUF_INIT; |
| 146 | unsigned long size; |
| 147 | size_t wrote, newsize = 0; |
Kjetil Barvik | e4c7292 | 2009-02-09 21:54:51 +0100 | [diff] [blame] | 148 | struct stat st; |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 149 | |
Junio C Hamano | b669109 | 2011-05-20 14:33:31 -0700 | [diff] [blame] | 150 | if (ce_mode_s_ifmt == S_IFREG) { |
John Keeping | 7297a44 | 2013-03-14 20:00:51 +0000 | [diff] [blame] | 151 | struct stream_filter *filter = get_stream_filter(ce->name, ce->sha1); |
Junio C Hamano | b669109 | 2011-05-20 14:33:31 -0700 | [diff] [blame] | 152 | if (filter && |
| 153 | !streaming_write_entry(ce, path, filter, |
| 154 | state, to_tempfile, |
| 155 | &fstat_done, &st)) |
| 156 | goto finish; |
| 157 | } |
Junio C Hamano | dd8e912 | 2011-05-12 14:31:08 -0700 | [diff] [blame] | 158 | |
Kjetil Barvik | 4857c76 | 2009-02-09 21:54:50 +0100 | [diff] [blame] | 159 | switch (ce_mode_s_ifmt) { |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 160 | case S_IFREG: |
Kjetil Barvik | 4857c76 | 2009-02-09 21:54:50 +0100 | [diff] [blame] | 161 | case S_IFLNK: |
| 162 | new = read_blob_entry(ce, &size); |
Linus Torvalds | f0807e6 | 2007-04-13 09:26:04 -0700 | [diff] [blame] | 163 | if (!new) |
Nguyễn Thái Ngọc Duy | d43e907 | 2010-11-28 11:36:38 +0700 | [diff] [blame] | 164 | return error("unable to read sha1 file of %s (%s)", |
Linus Torvalds | f0807e6 | 2007-04-13 09:26:04 -0700 | [diff] [blame] | 165 | path, sha1_to_hex(ce->sha1)); |
Junio C Hamano | 1a9d7e9 | 2007-08-14 01:41:02 -0700 | [diff] [blame] | 166 | |
Kjetil Barvik | 4857c76 | 2009-02-09 21:54:50 +0100 | [diff] [blame] | 167 | if (ce_mode_s_ifmt == S_IFLNK && has_symlinks && !to_tempfile) { |
| 168 | ret = symlink(new, path); |
| 169 | free(new); |
| 170 | if (ret) |
Nguyễn Thái Ngọc Duy | e1ebb3c | 2016-05-08 16:47:44 +0700 | [diff] [blame] | 171 | return error_errno("unable to create symlink %s", |
| 172 | path); |
Kjetil Barvik | 4857c76 | 2009-02-09 21:54:50 +0100 | [diff] [blame] | 173 | break; |
| 174 | } |
| 175 | |
Junio C Hamano | 1a9d7e9 | 2007-08-14 01:41:02 -0700 | [diff] [blame] | 176 | /* |
| 177 | * Convert from git internal format to working tree format |
| 178 | */ |
Kjetil Barvik | 4857c76 | 2009-02-09 21:54:50 +0100 | [diff] [blame] | 179 | if (ce_mode_s_ifmt == S_IFREG && |
| 180 | convert_to_working_tree(ce->name, new, size, &buf)) { |
Junio C Hamano | 1a9d7e9 | 2007-08-14 01:41:02 -0700 | [diff] [blame] | 181 | free(new); |
René Scharfe | c32f749 | 2007-10-21 11:23:49 +0200 | [diff] [blame] | 182 | new = strbuf_detach(&buf, &newsize); |
| 183 | size = newsize; |
Junio C Hamano | 1a9d7e9 | 2007-08-14 01:41:02 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Junio C Hamano | fd5db55 | 2011-05-12 21:36:42 -0700 | [diff] [blame] | 186 | fd = open_output_fd(path, ce, to_tempfile); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 187 | if (fd < 0) { |
| 188 | free(new); |
Nguyễn Thái Ngọc Duy | e1ebb3c | 2016-05-08 16:47:44 +0700 | [diff] [blame] | 189 | return error_errno("unable to create file %s", path); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 190 | } |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 191 | |
Andy Whitcroft | 93822c2 | 2007-01-08 15:58:23 +0000 | [diff] [blame] | 192 | wrote = write_in_full(fd, new, size); |
Junio C Hamano | fd5db55 | 2011-05-12 21:36:42 -0700 | [diff] [blame] | 193 | if (!to_tempfile) |
| 194 | fstat_done = fstat_output(fd, state, &st); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 195 | close(fd); |
| 196 | free(new); |
| 197 | if (wrote != size) |
Nguyễn Thái Ngọc Duy | d43e907 | 2010-11-28 11:36:38 +0700 | [diff] [blame] | 198 | return error("unable to write file %s", path); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 199 | break; |
Martin Waitz | 302b928 | 2007-05-21 22:08:28 +0200 | [diff] [blame] | 200 | case S_IFGITLINK: |
Linus Torvalds | f0807e6 | 2007-04-13 09:26:04 -0700 | [diff] [blame] | 201 | if (to_tempfile) |
Thomas Rast | 42063f9 | 2013-07-18 14:26:55 +0200 | [diff] [blame] | 202 | return error("cannot create temporary submodule %s", path); |
Linus Torvalds | f0807e6 | 2007-04-13 09:26:04 -0700 | [diff] [blame] | 203 | if (mkdir(path, 0777) < 0) |
Thomas Rast | 42063f9 | 2013-07-18 14:26:55 +0200 | [diff] [blame] | 204 | return error("cannot create submodule directory %s", path); |
Linus Torvalds | f0807e6 | 2007-04-13 09:26:04 -0700 | [diff] [blame] | 205 | break; |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 206 | default: |
Nguyễn Thái Ngọc Duy | d43e907 | 2010-11-28 11:36:38 +0700 | [diff] [blame] | 207 | return error("unknown file mode for %s in index", path); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Junio C Hamano | dd8e912 | 2011-05-12 14:31:08 -0700 | [diff] [blame] | 210 | finish: |
Linus Torvalds | 6ee67f2 | 2005-06-05 23:15:40 -0700 | [diff] [blame] | 211 | if (state->refresh_cache) { |
Nguyễn Thái Ngọc Duy | d4a2024 | 2014-06-13 19:19:34 +0700 | [diff] [blame] | 212 | assert(state->istate); |
Kjetil Barvik | e4c7292 | 2009-02-09 21:54:51 +0100 | [diff] [blame] | 213 | if (!fstat_done) |
| 214 | lstat(ce->name, &st); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 215 | fill_stat_cache_info(ce, &st); |
Nguyễn Thái Ngọc Duy | 078a58e | 2014-06-13 19:19:39 +0700 | [diff] [blame] | 216 | ce->ce_flags |= CE_UPDATE_IN_BASE; |
Nguyễn Thái Ngọc Duy | d4a2024 | 2014-06-13 19:19:34 +0700 | [diff] [blame] | 217 | state->istate->cache_changed |= CE_ENTRY_CHANGED; |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 218 | } |
| 219 | return 0; |
| 220 | } |
| 221 | |
Linus Torvalds | b6986d8 | 2009-07-29 20:22:25 -0700 | [diff] [blame] | 222 | /* |
| 223 | * This is like 'lstat()', except it refuses to follow symlinks |
Junio C Hamano | da02ca5 | 2009-08-16 23:53:12 -0700 | [diff] [blame] | 224 | * in the path, after skipping "skiplen". |
Linus Torvalds | b6986d8 | 2009-07-29 20:22:25 -0700 | [diff] [blame] | 225 | */ |
Junio C Hamano | 61b97df | 2010-01-11 22:27:31 -0800 | [diff] [blame] | 226 | static int check_path(const char *path, int len, struct stat *st, int skiplen) |
Linus Torvalds | b6986d8 | 2009-07-29 20:22:25 -0700 | [diff] [blame] | 227 | { |
Junio C Hamano | da02ca5 | 2009-08-16 23:53:12 -0700 | [diff] [blame] | 228 | const char *slash = path + len; |
| 229 | |
| 230 | while (path < slash && *slash != '/') |
| 231 | slash--; |
| 232 | if (!has_dirs_only_path(path, slash - path, skiplen)) { |
Linus Torvalds | b6986d8 | 2009-07-29 20:22:25 -0700 | [diff] [blame] | 233 | errno = ENOENT; |
| 234 | return -1; |
| 235 | } |
| 236 | return lstat(path, st); |
| 237 | } |
| 238 | |
Junio C Hamano | af2a651 | 2013-10-23 10:52:42 -0700 | [diff] [blame] | 239 | /* |
| 240 | * Write the contents from ce out to the working tree. |
| 241 | * |
| 242 | * When topath[] is not NULL, instead of writing to the working tree |
| 243 | * file named by ce, a temporary file is created by this function and |
| 244 | * its name is returned in topath[], which must be able to hold at |
| 245 | * least TEMPORARY_FILENAME_LENGTH bytes long. |
| 246 | */ |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 247 | int checkout_entry(struct cache_entry *ce, |
| 248 | const struct checkout *state, char *topath) |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 249 | { |
Michael Haggerty | f63272a | 2014-03-13 10:19:07 +0100 | [diff] [blame] | 250 | static struct strbuf path = STRBUF_INIT; |
Shawn Pearce | de84f99 | 2006-03-05 03:24:15 -0500 | [diff] [blame] | 251 | struct stat st; |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 252 | |
Shawn Pearce | de84f99 | 2006-03-05 03:24:15 -0500 | [diff] [blame] | 253 | if (topath) |
| 254 | return write_entry(ce, topath, state, 1); |
| 255 | |
Michael Haggerty | f63272a | 2014-03-13 10:19:07 +0100 | [diff] [blame] | 256 | strbuf_reset(&path); |
| 257 | strbuf_add(&path, state->base_dir, state->base_dir_len); |
| 258 | strbuf_add(&path, ce->name, ce_namelen(ce)); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 259 | |
Michael Haggerty | f63272a | 2014-03-13 10:19:07 +0100 | [diff] [blame] | 260 | if (!check_path(path.buf, path.len, &st, state->base_dir_len)) { |
Nguyễn Thái Ngọc Duy | 56cac48 | 2009-12-14 18:43:58 +0700 | [diff] [blame] | 261 | unsigned changed = ce_match_stat(ce, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 262 | if (!changed) |
| 263 | return 0; |
| 264 | if (!state->force) { |
| 265 | if (!state->quiet) |
Michael Haggerty | f63272a | 2014-03-13 10:19:07 +0100 | [diff] [blame] | 266 | fprintf(stderr, |
| 267 | "%s already exists, no checkout\n", |
| 268 | path.buf); |
Junio C Hamano | 4b12dae | 2005-10-03 12:44:48 -0700 | [diff] [blame] | 269 | return -1; |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | /* |
| 273 | * We unlink the old file, to get the new one with the |
| 274 | * right permissions (including umask, which is nasty |
| 275 | * to emulate by hand - much easier to let the system |
| 276 | * just do the right thing) |
| 277 | */ |
Linus Torvalds | d48a72f | 2005-07-14 09:58:45 -0700 | [diff] [blame] | 278 | if (S_ISDIR(st.st_mode)) { |
Linus Torvalds | f0807e6 | 2007-04-13 09:26:04 -0700 | [diff] [blame] | 279 | /* If it is a gitlink, leave it alone! */ |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 280 | if (S_ISGITLINK(ce->ce_mode)) |
Linus Torvalds | f0807e6 | 2007-04-13 09:26:04 -0700 | [diff] [blame] | 281 | return 0; |
Linus Torvalds | d48a72f | 2005-07-14 09:58:45 -0700 | [diff] [blame] | 282 | if (!state->force) |
Michael Haggerty | f63272a | 2014-03-13 10:19:07 +0100 | [diff] [blame] | 283 | return error("%s is a directory", path.buf); |
Michael Haggerty | 2f29e0c | 2014-03-13 10:19:08 +0100 | [diff] [blame] | 284 | remove_subtree(&path); |
Michael Haggerty | f63272a | 2014-03-13 10:19:07 +0100 | [diff] [blame] | 285 | } else if (unlink(path.buf)) |
Nguyễn Thái Ngọc Duy | e1ebb3c | 2016-05-08 16:47:44 +0700 | [diff] [blame] | 286 | return error_errno("unable to unlink old '%s'", path.buf); |
Shawn Pearce | de84f99 | 2006-03-05 03:24:15 -0500 | [diff] [blame] | 287 | } else if (state->not_new) |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 288 | return 0; |
Michael Haggerty | f63272a | 2014-03-13 10:19:07 +0100 | [diff] [blame] | 289 | |
| 290 | create_directories(path.buf, path.len, state); |
| 291 | return write_entry(ce, path.buf, state, 0); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 292 | } |