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 | { |
Kjetil Barvik | 81a9aa6 | 2009-02-09 21:54:08 +0100 | [diff] [blame] | 9 | char *buf = xmalloc(path_len + 1); |
| 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 | |
| 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; |
Junio C Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 53 | |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 54 | if (!dir) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 55 | die_errno("cannot opendir '%s'", path); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 56 | strcpy(pathbuf, path); |
| 57 | name = pathbuf + strlen(path); |
| 58 | *name++ = '/'; |
| 59 | while ((de = readdir(dir)) != NULL) { |
| 60 | struct stat st; |
Alexander Potashev | 8ca12c0 | 2009-01-10 15:07:50 +0300 | [diff] [blame] | 61 | if (is_dot_or_dotdot(de->d_name)) |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 62 | continue; |
| 63 | strcpy(name, de->d_name); |
| 64 | if (lstat(pathbuf, &st)) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 65 | die_errno("cannot lstat '%s'", pathbuf); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 66 | if (S_ISDIR(st.st_mode)) |
| 67 | remove_subtree(pathbuf); |
| 68 | else if (unlink(pathbuf)) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 69 | die_errno("cannot unlink '%s'", pathbuf); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 70 | } |
| 71 | closedir(dir); |
| 72 | if (rmdir(path)) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 73 | die_errno("cannot rmdir '%s'", path); |
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 | |
Kjetil Barvik | 4857c76 | 2009-02-09 21:54:50 +0100 | [diff] [blame] | 82 | static void *read_blob_entry(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 | |
Junio C Hamano | fd5db55 | 2011-05-12 21:36:42 -0700 | [diff] [blame] | 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 | } |
| 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 | |
Junio C Hamano | dd8e912 | 2011-05-12 14:31:08 -0700 | [diff] [blame] | 118 | static int streaming_write_entry(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 | { |
| 123 | struct git_istream *st; |
| 124 | enum object_type type; |
| 125 | unsigned long sz; |
| 126 | int result = -1; |
Junio C Hamano | de6182d | 2011-05-13 15:55:00 -0700 | [diff] [blame] | 127 | ssize_t kept = 0; |
Junio C Hamano | dd8e912 | 2011-05-12 14:31:08 -0700 | [diff] [blame] | 128 | int fd = -1; |
| 129 | |
Junio C Hamano | b669109 | 2011-05-20 14:33:31 -0700 | [diff] [blame] | 130 | st = open_istream(ce->sha1, &type, &sz, filter); |
Junio C Hamano | dd8e912 | 2011-05-12 14:31:08 -0700 | [diff] [blame] | 131 | if (!st) |
| 132 | return -1; |
| 133 | if (type != OBJ_BLOB) |
| 134 | goto close_and_exit; |
| 135 | |
| 136 | fd = open_output_fd(path, ce, to_tempfile); |
| 137 | if (fd < 0) |
| 138 | goto close_and_exit; |
| 139 | |
| 140 | for (;;) { |
Junio C Hamano | de6182d | 2011-05-13 15:55:00 -0700 | [diff] [blame] | 141 | char buf[1024 * 16]; |
| 142 | ssize_t wrote, holeto; |
Junio C Hamano | dd8e912 | 2011-05-12 14:31:08 -0700 | [diff] [blame] | 143 | ssize_t readlen = read_istream(st, buf, sizeof(buf)); |
| 144 | |
| 145 | if (!readlen) |
| 146 | break; |
Junio C Hamano | de6182d | 2011-05-13 15:55:00 -0700 | [diff] [blame] | 147 | if (sizeof(buf) == readlen) { |
| 148 | for (holeto = 0; holeto < readlen; holeto++) |
| 149 | if (buf[holeto]) |
| 150 | break; |
| 151 | if (readlen == holeto) { |
| 152 | kept += holeto; |
| 153 | continue; |
| 154 | } |
| 155 | } |
Junio C Hamano | dd8e912 | 2011-05-12 14:31:08 -0700 | [diff] [blame] | 156 | |
Junio C Hamano | de6182d | 2011-05-13 15:55:00 -0700 | [diff] [blame] | 157 | if (kept && lseek(fd, kept, SEEK_CUR) == (off_t) -1) |
| 158 | goto close_and_exit; |
| 159 | else |
| 160 | kept = 0; |
Junio C Hamano | dd8e912 | 2011-05-12 14:31:08 -0700 | [diff] [blame] | 161 | wrote = write_in_full(fd, buf, readlen); |
| 162 | |
| 163 | if (wrote != readlen) |
| 164 | goto close_and_exit; |
| 165 | } |
Junio C Hamano | de6182d | 2011-05-13 15:55:00 -0700 | [diff] [blame] | 166 | if (kept && (lseek(fd, kept - 1, SEEK_CUR) == (off_t) -1 || |
| 167 | write(fd, "", 1) != 1)) |
| 168 | goto close_and_exit; |
Junio C Hamano | dd8e912 | 2011-05-12 14:31:08 -0700 | [diff] [blame] | 169 | *fstat_done = fstat_output(fd, state, statbuf); |
| 170 | |
| 171 | close_and_exit: |
| 172 | close_istream(st); |
| 173 | if (0 <= fd) |
| 174 | result = close(fd); |
| 175 | if (result && 0 <= fd) |
| 176 | unlink(path); |
| 177 | return result; |
| 178 | } |
| 179 | |
Luiz Fernando N. Capitulino | efbc583 | 2007-04-25 11:18:08 -0300 | [diff] [blame] | 180 | static int write_entry(struct cache_entry *ce, char *path, const struct checkout *state, int to_tempfile) |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 181 | { |
Kjetil Barvik | 4857c76 | 2009-02-09 21:54:50 +0100 | [diff] [blame] | 182 | unsigned int ce_mode_s_ifmt = ce->ce_mode & S_IFMT; |
Kjetil Barvik | e4c7292 | 2009-02-09 21:54:51 +0100 | [diff] [blame] | 183 | int fd, ret, fstat_done = 0; |
Kjetil Barvik | 4857c76 | 2009-02-09 21:54:50 +0100 | [diff] [blame] | 184 | char *new; |
| 185 | struct strbuf buf = STRBUF_INIT; |
| 186 | unsigned long size; |
| 187 | size_t wrote, newsize = 0; |
Kjetil Barvik | e4c7292 | 2009-02-09 21:54:51 +0100 | [diff] [blame] | 188 | struct stat st; |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 189 | |
Junio C Hamano | b669109 | 2011-05-20 14:33:31 -0700 | [diff] [blame] | 190 | if (ce_mode_s_ifmt == S_IFREG) { |
| 191 | struct stream_filter *filter = get_stream_filter(path, ce->sha1); |
| 192 | if (filter && |
| 193 | !streaming_write_entry(ce, path, filter, |
| 194 | state, to_tempfile, |
| 195 | &fstat_done, &st)) |
| 196 | goto finish; |
| 197 | } |
Junio C Hamano | dd8e912 | 2011-05-12 14:31:08 -0700 | [diff] [blame] | 198 | |
Kjetil Barvik | 4857c76 | 2009-02-09 21:54:50 +0100 | [diff] [blame] | 199 | switch (ce_mode_s_ifmt) { |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 200 | case S_IFREG: |
Kjetil Barvik | 4857c76 | 2009-02-09 21:54:50 +0100 | [diff] [blame] | 201 | case S_IFLNK: |
| 202 | new = read_blob_entry(ce, &size); |
Linus Torvalds | f0807e6 | 2007-04-13 09:26:04 -0700 | [diff] [blame] | 203 | if (!new) |
Nguyễn Thái Ngọc Duy | d43e907 | 2010-11-28 11:36:38 +0700 | [diff] [blame] | 204 | return error("unable to read sha1 file of %s (%s)", |
Linus Torvalds | f0807e6 | 2007-04-13 09:26:04 -0700 | [diff] [blame] | 205 | path, sha1_to_hex(ce->sha1)); |
Junio C Hamano | 1a9d7e9 | 2007-08-14 01:41:02 -0700 | [diff] [blame] | 206 | |
Kjetil Barvik | 4857c76 | 2009-02-09 21:54:50 +0100 | [diff] [blame] | 207 | if (ce_mode_s_ifmt == S_IFLNK && has_symlinks && !to_tempfile) { |
| 208 | ret = symlink(new, path); |
| 209 | free(new); |
| 210 | if (ret) |
Nguyễn Thái Ngọc Duy | d43e907 | 2010-11-28 11:36:38 +0700 | [diff] [blame] | 211 | return error("unable to create symlink %s (%s)", |
Kjetil Barvik | 4857c76 | 2009-02-09 21:54:50 +0100 | [diff] [blame] | 212 | path, strerror(errno)); |
| 213 | break; |
| 214 | } |
| 215 | |
Junio C Hamano | 1a9d7e9 | 2007-08-14 01:41:02 -0700 | [diff] [blame] | 216 | /* |
| 217 | * Convert from git internal format to working tree format |
| 218 | */ |
Kjetil Barvik | 4857c76 | 2009-02-09 21:54:50 +0100 | [diff] [blame] | 219 | if (ce_mode_s_ifmt == S_IFREG && |
| 220 | convert_to_working_tree(ce->name, new, size, &buf)) { |
Junio C Hamano | 1a9d7e9 | 2007-08-14 01:41:02 -0700 | [diff] [blame] | 221 | free(new); |
René Scharfe | c32f749 | 2007-10-21 11:23:49 +0200 | [diff] [blame] | 222 | new = strbuf_detach(&buf, &newsize); |
| 223 | size = newsize; |
Junio C Hamano | 1a9d7e9 | 2007-08-14 01:41:02 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Junio C Hamano | fd5db55 | 2011-05-12 21:36:42 -0700 | [diff] [blame] | 226 | fd = open_output_fd(path, ce, to_tempfile); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 227 | if (fd < 0) { |
| 228 | free(new); |
Nguyễn Thái Ngọc Duy | d43e907 | 2010-11-28 11:36:38 +0700 | [diff] [blame] | 229 | return error("unable to create file %s (%s)", |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 230 | path, strerror(errno)); |
| 231 | } |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 232 | |
Andy Whitcroft | 93822c2 | 2007-01-08 15:58:23 +0000 | [diff] [blame] | 233 | wrote = write_in_full(fd, new, size); |
Junio C Hamano | fd5db55 | 2011-05-12 21:36:42 -0700 | [diff] [blame] | 234 | if (!to_tempfile) |
| 235 | fstat_done = fstat_output(fd, state, &st); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 236 | close(fd); |
| 237 | free(new); |
| 238 | if (wrote != size) |
Nguyễn Thái Ngọc Duy | d43e907 | 2010-11-28 11:36:38 +0700 | [diff] [blame] | 239 | return error("unable to write file %s", path); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 240 | break; |
Martin Waitz | 302b928 | 2007-05-21 22:08:28 +0200 | [diff] [blame] | 241 | case S_IFGITLINK: |
Linus Torvalds | f0807e6 | 2007-04-13 09:26:04 -0700 | [diff] [blame] | 242 | if (to_tempfile) |
Nguyễn Thái Ngọc Duy | d43e907 | 2010-11-28 11:36:38 +0700 | [diff] [blame] | 243 | return error("cannot create temporary subproject %s", path); |
Linus Torvalds | f0807e6 | 2007-04-13 09:26:04 -0700 | [diff] [blame] | 244 | if (mkdir(path, 0777) < 0) |
Nguyễn Thái Ngọc Duy | d43e907 | 2010-11-28 11:36:38 +0700 | [diff] [blame] | 245 | return error("cannot create subproject directory %s", path); |
Linus Torvalds | f0807e6 | 2007-04-13 09:26:04 -0700 | [diff] [blame] | 246 | break; |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 247 | default: |
Nguyễn Thái Ngọc Duy | d43e907 | 2010-11-28 11:36:38 +0700 | [diff] [blame] | 248 | return error("unknown file mode for %s in index", path); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Junio C Hamano | dd8e912 | 2011-05-12 14:31:08 -0700 | [diff] [blame] | 251 | finish: |
Linus Torvalds | 6ee67f2 | 2005-06-05 23:15:40 -0700 | [diff] [blame] | 252 | if (state->refresh_cache) { |
Kjetil Barvik | e4c7292 | 2009-02-09 21:54:51 +0100 | [diff] [blame] | 253 | if (!fstat_done) |
| 254 | lstat(ce->name, &st); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 255 | fill_stat_cache_info(ce, &st); |
| 256 | } |
| 257 | return 0; |
| 258 | } |
| 259 | |
Linus Torvalds | b6986d8 | 2009-07-29 20:22:25 -0700 | [diff] [blame] | 260 | /* |
| 261 | * This is like 'lstat()', except it refuses to follow symlinks |
Junio C Hamano | da02ca5 | 2009-08-16 23:53:12 -0700 | [diff] [blame] | 262 | * in the path, after skipping "skiplen". |
Linus Torvalds | b6986d8 | 2009-07-29 20:22:25 -0700 | [diff] [blame] | 263 | */ |
Junio C Hamano | 61b97df | 2010-01-11 22:27:31 -0800 | [diff] [blame] | 264 | 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] | 265 | { |
Junio C Hamano | da02ca5 | 2009-08-16 23:53:12 -0700 | [diff] [blame] | 266 | const char *slash = path + len; |
| 267 | |
| 268 | while (path < slash && *slash != '/') |
| 269 | slash--; |
| 270 | if (!has_dirs_only_path(path, slash - path, skiplen)) { |
Linus Torvalds | b6986d8 | 2009-07-29 20:22:25 -0700 | [diff] [blame] | 271 | errno = ENOENT; |
| 272 | return -1; |
| 273 | } |
| 274 | return lstat(path, st); |
| 275 | } |
| 276 | |
Luiz Fernando N. Capitulino | efbc583 | 2007-04-25 11:18:08 -0300 | [diff] [blame] | 277 | int checkout_entry(struct cache_entry *ce, const struct checkout *state, char *topath) |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 278 | { |
Jonas Fonseca | 095c424 | 2006-08-26 16:09:17 +0200 | [diff] [blame] | 279 | static char path[PATH_MAX + 1]; |
Shawn Pearce | de84f99 | 2006-03-05 03:24:15 -0500 | [diff] [blame] | 280 | struct stat st; |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 281 | int len = state->base_dir_len; |
| 282 | |
Shawn Pearce | de84f99 | 2006-03-05 03:24:15 -0500 | [diff] [blame] | 283 | if (topath) |
| 284 | return write_entry(ce, topath, state, 1); |
| 285 | |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 286 | memcpy(path, state->base_dir, len); |
| 287 | strcpy(path + len, ce->name); |
Kjetil Barvik | 81a9aa6 | 2009-02-09 21:54:08 +0100 | [diff] [blame] | 288 | len += ce_namelen(ce); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 289 | |
Junio C Hamano | da02ca5 | 2009-08-16 23:53:12 -0700 | [diff] [blame] | 290 | if (!check_path(path, len, &st, state->base_dir_len)) { |
Nguyễn Thái Ngọc Duy | 56cac48 | 2009-12-14 18:43:58 +0700 | [diff] [blame] | 291 | 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] | 292 | if (!changed) |
| 293 | return 0; |
| 294 | if (!state->force) { |
| 295 | if (!state->quiet) |
Nguyễn Thái Ngọc Duy | d43e907 | 2010-11-28 11:36:38 +0700 | [diff] [blame] | 296 | fprintf(stderr, "%s already exists, no checkout\n", path); |
Junio C Hamano | 4b12dae | 2005-10-03 12:44:48 -0700 | [diff] [blame] | 297 | return -1; |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | /* |
| 301 | * We unlink the old file, to get the new one with the |
| 302 | * right permissions (including umask, which is nasty |
| 303 | * to emulate by hand - much easier to let the system |
| 304 | * just do the right thing) |
| 305 | */ |
Linus Torvalds | d48a72f | 2005-07-14 09:58:45 -0700 | [diff] [blame] | 306 | if (S_ISDIR(st.st_mode)) { |
Linus Torvalds | f0807e6 | 2007-04-13 09:26:04 -0700 | [diff] [blame] | 307 | /* If it is a gitlink, leave it alone! */ |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 308 | if (S_ISGITLINK(ce->ce_mode)) |
Linus Torvalds | f0807e6 | 2007-04-13 09:26:04 -0700 | [diff] [blame] | 309 | return 0; |
Linus Torvalds | d48a72f | 2005-07-14 09:58:45 -0700 | [diff] [blame] | 310 | if (!state->force) |
| 311 | return error("%s is a directory", path); |
| 312 | remove_subtree(path); |
Linus Torvalds | 971f229 | 2008-03-17 08:56:27 -0700 | [diff] [blame] | 313 | } else if (unlink(path)) |
| 314 | return error("unable to unlink old '%s' (%s)", path, strerror(errno)); |
Shawn Pearce | de84f99 | 2006-03-05 03:24:15 -0500 | [diff] [blame] | 315 | } else if (state->not_new) |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 316 | return 0; |
Kjetil Barvik | 81a9aa6 | 2009-02-09 21:54:08 +0100 | [diff] [blame] | 317 | create_directories(path, len, state); |
Shawn Pearce | de84f99 | 2006-03-05 03:24:15 -0500 | [diff] [blame] | 318 | return write_entry(ce, path, state, 0); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 319 | } |