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" |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 3 | |
Luiz Fernando N. Capitulino | efbc583 | 2007-04-25 11:18:08 -0300 | [diff] [blame] | 4 | static void create_directories(const char *path, const struct checkout *state) |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 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) { |
Junio C Hamano | fa2e71c | 2007-07-17 22:58:28 -0700 | [diff] [blame] | 11 | struct stat st; |
| 12 | int stat_status; |
| 13 | |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 14 | len = slash - path; |
| 15 | memcpy(buf, path, len); |
| 16 | buf[len] = 0; |
Junio C Hamano | fa2e71c | 2007-07-17 22:58:28 -0700 | [diff] [blame] | 17 | |
| 18 | if (len <= state->base_dir_len) |
| 19 | /* |
| 20 | * checkout-index --prefix=<dir>; <dir> is |
| 21 | * allowed to be a symlink to an existing |
| 22 | * directory. |
| 23 | */ |
| 24 | stat_status = stat(buf, &st); |
| 25 | else |
| 26 | /* |
| 27 | * if there currently is a symlink, we would |
| 28 | * want to replace it with a real directory. |
| 29 | */ |
| 30 | stat_status = lstat(buf, &st); |
| 31 | |
| 32 | if (!stat_status && S_ISDIR(st.st_mode)) |
| 33 | continue; /* ok, it is already a directory. */ |
| 34 | |
| 35 | /* |
| 36 | * We know stat_status == 0 means something exists |
| 37 | * there and this mkdir would fail, but that is an |
| 38 | * error codepath; we do not care, as we unlink and |
| 39 | * mkdir again in such a case. |
| 40 | */ |
Junio C Hamano | f312de0 | 2005-07-06 01:21:46 -0700 | [diff] [blame] | 41 | if (mkdir(buf, 0777)) { |
Junio C Hamano | fa2e71c | 2007-07-17 22:58:28 -0700 | [diff] [blame] | 42 | if (errno == EEXIST && state->force && |
| 43 | !unlink(buf) && !mkdir(buf, 0777)) |
| 44 | continue; |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 45 | die("cannot create directory at %s", buf); |
| 46 | } |
| 47 | } |
| 48 | free(buf); |
| 49 | } |
| 50 | |
| 51 | static void remove_subtree(const char *path) |
| 52 | { |
| 53 | DIR *dir = opendir(path); |
| 54 | struct dirent *de; |
| 55 | char pathbuf[PATH_MAX]; |
| 56 | char *name; |
Junio C Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 57 | |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 58 | if (!dir) |
Luiz Fernando N. Capitulino | cc2903f | 2007-04-25 11:17:56 -0300 | [diff] [blame] | 59 | die("cannot opendir %s (%s)", path, strerror(errno)); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 60 | strcpy(pathbuf, path); |
| 61 | name = pathbuf + strlen(path); |
| 62 | *name++ = '/'; |
| 63 | while ((de = readdir(dir)) != NULL) { |
| 64 | struct stat st; |
| 65 | if ((de->d_name[0] == '.') && |
| 66 | ((de->d_name[1] == 0) || |
| 67 | ((de->d_name[1] == '.') && de->d_name[2] == 0))) |
| 68 | continue; |
| 69 | strcpy(name, de->d_name); |
| 70 | if (lstat(pathbuf, &st)) |
Luiz Fernando N. Capitulino | cc2903f | 2007-04-25 11:17:56 -0300 | [diff] [blame] | 71 | die("cannot lstat %s (%s)", pathbuf, strerror(errno)); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 72 | if (S_ISDIR(st.st_mode)) |
| 73 | remove_subtree(pathbuf); |
| 74 | else if (unlink(pathbuf)) |
Luiz Fernando N. Capitulino | cc2903f | 2007-04-25 11:17:56 -0300 | [diff] [blame] | 75 | die("cannot unlink %s (%s)", pathbuf, strerror(errno)); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 76 | } |
| 77 | closedir(dir); |
| 78 | if (rmdir(path)) |
Luiz Fernando N. Capitulino | cc2903f | 2007-04-25 11:17:56 -0300 | [diff] [blame] | 79 | die("cannot rmdir %s (%s)", path, strerror(errno)); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Linus Torvalds | d48a72f | 2005-07-14 09:58:45 -0700 | [diff] [blame] | 82 | static int create_file(const char *path, unsigned int mode) |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 83 | { |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 84 | mode = (mode & 0100) ? 0777 : 0666; |
Alex Riesen | 781411e | 2006-01-05 09:58:06 +0100 | [diff] [blame] | 85 | return open(path, O_WRONLY | O_CREAT | O_EXCL, mode); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Linus Torvalds | f0807e6 | 2007-04-13 09:26:04 -0700 | [diff] [blame] | 88 | static void *read_blob_entry(struct cache_entry *ce, const char *path, unsigned long *size) |
| 89 | { |
| 90 | enum object_type type; |
| 91 | void *new = read_sha1_file(ce->sha1, &type, size); |
| 92 | |
| 93 | if (new) { |
| 94 | if (type == OBJ_BLOB) |
| 95 | return new; |
| 96 | free(new); |
| 97 | } |
| 98 | return NULL; |
| 99 | } |
| 100 | |
Luiz Fernando N. Capitulino | efbc583 | 2007-04-25 11:18:08 -0300 | [diff] [blame] | 101 | 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] | 102 | { |
| 103 | int fd; |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 104 | long wrote; |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 105 | |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 106 | switch (ntohl(ce->ce_mode) & S_IFMT) { |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 107 | char *new; |
| 108 | struct strbuf buf; |
Junio C Hamano | a2d7c6c | 2007-04-21 17:38:00 -0700 | [diff] [blame] | 109 | unsigned long size; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 110 | |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 111 | case S_IFREG: |
Linus Torvalds | f0807e6 | 2007-04-13 09:26:04 -0700 | [diff] [blame] | 112 | new = read_blob_entry(ce, path, &size); |
| 113 | if (!new) |
| 114 | return error("git-checkout-index: unable to read sha1 file of %s (%s)", |
| 115 | path, sha1_to_hex(ce->sha1)); |
Junio C Hamano | 1a9d7e9 | 2007-08-14 01:41:02 -0700 | [diff] [blame] | 116 | |
| 117 | /* |
| 118 | * Convert from git internal format to working tree format |
| 119 | */ |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 120 | strbuf_init(&buf, 0); |
| 121 | if (convert_to_working_tree(ce->name, new, size, &buf)) { |
René Scharfe | c32f749 | 2007-10-21 11:23:49 +0200 | [diff] [blame] | 122 | size_t newsize = 0; |
Junio C Hamano | 1a9d7e9 | 2007-08-14 01:41:02 -0700 | [diff] [blame] | 123 | free(new); |
René Scharfe | c32f749 | 2007-10-21 11:23:49 +0200 | [diff] [blame] | 124 | new = strbuf_detach(&buf, &newsize); |
| 125 | size = newsize; |
Junio C Hamano | 1a9d7e9 | 2007-08-14 01:41:02 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Shawn Pearce | de84f99 | 2006-03-05 03:24:15 -0500 | [diff] [blame] | 128 | if (to_tempfile) { |
| 129 | strcpy(path, ".merge_file_XXXXXX"); |
| 130 | fd = mkstemp(path); |
| 131 | } else |
| 132 | fd = create_file(path, ntohl(ce->ce_mode)); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 133 | if (fd < 0) { |
| 134 | free(new); |
Junio C Hamano | 215a7ad | 2005-09-07 17:26:23 -0700 | [diff] [blame] | 135 | return error("git-checkout-index: unable to create file %s (%s)", |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 136 | path, strerror(errno)); |
| 137 | } |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 138 | |
Andy Whitcroft | 93822c2 | 2007-01-08 15:58:23 +0000 | [diff] [blame] | 139 | wrote = write_in_full(fd, new, size); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 140 | close(fd); |
| 141 | free(new); |
| 142 | if (wrote != size) |
Junio C Hamano | 215a7ad | 2005-09-07 17:26:23 -0700 | [diff] [blame] | 143 | return error("git-checkout-index: unable to write file %s", path); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 144 | break; |
| 145 | case S_IFLNK: |
Linus Torvalds | f0807e6 | 2007-04-13 09:26:04 -0700 | [diff] [blame] | 146 | new = read_blob_entry(ce, path, &size); |
| 147 | if (!new) |
| 148 | return error("git-checkout-index: unable to read sha1 file of %s (%s)", |
| 149 | path, sha1_to_hex(ce->sha1)); |
Johannes Sixt | 78a8d64 | 2007-03-02 22:11:30 +0100 | [diff] [blame] | 150 | if (to_tempfile || !has_symlinks) { |
| 151 | if (to_tempfile) { |
| 152 | strcpy(path, ".merge_link_XXXXXX"); |
| 153 | fd = mkstemp(path); |
| 154 | } else |
| 155 | fd = create_file(path, 0666); |
Shawn Pearce | de84f99 | 2006-03-05 03:24:15 -0500 | [diff] [blame] | 156 | if (fd < 0) { |
| 157 | free(new); |
| 158 | return error("git-checkout-index: unable to create " |
| 159 | "file %s (%s)", path, strerror(errno)); |
| 160 | } |
Andy Whitcroft | 93822c2 | 2007-01-08 15:58:23 +0000 | [diff] [blame] | 161 | wrote = write_in_full(fd, new, size); |
Shawn Pearce | de84f99 | 2006-03-05 03:24:15 -0500 | [diff] [blame] | 162 | close(fd); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 163 | free(new); |
Shawn Pearce | de84f99 | 2006-03-05 03:24:15 -0500 | [diff] [blame] | 164 | if (wrote != size) |
| 165 | return error("git-checkout-index: unable to write file %s", |
| 166 | path); |
| 167 | } else { |
| 168 | wrote = symlink(new, path); |
| 169 | free(new); |
| 170 | if (wrote) |
| 171 | return error("git-checkout-index: unable to create " |
| 172 | "symlink %s (%s)", path, strerror(errno)); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 173 | } |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 174 | break; |
Martin Waitz | 302b928 | 2007-05-21 22:08:28 +0200 | [diff] [blame] | 175 | case S_IFGITLINK: |
Linus Torvalds | f0807e6 | 2007-04-13 09:26:04 -0700 | [diff] [blame] | 176 | if (to_tempfile) |
| 177 | return error("git-checkout-index: cannot create temporary subproject %s", path); |
| 178 | if (mkdir(path, 0777) < 0) |
| 179 | return error("git-checkout-index: cannot create subproject directory %s", path); |
| 180 | break; |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 181 | default: |
Junio C Hamano | 215a7ad | 2005-09-07 17:26:23 -0700 | [diff] [blame] | 182 | return error("git-checkout-index: unknown file mode for %s", path); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Linus Torvalds | 6ee67f2 | 2005-06-05 23:15:40 -0700 | [diff] [blame] | 185 | if (state->refresh_cache) { |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 186 | struct stat st; |
| 187 | lstat(ce->name, &st); |
| 188 | fill_stat_cache_info(ce, &st); |
| 189 | } |
| 190 | return 0; |
| 191 | } |
| 192 | |
Luiz Fernando N. Capitulino | efbc583 | 2007-04-25 11:18:08 -0300 | [diff] [blame] | 193 | 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] | 194 | { |
Jonas Fonseca | 095c424 | 2006-08-26 16:09:17 +0200 | [diff] [blame] | 195 | static char path[PATH_MAX + 1]; |
Shawn Pearce | de84f99 | 2006-03-05 03:24:15 -0500 | [diff] [blame] | 196 | struct stat st; |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 197 | int len = state->base_dir_len; |
| 198 | |
Shawn Pearce | de84f99 | 2006-03-05 03:24:15 -0500 | [diff] [blame] | 199 | if (topath) |
| 200 | return write_entry(ce, topath, state, 1); |
| 201 | |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 202 | memcpy(path, state->base_dir, len); |
| 203 | strcpy(path + len, ce->name); |
| 204 | |
| 205 | if (!lstat(path, &st)) { |
Junio C Hamano | 4bd5b7d | 2007-11-10 00:15:03 -0800 | [diff] [blame] | 206 | unsigned changed = ce_match_stat(ce, &st, CE_MATCH_IGNORE_VALID); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 207 | if (!changed) |
| 208 | return 0; |
| 209 | if (!state->force) { |
| 210 | if (!state->quiet) |
Junio C Hamano | 215a7ad | 2005-09-07 17:26:23 -0700 | [diff] [blame] | 211 | fprintf(stderr, "git-checkout-index: %s already exists\n", path); |
Junio C Hamano | 4b12dae | 2005-10-03 12:44:48 -0700 | [diff] [blame] | 212 | return -1; |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /* |
| 216 | * We unlink the old file, to get the new one with the |
| 217 | * right permissions (including umask, which is nasty |
| 218 | * to emulate by hand - much easier to let the system |
| 219 | * just do the right thing) |
| 220 | */ |
| 221 | unlink(path); |
Linus Torvalds | d48a72f | 2005-07-14 09:58:45 -0700 | [diff] [blame] | 222 | if (S_ISDIR(st.st_mode)) { |
Linus Torvalds | f0807e6 | 2007-04-13 09:26:04 -0700 | [diff] [blame] | 223 | /* If it is a gitlink, leave it alone! */ |
Martin Waitz | 302b928 | 2007-05-21 22:08:28 +0200 | [diff] [blame] | 224 | if (S_ISGITLINK(ntohl(ce->ce_mode))) |
Linus Torvalds | f0807e6 | 2007-04-13 09:26:04 -0700 | [diff] [blame] | 225 | return 0; |
Linus Torvalds | d48a72f | 2005-07-14 09:58:45 -0700 | [diff] [blame] | 226 | if (!state->force) |
| 227 | return error("%s is a directory", path); |
| 228 | remove_subtree(path); |
| 229 | } |
Shawn Pearce | de84f99 | 2006-03-05 03:24:15 -0500 | [diff] [blame] | 230 | } else if (state->not_new) |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 231 | return 0; |
| 232 | create_directories(path, state); |
Shawn Pearce | de84f99 | 2006-03-05 03:24:15 -0500 | [diff] [blame] | 233 | return write_entry(ce, path, state, 0); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 234 | } |